hpcaitech/Open-Sora · error · ValueError

Expected 5D input tensor (B, C, D, H, W)

Error message

Expected 5D input tensor (B, C, D, H, W)

What it means

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.

Source

Thrown at opensora/models/dc_ae/models/nn/vo_ops.py:100


def chunked_interpolate(x, scale_factor, mode="nearest"):
    """
    Interpolate large tensors by chunking along the channel dimension. https://discuss.pytorch.org/t/error-using-f-interpolate-for-large-3d-input/207859
    Only supports 'nearest' interpolation mode.

    Args:
        x (torch.Tensor): Input tensor (B, C, D, H, W)
        scale_factor: Tuple of scaling factors (d, h, w)

    Returns:
        torch.Tensor: Interpolated tensor
    """
    assert (
        mode == "nearest"
    ), "Only the nearest mode is supported"  # actually other modes are theoretically supported but not tested
    if len(x.shape) != 5:
        raise ValueError("Expected 5D input tensor (B, C, D, H, W)")

    # Calculate max chunk size to avoid int32 overflow. num_elements < max_int32
    # Max int32 is 2^31 - 1
    max_elements_per_chunk = 2**31 - 1

    # Calculate output spatial dimensions
    out_d = math.ceil(x.shape[2] * scale_factor[0])
    out_h = math.ceil(x.shape[3] * scale_factor[1])
    out_w = math.ceil(x.shape[4] * scale_factor[2])

    # Calculate max channels per chunk to stay under limit
    elements_per_channel = out_d * out_h * out_w
    max_channels = max_elements_per_chunk // (x.shape[0] * elements_per_channel)

    # Use smaller of max channels or input channels
    chunk_size = min(max_channels, x.shape[1])

    # Ensure at least 1 channel per chunk

View on GitHub (pinned to 7ad6a96a13)

Solutions

  1. Reshape/unsqueeze the input to 5D: x.unsqueeze(2) for images (D=1)
  2. Use a standard F.interpolate path for 4D image tensors instead of the video chunked helper
  3. Ensure mode='nearest'

Example fix

# before
y = chunked_interpolate(img_4d, scale=(2,2), mode='nearest')
# after
y = chunked_interpolate(img_4d.unsqueeze(2), scale=(1,2,2), mode='nearest')
Defensive patterns

Strategy: type-guard

Validate before calling

if x.dim() != 5:
    x = x.unsqueeze(2) if x.dim() == 4 else x  # promote 4D image to 5D with D=1
assert x.dim() == 5, f'expected 5D (B,C,D,H,W), got {tuple(x.shape)}'

Type guard

def is_5d_video_tensor(x: torch.Tensor) -> bool:
    return x.dim() == 5 and x.shape[0] > 0

Prevention

When it happens

Trigger: 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.

Common situations: Reusing the video interpolation helper for image tensors; forgetting to unsqueeze the temporal dimension D=1 when adapting image pipelines.

Related errors


AI-assisted analysis of hpcaitech/Open-Sora@7ad6a96a13 (2026-08-28). Data as JSON: /api/errors/3888f718367d6511. Report an issue: GitHub.