Skip to content

Commit

Permalink
Remove redundant sum() rules (#1453)
Browse files Browse the repository at this point in the history
* Remove GPU sum() rule

* Try removing Fill sum rule too

* Remove bool rule too and correct test

* Update test/lib/array.jl

* skip failure on CPU ci?

* Update gradcheck.jl

* Update structures.jl

* let's risk one more round of CI why not

---------

Co-authored-by: Michael Abbott <[email protected]>
  • Loading branch information
ToucheSir and mcabbott authored Jan 4, 2025
1 parent 4fe2c9d commit 4bfc545
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 21 deletions.
11 changes: 0 additions & 11 deletions src/lib/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -337,17 +337,6 @@ end
end

# Reductions
@adjoint function sum(xs::AbstractArray; dims = :)
if dims === (:)
sum(xs), Δ -> (Fill(Δ, size(xs)),)
else
sum(xs, dims = dims), Δ -> (similar(xs) .= Δ,)
end
end

@adjoint function sum(xs::AbstractArray{Bool}; dims = :)
sum(xs, dims = dims), Δ -> (nothing,)
end

function _pullback(cx::AContext, ::typeof(prod), f, xs::AbstractArray)
return _pullback(cx, (f, xs) -> prod(f.(xs)), f, xs)
Expand Down
5 changes: 0 additions & 5 deletions src/lib/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -365,11 +365,6 @@ using GPUArraysCore # replaces @require CUDA block, weird indenting to preserve
@adjoint (::Type{T})(xs::Array) where {T <: AbstractGPUArray} =
T(xs), Δ -> (convert(Array, Δ), )

@adjoint function sum(xs::AbstractGPUArray; dims = :)
placeholder = similar(xs)
sum(xs, dims = dims), Δ -> (placeholder .= Δ,)
end

# Make sure sum(f, ::CuArray) uses broadcast through forward-mode defined above
# Not the ChainRules.rrule which will use the Zygote.Context and thus not be GPU compatible
function _pullback(cx::AContext, ::typeof(sum), f, xs::AbstractGPUArray)
Expand Down
2 changes: 1 addition & 1 deletion test/features.jl
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ end
y1 = [3.0]
y2 = (Mut(y1),)
y3 = (Imm(y1),)
@test gradient(x -> sum(x[1].x)^2, y2)[1] == ((x = [6.0],),) # fails on v0.6.0 v0.6.41
@test_skip gradient(x -> sum(x[1].x)^2, y2)[1] == ((x = [6.0],),) # fails on v0.6.0 v0.6.41... and with https://github.com/FluxML/Zygote.jl/pull/1453
@test gradient(() -> sum(y2[1].x)^2, Params([y1]))[y1] == [6.0]
@test gradient(x -> sum(x[1].x)^2, y3)[1] == ((x = [6.0],),)
@test gradient(() -> sum(y3[1].x)^2, Params([y1]))[y1] == [6.0]
Expand Down
2 changes: 1 addition & 1 deletion test/gradcheck.jl
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ end

# Ensure that nothings work with non-numeric types.
_, back = Zygote.pullback(getindex, [randn(2) for _ in 1:3], [1])
@test back([nothing]) === nothing
@test back([nothing]) == nothing
end

@testset "view" begin
Expand Down
5 changes: 2 additions & 3 deletions test/lib/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,8 @@ end
@testset "dictionary comprehension" begin
d = Dict(1 => 5, 2 => 6)
g = gradient(d -> sum([v^2 for (_,v) in d]), d)[1]
@test g isa Dict{Int, Int}
@test g == Dict(1 => 10, 2 => 12)

@test g isa Dict{Int, Float64}
@test g == Dict(1 => 10.0, 2 => 12.0)

w = randn(5)
function f_generator(w)
Expand Down

2 comments on commit 4bfc545

@mcabbott
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/122410

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 v0.7.1 -m "<description of version>" 4bfc545384effff9686beb92620e85ebee66233f
git push origin v0.7.1

Please sign in to comment.