Why C++ and How ML Frameworks Work¶
Every jnp.matmul, every torch.nn.Linear, every np.dot call in this book has been executing C++ and CUDA code underneath. This file pulls back the curtain: why ML frameworks are built this way, quick C++ fundamentals for Python engineers, when to write custom C++ kernels, and how to bind them into Python, the bridge between the code you write and the hardware it runs on.
-
You have spent 18 chapters writing Python. You imported JAX, called
jax.grad, ran training loops, and built models. It all felt like Python. But here is the truth: almost none of the actual computation happened in Python. -
When you write
output = model(input)in PyTorch oroutput = jnp.matmul(W, x)in JAX, Python does almost nothing. It constructs a description of the computation (a graph of operations), then hands it off to a C++/CUDA backend that does the real work. Python is the steering wheel; C++ is the engine.
Why Python Frontend, C++ Backend¶
- This two-language architecture exists because Python and C++ are good at opposite things:
| Python | C++ | |
|---|---|---|
| Development speed | Fast (dynamic typing, REPL, no compilation) | Slow (static typing, headers, compile times) |
| Execution speed | ~100x slower than C (interpreted, GIL) | Near-hardware speed (compiled, no overhead) |
| Memory control | Automatic (GC), no control over layout | Manual, precise control over every byte |
| Hardware access | None (no SIMD, no GPU, no custom memory) | Full (intrinsics, CUDA, inline assembly) |
| Ecosystem | Rich for ML (notebooks, visualisation, data) | Rich for systems (OS, drivers, engines) |
-
The insight: use each language for what it is good at. Python handles the parts where human productivity matters (experiment design, hyperparameter tuning, data exploration). C++ handles the parts where machine performance matters (matrix multiplication, convolution, attention kernels).
-
A single matrix multiplication
jnp.matmul(A, B)where \(A\) is \(4096 \times 4096\) performs ~137 billion floating-point operations. In pure Python (nested loops), this takes ~30 minutes. In optimised C++ with AVX-512 SIMD and multithreading, it takes ~10 milliseconds. That is a 180,000x difference. No amount of Python cleverness closes this gap.
How ML Frameworks Are Structured¶
- Every major ML framework follows the same architecture:
User code (Python)
↓
Python API layer (torch.nn, jax.numpy, numpy)
↓
Dispatch / JIT compiler (torch.compile, XLA, NumPy dispatch)
↓
C++ kernel library (ATen/PyTorch, XLA, BLAS/LAPACK)
↓
Hardware-specific backends (CUDA, cuDNN, MKL, oneDNN, Metal)
↓
Hardware (CPU SIMD units, GPU cores, TPU MXUs)
NumPy¶
-
NumPy's core is written in C. When you call
np.dot(A, B), Python calls a C function that calls BLAS (Basic Linear Algebra Subprograms), typically Intel MKL or OpenBLAS. BLAS is hand-optimised C and Fortran code that uses SIMD instructions, cache-aware memory access patterns, and multithreading. Decades of optimisation went into making matrix multiplication fast. -
NumPy is CPU-only. It does not use GPUs. But on CPU, it is extremely fast because it delegates to the best available BLAS implementation.
PyTorch¶
-
PyTorch's computation engine is ATen (A Tensor Library), written in C++. ATen implements ~2000 tensor operations (add, matmul, conv2d, softmax, ...), each with CPU and CUDA backends.
-
When you call
torch.matmul(A, B):- Python dispatches to the ATen C++ function.
- ATen checks the device (CPU or CUDA) and dtype.
- On CPU: calls MKL/OpenBLAS. On GPU: calls cuBLAS (NVIDIA's GPU-optimised BLAS).
- The result is wrapped in a Python tensor object and returned.
-
torch.compile (PyTorch 2.0+) takes this further: it traces your Python code, builds a computation graph, and compiles it using Triton (for GPU) or C++/OpenMP (for CPU). The compiled code fuses operations, eliminates Python overhead, and can be 2-5x faster than eager mode.
JAX¶
-
JAX compiles Python functions to XLA (Accelerated Linear Algebra), Google's compiler for ML workloads. When you
jax.jita function:- JAX traces the function, capturing the operations as an XLA computation graph (HLO — High Level Operations).
- XLA optimises the graph: fuses operations, eliminates redundant computation, optimises memory layout.
- XLA compiles to the target backend: CPU (via LLVM), GPU (via CUDA/PTX), or TPU (via TPU-specific instructions).
- The compiled code runs directly on hardware with zero Python involvement.
-
This is why
jax.jitis so important: without it, every operation is a separate Python→C++ round trip. With it, the entire function is a single compiled kernel.
Quick C++ Fundamentals for Python Engineers¶
- You do not need to become a C++ expert. You need to understand enough to read kernel code, write simple extensions, and understand performance discussions. Here are the essentials.
Types and Variables¶
// C++ requires explicit types (unlike Python)
int count = 0; // 32-bit integer
float loss = 0.5f; // 32-bit float
double lr = 3e-4; // 64-bit float
bool training = true; // boolean
// Arrays (fixed size, stack-allocated)
float weights[1024]; // 1024 floats, contiguous in memory
// Pointers: a variable that holds a memory address
float* ptr = weights; // ptr points to the first element of weights
float val = ptr[42]; // access element 42 via pointer arithmetic
// ptr[42] is equivalent to *(ptr + 42)
// Rust is statically typed with type inference and explicit mutability
let count: i32 = 0; // 32-bit signed integer
let loss: f32 = 0.5; // 32-bit float
let lr: f64 = 3e-4; // 64-bit float
let training: bool = true; // boolean
// Fixed-size array, stack-allocated and initialised
let weights: [f32; 1024] = [0.0; 1024];
// Slices replace raw pointers for safe, bounds-checked contiguous access
let slice: &[f32] = &weights;
let val: f32 = slice[42]; // safe bounds-checked indexing
// Raw pointers exist for FFI / low-level code (dereferencing requires unsafe)
let ptr: *const f32 = weights.as_ptr();
let raw_val: f32 = unsafe { *ptr.add(42) };
- Pointers are the biggest conceptual difference from Python. In Python, everything is a reference and you never think about memory addresses. In C++, pointers give you direct access to memory — powerful but dangerous (dangling pointers, buffer overflows).
Functions¶
// Function declaration: return_type name(param_type param_name)
float relu(float x) {
return x > 0.0f ? x : 0.0f;
}
// Passing by reference (avoids copying large objects)
void scale_vector(std::vector<float>& vec, float factor) {
for (size_t i = 0; i < vec.size(); i++) {
vec[i] *= factor;
}
}
// const reference: read-only, no copy
float sum(const std::vector<float>& vec) {
float total = 0.0f;
for (float x : vec) { // range-based for loop (like Python's for x in vec)
total += x;
}
return total;
}
// Function declaration: fn name(param: Type) -> ReturnType
fn relu(x: f32) -> f32 {
if x > 0.0 { x } else { 0.0 }
}
// Mutable slice borrow (exclusive access, avoids copying)
fn scale_vector(vec: &mut [f32], factor: f32) {
for x in vec.iter_mut() {
*x *= factor;
}
}
// Shared slice borrow: read-only, no copy
fn sum(vec: &[f32]) -> f32 {
let mut total = 0.0f32;
for &x in vec {
total += x;
}
total
}
Memory: Stack vs Heap¶
// Stack allocation: fast, automatic lifetime (freed when function returns)
float buffer[256]; // 256 floats on the stack
// Heap allocation: manual, survives beyond the function
float* data = new float[n]; // allocate n floats on the heap
// ... use data ...
delete[] data; // YOU must free it (no garbage collector)
// Modern C++: smart pointers (automatic cleanup, like Python references)
#include <memory>
auto data = std::make_unique<float[]>(n); // freed automatically when out of scope
// Stack allocation: fast, fixed size, freed when the scope ends
let buffer: [f32; 256] = [0.0; 256];
// Heap allocation with RAII ownership (no garbage collector, no manual delete)
let data: Vec<f32> = vec![0.0; n]; // growable heap buffer
// ... use data ...
// Automatically freed via Drop when `data` leaves scope
// Fixed-size heap slice (direct analogue of std::unique_ptr<float[]>)
let boxed: Box<[f32]> = vec![0.0; n].into_boxed_slice();
- The key rule: stack is fast but limited (typically 1-8 MB). Large arrays (tensors, feature maps) must go on the heap. In Python, everything is on the heap and the GC handles cleanup. In C++, you manage it yourself (or use smart pointers).
Templates (Generics)¶
- Templates are how C++ libraries (like ATen) write code that works with float16, float32, float64, etc. without duplicating the implementation.
The Standard Library Essentials¶
#include <vector> // dynamic array (like Python list)
#include <string> // string type
#include <unordered_map> // hash map (like Python dict)
#include <algorithm> // sort, find, transform, etc.
#include <cmath> // math functions
std::vector<float> vec = {1.0f, 2.0f, 3.0f};
vec.push_back(4.0f); // append
float first = vec[0]; // index
size_t len = vec.size(); // length
std::unordered_map<std::string, int> counts;
counts["hello"] = 5; // insert
if (counts.count("hello")) { } // check existence
use std::collections::HashMap; // hash map (like Python dict)
let mut vec: Vec<f32> = vec![1.0, 2.0, 3.0];
vec.push(4.0); // append
let first: f32 = vec[0]; // index
let len: usize = vec.len(); // length
let mut counts: HashMap<String, i32> = HashMap::new();
counts.insert(String::from("hello"), 5); // insert
if counts.contains_key("hello") { } // check existence
When to Write Custom C++ Kernels¶
-
Most ML engineers never need to write C++. The framework's built-in operations cover 99% of use cases. You should consider custom C++ only when:
-
Your operation does not exist in the framework: a novel activation function, a custom attention pattern, a specialised loss function that cannot be expressed as a composition of existing ops.
-
Fusing operations for performance: your model does
relu(layernorm(matmul(x, W) + b)). Each operation launches a separate kernel, reads and writes memory, and synchronises. A fused kernel does it all in one pass, avoiding memory round-trips. This can be 2-5x faster. -
Reducing memory usage: a custom kernel can compute gradients without storing all intermediate activations (gradient checkpointing at the kernel level).
-
Targeting novel hardware: a new accelerator (e.g., Cerebras, Groq) may not have framework support. You write kernels directly.
-
For cases 1 and 2, Triton (file 03 of this chapter) is often sufficient and much easier than writing CUDA C directly. Only drop to CUDA C when Triton cannot express what you need.
How to Bind C++ to Python¶
- Writing C++ is half the job. You also need to call it from Python.
pybind11 (General Purpose)¶
- pybind11 creates Python bindings for C++ functions with minimal boilerplate:
// my_ops.cpp
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
namespace py = pybind11;
// A simple custom operation
py::array_t<float> custom_relu(py::array_t<float> input) {
auto buf = input.request();
float* ptr = static_cast<float*>(buf.ptr);
size_t n = buf.size;
auto result = py::array_t<float>(n);
float* out = static_cast<float*>(result.request().ptr);
for (size_t i = 0; i < n; i++) {
out[i] = ptr[i] > 0 ? ptr[i] : 0;
}
return result;
}
PYBIND11_MODULE(my_ops, m) {
m.def("custom_relu", &custom_relu, "Custom ReLU operation");
}
// src/lib.rs — build with: maturin develop --release (PyO3 + rust-numpy)
use numpy::{IntoPyArray, PyArray1, PyReadonlyArray1};
use pyo3::prelude::*;
#[pyfunction]
fn custom_relu<'py>(py: Python<'py>, input: PyReadonlyArray1<'py, f32>) -> Bound<'py, PyArray1<f32>> {
let slice = input.as_slice().expect("contiguous input array");
let out: Vec<f32> = slice.iter().map(|&v| v.max(0.0)).collect();
out.into_pyarray(py)
}
#[pymodule]
fn my_ops(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(custom_relu, m)?)?;
Ok(())
}
# Compile
pip install pybind11
c++ -O3 -shared -std=c++17 -fPIC $(python3 -m pybind11 --includes) my_ops.cpp -o my_ops$(python3-config --extension-suffix)
PyTorch C++ Extensions¶
- PyTorch provides a streamlined way to add custom ops:
// src/lib.rs — custom GELU exposed to Python via PyO3 (or callable directly in Rust)
use numpy::{IntoPyArray, PyArray1, PyReadonlyArray1};
use pyo3::prelude::*;
fn gelu_scalar(x: f32) -> f32 {
// Exact GELU via Abramowitz-Stegun erf approximation (or libm::erff)
let z = x / std::f32::consts::SQRT_2;
let t = 1.0 / (1.0 + 0.3275911 * z.abs());
let poly = t * (0.254829592
+ t * (-0.284496736
+ t * (1.421413741
+ t * (-1.453152027 + t * 1.061405429))));
let erf_z = (1.0 - poly * (-z * z).exp()).copysign(z);
0.5 * x * (1.0 + erf_z)
}
#[pyfunction]
fn custom_gelu<'py>(py: Python<'py>, input: PyReadonlyArray1<'py, f32>) -> Bound<'py, PyArray1<f32>> {
let out: Vec<f32> = input.as_slice().unwrap().iter().map(|&v| gelu_scalar(v)).collect();
out.into_pyarray(py)
}
#[pymodule]
fn custom_ops(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(custom_gelu, m)?)?;
Ok(())
}
// Native Rust execution (compile with: cargo run --release)
fn custom_gelu(input: &[f32]) -> Vec<f32> {
input
.iter()
.map(|&x| {
// Tanh-based GELU approximation: 0.5 * x * (1 + tanh(sqrt(2/pi) * (x + 0.044715 * x^3)))
let k = (2.0f32 / std::f32::consts::PI).sqrt();
0.5 * x * (1.0 + (k * (x + 0.044715 * x * x * x)).tanh())
})
.collect()
}
fn main() {
let x: Vec<f32> = (0..1000).map(|i| (i as f32 - 500.0) * 0.01).collect();
let y = custom_gelu(&x);
println!("y[0] = {:.4}, y[500] = {:.4}", y[0], y[500]);
}
torch.utils.cpp_extension.loadcompiles the C++ code, creates a shared library, and loads it as a Python module, all in one call. This is the easiest way to experiment with custom C++ ops in PyTorch.
JAX Custom Calls¶
-
JAX uses XLA custom calls. The process is more involved (you register a C function with XLA), but the concept is the same: write C/C++, bind it, call it from Python.
-
For most JAX users, Pallas (covered in file 03) is the better choice because it lets you write custom GPU and TPU kernels in a Python-like syntax that XLA compiles without leaving the JAX ecosystem.
The Big Picture¶
-
This file explained the layer between Python and hardware. The remaining files in this chapter go deeper into the hardware and kernel stack.
- File 01 introduces the hardware itself (CPU execution, SIMD vector units, the roofline model, and chip architectures).
- File 02 covers GPU architecture and CUDA, from warps and shared-memory tiling through Hopper and Blackwell primitives.
- File 03 climbs to Python-first kernel DSLs and custom silicon, covering Triton, ThunderKittens, CuTe DSL, TileLang, Google TPUs, and JAX Pallas.
- File 04 explores cross-platform GPU compute across Vulkan, WebGPU, Apple Metal with MLX, and AMD ROCm with HIP.
- Files 05 to 07 walk through 19 hands-on compilable CUDA tutorials spanning vectors, statistics, matrices, sparse kernels, deep learning, and reinforcement learning.
-
The progression mirrors the abstraction ladder: C++ intrinsics (lowest, most control) → CUDA (GPU-specific) → Triton/Pallas (Pythonic, compiled) → JAX/PyTorch (highest, automatic). Each level trades control for convenience. Understanding the lower levels makes you a better user of the higher ones.
Coding Tasks (compile with g++ or clang++)¶
-
Write your first C++ program. Allocate an array, fill it, compute the sum, and measure the time. This introduces compilation, arrays, pointers, and timing.
// task1_basics.cpp // Compile: g++ -O3 -o task1 task1_basics.cpp // Run: ./task1 #include <iostream> #include <chrono> #include <vector> int main() { const int N = 10'000'000; // C++ allows ' as digit separator std::vector<float> data(N); // Fill the array for (int i = 0; i < N; i++) { data[i] = static_cast<float>(i) * 0.001f; } // Compute sum auto start = std::chrono::high_resolution_clock::now(); float sum = 0.0f; for (int i = 0; i < N; i++) { sum += data[i]; } auto end = std::chrono::high_resolution_clock::now(); double elapsed = std::chrono::duration<double, std::milli>(end - start).count(); std::cout << "Sum: " << sum << std::endl; std::cout << "Time: " << elapsed << " ms" << std::endl; std::cout << "Elements: " << N << std::endl; std::cout << "Throughput: " << (N * sizeof(float)) / elapsed / 1e6 << " GB/s" << std::endl; return 0; }// task1_basics.rs // Compile: rustc -O -o task1 task1_basics.rs // Run: ./task1 use std::hint::black_box; use std::time::Instant; fn main() { const N: usize = 10_000_000; let data: Vec<f32> = (0..N).map(|i| (i as f32) * 0.001).collect(); // Compute sum let start = Instant::now(); let mut sum = 0.0f32; for &x in &data { sum += x; } let sum = black_box(sum); let elapsed = start.elapsed().as_secs_f64() * 1000.0; println!("Sum: {sum}"); println!("Time: {elapsed:.3} ms"); println!("Elements: {N}"); println!( "Throughput: {:.2} GB/s", (N * std::mem::size_of::<f32>()) as f64 / elapsed / 1e6 ); } -
Write a C++ function that computes ReLU on an array, then build a Python binding using pybind11. Call it from Python and compare speed against NumPy.
// task2_relu.cpp // Compile: c++ -O3 -shared -std=c++17 -fPIC $(python3 -m pybind11 --includes) \ // task2_relu.cpp -o my_relu$(python3-config --extension-suffix) #include <pybind11/pybind11.h> #include <pybind11/numpy.h> namespace py = pybind11; py::array_t<float> cpp_relu(py::array_t<float> input) { auto buf = input.request(); float* ptr = static_cast<float*>(buf.ptr); int n = buf.size; auto result = py::array_t<float>(n); float* out = static_cast<float*>(result.request().ptr); for (int i = 0; i < n; i++) { out[i] = ptr[i] > 0.0f ? ptr[i] : 0.0f; } return result; } PYBIND11_MODULE(my_relu, m) { m.def("relu", &cpp_relu, "C++ ReLU"); }// src/lib.rs — build with: maturin develop --release (PyO3 + rust-numpy) use numpy::{IntoPyArray, PyArray1, PyReadonlyArray1}; use pyo3::prelude::*; #[pyfunction] fn relu<'py>(py: Python<'py>, input: PyReadonlyArray1<'py, f32>) -> Bound<'py, PyArray1<f32>> { let slice = input.as_slice().expect("contiguous float32 array"); let out: Vec<f32> = slice.iter().map(|&v| v.max(0.0)).collect(); out.into_pyarray(py) } #[pymodule] fn my_relu(m: &Bound<'_, PyModule>) -> PyResult<()> { m.add_function(wrap_pyfunction!(relu, m)?)?; Ok(()) }
# test_relu.py — run after compiling the C++ module above
import numpy as np
import torch
import time
import my_relu # the compiled C++ module
x = np.random.randn(10_000_000).astype(np.float32)
x_torch = torch.from_numpy(x)
# C++ ReLU
start = time.time()
for _ in range(100):
y_cpp = my_relu.relu(x)
cpp_time = (time.time() - start) / 100
# PyTorch ReLU
start = time.time()
for _ in range(100):
y_torch = torch.relu(x_torch)
torch_time = (time.time() - start) / 100
print(f"C++ ReLU: {cpp_time*1000:.2f} ms")
print(f"PyTorch ReLU: {torch_time*1000:.2f} ms")
print(f"Match: {np.allclose(y_cpp, y_torch.numpy())}")
# test_relu.py — run after compiling the C++ module above
import numpy as np
import time
import my_relu # the compiled C++ module
x = np.random.randn(10_000_000).astype(np.float32)
# C++ ReLU
start = time.time()
for _ in range(100):
y_cpp = my_relu.relu(x)
cpp_time = (time.time() - start) / 100
# NumPy ReLU
start = time.time()
for _ in range(100):
y_np = np.maximum(x, 0)
np_time = (time.time() - start) / 100
print(f"C++ ReLU: {cpp_time*1000:.2f} ms")
print(f"NumPy ReLU: {np_time*1000:.2f} ms")
print(f"Match: {np.allclose(y_cpp, y_np)}")
use jacobi::prelude::*;
use std::time::Instant;
fn main() -> Result<()> {
manual_seed(42);
let n = 10_000_000;
let x = Tensor::randn(&[n]);
let x_host = x.to_vec_f32()?;
// Native Rust loop ReLU (no Python/C++ binding overhead)
let start = Instant::now();
let mut y_rust = Vec::with_capacity(n);
for _ in 0..100 {
y_rust = x_host.iter().map(|&v| v.max(0.0)).collect();
}
let rust_ms = start.elapsed().as_secs_f64() * 1000.0 / 100.0;
// XLA-compiled Jacobi ReLU
let relu_fn = jit(|t: Tensor| t.relu());
relu_fn.call(x.clone())?.check()?; // warm up
let start = Instant::now();
let mut y_jacobi = x.clone();
for _ in 0..100 {
y_jacobi = relu_fn.call(x.clone())?;
}
y_jacobi.check()?;
let jacobi_ms = start.elapsed().as_secs_f64() * 1000.0 / 100.0;
let y_jacobi_host = y_jacobi.to_vec_f32()?;
let max_diff = y_rust
.iter()
.zip(&y_jacobi_host)
.map(|(a, b)| (a - b).abs())
.fold(0.0f32, f32::max);
println!("Rust loop ReLU: {rust_ms:.2} ms");
println!("Jacobi ReLU: {jacobi_ms:.2} ms");
println!("Match: {}", max_diff < 1e-5);
Ok(())
}
-
Write a C++ program that demonstrates why memory layout matters. Compare row-major vs column-major access patterns and measure the performance difference.
// task3_layout.cpp // Compile: g++ -O3 -o task3 task3_layout.cpp #include <iostream> #include <chrono> #include <vector> int main() { const int N = 4096; std::vector<float> matrix(N * N, 1.0f); // Row-major access: sequential memory addresses (cache-friendly) auto start = std::chrono::high_resolution_clock::now(); float sum_row = 0.0f; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { sum_row += matrix[i * N + j]; // stride-1 access } } auto end = std::chrono::high_resolution_clock::now(); double row_ms = std::chrono::duration<double, std::milli>(end - start).count(); // Column-major access: stride-N access (cache-unfriendly) start = std::chrono::high_resolution_clock::now(); float sum_col = 0.0f; for (int j = 0; j < N; j++) { for (int i = 0; i < N; i++) { sum_col += matrix[i * N + j]; // stride-N access (cache misses!) } } end = std::chrono::high_resolution_clock::now(); double col_ms = std::chrono::duration<double, std::milli>(end - start).count(); std::cout << "Row-major (cache-friendly): " << row_ms << " ms" << std::endl; std::cout << "Col-major (cache-hostile): " << col_ms << " ms" << std::endl; std::cout << "Slowdown: " << col_ms / row_ms << "x" << std::endl; std::cout << "(Both sums: " << sum_row << ", " << sum_col << ")" << std::endl; return 0; }// task3_layout.rs // Compile: rustc -O -o task3 task3_layout.rs use std::hint::black_box; use std::time::Instant; fn main() { const N: usize = 4096; let matrix = vec![1.0f32; N * N]; // Row-major access: sequential memory addresses (cache-friendly) let start = Instant::now(); let mut sum_row = 0.0f32; for i in 0..N { let row = &matrix[i * N..(i + 1) * N]; for &val in row { sum_row += val; // stride-1 access } } let sum_row = black_box(sum_row); let row_ms = start.elapsed().as_secs_f64() * 1000.0; // Column-major access: stride-N access (cache-unfriendly) let start = Instant::now(); let mut sum_col = 0.0f32; for j in 0..N { for i in 0..N { sum_col += matrix[i * N + j]; // stride-N access (cache misses!) } } let sum_col = black_box(sum_col); let col_ms = start.elapsed().as_secs_f64() * 1000.0; println!("Row-major (cache-friendly): {row_ms:.3} ms"); println!("Col-major (cache-hostile): {col_ms:.3} ms"); println!("Slowdown: {:.2}x", col_ms / row_ms); println!("(Both sums: {sum_row}, {sum_col})"); }