pytorch / pytorch/executorch

SDPA decomposition does not preserve intermediate dtype for fp16/bf16 inputs

Aperta
#22,012 4 commenti 0 reazioni 1 assegnatario Vedi su GitHub

@JacobSzwejbka ci sta già lavorando.

Dal 24/8/2026.

Lingua principale
Python
Stelle
5k
Fork
1.2k
Merge medio
2g 10h
PR unite (30g)
581

Descrizione

When exporting an fp16/bf16 model, aten.scaled_dot_product_attention is decomposed during to_edge before backend partitioning. For fp16/bf16 inputs, the current decomposition promotes SDPA intermediates to fp32, then casts the final result back to the original output dtype. In other words, the public output dtype is preserved, but the intermediate dtype is not.

Is this fp32 intermediate behavior intended to be unconditional, or should users/backends have a way to request dtype-preserving SDPA decomposition?

More generally, what should users/backends expect from Core ATen decompositions with respect to dtype? Should decompositions generally preserve the dtype of intermediate operations when the original op is exported with fp16/bf16 inputs, or is it expected that decompositions may introduce higher-precision opmath intermediates as long as the public operator output dtype is preserved?

Minimal example:

import torch
from executorch.exir import to_edge

class M(torch.nn.Module):
    def forward(self, q, k, v):
        return torch.nn.functional.scaled_dot_product_attention(
            q, k, v, dropout_p=0.0
        )

inputs = tuple(torch.randn(1, 2, 4, 8, dtype=torch.float16) for _ in range(3))
ep = torch.export.export(M(), inputs)

edge = to_edge(ep)
print(edge.exported_program().graph_module)

After to_edge, the graph contains fp32 intermediates from the SDPA decomposition even though the model inputs and final SDPA output are fp16. The same applies to bf16.

This is understandable from a numerical-stability perspective, especially around the score matmul and softmax. However, since SDPA is decomposed before backend partitioning, backend-specific passes do not see the original SDPA op and cannot easily choose a lower-precision SDPA implementation.

cc @digantdesai @freddan80 @per @zingo @mansnils @Sebastian-Larsson @robell @rascani

Guida per i contributori

Apri la guida per i contributori

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.

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.