Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/operators/growth.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 6 additions & 4 deletions test/ADFixtures/src/registry.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
),
),
)
Expand Down
25 changes: 25 additions & 0 deletions test/ad/checks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading