Skip to content
This repository was archived by the owner on May 20, 2026. It is now read-only.

Commit c268a52

Browse files
akoumpalbliii
authored andcommitted
fix: support batch size > 1 (#58)
* replace torch.stack with torch.cat Signed-off-by: Alexandros Koumparoulis <akoumparouli@nvidia.com> * fix Signed-off-by: Alexandros Koumparoulis <akoumparouli@nvidia.com> --------- Signed-off-by: Alexandros Koumparoulis <akoumparouli@nvidia.com> Signed-off-by: Lawrence Lane <llane@nvidia.com>
1 parent e4d392b commit c268a52

4 files changed

Lines changed: 487 additions & 10 deletions

File tree

dfm/src/automodel/datasets/wan21.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,12 @@ def __getitem__(self, index: int) -> Dict[str, torch.Tensor]: # type: ignore[ov
135135

136136

137137
def collate_fn(batch: List[Dict[str, torch.Tensor]]) -> Dict[str, torch.Tensor]:
138-
text_embeddings = torch.stack([item["text_embeddings"] for item in batch])
139-
video_latents = torch.stack([item["video_latents"] for item in batch])
138+
if len(batch) > 0:
139+
assert batch[0]["text_embeddings"].ndim == 3, "Expected text_embeddings.ndim to be 3"
140+
assert batch[0]["video_latents"].ndim == 5, "Expected video_latents.ndim to be 5"
141+
# use cat to stack the tensors in the batch
142+
text_embeddings = torch.cat([item["text_embeddings"] for item in batch], dim=0)
143+
video_latents = torch.cat([item["video_latents"] for item in batch], dim=0)
140144
return {
141145
"text_embeddings": text_embeddings,
142146
"video_latents": video_latents,

dfm/src/automodel/flow_matching/training_step_t2v.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,16 @@ def step_fsdp_transformer_t2v(
5959
video_latents = batch["video_latents"].to(device, dtype=bf16)
6060
text_embeddings = batch["text_embeddings"].to(device, dtype=bf16)
6161

62+
assert video_latents.ndim in (4, 5), "Expected video_latents.ndim to be 4 or 5 "
63+
assert text_embeddings.ndim in (2, 3), "Expected text_embeddings.ndim to be 2 or 3 "
6264
# Handle tensor shapes
63-
while video_latents.ndim > 5:
64-
video_latents = video_latents.squeeze(0)
6565
if video_latents.ndim == 4:
6666
video_latents = video_latents.unsqueeze(0)
6767

68-
while text_embeddings.ndim > 3:
69-
text_embeddings = text_embeddings.squeeze(0)
7068
if text_embeddings.ndim == 2:
7169
text_embeddings = text_embeddings.unsqueeze(0)
7270

73-
batch_size = video_latents.shape[0]
74-
_, channels, frames, height, width = video_latents.shape
71+
batch_size, channels, frames, height, width = video_latents.shape
7572

7673
# ========================================================================
7774
# Flow Matching Timestep Sampling

0 commit comments

Comments
 (0)