Spaces:
Running on Zero
Running on Zero
Commit ·
e4b15be
1
Parent(s): 0a2678b
Replace vllm-flash-attn3 kernel with native SDPA cuDNN backend; wire up attn processor
Browse filesThe external vllm-flash-attn3 kernel had no compiled binary for compute
capability 12.0 (Blackwell), crashing at construction. Switch to
sdpa_kernel(SDPBackend.CUDNN_ATTENTION) which ships inside PyTorch/CUDA
itself. Also wire up set_attn_processor() in app.py — previously the
class was imported but never applied, leaving the transformer on its
default processor the whole time.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- app.py +3 -1
- qwenimage/qwen_fa3_processor.py +100 -142
app.py
CHANGED
|
@@ -193,7 +193,9 @@ _hb.set()
|
|
| 193 |
pipe.vae.enable_tiling()
|
| 194 |
print(f"[startup] pipeline loaded in {time.perf_counter()-_t1_load:.1f}s", flush=True)
|
| 195 |
|
| 196 |
-
print("[startup]
|
|
|
|
|
|
|
| 197 |
|
| 198 |
with open("examples.json") as _f:
|
| 199 |
EXAMPLES_CONFIG = json.load(_f)
|
|
|
|
| 193 |
pipe.vae.enable_tiling()
|
| 194 |
print(f"[startup] pipeline loaded in {time.perf_counter()-_t1_load:.1f}s", flush=True)
|
| 195 |
|
| 196 |
+
print("[startup] setting cuDNN SDPA attention processor...", flush=True)
|
| 197 |
+
pipe.transformer.set_attn_processor(QwenDoubleStreamAttnProcessorFA3())
|
| 198 |
+
print("[startup] cuDNN SDPA attention processor set.", flush=True)
|
| 199 |
|
| 200 |
with open("examples.json") as _f:
|
| 201 |
EXAMPLES_CONFIG = json.load(_f)
|
qwenimage/qwen_fa3_processor.py
CHANGED
|
@@ -1,142 +1,100 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
import
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
def
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
if getattr(attn, "norm_q", None) is not None:
|
| 102 |
-
img_q = attn.norm_q(img_q)
|
| 103 |
-
if getattr(attn, "norm_k", None) is not None:
|
| 104 |
-
img_k = attn.norm_k(img_k)
|
| 105 |
-
if getattr(attn, "norm_added_q", None) is not None:
|
| 106 |
-
txt_q = attn.norm_added_q(txt_q)
|
| 107 |
-
if getattr(attn, "norm_added_k", None) is not None:
|
| 108 |
-
txt_k = attn.norm_added_k(txt_k)
|
| 109 |
-
|
| 110 |
-
# ---- RoPE (Qwen variant) ----
|
| 111 |
-
if image_rotary_emb is not None:
|
| 112 |
-
img_freqs, txt_freqs = image_rotary_emb
|
| 113 |
-
# expects tensors shaped (B, S, H, D_h)
|
| 114 |
-
img_q = apply_rotary_emb_qwen(img_q, img_freqs, use_real=False)
|
| 115 |
-
img_k = apply_rotary_emb_qwen(img_k, img_freqs, use_real=False)
|
| 116 |
-
txt_q = apply_rotary_emb_qwen(txt_q, txt_freqs, use_real=False)
|
| 117 |
-
txt_k = apply_rotary_emb_qwen(txt_k, txt_freqs, use_real=False)
|
| 118 |
-
|
| 119 |
-
# ---- Joint attention over [text, image] along sequence axis ----
|
| 120 |
-
# Shapes: (B, S_total, H, D_h)
|
| 121 |
-
q = torch.cat([txt_q, img_q], dim=1)
|
| 122 |
-
k = torch.cat([txt_k, img_k], dim=1)
|
| 123 |
-
v = torch.cat([txt_v, img_v], dim=1)
|
| 124 |
-
|
| 125 |
-
# FlashAttention-3 path expects (B, S, H, D_h) and returns (out, softmax_lse)
|
| 126 |
-
out = flash_attn_func(q, k, v, causal=False) # out: (B, S_total, H, D_h)
|
| 127 |
-
|
| 128 |
-
# ---- Back to (B, S, D_model) ----
|
| 129 |
-
out = out.flatten(2, 3).to(q.dtype)
|
| 130 |
-
|
| 131 |
-
# Split back to text / image segments
|
| 132 |
-
txt_attn_out = out[:, :S_txt, :]
|
| 133 |
-
img_attn_out = out[:, S_txt:, :]
|
| 134 |
-
|
| 135 |
-
# ---- Output projections ----
|
| 136 |
-
img_attn_out = attn.to_out[0](img_attn_out)
|
| 137 |
-
if len(attn.to_out) > 1:
|
| 138 |
-
img_attn_out = attn.to_out[1](img_attn_out) # dropout if present
|
| 139 |
-
|
| 140 |
-
txt_attn_out = attn.to_add_out(txt_attn_out)
|
| 141 |
-
|
| 142 |
-
return img_attn_out, txt_attn_out
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn.functional as F
|
| 3 |
+
from typing import Optional, Tuple
|
| 4 |
+
from torch.nn.attention import SDPBackend, sdpa_kernel
|
| 5 |
+
from diffusers.models.transformers.transformer_qwenimage import apply_rotary_emb_qwen
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class QwenDoubleStreamAttnProcessorFA3:
|
| 9 |
+
"""
|
| 10 |
+
Attention processor for Qwen double-stream architecture using PyTorch's native
|
| 11 |
+
SDPA cuDNN backend (FA3-equivalent fused kernel). Falls back to default SDPA
|
| 12 |
+
with a log line if the cuDNN backend fails to dispatch at runtime.
|
| 13 |
+
"""
|
| 14 |
+
|
| 15 |
+
_attention_backend = "cudnn_sdpa"
|
| 16 |
+
|
| 17 |
+
@torch.no_grad()
|
| 18 |
+
def __call__(
|
| 19 |
+
self,
|
| 20 |
+
attn,
|
| 21 |
+
hidden_states: torch.FloatTensor,
|
| 22 |
+
encoder_hidden_states: torch.FloatTensor = None,
|
| 23 |
+
encoder_hidden_states_mask: torch.FloatTensor = None,
|
| 24 |
+
attention_mask: Optional[torch.FloatTensor] = None,
|
| 25 |
+
image_rotary_emb: Optional[Tuple[torch.Tensor, torch.Tensor]] = None,
|
| 26 |
+
) -> Tuple[torch.FloatTensor, torch.FloatTensor]:
|
| 27 |
+
if encoder_hidden_states is None:
|
| 28 |
+
raise ValueError("QwenDoubleStreamAttnProcessorFA3 requires encoder_hidden_states (text stream).")
|
| 29 |
+
|
| 30 |
+
B, S_img, _ = hidden_states.shape
|
| 31 |
+
S_txt = encoder_hidden_states.shape[1]
|
| 32 |
+
|
| 33 |
+
# QKV projections
|
| 34 |
+
img_q = attn.to_q(hidden_states)
|
| 35 |
+
img_k = attn.to_k(hidden_states)
|
| 36 |
+
img_v = attn.to_v(hidden_states)
|
| 37 |
+
|
| 38 |
+
txt_q = attn.add_q_proj(encoder_hidden_states)
|
| 39 |
+
txt_k = attn.add_k_proj(encoder_hidden_states)
|
| 40 |
+
txt_v = attn.add_v_proj(encoder_hidden_states)
|
| 41 |
+
|
| 42 |
+
# Reshape to (B, S, H, D_h)
|
| 43 |
+
H = attn.heads
|
| 44 |
+
img_q = img_q.unflatten(-1, (H, -1))
|
| 45 |
+
img_k = img_k.unflatten(-1, (H, -1))
|
| 46 |
+
img_v = img_v.unflatten(-1, (H, -1))
|
| 47 |
+
|
| 48 |
+
txt_q = txt_q.unflatten(-1, (H, -1))
|
| 49 |
+
txt_k = txt_k.unflatten(-1, (H, -1))
|
| 50 |
+
txt_v = txt_v.unflatten(-1, (H, -1))
|
| 51 |
+
|
| 52 |
+
# Q/K normalization
|
| 53 |
+
if getattr(attn, "norm_q", None) is not None:
|
| 54 |
+
img_q = attn.norm_q(img_q)
|
| 55 |
+
if getattr(attn, "norm_k", None) is not None:
|
| 56 |
+
img_k = attn.norm_k(img_k)
|
| 57 |
+
if getattr(attn, "norm_added_q", None) is not None:
|
| 58 |
+
txt_q = attn.norm_added_q(txt_q)
|
| 59 |
+
if getattr(attn, "norm_added_k", None) is not None:
|
| 60 |
+
txt_k = attn.norm_added_k(txt_k)
|
| 61 |
+
|
| 62 |
+
# RoPE
|
| 63 |
+
if image_rotary_emb is not None:
|
| 64 |
+
img_freqs, txt_freqs = image_rotary_emb
|
| 65 |
+
img_q = apply_rotary_emb_qwen(img_q, img_freqs, use_real=False)
|
| 66 |
+
img_k = apply_rotary_emb_qwen(img_k, img_freqs, use_real=False)
|
| 67 |
+
txt_q = apply_rotary_emb_qwen(txt_q, txt_freqs, use_real=False)
|
| 68 |
+
txt_k = apply_rotary_emb_qwen(txt_k, txt_freqs, use_real=False)
|
| 69 |
+
|
| 70 |
+
# Joint attention: concat along sequence axis -> (B, S_total, H, D_h)
|
| 71 |
+
q = torch.cat([txt_q, img_q], dim=1)
|
| 72 |
+
k = torch.cat([txt_k, img_k], dim=1)
|
| 73 |
+
v = torch.cat([txt_v, img_v], dim=1)
|
| 74 |
+
|
| 75 |
+
# SDPA expects (B, H, S, D_h)
|
| 76 |
+
q = q.transpose(1, 2)
|
| 77 |
+
k = k.transpose(1, 2)
|
| 78 |
+
v = v.transpose(1, 2)
|
| 79 |
+
|
| 80 |
+
try:
|
| 81 |
+
with sdpa_kernel(SDPBackend.CUDNN_ATTENTION):
|
| 82 |
+
out = F.scaled_dot_product_attention(q, k, v)
|
| 83 |
+
except RuntimeError as e:
|
| 84 |
+
print(f"[attn] cuDNN SDPA backend unavailable ({e}), falling back to default", flush=True)
|
| 85 |
+
out = F.scaled_dot_product_attention(q, k, v)
|
| 86 |
+
|
| 87 |
+
# Back to (B, S_total, D_model)
|
| 88 |
+
out = out.transpose(1, 2).flatten(2, 3).to(q.dtype)
|
| 89 |
+
|
| 90 |
+
txt_attn_out = out[:, :S_txt, :]
|
| 91 |
+
img_attn_out = out[:, S_txt:, :]
|
| 92 |
+
|
| 93 |
+
# Output projections
|
| 94 |
+
img_attn_out = attn.to_out[0](img_attn_out)
|
| 95 |
+
if len(attn.to_out) > 1:
|
| 96 |
+
img_attn_out = attn.to_out[1](img_attn_out)
|
| 97 |
+
|
| 98 |
+
txt_attn_out = attn.to_add_out(txt_attn_out)
|
| 99 |
+
|
| 100 |
+
return img_attn_out, txt_attn_out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|