{"record":{"id":"def8ad6bf0ed79f2","repo":"sgl-project/sglang","slug":"module-instance-is-not-unique","errorCode":null,"errorMessage":"Module instance {} is not unique ","messagePattern":"Module instance (.+?) is not unique ","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/srt/utils/nvtx_pytorch_hooks.py","lineNumber":291,"sourceCode":"        skip_types = (\n            torch.nn.Identity,\n            torch.nn.Dropout,\n            torch.nn.Dropout1d,\n            torch.nn.Dropout2d,\n            torch.nn.Dropout3d,\n        )\n\n        for name, module in network_model.named_modules(prefix=module_prefix):\n            # Skip certain module types to reduce profiling overhead\n            if isinstance(module, skip_types):\n                continue\n\n            module.register_forward_pre_hook(self.module_fwd_pre_hook)\n            module.register_forward_hook(self.module_fwd_hook)\n            if module not in self.module_to_name_map:\n                self.module_to_name_map[module] = name\n            else:\n                raise ValueError(\"Module instance {} is not unique \".format(module))\n        return\n","sourceCodeStart":273,"sourceCodeEnd":293,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/srt/utils/nvtx_pytorch_hooks.py#L273-L293","documentation":"register_hooks found the same module instance already present in module_to_name_map, i.e. the same nn.Module object was encountered twice (typically because it is shared/reused and named_modules() yields it again under another name, or hooks were registered twice).","triggerScenarios":"Calling register_hooks on a model with weight-tied/shared modules where named_modules() revisits the same instance, or calling register_hooks twice without unregistering.","commonSituations":"NVTX profiling instrumentation on models with tied embeddings (lm_head shared with embed_tokens), or re-registering hooks across profiling sessions.","solutions":["Deduplicate by id(module) before registering (skip already-seen instances)","Remove existing hooks before re-registering (track and remove_forward_hook handles)","Use named_modules(remove_duplicate=True) (the default) and avoid memo overrides"],"exampleFix":"# before\nfor name, module in model.named_modules():\n    self._register_one(name, module)\n# after\nseen = set()\nfor name, module in model.named_modules():\n    if id(module) in seen:\n        continue\n    seen.add(id(module))\n    self._register_one(name, module)","handlingStrategy":"validation","validationCode":"seen = set()\nunique_modules = [(n, m) for n, m in model.named_modules() if not (id(m) in seen or seen.add(id(m)))]","typeGuard":"def is_unique_module(module, registered: set) -> bool:\n    return id(module) not in registered","tryCatchPattern":"try:\n    hooker.register_hooks(model)\nexcept ValueError as e:\n    if 'not unique' in str(e):\n        logger.warning('shared modules present; skip NVTX hooks')","preventionTips":["Deduplicate modules by id() before registering hooks","Track and remove old hook handles before re-registration","Test instrumentation on models with tied weights"],"tags":["nvtx","pytorch-hooks","shared-weights","profiling","sglang"],"backgroundTag":"duplicate-module-registration","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}