export
shrinkai.export
Deployment export: get a model out of the PyTorch Python process.
export_onnx and export_torchscript work on any nn.Module,
one already pruned/quantized via shrinkai.compression, or a distilled
trained student (also reachable directly as Distiller.export_onnx/
Distiller.export_torchscript).
Modules:
| Name | Description |
|---|---|
exporter |
Deployment export utilities: get a trained/compressed model out of the PyTorch |
Functions:
| Name | Description |
|---|---|
export_onnx |
Exports a model to ONNX, for inference on ONNX Runtime or other non-PyTorch |
export_torchscript |
Exports a model to TorchScript, for deployment outside a Python process |
Functions:
export_onnx
export_onnx(
model: Module,
sample_input: Tensor | tuple[Tensor, ...],
path: str | Path,
input_names: list[str] | None = None,
output_names: list[str] | None = None,
dynamic_batch: bool = True,
opset_version: int = 17,
) -> Path
Exports a model to ONNX, for inference on ONNX Runtime or other non-PyTorch engines (many mobile/edge inference stacks consume ONNX).
Note
Uses PyTorch's legacy TorchScript-based exporter (dynamo=False) rather than
the newer torch.export-based one, since the latter additionally requires the
onnxscript package. As of PyTorch 2.9+, this legacy exporter is itself
deprecated upstream (a DeprecationWarning is expected) in favor of the
torch.export, it still works correctly today, but migrating this
function once onnxscript is a lighter/more stable dependency is a known
next step.
Requires the optional onnx package: pip install shrinkai[export].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
The model to export. Switched to eval mode internally. |
required |
sample_input
|
Tensor | tuple[Tensor, ...]
|
A representative input tensor (or tuple of tensors, for multi-input models) used to trace the model's operations. |
required |
path
|
str | Path
|
Destination |
required |
input_names
|
list[str] | None
|
Optional names for the graph's input nodes. If omitted while
|
None
|
output_names
|
list[str] | None
|
Optional names for the graph's output nodes. |
None
|
dynamic_batch
|
bool
|
If True (default), dimension 0 of every input is marked
dynamic, so the exported graph accepts any batch size at inference
time (PyTorch's shape inference propagates this to the outputs too).
If False, the graph is frozen to |
True
|
opset_version
|
int
|
Target ONNX opset version. Defaults to 17. |
17
|
Returns:
| Name | Type | Description |
|---|---|---|
Path |
Path
|
The path the model was exported to. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If the optional |
Source code in src/shrinkai/export/exporter.py
export_torchscript
export_torchscript(
model: Module,
path: str | Path,
sample_input: Tensor | tuple[Tensor, ...] | None = None,
method: Literal["trace", "script"] = "trace",
) -> Path
Exports a model to TorchScript, for deployment outside a Python process (the LibTorch C++ runtime, PyTorch Mobile, ...).
Note
torch.jit.trace/torch.jit.script are flagged as deprecated upstream
in favor of torch.export (a DeprecationWarning is expected). They are
used here anyway because TorchScript .pt files remain, as of this
writing, the format most consistently supported by LibTorch C++ and
PyTorch Mobile in practice; migrating to torch.export once its own
deployment story (C++/mobile loading) is verified as a solid replacement
is being followed-up.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
The model to export. Switched to eval mode internally. |
required |
path
|
str | Path
|
Destination file path (conventionally |
required |
sample_input
|
Tensor | tuple[Tensor, ...] | None
|
Required when |
None
|
method
|
Literal['trace', 'script']
|
"trace" (default) records the actual tensor operations executed
for |
'trace'
|
Returns:
| Name | Type | Description |
|---|---|---|
Path |
Path
|
The path the model was exported to. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |