Skip to content
Draft
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
16 changes: 12 additions & 4 deletions src/TracedRArray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,12 @@ function __default_init(T::Type{<:Reactant.ReactantFloat8}, op::F) where {F}
return T(__default_init(Float16, op))
end

struct TracedCall{F} <: Function
f::F
end

(fn::TracedCall)(args...) = @opcall call(fn.f, args...)

function overloaded_mapreduce(
@nospecialize(f), @nospecialize(op), @nospecialize(A); dims=:, init=Base._InitialValue()
)
Expand All @@ -558,7 +564,7 @@ function overloaded_mapreduce(
# unroll the mapreduce.
if typeof(res) == typeof(A)
@assert dims == Colon() "dims not supported for mapreduce currently."
return foldl(op, res; init)
return foldl(TracedCall(op), res; init)
end
return overloaded_mapreduce(identity, op, res; dims=:, init)
end
Expand Down Expand Up @@ -1503,6 +1509,9 @@ end

struct BroadcastIterator{F}
f::F

BroadcastIterator{F}(f::F) where {F} = new{F}(f)
BroadcastIterator(f::F) where {F} = new{F}(f)
end

(fn::BroadcastIterator)(args...) = fn.f((args...,))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Trying to understand what this is doing. Why isn't call_with_reactant required anymore, or why was it required before?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is meant to be only used instead broadcast which with force the call_via_reactant in elem_apply (it should have not been added in the first place)

Expand All @@ -1523,22 +1532,21 @@ end

unwrapped_broadcast(f::F, xs) where {F} = unrolled_map(f, xs)

# TODO: once traced_call supports internal mutations, we can use traced_call here
# TODO: we should overload this for Slices and use mapslices instead
function unrolled_map(f::F, itr) where {F}
y = Reactant.call_with_reactant(iterate, itr)
y === nothing && return []

first, state = y
res_first = Reactant.call_with_reactant(f, first)
res_first = @opcall call(f, first)
result = [res_first]

while true
y = Reactant.call_with_reactant(iterate, itr, state)
y === nothing && break

val, state = y
res = Reactant.call_with_reactant(f, val)
res = @opcall call(f, val)
push!(result, res)
end

Expand Down
9 changes: 9 additions & 0 deletions test/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1496,6 +1496,15 @@ end

@test @jit(nested_mapreduce_hcat(x_ra, y_ra)) ≈ nested_mapreduce_hcat(x, y)
end

@testset "mapreduce vector" begin
x = [rand(Float32, 2, 3) for _ in 1:10]
x_ra = Reactant.to_rarray(x)

@test @jit(mapreduce_vector(x_ra)) ≈ mapreduce_vector(x)
hlo = repr(@code_hlo optimize = false mapreduce_vector(x_ra))
@test contains(hlo, "call")
end
end

@testset "Base.Generator" begin
Expand Down
2 changes: 1 addition & 1 deletion test/integration/fillarrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ end
x = OneElement(3.4f0, (3, 4), (32, 32))
rx = Reactant.to_rarray(x)

@test @jit(fn(rx, rx)) ≈ fn(x, x)
@test @jit(fn(rx, rx)) ≈ fn(x, x) atol = 1e-3 rtol = 1e-3
end
Loading