emmtrix ONNX-to-C Code Generator
Project description
emmtrix ONNX-to-C Code Generator (emx-onnx-cgen)
emx-onnx-cgen compiles ONNX models to portable, deterministic C code for deeply embedded systems. The generated code is designed to run without dynamic memory allocation, operating systems, or external runtimes, making it suitable for safety-critical and resource-constrained targets.
Key characteristics:
- No dynamic memory allocation (
malloc,free, heap usage) - Static, compile-time known memory layout for parameters, activations, and temporaries
- Deterministic control flow (explicit loops, no hidden dispatch or callbacks)
- No OS or libc dependencies beyond basic C
- Single-threaded execution model
- Bitwise-stable code generation for reproducible builds
- Readable, auditable C code suitable for certification and code reviews
- Designed for bare-metal and RTOS-based systems
Goals
- Correctness-first compilation with outputs comparable to ONNX Runtime.
- Deterministic and reproducible C code generation.
- Clean, pass-based compiler architecture (import → normalize → optimize → lower → emit).
- Minimal C runtime with explicit, predictable data movement.
Non-goals
- Aggressive performance optimizations in generated C.
- Implicit runtime dependencies or dynamic loading.
- Training/backpropagation support.
Features
- CLI for ONNX-to-C compilation and verification.
- Deterministic codegen with explicit tensor shapes and loop nests.
- Minimal C runtime templates in
templates/. - ONNX Runtime comparison for end-to-end validation.
- Official ONNX operator coverage tracking.
- Support for a wide range of ONNX operators (see
OFFICIAL_ONNX_FILE_SUPPORT.md). - Supported data types:
float,double,float16int8_t,uint8_t,int16_t,uint16_t,int32_t,uint32_t,int64_t,uint64_tbool
- Supporting dynamic dimensions by utilizing C99 variable-length arrays (VLAs).
Installation
Install the package directly from PyPI (recommended):
pip install emx-onnx-cgen
Optional for verification and tests:
onnxruntimenumpy- A C compiler (
cc,gcc,clangor via--cc)
Quickstart
Compile an ONNX model into a C source file:
emx-onnx-cgen compile path/to/model.onnx build/model.c
Verify an ONNX model end-to-end against ONNX Runtime:
emx-onnx-cgen verify path/to/model.onnx
CLI Reference
emx-onnx-cgen provides two subcommands: compile and verify.
compile
emx-onnx-cgen compile <model.onnx> <output.c> [options]
Options:
--template-dir: Directory containing the C templates (default:templates).--model-name: Override the generated model name (default: output file stem).--emit-testbench: Emit a JSON-producingmain()testbench for validation.--emit-data-file: Emit constant data arrays into a companion_dataC file.--no-restrict-arrays: Disablerestrictqualifiers on generated array parameters.
verify
emx-onnx-cgen verify <model.onnx> [options]
Options:
--template-dir: Directory containing the C templates (default:templates).--model-name: Override the generated model name (default: model file stem).--cc: Explicit C compiler command for building the testbench binary.
Output
By default, the compiler emits a single C source file that includes:
- A generated entry point that mirrors the ONNX graph inputs/outputs.
- Tensor buffers for constants and temporaries.
- A lightweight runtime implemented via templates in
templates/.
When --emit-data-file is enabled, the main C source declares constant arrays
as extern, and a second file named like the output with a _data suffix
contains the constant definitions.
Official ONNX test coverage
See OFFICIAL_ONNX_FILE_SUPPORT.md for the generated support matrix.
Maintained by
This project is maintained by emmtrix.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file emx_onnx_cgen-0.2.0.tar.gz.
File metadata
- Download URL: emx_onnx_cgen-0.2.0.tar.gz
- Upload date:
- Size: 149.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3bc7fdb611440fd6905d5a6e4d80e684da917b778d77cde5972465b945f4dcf6
|
|
| MD5 |
d93febe93f671785d9eba91fd4257fdd
|
|
| BLAKE2b-256 |
839b76bcfb4ed8a6d01231346b59ba097e9986baad5225cb05158b2da4533ec4
|
Provenance
The following attestation bundles were made for emx_onnx_cgen-0.2.0.tar.gz:
Publisher:
release.yml on emmtrix/emx-onnx-cgen
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
emx_onnx_cgen-0.2.0.tar.gz -
Subject digest:
3bc7fdb611440fd6905d5a6e4d80e684da917b778d77cde5972465b945f4dcf6 - Sigstore transparency entry: 829385427
- Sigstore integration time:
-
Permalink:
emmtrix/emx-onnx-cgen@0386725513b8cecf5d7b93e61a6a373ccb9e2ede -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/emmtrix
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@0386725513b8cecf5d7b93e61a6a373ccb9e2ede -
Trigger Event:
release
-
Statement type:
File details
Details for the file emx_onnx_cgen-0.2.0-py3-none-any.whl.
File metadata
- Download URL: emx_onnx_cgen-0.2.0-py3-none-any.whl
- Upload date:
- Size: 160.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
69e8d3191e5ff85235f192a58e58915394fb221daf0b3b700ffb600adf66f139
|
|
| MD5 |
425ec0d46abb796d9f847c52c9b2a3ae
|
|
| BLAKE2b-256 |
0353e661a52fa2e31823a7b360acde12a6cd4b30aabdfa8e60d5a08190eeddb5
|
Provenance
The following attestation bundles were made for emx_onnx_cgen-0.2.0-py3-none-any.whl:
Publisher:
release.yml on emmtrix/emx-onnx-cgen
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
emx_onnx_cgen-0.2.0-py3-none-any.whl -
Subject digest:
69e8d3191e5ff85235f192a58e58915394fb221daf0b3b700ffb600adf66f139 - Sigstore transparency entry: 829385430
- Sigstore integration time:
-
Permalink:
emmtrix/emx-onnx-cgen@0386725513b8cecf5d7b93e61a6a373ccb9e2ede -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/emmtrix
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@0386725513b8cecf5d7b93e61a6a373ccb9e2ede -
Trigger Event:
release
-
Statement type: