{"record":{"id":"3888f718367d6511","repo":"hpcaitech/Open-Sora","slug":"expected-5d-input-tensor-b-c-d-h-w","errorCode":null,"errorMessage":"Expected 5D input tensor (B, C, D, H, W)","messagePattern":"Expected 5D input tensor \\(B, C, D, H, W\\)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"opensora/models/dc_ae/models/nn/vo_ops.py","lineNumber":100,"sourceCode":"\n\ndef chunked_interpolate(x, scale_factor, mode=\"nearest\"):\n    \"\"\"\n    Interpolate large tensors by chunking along the channel dimension. https://discuss.pytorch.org/t/error-using-f-interpolate-for-large-3d-input/207859\n    Only supports 'nearest' interpolation mode.\n\n    Args:\n        x (torch.Tensor): Input tensor (B, C, D, H, W)\n        scale_factor: Tuple of scaling factors (d, h, w)\n\n    Returns:\n        torch.Tensor: Interpolated tensor\n    \"\"\"\n    assert (\n        mode == \"nearest\"\n    ), \"Only the nearest mode is supported\"  # actually other modes are theoretically supported but not tested\n    if len(x.shape) != 5:\n        raise ValueError(\"Expected 5D input tensor (B, C, D, H, W)\")\n\n    # Calculate max chunk size to avoid int32 overflow. num_elements < max_int32\n    # Max int32 is 2^31 - 1\n    max_elements_per_chunk = 2**31 - 1\n\n    # Calculate output spatial dimensions\n    out_d = math.ceil(x.shape[2] * scale_factor[0])\n    out_h = math.ceil(x.shape[3] * scale_factor[1])\n    out_w = math.ceil(x.shape[4] * scale_factor[2])\n\n    # Calculate max channels per chunk to stay under limit\n    elements_per_channel = out_d * out_h * out_w\n    max_channels = max_elements_per_chunk // (x.shape[0] * elements_per_channel)\n\n    # Use smaller of max channels or input channels\n    chunk_size = min(max_channels, x.shape[1])\n\n    # Ensure at least 1 channel per chunk","sourceCodeStart":82,"sourceCodeEnd":118,"githubUrl":"https://github.com/hpcaitech/Open-Sora/blob/7ad6a96a135feb81f755c84fb391818718f6beb2/opensora/models/dc_ae/models/nn/vo_ops.py#L82-L118","documentation":"chunked_interpolate only accepts 5D video tensors (B,C,D,H,W) because its chunking logic avoids int32 overflow in the interpolation kernel by splitting over spatial dims of 5D inputs. A 4D image tensor or any other rank raises this ValueError immediately.","triggerScenarios":"Calling chunked_interpolate (directly or via a video model's forward / test_chunked_interpolate) with x.dim() != 5, e.g. a (B,C,H,W) image tensor; also passing a mode other than 'nearest' trips the preceding assert.","commonSituations":"Reusing the video interpolation helper for image tensors; forgetting to unsqueeze the temporal dimension D=1 when adapting image pipelines.","solutions":["Reshape/unsqueeze the input to 5D: x.unsqueeze(2) for images (D=1)","Use a standard F.interpolate path for 4D image tensors instead of the video chunked helper","Ensure mode='nearest'"],"exampleFix":"# before\ny = chunked_interpolate(img_4d, scale=(2,2), mode='nearest')\n# after\ny = chunked_interpolate(img_4d.unsqueeze(2), scale=(1,2,2), mode='nearest')","handlingStrategy":"type-guard","validationCode":"if x.dim() != 5:\n    x = x.unsqueeze(2) if x.dim() == 4 else x  # promote 4D image to 5D with D=1\nassert x.dim() == 5, f'expected 5D (B,C,D,H,W), got {tuple(x.shape)}'","typeGuard":"def is_5d_video_tensor(x: torch.Tensor) -> bool:\n    return x.dim() == 5 and x.shape[0] > 0","tryCatchPattern":null,"preventionTips":["Unsqueeze a temporal dim (D=1) before calling video interpolation helpers","Use F.interpolate directly for 4D image tensors","Add shape guards at model input boundaries"],"tags":["tensor-shape","interpolation","video","validation"],"backgroundTag":"invalid-tensor-shape","analyzedSha":"7ad6a96a135feb81f755c84fb391818718f6beb2","analyzedAt":"2026-08-28T16:58:37.171Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}