Skip to content

[Fix][Relax][Frontend][Torch] Give integer item() scalars the tensor dtype in binary ops - #21021

Open
sohailshk wants to merge 1 commit into
apache:mainfrom
sohailshk:fix/binary-op-item-scalar-dtype
Open

sohailshk wants to merge 1 commit into
apache:mainfrom
sohailshk:fix/binary-op-item-scalar-dtype

Conversation

@sohailshk

Copy link
Copy Markdown

x * t.item() with a float x and an integer tensor t is valid PyTorch and survives torch.export, but from_exported_program fails on it:

TypeError: Binary operators must have the same datatype for both operands. ... uses datatype T.float32 on the LHS ... and datatype T.int64 on the RHS (Type of T.int64).

The same happens for +, -, /, for t.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_op passed 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, which fill_ and copy_ already use). Other operand kinds are unchanged. The change is in the shared base class, so from_fx and 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.py and test_frontend_from_fx.py pass apart from test_extended_unary_ops, test_logaddexp and test_norm, which already fail on main with torch 2.15, and the flaky test_einsum.

Fixes #20939

The implementation and tests were written with AI assistance.

…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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug][Relax] x * t.item() fails in from_exported_program when x is float and t is an integer tensor

1 participant