MCPcopy Create free account
hub / github.com/TencentARC/MotionCtrl / TemporalTransformer_forward

Function TemporalTransformer_forward

motionctrl/lvdm_modified_modules.py:19–69  ·  view source on GitHub ↗
(self, x, context=None, is_imgbatch=False)

Source from the content-addressed store, hash-verified

17
18
19def TemporalTransformer_forward(self, x, context=None, is_imgbatch=False):
20 b, c, t, h, w = x.shape
21 x_in = x
22 x = self.norm(x)
23 x = rearrange(x, 'b c t h w -> (b h w) c t').contiguous()
24 if not self.use_linear:
25 x = self.proj_in(x)
26 x = rearrange(x, 'bhw c t -> bhw t c').contiguous()
27 if self.use_linear:
28 x = self.proj_in(x)
29
30 temp_mask = None
31 if self.causal_attention:
32 temp_mask = torch.tril(torch.ones([1, t, t]))
33 if is_imgbatch:
34 temp_mask = torch.eye(t).unsqueeze(0)
35 if temp_mask is not None:
36 mask = temp_mask.to(x.device)
37 mask = repeat(mask, 'l i j -> (l bhw) i j', bhw=b*h*w)
38 else:
39 mask = None
40
41 if self.only_self_att:
42 ## note: if no context is given, cross-attention defaults to self-attention
43 for i, block in enumerate(self.transformer_blocks):
44 x = block(x, context=context, mask=mask)
45 x = rearrange(x, '(b hw) t c -> b hw t c', b=b).contiguous()
46 else:
47 x = rearrange(x, '(b hw) t c -> b hw t c', b=b).contiguous()
48 context = rearrange(context, '(b t) l con -> b t l con', t=t).contiguous()
49 for i, block in enumerate(self.transformer_blocks):
50 # calculate each batch one by one (since number in shape could not greater then 65,535 for some package)
51 for j in range(b):
52 unit_context = context[j][0:1]
53 context_j = repeat(unit_context, 't l con -> (t r) l con', r=(h * w)).contiguous()
54 ## note: causal mask will not applied in cross-attention case
55 x[j] = block(x[j], context=context_j)
56
57 if self.use_linear:
58 x = self.proj_out(x)
59 x = rearrange(x, 'b (h w) t c -> b c t h w', h=h, w=w).contiguous()
60 if not self.use_linear:
61 x = rearrange(x, 'b hw t c -> (b hw) c t').contiguous()
62 x = self.proj_out(x)
63 x = rearrange(x, '(b h w) c t -> b c t h w', b=b, h=h, w=w).contiguous()
64
65 if self.use_image_dataset:
66 x = 0.0 * x + x_in
67 else:
68 x = x + x_in
69 return x
70
71def selfattn_forward_unet(self, x, timesteps, context=None, y=None, features_adapter=None, is_imgbatch=False, T=None, **kwargs):
72 b,_,t,_,_ = x.shape

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected