Fix Div integer semantics to use trunc division#7585
Conversation
Signed-off-by: yash solanki <alphacr792@gmail.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #7585 +/- ##
==========================================
- Coverage 55.25% 55.24% -0.01%
==========================================
Files 515 515
Lines 32349 32356 +7
Branches 2894 2894
==========================================
+ Hits 17873 17876 +3
- Misses 13686 13690 +4
Partials 790 790 ☔ View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Pull request overview
This PR fixes the integer division semantics in ONNX to use floor division (rounding toward negative infinity) instead of potentially using truncation division. The change ensures consistency with NumPy's floor division operator and adds comprehensive documentation and tests.
Changes:
- Updated reference implementation to use
np.floor_divide()for integer division - Added regression tests verifying floor division behavior with negative numbers
- Documented integer division semantics across all opset versions (1, 6, 7, 13, 14)
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| onnx/reference/ops/op_div.py | Changed integer division implementation to explicitly use np.floor_divide() |
| onnx/test/reference_evaluator_test.py | Added test case verifying floor division behavior with negative integers |
| onnx/backend/test/case/node/div.py | Added test case with specific negative number combinations to verify floor division |
| onnx/defs/math/defs.cc | Added documentation for integer floor division in opset 14 schema |
| onnx/defs/math/old.cc | Added documentation for integer floor division in opsets 6, 7, and 13 schemas |
| docs/Operators.md | Updated generated documentation to include floor division description |
|
any thoughts? @titaiwangms |
Signed-off-by: yash solanki <alphacr792@gmail.com>
|
cc @fdwr @yuanyao-nv |
|
@MagellaX Please also fix lint, thanks |
Signed-off-by: yash solanki <alphacr792@gmail.com>
Err, I don't think we should change Div's behavior for integers which existed for many many years as truncation toward zero (you'll break lots of EPs). Looking at spec commit 49e4798, that was a breaking change o_o (note this will also impact WebNN which uses truncating division and calls via Chromium into ORT). If we want a |
Thanks for the clarification. I’ve kept integer Div as truncation‑toward‑zero (no behavioral change) and updated the docs accordingly. The reference implementation now uses an integer‑only truncation path (no np.trunc(x / y) to avoid float precision loss). If a DivFloor op is desired, I’m happy to explore that separately. just lmk! |
Signed-off-by: yash solanki <alphacr792@gmail.com>
Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Signed-off-by: yash solanki <alphacr792@gmail.com>
Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
|
Thank you! |
Fixes #7570