@@ -45,18 +45,23 @@ def __init__(
4545 has_relative_attention_bias : bool = False ,
4646 bias : bool = True ,
4747 scale : float | None = None ,
48+ linear_class : type | None = None ,
4849 ):
4950 super ().__init__ ()
51+ if linear_class is None :
52+ linear_class = Linear
5053 self .hidden_size = config .hidden_size
5154 self .num_heads = config .num_attention_heads
5255 self .head_dim = config .head_dim
5356 self .is_causal = is_causal
5457 self ._scale = scale if scale is not None else float (self .head_dim ** - 0.5 )
5558
56- self .q_proj = Linear (self .hidden_size , self .num_heads * self .head_dim , bias = bias )
57- self .k_proj = Linear (self .hidden_size , self .num_heads * self .head_dim , bias = bias )
58- self .v_proj = Linear (self .hidden_size , self .num_heads * self .head_dim , bias = bias )
59- self .out_proj = Linear (self .num_heads * self .head_dim , self .hidden_size , bias = bias )
59+ self .q_proj = linear_class (self .hidden_size , self .num_heads * self .head_dim , bias = bias )
60+ self .k_proj = linear_class (self .hidden_size , self .num_heads * self .head_dim , bias = bias )
61+ self .v_proj = linear_class (self .hidden_size , self .num_heads * self .head_dim , bias = bias )
62+ self .out_proj = linear_class (
63+ self .num_heads * self .head_dim , self .hidden_size , bias = bias
64+ )
6065
6166 if has_relative_attention_bias :
6267 self .relative_attention_bias = Embedding (
@@ -72,6 +77,7 @@ def forward(
7277 key_value_states : ir .Value | None = None ,
7378 attention_bias : ir .Value | None = None ,
7479 past_key_value : tuple | None = None ,
80+ use_cross_attention_cache : bool = False ,
7581 ):
7682 """Forward pass.
7783
@@ -89,15 +95,39 @@ def forward(
8995 query_states = self .q_proj (op , hidden_states )
9096
9197 if key_value_states is not None :
92- # Cross-attention: K/V projected from encoder hidden states.
93- # Encoder output is constant across decode steps, so we
94- # recompute the same projections each step and never
95- # concatenate with past — concatenation would incorrectly
96- # double the cross-attention sequence length.
98+ # Cache-enabled cross-attention accepts an empty encoder sequence
99+ # during decode; other callers continue to project full encoder
100+ # states on every step.
97101 key_states = self .k_proj (op , key_value_states )
98102 value_states = self .v_proj (op , key_value_states )
99- past_key = None
100- past_value = None
103+ if past_key_value is not None :
104+ past_key , past_value = past_key_value
105+ if use_cross_attention_cache :
106+ # Attention requires a non-empty current K/V sequence even
107+ # when past is supplied. Flatten the cached head layout and
108+ # concatenate it with the newly projected encoder suffix.
109+ flat_shape = op .Concat (
110+ op .Shape (past_key , start = 0 , end = 1 ),
111+ op .Shape (past_key , start = 2 , end = 3 ),
112+ op .Constant (value_ints = [self .num_heads * self .head_dim ]),
113+ axis = 0 ,
114+ )
115+ flat_past_key = op .Reshape (
116+ op .Transpose (past_key , perm = [0 , 2 , 1 , 3 ]), flat_shape
117+ )
118+ flat_past_value = op .Reshape (
119+ op .Transpose (past_value , perm = [0 , 2 , 1 , 3 ]), flat_shape
120+ )
121+ key_states = op .Concat (flat_past_key , key_states , axis = 1 )
122+ value_states = op .Concat (flat_past_value , value_states , axis = 1 )
123+ # Cross-attention presents the complete K/V sequence as current
124+ # input. Non-caching callers keep the historical behavior of
125+ # recomputing it from full encoder states.
126+ past_key = None
127+ past_value = None
128+ else :
129+ past_key = None
130+ past_value = None
101131 else :
102132 key_states = self .k_proj (op , hidden_states )
103133 value_states = self .v_proj (op , hidden_states )
@@ -120,6 +150,16 @@ def forward(
120150 is_causal = 1 if self .is_causal else 0 ,
121151 _outputs = 3 ,
122152 )
153+ if key_value_states is not None and use_cross_attention_cache :
154+ present_shape = op .Concat (
155+ op .Shape (key_states , start = 0 , end = 1 ),
156+ op .Constant (value_ints = [self .num_heads ]),
157+ op .Shape (key_states , start = 1 , end = 2 ),
158+ op .Constant (value_ints = [self .head_dim ]),
159+ axis = 0 ,
160+ )
161+ present_key = op .Reshape (present_key , present_shape )
162+ present_value = op .Reshape (present_value , present_shape )
123163
124164 output = self .out_proj (op , attn_output )
125165 return output , (present_key , present_value )
0 commit comments