[Bug][Relax][PyTorch] float64 `matmul` / `mm` / `conv` results are downcast to float32 by `from_exported_program`

オープン
#20,883 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
42/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
python, pytorch

調査の方向性

The float32 cast is likely hard-coded in the matmul, mm, addmm and conv converters of the PyTorch frontend under python/tvm/relax/frontend/torch, so start by searching there for out_dtype="float32". Reproduce with the matmul snippet in the issue and the Linear-with-bias case, which currently raises a TypeError. Done means float64 and bfloat16 inputs keep their dtype, and the bias case converts and matches eager output.

索引モデルが issue の本文から書いたものです。

説明

needs-triage type: bug
Expected behavior

torch.mm on float64 inputs returns float64. The model converted with from_exported_program should keep the dtype and precision.

Actual behavior

The result is silently float32, so the precision is lost. The same happens for torch.matmul, torch.einsum, Conv1d, Conv2d, ConvTranspose2d and Linear without bias, and for bfloat16 inputs. With a float64 bias (Linear, Conv2d), the conversion fails instead:

eager: torch.float64 [[5.0000000002]]
tvm:   float32 [[5.0]]
TypeError: Binary operators must have the same datatype for both operands.

Possibly out_dtype="float32" is hard-coded in the matmul, mm, addmm and conv converters.

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 torch.mm(x, x.T)


x = torch.tensor([[1.0 + 1e-10, 2.0]], dtype=torch.float64)
e = M()(x)
print("eager:", e.dtype, e.tolist())

ep = torch.export.export(M(), (x,))
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(x.numpy()))[0].numpy()
print("tvm:  ", out.dtype, out.tolist())

# With a bias, the conversion itself fails
m = torch.nn.Linear(2, 2, dtype=torch.float64)
ep = torch.export.export(m, (x,))
from_exported_program(ep)
主要言語
Python
スター
13.9k
フォーク
4k
平均マージ
7時間 59分
マージ済み PR(30日)
159

環境構築

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

はじめの一歩

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

apache/tvm のほかの issue

apache/tvm の issue をすべて見る

似ている issue

Python の issue をもっと見る

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

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