Graph recording
XAD is open source under AGPL-3.0. In practice, the AGPL's terms don't suit most production settings in finance. For production use, XAD is available under an annual subscription: commercial terms, support, updates and code generation. See XAD licensing.
Overview
XAD provides optional graph recording: it records a computation once into a graph, which can then be replayed many times with different inputs, with forward and adjoint passes. This is particularly useful when:
- The same computation needs to be evaluated repeatedly with varying inputs
- You want to avoid the overhead of re-recording a tape for each evaluation
- Performance is critical for repeated derivative calculations
Compile-time feature flag
Graph recording is only available when XAD is compiled with XAD_ENABLE_GRAPH=ON (off by default). This is a compile-time configuration option; it builds the graph recorder together with its reference backend, the graph interpreter (see XAD_ENABLE_GRAPH).
Tape vs. Graph: Key Differences
Understanding the difference between tape-based adjoint mode and graph recording is essential:
| Aspect | Tape | Graph |
|---|---|---|
| Recording | Re-records every run | Records once, replays many times |
| Control flow | Evaluated per run | Baked in at record time (unless using ABool::If) |
| Memory | Tape grows with computation | Fixed graph size after recording |
| Use case | Single or varying computations | Repeated evaluations with same structure |
Basic Graph Usage
Here is a simple example showing how to record and replay a graph:
#include <XAD/XAD.hpp>
using AD = xad::AReal<double, 1>;
xad::GraphRecorder<double, 1> recorder;
// Create and register inputs
AD x = 2.0;
AD y = 3.0;
recorder.registerInput(x);
recorder.registerInput(y);
// Record the computation
AD z = x * y + x;
recorder.registerOutput(z);
// Prepare the recorded graph for the backend
recorder.compile();
// Execute forward pass
double out = 0.0;
recorder.forward(&out);
// out now contains the result
// Compute adjoints
recorder.setDerivative(z.getSlot(), 1.0);
recorder.computeAdjoints();
double dz_dx = recorder.getDerivative(x.getSlot());
double dz_dy = recorder.getDerivative(y.getSlot()); Handling Branching: The Control Flow Problem
A critical difference between tape mode and graph recording is how they handle control flow.
Consider a piecewise function:
template <class AD>
AD piecewise(const AD& x)
{
if (xad::value(x) < 2.0)
return 1.0 * x;
return 7.0 * x;
} With Tape Mode
Tape mode re-records the computation for each run. The if statement is evaluated fresh each time based on the current input value:
using mode = xad::adj<double>;
using tape_type = mode::tape_type;
using AD = mode::active_type;
auto run = [](double x0) {
tape_type tape;
AD x = x0;
tape.registerInput(x);
tape.newRecording();
AD y = piecewise(x); // if is evaluated NOW with current x
tape.registerOutput(y);
xad::derivative(y) = 1.0;
tape.computeAdjoints();
std::cout << "x=" << x0 << " y=" << xad::value(y)
<< " dy/dx=" << xad::derivative(x) << "\n";
};
run(1.0); // x < 2, so y = 1*x = 1, dy/dx = 1
run(3.0); // x >= 2, so y = 7*x = 21, dy/dx = 7 Output:
x=1 y=1 dy/dx=1
x=3 y=21 dy/dx=7 With Graph Recording (Plain if - Incorrect)
Graph recording records the computation once. A plain C++ if is evaluated at record time and the chosen branch is baked into the graph:
using AD = xad::AReal<double, 1>;
xad::GraphRecorder<double, 1> recorder;
AD x = 1.0; // Record with x=1
recorder.registerInput(x);
AD y = piecewise(x); // if evaluated NOW: x < 2 is true, records "return 1*x"
recorder.registerOutput(y);
recorder.compile();
// First run: x=1
double out = 0.0;
recorder.forward(&out);
recorder.setDerivative(y.getSlot(), 1.0);
recorder.computeAdjoints();
std::cout << "x=1 y=" << out << " dy/dx=" << recorder.getDerivative(x.getSlot()) << "\n";
// Second run: x=3 (but graph still has the x < 2 branch!)
x = 3.0;
recorder.clearDerivatives();
recorder.forward(&out);
recorder.setDerivative(y.getSlot(), 1.0);
recorder.computeAdjoints();
std::cout << "x=3 y=" << out << " dy/dx=" << recorder.getDerivative(x.getSlot()) << "\n"; Output (wrong for x=3):
x=1 y=1 dy/dx=1
x=3 y=3 dy/dx=1 The graph was recorded with the x < 2 branch, so even when x=3 it still computes 1*x = 3 instead of 7*x = 21.
The Solution: ABool::If
To handle branching correctly in a recorded graph, use ABool::If which records a conditional node that evaluates the condition at runtime:
template <class AD>
AD piecewise_abool(const AD& x)
{
// Create a trackable boolean from the comparison
auto cond = xad::less(x, 2.0);
// Compute BOTH branches
AD true_branch = 1.0 * x;
AD false_branch = 7.0 * x;
// Record a conditional selection node
return cond.If(true_branch, false_branch);
} Now the replayed graph is correct:
using AD = xad::AReal<double, 1>;
xad::GraphRecorder<double, 1> recorder;
AD x = 1.0;
recorder.registerInput(x);
AD y = piecewise_abool(x); // Records conditional node with both branches
recorder.registerOutput(y);
recorder.compile();
// Run with x=1
double out = 0.0;
x = 1.0;
recorder.clearDerivatives();
recorder.forward(&out);
recorder.setDerivative(y.getSlot(), 1.0);
recorder.computeAdjoints();
std::cout << "x=1 y=" << out << " dy/dx=" << recorder.getDerivative(x.getSlot()) << "\n";
// Run with x=3
x = 3.0;
recorder.clearDerivatives();
recorder.forward(&out);
recorder.setDerivative(y.getSlot(), 1.0);
recorder.computeAdjoints();
std::cout << "x=3 y=" << out << " dy/dx=" << recorder.getDerivative(x.getSlot()) << "\n"; Output (correct):
x=1 y=1 dy/dx=1
x=3 y=21 dy/dx=7 Comparison Summary
The following table summarises the behaviour across all combinations:
| Scenario | x | y | dy/dx | Note |
|---|---|---|---|---|
Tape with plain if | 1 | 1 | 1 | Correct |
Tape with plain if | 3 | 21 | 7 | Correct |
Graph with plain if (record) | 1 | 1 | 1 | Correct |
Graph with plain if (replay) | 3 | 3 | 1 | Wrong - branch baked in |
Tape with ABool::If | 1 | 1 | 1 | Correct |
Tape with ABool::If | 3 | 21 | 7 | Correct - ABool works with tape too |
Graph with ABool::If (record) | 1 | 1 | 1 | Correct |
Graph with ABool::If (replay) | 3 | 21 | 7 | Correct - conditional node |
Best Practice
When writing code that may be recorded in a graph, use ABool::If for all conditional logic that depends on active variables. This ensures correct behavior with both the tape and graph recording.
Available Comparison Functions
XAD provides comparison functions that return ABool for use in recorded graphs:
xad::less(a, b)- returnsABoolfora < bxad::lessEqual(a, b)- returnsABoolfora <= bxad::greater(a, b)- returnsABoolfora > bxad::greaterEqual(a, b)- returnsABoolfora >= b
Each takes two active values, or an active value and a scalar (in that order).
Reference
For detailed API documentation, see:
See also
This tutorial is based on the included XAD sample (graph_tutorial).
Production use: xad-codegen, a native code generation backend for recorded graphs, is available under a commercial licence (see XAD editions). See also licensing and support.
Questions or issues: GitHub Discussions · issues. Would you rather we AAD-enabled your code? Get a free speed-up estimate.