Fix float16 trunc division rounding - #3025
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes the float16 off-by-one mismatch for aten::div(..., rounding_mode="trunc") by ensuring the division result is rounded to float16 before applying truncation, matching PyTorch’s float16 semantics and re-enabling the corresponding regression coverage (issue #990).
Changes:
- In
aten_div_mode(..., rounding_mode="trunc"), materialize the float16quotientvia an explicit cast before callingaten_trunc. - Re-enable the float16
trunc_roundingvariant in the TorchLib OpInfo test data by removing the skip for issue #990.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| onnxscript/function_libs/torch_lib/ops/core.py | Casts the float16 division quotient to float16 before truncation to match PyTorch rounding semantics. |
| tests/function_libs/torch_lib/ops_test_data.py | Removes the float16 skip for the div_mode trunc-rounding variant to restore regression coverage. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3025 +/- ##
=======================================
Coverage 72.64% 72.64%
=======================================
Files 265 265
Lines 32251 32254 +3
Branches 3050 3051 +1
=======================================
+ Hits 23429 23432 +3
Misses 7786 7786
Partials 1036 1036 ☔ View full report in Codecov by Harness. |
|
@microsoft-github-policy-service agree |
Justin Chu (justinchuby)
left a comment
There was a problem hiding this comment.
Thanks!
fc4357c
into
microsoft:main
Summary
Fixes #990.
ONNX Runtime can evaluate the float16 division in
aten::div(..., rounding_mode="trunc")with a higher-precision intermediate than PyTorch. Applying truncation to that value can produce an off-by-one result at float16 boundaries.Materialize the float16 quotient before truncation, matching PyTorch's float16 rounding semantics. The existing float16
trunc_roundingregression case is enabled again.Tests
python -m pytest tests/function_libs/torch_lib/ops_test.py -k test_output_match_opinfo__div_mode -vvruff check onnxscript/function_libs/torch_lib/ops/core.py tests/function_libs/torch_lib/ops_test_data.pyruff format --check onnxscript/function_libs/torch_lib/ops/core.py tests/function_libs/torch_lib/ops_test_data.pygit diff --check