Skip to content

Commit

Permalink
Fix computing of array element type for ∇eachslice (#808)
Browse files Browse the repository at this point in the history
  • Loading branch information
BioTurboNick authored Sep 18, 2024
1 parent 30f9b12 commit e245d50
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ concurrency:
jobs:
format:
runs-on: ubuntu-latest
permissions:
contents: read
checks: write
pull-requests: write
steps:
- uses: actions/checkout@v4
- uses: julia-actions/setup-julia@latest
Expand All @@ -22,6 +26,7 @@ jobs:
julia -e 'using JuliaFormatter; format("."; verbose=true)'
- uses: reviewdog/action-suggester@v1
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
tool_name: JuliaFormatter
fail_on_error: true
filter_mode: added
4 changes: 2 additions & 2 deletions 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.70.0"
version = "1.71.0"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand All @@ -20,7 +20,7 @@ SuiteSparse = "4607b0f0-06f3-5cda-b6b1-a6196a1729e9"

[compat]
Adapt = "3.4.0, 4"
ChainRulesCore = "1.20"
ChainRulesCore = "1.25"
ChainRulesTestUtils = "1.5"
Compat = "3.46, 4.2"
Distributed = "1"
Expand Down
5 changes: 2 additions & 3 deletions src/rulesets/Base/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ function ∇eachslice(dys_raw, x::AbstractArray, vd::Val{dim}) where {dim}
if i1 === nothing # all slices are Zero!
return _zero_fill!(similar(x, float(eltype(x)), axes(x)))
end
T = promote_type(eltype(dys[i1]), eltype(x))
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)
Expand All @@ -282,8 +282,7 @@ function ∇eachslice(dys_raw, x::AbstractArray, vd::Val{dim}) where {dim}
end
∇eachslice(dys::AbstractZero, x::AbstractArray, vd::Val{dim}) where {dim} = dys

_zero_fill!(dx::AbstractArray{<:Number}) = fill!(dx, zero(eltype(dx)))
_zero_fill!(dx::AbstractArray) = map!(zero, dx, dx)
_zero_fill!(dx::AbstractArray) = fill!(dx, zero(eltype(dx)))

function rrule(::typeof(∇eachslice), dys, x, vd::Val)
function ∇∇eachslice(dz_raw)
Expand Down
36 changes: 29 additions & 7 deletions test/rulesets/Base/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -217,16 +217,24 @@ end
# DimensionMismatch("second dimension of A, 6, does not match length of x, 5")
# Probably similar to https://github.com/JuliaDiff/ChainRulesTestUtils.jl/issues/234 (about Broadcasted not Generator)

test_rrule(collecteachrow, rand(5))
test_rrule(collecteachrow, rand(3, 4))
# Inference on 1.6 sometimes fails, so don't enforce there.
test_rrule(collect eachrow, rand(5); check_inferred=(VERSION >= v"1.7"))
test_rrule(collect eachrow, rand(3, 4); check_inferred=(VERSION >= v"1.7"))

test_rrule(collecteachcol, rand(3, 4))
@test_skip test_rrule(collecteachcol, Diagonal(rand(5))) # works locally!
test_rrule(collect eachcol, rand(3, 4); check_inferred=(VERSION >= v"1.7"))
@test_skip test_rrule(collect eachcol, Diagonal(rand(5))) # works locally!

if VERSION >= v"1.7"
# On 1.6, ComposedFunction doesn't take keywords. Only affects this testing strategy, not real use.
test_rrule(collecteachslice, rand(3, 4, 5); fkwargs = (; dims = 3))
test_rrule(collecteachslice, rand(3, 4, 5); fkwargs = (; dims = (2,)))
test_rrule(collect eachslice, rand(3, 4, 5); fkwargs=(; dims=3))
test_rrule(collect eachslice, rand(3, 4, 5); fkwargs=(; dims=(2,)))

test_rrule(
collect eachslice,
FooTwoField.(rand(3, 4, 5), rand(3, 4, 5));
check_inferred=false,
fkwargs=(; dims=3),
)
end

# Make sure pulling back an array that mixes some AbstractZeros in works right
Expand All @@ -235,8 +243,22 @@ end
@test back([1:3, ZeroTangent(), 7:9, NoTangent()])[2] isa Matrix{Float64}
@test back([ZeroTangent(), ZeroTangent(), NoTangent(), NoTangent()]) == (NoTangent(), [0 0 0 0; 0 0 0 0; 0 0 0 0])

_, back = ChainRules.rrule(
eachslice, FooTwoField.(rand(2, 3, 2), rand(2, 3, 2)); dims=3
)
@test back([fill(Tangent{Any}(; x=0.0, y=1.0), 2, 3), fill(ZeroTangent(), 2, 3)]) == (
NoTangent(),
cat(fill(Tangent{Any}(; x=0.0, y=1.0), 2, 3), fill(ZeroTangent(), 2, 3); dims=3),
)

# Second derivative rule
test_rrule(ChainRules.∇eachslice, [rand(4) for _ in 1:3], rand(3, 4), Val(1))
test_rrule(ChainRules.∇eachslice, [rand(3) for _ in 1:4], rand(3, 4), Val(2))
test_rrule(ChainRules.∇eachslice, [rand(2, 3) for _ in 1:4], rand(2, 3, 4), Val(3), check_inferred=false)
test_rrule(
ChainRules.∇eachslice,
[rand(2, 3) for _ in 1:4],
rand(2, 3, 4),
Val(3);
check_inferred=(VERSION >= v"1.7"),
)
end

2 comments on commit e245d50

@simeonschaub
Copy link
Member

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/115427

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.71.0 -m "<description of version>" e245d50a1ae56ce46fc8c1f0fe9b925964f1146e
git push origin v1.71.0

Please sign in to comment.