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
Expand Up @@ -106,4 +106,4 @@ Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"

[targets]
test = ["Aqua", "FastBroadcast", "ForwardDiff", "KernelAbstractions", "Measurements", "Mooncake", "NLsolve", "Pkg", "Polyester", "Random", "SafeTestsets", "SparseArrays", "StaticArrays", "Statistics", "StructArrays", "Tables", "Test", "Unitful", "Zygote"]
test = ["Aqua", "FastBroadcast", "ForwardDiff", "KernelAbstractions", "Measurements", "Mooncake", "NLsolve", "Pkg", "Polyester", "Random", "ReverseDiff", "SafeTestsets", "SparseArrays", "StaticArrays", "Statistics", "StructArrays", "Tables", "Test", "Unitful", "Zygote"]
10 changes: 7 additions & 3 deletions src/vector_of_array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1137,10 +1137,14 @@ vecarr_to_vectors(VA::AbstractVectorOfArray) = [VA[i, :] for i in eachindex(VA.u
# linear algebra
ArrayInterface.issingular(va::AbstractVectorOfArray) = ArrayInterface.issingular(Matrix(va))

# Type-stable sum/mapreduce that avoids inference issues on Julia 1.10
# with deeply nested VectorOfArray type parameters
# The `::` assertion pins the return type so Julia 1.10's inference does not bail
# (widening to `Any`) on the self-recursion of `sum(::AbstractVectorOfArray)` for
# deeply nested VectorOfArrays. It must be the type `sum` actually produces -- the
# `add_sum` accumulator type -- and NOT the element type `T`, which over-constrains
# the result for promoting eltypes (`Int8` -> `Int64`) and for AD scalars whose
# summation drops metadata (e.g. `ReverseDiff.TrackedReal` origin); see issue #595.
function Base.sum(VA::AbstractVectorOfArray{T}) where {T}
return sum(sum, VA.u)::T
return sum(sum, VA.u)::Base.promote_op(Base.add_sum, T, T)
end

function Base.sum(f::F, VA::AbstractVectorOfArray{T}) where {F, T}
Expand Down
11 changes: 10 additions & 1 deletion test/adjoints.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using RecursiveArrayTools, Zygote, ForwardDiff, Test
using RecursiveArrayTools, Zygote, ForwardDiff, ReverseDiff, Test

function loss(x)
return sum(abs2, Array(VectorOfArray([x .* i for i in 1:5])))
Expand Down Expand Up @@ -90,3 +90,12 @@ voa_gs, = Zygote.gradient(voa) do x
sum(sum.(x.u))
end
@test voa_gs isa RecursiveArrayTools.VectorOfArray

# issue #595: `sum(::VectorOfArray)` must not assert the element type, which
# discarded ReverseDiff's `TrackedReal` origin and threw a `TypeError`.
let g = ReverseDiff.gradient(x -> sum(VectorOfArray([x])), float.(1:3))
@test g ≈ ones(3)
end
let g = ReverseDiff.gradient(x -> sum(VectorOfArray([VectorOfArray([x])])), float.(1:3))
@test g ≈ ones(3)
end
7 changes: 7 additions & 0 deletions test/interface_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ push!(testda, [-1, -2, -3, -4])
@inferred sum(testva)
@inferred sum(VA[VA[zeros(4, 4)]])
@inferred mapreduce(string, *, testva)

# sum must follow Base's promotion, not the element type (issue #595):
# `Int8` elements sum to `Int64`, so asserting the result is `::Int8` is wrong.
@test sum(VA[Int8[1, 2, 3]]) === Int64(6)
@test sum(VA[Int8[1, 2], Int8[3, 4]]) === Int64(10)
@test sum(VA[Float32[1, 2, 3]]) === 6.0f0
@inferred sum(VA[Int8[1, 2, 3]])
# Type stability for `end` indexing (issue #525)
testva_end = VectorOfArray(fill(fill(2.0, 2), 10))
# Use lastindex directly since `end` doesn't work in SafeTestsets
Expand Down
Loading