{"record":{"id":"f839df0ac9bb9ac2","repo":"sgl-project/sglang","slug":"pred-noise-to-pred-video-invalid-timestep-shape","errorCode":null,"errorMessage":"[pred_noise_to_pred_video] Invalid timestep shape: {timestep.shape}","messagePattern":"\\[pred_noise_to_pred_video\\] Invalid timestep shape: (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/multimodal_gen/runtime/pipelines_core/diffusion_scheduler_utils.py","lineNumber":67,"sourceCode":"\n\ndef pred_noise_to_pred_video(\n    pred_noise: torch.Tensor,\n    noise_input_latent: torch.Tensor,\n    timestep: torch.Tensor,\n    scheduler: Any,\n) -> torch.Tensor:\n    \"\"\"Convert predicted noise to clean latent.\"\"\"\n    if timestep.ndim == 2:\n        timestep = timestep.flatten(0, 1)\n        assert timestep.numel() == noise_input_latent.shape[0]\n    elif timestep.ndim == 1:\n        if timestep.shape[0] == 1:\n            timestep = timestep.expand(noise_input_latent.shape[0])\n        else:\n            assert timestep.numel() == noise_input_latent.shape[0]\n    else:\n        raise ValueError(\n            f\"[pred_noise_to_pred_video] Invalid timestep shape: {timestep.shape}\"\n        )\n\n    dtype = pred_noise.dtype\n    device = pred_noise.device\n    pred_noise = pred_noise.double().to(device)\n    noise_input_latent = noise_input_latent.double().to(device)\n    sigmas = scheduler.sigmas.double().to(device)\n    high_dtype = (\n        torch.float64 if current_platform.is_float64_supported() else torch.float32\n    )\n    timesteps = scheduler.timesteps.to(high_dtype).to(device)\n    timestep_id = torch.argmin(\n        (timesteps.unsqueeze(0) - timestep.unsqueeze(1)).abs(), dim=1\n    )\n    sigma_t = sigmas[timestep_id].reshape(-1, 1, 1, 1)\n    pred_video = noise_input_latent - sigma_t * pred_noise\n    return pred_video.to(dtype)","sourceCodeStart":49,"sourceCodeEnd":85,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/multimodal_gen/runtime/pipelines_core/diffusion_scheduler_utils.py#L49-L85","documentation":"pred_noise_to_pred_video validates that the timestep tensor is 0-D (scalar) or 1-D; anything else (2-D, 3-D, ...) raises this error. The scheduler needs a scalar or per-sample timestep to align with the noise_input_latent batch dimension for x0 prediction in diffusion sampling. A wrong-rank timestep almost always means the caller passed an un-squeezed or batched-padded tensor.","triggerScenarios":"Calling the scheduler's forward (or _predict_x0_btchw) with timestep of ndim >= 2, or a 1-D tensor whose length mismatches noise_input_latent.shape[0] (that path asserts instead). E.g. passing timestep.reshape(B,1) or a (B,1,1) tensor from a training-style code path.","commonSituations":"Porting training-loop code (which often uses (B,1) timesteps) into the inference scheduler; passing timesteps from a different diffusion library (diffusers uses (B,) or scalar); shape drift after batching refactor.","solutions":["Squeeze the timestep to a scalar or 1-D tensor: timestep.reshape(-1) or timestep.squeeze() before calling","Pass a scalar (0-D) tensor when the whole batch shares a timestep","Verify len(timestep) == noise_input_latent.shape[0] for the 1-D case"],"exampleFix":"# before\npred = scheduler.pred_noise_to_pred_video(t, model_out, noise_latent)  # t.shape == (B,1)\n# after\nt = t.reshape(-1) if t.ndim == 2 and t.shape[1] == 1 else t\npred = scheduler.pred_noise_to_pred_video(t, model_out, noise_latent)","handlingStrategy":"validation","validationCode":"assert timestep.ndim <= 1, f\"timestep must be 0/1-D, got {tuple(timestep.shape)}\"\nif timestep.ndim == 1 and timestep.shape[0] == 1:\n    timestep = timestep.expand(noise_latent.shape[0])\nout = scheduler.pred_noise_to_pred_video(timestep, pred_noise, noise_latent)","typeGuard":"def is_valid_timestep(t: torch.Tensor, batch: int) -> bool:\n    return t.ndim == 0 or (t.ndim == 1 and t.numel() in (1, batch))","tryCatchPattern":null,"preventionTips":["Standardize on scalar or flat (B,) timesteps in inference code","Never pass training-style (B,1) timestep tensors into the scheduler"],"tags":["tensor-shape","diffusion","scheduler","validation"],"backgroundTag":"invalid-tensor-shape","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}