Skip to content

Compiling with Reactant ​

Loading Reactant lets operator calls on traced arrays compile with Reactant.@compile, which makes Reactant one of the backends. The time loop compiles to one loop, so the program does not grow with steps. With KernelAbstractions loaded too, a large loop over strata runs as one kernel. Pass the history as a traced array too, to join it to a traced output. These blocks are not run in the docs build, which has no Reactant:

julia
using ComposableRecurrences, Reactant, KernelAbstractions, CUDA, Enzyme
r = Recurrence([0.5, 0.3])
R = Reactant.to_rarray(fill(1.1, 50))
f(R) = r(R; history = [1.0, 1.0])
y = (Reactant.@compile f(R))(R)

loss(R) = sum(f(R))
grad(R) = Enzyme.gradient(Enzyme.Reverse, loss, R)[1]
dR = (Reactant.@compile raise = true raise_first = true grad(R))(R)

A loop over many strata runs as one kernel, which compiles through CUDA.jl, so load CUDA, on the CPU too. An Enzyme gradient through the loop's kernels compiles once Reactant raises them to array operations, so pass raise = true raise_first = true to @compile, as above. A user modifier follows the rules for traced values.

Executors, rules and growth under Reactant

A traced call ignores the set executor, and Enzyme differentiates the traced program rather than the package's rules. growth solves for its root inside the compiled program, in a loop that stops when the root has settled.

The support matrix records which scenarios compile and match.