MCPcopy Create free account
hub / github.com/MotrixLab/ViMoGen / create_sdpa_mask

Function create_sdpa_mask

models/transformer/wan/modules/tm2m_model.py:144–161  ·  view source on GitHub ↗
(q, k, q_lens, k_lens, causal=False)

Source from the content-addressed store, hash-verified

142
143
144def create_sdpa_mask(q, k, q_lens, k_lens, causal=False):
145 b, lq, lk = q.size(0), q.size(1), k.size(1)
146 if q_lens is None:
147 q_lens = torch.tensor([lq] * b, dtype=torch.int32)
148 if k_lens is None:
149 k_lens = torch.tensor([lk] * b, dtype=torch.int32)
150 attn_mask = torch.zeros((b, lq, lk), dtype=torch.bool)
151 for i in range(b):
152 q_len, k_len = q_lens[i], k_lens[i]
153 attn_mask[i, q_len:, :] = True
154 attn_mask[i, :, k_len:] = True
155
156 if causal:
157 causal_mask = torch.triu(torch.ones((lq, lk), dtype=torch.bool), diagonal=1)
158 attn_mask[i, :, :] = torch.logical_or(attn_mask[i, :, :], causal_mask)
159
160 attn_mask = attn_mask.logical_not().to(q.device, non_blocking=True)
161 return attn_mask
162
163
164def attention(

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected