Skip to content

fix(converters): true division of integer tensors truncates for int64 and scalar divisors - #4850

Open
usmanmateen wants to merge 1 commit into
pytorch:mainfrom
usmanmateen:fix/int-true-division
Open

usmanmateen wants to merge 1 commit into
pytorch:mainfrom
usmanmateen:fix/int-true-division

Conversation

@usmanmateen

Copy link
Copy Markdown

Description

True division of integer tensors returned truncated values when the operands were int64 or one of them was a Python int. torch.arange(5) / 10 came out as [0, 0, 0, 0, 0] and an int64 count / total as zeros, while int32 tensor / int32 tensor was correct (#4849).

cast_int_int_div_trt_tensor only cast to float32 when both operands were trt.int32 TRT tensors, and impl.elementwise.div only called it when both operands were TRT tensors. With int64 (PyTorch's default integer type), int8, uint8 or bool tensors, or a Python scalar divisor, the operands stayed integers, convert_binary_elementwise promoted them to an integer type, and TensorRT ran an integer DIV.

The helper now casts both operands to float32 when both are integer or bool, whether they are TRT tensors, PyTorch tensors or Python scalars. div calls it for every operand combination. Floating point operands and mixed integer/float operands are unchanged. The other impl.elementwise.div callers (attention scaling, embedding_bag mean, normalization, quantize, the unary ops) divide floating point values or want true division too, so they're unaffected.

Fixes #4849

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

  • My code follows the style guidelines of this project (You can use the linters)
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas and hacks
  • I have made corresponding changes to the documentation
  • I have added tests to verify my fix or my feature
  • New and existing unit tests pass locally with my changes
  • I have added the relevant labels to my PR in so that relevant reviewers are notified

Testing

Added test_div_tensor_integer (int64/int64, int32/int64, bool/int64) and test_div_tensor_integer_scalar (int64 and int32 tensors divided by an int, and by True) to tests/py/dynamo/conversion/test_div_aten.py.

I ran the converter tests from this branch against the torch-tensorrt 2.14.0 wheel (PyTorch 2.14.1+cu130, TensorRT 11.1, RTX 5060 Ti), with converter_utils.py and impl/elementwise/ops.py patched the same way. Both functions are the same on main and in 2.14.0.

  • Without the change, 5 of the 6 new cases fail (the outputs are integers). With it, test_div_aten.py passes (13 passed).
  • test_floor_div_aten.py, test_mean_aten.py, test_layer_norm_aten.py, test_rsqrt_aten.py, test_mul_aten.py, test_sub_aten.py, test_add_aten.py, test_pow_aten.py, test_remainder_aten.py, test_trunc_aten.py and test_attention_aten.py pass. In test_embedding_bag_aten.py, 8 cases fail both with and without this change, because they need converter fixes that landed on main after 2.14.0.
  • The pre-commit hooks pass on the changed files (isort, mypy, ruff, black).

Note: in this file, test_div_tensor and test_div_tensor_rounding_mode are each defined twice, so the second definitions replace the first and the tensor/tensor cases don't run. I left that alone here and used new names for the new tests.

… and scalar divisors

cast_int_int_div_trt_tensor only cast to float32 when both operands were
int32 TRT tensors, and impl.elementwise.div only called it when both
operands were TRT tensors. int64 tensors (PyTorch's default integer
type), int8/uint8/bool tensors and Python int divisors kept integer
division, so torch.arange(5) / 10 gave all zeros.

Cast both operands of a true division to float32 when both are integer
or bool, whether they are TRT tensors, PyTorch tensors or Python
scalars.

Fixes pytorch#4849
@meta-cla meta-cla Bot added the cla signed label Oct 12, 2026
@github-actions github-actions Bot added component: tests Issues re: Tests component: conversion Issues re: Conversion stage component: core Issues re: The core compiler component: converters Issues re: Specific op converters component: api [Python] Issues re: Python API component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths labels Oct 12, 2026
@github-actions
github-actions Bot requested a review from cehongwang October 12, 2026 12:10
@usmanmateen

Copy link
Copy Markdown
Author

Hi @cehongwang (cc @lanluo-nvidia), gentle ping on this when you have a moment. Happy to adjust anything.

@usmanmateen

Copy link
Copy Markdown
Author

@lanluo-nvidia, could you take a look at this one too when you have a moment? Happy to adjust anything.

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

cla signed component: api [Python] Issues re: Python API component: conversion Issues re: Conversion stage component: converters Issues re: Specific op converters component: core Issues re: The core compiler component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths component: tests Issues re: Tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐛 [Bug] True division of int64 tensors or by a Python int truncates the result

1 participant