Repository navigation
fix(converters): true division of integer tensors truncates for int64 and scalar divisors - #4850
Open
usmanmateen wants to merge 1 commit into
Open
usmanmateen wants to merge 1 commit into
usmanmateen wants to merge 1 commit into
Conversation
… 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
Author
|
Hi @cehongwang (cc @lanluo-nvidia), gentle ping on this when you have a moment. Happy to adjust anything. |
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
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.
Description
True division of integer tensors returned truncated values when the operands were
int64or one of them was a Pythonint.torch.arange(5) / 10came out as[0, 0, 0, 0, 0]and anint64count / totalas zeros, whileint32tensor /int32tensor was correct (#4849).cast_int_int_div_trt_tensoronly cast tofloat32when both operands weretrt.int32TRT tensors, andimpl.elementwise.divonly called it when both operands were TRT tensors. Withint64(PyTorch's default integer type),int8,uint8orbooltensors, or a Python scalar divisor, the operands stayed integers,convert_binary_elementwisepromoted them to an integer type, and TensorRT ran an integerDIV.The helper now casts both operands to
float32when both are integer or bool, whether they are TRT tensors, PyTorch tensors or Python scalars.divcalls it for every operand combination. Floating point operands and mixed integer/float operands are unchanged. The otherimpl.elementwise.divcallers (attention scaling,embedding_bagmean, normalization, quantize, the unary ops) divide floating point values or want true division too, so they're unaffected.Fixes #4849
Type of change
Checklist:
Testing
Added
test_div_tensor_integer(int64/int64,int32/int64,bool/int64) andtest_div_tensor_integer_scalar(int64andint32tensors divided by anint, and byTrue) totests/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.pyandimpl/elementwise/ops.pypatched the same way. Both functions are the same on main and in 2.14.0.test_div_aten.pypasses (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.pyandtest_attention_aten.pypass. Intest_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.Note: in this file,
test_div_tensorandtest_div_tensor_rounding_modeare 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.