Skip to content

fix: use sys.modules.get() to avoid KeyError in modeling_utils#45089

Open
Krishnachaitanyakc wants to merge 1 commit intohuggingface:mainfrom
Krishnachaitanyakc:fix/sys-modules-safe-access
Open

fix: use sys.modules.get() to avoid KeyError in modeling_utils#45089
Krishnachaitanyakc wants to merge 1 commit intohuggingface:mainfrom
Krishnachaitanyakc:fix/sys-modules-safe-access

Conversation

@Krishnachaitanyakc
Copy link
Copy Markdown
Contributor

Summary

Fixes #45003

_can_set_attn_implementation and _can_set_experts_implementation in PreTrainedModel use sys.modules[cls.__module__], which raises KeyError when a module has been removed from sys.modules at runtime. This can happen with:

  • vLLM CI (modules deleted during cleanup)
  • Griptape Nodes and similar tools that manipulate sys.modules for dependency management
  • Any lazy-loading or packaging tool that removes modules from the registry

Changes

  • Replace sys.modules[cls.__module__] with sys.modules.get(cls.__module__) (2 locations)
  • Add class_module is None guard before the existing hasattr(class_module, "__file__") check
  • When the module is missing, returns False — the same safe fallback already used for modules without __file__ (e.g., Jupyter notebooks)

Why not a broader fix?

This is intentionally minimal (4 lines changed). The existing comment already acknowledges that module lookup can fail ("This can happen for a custom model in a jupyter notebook or repl"), and sys.modules.get() is the standard safe-access pattern.

Test plan

  • Verified the fix matches the approach discussed by maintainers in modeling_utils unsafely accesses sys.modules[] #45003
  • Confirmed no open PRs exist for this issue
  • sys.modules.get() returns None (not KeyError) when module is absent — None check short-circuits to return False
  • Normal-case behavior is unchanged (module exists → same flow as before)

AI assistance was used in preparing this PR.

Replace unsafe `sys.modules[cls.__module__]` bracket access with
`sys.modules.get(cls.__module__)` in `_can_set_attn_implementation`
and `_can_set_experts_implementation`. When a module has been deleted
from sys.modules (e.g. during testing or dynamic reloading), bracket
access raises KeyError. Using .get() returns None, which is then
handled by the existing guard that already checks for missing __file__.

Fixes huggingface#45003
@Krishnachaitanyakc Krishnachaitanyakc marked this pull request as ready for review March 28, 2026 17:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

modeling_utils unsafely accesses sys.modules[]

1 participant