Comfy-Org/ComfyUI · critical · ValueError

Input img and txt tensors must have 3 dimensions.

Error message

Input img and txt tensors must have 3 dimensions.

What it means

Chroma's _forward patchifies the input latent to img tokens of shape [B, seq, c*ph*pw] and expects the text context to already be [B, seq_txt, dim]. If either tensor is not 3-D after patchify/rearrange, downstream attention (img-txt concatenation, RoPE) would silently mis-broadcast, so an explicit ValueError guards it. In practice the check fires on context.ndim != 3 far more often than on img, because img is reshaped one line above into 3-D unconditionally.

Source

Thrown at comfy/ldm/chroma/model.py:284

            final_mod = self.get_modulations(mod_vectors, "final")
            img = self.final_layer(img, vec=final_mod)  # (N, T, patch_size ** 2 * out_channels)
        return img

    def forward(self, x, timestep, context, guidance, control=None, transformer_options={}, **kwargs):
        return comfy.patcher_extension.WrapperExecutor.new_class_executor(
            self._forward,
            self,
            comfy.patcher_extension.get_all_wrappers(comfy.patcher_extension.WrappersMP.DIFFUSION_MODEL, transformer_options)
        ).execute(x, timestep, context, guidance, control, transformer_options, **kwargs)

    def _forward(self, x, timestep, context, guidance, control=None, transformer_options={}, **kwargs):
        bs, c, h, w = x.shape
        x = comfy.ldm.common_dit.pad_to_patch_size(x, (self.patch_size, self.patch_size))

        img = rearrange(x, "b c (h ph) (w pw) -> b (h w) (c ph pw)", ph=self.patch_size, pw=self.patch_size)

        if img.ndim != 3 or context.ndim != 3:
            raise ValueError("Input img and txt tensors must have 3 dimensions.")

        h_len = ((h + (self.patch_size // 2)) // self.patch_size)
        w_len = ((w + (self.patch_size // 2)) // self.patch_size)
        img_ids = torch.zeros((h_len, w_len, 3), device=x.device, dtype=x.dtype)
        img_ids[:, :, 1] = img_ids[:, :, 1] + torch.linspace(0, h_len - 1, steps=h_len, device=x.device, dtype=x.dtype).unsqueeze(1)
        img_ids[:, :, 2] = img_ids[:, :, 2] + torch.linspace(0, w_len - 1, steps=w_len, device=x.device, dtype=x.dtype).unsqueeze(0)
        img_ids = repeat(img_ids, "h w c -> b (h w) c", b=bs)

        txt_ids = torch.zeros((bs, context.shape[1], 3), device=x.device, dtype=x.dtype)
        out = self.forward_orig(img, img_ids, context, txt_ids, timestep, guidance, control, transformer_options, attn_mask=kwargs.get("attention_mask", None))
        return rearrange(out, "b (h w) (c ph pw) -> b c (h ph) (w pw)", h=h_len, w=w_len, ph=self.patch_size, pw=self.patch_size)[:,:,:h,:w]

View on GitHub (pinned to 1c6d8d45b3)

Solutions

  1. Unsqueeze the context to batch format: context = context.unsqueeze(0).expand(bs, -1, -1) before calling forward
  2. Pass a latent tensor of shape [B, C, H, W] as x and let _forward do the patchify itself
  3. Use the standard ComfyUI sampling entry points (samplers in comfy/samplers.py) instead of calling _forward directly

Example fix

# before
out = model._forward(x, t, context=text_emb)  # text_emb: [S, D] -> raises

# after
if context.ndim == 2:
    context = context.unsqueeze(0)
out = model._forward(x, t, context=context)
Defensive patterns

Strategy: validation

Validate before calling

def ensure_batched_context(context, bs):
    if context.ndim == 2:
        context = context.unsqueeze(0)
    if context.shape[0] == 1 and bs > 1:
        context = context.expand(bs, -1, -1)
    assert context.ndim == 3
    return context

Type guard

def is_3d_batched(t) -> bool:
    return isinstance(t, torch.Tensor) and t.ndim == 3

Prevention

When it happens

Trigger: Calling Chroma._forward with context that is 2-D (a single unbatched text embedding [seq, dim]) or 4-D (e.g. an image-shaped tensor passed as conditioning), or with a non-square/padded latent whose rearrange produces something unexpected. Direct calls from custom code hit this; the normal ComfyUI sampling path always passes batched [B, S, D] context.

Common situations: Custom nodes calling the diffusion model forward directly with unbatched CLIP/Qwen embeddings; feeding a per-prompt context list instead of a stacked tensor; patches that skip the standard conditioning pipeline.

Related errors


AI-assisted analysis of Comfy-Org/ComfyUI@1c6d8d45b3 (2026-08-14). Data as JSON: /api/errors/0988b9dfbe49a896. Report an issue: GitHub.