Repository navigation
Description
Native reverse-mode Enzyme gradients run slower in a Julia process started with more than one default-pool thread. Every ReactiveKernels gradient inherits this (prepare_ad, and ReactiveKernelsPPL.prepare_sampler / sampler_value_and_gradient!), and so does any other Enzyme gradient in the same process. It is not caused by allocation: a derivative that allocates nothing slows the most.
Cause (upstream, Enzyme.jl)
Enzyme's native codegen sets parallel = Base.Threads.nthreads() > 1 and passes it as the atomicAdd argument of the reverse-mode transform. In a multi-threaded process every shadow accumulation (dx[i] += …) then compiles to atomicrmw fadd, which is a compare-and-swap loop on x86-64 and prevents vectorization. The flag is chosen from the process's default-pool thread count when the derivative compiles, not from whether the differentiated code runs in parallel, and autodiff exposes no option to turn it off. ReactiveKernels cannot pass one either.
Reproducer (Enzyme only)
using Enzyme
sum_of_squares(x) = (s = 0.0; @inbounds for i in eachindex(x); s += x[i]^2; end; s)
x = sin.(1:1000); dx = zero(x)
g() = (fill!(dx, 0.0); autodiff(Reverse, Const(sum_of_squares), Active, Duplicated(x, dx)))
g(); t = minimum(@elapsed(g()) for _ in 1:10_000)
ir = sprint(io -> Enzyme.Compiler.enzyme_code_llvm(io, sum_of_squares, Active,
Tuple{Duplicated{Vector{Float64}}}; dump_module = true))
println(Threads.nthreads(), " threads: ", round(Int, 1e9t), " ns, ", count("atomicrmw", ir), " atomicrmw")Run it with JULIA_NUM_THREADS=1 and with JULIA_NUM_THREADS=8. On the machine below (Enzyme 0.13.216) it printed 1 threads: 209 ns, 0 atomicrmw and 8 threads: 3739 ns, 5 atomicrmw; the global-scope snippet adds dispatch overhead, so the table below uses the repository's benchmark/repro_enzyme_thread_atomic_add.jl, which also has a Gaussian log-likelihood case.
Measured (AMD Ryzen 9 9950X, Julia 1.10.12, Enzyme 0.13.216 and 0.13.206)
| gradient | 1 thread | 8 threads |
|---|---|---|
| sum-of-squares loop, n = 1000, 0 B | 50–82 ns, 0 atomicrmw |
3576–3608 ns, 5 atomicrmw |
| Gaussian log likelihood, 434 × 4, 14592 B | 1213–1527 ns, 0 atomicrmw |
4335–4476 ns, 14 atomicrmw |
prepare_sampler gradient, AR(5) regression on 195 rows |
1508–1520 ns | 3284–3288 ns |
prepare_sampler gradient, 4-coefficient regression on 434 rows |
1818–1822 ns | 3343–3393 ns |
The prepare_sampler rows used JULIA_NUM_THREADS=8,2. With JULIA_NUM_THREADS=1,4 (one default thread, four interactive) Enzyme compiles no atomics: interactive threads do not count. A zero-allocation non-allocating ReactiveKernels gradient slows the same way (1722 → 4089 ns for the AR(5) model).
What consumers can do now
Run gradient-heavy work (samplers, benchmarks) in a process with one default-pool thread; interactive threads may be added for serving. Compare gradient timings only at equal default-pool thread counts. Documented in reactivekernels-use §7an.
What would fix it
Upstream in Enzyme.jl: set atomicAdd only when the differentiated module can run in parallel (Enzyme already recognises Base.Threads.threading_run), or expose an option to compile without atomics. This issue tracks that upstream change; ReactiveKernels needs no code change once it lands.
Activity