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

オープン
#20,926 コメント 1 件 リアクション 0 件 担当者 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時間
マージ済み PR(30日)
22

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

apache/tvm のほかの issue

apache/tvm の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。