Onise commited on
Commit
c3b3e31
·
verified ·
1 Parent(s): 37ac7bd

Update qwenimage/qwen_fa3_processor.py

Browse files
Files changed (1) hide show
  1. qwenimage/qwen_fa3_processor.py +47 -12
qwenimage/qwen_fa3_processor.py CHANGED
@@ -1,5 +1,16 @@
1
  """
2
  Paired with a good language model. Thanks!
 
 
 
 
 
 
 
 
 
 
 
3
  """
4
 
5
  import torch
@@ -10,25 +21,44 @@ import torch.nn.functional as F
10
 
11
  _flash_attn_func = None
12
  _kernels_err = None
 
 
 
 
 
 
 
 
13
 
14
- try:
15
- from kernels import get_kernel
16
- # Blackwell (sm_120+) is not yet supported by the vllm-flash-attn3 kernel binary
17
- _cap = torch.cuda.get_device_capability() if torch.cuda.is_available() else (0, 0)
18
- if _cap >= (12, 0):
 
 
 
 
 
 
 
19
  _kernels_err = RuntimeError(
20
- f"GPU compute capability sm_{_cap[0]}{_cap[1]} (Blackwell+) is not supported "
21
  "by the kernels-community/vllm-flash-attn3 binary; using SDPA fallback."
22
  )
23
- else:
 
 
 
24
  _k = get_kernel("kernels-community/vllm-flash-attn3")
25
  _flash_attn_func = _k.flash_attn_func
26
- except Exception as e:
27
- _flash_attn_func = None
28
- _kernels_err = e
29
 
30
 
31
  def _fa3_available() -> bool:
 
32
  return _flash_attn_func is not None
33
 
34
 
@@ -45,9 +75,13 @@ def _sdpa_fallback(q: torch.Tensor, k: torch.Tensor, v: torch.Tensor) -> torch.T
45
  def flash_attn_func(
46
  q: torch.Tensor, k: torch.Tensor, v: torch.Tensor, causal: bool = False
47
  ) -> torch.Tensor:
 
 
 
48
  outputs, lse = _flash_attn_func(q, k, v, causal=causal)
49
  return outputs
50
 
 
51
  @flash_attn_func.register_fake
52
  def _(q, k, v, **kwargs):
53
  return torch.empty_like(q).contiguous()
@@ -57,7 +91,7 @@ class QwenDoubleStreamAttnProcessorFA3:
57
  """
58
  FA3-based attention processor for Qwen double-stream architecture.
59
  Computes joint attention over concatenated [text, image] streams using vLLM FlashAttention-3
60
- accessed via Hugging Face `kernels`.
61
 
62
  Notes / limitations:
63
  - General attention masks are not supported here (FA3 path). `is_causal=False` and no arbitrary mask.
@@ -68,6 +102,7 @@ class QwenDoubleStreamAttnProcessorFA3:
68
  _attention_backend = "fa3" # for parity with your other processors, not used internally
69
 
70
  def __init__(self):
 
71
  if not _fa3_available():
72
  print(f"[QwenDoubleStreamAttnProcessorFA3] FA3 unavailable, using SDPA fallback. Reason: {_kernels_err}")
73
 
@@ -154,4 +189,4 @@ class QwenDoubleStreamAttnProcessorFA3:
154
 
155
  txt_attn_out = attn.to_add_out(txt_attn_out)
156
 
157
- return img_attn_out, txt_attn_out
 
1
  """
2
  Paired with a good language model. Thanks!
3
+
4
+ ZeroGPU note:
5
+ FlashAttention-3 (kernels-community/vllm-flash-attn3) has no binary for
6
+ Blackwell (sm_120), which is what ZeroGPU currently attaches. Loading the
7
+ kernel at import time under CUDA *emulation* reports a fake capability, so
8
+ FA3 gets enabled and then crashes on the real GPU with:
9
+
10
+ CUDA error: no kernel image is available for execution on the device
11
+
12
+ Kernel init is therefore deferred until a real CUDA device is present, and
13
+ sm_120+ always falls back to PyTorch SDPA.
14
  """
15
 
16
  import torch
 
21
 
22
  _flash_attn_func = None
23
  _kernels_err = None
24
+ _fa3_inited = False
25
+
26
+
27
+ def _ensure_fa3() -> None:
28
+ global _flash_attn_func, _kernels_err, _fa3_inited
29
+ if _fa3_inited:
30
+ return
31
+ _fa3_inited = True
32
 
33
+ if not torch.cuda.is_available():
34
+ _kernels_err = RuntimeError("CUDA not available; using SDPA fallback.")
35
+ return
36
+
37
+ try:
38
+ cap = torch.cuda.get_device_capability()
39
+ except Exception as e:
40
+ _kernels_err = e
41
+ return
42
+
43
+ # Blackwell (sm_120+) is not supported by the vllm-flash-attn3 kernel binary.
44
+ if cap >= (12, 0):
45
  _kernels_err = RuntimeError(
46
+ f"GPU compute capability sm_{cap[0]}{cap[1]} (Blackwell+) is not supported "
47
  "by the kernels-community/vllm-flash-attn3 binary; using SDPA fallback."
48
  )
49
+ return
50
+
51
+ try:
52
+ from kernels import get_kernel
53
  _k = get_kernel("kernels-community/vllm-flash-attn3")
54
  _flash_attn_func = _k.flash_attn_func
55
+ except Exception as e:
56
+ _flash_attn_func = None
57
+ _kernels_err = e
58
 
59
 
60
  def _fa3_available() -> bool:
61
+ _ensure_fa3()
62
  return _flash_attn_func is not None
63
 
64
 
 
75
  def flash_attn_func(
76
  q: torch.Tensor, k: torch.Tensor, v: torch.Tensor, causal: bool = False
77
  ) -> torch.Tensor:
78
+ _ensure_fa3()
79
+ if _flash_attn_func is None:
80
+ return _sdpa_fallback(q, k, v)
81
  outputs, lse = _flash_attn_func(q, k, v, causal=causal)
82
  return outputs
83
 
84
+
85
  @flash_attn_func.register_fake
86
  def _(q, k, v, **kwargs):
87
  return torch.empty_like(q).contiguous()
 
91
  """
92
  FA3-based attention processor for Qwen double-stream architecture.
93
  Computes joint attention over concatenated [text, image] streams using vLLM FlashAttention-3
94
+ accessed via Hugging Face `kernels`, with an automatic SDPA fallback.
95
 
96
  Notes / limitations:
97
  - General attention masks are not supported here (FA3 path). `is_causal=False` and no arbitrary mask.
 
102
  _attention_backend = "fa3" # for parity with your other processors, not used internally
103
 
104
  def __init__(self):
105
+ _ensure_fa3()
106
  if not _fa3_available():
107
  print(f"[QwenDoubleStreamAttnProcessorFA3] FA3 unavailable, using SDPA fallback. Reason: {_kernels_err}")
108
 
 
189
 
190
  txt_attn_out = attn.to_add_out(txt_attn_out)
191
 
192
+ return img_attn_out, txt_attn_out