Skip to content

Commit dcab3f9

Browse files
committed
bring back functionalities that were lost in v2 during rebasing
1 parent 39be9a0 commit dcab3f9

File tree

2 files changed

+27
-23
lines changed

2 files changed

+27
-23
lines changed

src/deepsparse/transformers/helpers.py

+10-21
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@
3030
from onnx import ModelProto
3131

3232
from deepsparse.log import get_main_logger
33-
from deepsparse.utils.onnx import MODEL_ONNX_NAME, truncate_onnx_model
34-
from sparsezoo import Model
33+
from deepsparse.utils.onnx import MODEL_ONNX_NAME, model_to_path, truncate_onnx_model
3534
from sparsezoo.utils import save_onnx
3635

3736

3837
__all__ = [
38+
"get_deployment_path",
3939
"setup_transformers_pipeline",
4040
"overwrite_transformer_onnx_model_inputs",
4141
"fix_numpy_types",
@@ -62,12 +62,12 @@ def setup_transformers_pipeline(
6262
:param sequence_length: The sequence length to use for the model
6363
:param tokenizer_padding_side: The side to pad on for the tokenizer,
6464
either "left" or "right"
65-
:param engine_kwargs: The kwargs to pass to the engine
6665
:param onnx_model_name: The name of the onnx model to be loaded.
6766
If not specified, defaults are used (see setup_onnx_file_path)
67+
:param engine_kwargs: The kwargs to pass to the engine
6868
:return The model path, config, tokenizer, and engine kwargs
6969
"""
70-
model_path, config, tokenizer = setup_onnx_file_path(
70+
model_path, config, tokenizer = fetch_onnx_file_path(
7171
model_path, sequence_length, onnx_model_name
7272
)
7373

@@ -87,7 +87,7 @@ def setup_transformers_pipeline(
8787
return model_path, config, tokenizer, engine_kwargs
8888

8989

90-
def setup_onnx_file_path(
90+
def fetch_onnx_file_path(
9191
model_path: str,
9292
sequence_length: int,
9393
onnx_model_name: Optional[str] = None,
@@ -102,6 +102,7 @@ def setup_onnx_file_path(
102102
:param onnx_model_name: optionally, the precise name of the ONNX model
103103
of interest may be specified. If not specified, the default ONNX model
104104
name will be used (refer to `get_deployment_path` for details)
105+
:param task: task to use for the config. Defaults to None
105106
:return: file path to the processed ONNX file for the engine to compile
106107
"""
107108
deployment_path, onnx_path = get_deployment_path(model_path, onnx_model_name)
@@ -148,6 +149,7 @@ def get_deployment_path(
148149
the deployment directory
149150
"""
150151
onnx_model_name = onnx_model_name or MODEL_ONNX_NAME
152+
151153
if os.path.isfile(model_path):
152154
# return the parent directory of the ONNX file
153155
return os.path.dirname(model_path), model_path
@@ -163,22 +165,9 @@ def get_deployment_path(
163165
)
164166
return model_path, os.path.join(model_path, onnx_model_name)
165167

166-
elif model_path.startswith("zoo:"):
167-
zoo_model = Model(model_path)
168-
deployment_path = zoo_model.deployment_directory_path
169-
return deployment_path, os.path.join(deployment_path, onnx_model_name)
170-
elif model_path.startswith("hf:"):
171-
from huggingface_hub import snapshot_download
172-
173-
deployment_path = snapshot_download(repo_id=model_path.replace("hf:", "", 1))
174-
onnx_path = os.path.join(deployment_path, onnx_model_name)
175-
if not os.path.isfile(onnx_path):
176-
raise ValueError(
177-
f"{onnx_model_name} not found in transformers model directory "
178-
f"{deployment_path}. Be sure that an export of the model is written to "
179-
f"{onnx_path}"
180-
)
181-
return deployment_path, onnx_path
168+
elif model_path.startswith("zoo:") or model_path.startswith("hf:"):
169+
onnx_model_path = model_to_path(model_path)
170+
return os.path.dirname(onnx_model_path), onnx_model_path
182171
else:
183172
raise ValueError(
184173
f"model_path {model_path} is not a valid file, directory, or zoo stub"

src/deepsparse/utils/onnx.py

+17-2
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def model_to_path(model: Union[str, Model, File]) -> str:
129129

130130
if Model is not object and isinstance(model, Model):
131131
# trigger download and unzipping of deployment directory if not cached
132-
model.deployment_directory_path
132+
model.deployment.path
133133

134134
# default to the main onnx file for the model
135135
model = model.deployment.get_file(MODEL_ONNX_NAME).path
@@ -138,6 +138,21 @@ def model_to_path(model: Union[str, Model, File]) -> str:
138138
# get the downloaded_path -- will auto download if not on local system
139139
model = model.path
140140

141+
if isinstance(model, str) and model.startswith("hf:"):
142+
# load Hugging Face model from stub
143+
from huggingface_hub import snapshot_download
144+
145+
deployment_path = snapshot_download(repo_id=model.replace("hf:", "", 1))
146+
onnx_path = os.path.join(deployment_path, MODEL_ONNX_NAME)
147+
if not os.path.isfile(onnx_path):
148+
raise ValueError(
149+
f"Could not find the ONNX model file '{MODEL_ONNX_NAME}' in the "
150+
f"Hugging Face Hub repository located at {deployment_path}. Please "
151+
f"ensure the model has been correctly exported to ONNX format and "
152+
f"exists in the repository."
153+
)
154+
return onnx_path
155+
141156
if not isinstance(model, str):
142157
raise ValueError("unsupported type for model: {}".format(type(model)))
143158

@@ -549,7 +564,7 @@ def overwrite_onnx_model_inputs_for_kv_cache_models(
549564
else:
550565
raise ValueError(f"Unexpected external input name: {external_input.name}")
551566

552-
_LOGGER.info(
567+
_LOGGER.debug(
553568
"Overwriting in-place the input shapes "
554569
f"of the transformer model at {onnx_file_path}"
555570
)

0 commit comments

Comments
 (0)