Vansh Chugh commited on
Commit
4bdf828
·
1 Parent(s): 401125f

no flex_attention

Browse files
unison/models/transformers/modules/attention.py CHANGED
@@ -17,7 +17,6 @@
17
  import einops
18
  import torch
19
  from typing import Optional
20
- from loguru import logger
21
  import numpy as np
22
  import torch.nn.functional as F
23
 
@@ -32,15 +31,7 @@ from unison.utils.flash_attn_no_pad import (
32
  )
33
  from unison.commons import maybe_fallback_attn_mode
34
 
35
- try:
36
- from torch.nn.attention.flex_attention import flex_attention
37
-
38
- # Left uncompiled: torch.compile(flex_attention, ...) hits a Dynamo bug on this
39
- # torch build (AssertionError: Cannot construct ConstantVariable for value of
40
- # type torch.device, while tracing _validate_sdpa_input).
41
- flex_mask_cache = {}
42
- except Exception:
43
- logger.warning("Could not load Sliding Tile Attention of FlexAttn.")
44
 
45
  from unison.models.transformers.modules.ssta_attention import ssta_3d_attention
46
  from unison.commons.infer_state import get_infer_state
@@ -180,17 +171,25 @@ def sequence_parallel_attention(q, k, v,
180
  elif attn_mask.dtype != torch.bool:
181
  attn_mask = attn_mask.to(query.dtype)
182
  raise NotImplementedError(f'Float attention mask is not implemented for torch attention.')
183
-
 
 
 
 
 
 
184
  # transpose q,k,v dim to fit scaled_dot_product_attention
185
  query = query.transpose(1, 2) # B * Head_num * length * dim
186
  key = key.transpose(1, 2) # B * Head_num * length * dim
187
  value = value.transpose(1, 2) # B * Head_num * length * dim
188
 
189
- def score_mod(score, b, h, q_idx, kv_idx):
190
- return torch.where(attn_mask[b, q_idx] & attn_mask[b, kv_idx], score, float('-inf'))
 
 
 
 
191
 
192
- hidden_states = flex_attention(query, key, value, score_mod=score_mod)
193
-
194
  # transpose back
195
  hidden_states = hidden_states.transpose(1, 2)
196
 
 
17
  import einops
18
  import torch
19
  from typing import Optional
 
20
  import numpy as np
21
  import torch.nn.functional as F
22
 
 
31
  )
32
  from unison.commons import maybe_fallback_attn_mode
33
 
34
+ flex_mask_cache = {}
 
 
 
 
 
 
 
 
35
 
36
  from unison.models.transformers.modules.ssta_attention import ssta_3d_attention
37
  from unison.commons.infer_state import get_infer_state
 
171
  elif attn_mask.dtype != torch.bool:
172
  attn_mask = attn_mask.to(query.dtype)
173
  raise NotImplementedError(f'Float attention mask is not implemented for torch attention.')
174
+
175
+ # [B, L] -> [B, 1, L, L]: query token q and key token k must both be
176
+ # unmasked.
177
+ attn_mask1 = einops.rearrange(attn_mask, 'b l -> b 1 l 1')
178
+ attn_mask2 = einops.rearrange(attn_mask1, 'b 1 l 1 -> b 1 1 l')
179
+ attn_mask = attn_mask1 & attn_mask2
180
+
181
  # transpose q,k,v dim to fit scaled_dot_product_attention
182
  query = query.transpose(1, 2) # B * Head_num * length * dim
183
  key = key.transpose(1, 2) # B * Head_num * length * dim
184
  value = value.transpose(1, 2) # B * Head_num * length * dim
185
 
186
+ # cut: flex_attention score_mod path, replaced by plain SDPA below —
187
+ # flex_attention always traces internally, which crashed on this torch build.
188
+ # def score_mod(score, b, h, q_idx, kv_idx):
189
+ # return torch.where(attn_mask[b, q_idx] & attn_mask[b, kv_idx], score, float('-inf'))
190
+ # hidden_states = flex_attention(query, key, value, score_mod=score_mod)
191
+ hidden_states = F.scaled_dot_product_attention(query, key, value, attn_mask=attn_mask)
192
 
 
 
193
  # transpose back
194
  hidden_states = hidden_states.transpose(1, 2)
195