Skip to content

Keep integer precision in rounded division - #3077

Open
Rio Yu (rioyu123) wants to merge 1 commit into
microsoft:mainfrom
rioyu123:codex/integer-rounded-div
Open

Rio Yu (rioyu123) wants to merge 1 commit into
microsoft:mainfrom
rioyu123:codex/integer-rounded-div

Conversation

@rioyu123

Copy link
Copy Markdown
Contributor

Summary

Integer torch.div(..., rounding_mode="floor") and rounding_mode="trunc" currently pass through float32 during export. Large integer quotients can consequently change value: for example, 50331653 / 3 with either rounding mode produces 16777218 instead of 16777217 in ONNX Runtime.

This change keeps rounded integer division in integers. trunc uses integer Div, and floor reuses the existing aten_floor_divide implementation, including its correction for negative operands. Floating-point inputs and the rounding_mode=None path are unchanged.

Fixes #3062.

Testing

  • Added eight end-to-end export tests covering int32/int64, both rounding modes, tensor/scalar divisors, broadcasting, positive and negative operands, zero, exact multiples, and large quotients. Results are compared to PyTorch with zero tolerance. All eight fail before the fix and pass afterward.
  • Existing division OpInfo coverage: 16 passed, 11 skipped, 135 subtests passed.
  • Additional export/runtime smoke checks for int8, int16 and uint8 passed for both rounding modes.
  • Full e2e_ops_tests.py: 135 passed, 1 skipped, 5 failed. Unmodified main has the same five failures (STFT value_info, boolean SDPA, and dynamic unbind), with 127 passed and 1 skipped.
  • lintrunner -a and git diff --check passed.

Tested on Windows with Python 3.12, PyTorch 2.14.1 CPU and ONNX Runtime 1.31.0. TensorRT and model-quality benchmarks were not run.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

[torchlib] aten::div with rounding_mode="floor"/"trunc" on integer tensors is computed in float32 and loses precision

1 participant