@@ -156,7 +156,7 @@ def rope_rotation_table(angles, dtype):
156156
157157
158158class Attention (nn .Module ):
159- def __init__ (self , hidden , heads , head_dim , eps , dtype = None , device = None , operations = None ):
159+ def __init__ (self , hidden , heads , head_dim , eps , gate_compress = False , dtype = None , device = None , operations = None ):
160160 super ().__init__ ()
161161 self .heads = heads
162162 self .head_dim = head_dim
@@ -165,6 +165,10 @@ def __init__(self, hidden, heads, head_dim, eps, dtype=None, device=None, operat
165165 self .q_norm = operations .RMSNorm (head_dim , eps = eps , dtype = dtype , device = device )
166166 self .k_norm = operations .RMSNorm (head_dim , eps = eps , dtype = dtype , device = device )
167167 self .out_proj = operations .Linear (inner , hidden , bias = False , dtype = dtype , device = device )
168+ self .to_gate_compress = None
169+ if gate_compress :
170+ # VSA gate, unused by the dense forward; consumed by sparse attention patches
171+ self .to_gate_compress = operations .Linear (hidden , inner , bias = False , dtype = dtype , device = device )
168172
169173 def forward (self , x , rope_freqs = None , transformer_options = {}):
170174 s = x .shape [0 ]
@@ -273,20 +277,22 @@ def forward(self, x, transformer_options={}):
273277
274278class DiTBlock (nn .Module ):
275279 def __init__ (self , hidden , heads , head_dim , ffn , t_dim , eps , qk_eps ,
276- apply_silu = True , adaln_dtype = None , dtype = None , device = None , operations = None ):
280+ apply_silu = True , adaln_dtype = None , gate_compress = False , dtype = None , device = None , operations = None ):
277281 super ().__init__ ()
278282 self .norm1 = operations .RMSNorm (hidden , eps = eps , dtype = dtype , device = device )
279283 self .norm2 = operations .RMSNorm (hidden , eps = eps , dtype = dtype , device = device )
280- self .attn = Attention (hidden , heads , head_dim , qk_eps , dtype = dtype , device = device , operations = operations )
284+ self .attn = Attention (hidden , heads , head_dim , qk_eps , gate_compress = gate_compress ,
285+ dtype = dtype , device = device , operations = operations )
281286 self .mlp = MLP (hidden , ffn , dtype = dtype , device = device , operations = operations )
282287 self .adaln_proj = AdalnProj (t_dim , hidden , 6 , 3 , apply_silu = apply_silu ,
283288 dtype = adaln_dtype if adaln_dtype is not None else dtype ,
284289 device = device , operations = operations )
285290
286- def forward (self , x , t_emb , mod_segments , rope_freqs , transformer_options = {}):
291+ def forward (self , x , t_emb , mod_segments , rope_freqs , transformer_options = {}, attention = None ):
292+ attention = self .attn if attention is None else attention
287293 shift_msa , scale_msa , gate_msa , shift_mlp , scale_mlp , gate_mlp = self .adaln_proj (t_emb )
288294 h = _mod_scale_shift (self .norm1 (x ), shift_msa , scale_msa , mod_segments )
289- x = _mod_gate (x , gate_msa , self . attn (h , rope_freqs = rope_freqs , transformer_options = transformer_options ), mod_segments )
295+ x = _mod_gate (x , gate_msa , attention (h , rope_freqs = rope_freqs , transformer_options = transformer_options ), mod_segments )
290296 h = _mod_scale_shift (self .norm2 (x ), shift_mlp , scale_mlp , mod_segments )
291297 return _mod_gate (x , gate_mlp , self .mlp (h ), mod_segments )
292298
@@ -471,7 +477,7 @@ def __init__(self, hidden_size=5376, num_layers=50, token_refiner_num_layers=2,
471477 timestep_input_dim = 256 , time_embed_hidden_size = 5376 , time_embed_dim = 2688 ,
472478 rope_inv_freq_len = 16 , norm_eps = 1e-5 , qk_norm_eps = 1e-5 , final_norm_eps = 1e-5 ,
473479 sigma_shift_video = 12.0 , sigma_shift_audio = 3.0 ,
474- adaln_curve_grid = None ,
480+ adaln_curve_grid = None , gate_compress = False ,
475481 image_model = None , dtype = None , device = None , operations = None , ** kwargs ):
476482 super ().__init__ ()
477483 self .dtype = dtype
@@ -502,7 +508,8 @@ def __init__(self, hidden_size=5376, num_layers=50, token_refiner_num_layers=2,
502508 final_norm_eps , dtype = dtype , device = device , operations = operations )
503509 self .blocks = nn .ModuleList ([
504510 DiTBlock (hidden_size , num_attention_heads , attention_head_dim , ffn_hidden_size ,
505- time_embed_dim , norm_eps , qk_norm_eps , ** curve , dtype = dtype , device = device , operations = operations )
511+ time_embed_dim , norm_eps , qk_norm_eps , ** curve , gate_compress = gate_compress ,
512+ dtype = dtype , device = device , operations = operations )
506513 for _ in range (num_layers )])
507514 self .final_layer = FinalLayer (hidden_size , time_embed_dim , video_patch_dim , audio_latents_dim ,
508515 final_norm_eps , ** curve , dtype = dtype , device = device , operations = operations )
@@ -607,6 +614,8 @@ def _forward(self, x, timestep, context, transformer_options={}, minimax_payload
607614 keyframes = payload .get ("keyframes" ),
608615 refs = payload .get ("refs" ))
609616
617+ transformer_options ["minimax_h3_layout" ] = layout # segment spans for attention patches
618+
610619 # model_base passes model_sampling.timestep(sigma) = sigma * 1000
611620 shift_v = float (transformer_options .get ("minimax_h3_sigma_shift_video" , self .sigma_shift_video ))
612621 shift_a = float (transformer_options .get ("minimax_h3_sigma_shift_audio" , self .sigma_shift_audio ))
@@ -736,10 +745,11 @@ def rows_to_mod_index(rows_t, tag):
736745 prefetch_queue = comfy .model_prefetch .make_prefetch_queue (list (self .blocks ), device , transformer_options )
737746 for i , block in enumerate (self .blocks ):
738747 comfy .model_prefetch .prefetch_queue_pop (prefetch_queue , device , block , malloc_scope = "block" )
748+ transformer_options ["block_index" ] = i
739749 if ("double_block" , i ) in blocks_replace :
740750 def block_wrap (args ):
741751 return {"img" : block (args ["img" ], args ["t_emb" ], args ["mod_segments" ], args ["rope_freqs" ],
742- transformer_options = args ["transformer_options" ])}
752+ transformer_options = args ["transformer_options" ], attention = args . get ( "attention" ) )}
743753 h = blocks_replace [("double_block" , i )](
744754 {"img" : h , "t_emb" : t_emb , "mod_segments" : mod_segments , "rope_freqs" : rope_freqs ,
745755 "layout" : layout , "transformer_options" : transformer_options },
0 commit comments