huggingface/transformers · error · ValueError
source keys={self.source_patterns}, target_patterns={self.ta
Error message
source keys={self.source_patterns}, target_patterns={self.target_patterns} but you can only have one to many, one to one or many to one. What it means
Raised by WeightConverter.__init__ (core_model_loading.py:1150). Conversions support 1:1, 1:N and N:1 pattern cardinalities; N:N (many sources to many targets) is only legal when at least one operation in the chain is a specially supported internal op (currently the Ernie fuse/split text-vision expert ops, see _INTERNAL_MANY_TO_MANY_CONVERSIONS at core_model_loading.py:1132). Any other N:N combination has no defined tensor-routing semantics and is rejected at construction.
Source
Thrown at src/transformers/core_model_loading.py:1150
_INTERNAL_MANY_TO_MANY_CONVERSIONS = (
ErnieFuseAndSplitTextVisionExperts,
ErnieSplitAndDecoupleTextVisionExperts,
)
class WeightConverter(WeightTransform):
__slots__ = ("operations",)
def __init__(
self, source_patterns: str | list[str], target_patterns: str | list[str], operations: list[ConversionOps]
):
super().__init__(source_patterns, target_patterns)
self.operations: list[ConversionOps] = operations
if bool(len(self.source_patterns) - 1) + bool(len(self.target_patterns) - 1) >= 2:
# We allow many-to-many only if we use an internal operation that can handle it
if not any(isinstance(op, _INTERNAL_MANY_TO_MANY_CONVERSIONS) for op in self.operations):
raise ValueError(
f"source keys={self.source_patterns}, target_patterns={self.target_patterns} but you can only have one to many, one to one or many to one."
)
if not self.operations:
raise ValueError("WeightConverter requires at least one operation.")
def convert(
self,
layer_name: str,
model=None,
config=None,
hf_quantizer=None,
loading_info: LoadStateDictInfo | None = None,
):
# Collect the tensors here - we use a new dictionary to avoid keeping them in memory in the internal
# attribute during the whole process
collected_tensors = self.materialize_tensors()
for op in self.operations:View on GitHub (pinned to a597f97485)
Solutions
- Split the N:N converter into separate 1:1 / 1:N / N:1 WeightConverter instances chained in order.
- If the conversion is genuinely many-to-many tensor routing (like Ernie expert fusion), implement/reuse an op registered in _INTERNAL_MANY_TO_MANY_CONVERSIONS.
- Re-express the mapping: use N:1 (many sources fused to one target via Concatenate-like ops) followed by 1:N (Chunk) converters in sequence.
Example fix
# before WeightConverter(source_patterns=[r'a.*', r'b.*'], target_patterns=[r'x.*', r'y.*'], operations=[MyOp()]) # raises # after: two chained converters WeightConverter(source_patterns=[r'a.*', r'b.*'], target_patterns=[r'fused'], operations=[Concatenate(dim=0)]) WeightConverter(source_patterns=[r'fused'], target_patterns=[r'x.*', r'y.*'], operations=[Chunk(dim=0)])
Defensive patterns
Strategy: validation
Validate before calling
n_src, n_tgt = len(source_patterns), len(target_patterns)
many_to_many = n_src > 1 and n_tgt > 1
internal = {type(op).__name__ for op in operations} & {'ErnieFuseAndSplitTextVisionExperts', 'ErnieSplitAndDecoupleTextVisionExperts'}
assert not (many_to_many and not internal), 'N:N requires an internal many-to-many op; split into chained converters' Prevention
- Design recipes as chains of 1:1 / 1:N / N:1 converters; never N:N with generic ops.
- Reserve N:N for the whitelisted internal ops (Ernie expert fuse/split).
- When review shows two multi-pattern lists meeting in one WeightConverter, refactor into two stages (N:1 fuse, then 1:N split).
When it happens
Trigger: WeightConverter(source_patterns=[p1, p2], target_patterns=[t1, t2], operations=[SomeOp()]) where SomeOp is not ErnieFuseAndSplitTextVisionExperts / ErnieSplitAndDecoupleTextVisionExperts (e.g. Chunk, Permute, or a custom ConversionOps subclass).
Common situations: Recipe authors trying to merge multiple checkpoint keys into multiple target keys in one converter (e.g. combining gate+up into two different fused targets) with generic ops. The framework cannot know how to pair sources with targets for arbitrary ops.
Related errors
- GroupWeightRename requires N:N length matching, but found le
- You must provide only one of `prefix_to_add` and `prefix_to_
- WeightConverter requires at least one operation.
- Multiple different capturing groups found in target_patterns
- Source pattern '{pattern}' contains \\1 backreference, but n
AI-assisted analysis of huggingface/transformers@a597f97485 (2026-08-14).
Data as JSON: /api/errors/a0ac21360dd75d51.
Report an issue: GitHub.