-
Notifications
You must be signed in to change notification settings - Fork 667
Exporters automatic task detection #445
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,8 @@ | |
| from transformers import PretrainedConfig, is_tf_available, is_torch_available | ||
| from transformers.utils import TF2_WEIGHTS_NAME, WEIGHTS_NAME, logging | ||
|
|
||
| import huggingface_hub | ||
|
|
||
|
|
||
| if TYPE_CHECKING: | ||
| from transformers import PreTrainedModel, TFPreTrainedModel | ||
|
|
@@ -568,7 +570,7 @@ def get_supported_tasks_for_model_type( | |
| return TasksManager._SUPPORTED_MODEL_TYPE[model_type][exporter] | ||
|
|
||
| @staticmethod | ||
| def task_to_task(task: str) -> str: | ||
| def format_task(task: str) -> str: | ||
| return task.replace("-with-past", "") | ||
|
|
||
| @staticmethod | ||
|
|
@@ -598,7 +600,7 @@ def get_model_class_for_task(task: str, framework: str = "pt") -> Type: | |
| Returns: | ||
| The AutoModel class corresponding to the task. | ||
| """ | ||
| task = TasksManager.task_to_task(task) | ||
| task = TasksManager.format_task(task) | ||
| TasksManager._validate_framework_choice(framework) | ||
| if framework == "pt": | ||
| task_to_automodel = TasksManager._TASKS_TO_AUTOMODELS | ||
|
|
@@ -659,6 +661,37 @@ def determine_framework(model: str, framework: str = None) -> str: | |
|
|
||
| return framework | ||
|
|
||
| @staticmethod | ||
| def infer_task_from_model(model_name_or_path): | ||
| tasks_to_automodels = {} | ||
| class_name_prefix = "" | ||
| if is_torch_available(): | ||
| tasks_to_automodels = TasksManager._TASKS_TO_AUTOMODELS | ||
| else: | ||
| tasks_to_automodels = TasksManager._TASKS_TO_TF_AUTOMODELS | ||
| class_name_prefix = "TF" | ||
|
|
||
| inferred_task_name = None | ||
| is_local = os.path.isdir(model_name_or_path) | ||
|
|
||
| if is_local: | ||
| # TODO: implement this. | ||
| raise NotImplementedError("Cannot infer the task from a local directory yet.") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure we even want to support this, in a first version at least
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alright, changed the message and the exception to something that does not open the door for future support (even though it might come someday). |
||
| else: | ||
| model_info = huggingface_hub.model_info(model_name_or_path) | ||
| transformers_info = model_info.transformersInfo | ||
| if transformers_info is None or transformers_info.get("auto_model") is None: | ||
| raise RuntimeError(f"Could not infer the task from the model repo {model_name_or_path}") | ||
| auto_model_class_name = f"{class_name_prefix}{transformers_info['auto_model']}" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think |
||
| for task_name, class_ in tasks_to_automodels.items(): | ||
| if class_.__name__ == auto_model_class_name: | ||
| inferred_task_name = task_name | ||
| break | ||
| if inferred_task_name is None: | ||
| raise KeyError(f"Could not find the proper task name for {auto_model_class_name}.") | ||
| logger.info(f"Automatic task detection to {inferred_task_name}.") | ||
| return inferred_task_name | ||
|
|
||
| @staticmethod | ||
| def get_model_from_task( | ||
| task: str, model: str, framework: str = None, cache_dir: str = None | ||
|
|
@@ -682,6 +715,8 @@ def get_model_from_task( | |
|
|
||
| """ | ||
| framework = TasksManager.determine_framework(model, framework) | ||
| if task == "auto": | ||
| task = TasksManager.infer_task_from_model(model) | ||
| model_class = TasksManager.get_model_class_for_task(task, framework) | ||
| try: | ||
| model = model_class.from_pretrained(model, cache_dir=cache_dir) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.