A compiler pipeline that converts ML models to optimized executables. The project includes both a basic compiler and a complete pipeline with optimization passes.
simple_ml_compiler/
├── basic_compiler/
│ └── ml_compiler.py # Basic implementation of model compilation
├── code_generator.py # Generates C++ implementation from IR
├── compiler_driver.py # Main compiler driver
├── converter.py # Converts PyTorch models to IR
├── kernels/
│ ├── kernels.cpp # Kernel implementations
│ └── kernels.hpp # Kernel interface declarations
├── model.py # MNIST model definition
├── optimizer.py # IR optimization passes
└── verifier.py # Verifies compilation correctness
The compilation process consists of several stages:
-
Model Definition (
model.py)- Defines PyTorch MNIST model
- Exports model to TorchScript format
-
Model Conversion (
converter.py)- Converts TorchScript model to IR
- Extracts weights and network structure
-
IR Optimization (
optimizer.py)- Applies optimization passes:
- Operator fusion (Conv+ReLU, Linear+ReLU)
- Dead code elimination
- Memory layout optimization
- Applies optimization passes:
-
Code Generation (
code_generator.py)- Generates C++ implementation
- Manages memory layout
- Creates weight definitions
-
Compilation (
compiler_driver.py)- Compiles generated code to binary
- Links with kernel implementations
- Handles build configurations
-
Verification (
verifier.py)- Verifies correctness of compilation
- Compares original and compiled model outputs
- Setup Environment
# Requirements
- Python 3.8+
- PyTorch
- NumPy
- G++ compiler with C++17 support
- OpenMP- Export Model
python model.py
# Creates mnist_cnn.pt- Convert and Optimize
# Convert to IR
python converter.py mnist_cnn.pt mnist_cnn_ir
# Optimize IR
python optimizer.py mnist_cnn_ir.graph.json mnist_cnn_ir.opt.json- Compile
python compiler_driver.py mnist_cnn_ir.opt.json mnist_cnn_ir.weights.bin mnist_model- Test
import numpy as np
# Create test input
test_input = np.random.randn(28, 28).astype(np.float32)
test_input.tofile('test_input.bin')
# Run model
!./mnist_model test_input.bin output.bin
# Check output
output = np.fromfile('output.bin', dtype=np.float32)
print("Predicted digit:", output.argmax())- Verify (Optional)
python verifier.py mnist_cnn_ir.graph.json mnist_cnn_ir.opt.jsonA simplified version is available in basic_compiler/ml_compiler.py. This version:
- Directly converts PyTorch model to C++
- No optimization passes
- Single file implementation
- MNIST-specific
Usage:
python basic_compiler/ml_compiler.py mnist_cnn.pt mnist_model- Add kernel declaration in
kernels/kernels.hpp:
void new_op(const float* input, float* output, ...);- Implement kernel in
kernels/kernels.cpp:
void new_op(const float* input, float* output, ...) {
// Implementation
}- Add IR node handling in
converter.py:
def convert_new_op(self, node):
# Convert op to IR- Add optimization handling in
optimizer.py:
def optimize_new_op(self, node):
# Apply optimizations- Add code generation in
code_generator.py:
def _gen_new_op(self, node: Dict) -> str:
# Generate C++ codeCompiler supports various build configurations:
# Debug build
python compiler_driver.py --debug input.json weights.bin output
# Custom kernel directory
python compiler_driver.py --kernel-dir ./my_kernels input.json weights.bin output
# Keep build files
python compiler_driver.py --keep-build input.json weights.bin output- Support for more model formats (ONNX, TFLite)
- Additional optimization passes
- Auto-tuning for kernel parameters
- GPU/accelerator support
- Quantization support
- More comprehensive verification tools
- Create an issue describing the feature/bug
- Fork the repository
- Create a feature branch
- Submit a pull request
MIT License