| 263 | self.resample = nn.Identity() |
| 264 | |
| 265 | def forward(self, x, feat_cache=None, feat_idx=[0]): |
| 266 | b, c, t, h, w = x.size() |
| 267 | if self.mode == "upsample3d": |
| 268 | if feat_cache is not None: |
| 269 | idx = feat_idx[0] |
| 270 | if feat_cache[idx] is None: |
| 271 | feat_cache[idx] = "Rep" |
| 272 | feat_idx[0] += 1 |
| 273 | else: |
| 274 | cache_x = x[:, :, -CACHE_T:, :, :].clone() |
| 275 | if cache_x.shape[2] < 2 and feat_cache[idx] is not None and feat_cache[idx] != "Rep": |
| 276 | # cache last frame of last two chunk |
| 277 | cache_x = torch.cat( |
| 278 | [feat_cache[idx][:, :, -1, :, :].unsqueeze(2).to(cache_x.device), cache_x], dim=2 |
| 279 | ) |
| 280 | if cache_x.shape[2] < 2 and feat_cache[idx] is not None and feat_cache[idx] == "Rep": |
| 281 | cache_x = torch.cat([torch.zeros_like(cache_x).to(cache_x.device), cache_x], dim=2) |
| 282 | if feat_cache[idx] == "Rep": |
| 283 | x = self.time_conv(x) |
| 284 | else: |
| 285 | x = self.time_conv(x, feat_cache[idx]) |
| 286 | feat_cache[idx] = cache_x |
| 287 | feat_idx[0] += 1 |
| 288 | |
| 289 | x = x.reshape(b, 2, c, t, h, w) |
| 290 | x = torch.stack((x[:, 0, :, :, :, :], x[:, 1, :, :, :, :]), 3) |
| 291 | x = x.reshape(b, c, t * 2, h, w) |
| 292 | t = x.shape[2] |
| 293 | x = x.permute(0, 2, 1, 3, 4).reshape(b * t, c, h, w) |
| 294 | x = self.resample(x) |
| 295 | x = x.view(b, t, x.size(1), x.size(2), x.size(3)).permute(0, 2, 1, 3, 4) |
| 296 | |
| 297 | if self.mode == "downsample3d": |
| 298 | if feat_cache is not None: |
| 299 | idx = feat_idx[0] |
| 300 | if feat_cache[idx] is None: |
| 301 | feat_cache[idx] = x.clone() |
| 302 | feat_idx[0] += 1 |
| 303 | else: |
| 304 | cache_x = x[:, :, -1:, :, :].clone() |
| 305 | x = self.time_conv(torch.cat([feat_cache[idx][:, :, -1:, :, :], x], 2)) |
| 306 | feat_cache[idx] = cache_x |
| 307 | feat_idx[0] += 1 |
| 308 | return x |
| 309 | |
| 310 | |
| 311 | class WanResidualBlock(nn.Module): |