[Bug][Relax] Integer tensor with a Python float scalar is truncated: `x * 0.5` returns zeros

未关闭
#20,926 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 3 天内回复

@GoyalIshaan 已经在做这个了。

开始于 2026年11月6日。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
48/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
python
领域
compilers

调研方向

Start by tracing the Relax PyTorch frontend's _binary_op handling of Python scalar operands; the issue suggests it may construct the scalar using the tensor's dtype. Check how dtype promotion is handled for arithmetic and comparisons, then reproduce the provided integer tensor cases and compare them with PyTorch eager results. Done means float scalars produce the expected promoted results across the listed operations and tensor dtypes.

由索引模型根据 Issue 内容生成。

描述

needs-triage type: bug
Expected behavior

An integer tensor with a Python float scalar is promoted to float32: x * 0.5 is [0.5, 1.0, 1.5] and x + 1.5 is [2.5, 3.5, 4.5].

Actual behavior

The result keeps the integer dtype and the scalar is truncated, so x * 0.5 is all zeros. The same happens for -, for the scalar on the left, for int32, uint8 and bool tensors, and for comparisons (x < 1.5 is all False). An integer scalar, or a float tensor, works.

eager: [([0.5, 1.0, 1.5], torch.float32), ([2.5, 3.5, 4.5], torch.float32)]
tvm:   [([0, 0, 0], dtype('int64')), ([2, 3, 4], dtype('int64'))]

Possibly _binary_op builds the scalar as relax.const(rhs, lhs.ty.dtype), so a float scalar is cast to the tensor's integer dtype instead of the tensor being promoted.

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):
        return x * 0.5, x + 1.5


x = torch.tensor([1, 2, 3])
args = (x,)
print("eager:", [(t.tolist(), t.dtype) for t in M()(x)])

ep = torch.export.export(M(), args)
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 args])
print("tvm:  ", [(o.numpy().tolist(), o.numpy().dtype) for o in out])
主要语言
Python
星标
13.9k
派生
4.1k
平均合并
4 天 22 小时
30 天内合并 PR
22

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

apache/tvm 的其他 Issue

查看 apache/tvm 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。