MCPcopy Create free account
hub / github.com/Francis-Rings/FlashPortrait / AttentionBlock

Class AttentionBlock

wan/models/wan_vae.py:227–266  ·  view source on GitHub ↗

Causal self-attention with a single head.

Source from the content-addressed store, hash-verified

225
226
227class AttentionBlock(nn.Module):
228 """
229 Causal self-attention with a single head.
230 """
231
232 def __init__(self, dim):
233 super().__init__()
234 self.dim = dim
235
236 # layers
237 self.norm = RMS_norm(dim)
238 self.to_qkv = nn.Conv2d(dim, dim * 3, 1)
239 self.proj = nn.Conv2d(dim, dim, 1)
240
241 # zero out the last layer params
242 nn.init.zeros_(self.proj.weight)
243
244 def forward(self, x):
245 identity = x
246 b, c, t, h, w = x.size()
247 x = rearrange(x, 'b c t h w -> (b t) c h w')
248 x = self.norm(x)
249 # compute query, key, value
250 q, k, v = self.to_qkv(x).reshape(b * t, 1, c * 3,
251 -1).permute(0, 1, 3,
252 2).contiguous().chunk(
253 3, dim=-1)
254
255 # apply attention
256 x = F.scaled_dot_product_attention(
257 q,
258 k,
259 v,
260 )
261 x = x.squeeze(1).permute(0, 2, 1).reshape(b * t, c, h, w)
262
263 # output
264 x = self.proj(x)
265 x = rearrange(x, '(b t) c h w-> b c t h w', t=t)
266 return x + identity
267
268
269class Encoder3d(nn.Module):

Callers 2

__init__Method · 0.70
__init__Method · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected