Skip to content
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@ PrecompileTools = "1"
Random = "1"
Reactant = "0.2.290"
SparseArrays = "1"
julia = "1.11, 1.12, 1.13"
julia = "1.12, 1.13"
2 changes: 1 addition & 1 deletion benchmark/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ DifferentiationInterface = "0.6, 0.7"
Enzyme = "0.13.221"
EpiAwarePackageTools = "0.3, 0.4"
FFTW = "1.10"
JLArrays = "0.3.1, 0.4, 0.5"
JLArrays = "0.3.3, 0.4, 0.5"
KernelAbstractions = "0.9.34, 0.10"
Mooncake = "0.5.70"
2 changes: 1 addition & 1 deletion benchmark/routes/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ ADTypes = "1"
Adapt = "4"
DifferentiationInterface = "0.6, 0.7"
Enzyme = "0.13.221"
JLArrays = "0.3.1, 0.4, 0.5"
JLArrays = "0.3.3, 0.4, 0.5"
Mooncake = "0.5.70"
OpenCL = "0.10, 1"
pocl_jll = "7"
2 changes: 1 addition & 1 deletion docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Enzyme = "0.13"
EpiAwarePackageTools = "0.3, 0.4"
ForwardDiff = "0.10, 1"
Interfaces = "0.3"
JLArrays = "0.3.1, 0.4, 0.5"
JLArrays = "0.3.3, 0.4, 0.5"
JSON = "0.21, 1"
KernelAbstractions = "0.9.34, 0.10"
Literate = "2"
Expand Down
2 changes: 1 addition & 1 deletion docs/src/developer/extending-performance.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ JLArrays runs each kernel on the CPU, so the two device rows compare the kernels

## [Trimmed executables](@id trimming)

On Julia 1.12 and later, `juliac --trim=safe` compiles an app into an executable that holds only the code the app can reach.
`juliac --trim=safe` compiles an app into an executable that holds only the code the app can reach.
The build fails on any call the compiler cannot resolve from the argument types.
A build can pass and the executable still fail, for example on a call the runtime makes, so the check runs the app as well as building it.

Expand Down
6 changes: 3 additions & 3 deletions docs/src/developer/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,14 @@ Run them with `julia --project=test test/runtests.jl usecase_only`.
## Threads, older Julia versions and Reactant in CI

The `Threads` workflow runs the tests with four threads, so `Threaded` loops run on separate threads.
On Julia 1.11 it runs the whole unit suite; on the current release it runs only the items tagged `:threads`, as the `Test` workflow runs the rest there.
On Julia 1.12, the oldest version the package allows, it runs the whole unit suite; on the current release it runs only the items tagged `:threads`, as the `Test` workflow runs the rest there.
Tag an item `:threads` when it uses `Threaded`, [`EXECUTOR`](@ref ComposableRecurrences.EXECUTOR) or `Threads`, so it runs with four threads on the current release too.
A quality check in `test/package/test_tags.jl` fails on an item that uses them without the tag.
To do the same locally, add `--threads=4` to a [subset run](@ref developer-faq), such as `julia --threads=4 --project=test test/runtests.jl threads_only`.

The `AD` workflow runs every backend on the current Julia release only.
The `AD versions` workflow runs every Enzyme reverse and Mooncake reverse item on Julia 1.11 and 1.12, weekly and on demand.
To run one of these locally, use `julia +1.11 --project=test/ad test/ad/runtests.jl enzyme_reverse`.
The `AD versions` workflow runs every Enzyme reverse and Mooncake reverse item on Julia 1.12, weekly and on demand.
To run one of these locally, use `julia +1.12 --project=test/ad test/ad/runtests.jl enzyme_reverse`.

The Reactant support matrix in `test/reactant/` runs weekly and on `main`, and fails when a cell's status differs from the committed `results.tsv`.
It runs every AD registry scenario, forward and reverse, and the hand-written cases in `cases.jl`.
Expand Down
55 changes: 3 additions & 52 deletions ext/ComposableRecurrencesEnzymeExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -159,50 +159,8 @@ function _addback1(dx::T, m) where {T}
return ccall(:jl_new_structv, Any, (Any, Ptr{Any}, UInt32), T, fs, length(fs))::T
end

# On Julia 1.11, Enzyme reverse mode with runtime activity drops the
# cotangent of a float held in an immutable value next to an array once that
# value passes through a call it cannot infer (for example a closure that
# captures a boxed variable), so the gradient of the float is zero. That
# call is in the caller's code, out of sight of these rules, so they warn
# once per operator type whenever the operator has an active scalar float.
# The annotations fix whether a scalar is active, so each annotation type is
# checked once and later calls only look it up.
const _SCALAR_LOSS_MSG = "On Julia 1.11, Enzyme reverse mode can return a " *
"zero gradient for a scalar float field of an operator when the code " *
"that builds the operator is not type-stable. Keep that code " *
"type-stable (check it with `@code_warntype`), or use Julia 1.12 or later."
const _SCALAR_LOSS_CHECKED = Set{Any}()
const _SCALAR_LOSS_WARNED = Set{Any}()
const _SCALAR_LOSS_LOCK = ReentrantLock()
function _warn_scalar_loss(::Type{T}, annotations, active::F) where {T, F}
A = typeof(annotations)
@lock _SCALAR_LOSS_LOCK (A in _SCALAR_LOSS_CHECKED) && return nothing
warn = active(annotations)
new = @lock _SCALAR_LOSS_LOCK begin
push!(_SCALAR_LOSS_CHECKED, A)
warn && !(T in _SCALAR_LOSS_WARNED) && (push!(_SCALAR_LOSS_WARNED, T); true)
end
new === true && @warn "$(nameof(T)): $(_SCALAR_LOSS_MSG)"
return nothing
end
# A scalar leaf of the rule's entry point.
_active_float(::Active{<:_IEEEFloat}) = true
_active_float(a::MixedDuplicated) = _holds_float(typeof(a.val))
_active_float(a) = false
_any_active_float(leaves) = any(_active_float, leaves)
# Whether a value of type `T` holds a float outside any array.
_holds_float(::Type{<:_IEEEFloat}) = true
function _holds_float(::Type{T}) where {T}
isconcretetype(T) && isstructtype(T) && !ismutabletype(T) || return false
return any(_holds_float, fieldtypes(T))
end
# A plain-AD operator whose shadow can take a cotangent.
_active_operator(::Union{Const, Active}) = false
_active_operator(op) = _holds_float(typeof(op.val))

# The plain-AD hook does nothing in forward mode. In reverse mode it says once
# why the call takes plain AD, and warns on Julia 1.11 when the operator has
# an active scalar float.
# why the call takes plain AD.
function EnzymeRules.forward(
::EnzymeRules.FwdConfig, ::Const{typeof(_plain_hook)}, ::Type,
args::Vararg{Annotation, N}
Expand All @@ -213,7 +171,6 @@ function EnzymeRules.augmented_primal(
::EnzymeRules.RevConfig, ::Const{typeof(_plain_hook)}, ::Type,
op::Annotation, args::Vararg{Annotation, N}
) where {N}
VERSION < v"1.12" && _warn_scalar_loss(typeof(op.val), op, _active_operator)
_note_stopped(op.val, map(a -> a.val, args)...)
return EnzymeRules.AugmentedReturn(nothing, nothing, nothing)
end
Expand Down Expand Up @@ -268,12 +225,8 @@ end

# Code Enzyme traces is not run as primal code, so a convolution it traces
# takes the direct method, which it can differentiate.
# The routes differ by Julia version. On 1.12 Enzyme calls these rules, so
# traced code takes the direct method, the faster one for short kernels. On
# 1.11 it runs a call that returns a `Bool` as it is, without its rule, so
# traced code takes the transform through the rules of `_fft_convolve!`
# below. 1.12 also skips these rules once the call has a constant argument,
# so they are kept as they are, with no arguments.
# Enzyme skips these rules once the call has a constant argument, so they
# are kept as they are, with no arguments.
function EnzymeRules.forward(
config::EnzymeRules.FwdConfig, ::Const{typeof(_untraced)}, ::Type{<:Const}
)
Expand Down Expand Up @@ -590,8 +543,6 @@ function EnzymeRules.augmented_primal(
) where {X, RA <: Annotation, N}
EnzymeRules.width(config) == 1 ||
throw(ArgumentError("batched Enzyme reverse mode is not supported"))
VERSION < v"1.12" &&
_warn_scalar_loss(fieldtype(X, 1), leaves, _any_active_float)
vals = _kept_operator(X, _map_leaves(_val, leaves))
args = _rebuild_leaves(X, vals)
y, cache = _run_forward(args...)
Expand Down
12 changes: 2 additions & 10 deletions ext/ComposableRecurrencesEnzymeTestUtilsExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,13 @@ _ad_ref(r, args...) = _ad(r[], args...)
# The operator is tested active (as Enzyme would annotate it) and constant;
# a constant operator needs runtime activity. EnzymeTestUtils cannot build
# MixedDuplicated, so an operator with array and scalar fields is tested
# active in a `Duplicated` `Ref`, whose shadow takes its cotangent. On Julia
# 1.11 Enzyme drops the cotangent of a scalar field read from a `Ref`, so
# that check runs from 1.12.
# active in a `Duplicated` `Ref`, whose shadow takes its cotangent.
function ComposableRecurrences.test_adjoint(::AutoEnzyme, op, args...; kwargs...)
ret = _act(first(_run_forward(op, args...)))
xs = map(x -> (x, _act(x)), args)
opact = Enzyme.guess_activity(typeof(op), Enzyme.Reverse)
if opact <: MixedDuplicated
if VERSION >= v"1.12"
test_reverse(_ad_ref, ret, (Ref(op), Duplicated), xs...; kwargs...)
else
@info "On Julia 1.11 an operator with array and scalar fields " *
"is checked as Const only: Enzyme drops the cotangent of its " *
"scalar fields read from a Ref." maxlog = 1
end
test_reverse(_ad_ref, ret, (Ref(op), Duplicated), xs...; kwargs...)
acts = (Const,)
else
acts = opact <: Active ? (Active, Const) : (Duplicated, Const)
Expand Down
2 changes: 1 addition & 1 deletion src/adjoints/adjoints.jl
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ _primal(op, args...) = first(_run_forward(op, args...))

# The `Mooncake` rule passes each leaf to the `Enzyme` rule as its own
# argument, so `Enzyme` never shadows a struct holding an array and an active
# scalar: on Julia 1.11 it loses the scalar's cotangent there, a silent zero.
# scalar.
struct _ADLeaves{X} end
function (::_ADLeaves{X})(leaves::Vararg{Any, N}) where {X, N}
return _primal(_rebuild_leaves(X, leaves)...)
Expand Down
23 changes: 6 additions & 17 deletions src/adjoints/duals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -148,18 +148,11 @@ end
return ntuple(i -> ifelse(i > o, h[i], g[i]), Val(N))
end
# One pass of width `W` that seeds leaves `o + 1` to `o + W`; the cotangents
# of the other leaves come back zero. The tag reaches the closures as a
# static parameter: a closure that captures a type as a value does not infer
# it on Julia 1.11.
# of the other leaves come back zero.
@inline function _dual_pass(
f::F, x, ȳ, xs::Tuple{T, Vararg{T}}, w::Val, o
) where {F, T}
f::F, x, ȳ, xs::Tuple{T, Vararg{T, M}}, ::Val{W}, o
) where {F, T, M, W}
G = typeof(ForwardDiff.Tag(_StepTag(), T))
return _dual_pass(G, f, x, ȳ, xs, w, o)
end
@inline function _dual_pass(
::Type{G}, f::F, x, ȳ, xs::Tuple{T, Vararg{T, M}}, ::Val{W}, o
) where {G, F, T, M, W}
N = Val(M + 1)
xd = ntuple(i -> Dual{G}(xs[i], _unit(T, i - o, Val(W))), N)
y = f(first(_unflat(x, xd)))
Expand All @@ -182,13 +175,9 @@ end
# == A map's local derivative ==

# A map's seeds: a `ForwardDiff.Tag` orders this dual against any outer one,
# so the derivative also runs on dual inputs. The tag reaches the seeds as a
# static parameter: a closure that captures a type as a value does not infer
# it on Julia 1.11.
function _transform_seeds(xs::Tuple{T, Vararg{T}}) where {T}
return _transform_seeds(typeof(ForwardDiff.Tag(_TransformTag(), T)), xs)
end
function _transform_seeds(::Type{G}, xs::Tuple{T, Vararg{T, M}}) where {G, T, M}
# so the derivative also runs on dual inputs.
function _transform_seeds(xs::Tuple{T, Vararg{T, M}}) where {T, M}
G = typeof(ForwardDiff.Tag(_TransformTag(), T))
N = Val(M + 1)
return ntuple(N) do i
Dual{G}(xs[i], ntuple(j -> T(i == j), N))
Expand Down
4 changes: 1 addition & 3 deletions src/modifiers/redistribute.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ end
function Redistribute(M; leak::Bool = false)
return leak ? Redistribute{true}(M) : Redistribute{false}(M)
end
# Julia 1.11 carries the literal `leak` through only with aggressive constant
# propagation.
Base.@constprop :aggressive function Redistribute(K, ε; leak::Bool = false)
function Redistribute(K, ε; leak::Bool = false)
return Redistribute(_kernel_movement(K, ε); leak)
end
function ConstructionBase.constructorof(::Type{<:Redistribute{L}}) where {L}
Expand Down
4 changes: 2 additions & 2 deletions src/operators/growth.jl
Original file line number Diff line number Diff line change
Expand Up @@ -601,8 +601,8 @@ function _eigen_jacobian(
)
M = _primal_matrix(lin, gain, at, ρ, L, S)
Dv = _fixed(_lagged(lin, gain, at, v, ρ, L))
# Filled block by block in an array like `M`, so that it infers on
# Julia 1.11 and takes the number type of the entries, tracked or not.
# Filled block by block in an array like `M`, so that it takes the
# number type of the entries, tracked or not.
T = promote_type(eltype(M), eltype(Dv))
J = similar(M, T, S + 1, S + 1)
view(J, 1:S, 1:S) .= M - I
Expand Down
7 changes: 4 additions & 3 deletions src/operators/recurrence.jl
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,10 @@ _modifier_names(::NamedTuple{N}) where {N} = Val(N)
# The modifiers' names as a `Val`, `Val(nothing)` when they have none.
_names(::Recurrence{K, C, M, N}) where {K, C, M, N} = Val(N)

# The identity coupling is stored with an integer scale: reverse AD on Julia
# 1.11 loses a scalar field's gradient beside a field wrapping a `Bool` in
# uninferred code. Route couplings in a `Pairwise` kernel are stored as given.
# The identity coupling is stored with an integer scale, so integer and
# `Bool` inputs give an output promoted with `Int`. A device extension stores
# a coupling in the form its kernels take. Route couplings in a `Pairwise`
# kernel are stored as given.
_stored_coupling(J::UniformScaling{Bool}) = UniformScaling(Int(J.λ))
_stored_coupling(C) = C

Expand Down
2 changes: 0 additions & 2 deletions src/operators/recurrence_adjoint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,6 @@ function _reverse!(ex, c, modifiers, H̄, Ȳ, r̄, ḡain, ādd, h̄, s̄0, st̄
C̄ = cotangent(r̄, :coupling)
m̄s = _mirrors(cotangent(r̄, :modifiers), modifiers)
_on_gpu(H) && _keep_mirrors!(modifiers, m̄s)
# `eltype(H)`, not a type bound to a local: a closure that captures a
# type as a value does not infer it on Julia 1.11.
s̄s = map(m -> _zeros(H, eltype(H), nstate(m, S)), modifiers)
_seed_states!(s̄s, cotangent(st̄, :states))
kbuf = _kernel_buffer(ḡ, kernel, H, S, L)
Expand Down
3 changes: 1 addition & 2 deletions src/operators/trace_run.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ function _run_forward(w::_WithTrace, gain, add, h, s0, τ0, stop)
y, cache = _run_forward(w.op, gain, add, h, s0, τ0, stop, Val(:traced))
return (y, _traces(_traced_recurrence(w.op), cache, w.sel)), cache
end
# The modifiers are read from the operator, not the cache: plain reverse AD
# on Julia 1.11 can lose a scalar field's cotangent read back from the cache.
# The modifiers are read from the operator, not the cache.
_traced_recurrence(r::Recurrence) = r
_traced_recurrence(w::_WithState) = w.r
# On host arrays a call writes each modifier's trace in its step
Expand Down
2 changes: 1 addition & 1 deletion test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Functors = "0.5"
ForwardDiff = "0.10, 1"
GPUArrays = "11.3, 12.0"
Interfaces = "0.3"
JLArrays = "0.3.1, 0.4, 0.5"
JLArrays = "0.3.3, 0.4, 0.5"
KernelAbstractions = "0.9.34, 0.10"
LinearAlgebra = "1"
LogExpFunctions = "0.3, 1"
Expand Down
2 changes: 1 addition & 1 deletion test/ad/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ FFTA = "0.3.2"
FFTW = "1.10"
ForwardDiff = "1.4.6"
Interfaces = "0.3"
JLArrays = "0.3.1, 0.4, 0.5"
JLArrays = "0.3.3, 0.4, 0.5"
Mooncake = "0.5.70"
ReverseDiff = "1.18.2"
Test = "< 0.0.1, 1"
Expand Down
Loading
Loading