-
Notifications
You must be signed in to change notification settings - Fork 68
[VIBE] fix mapreduce stackoverflow #3000
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -229,16 +229,57 @@ function Base.mapreducedim!( | |||||||||||||
| A::Base.AbstractArrayOrBroadcasted, | ||||||||||||||
| ) where {T,N} | ||||||||||||||
| @assert length(size(R)) == length(size(A)) | ||||||||||||||
| dims = map(enumerate(zip(size(R), size(A)))) do (i, (sR, sA)) | ||||||||||||||
| sR == sA && return nothing | ||||||||||||||
| @assert sR == 1 | ||||||||||||||
| return i | ||||||||||||||
| dims = Int64[] | ||||||||||||||
| for (i, (sR, sA)) in enumerate(zip(size(R), size(A))) | ||||||||||||||
| if sR != sA | ||||||||||||||
| @assert sR == 1 | ||||||||||||||
| push!(dims, i) | ||||||||||||||
| end | ||||||||||||||
| end | ||||||||||||||
| if A isa Base.Broadcast.Broadcasted | ||||||||||||||
| A = Base.Broadcast.materialize(A) | ||||||||||||||
| end | ||||||||||||||
| tmp = mapreduce(f, op, A; dims=filter(!isnothing, dims)) | ||||||||||||||
| tmp = overloaded_mapreduce(f, op, A; dims) | ||||||||||||||
| copyto!(R, op.(R, tmp)) | ||||||||||||||
| return R | ||||||||||||||
| end | ||||||||||||||
|
|
||||||||||||||
| function Base._mapreduce_dim( | ||||||||||||||
| @nospecialize(f), @nospecialize(op), @nospecialize(init), A::AnyTracedRArray, dims | ||||||||||||||
| ) | ||||||||||||||
| return overloaded_mapreduce(f, op, A; dims, init) | ||||||||||||||
| end | ||||||||||||||
|
|
||||||||||||||
| function Base._mapreduce_dim( | ||||||||||||||
| @nospecialize(f), @nospecialize(op), @nospecialize(init::Base._InitialValue), A::AnyTracedRArray, dims | ||||||||||||||
| ) | ||||||||||||||
| return overloaded_mapreduce(f, op, A; dims, init) | ||||||||||||||
| end | ||||||||||||||
|
|
||||||||||||||
| function Base._mapreduce_dim( | ||||||||||||||
| @nospecialize(f), @nospecialize(op), @nospecialize(init::Base._InitialValue), A::AnyTracedRArray, dims::AbstractVector{<:Integer} | ||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [JuliaFormatter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||
| ) | ||||||||||||||
| return overloaded_mapreduce(f, op, A; dims, init) | ||||||||||||||
| end | ||||||||||||||
|
|
||||||||||||||
| function Base._mapreduce_dim( | ||||||||||||||
| @nospecialize(f), @nospecialize(op), @nospecialize(init::Base._InitialValue), A::AnyTracedRArray, dims::Colon | ||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [JuliaFormatter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||
| ) | ||||||||||||||
| return overloaded_mapreduce(f, op, A; dims, init) | ||||||||||||||
| end | ||||||||||||||
|
|
||||||||||||||
| function Base._mapreduce_dim( | ||||||||||||||
| @nospecialize(f), @nospecialize(op), @nospecialize(init), A::AnyTracedRArray, dims::AbstractVector{<:Integer} | ||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [JuliaFormatter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||
| ) | ||||||||||||||
| return overloaded_mapreduce(f, op, A; dims, init) | ||||||||||||||
| end | ||||||||||||||
|
|
||||||||||||||
| function Base._mapreduce_dim( | ||||||||||||||
| @nospecialize(f), @nospecialize(op), @nospecialize(init), A::AnyTracedRArray, dims::Colon | ||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [JuliaFormatter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||
| ) | ||||||||||||||
| return overloaded_mapreduce(f, op, A; dims, init) | ||||||||||||||
| end | ||||||||||||||
|
|
||||||||||||||
| function Base.fill!(A::AnyTracedRArray{T,N}, x) where {T,N} | ||||||||||||||
| bcast = Reactant.broadcast_to_size(T(x), size(A)) | ||||||||||||||
| set_mlir_data!(A, get_mlir_data(bcast)) | ||||||||||||||
|
|
@@ -949,6 +990,14 @@ function overloaded_map(f, x::AbstractArray, xs::AbstractArray...) | |||||||||||||
| length(inputs) == 1 && return unrolled_map(f, only(inputs)) | ||||||||||||||
| return unrolled_map(splat(f), zip(inputs...)) | ||||||||||||||
| end | ||||||||||||||
| if !isempty(first(inputs)) | ||||||||||||||
| first_args = map(first_scalar, inputs) | ||||||||||||||
| res_tmp = Reactant.call_with_reactant(f, first_args...) | ||||||||||||||
| if !(res_tmp isa TracedRNumber) | ||||||||||||||
| length(inputs) == 1 && return unrolled_map(f, only(inputs)) | ||||||||||||||
| return unrolled_map(splat(f), zip(inputs...)) | ||||||||||||||
| end | ||||||||||||||
| end | ||||||||||||||
| return TracedUtils.elem_apply(f, inputs...) | ||||||||||||||
| end | ||||||||||||||
|
|
||||||||||||||
|
|
@@ -1325,15 +1374,15 @@ end | |||||||||||||
|
|
||||||||||||||
| # Note: once traced_call supports internal mutations, we can use traced_call here | ||||||||||||||
| function unrolled_map(f::F, itr) where {F} | ||||||||||||||
| y = Reactant.call_with_reactant(iterate, itr) | ||||||||||||||
| y = @allowscalar Reactant.call_with_reactant(iterate, itr) | ||||||||||||||
| y === nothing && return [] | ||||||||||||||
|
|
||||||||||||||
| first, state = y | ||||||||||||||
| res_first = Reactant.call_with_reactant(f, first) | ||||||||||||||
| result = [res_first] | ||||||||||||||
|
|
||||||||||||||
| while true | ||||||||||||||
| y = Reactant.call_with_reactant(iterate, itr, state) | ||||||||||||||
| y = @allowscalar Reactant.call_with_reactant(iterate, itr, state) | ||||||||||||||
| y === nothing && break | ||||||||||||||
|
|
||||||||||||||
| val, state = y | ||||||||||||||
|
|
||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -443,3 +443,11 @@ mapped_sub(xs...) = stack(map(-, xs...)) | |||||
| end | ||||||
| end | ||||||
| end | ||||||
|
|
||||||
| map_sum_closure_bad(x) = only(map(_ -> sum(x; dims = 2), 1:1)) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [JuliaFormatter] reported by reviewdog 🐶
Suggested change
|
||||||
|
|
||||||
| @testset "map of closure returning sum" begin | ||||||
| x = randn(5, 3) | ||||||
| x_ra = Reactant.to_rarray(x) | ||||||
| @test @jit(map_sum_closure_bad(x_ra)) ≈ map_sum_closure_bad(x) | ||||||
| end | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[JuliaFormatter] reported by reviewdog 🐶