Skip to content

Commit 6adfae5

Browse files
author
Siyi Chen
committed
Address review: move per-frame timestep into forward(), robust t handling
1 parent 81c4cb3 commit 6adfae5

1 file changed

Lines changed: 27 additions & 7 deletions

File tree

fastgen/networks/cosmos_predict2/network.py

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,20 +1232,29 @@ def sample(
12321232
t_per_frame = t
12331233

12341234
# Wrap condition with mask for forward() to use
1235-
cond_with_mask = {"text_embeds": condition, "condition_mask": condition_mask}
1236-
neg_cond_with_mask = {"text_embeds": neg_condition, "condition_mask": condition_mask}
1235+
# cond_with_mask = {"text_embeds": condition, "condition_mask": condition_mask}
1236+
cond_with_mask = {
1237+
"text_embeds": condition,
1238+
"conditioning_latents": conditioning_latents,
1239+
"condition_mask": condition_mask,
1240+
}
1241+
neg_cond_with_mask = {
1242+
"text_embeds": neg_condition,
1243+
"conditioning_latents": conditioning_latents,
1244+
"condition_mask": condition_mask,
1245+
}
12371246
else:
12381247
model_input = latents
12391248
t_per_frame = t
12401249
cond_with_mask = condition
12411250
neg_cond_with_mask = neg_condition
12421251

12431252
# Forward pass
1244-
velocity_pred = self(model_input, t_per_frame, cond_with_mask, fps=fps)
1253+
velocity_pred = self(model_input, t_per_frame, cond_with_mask, fps=fps, conditional_frame_timestep=conditional_frame_timestep)
12451254

12461255
# Classifier-free guidance
12471256
if guidance_scale > 1.0:
1248-
velocity_uncond = self(model_input, t_per_frame, neg_cond_with_mask, fps=fps)
1257+
velocity_uncond = self(model_input, t_per_frame, neg_cond_with_mask, fps=fps, conditional_frame_timestep=conditional_frame_timestep)
12491258
velocity_pred = velocity_uncond + guidance_scale * (velocity_pred - velocity_uncond)
12501259

12511260
# Replace velocity for conditioning frames with analytical velocity: v = noise - x0
@@ -1278,6 +1287,7 @@ def forward(
12781287
fps: Optional[torch.Tensor] = None,
12791288
padding_mask: Optional[torch.Tensor] = None,
12801289
skip_layers: Optional[List[int]] = None,
1290+
conditional_frame_timestep: float = 0.0,
12811291
**fwd_kwargs,
12821292
) -> Union[torch.Tensor, List[torch.Tensor], Tuple[torch.Tensor, torch.Tensor]]:
12831293
"""
@@ -1304,6 +1314,8 @@ def forward(
13041314
fps: Frames per second tensor
13051315
padding_mask: Padding mask tensor
13061316
skip_layers: List of block indices to skip during forward pass
1317+
conditional_frame_timestep: Timestep value for conditioning frames (default 0.0).
1318+
Use 0.0 to indicate clean frames with no noise.
13071319
13081320
Returns:
13091321
Depending on the arguments:
@@ -1340,6 +1352,7 @@ def forward(
13401352

13411353
# Video2world training: replace input frames with conditioning latents
13421354
model_input = x_t
1355+
transformer_t = t
13431356
if conditioning_latents is not None and condition_mask is not None:
13441357
B, C, T, H, W = x_t.shape
13451358
# Expand condition_mask to channel dimension
@@ -1356,13 +1369,20 @@ def forward(
13561369
# model knows they are noise-free and should not be denoised. Without this, the
13571370
# model treats the clean first frame as a fully-noised frame, causing incoherent
13581371
# video2world (image2world) generation.
1359-
t_expanded = t.unsqueeze(1).expand(B, T)
1372+
# t_expanded = t.unsqueeze(1).expand(B, T)
1373+
if t.ndim == 1:
1374+
t_expanded = t.unsqueeze(1).expand(B, T)
1375+
else:
1376+
t_expanded = t.expand(B, T)
13601377
mask_B_T = condition_mask[:, 0, :, 0, 0] # (B, T)
1361-
t = t_expanded * (1 - mask_B_T)
1378+
transformer_t = conditional_frame_timestep * mask_B_T + t_expanded * (1 - mask_B_T)
1379+
1380+
# Reshape for convert_model_output broadcasting against (B, C, T, H, W)
1381+
t = transformer_t.reshape(B, 1, T, 1, 1)
13621382

13631383
model_outputs = self.transformer(
13641384
x_B_C_T_H_W=model_input,
1365-
timesteps_B_T=t,
1385+
timesteps_B_T=transformer_t,
13661386
crossattn_emb=text_embeds,
13671387
fps=fps,
13681388
padding_mask=padding_mask,

0 commit comments

Comments
 (0)