{"record":{"id":"79e21afe0ea85cfa","repo":"huggingface/transformers","slug":"expected-pattern-key-in-collected-tensors-but-on","errorCode":null,"errorMessage":"Expected pattern {key} in collected tensors but only found tensors for: {valid_keys}","messagePattern":"Expected pattern (.+?) in collected tensors but only found tensors for: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/transformers/core_model_loading.py","lineNumber":623,"sourceCode":"\n    def split_list_into_chunks(self, tensor_list: list[torch.Tensor], chunks: int = 2):\n        split_size = math.ceil(len(tensor_list) / chunks)  # best effort split size\n        return [tensor_list[i * split_size : (i + 1) * split_size] for i in range(chunks)]\n\n    @torch.no_grad()\n    def convert(\n        self,\n        input_dict: dict[str, list[torch.Tensor]],\n        source_patterns: list[str],\n        target_patterns: list[str],\n        config,\n        **kwargs,\n    ) -> dict[str, list[torch.Tensor]]:\n        valid_keys = input_dict.keys()\n        split_and_fused = defaultdict(list)\n        for key in source_patterns:\n            if key not in valid_keys:\n                raise ValueError(\n                    f\"Expected pattern {key} in collected tensors but only found tensors for: {valid_keys}\"\n                )\n\n            tensors = input_dict.get(key, [])\n            split_tensor_lists = self.split_list_into_chunks(tensors, chunks=len(target_patterns))\n            stacked_tensors = (torch.stack(tensor_group, dim=self.stack_dim) for tensor_group in split_tensor_lists)\n            for idx, tensor_group in enumerate(stacked_tensors):\n                split_and_fused[target_patterns[idx]].append(tensor_group)\n\n        for k, v in split_and_fused.items():\n            split_and_fused[k] = torch.cat(v, dim=self.concat_dim)\n\n        return split_and_fused\n\n    @property\n    def reverse_op(self) -> ConversionOps:\n        return ErnieSplitAndDecoupleTextVisionExperts(stack_dim=self.stack_dim, concat_dim=self.concat_dim)\n","sourceCodeStart":605,"sourceCodeEnd":641,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/core_model_loading.py#L605-L641","documentation":"Raised by ErnieFuseAndSplitTextVisionExperts.convert (core_model_loading.py:623). This many-to-many op expects every string in source_patterns to be present as a key in the collected input_dict (the tensors gathered by matching checkpoint keys). If a source pattern matched nothing — because the regex does not match the actual checkpoint key names, or a prior op consumed them — the fusing cannot proceed and the error lists the keys that WERE collected for comparison.","triggerScenarios":"Building a WeightConverter with ErnieFuseAndSplitTextVisionExperts whose source_patterns contain a pattern that matches zero keys of the current checkpoint (typo, wrong layer prefix, wrong index format like layers.0 vs blocks.0), or running the recipe against a checkpoint variant that lacks one of the expert weight groups.","commonSituations":"Adapting an Ernie/MoE-style conversion recipe to a new checkpoint release where key names changed (e.g. 'mlp.experts' renamed to 'feed_forward.experts'), or applying the recipe to a text-only / vision-only checkpoint missing one side's expert weights.","solutions":["Read the error message: it prints the valid_keys actually collected — diff those against your source_patterns.","Fix each source pattern so it matches the real checkpoint key names (list checkpoint keys with the hub API or torch.load to compare).","If the checkpoint legitimately lacks one group (e.g. no vision experts), split the converter into separate converters that only reference existing keys."],"exampleFix":"# before\nWeightConverter(source_patterns=[r\"text_mlp.experts.*\", r\"vision_mlp.experts.*\"], target_patterns=[r\"mlp.experts.*\"], operations=[ErnieFuseAndSplitTextVisionExperts(...)])\n\n# after (checkpoint uses feed_forward / tower names)\nWeightConverter(source_patterns=[r\"language_model.feed_forward.experts.*\", r\"vision_tower.mlp.experts.*\"], target_patterns=[r\"mlp.experts.*\"], operations=[ErnieFuseAndSplitTextVisionExperts(...)])","handlingStrategy":"validation","validationCode":"def all_sources_collected(source_patterns, state_dict_keys):\n    import re\n    missing = [p for p in source_patterns if not any(re.search(p, k) for k in state_dict_keys)]\n    return missing\n\nmissing = all_sources_collected(converter.source_patterns, list(state_dict))\nassert not missing, f'patterns matching nothing: {missing} — fix regexes before converting'","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Dry-run every source pattern against the checkpoint's key list before starting conversion.","Print the error's valid_keys list and diff it against source_patterns when it fires.","Pin checkpoint revisions in recipes so key renames upstream cannot silently break matching."],"tags":["weight-conversion","moe","pattern-mismatch","model-loading"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}