Files
wehub-resource-sync c8a779b1bb
Docker Image CI / build-ubuntu2004 (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:36:55 +08:00

65 lines
2.1 KiB
Markdown

# Polygraphy Python API Examples
This directory includes examples that use the Polygraphy Python API.
For examples of the command-line tools, see the [cli](../cli/) directory instead.
You may find it useful to read the [Python API Overview](../../polygraphy/) prior
to looking at the API examples.
## Generating Your Own Examples
In the event that the examples here do not cover a particular use-case, you can typically
use `polygraphy run` to fill the gap; `polygraphy run` is capable of dynamically generating
Python scripts that use the Polygraphy API that do exactly what the tool would otherwise do.
Thus, if `polygraphy run` includes functionality you need, but you cannot find a
corresponding API example, try running:
```bash
polygraphy run --gen - <options...>
```
The argument to `--gen` should be the name of the file in which to write the generated script.
The special value `-` corresponds to `stdout`.
For example, running:
```bash
polygraphy run --gen - model.onnx --trt --onnxrt
```
will display something like this on `stdout`:
```py
#!/usr/bin/env python3
# Template auto-generated by polygraphy [v0.31.0] on 01/01/20 at 10:10:10
# Generation Command: polygraphy run --gen - model.onnx --trt --onnxrt
# This script compares model.onnx between TensorRT and ONNX-Runtime
from polygraphy.logger import G_LOGGER
from polygraphy.backend.onnxrt import OnnxrtRunner, SessionFromOnnx
from polygraphy.backend.trt import EngineFromNetwork, NetworkFromOnnxPath, TrtRunner
from polygraphy.comparator import Comparator
import sys
# Loaders
parse_network_from_onnx = NetworkFromOnnxPath('model.onnx')
build_engine = EngineFromNetwork(parse_network_from_onnx)
build_onnxrt_session = SessionFromOnnx('model.onnx')
# Runners
runners = [
TrtRunner(build_engine),
OnnxrtRunner(build_onnxrt_session),
]
# Runner Execution
results = Comparator.run(runners)
success = True
# Accuracy Comparison
success &= bool(Comparator.compare_accuracy(results))
# Report Results
cmd_run = ' '.join(sys.argv)
if not success:
G_LOGGER.critical(f"FAILED | Command: {cmd_run}"))
G_LOGGER.finish(f"PASSED | Command: {cmd_run}"))
```