Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

better handle noninstalled libs error during model loading #2071

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions optimum/exporters/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1378,7 +1378,12 @@ def get_model_class_for_task(

if (framework, model_type, task) in TasksManager._CUSTOM_CLASSES:
library, class_name = TasksManager._CUSTOM_CLASSES[(framework, model_type, task)]
loaded_library = importlib.import_module(library)

try:
loaded_library = importlib.import_module(library)
except ModuleNotFoundError:
raise ValueError(f"`{library}` selected as model source, but `{library}` is not installed. Please install it.")

IlyasMoutawwakil marked this conversation as resolved.
Show resolved Hide resolved

return getattr(loaded_library, class_name)
else:
Expand All @@ -1387,7 +1392,10 @@ def get_model_class_for_task(
else:
tasks_to_model_loader = TasksManager._LIBRARY_TO_TF_TASKS_TO_MODEL_LOADER_MAP[library]

loaded_library = importlib.import_module(library)
try:
loaded_library = importlib.import_module(library)
except ModuleNotFoundError:
raise ValueError(f"`{library}` selected as model source, but `{library}` is not installed. Please install it.")

if model_class_name is None:
if task not in tasks_to_model_loader:
Expand Down Expand Up @@ -2063,10 +2071,13 @@ def get_model_from_task(
model_class_name = config.architectures[0]

if library_name == "diffusers":
config = DiffusionPipeline.load_config(model_name_or_path, **kwargs)
class_name = config.get("_class_name", None)
loaded_library = importlib.import_module(library_name)
model_class = getattr(loaded_library, class_name)
if is_diffusers_available():
config = DiffusionPipeline.load_config(model_name_or_path, **kwargs)
class_name = config.get("_class_name", None)
loaded_library = importlib.import_module(library_name)
model_class = getattr(loaded_library, class_name)
else:
raise ValueError("`diffusers` library selected as model source, but `diffusers` is not installed. Please install it.")
else:
model_class = TasksManager.get_model_class_for_task(
task, framework, model_type=model_type, model_class_name=model_class_name, library=library_name
Expand Down Expand Up @@ -2219,3 +2230,4 @@ def get_exporter_config_constructor(
exporter_config_constructor = partial(exporter_config_constructor, **exporter_config_kwargs)

return exporter_config_constructor

IlyasMoutawwakil marked this conversation as resolved.
Show resolved Hide resolved
Loading