You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Native Enzyme Reverse: plain AutoEnzyme rejects a prepared callable holding a constant UniformScaling (readonly proof flags Enzyme's own innerparm copy) #54
Tracking issue for an upstream Enzyme readonly-proof false positive that native prepared AD meets through DifferentiationInterface's default AutoEnzyme.
Symptom
A model that adds a parameter-free UniformScaling to a matrix, for example a covariance jitter, fails on the first native Reverse gradient with plain AutoEnzyme(; mode = Enzyme.Reverse):
@rkpplbegin
rho ~LogNormal(0, 0.5)
alpha ~HalfNormal(2)
z[1:N] .~Normal.(0, 1)
K = alpha^2.*exp.(-0.5.* D ./ rho^2) +1e-9* I
L =rk_cholesky_lower(K) # or cholesky(Symmetric(K)).L
f = L * z
k .~PoissonLog.(f)
end
EnzymeMutabilityException: Function argument passed to autodiff cannot be proven readonly.
... The potentially writing call is store double %.fca.0.0.1.0.0.0.extract, ptr %.fca.0.0.1.0.0.0.gep, ...
using %.fca.0.0.1.0.0.0.gep = getelementptr inbounds nuw i8, ptr %"call::_ADKernelCall.innerparm", i64 8
Measured with Enzyme 0.13.216 on Julia 1.12.7 and 1.13.1; the same model differentiates exactly on Julia 1.10.12. On Julia 1.13.1, a shared computed slice prior over bound slices (MvNormal.(eachrow(M), Ref(s2 .* S0))) fails at the same kind of store into the rooted copy (.roots.call.innerparm).
Cause
1e-9 * I depends on no parameter, so partial evaluation folds it into a UniformScaling{Float64} constant inside the prepared callable. The flagged store is not in the model: innerparm is the stack slot Enzyme's calling-convention wrapper creates for a function argument passed by value, and the store initializes that copy field by field. Enzyme's readonly proof (err_if_func_written) reports it as a write to the function argument.
benchmark/repro_enzyme_innerparm_split_store_readonly.jl reproduces it with Enzyme only (a callable holding a matrix and a UniformScaling that computes x^2 .* A + s) on Julia 1.10.12, 1.12.7 and 1.13.1 with Enzyme 0.13.216, and on Julia 1.13.1 with the local Enzyme dogfood line (0.13.215).
Workaround
AutoEnzyme(; mode = Enzyme.Reverse, function_annotation = Enzyme.Const) differentiates the model exactly. A jitter that depends on a parameter, or one bound as a matrix, also passes (measured on Julia 1.13.1).
Pins to drop when a registered Enzyme carries the repair
packages/ReactiveKernelsPPL/test/test_multivariate_prior_values.jl, testset "a non-centered latent GP with a constant jitter": the plain-backend branch that accepts this specific exception.
Another trigger, and a second readonly-proof site that the innerparm repair described above does not cover.
A BetaBinomial2 response whose mean or precision is a sampled scalar raises the same EnzymeMutabilityException ("Function argument passed to autodiff cannot be proven readonly") on the first plain AutoEnzyme(; mode = Enzyme.Reverse) gradient, on Julia 1.10.12. The value is exact, and function_annotation = Enzyme.Const returns the exact gradient, so nothing writes the callable.
using ReactiveKernels, ReactiveKernelsPPL, Enzyme, Distributions
using DifferentiationInterface: AutoEnzyme
m =@rkpplbegin
a ~Normal(0.0, 5.0)
c .~BetaBinomial2.(10, logistic.(a), 5.0) # also: BetaBinomial2.(10, 0.4, exp.(a))end
plan =m() | (; c = [3, 5, 7, 2, 6, 4])
built =build_kernel(plan)
q =prepare_sampler(built, plan, [0.5]; backend =AutoEnzyme(; mode = Enzyme.Reverse))
sampler_value_and_gradient!(q, similar([0.5]), [0.5]) # expected gradient -4.1397861188805125
The location stays a scalar, so the observation plate hoists its cell invariant logbeta(alpha, beta), a ScalarDerivativeRule call with custom Enzyme rules, out of the loop. A mean with one value per observation takes no hoist and differentiates.
Enzyme 0.13.216 flags the innerparm store described in this issue.
An Enzyme build carrying the repair for that store accepts it, and the proof then flags the hoisted call itself, although LLVM marks its pointer argument into the callable readonly and nocapture:
Still open with registered Enzyme 0.13.219 (Enzyme_jll 0.0.307), Julia 1.13.1, ReactiveKernels 0.1.129 / ReactiveKernelsPPL 0.1.133 (079bbe4b).
Since ReactiveKernelsPPL 0.1.133, prepare_sampler prepares the non-allocating gradient by default (nonallocating = :auto) for a reverse-mode AutoEnzyme backend. For the latent GP in this issue (PosteriorDB gp_pois_regr: 11 inputs, K = alpha^2 .* exp.(-0.5 .* dist_sq ./ rho^2) + 1e-10 * I, with either rk_cholesky_lower(K) or cholesky(Symmetric(K)).L), that default raises the same exception under plain AutoEnzyme(; mode = Enzyme.Reverse):
EnzymeMutabilityException: Function argument passed to autodiff cannot be proven readonly.
The potentially writing call is store double %.fca.0.0.0.0.0.extract, ptr %"call::_ADNonAllocatingKernelCall.innerparm", align 8, ...
Measured on those versions, both Cholesky spellings:
prepare_sampler(...; nonallocating)
plain AutoEnzyme
function_annotation = Enzyme.Const
:auto (default)
raises
differentiates
false
differentiates
differentiates
Every row that differentiates gives the same gradient. Against a 256-bit BigFloat central-difference gradient at five points, its relative error is at most 6.8e-10 for rho <= 5.35. It grows with the covariance's condition number, to about 5e-7 at rho = 8.04 and 3e-6 at rho = 10 (cond(K) 2.5e11 and 7.2e11). A hand-written Float64 LAPACK Cholesky adjoint is off by a similar amount there.
The workaround in the issue body (function_annotation = Enzyme.Const) still applies. The pins listed there stay until a registered Enzyme release carries the readonly-proof repair.
Activity
nsiccha commentedon Oct 27, 2026
Another trigger, and a second readonly-proof site that the
innerparmrepair described above does not cover.A
BetaBinomial2response whose mean or precision is a sampled scalar raises the sameEnzymeMutabilityException("Function argument passed to autodiff cannot be proven readonly") on the first plainAutoEnzyme(; mode = Enzyme.Reverse)gradient, on Julia 1.10.12. The value is exact, andfunction_annotation = Enzyme.Constreturns the exact gradient, so nothing writes the callable.The location stays a scalar, so the observation plate hoists its cell invariant
logbeta(alpha, beta), aScalarDerivativeRulecall with custom Enzyme rules, out of the loop. A mean with one value per observation takes no hoist and differentiates.innerparmstore described in this issue.readonlyandnocapture:Both sites need repairing in Enzyme's readonly proof before plain
AutoEnzymedifferentiates these shapes.nsiccha commentedon Nov 10, 2026
Still open with registered Enzyme 0.13.219 (Enzyme_jll 0.0.307), Julia 1.13.1, ReactiveKernels 0.1.129 / ReactiveKernelsPPL 0.1.133 (
079bbe4b).Since ReactiveKernelsPPL 0.1.133,
prepare_samplerprepares the non-allocating gradient by default (nonallocating = :auto) for a reverse-modeAutoEnzymebackend. For the latent GP in this issue (PosteriorDBgp_pois_regr: 11 inputs,K = alpha^2 .* exp.(-0.5 .* dist_sq ./ rho^2) + 1e-10 * I, with eitherrk_cholesky_lower(K)orcholesky(Symmetric(K)).L), that default raises the same exception under plainAutoEnzyme(; mode = Enzyme.Reverse):Measured on those versions, both Cholesky spellings:
prepare_sampler(...; nonallocating)AutoEnzymefunction_annotation = Enzyme.Const:auto(default)falseEvery row that differentiates gives the same gradient. Against a 256-bit BigFloat central-difference gradient at five points, its relative error is at most 6.8e-10 for
rho <= 5.35. It grows with the covariance's condition number, to about 5e-7 atrho = 8.04and 3e-6 atrho = 10(cond(K)2.5e11 and 7.2e11). A hand-written Float64 LAPACK Cholesky adjoint is off by a similar amount there.The workaround in the issue body (
function_annotation = Enzyme.Const) still applies. The pins listed there stay until a registered Enzyme release carries the readonly-proof repair.