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

Improve the offline support for the ONNX/TFLite export #1109

Merged
merged 4 commits into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 14 additions & 8 deletions optimum/exporters/onnx/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import argparse
from pathlib import Path

from requests.exceptions import ConnectionError as RequestsConnectionError
fxmarty marked this conversation as resolved.
Show resolved Hide resolved
from transformers import AutoTokenizer
from transformers.utils import is_torch_available

Expand Down Expand Up @@ -166,6 +167,19 @@ def main_export(
)

torch_dtype = None if fp16 is False else torch.float16

if task == "auto":
try:
task = TasksManager.infer_task_from_model(model_name_or_path)
except KeyError as e:
raise KeyError(
f"The task could not be automatically inferred. Please provide the argument --task with the relevant task from {', '.join(TasksManager.get_all_tasks())}. Detailed error: {e}"
)
except RequestsConnectionError as e:
raise RequestsConnectionError(
f"The task could not be automatically inferred as this is available only for models hosted on the Hugging Face Hub. Please provide the argument --task with the relevant task from {', '.join(TasksManager.get_all_tasks())}. Detailed error: {e}"
)

model = TasksManager.get_model_from_task(
task,
model_name_or_path,
Expand All @@ -180,14 +194,6 @@ def main_export(
torch_dtype=torch_dtype,
)

if task == "auto":
try:
task = TasksManager.infer_task_from_model(model_name_or_path)
except KeyError as e:
raise KeyError(
f"The task could not be automatically inferred. Please provide the argument --task with the task from {', '.join(TasksManager.get_all_tasks())}. Detailed error: {e}"
)

if task != "stable-diffusion" and task + "-with-past" in TasksManager.get_supported_tasks_for_model_type(
model.config.model_type.replace("_", "-"), "onnx"
):
Expand Down
68 changes: 51 additions & 17 deletions optimum/exporters/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Set, Tuple, Type, Union

import huggingface_hub
from requests.exceptions import ConnectionError as RequestsConnectionError
from transformers import AutoConfig, PretrainedConfig, is_tf_available, is_torch_available
from transformers.utils import TF2_WEIGHTS_NAME, WEIGHTS_NAME, logging
from transformers.utils import SAFE_WEIGHTS_NAME, TF2_WEIGHTS_NAME, WEIGHTS_NAME, logging

from ..utils.import_utils import is_onnx_available

Expand Down Expand Up @@ -1125,15 +1126,18 @@ def get_model_class_for_task(

@staticmethod
def determine_framework(
model_name_or_path: Union[str, Path], subfolder: str = "", framework: Optional[str] = None
model_name_or_path: Union[str, Path],
subfolder: str = "",
framework: Optional[str] = None,
cache_dir: str = huggingface_hub.constants.HUGGINGFACE_HUB_CACHE,
) -> str:
"""
Determines the framework to use for the export.

The priority is in the following order:
1. User input via `framework`.
2. If local checkpoint is provided, use the same framework as the checkpoint.
3. If model repo, try to infer the framework from the Hub.
3. If model repo, try to infer the framework from the cache if available, else from the Hub.
4. If could not infer, use available framework in environment, with priority given to PyTorch.

Args:
Expand All @@ -1153,6 +1157,7 @@ def determine_framework(
if framework is not None:
return framework

request_exception = None
full_model_path = Path(model_name_or_path) / subfolder
if full_model_path.is_dir():
all_files = [
Expand All @@ -1161,15 +1166,39 @@ def determine_framework(
for file in filenames
]
else:
if not isinstance(model_name_or_path, str):
model_name_or_path = str(model_name_or_path)
all_files = huggingface_hub.list_repo_files(model_name_or_path, repo_type="model")
if subfolder != "":
all_files = [file[len(subfolder) + 1 :] for file in all_files if file.startswith(subfolder)]

weight_name = Path(WEIGHTS_NAME).stem
weight_extension = Path(WEIGHTS_NAME).suffix
is_pt_weight_file = [file.startswith(weight_name) and file.endswith(weight_extension) for file in all_files]
try:
if not isinstance(model_name_or_path, str):
model_name_or_path = str(model_name_or_path)
all_files = huggingface_hub.list_repo_files(model_name_or_path, repo_type="model")
if subfolder != "":
all_files = [file[len(subfolder) + 1 :] for file in all_files if file.startswith(subfolder)]
except RequestsConnectionError as e: # Hub not accessible
request_exception = e
object_id = model_name_or_path.replace("/", "--")
full_model_path = Path(cache_dir, f"models--{object_id}")
if full_model_path.is_dir(): # explore the cache first
# Resolve refs (for instance to convert main to the associated commit sha)
revision_file = Path(full_model_path, "refs", "main")
revision = ""
if revision_file.is_file():
with open(revision_file) as f:
revision = f.read()
cached_path = Path(full_model_path, "snapshots", revision, subfolder)
all_files = [
os.path.relpath(os.path.join(dirpath, file), cached_path)
for dirpath, _, filenames in os.walk(cached_path)
for file in filenames
]

pt_weight_name = Path(WEIGHTS_NAME).stem
pt_weight_extension = Path(WEIGHTS_NAME).suffix
safe_weight_name = Path(SAFE_WEIGHTS_NAME).stem
safe_weight_extension = Path(SAFE_WEIGHTS_NAME).suffix
is_pt_weight_file = [
(file.startswith(pt_weight_name) and file.endswith(pt_weight_extension))
or (file.startswith(safe_weight_name) and file.endswith(safe_weight_extension))
for file in all_files
]

weight_name = Path(TF2_WEIGHTS_NAME).stem
weight_extension = Path(TF2_WEIGHTS_NAME).suffix
Expand All @@ -1183,11 +1212,16 @@ def determine_framework(
# stable diffusion case
framework = "pt"
else:
raise FileNotFoundError(
"Cannot determine framework from given checkpoint location."
f" There should be a {Path(WEIGHTS_NAME).stem}*{Path(WEIGHTS_NAME).suffix} for PyTorch"
f" or {Path(TF2_WEIGHTS_NAME).stem}*{Path(TF2_WEIGHTS_NAME).suffix} for TensorFlow."
)
if request_exception is not None:
raise RequestsConnectionError(
f"The framework could not be automatically inferred. If using the command-line, please provide the argument --framework (pt,tf) Detailed error: {request_exception}"
)
else:
raise FileNotFoundError(
"Cannot determine framework from given checkpoint location."
f" There should be a {Path(WEIGHTS_NAME).stem}*{Path(WEIGHTS_NAME).suffix} for PyTorch"
f" or {Path(TF2_WEIGHTS_NAME).stem}*{Path(TF2_WEIGHTS_NAME).suffix} for TensorFlow."
)

if is_torch_available():
framework = framework or "pt"
Expand Down
6 changes: 6 additions & 0 deletions optimum/exporters/tflite/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

from argparse import ArgumentParser

from requests.exceptions import ConnectionError as RequestsConnectionError

from ...commands.export.tflite import parse_args_tflite
from ...utils import logging
from ...utils.save_utils import maybe_load_preprocessors, maybe_save_preprocessors
Expand Down Expand Up @@ -51,6 +53,10 @@ def main():
"The task could not be automatically inferred. Please provide the argument --task with the task "
f"from {', '.join(TasksManager.get_all_tasks())}. Detailed error: {e}"
)
except RequestsConnectionError as e:
raise RequestsConnectionError(
f"The task could not be automatically inferred as this is available only for models hosted on the Hugging Face Hub. Please provide the argument --task with the relevant task from {', '.join(TasksManager.get_all_tasks())}. Detailed error: {e}"
)

model = TasksManager.get_model_from_task(
task, args.model, framework="tf", cache_dir=args.cache_dir, trust_remote_code=args.trust_remote_code
Expand Down