Repository navigation
Description
The ordinary fixed-size matrix exponential fails during Reactant tracing, before differentiation. This prevents compiled propagation in ReactiveKernelsPPL's subject/event recurrences after fixed-matrix batching itself succeeds.
Environment tested: Julia 1.10.12, Reactant 0.2.290, StaticArrays 1.9.22, Enzyme 0.13.209, CPU backend. No ReactiveKernels code is loaded by this reproducer.
using Reactant, StaticArrays, LinearAlgebra
Reactant.set_default_backend("cpu")
function system_matrix(q)
Reactant.@allowscalar begin
k10,k12,k21,ka = exp(q[1]),exp(q[2]),exp(q[3]),exp(q[4])
SMatrix{3,3}(-ka,ka,0.0,0.0,-(k10+k12),k12,0.0,k21,-k21)
end
end
step(q) = exp(system_matrix(q))*SVector(1.0,0.0,0.0)
q=log.([0.1,0.2,0.3,0.5])
rq=Reactant.to_rarray(q)
matrix=Reactant.@compile system_matrix(rq)
@assert Float64.(matrix(Reactant.to_rarray(q))) ≈ system_matrix(q)
@assert step(q) ≈ exp(Matrix(system_matrix(q)))*[1.0,0.0,0.0]
Reactant.@compile step(Reactant.to_rarray(q))Both controls pass: compiled matrix construction agrees with native Julia, and native static/dense exponentials agree. The final compile raises:
TypeError: non-boolean (Reactant.TracedRNumber{Bool}) used in boolean context
StaticArrays/src/expm.jl:107, _exp
The complete guarded reproduction is benchmark/repro_reactant_static_matrix_exp.jl; a fresh pinned run passes 5 assertions with 1 expected broken compiled capability.
StaticArrays' generic exponential selects Padé approximation order with numerical branches, then scales and squares in a value-dependent loop. Reactant's StaticArrays extension supports tracing static values but has no exponential/control lowering. I also inspected official Reactant HEAD 82a66dc8a75fcde1e6805d1a20fb41bf118d04c7: that extension has the same relevant content; I did not execute that full HEAD stack.
Additional diagnostics, not repaired-exponential acceptance:
- Copying the unchanged generic exponential body to a local diagnostic function and annotating only its outer branch with
Reactant.@tracepreserves the native value on this input. Compilation then fails in Reactant'soverloaded_lu: its tensor transpose operation receives anSMatrix{3,3,TracedRNumber{Float64}}. Simply tracing the first branch is therefore insufficient. - A separate runtime-count squaring loop passes compiled values at counts 0, 1, 3 and 5 and an ordinary native Enzyme gradient control. Default compiled reverse creates a dynamic gradient tape and fails with
'stablehlo.dynamic_pad' op can't be translated to XLA HLO. This is a related acceptance guard, not proof that the full exponential reaches the same derivative failure.
A general repair must lower the existing numerical branches lazily, handle static-matrix operands across linear-algebra tensor operations, preserve runtime scaling/squaring iteration, and support ordinary default reverse. Acceptance must include every approximation branch, compiled executable reuse across scaling counts, repeated-dose matrix powers, and invariant backend loop/control structure as subject, event and dose sizes grow. Related retained-loop reverse tracking: #9.
The existing RK boundary documentation and named broken tests remain valid. Native propagation remains the supported route pending the capability. Eager predication, input-dependent host propagation, unrolling runtime counts, custom derivative rules on dependency functions, and activity overrides do not satisfy the repository constraints.
Activity