Skip to main content

Neural Network to C Compiler

Project description

nncc - Neural Network to C Compiler

A minimal, dependency-free compiler that converts PyTorch models into portable C99 code for inference.

Features

  • Python-First Workflow: Compile directly from your Python training script using nncc.compile().
  • Zero-Dependency Inference: Generated C code only requires <math.h> and <stdlib.h>.
  • Embedded Weights: Weights are embedded directly into the generated C header/source, making it a single-file solution.
  • Dynamic & Portable: Supports arbitrary model architectures and runs on any C99-compliant compiler (GCC, Clang, MSVC).

Installation

pip install nncc

Quick Start

1. Compile directly from PyTorch

import torch
import torch.nn as nn
import nncc

# 1. Define your model
model = nn.Sequential(
    nn.Linear(10, 64),
    nn.ReLU(),
    nn.Linear(64, 2),
    nn.Softmax(dim=-1)
)

# 2. Compile to C source code
# We pass a sample input to infer shapes
c_code = nncc.compile(model, torch.randn(1, 10), name="my_model")

# 3. Save to file
with open("model.c", "w") as f:
    f.write(c_code)

print("Saved model.c!")

2. Use the generated C code

The generated file is a single-header library. You can include it directly in your C application.

// main.c
#define MODEL_IMPLEMENTATION // Define this in exactly one .c file to implement functions
#include "model.c"

#include <stdio.h>

int main() {
    // Input matches the shape provided during compilation (1, 10)
    float input[10] = {0.5f, -0.2f, /* ... */};
    float output[2];
    
    // Run inference
    my_model_inference(input, output);
    
    printf("Class 0: %f\n", output[0]);
    printf("Class 1: %f\n", output[1]);
    
    return 0;
}

Compile it with standard tools:

gcc -O3 main.c -o app -lm

Supported Layers

Category Operations
Linear Linear (GEMM)
Conv Conv2d
Pool MaxPool2d, AvgPool2d, AdaptiveAvgPool2d (Global)
Act ReLU, ReLU6,, Sigmoid, Tanh, Softmax
Norm BatchNorm2d
Shape Flatten, Dropout (No-op)

Advanced / Manual Usage

CLI Tool

If you prefer the command-line interface or want to work with raw files:

  1. Export Model: Use scripts/export_model.py to generate .safetensors and .nnmodel files.
  2. Compile:
    nncc model.safetensors model.nnmodel -o model.c
    

Building from Source

To build the nncc compiler and shared library from source:

Windows (MinGW/Git Bash):

make

Windows (MSVC):

cl /O2 /Fe:nncc.exe src/main.c src/model.c src/loader.c src/codegen.c

Linux/macOS:

make

Project Structure

nncc/
├── nncc/              # Python package
│   ├── __init__.py
│   ├── compiler.py    # Python API & Ctypes bindings
│   └── nncc.dll       # Pre-compiled core
├── src/               # C Compiler Core
│   ├── libnncc.c      # Shared library entry point
│   ├── main.c         # CLI entry point
│   ├── model.c        # Graph IR
│   └── codegen.c      # C99 Generator
├── examples/          # Usage examples
└── setup.py           # Packaging config

License

MIT License

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

nncc-2.0.2.tar.gz (85.0 kB view details)

Uploaded Source

Built Distribution

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

nncc-2.0.2-py3-none-any.whl (60.3 kB view details)

Uploaded Python 3

File details

Details for the file nncc-2.0.2.tar.gz.

File metadata

  • Download URL: nncc-2.0.2.tar.gz
  • Upload date:
  • Size: 85.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for nncc-2.0.2.tar.gz
Algorithm Hash digest
SHA256 32b8ff5aa3fdf4f8584fb0c941c1259f5d94f965727082d25a49d17087455bb3
MD5 11fb2dd65f30cd73a8f7bab07d01aa8f
BLAKE2b-256 39bbea05cfdd90322fc34a0cb56c251ec7fd83308e0455b5e8495d2f0ade970f

See more details on using hashes here.

File details

Details for the file nncc-2.0.2-py3-none-any.whl.

File metadata

  • Download URL: nncc-2.0.2-py3-none-any.whl
  • Upload date:
  • Size: 60.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for nncc-2.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 5f5b2cbc93df2eb9471dab705dca044695085c06a8f17cf32cc30c4c026bfdbf
MD5 7a1fa3e772ad7826e07cd41fd0a36305
BLAKE2b-256 7ab21181fc7584c7d05e47c7cfa475c79530f6a5a40cfe2ca28e7df1224a73a0

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