sgl-project/sglang · error · ValueError

condition_image tensor must be CHW or HWC with 1, 3, or 4 ch

Error message

condition_image tensor must be CHW or HWC with 1, 3, or 4 channels; got {tuple(image.shape)}.

What it means

Raised by _canonical_condition_image_tensor when a 3D condition_image tensor is neither CHW (first dim 1/3/4) nor HWC (last dim 1/3/4). Ambiguous channel layouts cannot be canonicalized.

Source

Thrown at python/sglang/multimodal_gen/runtime/pipelines_core/stages/model_specific_stages/sana_wm/base.py:1228

                raise ValueError(
                    "SANA-WM seed list length must be 1 or match latent batch "
                    f"size; got {len(seed)} seeds for batch {batch_size}."
                )
        return torch.Generator(device=device).manual_seed(int(seed))

    @staticmethod
    def _canonical_condition_image_tensor(image: torch.Tensor) -> torch.Tensor:
        """Return image as NCHW RGB float tensor without changing its value range."""
        image = image.float()
        if image.dim() == 5 and image.shape[2] == 1:
            image = image.squeeze(2)
        if image.dim() == 3:
            if image.shape[0] in (1, 3, 4):
                image = image.unsqueeze(0)
            elif image.shape[-1] in (1, 3, 4):
                image = image.permute(2, 0, 1).unsqueeze(0)
            else:
                raise ValueError(
                    "condition_image tensor must be CHW or HWC with 1, 3, "
                    f"or 4 channels; got {tuple(image.shape)}."
                )
        elif image.dim() == 4:
            if image.shape[1] in (1, 3, 4):
                pass
            elif image.shape[-1] in (1, 3, 4):
                image = image.permute(0, 3, 1, 2)
            else:
                raise ValueError(
                    "condition_image tensor must be NCHW or NHWC with 1, 3, "
                    f"or 4 channels; got {tuple(image.shape)}."
                )
        else:
            raise ValueError(
                "condition_image tensor must have shape CHW, HWC, NCHW, NHWC, "
                f"or NCHW singleton-video; got {tuple(image.shape)}."
            )

View on GitHub (pinned to 0132848349)

Solutions

  1. Ensure the tensor is CHW or HWC with 1, 3, or 4 channels
  2. Convert 8-channel latent-like tensors to an image before conditioning (e.g. decode or take first 3 channels)
  3. For (H,W) grayscale, unsqueeze a channel dim first

Example fix

# before
img = torch.rand(8, 512, 512)  # 8 channels
# after
img = img[:3]  # (3,512,512) CHW
Defensive patterns

Strategy: type-guard

Validate before calling

assert image.dim() in (3, 4) and (image.dim() != 3 or image.shape[0] in (1,3,4) or image.shape[-1] in (1,3,4))

Type guard

def condition_image_ok(t) -> bool:
    if t.dim() != 3: return True
    return t.shape[0] in (1, 3, 4) or t.shape[-1] in (1, 3, 4)

Prevention

When it happens

Trigger: Passing a 3D tensor like (8, 512, 512) or (512, 512, 8) where neither leading nor trailing dim is a valid channel count. Called via _resize_center_crop_tensor.

Common situations: Condition images with 8-channel latents passed as raw images; grayscale stacked into 2 channels; wrong axis order after a custom transform.

Related errors


AI-assisted analysis of sgl-project/sglang@0132848349 (2026-08-28). Data as JSON: /api/errors/407cb3c4e87e68fb. Report an issue: GitHub.