Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
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 = "0.7.62"
version = "0.7.63"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand Down
17 changes: 12 additions & 5 deletions src/rulesets/Base/mapreduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ function rrule(::typeof(sum), x::AbstractArray{T}; dims=:) where {T<:Number}
y = sum(x; dims=dims)
function sum_pullback(ȳ)
# broadcasting the two works out the size no-matter `dims`
x̄ = broadcast(x, ȳ) do xi, ȳi
ȳi
end
x̄ = InplaceableThunk(
@thunk(broadcast(last∘tuple, x, ȳ)),
x -> x .+= ȳ
Copy link
Member

Choose a reason for hiding this comment

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

This reminded me we to do JuliaDiff/ChainRulesCore.jl#348
but i think even once that is merged this is still better (also that will not always work if dims are different, which maybe should change JuliaDiff/ChainRulesCore.jl#349)

)
return (NO_FIELDS, x̄)
end
return y, sum_pullback
Expand All @@ -29,7 +30,9 @@ function frule(
∂y = if dims isa Colon
2 * real(dot(x, ẋ))
elseif VERSION ≥ v"1.2" # multi-iterator mapreduce introduced in v1.2
2 * mapreduce(_realconjtimes, +, x, ẋ; dims=dims)
mapreduce(+, x, ẋ; dims=dims) do xi, dxi
2 * _realconjtimes(xi, dxi)
end
else
2 * sum(_realconjtimes.(x, ẋ); dims=dims)
end
Expand All @@ -44,7 +47,11 @@ function rrule(
) where {T<:Union{Real,Complex}}
y = sum(abs2, x; dims=dims)
function sum_abs2_pullback(ȳ)
return (NO_FIELDS, DoesNotExist(), 2 .* real.(ȳ) .* x)
x_thunk = InplaceableThunk(
@thunk(2 .* real.(ȳ) .* x),
dx -> dx .+= 2 .* real.(ȳ) .* x
)
return (NO_FIELDS, DoesNotExist(), x_thunk)
end
return y, sum_abs2_pullback
end