From 14959da6697886bae4f1176a66c641ec31ef9896 Mon Sep 17 00:00:00 2001 From: cjkindel Date: Tue, 24 Mar 2026 13:55:03 -0700 Subject: [PATCH] fix: handle absent sys.modules entry in _can_set_attn/experts_implementation --- src/transformers/modeling_utils.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/transformers/modeling_utils.py b/src/transformers/modeling_utils.py index 1cdb033cb709..59bee80e7a89 100644 --- a/src/transformers/modeling_utils.py +++ b/src/transformers/modeling_utils.py @@ -1948,9 +1948,9 @@ def _can_set_attn_implementation(cls) -> bool: """Detect whether the class supports setting its attention implementation dynamically. It is an ugly check based on opening the file, but avoids maintaining yet another property flag. """ - class_module = sys.modules[cls.__module__] + class_module = sys.modules.get(cls.__module__) # This can happen for a custom model in a jupyter notebook or repl for example - simply do not allow to set it then - if not hasattr(class_module, "__file__"): + if class_module is None or not hasattr(class_module, "__file__"): return False class_file = class_module.__file__ with open(class_file, "r", encoding="utf-8") as f: @@ -1967,9 +1967,9 @@ def _can_set_experts_implementation(cls) -> bool: """Detect whether the class supports setting its experts implementation dynamically. It is an ugly check based on opening the file, but avoids maintaining yet another property flag. """ - class_module = sys.modules[cls.__module__] + class_module = sys.modules.get(cls.__module__) # This can happen for a custom model in a jupyter notebook or repl for example - simply do not allow to set it then - if not hasattr(class_module, "__file__"): + if class_module is None or not hasattr(class_module, "__file__"): return False class_file = class_module.__file__ with open(class_file, "r", encoding="utf-8") as f: