Skip to content

Add a performance benchmark for forward mode autodiff - #1664

Open
reach2sayan wants to merge 4 commits into
boostorg:developfrom
reach2sayan:feature/autodiff-performance-benchmark
Open

reach2sayan wants to merge 4 commits into
boostorg:developfrom
reach2sayan:feature/autodiff-performance-benchmark

Conversation

@reach2sayan

@reach2sayan reach2sayan commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Adds reporting/performance/autodiff_performance.cpp, a Google Benchmark for fvar (forward mode autodiff).

  • Horner: a degree-63 polynomial
  • Transcendental: exp(sin(x)) * log(1 + x²) / sqrt(x)
  • BlackScholes: the call price from example/autodiff_black_scholes.cpp (log, erfc)
  • MseGibbsEnergy: the excess Gibbs energy of a 16-species mixed-solvent electrolyte as a function of pressure (exp, log, sqrt, pow, and a double loop of mixed fvar/double arithmetic)

Proposition

Differentiating $y = \exp(x)$ with respect to $\varepsilon$ gives $y' = x' y$. Expanding both sides as power series gives

$$\sum_{k=1}^{N} k y_k \varepsilon^{k-1} = \left(\sum_{m=1}^{N} m x_m \varepsilon^{m-1}\right) \left(\sum_{j=0}^{N} y_j \varepsilon^{j}\right)$$

The terms contributing to $\varepsilon^{k-1}$ are those with $j = k - m$. Equating the coefficients of $\varepsilon^{k-1}$ on both sides therefore yields

$$ y_0 = e^{x_0} $$ $$ y_k = \frac{1}{k} \sum_{m=1}^{k} m x_m y_{k-m} \quad \text{for} \quad 1 \le k \le N $$

Since each $y_k$ depends only on $y_0, \dots, y_{k-1}$, the coefficients are computed in increasing order of $k$, and the $k$-th one costs $k$ multiplications. The total cost
is therefore

$$ \sum_{k=1}^{N} k = \frac{N(N+1)}{2} = \Theta(N^2) $$

multiplications and a single scalar exp. The present implementation, by comparison, evaluates $$\exp(x) = \sum_{i=0}^{N} \frac{e^{x_0}}{i!} (x - x_0)^i$$ by Horner's scheme. This performs $N$ full series products of $\Theta(N^2)$ multiplications each, for a total of $\Theta(N^3)$.

@NAThompson Would this be of interest? Otherwise I guess a benchmark is good enough

@NAThompson

Copy link
Copy Markdown
Collaborator

@reach2sayan : It is indeed of interest. Could you post the benchmark results on this thread?

@reach2sayan

Copy link
Copy Markdown
Contributor Author

Google Benchmark's complexity fit over all orders 1–32:

Results on an AMD Ryzen 7 PRO 6850U, GCC 15.2, -O2, pinned to one core, median of 5 repetitions: N = derivative order,

Workload N=1 N=2 N=4 N=8 N=16 N=32 Fit
Horner 417 ns 681 ns 901 ns 1.48 µs 3.85 µs 13.3 µs N²
Transcendental 75 ns 110 ns 246 ns 840 ns 3.72 µs 24.2 µs N³
BlackScholes 64 ns 160 ns 306 ns 902 ns 4.29 µs 29.5 µs N³
MseGibbsEnergy 329 ns 1.64 µs 2.12 µs 3.59 µs 8.72 µs 42.2 µs N²

@NAThompson

Copy link
Copy Markdown
Collaborator

@reach2sayan : If you don't mind I'm going to try to get this fixed in this PR-I see you "allow edit by maintainers".

NAThompson and others added 2 commits November 18, 2026 21:18
Co-Authored-By: Claude <noreply@anthropic.com>
O(N^2) multiplications at order N, not the O(N^3) of Horner.

Co-Authored-By: Claude <noreply@anthropic.com>
@NAThompson

Copy link
Copy Markdown
Collaborator

@reach2sayan: you are right. I pushed two commits to your branch: multivariate workloads for the benchmark, then the recurrences for exp, log, sqrt, pow, sin, cos, sinh, cosh, tan, the inverse trigonometric and hyperbolic functions, erf and erfc.

Apple M-series, clang -O2, time per evaluation before → after.

One variable, by derivative order:

Workload 1 2 4 8 16 32
Horner 119 → 117 ns 120 → 122 ns 154 → 150 ns 247 → 246 ns 3.77 → 3.73 µs 14.0 → 13.7 µs
Transcendental 41 → 32 ns 86 → 46 ns 363 → 264 ns 4.18 → 1.36 µs 26.5 → 4.60 µs
BlackScholes 32 → 31 ns 47 → 38 ns 121 → 63 ns 373 → 208 ns 4.64 → 1.20 µs 31.6 → 3.79 µs
MseGibbsEnergy 273 → 276 ns 300 → 291 ns 511 → 492 ns 1.09 µs → 981 ns 5.28 → 2.52 µs 33.3 → 11.1 µs

Several variables, by order in each variable:

Workload 1 2 3 4 6 12
TwoVariables 84 → 35 ns 266 → 72 ns 656 → 195 ns 1.64 µs → 401 ns 6.75 → 1.03 µs 249 → 28.6 µs
ThreeVariables 97 → 63 ns 447 → 171 ns 1.21 µs → 569 ns 2.88 → 1.15 µs 10.8 → 3.69 µs
BlackScholesFourVariables 304 → 213 ns 4.26 → 2.07 µs 61.4 → 16.2 µs 287 → 48.2 µs

Against 100-digit arithmetic, the worst relative error in the 33 coefficients of Transcendental at order 32 goes from 2e8 ε to 14 ε. One tolerance in test_autodiff_2.cpp moves from 1 ε to 4 ε.

Skip zero coefficients; integer powers and non-finite roots
take the old path; sin and cos share one recursion.

Co-Authored-By: Claude <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants