Comfy-Org/ComfyUI · error · ValueError

Unexpected token width: {out_x.shape[-1]}

Error message

Unexpected token width: {out_x.shape[-1]}

What it means

Raised in get_intermediate_layers_da3 while normalizing per-layer outputs. DA3 heads concatenate x2-width tokens (embed_dim*2) from some layers; the loop accepts widths of exactly embed_dim (full layernorm) or embed_dim*2 (norm right half, keep left half raw). Any other last-dimension width means the layer indices or the checkpoint architecture do not match the model's expected token layout.

Source

Thrown at comfy/image_encoders/dino2.py:488

                if b_idx is not None and self.alt_start != -1:
                    aux = restore_original_order(aux, b_idx)
                aux_outputs.append(aux)

        # Apply final norm. When cat_token is set, only the right half
        # ("global" features) is normalised; the left half is left as-is to
        # match the upstream DA3 head signature.
        normed: list[torch.Tensor] = []
        cls_tokens: list[torch.Tensor] = []
        for out_x in outputs:
            cls_tokens.append(out_x[:, :, 0])
            if out_x.shape[-1] == self.embed_dim:
                normed.append(self.layernorm(out_x))
            elif out_x.shape[-1] == self.embed_dim * 2:
                left = out_x[..., :self.embed_dim]
                right = self.layernorm(out_x[..., self.embed_dim:])
                normed.append(torch.cat([left, right], dim=-1))
            else:
                raise ValueError(f"Unexpected token width: {out_x.shape[-1]}")

        # Drop cls/cam token from the patch sequence.
        normed = [o[..., 1 + self.num_register_tokens:, :] for o in normed]

        # Final layernorm + drop cls token from auxiliary features too.
        aux_normed = [self.layernorm(o)[..., 1 + self.num_register_tokens:, :]
                      for o in aux_outputs]
        return list(zip(normed, cls_tokens)), aux_normed

View on GitHub (pinned to 1c6d8d45b3)

Solutions

  1. Verify the checkpoint is a genuine Depth Anything 3 model and load it through the stock DA3 loading code so embed_dim and layer config are set correctly
  2. Use the layer indices the DA3 head was trained with (the defaults) rather than arbitrary intermediate indices
  3. If you control the checkpoint, ensure concat-head layers output exactly 2*embed_dim
Defensive patterns

Strategy: validation

Validate before calling

w = out_x.shape[-1]
assert w in (model.embed_dim, model.embed_dim * 2), f'unexpected token width {w}'

Type guard

def token_width_valid(model, tensor: torch.Tensor) -> bool:
    return tensor.shape[-1] in (model.embed_dim, model.embed_dim * 2)

Prevention

When it happens

Trigger: Requesting intermediate layer indices that return tensors whose feature width is neither embed_dim nor 2*embed_dim — typically from a checkpoint with a different head widening scheme, a mis-detected embed_dim at load, or manual slicing that leaves partial concatenation.

Common situations: Loading a non-DA3 or experimental DINOv2 variant through the DA3 path; custom checkpoints with modified concat heads; passing indices that hit auxiliary projection outputs with custom widths.

Understand the failure class

Related errors


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