Coming from autodiff
If your C++ code uses autodiff, moving it to XAD is mostly a change of types and of how you ask for the derivatives. Both libraries differentiate ordinary C++ by operator overloading, so the functions you differentiate usually stay as they are.
This page maps the autodiff names to XAD and shows the same function in both.
The types
| autodiff | XAD | Mode |
|---|---|---|
dual, real | xad::fwd<double>::active_type (FReal) | forward, one direction |
VectorXdual, VectorXreal | std::vector<FReal>, or an Eigen vector of FReal (Eigen) | forward |
var | xad::adj<double>::active_type (AReal) plus a Tape | adjoint (reverse) |
dual2nd, real2nd | xad::fwd_fwd<double> or xad::fwd_adj<double> | second order |
HigherOrderDual<N> | nest the modes (higher-order derivatives) | higher order |
The main difference is in adjoint mode. autodiff's var builds an expression graph of heap-allocated nodes as the code runs. XAD records the operations on a tape, and you decide when recording starts and stops and which values are inputs and outputs. That gives control over memory: the tape can be cleared and reused across calls, and long computations can be checkpointed.
The same function in both
Take a function of three inputs:
template <class T>
T f(const T& x, const T& y, const T& z)
{
return x * sin(y) + exp(x * z) / y;
} Forward mode
With autodiff:
#include <autodiff/forward/dual.hpp>
using namespace autodiff;
dual x = 1.0, y = 2.0, z = 3.0;
double dudx = derivative(f<dual>, wrt(x), at(x, y, z)); With XAD, you seed the input you want the derivative for and read it from the output:
#include <XAD/XAD.hpp>
using AD = xad::fwd<double>::active_type;
AD x = 1.0, y = 2.0, z = 3.0;
derivative(x) = 1.0; // differentiate with respect to x
AD u = f(x, y, z);
double dudx = derivative(u); Adjoint mode
With autodiff:
#include <autodiff/reverse/var.hpp>
using namespace autodiff;
var x = 1.0, y = 2.0, z = 3.0;
var u = f(x, y, z);
auto [ux, uy, uz] = derivatives(u, wrt(x, y, z)); With XAD, the tape records the evaluation and one backward pass gives all three derivatives:
#include <XAD/XAD.hpp>
using mode = xad::adj<double>;
using AD = mode::active_type;
mode::tape_type tape;
AD x = 1.0, y = 2.0, z = 3.0;
tape.registerInput(x);
tape.registerInput(y);
tape.registerInput(z);
tape.newRecording();
AD u = f(x, y, z);
tape.registerOutput(u);
derivative(u) = 1.0;
tape.computeAdjoints();
double ux = derivative(x), uy = derivative(y), uz = derivative(z); To differentiate again at new input values, assign the new values, call tape.newRecording() and evaluate again.
Gradients, Jacobians and Hessians
autodiff's gradient, jacobian and hessian functions have XAD counterparts that take the inputs and the function:
For example, the Jacobian of a vector function:
#include <XAD/Jacobian.hpp>
#include <XAD/XAD.hpp>
using mode = xad::adj<double>;
using AD = mode::active_type;
mode::tape_type tape;
std::vector<AD> x = {1.0, 1.5, 1.3, 1.2};
std::function<std::vector<AD>(std::vector<AD>&)> F = [](std::vector<AD>& x) {
return std::vector<AD>{sin(x[0] + x[1]), sin(x[1] + x[2]), cos(x[2] + x[3]), cos(x[3] + x[0])};
};
auto J = computeJacobian(x, F); // J[i][j] = dF_i / dx_j The full samples are in the XAD repository: Jacobian and Hessian.
Moving a code base
- Make the functions you differentiate templates on the number type, as in
fabove, if they are not already. Code written for autodiff'sdualorvarusually is. - Replace
dualandvarwith the XAD types from the table, and thederivative,derivativesandgradientcalls with the patterns above. - Check one gradient against the old results, or against finite differences, before moving the rest.
What XAD adds
- Tape control for large computations: reuse the tape across calls, checkpointing to bound memory, and external functions for code you cannot or do not want to record, such as a library call with a known derivative.
- Vector modes: several forward directions or adjoints in one pass (vector forward, vector adjoint).
- Smoothed functions for code with branches and discontinuities.
- Python bindings:
pip install xad. - Real-world integration: QuantLib, several hundred thousand lines of C++, runs with XAD through the small QuantLibAAD adaptor.
To get started, see installation and the C++ tutorial. Questions are welcome in the XAD discussions.
Questions or issues: GitHub Discussions · issues. Would you rather we AAD-enabled your code? Get a free speed-up estimate.