@@ -275,21 +275,7 @@ def __init__(
275275 # Gemma2-style logit soft-capping; 0.0 means disabled.
276276 self ._softcap = getattr (config , "attn_logit_softcapping" , 0.0 ) or 0.0
277277
278- self .q_proj = linear_class (
279- self .hidden_size ,
280- self .num_attention_heads * self .head_dim ,
281- bias = config .attn_qkv_bias ,
282- )
283- self .k_proj = linear_class (
284- self .hidden_size ,
285- self .num_key_value_heads * self .head_dim ,
286- bias = config .attn_qkv_bias ,
287- )
288- self .v_proj = linear_class (
289- self .hidden_size ,
290- self .num_key_value_heads * self .head_dim ,
291- bias = config .attn_qkv_bias ,
292- )
278+ self ._init_qkv_projections (config , linear_class )
293279 self .o_proj = linear_class (
294280 self .num_attention_heads * self .head_dim ,
295281 self .hidden_size ,
@@ -314,6 +300,24 @@ def __init__(
314300 self .q_norm = None
315301 self .k_norm = None
316302
303+ def _init_qkv_projections (self , config : ArchitectureConfig , linear_class : type ) -> None :
304+ """Create the default independent query, key, and value projections."""
305+ self .q_proj = linear_class (
306+ self .hidden_size ,
307+ self .num_attention_heads * self .head_dim ,
308+ bias = config .attn_qkv_bias ,
309+ )
310+ self .k_proj = linear_class (
311+ self .hidden_size ,
312+ self .num_key_value_heads * self .head_dim ,
313+ bias = config .attn_qkv_bias ,
314+ )
315+ self .v_proj = linear_class (
316+ self .hidden_size ,
317+ self .num_key_value_heads * self .head_dim ,
318+ bias = config .attn_qkv_bias ,
319+ )
320+
317321 def forward (
318322 self ,
319323 op : OpBuilder ,
@@ -323,9 +327,7 @@ def forward(
323327 past_key_value : tuple | None = None ,
324328 static_cache : StaticCacheState | None = None ,
325329 ):
326- query_states = self .q_proj (op , hidden_states )
327- key_states = self .k_proj (op , hidden_states )
328- value_states = self .v_proj (op , hidden_states )
330+ query_states , key_states , value_states = self ._project_qkv (op , hidden_states )
329331 if not math .isclose (self ._key_multiplier , 1.0 ):
330332 key_states = op .Mul (key_states , self ._key_multiplier )
331333
@@ -402,6 +404,16 @@ def forward(
402404 attn_output = self ._project_output (op , attn_output )
403405 return attn_output , (present_key , present_value )
404406
407+ def _project_qkv (
408+ self , op : OpBuilder , hidden_states : ir .Value
409+ ) -> tuple [ir .Value , ir .Value , ir .Value ]:
410+ """Project hidden states into Q, K, and V tensors."""
411+ return (
412+ self .q_proj (op , hidden_states ),
413+ self .k_proj (op , hidden_states ),
414+ self .v_proj (op , hidden_states ),
415+ )
416+
405417 def _project_output (self , op : OpBuilder , attn_output : ir .Value ) -> ir .Value :
406418 """Apply architecture-specific processing before the output projection."""
407419 return self .o_proj (op , attn_output )
@@ -466,6 +478,51 @@ def _forward_gqa(
466478 return attn_out , (present_key , present_value )
467479
468480
481+ class FusedQKVAttention (Attention ):
482+ """Attention whose checkpoint stores one contiguous Q/K/V projection.
483+
484+ The projection rows are ordered ``[Q | K | V]``. An optional symmetric
485+ activation clamp is applied to the fused output before it is split, so
486+ callers can represent architectures where clipping is part of the fused
487+ projection contract rather than an independent per-projection transform.
488+ """
489+
490+ def __init__ (
491+ self ,
492+ config : ArchitectureConfig ,
493+ * ,
494+ clamp : float | None = None ,
495+ linear_class : type | None = None ,
496+ ):
497+ super ().__init__ (config , linear_class = linear_class )
498+ self ._clamp = clamp
499+
500+ def _init_qkv_projections (self , config : ArchitectureConfig , linear_class : type ) -> None :
501+ self ._q_width = self .num_attention_heads * self .head_dim
502+ self ._kv_width = self .num_key_value_heads * self .head_dim
503+ self .qkv_proj = linear_class (
504+ self .hidden_size ,
505+ self ._q_width + 2 * self ._kv_width ,
506+ bias = config .attn_qkv_bias ,
507+ )
508+
509+ def _project_qkv (
510+ self , op : OpBuilder , hidden_states : ir .Value
511+ ) -> tuple [ir .Value , ir .Value , ir .Value ]:
512+ # Keep clipping ahead of the split: this is observably different from
513+ # clipping only Q/K after RoPE or leaving V unclipped.
514+ qkv = self .qkv_proj (op , hidden_states ) # (B, S, Q + K + V)
515+ if self ._clamp is not None and self ._clamp > 0 :
516+ limit = op .CastLike (self ._clamp , qkv )
517+ qkv = op .Clip (qkv , op .Neg (limit ), limit )
518+ return op .Split (
519+ qkv ,
520+ [self ._q_width , self ._kv_width , self ._kv_width ],
521+ axis = - 1 ,
522+ _outputs = 3 ,
523+ )
524+
525+
469526class Qwen35Attention (nn .Module ):
470527 """Multi-head attention with output gating for Qwen3.5.
471528
0 commit comments