Skip to content

Commit

Permalink
Merge pull request #814 from pxl-th/pxl-th/eachslice
Browse files Browse the repository at this point in the history
Unthunk each element in `∇eachslice`
  • Loading branch information
oxinabox authored Jan 1, 2025
2 parents e055009 + f9754c1 commit 02e21ba
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "ChainRules"
uuid = "082447d4-558c-5d27-93f4-14fc19e9eca2"
version = "1.72.1"
version = "1.72.2"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
8 changes: 5 additions & 3 deletions src/rulesets/Base/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -262,20 +262,22 @@ end
# Using Val(dim) here is worth a factor of 2 in this, on Julia 1.8-
# @btime rrule(eachcol, $([1 2; 3 4]))[2]($([[10, 20], [30, 40]]))
function ∇eachslice(dys_raw, x::AbstractArray, vd::Val{dim}) where {dim}
dys = unthunk(dys_raw)
dys = unthunk.(unthunk(dys_raw))
i1 = findfirst(dy -> dy isa AbstractArray, dys)
if i1 === nothing # all slices are Zero!
return _zero_fill!(similar(x, float(eltype(x)), axes(x)))
end

T = Base.promote_eltype(dys...)
# The whole point of this gradient is that we can allocate one `dx` array:
dx = similar(x, T, axes(x))
for i in axes(x, dim)
slice = selectdim(dx, dim, i)
if dys[i] isa AbstractZero
dy = dys[i]
if dy isa AbstractZero
_zero_fill!(slice) # Avoids this: copyto!([1,2,3], ZeroTangent()) == [0,2,3]
else
copyto!(slice, dys[i])
copyto!(slice, dy)
end
end
return ProjectTo(x)(dx)
Expand Down
13 changes: 13 additions & 0 deletions test/rulesets/Base/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -261,4 +261,17 @@ end
Val(3);
check_inferred=(VERSION >= v"1.7"),
)

# eachslice: Make sure pulling back an array of thunks unthunks them and does not return all zeros.
x = ones(Float32, 3)
Δ = ones(Float32, 1)
_, norm_back = ChainRules.rrule(norm, x)
dx = norm_back(Δ)[2]
@test dx isa AbstractThunk

x = ones(Float32, 3, 1)
_, eachcol_back = ChainRules.rrule(eachcol, x)
Δ2 = [dx]
dx2 = eachcol_back(Δ2)[2]
@test all(dx2 .≉ 0f0)
end

2 comments on commit 02e21ba

@oxinabox
Copy link
Member Author

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/122237

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.72.2 -m "<description of version>" 02e21bacd8cb0aec3e80678ec2d0e1e1a964e9e3
git push origin v1.72.2

Please sign in to comment.