Skip to content

test(converters): cover integer true division and run the shadowed div tests - #4850

Merged
micwill755 merged 3 commits into
pytorch:mainfrom
usmanmateen:fix/int-true-division
Nov 25, 2026
Merged

micwill755 merged 3 commits into
pytorch:mainfrom
usmanmateen:fix/int-true-division

Conversation

@usmanmateen

@usmanmateen usmanmateen commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Description

#4964 fixed integer true division on main, so this PR now only adds tests (the converter change is dropped in the merge with main):

  • test_div_tensor_integer: int64/int64, int32/int64 and bool/int64 tensors. Before, only int32/int32 was covered.
  • test_div_tensor_integer_scalar: int64 and int32 tensors divided by a Python int, and by True.
  • test_div_tensor and test_div_tensor_rounding_mode were each defined twice, so the scalar versions replaced the tensor/tensor ones and those never ran. The scalar versions are now test_div_tensor_scalar and test_div_tensor_scalar_rounding_mode, so both sets run.

Closes #4849

Type of change

  • Tests only

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

I ran -k test_div_tensor from test_div_aten.py against the torch-tensorrt 2.14.0 wheel (PyTorch 2.14.1+cu130, TensorRT 11.1, RTX 5060 Ti), using the 2.14 test harness, since main's harness passes settings that 2.14 doesn't have.

… 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
Contributor Author

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

@usmanmateen

Copy link
Copy Markdown
Contributor Author

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

@micwill755
micwill755 self-requested a review November 16, 2026 21:32

@micwill755 micwill755 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 for the fix and the clear write-up. #4964 has since fixed this on main: div now casts integer and bool operands to float32 and no longer uses cast_int_int_div_trt_tensor, so this PR conflicts. I checked the int64, bool and Python-scalar cases from this PR against main, and they all return the correct float32 results.

Your tests would still be useful, though. test_div_aten.py only covers int32 / int32 for integer true division. Could you rebase this down to:

the new test_div_tensor_integer and test_div_tensor_integer_scalar cases
renaming the duplicate test_div_tensor and test_div_tensor_rounding_mode definitions so the tensor / tensor cases run again

Once that's in, #4849 can be closed.

main fixed integer true division in pytorch#4964, so keep main's div and converter_utils and drop the cast_int_int_div_trt_tensor change from this branch. Only the new integer division tests remain.
…or cases run

test_div_tensor and test_div_tensor_rounding_mode were each defined twice, and the scalar versions replaced the tensor / tensor ones. Rename the scalar versions to test_div_tensor_scalar and test_div_tensor_scalar_rounding_mode.
@usmanmateen usmanmateen changed the title fix(converters): true division of integer tensors truncates for int64 and scalar divisors test(converters): cover integer true division and run the shadowed div tests Nov 18, 2026
@usmanmateen

Copy link
Copy Markdown
Contributor Author

@micwill755 Thanks for checking against main. I merged main and kept main's div and converter_utils.py, so the converter change is gone (944d273). The PR is now just the two new test cases plus the renames: the scalar duplicates are now test_div_tensor_scalar and test_div_tensor_scalar_rounding_mode (1c7fadb). Against 2.14.0 with main's div patched in, all 16 test_div_tensor* cases pass, and 5 of the new integer cases fail without it. I merged rather than rebased to avoid force-pushing; happy to squash on merge.

@micwill755

Copy link
Copy Markdown
Collaborator

LGTM

@micwill755
micwill755 merged commit e090095 into pytorch:main Nov 25, 2026
2 checks passed
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

2 participants