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

Function attention

models/transformer/wan/modules/t2m_model.py:163–206  ·  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

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

Callers

nothing calls this directly

Calls 1

flash_attentionFunction · 0.70

Tested by

no test coverage detected