Polygraphy

0.49.26 · active · verified Thu Apr 16

Polygraphy is a Deep Learning Inference Prototyping and Debugging Toolkit developed by NVIDIA. It helps users run, compare, and debug inference across various deep learning frameworks and backends, especially NVIDIA TensorRT. It provides Python APIs and a command-line interface for tasks like model conversion, correctness checking, and performance profiling. Polygraphy is tightly integrated with the TensorRT ecosystem and typically receives updates in conjunction with TensorRT releases.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use Polygraphy's `Comparator` to run and compare inference on a dummy ONNX model using both TensorRT and ONNX Runtime backends. It highlights the need to specify input shapes for the TensorRT runner and how to set up basic logging.

import numpy as np
from polygraphy.comparator import Comparator, SizingInput
from polygraphy.backend.trt import TrtRunner, TrtConfig
from polygraphy.backend.onnxrt import OnnxrtRunner
from polygraphy.logger import G_LOGGER
import os

# --- Dummy ONNX Model Creation (for runnable quickstart) ---
# In a real scenario, you would load your own ONNX model.
# This creates a simple identity model for demonstration purposes.
onnx_model_path = "identity.onnx"
if not os.path.exists(onnx_model_path):
    import onnx
    graph = onnx.helper.make_graph(
        [onnx.helper.make_node("Identity", ["input_0"], ["output_0"])],
        "identity_graph",
        [onnx.helper.make_tensor_value_info("input_0", onnx.TensorProto.FLOAT, [1, 3, 224, 224])],
        [onnx.helper.make_tensor_value_info("output_0", onnx.TensorProto.FLOAT, [1, 3, 224, 224])],
    )
    onnx_model = onnx.helper.make_model(graph, producer_name="polygraphy-quickstart")
    onnx.save(onnx_model, onnx_model_path)
# --- End Dummy Model Creation ---

G_LOGGER.severity = G_LOGGER.INFO # Set logging severity

# Define input data for the model
input_data = {
    "input_0": np.random.rand(1, 3, 224, 224).astype(np.float32)
}

with Comparator() as c:
    # Add a TensorRT runner
    # Input shapes are required for TensorRT
    c.add_runner(TrtRunner(
        TrtConfig(),
        input_shapes=[SizingInput("input_0", (1, 3, 224, 224))]
    ))

    # Add an ONNX-Runtime runner
    c.add_runner(OnnxrtRunner())

    # Run the comparison
    c.run(onnx_model_path, data=input_data)

    # Results can be accessed via c.get_comparison_results()
    print("Polygraphy comparison completed for", onnx_model_path)

# Clean up the dummy model
os.remove(onnx_model_path)

view raw JSON →