Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

lib/autodiff.x

Reverse-mode differentiation recorded on a runtime tape.

Primary API

FunctionSummary
AdNode.addSum.
AdNode.compareOrders nodes by primal value, so < and > compare values.
AdNode.cosCosine.
AdNode.divQuotient.
AdNode.expExponential.
AdNode.logNatural logarithm.
AdNode.mulProduct.
AdNode.negNegation.
AdNode.sinSine.
AdNode.sqrtSquare root.
AdNode.subDifference.
AdNode.tanhHyperbolic tangent.
AdNode.varBoxes a node for Var participation.
AdTape.backwardSeeds result with adjoint 1 and propagates active adjoints to its operands.
AdTape.inputRecords an input or constant.
AdTape.newCreates an empty tape in the active Scope.
Var.adnodeUnboxes a node from a Var produced by AdNode.var.

AdNode

AdNode.add

AdNode AdNode.add(AdNode a, AdNode b)

Sum.

Source: lib/autodiff.x:81

AdNode.compare

int AdNode.compare(AdNode a, AdNode b)

Orders nodes by primal value, so < and > compare values.

Source: lib/autodiff.x:128

AdNode.cos

AdNode AdNode.cos(AdNode a)

Cosine.

Source: lib/autodiff.x:141

AdNode.div

AdNode AdNode.div(AdNode a, AdNode b)

Quotient.

Source: lib/autodiff.x:111

AdNode.exp

AdNode AdNode.exp(AdNode a)

Exponential.

Source: lib/autodiff.x:144

AdNode.log

AdNode AdNode.log(AdNode a)

Natural logarithm.

Source: lib/autodiff.x:150

AdNode.mul

AdNode AdNode.mul(AdNode a, AdNode b)

Product.

Source: lib/autodiff.x:101

AdNode.neg

AdNode AdNode.neg(AdNode a)

Negation.

Source: lib/autodiff.x:121

AdNode.sin

AdNode AdNode.sin(AdNode a)

Sine.

Source: lib/autodiff.x:138

AdNode.sqrt

AdNode AdNode.sqrt(AdNode a)

Square root.

Source: lib/autodiff.x:153

AdNode.sub

AdNode AdNode.sub(AdNode a, AdNode b)

Difference.

Source: lib/autodiff.x:91

AdNode.tanh

AdNode AdNode.tanh(AdNode a)

Hyperbolic tangent.

Source: lib/autodiff.x:159

AdNode.var

Var AdNode.var(AdNode node)

Boxes a node for Var participation.

Source: lib/autodiff.x:40

AdTape

AdTape.backward

void AdTape.backward(AdTape tape, AdNode result)

Seeds result with adjoint 1 and propagates active adjoints to its operands. Zero adjoints do not invoke reverse callbacks. Earlier adjoints on the tape are cleared first, so repeated calls do not accumulate.

Source: lib/autodiff.x:68

AdTape.input

AdNode AdTape.input(AdTape tape, double value)

Records an input or constant. Read adjoint after AdTape.backward.

Source: lib/autodiff.x:61

AdTape.new

AdTape AdTape.new(void)

Creates an empty tape in the active Scope.

Source: lib/autodiff.x:46

Var

Var.adnode

AdNode Var.adnode(Var value)

Unboxes a node from a Var produced by AdNode.var.

Source: lib/autodiff.x:43

Public types

TypeKindSummary
AdNodestructOne recorded value: its primal, its accumulated adjoint, and the closure that pushes that adjoint to the operands it came from.
AdTapestructA recording of AdNode operations; see the struct below.

AdNode

typedef struct AdNode { double value, adjoint; Func back; AdTape tape; } *AdNode

One recorded value: its primal, its accumulated adjoint, and the closure that pushes that adjoint to the operands it came from.

Source: lib/autodiff.x:26

AdTape

typedef struct AdTape *AdTape

A recording of AdNode operations; see the struct below. The Automatic Differentiation guide explains runtime tapes and the compile-time alternatives.

Source: lib/autodiff.x:21

Design notes

This optional module is included explicitly; it is not part of the implicit prelude. AdTape records every arithmetic operation on its AdNode values as a closure that propagates an adjoint back to the operands, so a data-dependent computation that $ad.reverse() cannot transform statically still yields a gradient. Values box through Var and each operation allocates a node, so the static decorators in autodiff.xmacro remain the fast path.

Tests and examples

make verify (unittest/test-autodiff.x) and make examples (magic/autodiff).