Skip to content

Commit

Permalink
Explicitly unthunk in a few rules (#670)
Browse files Browse the repository at this point in the history
* unthunk in rrule for +

* unthunk in unbroadcast

* v1.44.5
  • Loading branch information
mcabbott authored Aug 25, 2022
1 parent 39c2d17 commit 13ccc86
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 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 = "1.44.4"
version = "1.44.5"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
3 changes: 2 additions & 1 deletion src/rulesets/Base/arraymath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,8 @@ frule((_, ΔAs...), ::typeof(+), As::AbstractArray...) = +(As...), +(ΔAs...)
function rrule(::typeof(+), arrs::AbstractArray...)
y = +(arrs...)
arr_axs = map(axes, arrs)
function add_pullback(dy)
function add_pullback(dy_raw)
dy = unthunk(dy_raw) # reshape will otherwise unthunk N times
return (NoTangent(), map(ax -> reshape(dy, ax), arr_axs)...)
end
return y, add_pullback
Expand Down
6 changes: 4 additions & 2 deletions src/rulesets/Base/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,8 @@ rrule(::typeof(broadcasted), ::typeof(complex), x::Number) = rrule(complex, x) |
# When sizes disagree, broadcasting gradient uses `unbroadcast` to reduce to correct shape.
# It's sometimes a little wasteful to allocate a too-large `dx`, but difficult to make more efficient.

function unbroadcast(x::Base.AbstractArrayOrBroadcasted, dx)
function unbroadcast(x::Base.AbstractArrayOrBroadcasted, dx_raw)
dx = unthunk(dx_raw)
N = ndims(dx)
if length(x) == length(dx)
ProjectTo(x)(dx) # handles trivial reshapes, offsets, structured matrices, row vectors
Expand All @@ -327,7 +328,8 @@ function unbroadcast(x::Base.AbstractArrayOrBroadcasted, dx)
end
unbroadcast(x::Base.AbstractArrayOrBroadcasted, dx::AbstractZero) = dx

function unbroadcast(x::T, dx) where {T<:Tuple{Vararg{Any,N}}} where {N}
function unbroadcast(x::T, dx_raw) where {T<:Tuple{Vararg{Any,N}}} where {N}
dx = unthunk(dx_raw)
val = if N == length(dx)
dx
else
Expand Down

2 comments on commit 13ccc86

@mcabbott
Copy link
Member Author

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

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.44.5 -m "<description of version>" 13ccc862899d8a3d98b09bd68edd9be8ca28197e
git push origin v1.44.5

Please sign in to comment.