Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
67 changes: 17 additions & 50 deletions src/modifiers/allocate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -596,74 +596,41 @@ _keeps_after(::Allocate{<:Any, <:_AllocateImports}) = true
function pullback!(
grads, m::Allocate{<:Any, <:_AllocateImports}, ::Step, v, s, t
)
(; fraction, pattern, spill) = m.imports
v̄, s̄ = grads.v, grads.s
S = length(v)
h̄ = view(s̄, (length(s̄) - S + 1):length(s̄))
īm = cotangent(grads.piece, :imports)
T̄, φ̄ = cotangent(grads.piece, :total), cotangent(īm, :fraction)
u, h = _spilled_and_pattern(_field(grads, :after), spill, pattern, v, t)
pass = _field(grads, :pass)
ε = eps(eltype(v))
for p in 1:_ngroups(m)
zs = _group(m, p)
su = zero(eltype(v))
sh = zero(eltype(v))
au = zero(eltype(v̄))
ah = zero(eltype(v̄))
for k in zs
su += u[k]
sh += h[k]
au += v̄[k] * u[k]
ah += v̄[k] * h[k]
end
T, φ = param(m.total, p, t), param(fraction, p, t)
cu, ch = _import_weights(T, φ, su, sh, ε)
mh = max(sh, ε)
r = sh / mh
au /= max(su, ε)
ah /= mh
add_param!(T̄, m.total, (1 - φ * r) * au + φ * ah, p, t)
add_param!(φ̄, fraction, T * (ah - r * au), p, t)
on = su >= ε
# Below the floor `h` moves the share through `r`, not through `mh`.
bh = sh > ε ? ah : au
for k in zs
ȳk = v̄[k]
h̄[k] += ch * (ȳk - bh)
v̄[k] = cu * (on ? ȳk - au : ȳk)
end
end
r̄, P̄ = cotangent(īm, :spill), cotangent(īm, :pattern)
_spill_back!(spill, r̄, v̄, s̄, v, t, _field(pass, :vc), pass)
_pattern_back!(pattern, P̄, h̄, v̄, v, t, _field(pass, :hb))
fill!(h̄, zero(eltype(h̄)))
return nothing
return _imports_pullback!(_host_imports_back!, grads, m, v, s, t)
end

# On a device, through a method of `pullback!` on device arrays in the
# GPUArrays extension, so the host method above holds no device code:
# the same steps, with each group one kernel index.
# GPUArrays extension, so the host method above holds no device code.

This comment was marked as resolved.

@seabbs-bot seabbs-bot Dec 2, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 147bd8e: the comment now names the device entry point, and the helpers are separated by blank lines.
Comment and whitespace only; no code moved.


Generated by Claude Code

function _device_imports_pullback!(grads, m, v, s, t)
(; fraction, pattern, spill) = m.imports
return _imports_pullback!(_device_imports_back!, grads, m, v, s, t)
end
# The step's pullback, with `groups_back!` the pass over the groups.
function _imports_pullback!(groups_back!::F, grads, m, v, s, t) where {F}
(; pattern, spill) = m.imports
v̄, s̄ = grads.v, grads.s
S = length(v)
h̄ = view(s̄, (length(s̄) - S + 1):length(s̄))
īm = cotangent(grads.piece, :imports)
T̄, φ̄ = cotangent(grads.piece, :total), cotangent(īm, :fraction)
u, h = _spilled_and_pattern(_field(grads, :after), spill, pattern, v, t)
pass = _field(grads, :pass)
_device_imports_back!(T̄, φ̄, m, v̄, h̄, u, h, t, eps(eltype(v)))
groups_back!(T̄, φ̄, m, v̄, h̄, u, h, t, eps(eltype(v)))
r̄, P̄ = cotangent(īm, :spill), cotangent(īm, :pattern)
_spill_back!(spill, r̄, v̄, s̄, v, t, _field(pass, :vc), pass)
_pattern_back!(pattern, P̄, h̄, v̄, v, t, _field(pass, :hb))
fill!(h̄, zero(eltype(h̄)))
return nothing
end
function _host_imports_back!(T̄, φ̄, m, v̄, h̄, u, h, t, ε)
(; total, imports) = m
for p in 1:_ngroups(m)
zs = _group(m, p)
_import_back!(T̄, φ̄, total, imports.fraction, v̄, h̄, u, h, zs, p, t, ε)
end
return nothing
end

# Group `p`'s pullback over its strata `zs`, which no other group writes,
# with the floor `ε` of the state's element type: the host loop's body, as a
# device kernel index runs it.
# with the floor `ε` of the state's element type.
@inline function _import_back!(
T̄, φ̄, total, fraction, v̄, h̄, u, h, zs, p, t, ε
)
Expand Down
9 changes: 2 additions & 7 deletions src/modifiers/builtin.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1045,10 +1045,7 @@ function pullback!(grads, m::Redistribute, ::Step, v, s, t)
_on_gpu(v) && _device_modifier(m) &&
return _device_redistribute_back!(M̄, M, v̄, s̄, v, t)
for p in eachindex(v, v̄, s̄)
s̄p = s̄[p]
s̄[p] = v̄[p] + s̄p
v̄[p] = -_diag_at(M, p, t) * s̄p
_add_diag!(M̄, M, -s̄p * v[p], p, t)
_redistribute_back_body!(p, M̄, M, v̄, s̄, v, t)
end
pullback!((; piece = M̄, q = s̄, p = v̄), M, Pressure(), s̄, v, t)
fill!(s̄, zero(eltype(s̄)))
Expand Down Expand Up @@ -1097,9 +1094,7 @@ function pullback!(
# `v̄` holds `s̄′` until its column is done, which reads it for the
# diagonal.
for p in eachindex(v, v̄, s̄)
d = s̄[p]
s̄[p] = v̄[p] + d
v̄[p] = d
_movement_swap!(p, v̄, s̄)
end
for b in axes(K, 2)
e = _intensity(E, b, t)
Expand Down
Loading