Skip to content

Native Enzyme Reverse: plain AutoEnzyme rejects a prepared callable holding a constant UniformScaling (readonly proof flags Enzyme's own innerparm copy) #54

Open

Description

@nsiccha

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):

@rkppl begin
    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.
  • The matching entry in docs/src/constraints.md.

Activity

  1. nsiccha commented on Oct 27, 2026

    @nsiccha
    OwnerAuthor

    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 = @rkppl begin
        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:
    %30 = call double @julia_ScalarDerivativeRule_4955({ [1 x { { i8 } }], [3 x { { { i8 } } }] } addrspace(11)* nocapture nofree noundef nonnull readonly align 8 dereferenceable(8) %10, double %38, double %40)
    

    Both sites need repairing in Enzyme's readonly proof before plain AutoEnzyme differentiates these shapes.

  2. nsiccha commented on Nov 10, 2026

    @nsiccha
    OwnerAuthor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions