mlp

Modular MLP in C++

CI Release License C++17 OpenMP Optional CUDA Optional Contributions Welcome Issues Welcome

An academic-focused, from-scratch multilayer perceptron (MLP) project in modern C++ with both:

Current scope:

Quick Start (CLI)

Requirements:

Build and run:

cmake -S . -B build
cmake --build build
./build/mlp

The CLI trains on a train split and reports loss and binary metrics on train, validation, and test.

Main Options

Common CLI options:

./build/mlp --optimizer sgd|momentum|nag|adam|adamw|nadam|rmsprop|adagrad|adadelta|lion
./build/mlp --hidden 16,16,8
./build/mlp --epochs 3000 --lr 0.01
./build/mlp --samples 1000 --seed 42
./build/mlp --train-ratio 0.7 --val-ratio 0.15 --threshold 0.5

Backend Options

OpenMP (CPU parallelism):

cmake -S . -B build-omp -DMLP_ENABLE_OPENMP=ON
cmake --build build-omp
./build-omp/mlp

BLAS / CBLAS (CPU GEMM acceleration):

cmake -S . -B build-blas -DMLP_ENABLE_BLAS=ON
cmake --build build-blas
./build-blas/mlp_dense_benchmark

CUDA (dense ops):

cmake -S . -B build-cuda -DMLP_ENABLE_CUDA=ON
cmake --build build-cuda
./build-cuda/mlp

CUDA notes:

BLAS notes:

Library Usage

Public API headers (stable surface):

Main API entry points:

Architecture

The training path is intentionally small and explicit. Sequential owns the layer stack, the loss produces the initial output gradient, and the optimizer updates trainable parameters after backpropagation.

                         FORWARD
Input Matrix
    |
    v
+-----------+     +--------------+     +-----------+     +--------------+
|   Dense   | --> |  Activation  | --> |   Dense   | --> |  Activation  |
+-----------+     +--------------+     +-----------+     +--------------+
      ^                                       ^
      | trainable parameters                  | trainable parameters
      |                                       |
      +---------------+   +-------------------+
                      |   |
                      v   v
                 +-------------+
                 |  Optimizer  |
                 +-------------+
                       ^
                       | parameter + gradient references
                       |
Target Matrix --> +---------+
                  |  Loss   |
Prediction ------>|         |
                  +---------+
                       |
                       | dLoss / dPrediction
                       v
                 BACKWARD through
          Activation <- Dense <- Activation <- Dense

Forward propagation moves data through each Dense and Activation layer. The Loss compares predictions with targets and starts the backward pass. Each layer propagates gradients in reverse order, while the Optimizer consumes the parameter/gradient references exposed by trainable layers and applies the update step.

Safety and validation contracts:

CMake targets:

Example targets included in this repo:

cmake --build build --target mlp_library_example
./build/mlp_library_example

cmake --build build --target mlp_io_example
./build/mlp_io_example

Install and find_package

Install locally:

cmake -S . -B build
cmake --build build
cmake --install build --prefix /tmp/mlp-install

Consume from another CMake project:

find_package(mlp REQUIRED)
target_link_libraries(your_app PRIVATE mlp::mlp_lib)

Or link only components:

find_package(mlp REQUIRED)
target_link_libraries(your_app PRIVATE mlp::mlp_train mlp::mlp_io)

If using custom install prefix:

cmake -S . -B build -DCMAKE_PREFIX_PATH=/tmp/mlp-install

Documentation

Project Structure

Core source layout:

Recent code-health cleanup:

Project Website (GitHub Pages)

Optimizers Included

Name CLI string Notes
SGD sgd Vanilla stochastic gradient descent
Momentum momentum SGD with exponential moving-average velocity
NAG nag Nesterov Accelerated Gradient
Adam adam Adaptive moment estimation
AdamW adamw Adam + decoupled weight decay
Nadam nadam Adam with Nesterov momentum correction
RMSProp rmsprop Root mean square propagation
AdaGrad adagrad Adaptive per-parameter learning rates (accumulative)
AdaDelta adadelta AdaGrad variant with running averages, no fixed lr
Lion lion Evolved Sign Momentum — sign-based, memory-efficient
LambdaOptimizer — Custom extension hook via user-supplied lambdas

Testing and CI

Run all tests locally:

cmake -S . -B build
cmake --build build
ctest --test-dir build --output-on-failure

Pre-push local check:

./scripts/pre_push_check.sh

Test suite includes:

CI (.github/workflows/ci.yml) runs:

CI verification:

License

Released under the MIT License. The project is provided as-is, without warranty. GitHub issues are welcome.