{"record":{"id":"407cb3c4e87e68fb","repo":"sgl-project/sglang","slug":"condition-image-tensor-must-be-chw-or-hwc-with-1","errorCode":null,"errorMessage":"condition_image tensor must be CHW or HWC with 1, 3, or 4 channels; got {tuple(image.shape)}.","messagePattern":"condition_image tensor must be CHW or HWC with 1, 3, or 4 channels; got (.+?)\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/multimodal_gen/runtime/pipelines_core/stages/model_specific_stages/sana_wm/base.py","lineNumber":1228,"sourceCode":"                raise ValueError(\n                    \"SANA-WM seed list length must be 1 or match latent batch \"\n                    f\"size; got {len(seed)} seeds for batch {batch_size}.\"\n                )\n        return torch.Generator(device=device).manual_seed(int(seed))\n\n    @staticmethod\n    def _canonical_condition_image_tensor(image: torch.Tensor) -> torch.Tensor:\n        \"\"\"Return image as NCHW RGB float tensor without changing its value range.\"\"\"\n        image = image.float()\n        if image.dim() == 5 and image.shape[2] == 1:\n            image = image.squeeze(2)\n        if image.dim() == 3:\n            if image.shape[0] in (1, 3, 4):\n                image = image.unsqueeze(0)\n            elif image.shape[-1] in (1, 3, 4):\n                image = image.permute(2, 0, 1).unsqueeze(0)\n            else:\n                raise ValueError(\n                    \"condition_image tensor must be CHW or HWC with 1, 3, \"\n                    f\"or 4 channels; got {tuple(image.shape)}.\"\n                )\n        elif image.dim() == 4:\n            if image.shape[1] in (1, 3, 4):\n                pass\n            elif image.shape[-1] in (1, 3, 4):\n                image = image.permute(0, 3, 1, 2)\n            else:\n                raise ValueError(\n                    \"condition_image tensor must be NCHW or NHWC with 1, 3, \"\n                    f\"or 4 channels; got {tuple(image.shape)}.\"\n                )\n        else:\n            raise ValueError(\n                \"condition_image tensor must have shape CHW, HWC, NCHW, NHWC, \"\n                f\"or NCHW singleton-video; got {tuple(image.shape)}.\"\n            )","sourceCodeStart":1210,"sourceCodeEnd":1246,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/multimodal_gen/runtime/pipelines_core/stages/model_specific_stages/sana_wm/base.py#L1210-L1246","documentation":"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.","triggerScenarios":"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.","commonSituations":"Condition images with 8-channel latents passed as raw images; grayscale stacked into 2 channels; wrong axis order after a custom transform.","solutions":["Ensure the tensor is CHW or HWC with 1, 3, or 4 channels","Convert 8-channel latent-like tensors to an image before conditioning (e.g. decode or take first 3 channels)","For (H,W) grayscale, unsqueeze a channel dim first"],"exampleFix":"# before\nimg = torch.rand(8, 512, 512)  # 8 channels\n# after\nimg = img[:3]  # (3,512,512) CHW","handlingStrategy":"type-guard","validationCode":"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))","typeGuard":"def condition_image_ok(t) -> bool:\n    if t.dim() != 3: return True\n    return t.shape[0] in (1, 3, 4) or t.shape[-1] in (1, 3, 4)","tryCatchPattern":null,"preventionTips":["Standardize condition images to CHW 3-channel before the pipeline","Don't feed latent-space tensors where images are expected"],"tags":["image-preprocessing","channels","shape-validation","sana-wm"],"backgroundTag":"tensor-shape-mismatch","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}