Skip to content
Open
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 = "1.74.8"
version = "1.74.9"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
4 changes: 3 additions & 1 deletion src/rulesets/Base/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,9 @@ _ndims(::Tuple) = 1

function unbroadcast(x::T, dx_raw) where {T<:Tuple{Vararg{Any,N}}} where {N}
dx = unthunk(dx_raw)
val = if N == length(dx)
# A tuple of length N ≥ 2 fills the first dimension, so a vector `dx` already has length N.
# Checking this statically keeps inference from compiling the (dead) reduction below.
val = if dx isa AbstractVector || N == length(dx)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What about dispatching on ndims instead?

function unbroadcast(x::T, dx_raw) where {T<:Tuple{Vararg{Any,N}}} where {N}
    val = _sum_trailing_dims(unthunk(dx_raw))
    eltype(val) <: AbstractZero && return NoTangent()
    return ProjectTo(x)(Tuple{Vararg{Any,N}}(val)) # Tangent
end
unbroadcast(x::Tuple, dx::AbstractZero) = dx

# A tuple only spans the first dimension of a broadcast, so only trailing dimensions need summing.
_sum_trailing_dims(dx::AbstractArray) = ndims(dx) == 1 ? dx : sum(dx; dims=2:ndims(dx))
_sum_trailing_dims(dx) = dx  # e.g. `Tuple`, `Tangent{<:Any,<:Tuple}`

This avoids the reduction for vectors, tuples and Tangents but still sums over trailing dimensions for matrices. I checked locally that it gives the same results as main (vectors, Vector{Any}, tuples, Tangents, N×k, N×1 and 3d arrays, thunks, zero cotangents), and the first call is as fast as with this PR.

dx
else
sum(dx; dims=2:ndims(dx))
Expand Down
6 changes: 6 additions & 0 deletions test/rulesets/Base/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,12 @@ BT1 = Broadcast.BroadcastStyle(Tuple)
@test ChainRules.unbroadcast((1, 2, [3]), [4, 5, [6]]) isa Tangent # earlier, NTuple demanded same type
@test ChainRules.unbroadcast(broadcasted(-, (1, 2), 3), (4, 5)) == (4, 5) # earlier, called ndims(::Tuple)
end
@testset "unbroadcast tuple with vector cotangent" begin
dx = Tangent{Tuple{Float64, Float64}}(3.0, 4.0)
@test ChainRules.unbroadcast((1.0, 2.0), [3.0, 4.0]) == dx
@test ChainRules.unbroadcast((1.0, 2.0), Any[3.0, 4.0]) == dx # abstract eltype, reduction not compiled

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The test doesn't check that the reduction is not compiled, it only compares values. Can you remove the comment, or test it?

@test ChainRules.unbroadcast((1.0, 2.0), [1.0 2.0; 1.5 2.5]) == dx # trailing dimensions still summed
end
@testset "unbroadcast with Matrix{Tangent}" begin # https://github.com/JuliaDiff/ChainRules.jl/issues/708
x = Base.Fix1.(*, 1:3.0)
dx1 = [Tangent{Base.Fix1}(; x = i/2) for i in 1:3, _ in 1:1]
Expand Down
Loading