diff --git a/src/operators/growth.jl b/src/operators/growth.jl index 60d7d6391..b41dde942 100644 --- a/src/operators/growth.jl +++ b/src/operators/growth.jl @@ -472,10 +472,14 @@ function _eigen_jacobian( ) M = _primal_matrix(lin, gain, at, ρ, L, S) Dv = _fixed(_lagged(lin, gain, at, v, ρ, L)) - # Joined with `hcat` and `vcat`: Julia 1.11 does not infer the bracket - # form. - T = eltype(M) - J = vcat(hcat(M - I, -Dv), hcat(ones(T, 1, S), zeros(T, 1, 1))) + # 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. + T = promote_type(eltype(M), eltype(Dv)) + J = similar(M, T, S + 1, S + 1) + view(J, 1:S, 1:S) .= M - I + view(J, 1:S, S + 1) .= .-Dv + view(J, S + 1, 1:S) .= one(T) + view(J, S + 1, (S + 1):(S + 1)) .= zero(T) return ρ, v, _inverse(J) end diff --git a/test/ADFixtures/src/registry.jl b/test/ADFixtures/src/registry.jl index e873e45cb..9f0b42650 100644 --- a/test/ADFixtures/src/registry.jl +++ b/test/ADFixtures/src/registry.jl @@ -2540,11 +2540,13 @@ function _growth_rate(w, θ) return ComposableRecurrences.growth(r, exp(only(logR))).rate^2 end # The same for strata that mix: the rate and the profile over strata, from -# a Newton step on both. +# a Newton step on both. The coupling is `K0` scaled entry by entry by the +# exponential of its parameters, so that it mixes the strata at the zero +# vector too. const WG = [0.7, -0.4, 1.1] function _growth_strata(w, θ) - logR, logG, K = _unpack(θ, (S,), (S, L), (S, S)) - r = Recurrence(PerStratum(exp.(logG)); coupling = K) + logR, logG, logK = _unpack(θ, (S,), (S, L), (S, S)) + r = Recurrence(PerStratum(exp.(logG)); coupling = K0 .* exp.(logK)) out = ComposableRecurrences.growth(r, PerStratum(exp.(logR))) return out.rate^2 + sum(WG .* out.profile) end @@ -2558,7 +2560,7 @@ push!( ( "Growth rate and profile of strata that mix", _growth_strata, () -> _flat( - [log(1.6), log(0.9), log(1.2)], repeat(log.(G0)', S), K0 + [log(1.6), log(0.9), log(1.2)], repeat(log.(G0)', S), zeros(S, S) ), ), ) @@ -3587,7 +3589,14 @@ function backend_broken_scenarios() # does not. twin = "NoAdjoint Recurrence two draws on one pool with a flow between" _coverage() && VERSION >= v"1.13" && push!(enzyme, twin) + # The growth of strata that mix solves for its root by a Newton loop + # and a power iteration that run until they settle: value-dependent + # iteration, which a compiled tape does not replay. A tape recorded at + # the zero vector runs 6 power steps, where the scenario's parameters + # need 40. + mixed = "Growth rate and profile of strata that mix" return Dict( + "ReverseDiff (compiled)" => Set([mixed, "NoAdjoint " * mixed]), "Reactant" => Set(reduce(vcat, values(REACTANT_BROKEN))), "Reactant forward" => Set(reduce(vcat, values(REACTANT_BROKEN_FORWARD))), "Reactant reverse" => Set(reduce(vcat, values(REACTANT_BROKEN_REVERSE))), diff --git a/test/ad/checks.jl b/test/ad/checks.jl index 7410710ab..164b7636c 100644 --- a/test/ad/checks.jl +++ b/test/ad/checks.jl @@ -67,6 +67,31 @@ fired = ComposableRecurrences._PULLBACK_CALLS[] > calls rules_fire && test_rule_fires(s.name, fired, plain, s.name in broken) end + name == "ReverseDiff (compiled)" && + test_replay(scens, backend; rtol) + end + return nothing + end + + # A compiled tape replays the operations it recorded, so a value taken + # as a constant when it was recorded gives a wrong gradient elsewhere. + # Each scenario of `scens` is recorded at its parameters and replayed at + # a point shifted by about 1% of each entry, or by 0.01 where an entry + # is zero, against the ForwardDiff gradient there. A scenario whose + # loop runs as many steps at both points replays there, so the check + # holds for every scenario, those broken on the backend included. + function test_replay(scens, backend; rtol) + forward = only( + b.backend for b in ADFixtures.backends() if b.name == "ForwardDiff" + ) + @testset "$(s.name), replayed at a second point" for s in scens + prep = DI.prepare_gradient(s.f, backend, copy(s.x)) + T = eltype(s.x) + scale = map(v -> iszero(v) ? one(v) : abs(v), s.x) + x = s.x .+ T(0.01) .* scale .* sin.(T.(eachindex(s.x))) + ref = DI.gradient(s.f, forward, x) + g = DI.gradient(s.f, prep, backend, x) + @test entrywise(g, ref; rtol) end return nothing end