Skip to content

Plain Enzyme over a closure capturing array data fails or silently returns zero on Julia 1.12+ (upstream readonly-proof gap) #48

Open

Description

@nsiccha

Summary

On Julia 1.12 and 1.13, differentiating an unannotated closure that captures array data with Enzyme (directly, or through DifferentiationInterface's default AutoEnzyme) can raise

EnzymeMutabilityException: Function argument passed to autodiff cannot be proven readonly.

even though the closure only reads its capture. In one loop shape no error is raised, and the primal and the Reverse and Forward derivatives all come back as 0.0. ReactiveKernels scans hit this when called as g -> k(xs, g) for a prepared scan kernel k. This is an upstream Enzyme limitation; prepare_ad passes data as Constant contexts and is unaffected.

Minimal reproducer (no ReactiveKernels)

using Enzyme
let xs = sin.(1:5)
    println(autodiff(Reverse, Const(g -> sum(xs .* g)), Active, Active(0.7)))  # ((0.1761616497223788,),)
    println(autodiff(Reverse, g -> sum(xs .* g), Active, Active(0.7)))         # EnzymeMutabilityException
end

Measured: Julia 1.12.7 and 1.13.1 with Enzyme 0.13.210 fail, and so does Enzyme 0.13.213 on Julia 1.12.7. Julia 1.10.12 with Enzyme 0.13.210 passes.

Cause

Enzyme treats an unannotated function argument as Const and first proves that the function never writes it. That proof follows every value derived from the argument. A Float64 read from the captured array at a loop-dependent offset gets no Julia type, so the proof follows it through the arithmetic. It then reports the store of that Float64 into the fresh result as a capture of the argument. The error is raised only when that store runs, so short inputs can pass, and whether a given loop trips it depends on its optimized IR.

What to do instead

All three are exact on Julia 1.10.12, 1.12.7 and 1.13.1:

  1. prepare_ad(kernel, AutoEnzyme(; mode = Enzyme.Reverse), args...; active = ...).
  2. Enzyme.autodiff(Reverse, Const(f), ...), or AutoEnzyme(; function_annotation = Enzyme.Const) through DifferentiationInterface.
  3. Pass the data as an argument: Enzyme.autodiff(Reverse, (g, xs) -> k(xs, g), Active, Active(g0), Const(xs)).

In this repository

  • benchmark/repro_enzyme_closure_float_store_readonly.jl: the Enzyme-only reproducer, including the silent-zero shape.
  • docs/src/constraints.md and docs/src/scan.md document the boundary.
  • test/test_scan_plate_ad.jl, testset "plain Enzyme over a closure capturing the sequence", pins the unannotated call through a prepared scan kernel as @test_broken on Julia 1.12 and later.

Close this once a registered Enzyme release carries a repair, removing the pin and the documentation together.

Activity

  1. nsiccha commented on Oct 13, 2026

    @nsiccha
    OwnerAuthor

    Update (2026-10-13): the test_scan_plate_ad.jl pin for the prepared-scan closure needed a narrower reading than “broken on Julia 1.12 and later.”

    Hosted 1 and pre core jobs on Julia 1.13.1 with Enzyme 0.13.210 produced the correct derivative, so the old @test_broken reported Unexpected Pass (run 38123158026). On gordito, the same unannotated prepared-scan call still raises Enzyme.Compiler.EnzymeMutabilityException with registered Enzyme 0.13.210 and 0.13.216. Julia 1.12.7/Enzyme 0.13.216 still fails; Julia 1.10.12/0.13.216 passes. The result depends on the optimized IR.

    The landed test and documentation change keeps @test_broken on Julia 1.12. On Julia 1.13+, it accepts only the exact derivative or this specific readonly exception; a wrong derivative or unrelated error fails. The full scan AD test file exits successfully against the landed tree locally on Julia 1.13.1/Enzyme 0.13.210. The new hosted 1/pre jobs are still queued at the time of this comment.

    The Enzyme-only reproducer and the underlying readonly-proof gap remain; this issue should stay open until a registered Enzyme release repairs that gap.

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