Skip to content

Native Enzyme gradients beside an allocating thread can use freed memory (segfaults on Julia 1.10, silent wrong gradients on 1.12; upstream Enzyme) #53

Open

Description

@nsiccha

A native reverse-mode Enzyme gradient that runs while another thread of the same Julia process allocates can use memory that a garbage collection has freed. On Julia 1.10 the process usually dies with signal (11) or GC error (probable corruption) (exit 134); on Julia 1.12 the gradient comes back wrong, value and entries, with no error. ReactiveKernels' native gradients inherit this (prepare_ad, and ReactiveKernelsPPL.prepare_sampler / sampler_value_and_gradient!), and so does any other Enzyme gradient in the process.

Preparing one object per thread does not prevent it: one gradient thread beside one allocating thread is enough, an allocating interactive thread included. Multi-chain sampling with one sampler per thread, parallel completion workers and multi-threaded servers that take gradients are exposed. A process that runs one thread is not.

Cause (upstream, Enzyme.jl)

Not yet diagnosed. It is not a ReactiveKernels lowering defect. A program with no ReactiveKernels code crashes the same way on Julia 1.10: a @noinline function that allocates a small Vector, fills it by forward substitution and reads earlier entries, differentiated with Enzyme.gradient(Reverse, …) on several threads while another task allocates and calls GC.gc(false). The same function with an NTuple buffer instead of the heap vector is clean. A Turing model with a per-observation loop crashes too, with runtime activity and function_annotation = Const. The failing gradients are those whose differentiated program allocates arrays that the reverse pass reads (gathers, matrix products, LKJ factors), so nearly every hierarchical model.

Reproducer

benchmark/repro_concurrent_native_reverse_gc.jl in this repository. Each default-pool thread but one takes prepare_sampler gradients of a correlated random-effects @rkppl model (Z[levels(g), 1:3], an LKJ Cholesky factor, R = Z * F', the gathers R[g, k]) and compares each bitwise with a single-threaded reference, while one task allocates and calls GC.gc(false):

julia --project=<env> -t 4 repro_concurrent_native_reverse_gc.jl 20 re
julia --project=<env> -t 1,1 repro_concurrent_native_reverse_gc.jl 20 re interactive
julia --project=<env> -t 4 repro_concurrent_native_reverse_gc.jl 20 glm

Measured (x86-64, ReactiveKernels ebb412f7d, Enzyme 0.13.216, Enzyme_jll 0.0.303, DifferentiationInterface 0.7.21, 20 s per run)

Julia gradient threads allocating task outcome
1.10.12 3 default thread segfault in 2 of 2 runs
1.10.12 3 default thread 1 wrong of 3,235,102
1.10.12 1 interactive thread segfault
1.10.12 3, prepare_nonallocating (18,000 B per gradient) default thread segfault
1.12.7 3 default thread 3 wrong of 1,830,086; in a second run a DomainError from log of a negative number in the LKJ log-Jacobian, whose argument is positive for every input
1.12.7 1 interactive thread none wrong of 843,583; in a second run 1 wrong of 891,203, the value off by 10.7 and an entry by 21.3
1.12.7 1, in a one-thread process same thread, taking turns none wrong of 2,074,998
1.10.12 3, broadcast GLM instead default thread none wrong of 26,019,776

Other Julia 1.10 measurements on Enzyme 0.13.209 / Enzyme_jll 0.0.301 agree: a logit GLM with a random intercept aborted in 1 of 2 runs with ONE gradient thread (--threads=2); a random-effects model returned 6 wrong gradients of 1,973,020 (value and all 46 entries off by 1 to 36) without crashing; the same model with all tasks on one thread had no mismatch in 19 million gradients.

What consumers can do now

Take native Enzyme gradients in a process that runs one thread; for parallel chains run one single-threaded process per chain (for example Distributed workers started with one thread each) rather than one thread per chain. Julia 1.12 does not avoid it, and neither do runtime activity, a Const function annotation or a non-allocating kernel. Documented in reactivekernels-use §7ao.

What would fix it

An Enzyme.jl repair that keeps the memory its reverse pass reads alive across a collection started by another thread, on Julia 1.10 and 1.12. ReactiveKernels needs no code change once it lands; re-run the reproducer above on both Julia versions to confirm before closing this issue.

Activity

  1. nsiccha commented on Oct 26, 2026

    @nsiccha
    OwnerAuthor

    Update (2026-10-26): the cause is diagnosed, and a repair exists outside registered Enzyme.

    Cause. Enzyme declares the Julia definitions of math functions (tanh, sqrt, exp, log, pow, …) as reading no memory, although their bodies can reach a GC safepoint. Julia's GC lowering then keeps no root for values that are live across those calls, so a collection that another thread starts while this thread polls can free them.

    Repair. A compiler pass that, right before GC lowering, gives every function that can reach a GC safepoint the memory effects of a safepoint again, so those values stay rooted. It lives on an Enzyme.jl development branch that is not yet published. No registered Enzyme release carries it, nor does upstream EnzymeAD main. Derivative values and single-thread speed are unchanged.

    Re-run of benchmark/repro_concurrent_native_reverse_gc.jl (re model) at ReactiveKernels b5e4760fc, 20 s per run, x86-64:

    Enzyme Julia threads, allocating task outcome
    development branch with the repair, Enzyme_jll 0.0.301 1.10.12 -t 4 default (3 runs), -t 1,1 interactive (3 runs) 0 wrong of 15,729,614
    development branch with the repair, Enzyme_jll 0.0.301 1.12.7 -t 4 default (3 runs), -t 1,1 interactive (3 runs) 0 wrong of 12,035,467
    registered 0.13.216, Enzyme_jll 0.0.303 1.10.12 -t 4 default (2 runs), -t 1,1 interactive (1 run) segfault in both -t 4 runs; 0 wrong of 751,961 in the interactive run

    A stress run without a failure is evidence, not proof. This issue stays open until a registered Enzyme release fixes the defect. Until then, take native Enzyme gradients in a process with one thread, and run one such process per parallel chain.

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