Skip to content

Commit

Permalink
Improve rules for sum (#336)
Browse files Browse the repository at this point in the history
* tweaks to sum rules

* Apply suggestions from code review

Co-authored-by: Lyndon White <oxinabox@ucc.asn.au>

* comma

* v0.7.62

* gone cold one more time...

Co-authored-by: David Widmann <devmotion@users.noreply.github.com>

Co-authored-by: Michael Abbott <me@escbook>
Co-authored-by: Lyndon White <oxinabox@ucc.asn.au>
Co-authored-by: David Widmann <devmotion@users.noreply.github.com>
  • Loading branch information
4 people authored May 7, 2021
1 parent daff86a commit 987ee45
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 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 = "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`
= broadcast(x, ȳ) do xi, ȳi
ȳi
end
= InplaceableThunk(
@thunk(broadcast(lasttuple, x, ȳ)),
x -> x .+=
)
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

2 comments on commit 987ee45

@oxinabox
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/36286

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.63 -m "<description of version>" 987ee4506ea928e6bab51a7fd5223082be1c141e
git push origin v0.7.63

Please sign in to comment.