sgl-project/sglang · error · ValueError
Module instance {} is not unique
Error message
Module instance {} is not unique What it means
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).
Source
Thrown at python/sglang/srt/utils/nvtx_pytorch_hooks.py:291
skip_types = (
torch.nn.Identity,
torch.nn.Dropout,
torch.nn.Dropout1d,
torch.nn.Dropout2d,
torch.nn.Dropout3d,
)
for name, module in network_model.named_modules(prefix=module_prefix):
# Skip certain module types to reduce profiling overhead
if isinstance(module, skip_types):
continue
module.register_forward_pre_hook(self.module_fwd_pre_hook)
module.register_forward_hook(self.module_fwd_hook)
if module not in self.module_to_name_map:
self.module_to_name_map[module] = name
else:
raise ValueError("Module instance {} is not unique ".format(module))
return
View on GitHub (pinned to 0132848349)
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
Example fix
# before
for name, module in model.named_modules():
self._register_one(name, module)
# after
seen = set()
for name, module in model.named_modules():
if id(module) in seen:
continue
seen.add(id(module))
self._register_one(name, module) Defensive patterns
Strategy: validation
Validate before calling
seen = set() unique_modules = [(n, m) for n, m in model.named_modules() if not (id(m) in seen or seen.add(id(m)))]
Type guard
def is_unique_module(module, registered: set) -> bool:
return id(module) not in registered Try / catch
try:
hooker.register_hooks(model)
except ValueError as e:
if 'not unique' in str(e):
logger.warning('shared modules present; skip NVTX hooks') Prevention
- Deduplicate modules by id() before registering hooks
- Track and remove old hook handles before re-registration
- Test instrumentation on models with tied weights
When it happens
Trigger: 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.
Common situations: NVTX profiling instrumentation on models with tied embeddings (lm_head shared with embed_tokens), or re-registering hooks across profiling sessions.
Related errors
- No trace files found for profile_id: {self.profile_id}
- manually start is only supported yet
- manually stop is only supported yet
- unsupported profile stage: {forward_mode=}
- Trace file is empty.
AI-assisted analysis of sgl-project/sglang@0132848349 (2026-08-28).
Data as JSON: /api/errors/def8ad6bf0ed79f2.
Report an issue: GitHub.