Jackmin108 commited on
Commit
9fbf0f6
·
1 Parent(s): 96e2301

add custom sdpa path that works with cp

Browse files
Files changed (1) hide show
  1. modeling_qwen3_moe.py +24 -16
modeling_qwen3_moe.py CHANGED
@@ -167,23 +167,31 @@ class Qwen3MoeAttention(nn.Module):
167
  cache_kwargs = {"sin": sin, "cos": cos, "cache_position": cache_position}
168
  key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, cache_kwargs)
169
 
170
- attention_interface: Callable = eager_attention_forward
171
- if self.config._attn_implementation != "eager":
172
- attention_interface = ALL_ATTENTION_FUNCTIONS[self.config._attn_implementation]
173
-
174
- attn_output, attn_weights = attention_interface(
175
- self,
176
- query_states,
177
- key_states,
178
- value_states,
179
- attention_mask,
180
- dropout=0.0 if not self.training else self.attention_dropout,
181
- scaling=self.scaling,
182
- sliding_window=self.sliding_window, # diff with Llama
183
- **kwargs,
184
- )
 
 
 
 
 
 
 
 
185
 
186
- attn_output = attn_output.reshape(*input_shape, -1).contiguous()
187
  attn_output = self.o_proj(attn_output)
188
  return attn_output, attn_weights
189
 
 
167
  cache_kwargs = {"sin": sin, "cos": cos, "cache_position": cache_position}
168
  key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, cache_kwargs)
169
 
170
+ if self.config._attn_implementation == "sdpa":
171
+ key_states = key_states.repeat_interleave(self.num_key_value_groups, dim=1)
172
+ value_states = value_states.repeat_interleave(self.num_key_value_groups, dim=1)
173
+ out = F.scaled_dot_product_attention(query_states, key_states, value_states, is_causal=True)
174
+ out = out.transpose(1, 2).contiguous() #.view(out.shape[0], out.shape[1], -1)
175
+ attn_output = out.view(out.shape[0], out.shape[1], -1)
176
+ attn_weights = None
177
+ else:
178
+ attention_interface: Callable = eager_attention_forward
179
+ if self.config._attn_implementation != "eager":
180
+ attention_interface = ALL_ATTENTION_FUNCTIONS[self.config._attn_implementation]
181
+
182
+ attn_output, attn_weights = attention_interface(
183
+ self,
184
+ query_states,
185
+ key_states,
186
+ value_states,
187
+ attention_mask,
188
+ dropout=0.0 if not self.training else self.attention_dropout,
189
+ scaling=self.scaling,
190
+ sliding_window=self.sliding_window, # diff with Llama
191
+ **kwargs,
192
+ )
193
 
194
+ attn_output = attn_output.reshape(*input_shape, -1).contiguous()
195
  attn_output = self.o_proj(attn_output)
196
  return attn_output, attn_weights
197
 
Free AI Image Generator No sign-up. Instant results. Open Now