Export configuration#
This guide covers export backend selection and advanced export options. For dry-run and non-traced debugging modes, see Debugging.
Backend names and aliases#
LEAPP supports these public backend names:
|
Actual backend |
Output |
|---|---|---|
|
|
TorchScript |
|
|
TorchScript |
|
|
TorchScript |
|
|
ONNX |
|
|
ONNX |
|
|
ONNX |
|
|
|
|
|
|
|
|
No compilation |
TorchScript export#
"jit" and "jit-script" select the TorchScript scripting backend.
"jit-trace" selects the tracing backend.
import torch
import leapp
from leapp import annotate
leapp.start("torchscript_example")
x = annotate.input_tensors("normalize", {"x": torch.randn(16)})
y = torch.relu((x - x.mean()) / (x.std() + 1e-6))
annotate.output_tensors("normalize", {"y": y}, export_with="jit")
leapp.stop()
leapp.compile_graph(validate=True)
ExportedProgram export#
"exported-program" exports each node with torch.export.export()
and saves a .pt2 artifact via torch.export.save(). The YAML
backend field is written as pt2.
"pt2" is a shorthand alias for "exported-program", similar to how
"onnx" aliases "onnx-dynamo".
Use this backend when you want the modern torch.export graph
representation rather than TorchScript or ONNX. It is a good fit for
feedforward PyTorch graphs that already compile cleanly through LEAPP’s
FX capture path.
import torch
import leapp
from leapp import annotate
leapp.start("exported_program_example")
x = annotate.input_tensors("policy", {"obs": torch.randn(1, 32)})
action = torch.tanh(x[..., :12])
annotate.output_tensors(
"policy",
{"action": action},
export_with="exported-program",
)
leapp.stop()
leapp.compile_graph(validate=True)
ExportedProgram backend parameters#
All ExportedProgram backend parameters are passed through
backend_params.
Parameter |
Default |
Description |
|---|---|---|
|
PyTorch default |
Passed through to |
Example:
annotate.output_tensors(
"policy",
{"action": action},
export_with="pt2",
backend_params={
"strict": True,
},
)
ONNX export#
LEAPP exposes two ONNX backends:
onnx-dynamois the default behindexport_with="onnx"onnx-torchscriptis the TorchScript-based ONNX path
Use onnx-dynamo for typical feedforward models. Use
onnx-torchscript when the dynamo path produces unstable graphs or when
exporting recurrent models (e.g. nn.GRU, nn.LSTM). See
examples/stateful_gru_export.py for a complete example.
import torch
import leapp
from leapp import annotate
leapp.start("onnx_example")
x = annotate.input_tensors("policy", {"obs": torch.randn(1, 32)})
action = torch.tanh(x[..., :12])
annotate.output_tensors("policy", {"action": action}, export_with="onnx")
leapp.stop()
leapp.compile_graph(validate=True)
ONNX backend parameters#
All ONNX backend parameters are passed through backend_params.
Parameter |
Default |
Used by |
Description |
|---|---|---|---|
|
PyTorch default |
both ONNX backends |
Override the ONNX opset version |
|
|
both ONNX backends |
Emit exporter diagnostics |
|
|
|
Enable exporter-side verification |
|
|
|
Enable dynamo ONNX optimization |
|
|
|
Script the module before ONNX export |
|
|
both at save time |
Skip |
Example:
annotate.output_tensors(
"policy",
{"action": action},
export_with="onnx-dynamo",
backend_params={
"opset_version": 17,
"verify": False,
"optimize": True,
"report": True,
},
)
Validation guidance#
Exporter-side options like verify are optional and backend-specific.
The main LEAPP validation path is still:
leapp.compile_graph(validate=True, rtol=1e-3, atol=1e-5, strict=True)
That validation compares exported model outputs against the captured traced outputs. See Debugging for validation details and LEAPP Runtime for Python runtime usage.
Bringing your own model#
Set export_with=None when you want a node to appear in the graph
without asking LEAPP to compile it. This is useful for:
prebuilt
.pt,.pt2, or.onnxartifactsplaceholder nodes that will be filled in later
flows where LEAPP should capture I/O and graph edges but not produce a model
import torch
import leapp
from leapp import annotate
leapp.start("precompiled_example")
x = annotate.input_tensors("precompiled_inference",
{"input_data": torch.randn(1, 10)})
predictions = x # representative traced output shape
annotate.output_tensors(
"precompiled_inference",
{"predictions": predictions},
export_with=None,
backend_params={
"model_path": "/models/my_optimized_model.pt",
"copy_original_model": True,
},
)
leapp.stop()
leapp.compile_graph()
None backend parameters#
model_path(optional): Path to an existing model artifact.copy_original_model(optional, defaultFalse): Copy the provided artifact into the LEAPP output directory.
Important behavior:
LEAPP still records input/output shapes and graph connectivity from the traced example tensors.
If
model_pathis provided, LEAPP verifies the file and stores checksums in the YAML.The model path written into the YAML is made relative to the YAML directory when possible.
InferenceManagercurrently runs referenced.pt,.pt2, and.onnxartifacts.