Skip to content

Commit 6e23e61

Browse files
committed
Unthunk each element in ∇eachslice
1 parent e055009 commit 6e23e61

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

src/rulesets/Base/indexing.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,20 +262,22 @@ end
262262
# Using Val(dim) here is worth a factor of 2 in this, on Julia 1.8-
263263
# @btime rrule(eachcol, $([1 2; 3 4]))[2]($([[10, 20], [30, 40]]))
264264
function ∇eachslice(dys_raw, x::AbstractArray, vd::Val{dim}) where {dim}
265-
dys = unthunk(dys_raw)
265+
dys = unthunk.(unthunk(dys_raw))
266266
i1 = findfirst(dy -> dy isa AbstractArray, dys)
267267
if i1 === nothing # all slices are Zero!
268268
return _zero_fill!(similar(x, float(eltype(x)), axes(x)))
269269
end
270+
270271
T = Base.promote_eltype(dys...)
271272
# The whole point of this gradient is that we can allocate one `dx` array:
272273
dx = similar(x, T, axes(x))
273274
for i in axes(x, dim)
274275
slice = selectdim(dx, dim, i)
275-
if dys[i] isa AbstractZero
276+
dy = dys[i]
277+
if dy isa AbstractZero
276278
_zero_fill!(slice) # Avoids this: copyto!([1,2,3], ZeroTangent()) == [0,2,3]
277279
else
278-
copyto!(slice, dys[i])
280+
copyto!(slice, dy)
279281
end
280282
end
281283
return ProjectTo(x)(dx)

0 commit comments

Comments
 (0)