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

Function attention

models/transformer/wan/modules/tm2m_model.py:164–207  ·  view source on GitHub ↗
(
    q,
    k,
    v,
    q_lens=None,
    k_lens=None,
    dropout_p=0.,
    softmax_scale=None,
    q_scale=None,
    causal=False,
    window_size=(-1, -1),
    deterministic=False,
    dtype=torch.bfloat16,
    fa_version=None,
)

Source from the content-addressed store, hash-verified

162
163
164def attention(
165 q,
166 k,
167 v,
168 q_lens=None,
169 k_lens=None,
170 dropout_p=0.,
171 softmax_scale=None,
172 q_scale=None,
173 causal=False,
174 window_size=(-1, -1),
175 deterministic=False,
176 dtype=torch.bfloat16,
177 fa_version=None,
178):
179 if FLASH_ATTN_2_AVAILABLE or FLASH_ATTN_3_AVAILABLE:
180 return flash_attention(
181 q=q,
182 k=k,
183 v=v,
184 q_lens=q_lens,
185 k_lens=k_lens,
186 dropout_p=dropout_p,
187 softmax_scale=softmax_scale,
188 q_scale=q_scale,
189 causal=causal,
190 window_size=window_size,
191 deterministic=deterministic,
192 dtype=dtype,
193 version=fa_version,
194 )
195 else:
196 if q_lens is not None or k_lens is not None:
197 warnings.warn('Padding mask is disabled when using scaled_dot_product_attention. It can have a significant impact on performance.')
198 attn_mask = None
199
200 q = q.transpose(1, 2).to(dtype)
201 k = k.transpose(1, 2).to(dtype)
202 v = v.transpose(1, 2).to(dtype)
203
204 out = torch.nn.functional.scaled_dot_product_attention(q, k, v, attn_mask=attn_mask, is_causal=causal, dropout_p=dropout_p)
205
206 out = out.transpose(1, 2).contiguous()
207 return out
208
209
210

Callers

nothing calls this directly

Calls 1

flash_attentionFunction · 0.70

Tested by

no test coverage detected