Skip to content

Commit 0caad87

Browse files
committed
[fastwam] perf: use channels_last memory format in VAE encode
1 parent 59fea1b commit 0caad87

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

  • loongforge/embodied/model/fastwam/wan

loongforge/embodied/model/fastwam/wan/vae.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,10 @@ def forward(self, x, feat_cache=None, feat_idx=None):
186186
x = x.reshape(b, c, t * 2, h, w)
187187
t = x.shape[2]
188188
x = rearrange(x, 'b c t h w -> (b t) c h w')
189+
x = x.to(memory_format=torch.channels_last) # maintain NHWC for 4D ops
189190
x = self.resample(x)
190191
x = rearrange(x, '(b t) c h w -> b c t h w', t=t)
192+
x = x.to(memory_format=torch.channels_last_3d) # restore 5D NHWC
191193

192194
if self.mode == 'downsample3d':
193195
if feat_cache is not None:
@@ -362,6 +364,7 @@ def forward(self, x):
362364
identity = x
363365
b, c, t, h, w = x.size()
364366
x = rearrange(x, 'b c t h w -> (b t) c h w')
367+
x = x.to(memory_format=torch.channels_last) # maintain NHWC for 4D ops
365368
x = self.norm(x)
366369
# compute query, key, value
367370
q, k, v = self.to_qkv(x).reshape(b * t, 1, c * 3, -1).permute(
@@ -379,6 +382,7 @@ def forward(self, x):
379382
# output
380383
x = self.proj(x)
381384
x = rearrange(x, '(b t) c h w-> b c t h w', t=t)
385+
x = x.to(memory_format=torch.channels_last_3d) # restore 5D NHWC
382386
return x + identity
383387

384388

@@ -408,7 +412,7 @@ def forward(self, x: torch.Tensor) -> torch.Tensor:
408412
pad = (0, 0, 0, 0, pad_t, 0)
409413
x = F.pad(x, pad)
410414
B, C, T, H, W = x.shape
411-
x = x.view(
415+
x = x.reshape(
412416
B,
413417
C,
414418
T // self.factor_t,
@@ -1410,6 +1414,7 @@ def encode(self, x, scale=None):
14101414
"""Encode videos or tensors into latent states."""
14111415
self.clear_cache()
14121416
x = patchify(x, patch_size=2)
1417+
x = x.to(memory_format=torch.channels_last_3d) # NHWC to avoid per-layer format conversion
14131418
t = x.shape[2]
14141419
iter_ = 1 + (t - 1) // 4
14151420
for i in range(iter_):
@@ -1433,7 +1438,7 @@ def encode(self, x, scale=None):
14331438
scale = scale.to(dtype=mu.dtype, device=mu.device)
14341439
mu = (mu - scale[0]) * scale[1]
14351440
self.clear_cache()
1436-
return mu
1441+
return mu.contiguous()
14371442

14381443

14391444
def decode(self, z, scale=None):

0 commit comments

Comments
 (0)