dagloop5 commited on
Commit
0bee401
Β·
verified Β·
1 Parent(s): aae6730

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -14
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] Before patch: memory_efficient_attention={_attn_mod.memory_efficient_attention}")
85
- try:
86
- from xformers.ops import memory_efficient_attention as _mea
87
- from xformers.ops.fmha import cutlass
88
-
89
- def _cutlass_memory_efficient_attention(*args, **kwargs):
90
- # Force CUTLASS and avoid FlashAttention paths that are crashing.
91
- kwargs["op"] = (cutlass.FwOp, cutlass.BwOp)
92
- return _mea(*args, **kwargs)
93
-
94
- _attn_mod.memory_efficient_attention = _cutlass_memory_efficient_attention
95
- print(f"[ATTN] After patch: memory_efficient_attention={_attn_mod.memory_efficient_attention}")
96
- except Exception as e:
97
- print(f"[ATTN] xformers patch FAILED: {type(e).__name__}: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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