Skip to content

Commit

Permalink
Add hf: stub support to model_to_path (#1378)
Browse files Browse the repository at this point in the history
* Add hf: stub support to model_to_path

* Refactor to use model_to_path in transformers/helpers
  • Loading branch information
mgoin authored Nov 2, 2023
1 parent 07e2a98 commit 28c666c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
26 changes: 8 additions & 18 deletions src/deepsparse/transformers/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@
from onnx import ModelProto

from deepsparse.log import get_main_logger
from deepsparse.utils.onnx import _MODEL_DIR_ONNX_NAME, truncate_onnx_model
from sparsezoo import Model
from deepsparse.utils.onnx import (
_MODEL_DIR_ONNX_NAME,
model_to_path,
truncate_onnx_model,
)
from sparsezoo.utils import save_onnx


Expand Down Expand Up @@ -71,22 +74,9 @@ def get_deployment_path(model_path: str) -> Tuple[str, str]:
)
return model_path, os.path.join(model_path, _MODEL_DIR_ONNX_NAME)

elif model_path.startswith("zoo:"):
zoo_model = Model(model_path)
deployment_path = zoo_model.deployment_directory_path
return deployment_path, os.path.join(deployment_path, _MODEL_DIR_ONNX_NAME)
elif model_path.startswith("hf:"):
from huggingface_hub import snapshot_download

deployment_path = snapshot_download(repo_id=model_path.replace("hf:", "", 1))
onnx_path = os.path.join(deployment_path, _MODEL_DIR_ONNX_NAME)
if not os.path.isfile(onnx_path):
raise ValueError(
f"{_MODEL_DIR_ONNX_NAME} not found in transformers model directory "
f"{deployment_path}. Be sure that an export of the model is written to "
f"{onnx_path}"
)
return deployment_path, onnx_path
elif model_path.startswith("zoo:") or model_path.startswith("hf:"):
onnx_model_path = model_to_path(model_path)
return os.path.dirname(onnx_model_path), onnx_model_path
else:
raise ValueError(
f"model_path {model_path} is not a valid file, directory, or zoo stub"
Expand Down
15 changes: 15 additions & 0 deletions src/deepsparse/utils/onnx.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,21 @@ def model_to_path(model: Union[str, Model, File]) -> str:
# get the downloaded_path -- will auto download if not on local system
model = model.path

if isinstance(model, str) and model.startswith("hf:"):
# load Hugging Face model from stub
from huggingface_hub import snapshot_download

deployment_path = snapshot_download(repo_id=model.replace("hf:", "", 1))
onnx_path = os.path.join(deployment_path, _MODEL_DIR_ONNX_NAME)
if not os.path.isfile(onnx_path):
raise ValueError(
f"Could not find the ONNX model file '{_MODEL_DIR_ONNX_NAME}' in the "
f"Hugging Face Hub repository located at {deployment_path}. Please "
f"ensure the model has been correctly exported to ONNX format and "
f"exists in the repository."
)
return onnx_path

if not isinstance(model, str):
raise ValueError("unsupported type for model: {}".format(type(model)))

Expand Down

0 comments on commit 28c666c

Please sign in to comment.