multimodalart HF Staff commited on
Commit
998073e
·
verified ·
1 Parent(s): 16685b4

text-rows-only full SDPA (exact)

Browse files
Files changed (1) hide show
  1. app.py +31 -0
app.py CHANGED
@@ -68,6 +68,37 @@ FPS = 8 # the authors' output frame rate
68
  STEM = "scene"
69
  DEVICE = "cuda"
70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
  # --------------------------------------------------------------------- models
72
  ckpt_dir = Path(
73
  snapshot_download(
 
68
  STEM = "scene"
69
  DEVICE = "cuda"
70
 
71
+ # Exact inference-time speedup of the authors' attention processor: its full
72
+ # SDPA's video rows are overwritten by the supervised decomposition, so only
73
+ # the text rows of that SDPA are computed here (same output, ~half the attention).
74
+ import torch.nn.functional as F # noqa: E402
75
+ from trajloc.backbones.cogvideox.attention import SupervisedJointAttnProcessor # noqa: E402
76
+
77
+ _orig_supervised_attention = SupervisedJointAttnProcessor._supervised_attention
78
+
79
+
80
+ def _fast_supervised_attention(self, query, key, value, attention_mask, text_seq_length, batch_size):
81
+ if self._batched_mode or attention_mask is not None:
82
+ return _orig_supervised_attention(
83
+ self, query, key, value, attention_mask, text_seq_length, batch_size
84
+ )
85
+ text_out = F.scaled_dot_product_attention(
86
+ query[:, :, :text_seq_length], key, value, dropout_p=0.0, is_causal=False
87
+ )
88
+ video_out = self._supervised_video_rows(
89
+ query=query,
90
+ key=key,
91
+ value=value,
92
+ text_seq_length=text_seq_length,
93
+ supervised_positions=self.per_sample_positions[0],
94
+ heatmaps=self._flat_heatmaps.to(query.device, query.dtype).detach(),
95
+ batch_size=batch_size,
96
+ )
97
+ return torch.cat([text_out, video_out], dim=2)
98
+
99
+
100
+ SupervisedJointAttnProcessor._supervised_attention = _fast_supervised_attention
101
+
102
  # --------------------------------------------------------------------- models
103
  ckpt_dir = Path(
104
  snapshot_download(