{"record":{"id":"3f5f52b577cfe0a8","repo":"Comfy-Org/ComfyUI","slug":"input-img-tensor-must-be-in-b-c-h-w-format","errorCode":null,"errorMessage":"Input img tensor must be in [B, C, H, W] format.","messagePattern":"Input img tensor must be in \\[B, C, H, W\\] format\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"critical","filePath":"comfy/ldm/chroma_radiance/model.py","lineNumber":307,"sourceCode":"        # non zero during training to prevent 0 div\n        eps = 0.0\n        return (noisy - predicted) / (timesteps.view(-1,1,1,1) + eps)\n\n    def _forward(\n        self,\n        x: Tensor,\n        timestep: Tensor,\n        context: Tensor,\n        guidance: Optional[Tensor],\n        control: Optional[dict]=None,\n        transformer_options: dict={},\n        **kwargs: dict,\n    ) -> Tensor:\n        bs, c, h, w = x.shape\n        img = comfy.ldm.common_dit.pad_to_patch_size(x, (self.patch_size, self.patch_size))\n\n        if img.ndim != 4:\n            raise ValueError(\"Input img tensor must be in [B, C, H, W] format.\")\n        if context.ndim != 3:\n            raise ValueError(\"Input txt tensors must have 3 dimensions.\")\n\n        params = self.radiance_get_override_params(transformer_options.get(\"chroma_radiance_options\", {}))\n\n        h_len = (img.shape[-2] // self.patch_size)\n        w_len = (img.shape[-1] // self.patch_size)\n\n        img_ids = torch.zeros((h_len, w_len, 3), device=x.device, dtype=x.dtype)\n        img_ids[:, :, 1] = img_ids[:, :, 1] + torch.linspace(0, h_len - 1, steps=h_len, device=x.device, dtype=x.dtype).unsqueeze(1)\n        img_ids[:, :, 2] = img_ids[:, :, 2] + torch.linspace(0, w_len - 1, steps=w_len, device=x.device, dtype=x.dtype).unsqueeze(0)\n        img_ids = repeat(img_ids, \"h w c -> b (h w) c\", b=bs)\n        txt_ids = torch.zeros((bs, context.shape[1], 3), device=x.device, dtype=x.dtype)\n        # Radiance after 2026-05-22 uses sequential txt_ids instead of zeros\n        if params.use_sequential_txt_ids:\n            txt_ids[:, :, 0] = torch.arange(context.shape[1], device=x.device, dtype=x.dtype).unsqueeze(0).expand(bs, -1)\n\n        img_out = self.forward_orig(","sourceCodeStart":289,"sourceCodeEnd":325,"githubUrl":"https://github.com/Comfy-Org/ComfyUI/blob/1c6d8d45b3693bfbb32385b410d813a7fd6be216/comfy/ldm/chroma_radiance/model.py#L289-L325","documentation":"ChromaRadiance._forward expects the raw image/latent tensor x in [B, C, H, W]; after pad_to_patch_size it checks img.ndim == 4 before building positional ids over H/p x W/p patches. A non-4-D input (e.g. an already-tokenized [B, seq, dim] tensor, or a missing-batch [C, H, W] latent) fails immediately. Unlike Chroma's check (which validates the patchified 3-D tokens), radiance works on the 4-D image through its own conv patchifier, hence the different dimensionality contract.","triggerScenarios":"Calling ChromaRadiance._forward with x of shape [C, H, W] (unbatched), [B, seq, dim] (pre-tokenized), or a 5-D video-style tensor. The unpack 'bs, c, h, w = x.shape' one line earlier would itself fail for other ranks, so in practice this guard catches 4-D-shaped edge cases after padding and documents the contract.","commonSituations":"Custom nodes feeding radiance models pre-patchified features; dropping the batch dim when batching tiles manually; porting Chroma call code (which passes latents the same way) but with tensors already converted to sequence form.","solutions":["Pass a 4-D latent/image tensor [B, C, H, W]; add the batch dim with x.unsqueeze(0) if missing","Do not pre-patchify: the model applies img_in_patch (Conv2d with stride=patch_size) itself","Route through the standard model patcher / sampling loop instead of calling _forward directly"],"exampleFix":"# before\nout = model(x=latent[0], ...)  # [C, H, W] -> fails ndim check path\n\n# after\nlatent = latent.unsqueeze(0) if latent.ndim == 3 else latent\nout = model(x=latent, ...)","handlingStrategy":"validation","validationCode":"if x.ndim == 3:\n    x = x.unsqueeze(0)\nassert x.ndim == 4, f\"expected [B, C, H, W], got {tuple(x.shape)}\"","typeGuard":"def is_bchw(t) -> bool:\n    return isinstance(t, torch.Tensor) and t.ndim == 4","tryCatchPattern":null,"preventionTips":["Always feed 4-D latents/images to radiance forwards","Do not pre-patchify inputs; the model's img_in_patch conv does it"],"tags":["chroma-radiance","tensor-shape","forward"],"backgroundTag":null,"analyzedSha":"1c6d8d45b3693bfbb32385b410d813a7fd6be216","analyzedAt":"2026-08-14T19:37:18.893Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}