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: