Skip to main content

Any PyTorch Model to NNEF file format.

Project description

Torch to NNEF

core CI LLM CI NeMo CI pythondocumentation MIT/Apache 2

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_nnef and 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:

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:

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

torch_to_nnef-0.24.3.tar.gz (338.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

torch_to_nnef-0.24.3-py3-none-any.whl (445.8 kB view details)

Uploaded Python 3

File details

Details for the file torch_to_nnef-0.24.3.tar.gz.

File metadata

  • Download URL: torch_to_nnef-0.24.3.tar.gz
  • Upload date:
  • Size: 338.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","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}

File hashes

Hashes for torch_to_nnef-0.24.3.tar.gz
Algorithm Hash digest
SHA256 f0a5f51f4eb203d86e64b47ef89a7ecfe4ac4763f6a05a07b704cf9f17602a62
MD5 c6c6b0eb3936b50340b92d5a78584f09
BLAKE2b-256 6c48fc54c01a8873364b2d4489fe02b03031cc3c724b41904f185f5ae348f1ff

See more details on using hashes here.

File details

Details for the file torch_to_nnef-0.24.3-py3-none-any.whl.

File metadata

  • Download URL: torch_to_nnef-0.24.3-py3-none-any.whl
  • Upload date:
  • Size: 445.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","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}

File hashes

Hashes for torch_to_nnef-0.24.3-py3-none-any.whl
Algorithm Hash digest
SHA256 4a6242ee6fc2cebf39d4a253a7812849f0fb5b9b5f6024ecba5bd7555cd176b0
MD5 c9a7c1e2fce24d091350b1f4de1848f2
BLAKE2b-256 ddbdf6c566b2caa1a5d3e28edb0fce8004fc55a1f2383b20e038099198df118f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page