Update app.py
Browse files
app.py
CHANGED
|
@@ -81,20 +81,54 @@ from ltx_core.loader.sd_ops import LTXV_LORA_COMFY_RENAMING_MAP
|
|
| 81 |
|
| 82 |
from ltx_core.model.transformer import attention as _attn_mod
|
| 83 |
|
| 84 |
-
print(f"[ATTN]
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
logging.getLogger().setLevel(logging.INFO)
|
| 100 |
|
|
|
|
| 81 |
|
| 82 |
from ltx_core.model.transformer import attention as _attn_mod
|
| 83 |
|
| 84 |
+
print(f"[ATTN] memory_efficient_attention={_attn_mod.memory_efficient_attention}")
|
| 85 |
+
print(f"[ATTN] flash_attn_interface={_attn_mod.flash_attn_interface}")
|
| 86 |
+
|
| 87 |
+
# If LTX-2's bare `import flash_attn_interface` failed, try the nested import
|
| 88 |
+
# and inject it back so the built-in FlashAttention3 class works.
|
| 89 |
+
if _attn_mod.flash_attn_interface is None:
|
| 90 |
+
try:
|
| 91 |
+
from flash_attn import flash_attn_interface as _flash_attn_interface
|
| 92 |
+
_attn_mod.flash_attn_interface = _flash_attn_interface
|
| 93 |
+
print("[ATTN] Recovered flash_attn_interface from flash_attn package")
|
| 94 |
+
except Exception as _e:
|
| 95 |
+
print(f"[ATTN] Could not recover flash_attn_interface: {type(_e).__name__}: {_e}")
|
| 96 |
+
|
| 97 |
+
# ββ Hard enforcement: error out if the wrong backend is present ββ
|
| 98 |
+
if _attn_mod.memory_efficient_attention is not None:
|
| 99 |
+
raise RuntimeError(
|
| 100 |
+
"xformers is still importable (memory_efficient_attention is not None). "
|
| 101 |
+
"Remove xformers from requirements.txt and rebuild the Space."
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
if _attn_mod.flash_attn_interface is None:
|
| 105 |
+
raise RuntimeError(
|
| 106 |
+
"FlashAttention3 (flash_attn_interface) is not available. "
|
| 107 |
+
"Install flash-attn and ensure the Space has a Hopper-compatible build."
|
| 108 |
+
)
|
| 109 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 110 |
+
|
| 111 |
+
# Defensively wrap flash_attn_func: FA3 sometimes returns (output, softmax_lse)
|
| 112 |
+
# while LTX-2 expects a single tensor.
|
| 113 |
+
_fa3_func = _attn_mod.flash_attn_interface.flash_attn_func
|
| 114 |
+
def _fa3_func_wrapped(q, k, v, *args, **kwargs):
|
| 115 |
+
result = _fa3_func(q, k, v, *args, **kwargs)
|
| 116 |
+
if isinstance(result, tuple):
|
| 117 |
+
return result[0]
|
| 118 |
+
return result
|
| 119 |
+
|
| 120 |
+
_attn_mod.flash_attn_interface.flash_attn_func = _fa3_func_wrapped
|
| 121 |
+
|
| 122 |
+
# Patch DEFAULT so it routes to FlashAttention3 instead of XFormers -> PyTorch
|
| 123 |
+
_orig_attn_fn_call = _attn_mod.AttentionFunction.__call__
|
| 124 |
+
|
| 125 |
+
def _default_to_fa3(self, q, k, v, heads, mask=None):
|
| 126 |
+
if self is _attn_mod.AttentionFunction.DEFAULT:
|
| 127 |
+
return _attn_mod.FlashAttention3()(q, k, v, heads, mask)
|
| 128 |
+
return _orig_attn_fn_call(self, q, k, v, heads, mask)
|
| 129 |
+
|
| 130 |
+
_attn_mod.AttentionFunction.__call__ = _default_to_fa3
|
| 131 |
+
print("[ATTN] Patched AttentionFunction.DEFAULT -> FlashAttention3")
|
| 132 |
|
| 133 |
logging.getLogger().setLevel(logging.INFO)
|
| 134 |
|