Export any PyTorch model to NNEF, a simple and explicit neural network exchange format, in a way that is auditable, debuggable, and stable across runtimes.
torch_to_nnef traces a vanilla nn.Module (any internal tensor type, quantized models included) and produces a portable NNEF archive. Unlike the protobuf used by ONNX, the NNEF graph is human-readable text with weights kept aside as separate files, so you can open it and review exactly which ops got serialized. tract, the inference engine developed openly by SONOS, is the primary supported target: it gets extended operator coverage and an optional check_io step that compares tract outputs against PyTorch at export time.
🚀 See it run: live browser demos (image classifier, pose estimation, voice activity detection, LLM) exported with
torch_to_nnefand running through tract compiled to WASM, no install required.
Quickstart
pip install torch_to_nnef # needs pip >= 23.2
import torch
from torch_to_nnef import export_model_to_nnef, TractNNEF
model = torch.nn.Linear(8, 4).eval()
example_input = torch.randn(1, 8)
export_model_to_nnef(
model=model,
args=example_input,
file_path_export="my_model.nnef.tgz",
# version pins the tract opset to target; check_io downloads that tract
# build once and asserts its output matches PyTorch on example_input.
inference_target=TractNNEF(version="0.23.0", check_io=True),
input_names=["input"],
output_names=["output"],
)
This writes a self-contained my_model.nnef.tgz; with check_io=True the call also raises if tract and PyTorch outputs diverge, so reaching the end means the export is validated. The getting started tutorial (linked below) walks through running the archive in tract from Python, Rust, and the CLI.
What's inside my_model.nnef.tgz
The archive keeps the graph and the weights apart:
graph.nnef weight.dat bias.dat
and graph.nnef is plain text you can read end to end (metadata header elided):
graph network(input) -> (output)
{
input = tract_core_external(shape = [1, 8], datum_type = 'f32');
weight = variable<scalar>(label = 'weight', shape = [4, 8]);
bias = variable<scalar>(label = 'bias', shape = [4]);
bias_aligned_rank_expanded = unsqueeze(bias, axes = [0]);
output = linear(input, weight, bias_aligned_rank_expanded);
}
What you can export
Any nn.Module whose forward takes and returns tensors can be exported out of the box, ordinary CNNs and transformers included. The tutorials below cover the cases that need more care:
- Dynamic axes : variable batch and streamable dimensions
- Large Language Models and quantized models
- Multimodal models (vision & audio): encoder + decoder as coordinated NNEF graphs
- NeMo ASR models
- Offloaded tensors, tensor-only / PEFT exports, and custom operators
Compatibility
PyTorch >= 1.10.0 on Linux and macOS, against the last two major tract releases (>= 0.20.22). Supported Python versions track those that are not end of life nor pre-release. Latest releases give the best operator coverage.
Learn more
The full documentation has step by step tutorials (from a first image classifier to LLM export), the export API reference, and the list of supported operators:
- Getting started : export, check, and run a real model end to end
- Live demos : exported models running in your browser via tract WASM
- Export API
- Supported operators
- Why NNEF?
Contributing & support
Contributions are welcome. If you hit a bug, please follow the Bug report instructions so it can be reproduced quickly.
Licensed under MIT OR Apache-2.0.
Metadata
Release files for torch_to_nnef 0.24.12
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| torch_to_nnef-0.24.12.tar.gz | 397.6 kB | Without provenance | Details for torch_to_nnef-0.24.12.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| torch_to_nnef-0.24.12-py3-none-any.whl | Python 3 | none | any | Details for torch_to_nnef-0.24.12-py3-none-any.whl |
Total release size: 913.8 kB
Release files / torch_to_nnef-0.24.12.tar.gz
| Download URL | torch_to_nnef-0.24.12.tar.gz |
|---|---|
| Size | 397.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d30ac55ced2b7483dd14891b082b3db20df89c9c03d3dfeb5274909ac83b9a6f
|
|
BLAKE2b-256 checksum How to use checksums |
aeb200f41537cb3c8b81c7e7fd487a4b343bd7353ff1630c1d857e7889255649
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.13.0 {"installer":{"name":"uv","version":"0.13.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / torch_to_nnef-0.24.12-py3-none-any.whl
| Download URL | torch_to_nnef-0.24.12-py3-none-any.whl |
|---|---|
| Size | 516.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8ba7f5a357b4bbeaec7d537a2357d5e5005cf531be0435f488b5d776a48f29b0
|
|
BLAKE2b-256 checksum How to use checksums |
9ed08323fbd1a9924a177727fce0c35ab6d48870a7756c041233f8c0c78bbff8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.13.0 {"installer":{"name":"uv","version":"0.13.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for torch_to_nnef 0.24.12
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.