{"record":{"id":"d495feec2fcbc809","repo":"huggingface/pytorch-image-models","slug":"no-module-names-found-matching-names","errorCode":null,"errorMessage":"No module names found matching {names}.","messagePattern":"No module names found matching (.+?)\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"timm/utils/attention_extract.py","lineNumber":70,"sourceCode":"            if not matched:\n                raise RuntimeError(f'No node names found matching {names}.')\n\n            self.model = GraphExtractNet(model, matched, return_dict=True)\n            self.hooks = None\n        else:\n            # names are module names\n            assert hook_type in ('forward', 'forward_pre')\n            from timm.models._features import FeatureHooks\n\n            module_names = [n for n, m in model.named_modules()]\n            names = names or self.default_module_names\n            if use_regex:\n                regexes = [re.compile(r) for r in names]\n                matched = [m for m in module_names if any([r.match(m) for r in regexes])]\n            else:\n                matched = [m for m in module_names if any([fnmatch.fnmatch(m, n) for n in names])]\n            if not matched:\n                raise RuntimeError(f'No module names found matching {names}.')\n\n            self.model = model\n            self.hooks = FeatureHooks(matched, model.named_modules(), default_hook_type=hook_type)\n\n        self.names = matched\n        self.mode = mode\n        self.method = method\n\n    def forward(self, x):\n        if self.hooks is not None:\n            self.model(x)\n            output = self.hooks.get_output(device=x.device)\n        else:\n            output = self.model(x)\n        return output\n","sourceCodeStart":52,"sourceCodeEnd":86,"githubUrl":"https://github.com/huggingface/pytorch-image-models/blob/9a5261e31b3b5128526eb2658333b4c0a54464ae/timm/utils/attention_extract.py#L52-L86","documentation":"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.","triggerScenarios":"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).","commonSituations":"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).","solutions":["List actual module names: print([n for n, _ in model.named_modules() if 'attn' in n]) and correct patterns","Use globs like 'blocks.*.attn.qkv' or enable use_regex=True with proper patterns","Confirm the model variant actually contains the named attention modules"],"exampleFix":"# before\next = AttentionExtractor(model, names=['blocks.0.attn.qkv'])\n# after\nprint([n for n, m in model.named_modules() if 'qkv' in n])\next = AttentionExtractor(model, names=['blocks.*.attn.qkv'])","handlingStrategy":"validation","validationCode":"mods = [n for n, _ in model.named_modules()]\nassert any(fnmatch.fnmatch(m, pat) for m in mods for pat in names), 'no module matches'","typeGuard":null,"tryCatchPattern":"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","preventionTips":["Derive hook names programmatically from named_modules() rather than hardcoding","Remember default matching is fnmatch; use_regex=True changes semantics"],"tags":["attention-extraction","module-names","hooks","timm"],"backgroundTag":"name-pattern-no-match","analyzedSha":"9a5261e31b3b5128526eb2658333b4c0a54464ae","analyzedAt":"2026-08-27T02:34:25.417Z","schemaVersion":2},"datasetVersion":"2026-08-27T03:17:27.898Z"}