JonusNattapong commited on
Commit
0aae3b6
·
verified ·
1 Parent(s): f27d49b

Upload modeling.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. modeling.py +11 -0
modeling.py CHANGED
@@ -236,6 +236,17 @@ class GroupedQueryAttention(nn.Module):
236
  scores = torch.matmul(q, k.transpose(-2, -1)) / math.sqrt(self.d_k)
237
 
238
  if attention_mask is not None:
 
 
 
 
 
 
 
 
 
 
 
239
  scores = scores + attention_mask
240
 
241
  attention = F.softmax(scores, dim=-1)
 
236
  scores = torch.matmul(q, k.transpose(-2, -1)) / math.sqrt(self.d_k)
237
 
238
  if attention_mask is not None:
239
+ # Handle different attention mask formats
240
+ if attention_mask.dim() == 4: # Standard transformers format [batch, 1, 1, seq_len]
241
+ attention_mask = attention_mask.squeeze(1).squeeze(1) # [batch, seq_len]
242
+ # Expand to match scores shape [batch, num_heads, seq_len, seq_len]
243
+ attention_mask = attention_mask.unsqueeze(1).unsqueeze(2)
244
+ attention_mask = attention_mask.expand(-1, self.num_heads, seq_len, -1)
245
+ elif attention_mask.dim() == 2: # [seq_len, seq_len] causal mask
246
+ # Expand to match scores shape [batch, num_heads, seq_len, seq_len]
247
+ attention_mask = attention_mask.unsqueeze(0).unsqueeze(0)
248
+ attention_mask = attention_mask.expand(batch_size, self.num_heads, -1, -1)
249
+
250
  scores = scores + attention_mask
251
 
252
  attention = F.softmax(scores, dim=-1)