Quickstart#
Embedl Deploy keeps deployment preparation inside PyTorch.
transform() rewrites your model’s graph into a deployable form
using the active backend’s patterns. If a single backend is installed
it is selected automatically; if several are present you must call
set_backend() first or transform() will raise an error.
Usage#
Exporting needs onnx and onnxscript installed – torch.onnx.export
imports onnxscript for the torch.export-based exporter it defaults to
from torch 2.9, and raises ModuleNotFoundError: onnxscript without it –
and onnx-simplifier to get the result through TensorRT:
pip install "onnx>=1.17" "onnxscript>=0.6.0" "onnx-simplifier>=0.4.36"
That exporter writes the weights to model.onnx.data beside model.onnx,
and the two files must stay together until onnxsim inlines the weights.
import torch
from embedl_deploy import transform
from embedl_deploy.quantize import (
convert_float_to_float16,
prepare_export,
prepare_qat,
quantize,
)
from torchvision.models import resnet18
# 1. Load a standard PyTorch model
model = resnet18().eval()
example_input = torch.randn(1, 3, 224, 224)
# 2. Transform — fuse and optimize for TensorRT in one call
res = transform(model, (example_input,))
print("Model")
res.model.print_readable()
print("Matches", "\n".join([str(match) for match in res.matches]))
# 3. Quantize (PTQ)
def forward_loop(model: torch.fx.GraphModule):
model.eval()
for _ in range(100):
model(torch.randn(1, 3, 224, 224))
quantized_model = quantize(
res.model,
(example_input,),
forward_loop=forward_loop,
freeze_weights=True,
)
quantized_model.eval()
# 4. Convert what the quantizer left unquantized to float16, so the strongly
# typed engine runs it at half rather than float32
half_model = convert_float_to_float16(quantized_model)
# 5. Export. The float16 path needs the torch.export-based exporter,
# which is the default from torch 2.9
torch.onnx.export(half_model, (example_input.half(),), "model.onnx")
Or fine-tune with quantization-aware training instead of PTQ, starting from a freshly transformed model:
res = transform(model, (example_input,))
# Calibrate the activation ranges, but leave the weight scales dynamic
# so they track the weights as training changes them
qat_model = quantize(
res.model,
(example_input,),
forward_loop=forward_loop,
freeze_weights=False,
)
prepare_qat(qat_model)
for epoch in range(num_epochs):
train_one_epoch(qat_model)
prepare_export(qat_model)
qat_half = convert_float_to_float16(qat_model)
torch.onnx.export(qat_half, (example_input.half(),), "model_qat.onnx")
Compile#
Compilation can be done with TensorRT’s trtexec tool, which can take the ONNX model and compile it for inference. The exported layer info and profile can be used for debugging, optimization and visualization.
The exported model contains explicit QDQ nodes, so compile it as a strongly
typed network. This preserves the quantized and floating-point types encoded
in ONNX. Do not combine --stronglyTyped with precision-selection flags
such as --fp16, --int8, or --best. TensorRT 11 enables strong
typing automatically and removes those precision-selection flags.
A strongly typed engine selects no precision of its own, so the regions the
quantizer left unquantized run at whatever precision the ONNX declares.
Exporting through convert_float_to_float16 as above is what makes those
regions float16 rather than float32.
The export might need simplifying with onnx-simplifier to make trtexec compile it.
onnxsim model.onnx model.onnx
/usr/src/tensorrt/bin/trtexec --onnx=model.onnx --stronglyTyped --useCudaGraph
See the quantization guide for TensorRT version differences and validation guidance.
Optionally you can get the layer profile with the following flags:
--exportLayerInfo=layer_info.json
--exportProfile=profile.json
--profilingVerbosity=detailed
Next steps#
Quantization guide — mixed precision, SmoothQuant, QAT
Embedl Hub tutorial — model tracking and visualization