This sample demonstrates how to use the TensorRT Python API to serialize an engine directly to a custom stream using the IStreamWriter interface, rather than to a file or in-memory buffer. This is useful for advanced scenarios where you want to control how and where the engine bytes are written (e.g., to a network socket, custom buffer, or in-memory stream).
- Builds a simple TensorRT network with two convolutional layers and ReLU activations.
- Implements a custom
StreamWriterclass inheriting fromtrt.IStreamWriterto collect serialized engine bytes. - Serializes the engine using
builder.build_serialized_network_to_stream()and writes the bytes to the custom stream. - Deserializes the engine from the collected bytes to verify correctness.
build.py: Main script containing the sample code.README.md: This document.
-
Install Requirements
Make sure you have the following Python packages installed:
tensorrtnumpypolygraphy
You can install Polygraphy via pip:
pip install polygraphyThe
tensorrtPython package is typically provided by NVIDIA as a wheel file. -
Run the Sample
python3 build.pyYou should see output indicating the network is constructed, the engine is built and serialized to the stream, and then deserialized successfully.
- IStreamWriter: An interface in TensorRT that allows you to define custom logic for writing serialized engine bytes. You must implement the
write(self, data)method. - build_serialized_network_to_stream: A method that serializes the network and writes the bytes to the provided
IStreamWriterinstance.
Constructing network...
[I] TF32 is disabled by default. Turn on TF32 for better performance with minor accuracy differences.
[I] Configuring with profiles:[
Profile 0:
{input [min=[1, 3, 224, 224], opt=[1, 3, 224, 224], max=[1, 3, 224, 224]]}
]
Building engine and serializing to stream...
The total bytes written to stream is 267836
Deserializing engine from stream...
Engine deserialized successfully
For terms and conditions for use, reproduction, and distribution, see the TensorRT Software License Agreement documentation.
September 2025 Initial release of this sample.
There are no known issues in this sample.