huggingface/transformers · error · ValueError

Source pattern '{pattern}' contains \\1 backreference, but n

Error message

Source pattern '{pattern}' contains \\1 backreference, but no capturing groups found in target_patterns.

What it means

Raised in WeightTransform.__init__ (core_model_loading.py:820). When a source pattern contains the backreference \1 (meaning 'insert whatever the target's capturing group matched'), at least one target pattern must define a capturing group to supply that content. If no target pattern has a capturing group, there is nothing to substitute into \1 and the transform is ill-defined, so init fails immediately.

Source

Thrown at src/transformers/core_model_loading.py:820

            if captured_group is not None:
                target_capturing_groups.append(captured_group)

        # Validate that we only have one unique capturing group pattern across all targets
        # This ensures deterministic reverse mapping when sources have \1 backreferences
        unique_capturing_groups = set(target_capturing_groups)
        if len(unique_capturing_groups) > 1:
            raise ValueError(
                f"Multiple different capturing groups found in target_patterns: {unique_capturing_groups}. "
                f"All target patterns must use the same capturing group pattern."
            )
        unique_capturing_group = unique_capturing_groups.pop() if unique_capturing_groups else None

        # We also need to check capturing groups in the sources during reverse mapping (e.g. timm_wrapper, sam3)
        for i, pattern in enumerate(self.source_patterns):
            # Replace capturing groups
            if r"\1" in pattern:
                if unique_capturing_group is None:
                    raise ValueError(
                        f"Source pattern '{pattern}' contains \\1 backreference, but no capturing groups "
                        f"found in target_patterns."
                    )
                # Use the unique capturing group from target_patterns for all sources
                pattern = pattern.replace(r"\1", unique_capturing_group, 1)
            # Potentially process a bit more for consistency - only if they are consistent pairs, i.e. the length is the same
            if len(self.source_patterns) == len(self.target_patterns):
                pattern = process_source_pattern(pattern, self._original_target_patterns[i])
            self.source_patterns[i] = pattern

        # Construct the regex we will use to rename keys from the sources to the targets
        branches = []
        for i, source_pattern in enumerate(self.source_patterns):
            group_name = f"g{i}"
            pattern = source_pattern.replace(".*.", r"\..*\.")
            branches.append(f"(?P<{group_name}>{pattern})")
        self.compiled_sources = re.compile("|".join(branches))

View on GitHub (pinned to a597f97485)

Solutions

  1. Add the capturing group to the target pattern(s), e.g. target r'layers.(\d+).attn' so \1 in sources resolves to the matched digits.
  2. Ensure ALL targets use that same group (see the sibling check for multiple distinct groups).
  3. If you do not need reverse mapping, remove \1 from the source patterns and write the source pattern explicitly.

Example fix

# before
WeightTransform(source_patterns=[r'blk.\1.attn'], target_patterns=[r'layers.attn'])

# after
WeightTransform(source_patterns=[r'blk.\1.attn'], target_patterns=[r'layers.(\d+).attn'])
Defensive patterns

Strategy: validation

Validate before calling

has_target_group = any(re.search(r'\((?!\?:)', p) for p in target_patterns)
uses_backref = any(r'\1' in p for p in source_patterns)
assert not (uses_backref and not has_target_group), 'source \\1 backreference needs a capturing group in target_patterns'

Prevention

When it happens

Trigger: WeightTransform(source_patterns=[r'blocks.\1.attn'], target_patterns=[r'layers.attn']) — the source uses \1 but no target contains a (...) group. Common when the author forgets the parentheses in the target pattern.

Common situations: Authoring bidirectional (round-trippable) conversion recipes: \1 in sources is what makes the reverse mapping reconstruct original names. Omitting the group in targets (or using a non-capturing (?:...) group) breaks the reverse direction and is rejected up front.

Related errors


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