Skip to content

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

Open

Description

@raindrops-0199

Expected behavior

t.item() on an integer tensor gives a Python int, and x * t.item() with a float x gives a float tensor. It works in eager and torch.export, so from_exported_program should convert it.

Actual behavior

The conversion fails. The same happens for +, -, /, for t.item() * x and for a float16 x. A float t, or an integer x, works.

eager: [4.5, 6.0] torch.float32
TypeError: Binary operators must have the same datatype for both operands.  However, ... "relax.multiply" ... uses datatype T.float32 on the LHS (Type of R.Tensor((2,), dtype="float32")), and datatype T.int64 on the RHS (Type of T.int64).

Possibly _item turns an integer tensor into an int64 symbolic variable, and _binary_op passes it on without casting it to the dtype of the float tensor.

Environment

  • TVM: main @ 2bf0532 (0.28.dev0), built from source, target llvm
  • PyTorch: 2.12.0+cu130
  • Python: 3.12, Ubuntu x86_64

Steps to reproduce

import torch
import tvm
import tvm_tirx
from tvm import relax
from tvm.relax.frontend.torch import from_exported_program


class M(torch.nn.Module):
    def forward(self, x, t):
        return x * t.item()


x = torch.tensor([1.5, 2.0])
t = torch.tensor(3)
print("eager:", M()(x, t).tolist(), M()(x, t).dtype)

ep = torch.export.export(M(), (x, t))
mod = from_exported_program(ep)
ex = tvm.compile(mod, target="llvm")
vm = relax.VirtualMachine(ex, tvm_tirx.cpu())
out = vm["main"](*[tvm_tirx.runtime.tensor(a.numpy()) for a in (x, t)])
print("tvm:  ", out[0].numpy().tolist(), out[0].numpy().dtype)

Activity

  1. added
    needs-triagePRs or issues that need to be investigated by maintainers to find the right assignees to address it
    on Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs-triagePRs or issues that need to be investigated by maintainers to find the right assignees to address ittype: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions