Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove redundant sum() rules #1453

Merged
merged 9 commits into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
11 changes: 0 additions & 11 deletions src/lib/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -329,17 +329,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 @@ -364,11 +364,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
4 changes: 2 additions & 2 deletions test/lib/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,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)

mcabbott marked this conversation as resolved.
Show resolved Hide resolved

w = randn(5)
Expand Down