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

Aperta
#20,883 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
42/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python, pytorch

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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)
Lingua principale
Python
Stelle
13.9k
Fork
4k
Merge medio
7h 59m
PR unite (30g)
159

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di apache/tvm

Tutte le issue di apache/tvm

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.