Fix integer true-divide silently truncating instead of promoting to float#32
Draft
gokulkrishna98 wants to merge 3 commits into
Draft
Fix integer true-divide silently truncating instead of promoting to float#32gokulkrishna98 wants to merge 3 commits into
gokulkrishna98 wants to merge 3 commits into
Conversation
…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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.Tensorwere wired to the genericreplace_binary_opshandler, which keeps same-kind integers as integers (correct for add/sub/mul, wrong for true divide). Re-pointed them toreplace_truediv, which already promotes to the node's real float output type before dividing.replace_div_tensor_mode'srounding_mode=Nonebranch.TestDivclass (matches existingTestCopy-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 TestDivpytest tests/ops/test_ops_ir.py -k "div or true_divide"