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

unbroadcast for tuples #977

Merged
merged 2 commits into from
May 22, 2021
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: 2 additions & 0 deletions src/lib/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ function Base.reducedim_init(::typeof(identity), ::typeof(accum), A::AbstractArr
end

trim(x, Δ) = reshape(Δ, ntuple(i -> size(Δ, i), Val(ndims(x))))
trim(x::Tuple, Δ) = ntuple(k -> Δ[k], length(x))

unbroadcast(x::AbstractArray, x̄) =
size(x) == size(x̄) ? x̄ :
Expand All @@ -55,6 +56,7 @@ unbroadcast(x::AbstractArray, x̄) =
unbroadcast(x::Number, x̄) = accum_sum(x̄)
unbroadcast(x::Tuple{<:Any}, x̄) = (accum_sum(x̄),)
unbroadcast(x::Base.RefValue, x̄) = (x=accum_sum(x̄),)
unbroadcast(x::Tuple, x̄) = trim(x, length(x) == length(x̄) ? x̄ : accum_sum(x̄; dims=2:ndims(x̄))) # case length(x) > 1
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if we can use dispatch on abstractarrays instead of this branching

Copy link
Member Author

Choose a reason for hiding this comment

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

What this should avoid is summing a 9x1 matrix over the 2nd dimension, and hence making a copy. whether this can plausibly matter for arrays short enough to become tuples, I don't know. The function does the same thing for arrays (just above) where it can matter:

julia> @btime sum($(rand(100,1,100)); dims=2);
  3.713 μs (8 allocations: 78.31 KiB)

julia> @btime sum($(rand(100,2,100)); dims=2);
  5.229 μs (8 allocations: 78.31 KiB)

julia> @btime reshape($(rand(100,1,100)), (100,100));
  17.117 ns (1 allocation: 64 bytes)


unbroadcast(x::AbstractArray, x̄::Nothing) = nothing

Expand Down
12 changes: 12 additions & 0 deletions test/features.jl
Original file line number Diff line number Diff line change
Expand Up @@ -481,3 +481,15 @@ end
Zygote.gradient(loss_adjoint,[1.0])
@test x[1] == x[2]
end

@testset "tuples & broadcasting" begin
@test gradient(x -> sum(x .+ ones(2,2)), (1,2)) == ((2,2),)
@test gradient(x -> sum(x .+ ones(2,2)), (1,)) == ((4,),)
@test gradient(x -> sum(x .+ ones(2,1)), (1,2)) == ((1,1),)

# https://github.com/FluxML/Zygote.jl/issues/975
gt = gradient((x,p) -> prod(x .^ p), [3,4], (1,2))
gv = gradient((x,p) -> prod(x .^ p), [3,4], [1,2])
@test gt[1] == gv[1]
@test collect(gt[2]) ≈ gv[2]
end