Vansh Chugh commited on
Commit
6f9efe3
·
1 Parent(s): afb6c50

fix mask padding trim missing from non-memory-efficient attention path

Browse files
src/audiocraft/modules/transformer.py CHANGED
@@ -399,15 +399,18 @@ class StreamingMultiheadAttention(StreamingModule):
399
  v = expand_repeated_kv(v, self.kv_repeat, self.memory_efficient)
400
  if self.attention_as_float32:
401
  q, k, v = [x.float() for x in [q, k, v]]
402
- if self.memory_efficient:
403
- if custom_attn_mask:
404
- # When using a custom attn mask:
405
- # Move to query's device, repeat for each sample, remove align8 padding
406
- seq_len = query.shape[1]
407
- attn_mask = attn_mask.to(q.dtype)
408
- attn_mask = attn_mask.repeat((q.shape[0], 1, 1, 1))
409
- attn_mask = attn_mask[..., :seq_len, :seq_len]
 
 
410
 
 
411
  p = self.dropout if self.training else 0
412
  if _efficient_attention_backend == 'torch':
413
  x = torch.nn.functional.scaled_dot_product_attention(
 
399
  v = expand_repeated_kv(v, self.kv_repeat, self.memory_efficient)
400
  if self.attention_as_float32:
401
  q, k, v = [x.float() for x in [q, k, v]]
402
+ # Original: this block was only reachable inside `if self.memory_efficient:`.
403
+ # Moved out so the plain (non-memory-efficient) path below also gets the
404
+ # align8-padding trim -- it needs the same seq_len match as the mask.
405
+ if custom_attn_mask:
406
+ # When using a custom attn mask:
407
+ # Move to query's device, repeat for each sample, remove align8 padding
408
+ seq_len = query.shape[1]
409
+ attn_mask = attn_mask.to(q.dtype)
410
+ attn_mask = attn_mask.repeat((q.shape[0], 1, 1, 1))
411
+ attn_mask = attn_mask[..., :seq_len, :seq_len]
412
 
413
+ if self.memory_efficient:
414
  p = self.dropout if self.training else 0
415
  if _efficient_attention_backend == 'torch':
416
  x = torch.nn.functional.scaled_dot_product_attention(