Skip to content

Fix integer true-divide silently truncating instead of promoting to float#32

Draft
gokulkrishna98 wants to merge 3 commits into
apple:mainfrom
gokulkrishna98:dev/gokul/fix_div
Draft

Fix integer true-divide silently truncating instead of promoting to float#32
gokulkrishna98 wants to merge 3 commits into
apple:mainfrom
gokulkrishna98:dev/gokul/fix_div

Conversation

@gokulkrishna98

@gokulkrishna98 gokulkrishna98 commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Summary

Integer true-divide (x / y, torch.true_divide, torch.div(x, y, rounding_mode=None)) was dividing operands as integers first and only casting the truncated result to float afterward, silently dropping the fractional part on every backend. Per PyTorch's promotion rules, integer operands must be promoted to float before dividing.

  • div.Tensor/div.Scalar/true_divide.Tensor were wired to the generic replace_binary_ops handler, which keeps same-kind integers as integers (correct for add/sub/mul, wrong for true divide). Re-pointed them to replace_truediv, which already promotes to the node's real float output type before dividing.
  • Fixed the same latent bug in replace_div_tensor_mode's rounding_mode=None branch.
  • Grouped div-family tests into a TestDiv class (matches existing TestCopy-style convention) and added regression tests for the integer cases above.

floordiv/mod/fmod/rounding_mode="floor"/"trunc" were already correct and untouched.

Test plan

  • pytest tests/ops/test_ops.py -k TestDiv
  • pytest tests/ops/test_ops_ir.py -k "div or true_divide"

…loat

aten.div.Tensor, div.Scalar, and true_divide.Tensor on integer operands were dispatched to the generic replace_binary_ops handler, which divides using the generic same-kind-stays-integer promotion rule and only casts the already-truncated result to float afterward. True divide promotes integer operands to a float dtype before dividing, so this silently dropped the fractional part on every backend.

Re-point these ops at replace_truediv, which already promotes operands to the correct float result type before dividing.
Fix adjacent latent same-class bug: replace_div_tensor_mode with rounding_mode=None used the generic integer-stays-integer promotion rule instead of promoting to the node output float type before dividing.

Add torch.true_divide(int, int) test to directly cover the true_divide.Tensor resolver rewiring.

Add torch.div(int, int, rounding_mode=None) test to cover the div_tensor_mode fix.
Combines test_div, test_div_integer_promotes_to_float, test_div_scalar_integer_promotes_to_float, test_true_divide_integer_promotes_to_float, test_div_tensor_mode_none_integer_promotes_to_float, test_div_tensor_mode, test_true_divide, and test_true_divide_scalar into a single TestDiv class, matching the per-op class convention used elsewhere in the file (e.g. TestCopy).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant