huggingface/transformers · error · ValueError

Expected pattern {key} in collected tensors but only found t

Error message

Expected pattern {key} in collected tensors but only found tensors for: {valid_keys}

What it means

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.

Source

Thrown at src/transformers/core_model_loading.py:623

    def split_list_into_chunks(self, tensor_list: list[torch.Tensor], chunks: int = 2):
        split_size = math.ceil(len(tensor_list) / chunks)  # best effort split size
        return [tensor_list[i * split_size : (i + 1) * split_size] for i in range(chunks)]

    @torch.no_grad()
    def convert(
        self,
        input_dict: dict[str, list[torch.Tensor]],
        source_patterns: list[str],
        target_patterns: list[str],
        config,
        **kwargs,
    ) -> dict[str, list[torch.Tensor]]:
        valid_keys = input_dict.keys()
        split_and_fused = defaultdict(list)
        for key in source_patterns:
            if key not in valid_keys:
                raise ValueError(
                    f"Expected pattern {key} in collected tensors but only found tensors for: {valid_keys}"
                )

            tensors = input_dict.get(key, [])
            split_tensor_lists = self.split_list_into_chunks(tensors, chunks=len(target_patterns))
            stacked_tensors = (torch.stack(tensor_group, dim=self.stack_dim) for tensor_group in split_tensor_lists)
            for idx, tensor_group in enumerate(stacked_tensors):
                split_and_fused[target_patterns[idx]].append(tensor_group)

        for k, v in split_and_fused.items():
            split_and_fused[k] = torch.cat(v, dim=self.concat_dim)

        return split_and_fused

    @property
    def reverse_op(self) -> ConversionOps:
        return ErnieSplitAndDecoupleTextVisionExperts(stack_dim=self.stack_dim, concat_dim=self.concat_dim)

View on GitHub (pinned to a597f97485)

Solutions

  1. Read the error message: it prints the valid_keys actually collected — diff those against your source_patterns.
  2. Fix each source pattern so it matches the real checkpoint key names (list checkpoint keys with the hub API or torch.load to compare).
  3. If the checkpoint legitimately lacks one group (e.g. no vision experts), split the converter into separate converters that only reference existing keys.

Example fix

# before
WeightConverter(source_patterns=[r"text_mlp.experts.*", r"vision_mlp.experts.*"], target_patterns=[r"mlp.experts.*"], operations=[ErnieFuseAndSplitTextVisionExperts(...)])

# after (checkpoint uses feed_forward / tower names)
WeightConverter(source_patterns=[r"language_model.feed_forward.experts.*", r"vision_tower.mlp.experts.*"], target_patterns=[r"mlp.experts.*"], operations=[ErnieFuseAndSplitTextVisionExperts(...)])
Defensive patterns

Strategy: validation

Validate before calling

def all_sources_collected(source_patterns, state_dict_keys):
    import re
    missing = [p for p in source_patterns if not any(re.search(p, k) for k in state_dict_keys)]
    return missing

missing = all_sources_collected(converter.source_patterns, list(state_dict))
assert not missing, f'patterns matching nothing: {missing} — fix regexes before converting'

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of huggingface/transformers@a597f97485 (2026-08-14). Data as JSON: /api/errors/79e21afe0ea85cfa. Report an issue: GitHub.