Micro-Infer is a zero-dependency, high-performance Neural Language Model runtime built entirely from scratch in C++20. It demonstrates how to execute a deep learning Multi-Layer Perceptron (MLP) model for character-level text generation without relying on heavy external libraries like PyTorch, Eigen, or BLAS.
The project is split into two distinct phases:
- Python Training Environment (
scripts/): We use PyTorch to train a tiny neural network (MLP) on the Shakespeare dataset. Once trained, a custom script (export_weights.py) dumps the raw floating-point weights into a custom binary format (model.bin). - C++ Inference Engine (
src/,include/): The core of the project. It uses OS-level memory mapping (Win32CreateFileMapping) to map themodel.bindirectly into memory instantly. It then runs a custom-built, multi-threaded Matrix Multiplication (GEMM) engine to generate text token-by-token.
Follow these steps to build, train, and run the inference engine from the very beginning.
You need a C++20 compiler (like MinGW GCC) and CMake installed.
- Open your terminal in the
d:\micro_inferdirectory. - Generate the build files and compile the executable in strict Release mode for maximum optimization:
This creates standalone executables in the
# Remove old unoptimized build rm -rf build # Configure CMake specifically for MinGW in Release mode cmake -B build -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=Release # Build the project using all your CPU cores (e.g., 16) cmake --build build -j 16
build/bin/folder.
We have already provided a pre-trained model in the pretrained/ folder, so you can skip this step if you just want to run the engine. However, if you want to train it yourself from scratch:
- Ensure you have Python installed with PyTorch.
- Run the training script (it will automatically download the Shakespeare dataset):
python scripts/train_mlp.py
- Export the trained PyTorch weights into our custom C++ binary format:
python scripts/export_weights.py
Now run the main executable to generate text!
-
In your terminal, run the compiled binary:
.\build\bin\micro_infer.exe
-
Customizing the Output: You can change the starting prompt, the length of the text, and the "creativity" (temperature) using command-line arguments:
.\build\bin\micro_infer.exe --prompt "HAMLET: " --tokens 1000 --temp 0.8
--prompt: The starting text to feed the model.*--tokens: How many characters to generate.*--temp: Lower numbers (0.2) make it repetitive and strict, higher numbers (1.2) make it chaotic.*
-
Hitting Maximum Speed (50K+ tok/s): When running interactively in Windows terminals (like VS Code), the C++ engine runs so fast that it gets bottlenecked waiting for the terminal UI to draw the characters on screen. To see the true hardware speed of the engine, redirect the output to a text file to bypass the UI rendering delay:
.\build\bin\micro_infer.exe > output.txt
(Open
output.txtto read the generated text and see the peak tokens/sec)
We built a comprehensive test suite to verify the custom math engine. You can run these to ensure your CPU is computing the matrix math correctly:
# Test 1: Checks zero-copy memory mapping and file parsing
.\build\bin\test_tensor.exe
# Test 2: Checks the math engine (Matrix Multiplication, Softmax, Tanh)
.\build\bin\test_ops.exe
# Test 3: Benchmarks the multi-threaded speed vs a naive loop
.\build\bin\test_bench.exe- Zero-Copy Loading: The
model.binis never copied into RAM usingmallocormemcpy. It uses Windows virtual memory mapping, meaning loading is instantaneous and uses 0 extra bytes of RAM. - Cache-Tiled GEMM: Matrix multiplication is broken down into 32x32 tiles so that the data stays entirely inside the CPU's ultra-fast L1 cache, preventing memory bottlenecks.
- Parallel Multithreading: Uses C++20
std::jthreadto split the matrix rows across all available CPU cores (e.g., 16 cores). - Standalone Binary: The
.exeis statically linked. You can copy it to any Windows machine, put it next tomodel.binandvocab.json, and it will run instantly without needing any installations.