{"record":{"id":"1638a20ec2e31d3d","repo":"Comfy-Org/ComfyUI","slug":"unexpected-token-width-out-x-shape-1","errorCode":null,"errorMessage":"Unexpected token width: {out_x.shape[-1]}","messagePattern":"Unexpected token width: (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"comfy/image_encoders/dino2.py","lineNumber":488,"sourceCode":"                if b_idx is not None and self.alt_start != -1:\n                    aux = restore_original_order(aux, b_idx)\n                aux_outputs.append(aux)\n\n        # Apply final norm. When cat_token is set, only the right half\n        # (\"global\" features) is normalised; the left half is left as-is to\n        # match the upstream DA3 head signature.\n        normed: list[torch.Tensor] = []\n        cls_tokens: list[torch.Tensor] = []\n        for out_x in outputs:\n            cls_tokens.append(out_x[:, :, 0])\n            if out_x.shape[-1] == self.embed_dim:\n                normed.append(self.layernorm(out_x))\n            elif out_x.shape[-1] == self.embed_dim * 2:\n                left = out_x[..., :self.embed_dim]\n                right = self.layernorm(out_x[..., self.embed_dim:])\n                normed.append(torch.cat([left, right], dim=-1))\n            else:\n                raise ValueError(f\"Unexpected token width: {out_x.shape[-1]}\")\n\n        # Drop cls/cam token from the patch sequence.\n        normed = [o[..., 1 + self.num_register_tokens:, :] for o in normed]\n\n        # Final layernorm + drop cls token from auxiliary features too.\n        aux_normed = [self.layernorm(o)[..., 1 + self.num_register_tokens:, :]\n                      for o in aux_outputs]\n        return list(zip(normed, cls_tokens)), aux_normed\n","sourceCodeStart":470,"sourceCodeEnd":497,"githubUrl":"https://github.com/Comfy-Org/ComfyUI/blob/1c6d8d45b3693bfbb32385b410d813a7fd6be216/comfy/image_encoders/dino2.py#L470-L497","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["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","Use the layer indices the DA3 head was trained with (the defaults) rather than arbitrary intermediate indices","If you control the checkpoint, ensure concat-head layers output exactly 2*embed_dim"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"w = out_x.shape[-1]\nassert w in (model.embed_dim, model.embed_dim * 2), f'unexpected token width {w}'","typeGuard":"def token_width_valid(model, tensor: torch.Tensor) -> bool:\n    return tensor.shape[-1] in (model.embed_dim, model.embed_dim * 2)","tryCatchPattern":null,"preventionTips":["Load DA3 checkpoints only through the stock loader so embed_dim matches the head","Prefer default DA3 layer indices over ad-hoc ones"],"tags":["vision-encoder","dino2","depth-anything","shape-validation","checkpoint"],"backgroundTag":null,"analyzedSha":"1c6d8d45b3693bfbb32385b410d813a7fd6be216","analyzedAt":"2026-08-14T19:37:18.893Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}