High-performance C-based Neural Network Library with Python Integration
FRAMEWORK-C is a lightweight, ISO C99 neural network library designed for seamless integration into Python workflows via the C API. It provides core primitives for building, training, and evaluating multi-layer perceptrons, while allowing you to choose between system BLAS or a portable fallback for matrix operations.
Key benefits:
- Pure C implementation for forward/backward propagation, gradient updates, and evaluation
- Flexible BLAS support: Accelerate (macOS), OpenBLAS/System BLAS, or built-in portable fallback (ELAS)
- Zero-copy NumPy bridge: expose
NNbuild,NNtrain,NNinfer,NNload, andNNsaveto Python - High performance: benchmarks show competitive inference & training times vs. PyTorch/SKlearn
- Modular design: easily swap kernels, tune tile sizes, or integrate custom optimized routines
-
Neural Network Primitives
- Build arbitrary MLP architectures: customizable input, hidden, and output layer sizes
- Fast & precise sigmoid activation variants
- SGD training loop with mean squared error (MSE) loss and accuracy metrics
-
Matrix Operations
- Level-1 & Level-2 routines: dot, saxpy, copy, scale
- GEMM via
cblas_sgemm(system BLAS) orelas_gemmfallback - Optional OpenMP parallelization for multi-core acceleration
-
Python Extension
- Wraps core C functions in a Python module for easy scripting
- Accepts NumPy arrays without extra data copies
-
Benchmark Suite
- Demo script on the Semeion digits dataset (2.9 MB)
- Reports loss, accuracy, and inference/training timing comparisons
-
C Compiler with C99 support (GCC, Clang, MSVC)
-
Python ≥ 3.8 (tested up to 3.13)
-
NumPy
-
BLAS Library (one of):
- Apple Accelerate (macOS)
- OpenBLAS / system BLAS (Linux/Windows)
- or use the bundled portable fallback (ELAS) by defining
ELAS_LOCAL
-
Optional: OpenMP flags (
-fopenmpor/openmp) to enable threading
-
Clone the repository
git clone https://github.com/<your-username>/framework-c.git cd framework-c
-
Create & activate a virtual environment
python -m venv venv source venv/bin/activate # Windows: venv\\Scripts\\activate
-
Install Python dependencies
pip install --upgrade pip setuptools wheel numpy
-
(Optional) Force portable fallback In
setup.py, add-DELAS_LOCALtoCFLAGSor set environment variable:export CFLAGS="-DELAS_LOCAL"
Under your activated virtual environment, execute:
source venv/bin/activate # Activate virtualenv
python setup.py clean --all # Remove previous build artifacts
python setup.py build_ext --inplace # Compile the C extension in-place
python benchmark.py # Run training & inference benchmarks
deactivate # Exit virtualenvclean --all: Cleans upbuild/,dist/, and shared libraries (*.so/.pyd).build_ext --inplace: Compiles and places the extension module alongside Python files for import.benchmark.py: Trains a 2-layer MLP on the Semeion dataset, printing loss, accuracy, and timing vs. PyTorch/SKlearn.
framework-c/
│
├── src/ # Core C implementation
│ ├── data_split.h
│ ├── data_split.c
│ ├── elas.c # Portable BLAS fallback (ELAS)
│ ├── elas.c
│ ├── model_selection.c
│ ├── model_selection.c
│ ├── my_module.c
│ ├── nn.c
│ ├── nn.c
│ ├── utils.c
│ └── utils.c
├── benchmark_mnist.py
├── benchmark_semeion.py
├── setup.py # Build script (setuptools Extension)
├── README.md # Project documentation
└── LICENSE # MIT License
Sample (100-run average) on x86_64 single-core:
| Metric | FRAMEWORK-C | SKlearn | PyTorch |
|---|---|---|---|
| Test Accuracy | 92.16% | 88.40% | 91.00% |
| Single Inference | 1.8 ms | 7.3 ms | 5.2 ms |
| Batch Inference | 0.036 ms/sample | 0.05 ms/sample | 0.04 ms/sample |
(Results vary by hardware & BLAS choice.)
- Tile sizes: Adjust default tiles in
elas.hfor L1/L2 cache fitting. - Threading: Compile with
-fopenmpor/openmpto enable OpenMP in BLAS routines. - Activations: Swap
fast_sigmoidinnn.cfor standardsigmoidfif needed. - Hyperparameters: Modify
benchmark.py(learning rate, batch size, epochs) for your dataset.
- Fork this repository
- Create a feature branch:
git checkout -b feature/your-feature - Implement changes & add tests/examples
- Submit a pull request with a clear description
Please follow the existing C coding style and include Doxygen comments for new functions.
This project is licensed under the MIT License. See LICENSE for details.