From dfce98c5da701a78939b751a0e134dec3e2b42b0 Mon Sep 17 00:00:00 2001 From: Codex Date: Sat, 17 Oct 2026 22:01:15 +0000 Subject: [PATCH 1/3] Fix dimension handling in VectorOfArray broadcasting Slice operands using the destination dimension and preserve lower-dimensional arguments across inner arrays. Expand each inner broadcast to its destination axes, including static arrays and FastBroadcast extension paths. Co-Authored-By: Chris Rackauckas Co-Authored-By: Codex Agent-Harness: Codex CLI 0.162.1 Agent-Model: gpt-6.1-sol Agent-Session: 01a14bd6-bd53-7dd2-94c0-6f6613286f91 --- ext/RecursiveArrayToolsFastBroadcastExt.jl | 5 +- ...siveArrayToolsFastBroadcastPolyesterExt.jl | 15 +++-- ext/RecursiveArrayToolsStructArraysExt.jl | 9 ++- src/vector_of_array.jl | 39 +++++++------ test/Core/voa_broadcast_dimensions.jl | 55 +++++++++++++++++++ test/runtests.jl | 1 + 6 files changed, 97 insertions(+), 27 deletions(-) create mode 100644 test/Core/voa_broadcast_dimensions.jl diff --git a/ext/RecursiveArrayToolsFastBroadcastExt.jl b/ext/RecursiveArrayToolsFastBroadcastExt.jl index f8d3ce3d..8d7b9c31 100644 --- a/ext/RecursiveArrayToolsFastBroadcastExt.jl +++ b/ext/RecursiveArrayToolsFastBroadcastExt.jl @@ -15,7 +15,10 @@ const AbstractVectorOfSArray = AbstractVectorOfArray{ ) where {S} if FastBroadcast.use_fast_broadcast(S) for i in 1:length(dst.u) - unpacked = RecursiveArrayTools.unpack_voa(bc, i) + inner_bc = RecursiveArrayTools.unpack_voa(bc, i, Val(ndims(dst))) + unpacked = Broadcast.instantiate( + Broadcast.Broadcasted(inner_bc.f, inner_bc.args, axes(dst.u[i])) + ) dst.u[i] = StaticArraysCore.similar_type(dst.u[i])( unpacked[j] for j in eachindex(unpacked) diff --git a/ext/RecursiveArrayToolsFastBroadcastPolyesterExt.jl b/ext/RecursiveArrayToolsFastBroadcastPolyesterExt.jl index 2e6c2fc2..fa1e74ef 100644 --- a/ext/RecursiveArrayToolsFastBroadcastPolyesterExt.jl +++ b/ext/RecursiveArrayToolsFastBroadcastPolyesterExt.jl @@ -10,17 +10,22 @@ const AbstractVectorOfSArray = AbstractVectorOfArray{ T, N, <:AbstractVector{<:StaticArraysCore.SArray}, } where {T, N} +# Keep the generator outside @batch so its closure captures only the inner broadcast. +@inline function materialize_inner(inner, bc, i, n) + inner_bc = RecursiveArrayTools.unpack_voa(bc, i, n) + unpacked = Broadcast.instantiate( + Broadcast.Broadcasted(inner_bc.f, inner_bc.args, axes(inner)) + ) + return StaticArraysCore.similar_type(inner)(unpacked[j] for j in eachindex(unpacked)) +end + @inline function _polyester_fast_materialize!( dst::AbstractVectorOfSArray, bc::Broadcast.Broadcasted{S} ) where {S} if FastBroadcast.use_fast_broadcast(S) @batch for i in 1:length(dst.u) - unpacked = RecursiveArrayTools.unpack_voa(bc, i) - dst.u[i] = StaticArraysCore.similar_type(dst.u[i])( - unpacked[j] - for j in eachindex(unpacked) - ) + dst.u[i] = materialize_inner(dst.u[i], bc, i, Val(ndims(dst))) end else Broadcast.materialize!(dst, bc) diff --git a/ext/RecursiveArrayToolsStructArraysExt.jl b/ext/RecursiveArrayToolsStructArraysExt.jl index 866f0e22..3e51a3f1 100644 --- a/ext/RecursiveArrayToolsStructArraysExt.jl +++ b/ext/RecursiveArrayToolsStructArraysExt.jl @@ -42,9 +42,12 @@ for (type, N_expr) in [ dest_i = dest[:, i] if dest_i isa AbstractArray if ArrayInterface.ismutable(dest_i) - copyto!(dest_i, unpack_voa(bc, i)) + Broadcast.materialize!(dest_i, unpack_voa(bc, i, Val(ndims(dest)))) else - unpacked = unpack_voa(bc, i) + inner_bc = unpack_voa(bc, i, Val(ndims(dest))) + unpacked = Broadcast.instantiate( + Broadcast.Broadcasted(inner_bc.f, inner_bc.args, axes(dest_i)) + ) arr_type = StaticArraysCore.similar_type(dest_i) dest_i = if length(unpacked) == 1 && length(dest_i) == 1 arr_type(unpacked[1]) @@ -55,7 +58,7 @@ for (type, N_expr) in [ end end else - dest_i = copy(unpack_voa(bc, i)) + dest_i = copy(unpack_voa(bc, i, Val(ndims(dest)))) end dest[:, i] = dest_i end diff --git a/src/vector_of_array.jl b/src/vector_of_array.jl index 043d42ae..fed84b73 100644 --- a/src/vector_of_array.jl +++ b/src/vector_of_array.jl @@ -1646,11 +1646,11 @@ end # this is the default behavior in v3.15.0 N = narrays(bc) map(1:N) do i - copy(unpack_voa(bc, i)) + copy(unpack_voa(bc, i, Val(ndims(bc)))) end else # if parent isa AbstractArray map(enumerate(Iterators.product(axes(parent)...))) do (i, _) - copy(unpack_voa(bc, i)) + copy(unpack_voa(bc, i, Val(ndims(bc)))) end end return VectorOfArray(rewrap(parent, u)) @@ -1676,9 +1676,12 @@ for (type, N_expr) in [ inner = dest.u[i] if inner isa AbstractArray if ArrayInterface.ismutable(inner) - copyto!(inner, unpack_voa(bc, i)) + Broadcast.materialize!(inner, unpack_voa(bc, i, Val(ndims(dest)))) else - unpacked = unpack_voa(bc, i) + inner_bc = unpack_voa(bc, i, Val(ndims(dest))) + unpacked = Broadcast.instantiate( + Broadcast.Broadcasted(inner_bc.f, inner_bc.args, axes(inner)) + ) arr_type = StaticArraysCore.similar_type(inner) dest.u[i] = if length(unpacked) == 1 && length(inner) == 1 arr_type(unpacked[1]) @@ -1689,7 +1692,7 @@ for (type, N_expr) in [ end end else - dest.u[i] = copy(unpack_voa(bc, i)) + dest.u[i] = copy(unpack_voa(bc, i, Val(ndims(dest)))) end end return dest @@ -1724,24 +1727,24 @@ _narrays(args::AbstractVectorOfArray) = length(args.u) _narrays(args::Tuple{Any}) = _narrays(args[1]) _narrays(::Any) = 0 -# drop axes because it is easier to recompute -@inline function unpack_voa(bc::Broadcast.Broadcasted{Style}, i) where {Style} - return Broadcast.Broadcasted{Style}(bc.f, unpack_args_voa(i, bc.args)) +# Slice along the destination's outer dimension; lower-dimensional arguments +# broadcast unchanged across all inner arrays. +@inline function unpack_voa(bc::Broadcast.Broadcasted, i, n) + return Broadcast.Broadcasted(bc.f, unpack_args_voa(i, n, bc.args)) end -@inline function unpack_voa(bc::Broadcast.Broadcasted{<:VectorOfArrayStyle}, i) - return Broadcast.Broadcasted(bc.f, unpack_args_voa(i, bc.args)) +unpack_voa(x, ::Any, ::Any) = x +function unpack_voa(x::AbstractVectorOfArray{T, M}, i, ::Val{N}) where {T, M, N} + return M < N ? x : x.u[length(x.u) == 1 ? 1 : i] end -unpack_voa(x, ::Any) = x -unpack_voa(x::AbstractVectorOfArray, i) = x.u[i] -function unpack_voa(x::AbstractArray{T, N}, i) where {T, N} - return @view x[ntuple(x -> Colon(), N - 1)..., i] +function unpack_voa(x::AbstractArray{T, M}, i, ::Val{N}) where {T, M, N} + M < N && return x + return @view x[ntuple(_ -> Colon(), Val(N - 1))..., size(x, N) == 1 ? 1 : i] end -@inline function unpack_args_voa(i, args::Tuple) - return (unpack_voa(args[1], i), unpack_args_voa(i, Base.tail(args))...) +@inline function unpack_args_voa(i, n, args::Tuple) + return (unpack_voa(args[1], i, n), unpack_args_voa(i, n, Base.tail(args))...) end -unpack_args_voa(i, args::Tuple{Any}) = (unpack_voa(args[1], i),) -unpack_args_voa(::Any, args::Tuple{}) = () +unpack_args_voa(::Any, ::Any, args::Tuple{}) = () """ VA diff --git a/test/Core/voa_broadcast_dimensions.jl b/test/Core/voa_broadcast_dimensions.jl new file mode 100644 index 00000000..93976f6d --- /dev/null +++ b/test/Core/voa_broadcast_dimensions.jl @@ -0,0 +1,55 @@ +using Test, RecursiveArrayTools, StaticArrays, FastBroadcast, Polyester, StructArrays + +@testset "VectorOfArray broadcast dimensions" begin + for wrap in (identity, SVector{3}, MVector{3}), storage in (identity, StructArray) + original = [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]] + make() = VectorOfArray(storage(wrap.(deepcopy(original)))) + dense = Array(make()) + for x in ( + [10.0, 20.0, 30.0], [10.0 20.0], fill(2.0), 2.0, fill(2.0, 1, 1), + reshape(1.0:6.0, 3, 2), reshape([10.0, 20.0, 30.0], 3, 1), + ) + for add_destination in (true, false) + v = make() + expected = add_destination ? dense .+ x : x .+ 1 + expected = broadcast(+, zeros(size(dense)), expected) + if add_destination + v .= v .+ x + else + v .= x .+ 1 + end + @test Array(v) == expected + if wrap === SVector{3} && storage === identity + for threaded in (false, true) + v = make() + if add_destination + @.. thread = threaded v = v + x + else + @.. thread = threaded v = x + 1 + end + @test Array(v) == expected + end + end + end + end + v = make() + @test Array(v .+ fill(2.0)) == dense .+ 2 + end + for wrap in (identity, SMatrix{2, 3}) + make() = VectorOfArray( + [ + wrap(reshape(collect(1.0:6.0), 2, 3)), + wrap(reshape(collect(7.0:12.0), 2, 3)), + ] + ) + for x in ( + [10.0, 20.0], 10.0:10.0:20.0, reshape(1.0:6.0, 2, 3), + reshape(1.0:6.0, 2, 3, 1), reshape([10.0, 20.0], 1, 1, 2), fill(2.0), + ) + v = make() + expected = Array(v) .+ x + v .= v .+ x + @test Array(v) == expected + end + end +end diff --git a/test/runtests.jl b/test/runtests.jl index b50e7b45..225042fa 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -18,6 +18,7 @@ run_tests(; @time @safetestset "Partitions Tests" include("Core/partitions_test.jl") @time @safetestset "Partitions and StaticArrays Tests" include("Core/partitions_and_static_arrays.jl") @time @safetestset "VecOfArr Indexing Tests" include("Core/basic_indexing.jl") + @time @safetestset "VecOfArr Broadcast Dimensions" include("Core/voa_broadcast_dimensions.jl") @time @safetestset "VecOfArr Interface Tests" include("Core/interface_tests.jl") @time @safetestset "Public API Tests" include("Core/public_api_test.jl") @time @safetestset "Table traits" include("Core/tabletraits.jl") From 5c18e1d34b3dc9fbe303fdcca6bb51f91db96a1d Mon Sep 17 00:00:00 2001 From: ChrisRackauckas-Claude Date: Fri, 6 Nov 2026 13:20:07 -0500 Subject: [PATCH 2/3] Use field-backed mutable vectors in StructArray broadcast fixtures Construct the mutable StructArray fixture from SizedVector elements so it has an accessible field schema on Julia 1.10. Keep ordinary Vector storage and all 130 broadcast assertions, including the existing threaded scalar assignment. Co-Authored-By: Chris Rackauckas Co-Authored-By: Codex Agent-Harness: Codex CLI 0.164.0 Agent-Model: gpt-6.1-sol Agent-Session: local fixer job SciML__RecursiveArrayTools.jl__685 (no session URL) --- test/Core/voa_broadcast_dimensions.jl | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/Core/voa_broadcast_dimensions.jl b/test/Core/voa_broadcast_dimensions.jl index 93976f6d..d4c6d5fe 100644 --- a/test/Core/voa_broadcast_dimensions.jl +++ b/test/Core/voa_broadcast_dimensions.jl @@ -1,7 +1,10 @@ using Test, RecursiveArrayTools, StaticArrays, FastBroadcast, Polyester, StructArrays @testset "VectorOfArray broadcast dimensions" begin - for wrap in (identity, SVector{3}, MVector{3}), storage in (identity, StructArray) + for (wrap, storage) in ( + (identity, identity), (SVector{3}, identity), (MVector{3}, identity), + (SizedVector{3}, StructArray), (SVector{3}, StructArray), (MVector{3}, StructArray), + ) original = [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]] make() = VectorOfArray(storage(wrap.(deepcopy(original)))) dense = Array(make()) From cf081d8fa9be7702d1f2635b141b3c748ac5501f Mon Sep 17 00:00:00 2001 From: ChrisRackauckas Date: Thu, 12 Nov 2026 11:33:03 -0100 Subject: [PATCH 3/3] Raise FastBroadcast compat floor to 1.3.1 The threaded scalar broadcast covered by the VectorOfArray broadcast dimension tests requires the Threaded fallback for non-Broadcasted sources added in FastBroadcast 1.3.1. The 1.3.0 floor fails the downgrade lane with a missing fast_materialize_threaded! method. Co-Authored-By: Chris Rackauckas Co-Authored-By: Muse Agent-Harness: Muse CLI Code Agent-Model: muse-wonder-full-1.0 Agent-Session: local fixer job SciML__RecursiveArrayTools.jl__685 (no session URL) --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 8a194fdd..b4ebe4ed 100644 --- a/Project.toml +++ b/Project.toml @@ -53,7 +53,7 @@ RecursiveArrayToolsZygoteExt = "Zygote" Adapt = "4" ArrayInterface = "7.17.0" CUDA = "6.0" -FastBroadcast = "1.3" +FastBroadcast = "1.3.1" ForwardDiff = "1" GPUArraysCore = "0.2" KernelAbstractions = "0.9.36, 0.10"