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
19 changes: 11 additions & 8 deletions src/ConcreteRArray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -393,14 +393,6 @@ buffer_on_cpu(::Any) = true
buffer_on_cpu(x::ConcretePJRTArray) = all(XLA.buffer_on_cpu, x.data)
buffer_on_cpu(x::ConcreteIFRTArray) = XLA.buffer_on_cpu(x.data)

function Ops.constant(x::AbstractConcreteArray; kwargs...)
return Ops.constant(Base.convert(Array, x); kwargs...)
end

function Ops.constant(x::AbstractConcreteNumber{T}; kwargs...) where {T}
return Ops.constant(Base.convert(T, x); kwargs...)
end

function Base.zero(x::ConcretePJRTArray{T,N}) where {T,N}
return ConcretePJRTArray(
zeros(T, size(x)...); client=XLA.client(x), device=XLA.device(x), x.sharding
Expand Down Expand Up @@ -464,3 +456,14 @@ function Base.mapreducedim!(
fn(f, op, R, A)
return R
end

function mymap!(f, R, A)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

same as the mymapreduce!, we are definitely doing some incorrect codegen for in place updates, else this change shouldn't matter

map!(f, R, A)
return nothing
end

function Base.map!(f, R::Union{AnyConcreteIFRTArray,AnyConcretePJRTArray}, A::AbstractArray)
fn = compile(mymap!, (f, R, A))
fn(f, R, A)
return R
end
14 changes: 14 additions & 0 deletions src/Ops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,16 @@ end
end
end

@noinline function constant(
x::AbstractArray{T,N}; location=mlir_stacktrace("constant", @__FILE__, @__LINE__)
) where {T,N}
return constant(collect(x); location)
end

@noinline function constant(x::Reactant.AbstractConcreteArray; kwargs...)
return constant(Base.convert(Array, x); kwargs...)
end

@noinline function constant(
x::T; location=mlir_stacktrace("constant", @__FILE__, @__LINE__)
) where {T<:Number}
Expand All @@ -130,6 +140,10 @@ end
return TracedRNumber{T}((), res.mlir_data)
end

@noinline function constant(x::Reactant.AbstractConcreteNumber{T}; kwargs...) where {T}
return constant(Base.convert(T, x); kwargs...)
end

function fill(
v, dims::Base.DimOrInd...; location=mlir_stacktrace("fill", @__FILE__, @__LINE__)
)
Expand Down
7 changes: 7 additions & 0 deletions src/TracedRArray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1084,4 +1084,11 @@ function Base.findmax(f, x::AnyTracedRArray; dims::Union{Integer,Nothing}=nothin
return (values, linear_indices)
end

Base.map(f, x::AnyTracedRArray) = f.(x)

function Base.map!(f, y::AnyTracedRArray, x::AbstractArray)
y .= f.(x)
return y
end

end
11 changes: 11 additions & 0 deletions test/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -938,3 +938,14 @@ end
rv
)
end

@testset "map!" begin
x = randn(Float32, 2, 3)
y = zeros(Float32, 2, 3)

x_ra = Reactant.to_rarray(x)
y_ra = Reactant.to_rarray(y)

@test Array(@jit(map!(abs2, y_ra, x_ra))) ≈ map!(abs2, y, x)
@test Array(y_ra) ≈ y
end
Loading