Skip to content

Fix float16 trunc division rounding - #3025

Merged
Justin Chu (justinchuby) merged 1 commit into
microsoft:mainfrom
Alphaxiaoteng:fix/issue-990-float16-trunc-div
Sep 3, 2026
Merged

Fix float16 trunc division rounding#3025
Justin Chu (justinchuby) merged 1 commit into
microsoft:mainfrom
Alphaxiaoteng:fix/issue-990-float16-trunc-div

Conversation

@Alphaxiaoteng

@Alphaxiaoteng Cpthimself (Alphaxiaoteng) commented Aug 30, 2026

Copy link
Copy Markdown
Contributor

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_rounding regression case is enabled again.

Tests

  • python -m pytest tests/function_libs/torch_lib/ops_test.py -k test_output_match_opinfo__div_mode -vv
  • ruff check onnxscript/function_libs/torch_lib/ops/core.py tests/function_libs/torch_lib/ops_test_data.py
  • ruff format --check onnxscript/function_libs/torch_lib/ops/core.py tests/function_libs/torch_lib/ops_test_data.py
  • git diff --check

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 float16 quotient via an explicit cast before calling aten_trunc.
  • Re-enable the float16 trunc_rounding variant 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

codecov Bot commented Aug 31, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 72.64%. Comparing base (3ba2bf7) to head (f34edf1).
⚠️ Report is 3 commits behind head on main.

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.
📢 Have feedback on the report? Share it here.

@Alphaxiaoteng

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree

@justinchuby Justin Chu (justinchuby) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@github-project-automation github-project-automation Bot moved this from Todo to Done in ONNX Script Review Board Sep 3, 2026
@justinchuby Justin Chu (justinchuby) added the module: torchlib Related to the torch/aten function lib in development label Sep 3, 2026
@justinchuby
Justin Chu (justinchuby) merged commit fc4357c into microsoft:main Sep 3, 2026
29 of 32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

module: torchlib Related to the torch/aten function lib in development

Projects

Development

Successfully merging this pull request may close these issues.

[torchlib] trunc_div float16 off-by-one mismatches

3 participants