Skip to content
This repository was archived by the owner on Jun 3, 2025. It is now read-only.

Commit e207a32

Browse files
rahul-tulibfineran
andauthored
Benchmark-Sweep script upgrades (#830)
* Align row values with column Headers * Upgrade `benchmark_sweep.py` to a cli script Add error catching Make `benchmark_sweep` function more robust Default `num_cores` to max available cores if `None`, (used to fail before due to a call to `_parse_num_cores` when in `benchmark_model`) Add docstrings wherever necessary Add Style * Address comments from @KSGulin and @corey-nm * Address comments from @bfineran * Dynamic Engine Load (#842) * Add skeleton for loading custom `Engine` * Add code to dynamically import the engine class * Add code on benchmark_sweep side to accept engine from user * Add support for generating random inputs using the engine object (required for dynamically imported engine classes) as the graph file might not be in `ONNX`, so our `generate_random_inputs` function could potentially fail * Update src/deepsparse/benchmark/benchmark_model.py Co-authored-by: Benjamin Fineran <[email protected]> Co-authored-by: Benjamin Fineran <[email protected]> * Address comments from @bfineran Co-authored-by: Benjamin Fineran <[email protected]>
1 parent 3fa85a0 commit e207a32

File tree

2 files changed

+337
-73
lines changed

2 files changed

+337
-73
lines changed

src/deepsparse/benchmark/benchmark_model.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
"""
9393

9494
import argparse
95+
import importlib
9596
import json
9697
import logging
9798
import os
@@ -214,10 +215,12 @@ def parse_args():
214215
"--engine",
215216
type=str,
216217
default=DEEPSPARSE_ENGINE,
217-
choices=[DEEPSPARSE_ENGINE, ORT_ENGINE],
218218
help=(
219219
"Inference engine backend to run eval on. Choices are 'deepsparse', "
220-
"'onnxruntime'. Default is 'deepsparse'"
220+
"'onnxruntime'. Default is 'deepsparse'. Can also specify a user "
221+
"defined engine class by giving the script and class name in the "
222+
"following format <path to python script>:<Engine Class name>. This "
223+
"engine class will be dynamically imported during runtime"
221224
),
222225
)
223226
parser.add_argument(
@@ -308,6 +311,22 @@ def parse_num_streams(num_streams: int, num_cores: int, scenario: str):
308311
return default_num_streams
309312

310313

314+
def load_custom_engine(custom_engine_identifier: str):
315+
"""
316+
import a custom engine based off the specified `custom_engine_identifier`
317+
from user specified script
318+
319+
:param custom_engine_identifier: string in the form of
320+
'<path_to_the_python_script>:<custom_engine_class_name>
321+
:return: custom engine class object
322+
"""
323+
path, engine_object_name = custom_engine_identifier.split(":")
324+
spec = importlib.util.spec_from_file_location("user_defined_custom_engine", path)
325+
module = importlib.util.module_from_spec(spec)
326+
spec.loader.exec_module(module)
327+
return getattr(module, engine_object_name)
328+
329+
311330
def benchmark_model(
312331
model_path: str,
313332
batch_size: int = 1,
@@ -352,6 +371,13 @@ def benchmark_model(
352371
num_cores=num_cores,
353372
input_shapes=input_shapes,
354373
)
374+
elif ":" in engine:
375+
engine = load_custom_engine(custom_engine_identifier=engine)
376+
model = engine(
377+
model_path=model_path,
378+
batch_size=batch_size,
379+
num_cores=num_cores,
380+
)
355381
else:
356382
raise ValueError(f"Invalid engine choice '{engine}'")
357383
_LOGGER.info(model)
@@ -361,6 +387,8 @@ def benchmark_model(
361387
if input_shapes:
362388
with override_onnx_input_shapes(model_path, input_shapes) as model_path:
363389
input_list = generate_random_inputs(model_path, batch_size)
390+
elif hasattr(engine, "generate_random_inputs"):
391+
input_list = engine.generate_random_inputs(batch_size=batch_size)
364392
else:
365393
input_list = generate_random_inputs(model_path, batch_size)
366394

0 commit comments

Comments
 (0)