Sub topic 15
Dynamic Batch Sizes and Optimization Profiles
Prerequisites
This chapter assumes that you know basic Python, NumPy, PyTorch, and image-model inference. You should also understand that a batch is a group of images processed together. The examples use an NVIDIA GPU, TensorRT, and an ONNX model with an input named input.
Why dynamic batch sizes matter
A fixed-batch model accepts only one batch size. For example, a model exported with an input shape of 1x3x224x224 accepts one image at a time. A model exported with a dynamic batch dimension can accept shapes such as 1x3x224x224, 8x3x224x224, and 32x3x224x224.
Dynamic batches help when traffic changes. A request with one image can receive a low-latency response, while several queued images can be grouped for better GPU utilization.
Latency is the time needed for one inference request. Throughput is the number of images processed per unit of time. Larger batches often improve throughput but increase latency for requests waiting in the batch.
| Goal | Useful batch strategy |
|---|---|
| Interactive response | Use a small batch, often 1 |
| Maximum throughput | Use a larger batch |
| Variable traffic | Define a supported batch range |
Export a dynamic input
During ONNX export, mark axis zero as dynamic for both the input and output. The symbolic name batch documents the purpose of that dimension.
import torch
import torchvision
model = torchvision.models.resnet50(
weights=torchvision.models.ResNet50_Weights.DEFAULT
).eval()
dummy = torch.randn(1, 3, 224, 224)
torch.onnx.export(
model,
dummy,
"resnet50.onnx",
input_names=["input"],
output_names=["logits"],
dynamic_axes={
"input": {0: "batch"},
"logits": {0: "batch"},
},
opset_version=17,
)Dynamic ONNX dimensions do not mean that every possible shape is automatically efficient. TensorRT still needs to know which shapes it should optimize.
Optimization profiles in TensorRT
An optimization profile describes three shapes:
min: the smallest shape the engine accepts.opt: the shape TensorRT tunes most carefully.max: the largest shape the engine accepts.
For a fixed image size and a batch range from 1 to 32, the profile can use batch 8 as the common case:
trtexec --onnx=resnet50.onnx --saveEngine=resnet50.plan --fp16 \
--minShapes=input:1x3x224x224 \
--optShapes=input:8x3x224x224 \
--maxShapes=input:32x3x224x224The runtime shape must be inside the profile range. A request with batch 16 is valid. A request with batch 64 is not valid for this engine.
The opt shape is a performance target, not a restriction. TensorRT can run other shapes in the range, but performance may vary. If your application has distinct traffic patterns, use profiles that represent those patterns when your TensorRT integration supports multiple profiles.
Validate the supported range
Test representative shapes after building the engine. Check batch 1 for interactive use, the preferred batch, and the largest expected batch. Also verify that preprocessing and postprocessing preserve the same batch order.
Exercise
An engine uses min batch 1, opt batch 8, and max batch 32. Is batch 16 valid, and is batch 64 valid?
Show answer
Batch 16 is valid because it lies within the profile range. Batch 64 is invalid because it exceeds the maximum batch size.