The replacement for TorchScript
torch.export is PyTorch's modern model export system. It captures your model into a clean, standardized graph representation that can be serialized, deployed, quantized, or lowered to other backends (ONNX, ExecuTorch, CoreML). It replaces the older TorchScript (torch.jit.trace / torch.jit.script) for new projects.
Why a new export system
TorchScript was hard. jit.trace couldn't see Python control flow. jit.script required a restricted Python subset. The error messages were notoriously cryptic. torch.export uses the same dynamo-based graph capture as torch.compile, which means much better coverage and far cleaner errors.
The contract
You give torch.export.export a model and example inputs. It runs the model with those inputs, captures the resulting graph, and returns an ExportedProgram. The exported graph is fully serializable — save it to disk and load it without needing the original Python class definition.
What torch.export buys you
- Backend portability — the same exported program lowers to ONNX, ExecuTorch (mobile), CoreML, TensorRT.
- Quantization — modern quant techniques in torchao operate on exported programs.
- Optimization — graph passes (constant folding, dead code elimination) operate on the exported representation.
- Versioning — the export format is meant to be stable across PyTorch versions.
TorchScript — when (rarely) you'd still reach for it
For deploying to environments where torch.export's runtime isn't yet available (some legacy embedded paths). For new projects, default to torch.export.