Update app.py
Browse files95 unmodified lines9697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716895 unmodified linesexcept Exception as e:print(f"[ATTN] xformers patch FAILED: {type(e).__name__}: {e}")# Add this patch after imports in app.pydef _patch_attention_for_kv_cache():"""Patch Attention.forward to accept pre-projected K/V."""from ltx_core.model.transformer.attention import Attention, apply_rotary_emb_original_forward = Attention.forwarddef patched_forward(self,x: torch.Tensor,context: torch.Tensor | None = None,mask: torch.Tensor | None = None,pe: torch.Tensor | None = None,k_pe: torch.Tensor | None = None,perturbation_mask: torch.Tensor | None = None,all_perturbed: bool = False,# NEW: pre-computed KV for cross-attentioncached_k: torch.Tensor | None = None,cached_v: torch.Tensor | None = None,) -> torch.Tensor:context = x if context is None else contextuse_attention = not all_perturbedv = cached_v if cached_v is not None else self.to_v(context)if not use_attention:out = velse:if cached_k is not None:q = self.to_q(x)q = self.q_norm(q)k = cached_kif pe is not None:q = apply_rotary_emb(q, pe, self.rope_type)k = apply_rotary_emb(k, pe if k_pe is None else k_pe, self.rope_type)else:q = self.to_q(x)k = self.to_k(context)q = self.q_norm(q)k = self.k_norm(k)if pe is not None:q = apply_rotary_emb(q, pe, self.rope_type)k = apply_rotary_emb(k, pe if k_pe is None else k_pe, self.rope_type)out = self.attention_function(q, k, v, self.heads, mask)if perturbation_mask is not None:out = out * perturbation_mask + v * (1 - perturbation_mask)# Gating logic remains the sameif self.to_gate_logits is not None:gate_logits = self.to_gate_logits(x)b, t, _ = out.shapeout = out.view(b, t, self.heads, self.dim_head)gates = 2.0 * torch.sigmoid(gate_logits)out = out * gates.unsqueeze(-1)out = out.view(b, t, self.heads * self.dim_head)return self.to_out(out)Attention.forward = patched_forward_patch_attention_for_kv_cache()logging.getLogger().setLevel(logging.INFO)MAX_SEED = np.iinfo(np.int32).max95 unmodified lines9697989910010195 unmodified linesexcept Exception as e:print(f"[ATTN] xformers patch FAILED: {type(e).__name__}: {e}")logging.getLogger().setLevel(logging.INFO)MAX_SEED = np.iinfo(np.int32).max
app.py-67