diff --git a/Project.toml b/Project.toml index 6898fb8b..91a8d1ae 100644 --- a/Project.toml +++ b/Project.toml @@ -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"] diff --git a/src/vector_of_array.jl b/src/vector_of_array.jl index 3db5b8ca..ac673613 100644 --- a/src/vector_of_array.jl +++ b/src/vector_of_array.jl @@ -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} diff --git a/test/adjoints.jl b/test/adjoints.jl index fec7f548..4e60c108 100644 --- a/test/adjoints.jl +++ b/test/adjoints.jl @@ -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]))) @@ -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 diff --git a/test/interface_tests.jl b/test/interface_tests.jl index 91a68a76..694334fc 100644 --- a/test/interface_tests.jl +++ b/test/interface_tests.jl @@ -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