{"record":{"id":"5a368c9291668ba7","repo":"huggingface/transformers","slug":"failed-to-convert-kwargs-get-full-layer-name","errorCode":null,"errorMessage":"Failed to convert {kwargs.get('full_layer_name')}","messagePattern":"Failed to convert (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/transformers/core_model_loading.py","lineNumber":130,"sourceCode":"\n\nclass Chunk(ConversionOps):\n    \"\"\"Split a tensor along `dim` into equally sized chunks.\"\"\"\n\n    def __init__(self, dim: int = 0):\n        self.dim = dim\n\n    @torch.no_grad\n    def convert(\n        self, input_dict: dict[str, torch.Tensor], source_patterns: list[str], target_patterns: list[str], **kwargs\n    ) -> dict[str, torch.Tensor]:\n        tensors = next(iter(input_dict.values()))\n        tensor = tensors[0] if isinstance(tensors, list) else tensors\n        targets = target_patterns\n        sizes = len(targets)\n        chunks = tuple(chunk.contiguous() for chunk in torch.chunk(tensor, sizes, dim=self.dim))\n        if len(input_dict) > 1 or len(target_patterns) == 1 or len(chunks) != len(target_patterns):\n            raise ValueError(f\"Failed to convert {kwargs.get('full_layer_name')}\")\n        return dict(zip(targets, chunks))\n\n    @property\n    def reverse_op(self) -> ConversionOps:\n        return Concatenate(self.dim)\n\n\nclass Concatenate(ConversionOps):\n    \"\"\"Concatenate tensors along `dim`.\"\"\"\n\n    def __init__(self, dim: int = 0):\n        self.dim = dim\n\n    @torch.no_grad\n    def convert(\n        self,\n        input_dict: dict[str, list[torch.Tensor]],\n        source_patterns: list[str],","sourceCodeStart":112,"sourceCodeEnd":148,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/core_model_loading.py#L112-L148","documentation":"Raised by the Chunk conversion op during checkpoint weight conversion (core_model_loading.py:130). Chunk splits one collected tensor along `dim` into exactly len(target_patterns) pieces; the error fires when more than one source pattern was collected, when there is only one target pattern (nothing to split), or when torch.chunk did not produce one chunk per target (the tensor's size along `dim` is not divisible into that many chunks). The layer name is included via kwargs['full_layer_name'] so you can locate the offending weight in the conversion recipe.","triggerScenarios":"Registering a Chunk(dim=...) op in a WeightConverter where: (a) source_patterns matches multiple checkpoint keys, (b) target_patterns has length 1, or (c) the fused checkpoint tensor's size along `dim` is smaller than / not evenly splittable into len(target_patterns) chunks (e.g. splitting a 2-tensor fused QKV into 3 targets, or N targets where N does not divide the dimension).","commonSituations":"Writing a custom conversion recipe for a new checkpoint variant (e.g. fused QKV or fused gate/up projections being split into HF-style separate weights), or when the upstream checkpoint changes how it fuses layers between releases so the split arity no longer matches.","solutions":["Print the collected keys and the tensor shape for the failing layer (wrap convert or inspect the checkpoint state dict) and compare tensor.shape[dim] against len(target_patterns).","Fix target_patterns so its length equals the number of chunks actually present (e.g. 3 for q/k/v, 2 for gate/up) and ensure the source pattern matches exactly ONE checkpoint key.","If the tensor's dim is not divisible by the number of targets, switch to an op that splits by explicit sizes or fix the dim argument (e.g. Chunk(dim=0) vs Chunk(dim=1)).","If you genuinely have multiple source tensors to fuse first, chain Concatenate before Chunk or use a many-to-many capable internal op."],"exampleFix":"# before: fused tensor has shape [2*hidden, ...] but 3 targets declared\nWeightConverter(source_patterns=[r\"layers\\..*\\.attn.fused\"], target_patterns=[r\"q_proj\", r\"k_proj\", r\"v_proj\"], operations=[Chunk(dim=0)])\n\n# after: fused tensor only holds q,k (2 chunks) -> match target count to the real arity\nWeightConverter(source_patterns=[r\"layers\\..*\\.attn\\.fused\"], target_patterns=[r\"q_proj\", r\"k_proj\"], operations=[Chunk(dim=0)])","handlingStrategy":"validation","validationCode":"def check_chunk convertible(transform, state_dict):\n    src = transform.source_patterns\n    keys = [k for k in state_dict if any(re.fullmatch(p.replace('.*', '.*'), k) for p in src)]\n    assert len(keys) == 1, f'Chunk expects exactly 1 source key, matched: {keys}'\n    tensor = state_dict[keys[0]]\n    n = len(transform.target_patterns)\n    assert n > 1, 'Chunk requires >1 target pattern'\n    assert tensor.size(0) % n == 0 or tensor.size(0) >= n, (\n        f'tensor dim {tensor.shape} cannot be chunked into {n} parts'\n    )","typeGuard":"def is_valid_chunk_config(source_keys: list[str], tensor: \"torch.Tensor\", n_targets: int) -> bool:\n    return len(source_keys) == 1 and n_targets > 1 and tensor.size(0) >= n_targets and tensor.size(0) % n_targets == 0","tryCatchPattern":null,"preventionTips":["Before registering a Chunk op, assert the source regex matches exactly one checkpoint key and print its shape.","Keep target_patterns length equal to the true fan-out (q/k/v=3, gate/up=2) of the fused tensor.","Add a unit test that runs the conversion recipe against a tiny synthetic checkpoint of the documented shapes."],"tags":["weight-conversion","model-loading","chunk","checkpoint"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}