huggingface/pytorch-image-models · error · RuntimeError

No module names found matching {names}.

Error message

No module names found matching {names}.

What it means

AttentionExtractor in module-hook mode matches the provided names (fnmatch globs or regexes) against model.named_modules() entries. RuntimeError is raised when no module matches, meaning the model has no submodules with those names and hooks cannot be attached.

Source

Thrown at timm/utils/attention_extract.py:70

            if not matched:
                raise RuntimeError(f'No node names found matching {names}.')

            self.model = GraphExtractNet(model, matched, return_dict=True)
            self.hooks = None
        else:
            # names are module names
            assert hook_type in ('forward', 'forward_pre')
            from timm.models._features import FeatureHooks

            module_names = [n for n, m in model.named_modules()]
            names = names or self.default_module_names
            if use_regex:
                regexes = [re.compile(r) for r in names]
                matched = [m for m in module_names if any([r.match(m) for r in regexes])]
            else:
                matched = [m for m in module_names if any([fnmatch.fnmatch(m, n) for n in names])]
            if not matched:
                raise RuntimeError(f'No module names found matching {names}.')

            self.model = model
            self.hooks = FeatureHooks(matched, model.named_modules(), default_hook_type=hook_type)

        self.names = matched
        self.mode = mode
        self.method = method

    def forward(self, x):
        if self.hooks is not None:
            self.model(x)
            output = self.hooks.get_output(device=x.device)
        else:
            output = self.model(x)
        return output

View on GitHub (pinned to 9a5261e31b)

Solutions

  1. List actual module names: print([n for n, _ in model.named_modules() if 'attn' in n]) and correct patterns
  2. Use globs like 'blocks.*.attn.qkv' or enable use_regex=True with proper patterns
  3. Confirm the model variant actually contains the named attention modules

Example fix

# before
ext = AttentionExtractor(model, names=['blocks.0.attn.qkv'])
# after
print([n for n, m in model.named_modules() if 'qkv' in n])
ext = AttentionExtractor(model, names=['blocks.*.attn.qkv'])
Defensive patterns

Strategy: validation

Validate before calling

mods = [n for n, _ in model.named_modules()]
assert any(fnmatch.fnmatch(m, pat) for m in mods for pat in names), 'no module matches'

Try / catch

try:\n    ext = AttentionExtractor(model, names)\nexcept RuntimeError as e:\n    if 'No module names' in str(e):\n        mods = [n for n, _ in model.named_modules()]\n        raise ValueError(f'have: {[m for m in mods if "attn" in m]}') from e\n    raise

Prevention

When it happens

Trigger: Calling AttentionExtractor(model, names=['blocks.0.attn.qkv']) on a model whose blocks use different naming (e.g. 'blocks.0.attn.q'); a glob like 'attn.*' that doesn't match any module; wrong model instance (feature-only variant without attention modules).

Common situations: Names taken from a different architecture or timm variant; using full parameter paths (with .weight) instead of module paths; typos; expecting regex but default is fnmatch (and vice versa re.match anchors at start).

Related errors


AI-assisted analysis of huggingface/pytorch-image-models@9a5261e31b (2026-08-27). Data as JSON: /api/errors/d495feec2fcbc809. Report an issue: GitHub.