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

Class WanSelfAttention

models/transformer/wan/modules/tm2m_model.py:259–306  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

257
258
259class WanSelfAttention(nn.Module):
260
261 def __init__(self,
262 dim,
263 num_heads,
264 window_size=(-1, -1),
265 qk_norm=True,
266 eps=1e-6):
267 assert dim % num_heads == 0
268 super().__init__()
269 self.dim = dim
270 self.num_heads = num_heads
271 self.head_dim = dim // num_heads
272 self.window_size = window_size
273 self.qk_norm = qk_norm
274 self.eps = eps
275
276 # layers
277 self.q = nn.Linear(dim, dim)
278 self.k = nn.Linear(dim, dim)
279 self.v = nn.Linear(dim, dim)
280 self.o = nn.Linear(dim, dim)
281 self.norm_q = WanRMSNorm(dim, eps=eps) if qk_norm else nn.Identity()
282 self.norm_k = WanRMSNorm(dim, eps=eps) if qk_norm else nn.Identity()
283
284 def forward(self, x, seq_lens, freqs):
285 b, s, n, d = *x.shape[:2], self.num_heads, self.head_dim
286
287 # query, key, value function
288 def qkv_fn(x):
289 q = self.norm_q(self.q(x)).view(b, s, n, d)
290 k = self.norm_k(self.k(x)).view(b, s, n, d)
291 v = self.v(x).view(b, s, n, d)
292 return q, k, v
293
294 # import pdb; pdb.set_trace()
295 q, k, v = qkv_fn(x)
296 x = flash_attention(
297 q=q,
298 k=k,
299 v=v,
300 k_lens=seq_lens,
301 window_size=self.window_size)
302
303 # output
304 x = x.flatten(2)
305 x = self.o(x)
306 return x
307
308
309class WanT2VCrossAttention(WanSelfAttention):

Callers 2

__init__Method · 0.70
__init__Method · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected