null theory.
← Back to notebook Technical note, October 2026

How close can a from-scratch autograd engine get to PyTorch?

A fair micro-benchmark of adjoint, the lab’s CPU-only, Numba-accelerated autograd engine, measured against PyTorch on a small MLP.

Setup

adjoint is the lab’s in-house autograd engine: written from scratch, CPU-only, and accelerated with Numba. It is the engine our transformer work runs on, so we wanted a baseline. On a workload small enough to run end to end in seconds, how far off is it from PyTorch?

  • Model: two Linear layers with a ReLU between them, dim to 2 times dim and back to dim.
  • Operation: one forward plus one backward pass. Gradients are zeroed before each iteration.
  • Batch size: 32. Dtype: float32 on both sides.
  • Device: CPU. Both sides pinned to one thread (OMP, MKL, OpenBLAS, Accelerate, Numba, and torch).
  • Warmup: one forward plus backward on each side before timing, matching how we exercise adjoint in practice.
  • Timing: 100 iterations per repeat, 7 repeats per cell. We report median and standard deviation across repeats, in ms per iteration.
  • Environment: Apple M5, macOS 26.5.1, Python 3.14.3, PyTorch 2.10.0, NumPy 2.3.5, Numba 0.63.1. Engine source is adjoint commit cecfd14 (the hash recorded in the results JSON); the benchmark script and results are published in commit 28b81ba, in which adjoint.py is byte-identical to cecfd14.

Results

Input size Adjoint (ms) PyTorch (ms) Ratio
1280.096 ± 0.0400.059 ± 0.0011.63x
2560.191 ± 0.0020.103 ± 0.0011.85x
5120.558 ± 0.0120.251 ± 0.0032.22x
10242.425 ± 0.0410.966 ± 0.0112.51x

Cells show median ± standard deviation across 7 repeats of 100 iterations each. Ratio is Adjoint median divided by PyTorch median. Lower is better; above 1.0x means Adjoint is slower.

Interpretation

Across every size we tested, adjoint is slower than PyTorch, and the gap widens with the input dimension: 1.63x at dim 128 up to 2.51x at dim 1024. At dim 256 the standard deviation is under one percent of the median on both sides. At dim 512 and 1024 it sits between about one and two percent, small enough that the ratios reflect the engines rather than measurement noise. The dim 128 adjoint cell has one slow repeat (0.199 ms) that inflates its standard deviation to roughly forty percent of the median; the median itself is consistent with the other six repeats and we report it unchanged.

Qualitatively, the gap grows with the shape of the matrices, which is also where PyTorch’s backward GEMM is most heavily optimized. We draw no stronger claim than that from a single workload.

Limits of this measurement

  • A single model shape and a single batch size, CPU only, on one machine.
  • Adjoint’s matmul backward unconditionally computes the input gradient. We create the input with gradient tracking off so the array is discarded, but the compute cost is not skipped; PyTorch skips that work in the same configuration. This counts against adjoint in every cell, so these numbers should be read as an upper bound on the current gap, not a like-for-like comparison of two optimized backends.
  • The two engines also differ in how they fuse operations, allocate temporaries, and dispatch BLAS calls. A fair profiling attribution of the remaining gap would need per-op timing we have not yet collected.

Script and environment

The benchmark script and the full per-repeat results, including the environment used, are published alongside this note.

Previous note: taking the transformer apart