Research CommonsResearch Commons
cppgrad/Overview

cppgrad

A high-performance C++ neural network library — a header-only API over a modular core, with a tape-based autograd engine and a swappable tensor backend.

cppgrad is a C++ neural network library built in two layers: a header-only API for ergonomic, PyTorch-like code, and a modular .cpp core tuned for performance. Tensor math runs on top of ArrayFire, so the same code can target CPU, CUDA, or OpenCL devices.

Why it exists

Most C++ ML code either reaches for a heavyweight framework or hand-rolls a one-off autograd. cppgrad sits in between: a small, readable codebase that still gives you real reverse-mode automatic differentiation and an accelerated backend, so you can learn how an autograd engine works — and use it.

  • Header-only API layer. Include a few headers and start writing models.
  • Modular core. Heavy lifting lives in compiled .cpp files for speed.
  • Autograd engine. Build a computation graph and call .backward() to get gradients.
  • Prebuilt gradient functions. Neg, Exp, Log, Pow, Sum, Mean, Max, and more.
  • Swappable backend. ArrayFire by default, with room for custom CUDA/OpenCL backends.
  • Modern CMake. Drop it in via FetchContent or a submodule.
  • Examples, tests, and benchmarks. Runnable examples plus a Google Benchmark setup.

At a glance

#include <cppgrad/tensor/tensor.hpp>
 
int main() {
  using namespace cppgrad;
 
  // Two tensors that track gradients
  Tensor a = Tensor::full({2, 2}, 3.0f, /*requires_grad=*/true);
  Tensor b = Tensor::full({2, 2}, 2.0f, /*requires_grad=*/true);
 
  Tensor c = a + b;
  Tensor d = c * b;
 
  d.backward();
 
  std::cout << "Grad of a:\n" << a.grad() << std::endl;
  std::cout << "Grad of b:\n" << b.grad() << std::endl;
}
Status

cppgrad is alpha and under active development — the autograd core and a growing set of gradient functions work today, while the test suite and op coverage are still expanding. See the roadmap and the DeepWiki overview for more.

Where to next