Skip to content

fix: guard sys.modules access in _can_set_attn/experts_implementation#45015

Closed
pnehete23 wants to merge 1 commit intohuggingface:mainfrom
pnehete23:fix/sys-modules-keyerror-45003
Closed

fix: guard sys.modules access in _can_set_attn/experts_implementation#45015
pnehete23 wants to merge 1 commit intohuggingface:mainfrom
pnehete23:fix/sys-modules-keyerror-45003

Conversation

@pnehete23
Copy link
Copy Markdown

What does this PR do?

Fixes a KeyError in _can_set_attn_implementation and _can_set_experts_implementation when a model's module is absent from sys.modules.

Fixes #45003

Root Cause

Both _can_set_attn_implementation (line 1951) and _can_set_experts_implementation (line 1970) use a direct dictionary subscript:

class_module = sys.modules[cls.__module__]

This raises KeyError when the module is not registered in sys.modules — which happens with lazy loaders, frozen importers, or tools like Griptape Nodes that manipulate the module registry.

The existing comment on the very next line already acknowledges this class of scenario ("This can happen for a custom model in a jupyter notebook or repl"), but the __file__ guard only runs after the dictionary access, so it never fires.

Fix

Replace sys.modules[cls.__module__] with sys.modules.get(cls.__module__) and add a class_module is None check alongside the existing hasattr guard. Both functions now gracefully return False instead of crashing.

Tests

Added TestCanSetImplementationWithMissingSysModule with 3 tests:

  • _can_set_attn_implementation returns False when module is missing from sys.modules
  • _can_set_experts_implementation returns False when module is missing from sys.modules
  • Full model initialization succeeds when module is missing from sys.modules

All existing TestAttentionImplementation tests continue to pass.

Code Agent Policy

  • I confirm that this is not a pure code agent PR.

Use sys.modules.get() instead of sys.modules[] to prevent KeyError when a module is absent from sys.modules, which can happen with lazy loaders, frozen importers, or packaging tools that manipulate the module registry.

The existing comment already acknowledged this class of scenario but the guard only ran after the dictionary access, so it never had a chance to fire.

Fixes huggingface#45003

Made-with: Cursor
@pnehete23 pnehete23 force-pushed the fix/sys-modules-keyerror-45003 branch from e57180f to 4a52f13 Compare March 26, 2026 09:10
@Rocketknight1
Copy link
Copy Markdown
Member

This is clearly a code agent PR. Not reviewing it, sorry!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

modeling_utils unsafely accesses sys.modules[]

2 participants