huggingface / huggingface/diffusers

`set_attention_backend` on one model leaks globally to other models via the active-backend registry

Aperta
#14,249 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Lingua principale
Python
Stelle
34.5k
Fork
7.3k
Merge medio
3g 3h
PR unite (30g)
91

Descrizione

Calling model.set_attention_backend(...) on a single model does not only configure that model — it also flips a global active backend (_AttentionBackendRegistry.set_active_backend(...)). Any other model in the same process whose per-module attention backend is left unset then silently picks up that global backend inside dispatch_attention_fn (the backend is None fallback path).

This is surprising: setting the backend on model A changes the attention behavior of unrelated model B. It's especially harmful when B calls attention with arguments the leaked backend doesn't support — e.g. a masked attention (attn_mask=...) with a FlashAttention-3 backend, which raises ValueError: attn_mask is not supported for flash-attn 3. Even though the user never asked FA3 to run in B.

Reproduction

import torch
from diffusers.models.attention_dispatch import (
    AttentionBackendName, _AttentionBackendRegistry, dispatch_attention_fn,
)

# Model A opts into a backend; nothing is configured on B.
# set_attention_backend() sets the module's processor._attention_backend AND, as a side
# effect, flips the process-global active backend:
_AttentionBackendRegistry.set_active_backend(AttentionBackendName("_flash_3_hub"))

# Model B just calls dispatch with backend=None (its per-module backend is unset) and a mask:
q = k = v = torch.randn(1, 4, 64, 64, device="cuda", dtype=torch.bfloat16)
mask = torch.zeros(1, 1, 64, 64, device="cuda", dtype=torch.bfloat16)

out = dispatch_attention_fn(q, k, v, attn_mask=mask, backend=None)  # -> uses the leaked global backend
# ValueError: `attn_mask` is not supported for flash-attn 3.

Cc: @dg845 @DN6

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.

Direzione di ricerca

Inizia tracciando set_attention_backend, _AttentionBackendRegistry.set_active_backend e dispatch_attention_fn, quindi esegui la riproduzione fornita di masked-attention. Determina come viene risolto tra i modelli un backend per modulo non impostato; il lavoro è completato quando la configurazione di un modello non modifica più la selezione del backend di un altro modello e la chiamata masked non utilizza il backend fuoriuscito.

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

Valutazione

Stack tecnologico
python, pytorch
Ambito
machine-learning
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Tranquilla
Chiarezza
Abbastanza chiara
Idoneità per principianti
56/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.