Repository navigation
Conversation
…dtype in binary ops A Tensor.item() on an integer tensor reaches the importer as an int64 primitive value. A binary op with a float or non-int64 integer tensor passed it through unchanged, so relax.add/multiply/... rejected the mismatched dtypes. The scalar is now materialized as a rank-zero tensor of the tensor operand's dtype, as PyTorch does for a Python int.
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.
x * t.item()with a floatxand an integer tensortis valid PyTorch and survivestorch.export, butfrom_exported_programfails on it:The same happens for
+,-,/, fort.item() * x, and for float16, bfloat16 and int32 tensors (only an int64 tensor happened to work).Tensor.item()on an integer tensor is imported as an int64 primitive value._binary_oppassed it to the Relax op unchanged, so the operand dtypes did not match. A Python int takes the dtype of the tensor operand in PyTorch, so the converter now materializes the primitive value as a rank-zero tensor of the tensor operand's dtype (reusing_prim_value_to_scalar_tensor, whichfill_andcopy_already use). Other operand kinds are unchanged. The change is in the shared base class, sofrom_fxand SymInt operands (x * x.shape[0]) are fixed as well.The new tests compare TVM with PyTorch numerically for
+,-,*and the reversed operands, for float and integer tensors (float32, float16, int32; bfloat16 is cast around the operation because NumPy has no bfloat16), and for/and reversed/with floating tensors. They fail on main (26 failures) and pass with the change.x // t.item()goes through_div, which open #20618 touches, and is not covered here.test_frontend_from_exported_program.pyandtest_frontend_from_fx.pypass apart fromtest_extended_unary_ops,test_logaddexpandtest_norm, which already fail on main with torch 2.15, and the flakytest_einsum.Fixes #20939
The implementation and tests were written with AI assistance.