Skip to content

feat: broadcasting of concrete arrays #913

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

Merged
merged 1 commit into from
Mar 14, 2025
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
50 changes: 39 additions & 11 deletions src/ConcreteRArray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,14 @@ function Base.isempty(x::Union{WrappedConcretePJRTArray,WrappedConcreteIFRTArray
return isempty(ancestor(x))
end

function Base.convert(::Type{<:Array}, X::ConcretePJRTArray{T,N}) where {T,N}
if Sharding.is_sharded(X)
data = Array{T,N}(undef, size(X)...)
function Base.convert(::Type{<:Array}, X::AbstractConcreteArray{T,N}) where {T,N}
data = Array{T,N}(undef, size(X)...)
write_to_host_buffer!(data, X)
return data
end

function write_to_host_buffer!(data::Array, X::ConcretePJRTArray{T,N}) where {T,N}
if Sharding.is_sharded(X)
completed = Set{eltype(X.sharding.device_to_array_slices)}()
for idx in 1:length(X.data)
slice = X.sharding.device_to_array_slices[idx]
Expand All @@ -73,19 +77,15 @@ function Base.convert(::Type{<:Array}, X::ConcretePJRTArray{T,N}) where {T,N}
XLA.to_host(X.data[idx], data_slice, Reactant.Sharding.NoSharding())
data[slice...] .= data_slice
end

return data
else
data = Array{T,N}(undef, size(X)...)
XLA.to_host(XLA.synced_buffer(only(X.data)), data, Reactant.Sharding.NoSharding())
return data
end
return nothing
end

function Base.convert(::Type{<:Array}, X::ConcreteIFRTArray{T,N}) where {T,N}
data = zeros(T, size(X)...)
function write_to_host_buffer!(data::Array, X::ConcreteIFRTArray{T,N}) where {T,N}
XLA.to_host(X.data, data, X.sharding)
return data
return nothing
end

function Base.convert(
Expand Down Expand Up @@ -347,7 +347,9 @@ function Base.copy(bc::Base.Broadcast.Broadcasted{Broadcast.ArrayStyle{ConcreteP
),
)
end
aux = copyto!(similar(Array{ElType}, axes(bc)), bc)
aux = copyto!(
similar(Array{ElType}, axes(bc)), convert(Broadcast.Broadcasted{Nothing}, bc)
)
return ConcretePJRTArray(aux) # XXX: result should be on correct device?
end

Expand All @@ -367,6 +369,32 @@ function Base.copyto!(dest::AbstractConcreteArray, src::AbstractConcreteArray)
return dest
end

for aType in (:ConcretePJRTArray, :ConcreteIFRTArray)
@eval begin
function Base.copyto!(
dest::AbstractConcreteArray,
src::Broadcast.Broadcasted{Broadcast.ArrayStyle{$(aType)}},
)
dest.data = copy(src).data
return dest
end

function Base.copyto!(
dest::Array, src::Broadcast.Broadcasted{Broadcast.ArrayStyle{$(aType)}}
)
write_to_host_buffer!(dest, copy(src))
return dest
end

function Base.copyto!(
dest::AbstractArray, src::Broadcast.Broadcasted{Broadcast.ArrayStyle{$(aType)}}
Copy link
Member

Choose a reason for hiding this comment

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

is this really sufficient? on a gpu backend [where indexing isn't free] I feel like this doesn't resolve the issue of the a * 2 part

)
copyto!(dest, convert(Array, copy(src)))
return dest
end
end
end

Base.collect(x::AbstractConcreteArray) = convert(Array, x)

function Base.mapreduce(
Expand Down
19 changes: 19 additions & 0 deletions test/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -961,3 +961,22 @@ end
@test Array(@jit(map!(abs2, y_ra, x_ra))) ≈ map!(abs2, y, x)
@test Array(y_ra) ≈ y
end

@testset "ConcreteRArray inplace broadcast" begin
x = Reactant.to_rarray(zeros(Float32, 2, 3))
y = Reactant.to_rarray(reshape(collect(Float32, 1:6), 2, 3))

x .= y ./ 2

@test Array(x) ≈ Array(y) ./ 2

x = zeros(Float32, 2, 3)
x .= y ./ 2

@test Array(x) ≈ Array(y) ./ 2

x = view(zeros(Float32, 2, 5), :, 1:3)
x .= y ./ 2

@test Array(x) ≈ Array(y) ./ 2
end
Loading