Skip to content

Commit 47248e1

Browse files
authored
Refactor build_model() out of benchmark_model() (#48)
* Refactor build out of benchmarking Signed-off-by: Jeremy Fowers <[email protected]>
1 parent f8f8093 commit 47248e1

File tree

20 files changed

+286
-565
lines changed

20 files changed

+286
-565
lines changed

.github/workflows/publish-to-test-pypi.yml

+8-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ on:
55
branches: ["main", "canary"]
66
tags:
77
- v*
8+
- RC*
89
pull_request:
910
branches: ["main", "canary"]
1011

@@ -33,7 +34,13 @@ jobs:
3334
models=$(turnkey models location --quiet)
3435
turnkey $models/selftest/linear.py
3536
- name: Publish distribution package to PyPI
36-
if: startsWith(github.ref, 'refs/tags')
37+
if: startsWith(github.ref, 'refs/tags/v')
3738
uses: pypa/gh-action-pypi-publish@release/v1
3839
with:
3940
password: ${{ secrets.PYPI_API_TOKEN }}
41+
- name: Publish distribution package to Test PyPI
42+
if: startsWith(github.ref, 'refs/tags/RC')
43+
uses: pypa/gh-action-pypi-publish@release/v1
44+
with:
45+
password: ${{ secrets.TEST_PYPI_API_TOKEN }}
46+
repository_url: https://test.pypi.org/legacy/

.github/workflows/test_turnkey.yml

-3
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ jobs:
5353
# turnkey examples
5454
# Note: we clear the default cache location prior to each example run
5555
rm -rf ~/.cache/turnkey
56-
python examples/model_api/hello_world.py
57-
rm -rf ~/.cache/turnkey
5856
python examples/files_api/onnx_opset.py --onnx-opset 15
5957
rm -rf ~/.cache/turnkey
6058
turnkey examples/cli/scripts/hello_world.py
@@ -71,7 +69,6 @@ jobs:
7169
cd test/
7270
python cli.py
7371
python analysis.py
74-
python model_api.py
7572
- name: Test example plugins
7673
shell: bash -el {0}
7774
run: |

docs/code.md

+5-6
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ The TurnkeyML source code has a few major top-level directories:
1111
- `models`: the corpora of models that makes up the TurnkeyML models (see [the models readme](https://github.com/onnx/turnkeyml/blob/main/models/readme.md)).
1212
- Each subdirectory under `models` represents a corpus of models pulled from somewhere on the internet. For example, `models/torch_hub` is a corpus of models from [Torch Hub](https://github.com/pytorch/hub).
1313
- `src/turnkey`: source code for the TurnkeyML tools (see [Benchmarking Tools](#benchmarking-tools) for a description of how the code is used).
14-
- `src/turnkeyml/analyze`: functions for profiling a model script, discovering model instances, and invoking `benchmark_model()` on those instances.
15-
- `src/turnkeyml/run`: implements the runtime and device plugin APIs and the built-in runtimes and devices.
14+
- `src/turnkeyml/analyze`: functions for profiling a model script, discovering model instances, and invoking `build_model()` and/or `BaseRT.benchmark()` on those instances.
15+
- `src/turnkeyml/run`: implements `BaseRT`, an abstract base class that defines TurnkeyML's vendor-agnostic benchmarking functionality. This module also includes the runtime and device plugin APIs and the built-in runtimes and devices.
1616
- `src/turnkeyml/cli`: implements the `turnkey` CLI and reporting tool.
1717
- `src/turnkeyml/common`: functions common to the other modules.
1818
- `src/turnkeyml/version.py`: defines the package version number.
@@ -29,10 +29,9 @@ TurnkeyML provides two main tools, the `turnkey` CLI and benchmarking APIs. Inst
2929
1. The default command for `turnkey` CLI runs the `benchmark_files()` API, which is implemented in [files_api.py](https://github.com/onnx/turnkeyml/blob/main/src/turnkeyml/files_api.py).
3030
- Other CLI commands are also implemented in `cli/`, for example the `report` command is implemented in `cli/report.py`.
3131
1. The `benchmark_files()` API takes in a set of scripts, each of which should invoke at least one model instance, to evaluate and passes each into the `evaluate_script()` function for analysis, which is implemented in [analyze/script.py](https://github.com/onnx/turnkeyml/blob/main/src/turnkeyml/analyze/script.py).
32-
1. `evaluate_script()` uses a profiler to discover the model instances in the script, and passes each into the `benchmark_model()` API, which is defined in [model_api.py](https://github.com/onnx/turnkeyml/blob/main/src/turnkeyml/model_api.py).
33-
1. The `benchmark_model()` API prepares the model for benchmarking (e.g., exporting and optimizing an ONNX file), which creates an instance of a `*Model` class, where `*` can be CPU, GPU, etc. The `*Model` classes are defined in [run/](https://github.com/onnx/turnkeyml/blob/main/src/turnkeyml/run/).
34-
1. The `*Model` classes provide a `.benchmark()` method that benchmarks the model on the device and returns an instance of the `MeasuredPerformance` class, which includes the performance statistics acquired during benchmarking.
35-
1. `benchmark_model()` and the `*Model` classes are built using [`build_model()`](#model-build-tool)
32+
1. `evaluate_script()` uses a profiler to discover the model instances in the script, and passes each into the `build_model()` API, which is defined in [build_api.py](https://github.com/onnx/turnkeyml/blob/main/src/turnkeyml/build_api.py).
33+
1. The `build_model()` API prepares the model for benchmarking (e.g., exporting and optimizing an ONNX file).
34+
1. `evaluate_script()` passes the build into `BaseRT.benchmark()` to benchmarks the model on the device and returns an instance of the `MeasuredPerformance` class, which includes the performance statistics acquired during benchmarking.
3635

3736
# Model Build Tool
3837

docs/readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
This directory contains documentation for the TurnkeyML project:
44
- [code.md](https://github.com/onnx/turnkeyml/blob/main/docs/code.md): Code organization for the benchmark and tools.
55
- [install.md](https://github.com/onnx/turnkeyml/blob/main/docs/install.md): Installation instructions for the tools.
6-
- [tools_user_guide.md](https://github.com/onnx/turnkeyml/blob/main/docs/tools_user_guide.md): User guide for the tools: `turnkey` CLI, `benchmark_files()`, and `benchmark_model()`.
6+
- [tools_user_guide.md](https://github.com/onnx/turnkeyml/blob/main/docs/tools_user_guide.md): User guide for the tools: the `turnkey` CLI and the `benchmark_files()` and `build_model()` APIs.
77
- [versioning.md](https://github.com/onnx/turnkeyml/blob/main/docs/versioning.md): Defines the semantic versioning rules for the `turnkey` package.
88

99
There is more useful documentation available in:

docs/tools_user_guide.md

+15-43
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ Where `your_script.py` is a Python script that instantiates and executes a PyTor
5151

5252
The `turnkey` CLI performs the following steps:
5353
1. [Analysis](#analysis): profile the Python script to identify the PyTorch models within
54-
2. [Build](#build): call the `benchmark_files()` [API](#the-turnkey-api) to prepare each model for benchmarking
55-
3. [Benchmark](#benchmark): call the `benchmark_model()` [API](#the-turnkey-api) on each model to gather performance statistics
54+
2. [Build](#build): call the `build_models()` [API](#the-turnkey-api) to prepare each model for benchmarking
55+
3. [Benchmark](#benchmark): call the `BaseRT.benchmark()` method on each model to gather performance statistics
5656

5757
_Note_: The benchmarking methodology is defined [here](#benchmark). If you are looking for more detailed instructions on how to install turnkey, you can find that [here](https://github.com/onnx/turnkeyml/blob/main/docs/install.md).
5858

@@ -64,31 +64,11 @@ _Note_: The benchmarking methodology is defined [here](#benchmark). If you are l
6464

6565
Most of the functionality provided by the `turnkey` CLI is also available in the the API:
6666
- `turnkey.benchmark_files()` provides the same benchmarking functionality as the `turnkey` CLI: it takes a list of files and target device, and returns performance results.
67-
- `turnkey.benchmark_model()` provides a subset of this functionality: it takes a model and its inputs, and returns performance results.
68-
- The main difference is that `benchmark_model()` does not include the [Analysis](#analysis) feature, and `benchmark_files()` does.
6967
- `turnkey.build_model(model, inputs)` is used to programmatically [build](#build) a model instance through a sequence of model-to-model transformations (e.g., starting with an fp32 PyTorch model and ending with an fp16 ONNX model).
7068

71-
Generally speaking, the `turnkey` CLI is a command line interface for the `benchmark_files()` API, which internally calls `benchmark_model()`, which in turn calls `build_model()`. You can read more about this code organization [here](https://github.com/onnx/turnkeyml/blob/main/docs/code.md).
69+
Generally speaking, the `turnkey` CLI is a command line interface for the `benchmark_files()` API which in turn calls `build_model()` and then performs benchmarking using `BaseRT.benchmark()`. You can read more about this code organization [here](https://github.com/onnx/turnkeyml/blob/main/docs/code.md).
7270

73-
For an example of `benchmark_model()`, the following script:
74-
75-
```python
76-
from turnkeyml import benchmark_model
77-
78-
model = YourModel() # Instantiate a torch.nn.module
79-
results = model(**inputs)
80-
perf = benchmark_model(model, inputs)
81-
```
82-
83-
Will print an output like this:
84-
85-
```
86-
> Performance of YourModel on device Intel® Xeon® Platinum 8380 is:
87-
> latency: 0.033 ms
88-
> throughput: 21784.8 ips
89-
```
90-
91-
`benchmark_model()` returns a `MeasuredPerformance` object that includes members:
71+
`BaseRT.benchmark()` returns a `MeasuredPerformance` object that includes members:
9272
- `latency_units`: unit of time used for measuring latency, which is set to `milliseconds (ms)`.
9373
- `mean_latency`: average benchmarking latency, measured in `latency_units`.
9474
- `throughput_units`: unit used for measuring throughput, which is set to `inferences per second (IPS)`.
@@ -135,7 +115,7 @@ A **runtime** is a piece of software that executes a model on a device.
135115

136116
**Analysis** is the process by which `benchmark_files()` inspects a Python script or ONNX file and identifies the models within.
137117

138-
`benchmark_files()` performs analysis by running and profiling your file(s). When a model object (see [Model](#model) is encountered, it is inspected to gather statistics (such as the number of parameters in the model) and/or pass it to the `benchmark_model()` API for benchmarking.
118+
`benchmark_files()` performs analysis by running and profiling your file(s). When a model object (see [Model](#model) is encountered, it is inspected to gather statistics (such as the number of parameters in the model) and/or passed to the build and benchmark APIs.
139119

140120
> _Note_: the `turnkey` CLI and `benchmark_files()` API both run your entire python script(s) whenever python script(s) are passed as input files. Please ensure that these scripts are safe to run, especially if you got them from the internet.
141121
@@ -205,12 +185,14 @@ The *build cache* is a location on disk that holds all of the artifacts from you
205185

206186
## Benchmark
207187

208-
*Benchmark* is the process by which the `benchmark_model()` API collects performance statistics about a [model](#model). Specifically, `benchmark_model()` takes a [build](#build) of a model and executes it on a target device using target runtime software (see [Devices and Runtimes](#devices-and-runtimes)).
188+
*Benchmark* is the process by which `BaseRT.benchmark()` collects performance statistics about a [model](#model). `BaseRT` is an abstract base class that defines the common benchmarking infrastructure that TurnkeyML provides across devices and runtimes.
189+
190+
Specifically, `BaseRT.benchmark()` takes a [build](#build) of a model and executes it on a target device using target runtime software (see [Devices and Runtimes](#devices-and-runtimes)).
209191

210-
By default, `benchmark_model()` will run the model 100 times to collect the following statistics:
192+
By default, `BaseRT.benchmark()` will run the model 100 times to collect the following statistics:
211193
1. Mean Latency, in milliseconds (ms): the average time it takes the runtime/device combination to execute the model/inputs combination once. This includes the time spent invoking the device and transferring the model's inputs and outputs between host memory and the device (when applicable).
212194
1. Throughput, in inferences per second (IPS): the number of times the model/inputs combination can be executed on the runtime/device combination per second.
213-
> - _Note_: `benchmark_model()` is not aware of whether `inputs` is a single input or a batch of inputs. If your `inputs` is actually a batch of inputs, you should multiply `benchmark_model()`'s reported IPS by the batch size.
195+
> - _Note_: `BaseRT.benchmark()` is not aware of whether `inputs` is a single input or a batch of inputs. If your `inputs` is actually a batch of inputs, you should multiply `BaseRT.benchmark()`'s reported IPS by the batch size.
214196
215197
# Devices and Runtimes
216198

@@ -226,7 +208,7 @@ If you are using a remote machine, it must:
226208
- include the target device
227209
- have `miniconda`, `python>=3.8`, and `docker>=20.10` installed
228210

229-
When you call `turnkey` CLI or `benchmark_model()`, the following actions are performed on your behalf:
211+
When you call `turnkey` CLI or `benchmark_files()`, the following actions are performed on your behalf:
230212
1. Perform a `build`, which exports all models from the script to ONNX and prepares for benchmarking.
231213
1. Set up the benchmarking environment by loading a container and/or setting up a conda environment.
232214
1. Run the benchmarks.
@@ -253,7 +235,6 @@ Valid values of `TYPE` include:
253235
254236
Also available as API arguments:
255237
- `benchmark_files(device=...)`
256-
- `benchmark_model(device=...)`.
257238

258239
> For a detailed example, see the [CLI Nvidia tutorial](https://github.com/onnx/turnkeyml/blob/main/examples/cli/readme.md#nvidia-benchmarking).
259240
@@ -274,9 +255,8 @@ Each device type has its own default runtime, as indicated below.
274255

275256
This feature is also be available as an API argument:
276257
- `benchmark_files(runtime=[...])`
277-
- `benchmark_model(runtime=...)`
278258

279-
> _Note_: Inputs to `torch-eager` and `torch-compiled` are not downcasted to FP16 by default. Downcast inputs before benchmarking for a fair comparison between runtimes.
259+
> _Note_: Inputs to `torch-eager` and `torch-compiled` are not downcasted to FP16 by default. You must perform your own downcast or quantization of inputs if needed for apples-to-apples comparisons with other runtimes.
280260
281261
# Additional Commands and Options
282262

@@ -381,7 +361,6 @@ Process isolation mode applies a timeout to each subprocess. The default timeout
381361

382362
Also available as API arguments:
383363
- `benchmark_files(cache_dir=...)`
384-
- `benchmark_model(cache_dir=...)`
385364
- `build_model(cache_dir=...)`
386365

387366
> See the [Cache Directory tutorial](https://github.com/onnx/turnkeyml/blob/main/examples/cli/cache.md#cache-directory) for a detailed example.
@@ -392,7 +371,6 @@ Also available as API arguments:
392371

393372
Also available as API arguments:
394373
- `benchmark_files(lean_cache=True/False, ...)` (default False)
395-
- `benchmark_model(lean_cache=True/False, ...)` (default False)
396374

397375
> _Note_: useful for benchmarking many models, since the `build` artifacts from the models can take up a significant amount of hard drive space.
398376
@@ -409,7 +387,6 @@ Takes one of the following values:
409387

410388
Also available as API arguments:
411389
- `benchmark_files(rebuild=...)`
412-
- `benchmark_model(rebuild=...)`
413390
- `build_model(rebuild=...)`
414391

415392
### Sequence
@@ -421,7 +398,6 @@ Usage:
421398

422399
Also available as API arguments:
423400
- `benchmark_files(sequence=...)`
424-
- `benchmark_model(sequence=...)`
425401
- `build_model(sequence=...)`
426402

427403
### Set Script Arguments
@@ -460,7 +436,6 @@ Usage:
460436

461437
Also available as API arguments:
462438
- `benchmark_files(onnx_opset=...)`
463-
- `benchmark_model(onnx_opset=...)`
464439
- `build_model(onnx_opset=...)`
465440

466441
> _Note_: ONNX opset can also be set by an environment variable. The --onnx-opset argument takes precedence over the environment variable. See [TURNKEY_ONNX_OPSET](#set-the-onnx-opset).
@@ -474,11 +449,10 @@ Usage:
474449

475450
Also available as API arguments:
476451
- `benchmark_files(iterations=...)`
477-
- `benchmark_model(iterations=...)`
478452

479453
### Analyze Only
480454

481-
Instruct `turnkey` or `benchmark_model()` to only run the [Analysis](#analysis) phase of the `benchmark` command.
455+
Instruct `turnkey` or `benchmark_files()` to only run the [Analysis](#analysis) phase of the `benchmark` command.
482456

483457
Usage:
484458
- `turnkey benchmark INPUT_FILES --analyze-only`
@@ -493,7 +467,7 @@ Also available as an API argument:
493467
494468
### Build Only
495469

496-
Instruct `turnkey`, `benchmark_files()`, or `benchmark_model()` to only run the [Analysis](#analysis) and [Build](#build) phases of the `benchmark` command.
470+
Instruct `turnkey` or `benchmark_files()` to only run the [Analysis](#analysis) and [Build](#build) phases of the `benchmark` command.
497471

498472
Usage:
499473
- `turnkey benchmark INPUT_FILES --build-only`
@@ -503,7 +477,6 @@ Usage:
503477
504478
Also available as API arguments:
505479
- `benchmark_files(build_only=True/False)` (default False)
506-
- `benchmark_model(build_only=True/False)` (default False)
507480

508481
> See the [Build Only tutorial](https://github.com/onnx/turnkeyml/blob/main/examples/cli/build.md#build-only) for a detailed example.
509482
@@ -515,7 +488,6 @@ None of the built-in runtimes support such arguments, however plugin contributor
515488

516489
Also available as API arguments:
517490
- `benchmark_files(rt_args=Dict)` (default None)
518-
- `benchmark_model(rt_args=Dict)` (default None)
519491

520492
## Cache Commands
521493

@@ -635,7 +607,7 @@ export TURNKEY_DEBUG=True
635607

636608
### Set the ONNX Opset
637609

638-
By default, `turnkey`, `benchmark_files()`, and `benchmark_model()` will use the default ONNX opset defined in `turnkey.common.build.DEFAULT_ONNX_OPSET`. You can set a different default ONNX opset by setting the `TURNKEY_ONNX_OPSET` environment variable.
610+
By default, `turnkey`, `benchmark_files()`, and `build_model()` will use the default ONNX opset defined in `turnkey.common.build.DEFAULT_ONNX_OPSET`. You can set a different default ONNX opset by setting the `TURNKEY_ONNX_OPSET` environment variable.
639611

640612
For example:
641613

examples/cli/plugins/example_seq/turnkeyml_plugin_example_seq/sequence.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
This script is an example of a sequence.py file for Sequence Plugin. Such a sequence.py
33
can be used to redefine the build phase of the turnkey CLI, benchmark_files(),
4-
and benchmark_model() to have any custom behavior.
4+
and build_model() to have any custom behavior.
55
66
In this example sequence.py file we are setting the build sequence to simply
77
export from pytorch to ONNX. This differs from the default build sequence, which

examples/model_api/hello_world.py

-62
This file was deleted.

examples/readme.md

-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@
22

33
This directory contains examples to help you learn how to use the tools. The examples are split up into two sub-directories:
44
1. `examples/cli`: a tutorial series for the `turnkey` CLI. This is the recommended starting point.
5-
1. `examples/model_api`: scripts that demonstrate how to use the `turnkey.benchmark_model()` API.
65
1. `examples/files_api`: scripts that demonstrate how to use the `turnkey.benchmark_files()` API.
76
1. `examples/build_api`: scripts that demonstrate how to use the `turnkey.build_model()` API.

src/turnkeyml/__init__.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from turnkeyml.version import __version__
22

33
from .files_api import benchmark_files
4-
from .model_api import benchmark_model
54
from .cli.cli import main as turnkeycli
65
from .build_api import build_model
76
from .common.build import load_state

0 commit comments

Comments
 (0)