Skip to content

Commit

Permalink
stablehlo.sort Ops (#374)
Browse files Browse the repository at this point in the history
* `stablehlo.sort` Ops

* review

* use `return_dialect`

* feedback

* fix test GPU
  • Loading branch information
glou-nes authored Dec 28, 2024
1 parent 925544f commit eeaf86c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 22 deletions.
65 changes: 43 additions & 22 deletions src/Ops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -950,28 +950,49 @@ end
# return TracedRArray{T,N}((), res, size(x))
# end

# sorting ops
# TODO need to trace over `comparator`
# function sort(
# x::TracedRArray{T,N};
# comparator,
# dimension=-1,
# is_stable=false,
# location=mlir_stacktrace("sort", @__FILE__, @__LINE__),
# ) where {T,N}
# dimension = MLIR.IR.Attribute(dimension)
# is_stable = MLIR.IR.Attribute(is_stable)
# res = MLIR.IR.result(
# stablehlo.sort(
# x.mlir_data;
# result=mlir_type(TracedRArray{T,N}, size(x)),
# dimension,
# is_stable,
# location,
# ),
# )
# return TracedRArray{T,N}((), res, size(x))
# end
@noinline function sort(
x::TracedRArray{T,N};
comparator,
dimension=1,
is_stable=false,
location=mlir_stacktrace("sort", @__FILE__, @__LINE__),
) where {T,N}
#C4:
@assert 0 < dimension <= ndims(x) "$x invalid dimension"

(a, b) = (Reactant.ConcreteRNumber(T(0)), Reactant.ConcreteRNumber(T(0)))
func = Reactant.TracedUtils.make_mlir_fn(comparator, (a, b), (), "comparator"; no_args_in_result=true, return_dialect=:stablehlo)[2]
@assert MLIR.IR.nregions(func) == 1
fn_name = String(
MLIR.IR.attr(func, String(MLIR.API.mlirSymbolTableGetSymbolAttributeName()))
)
#C5:
@assert fn_name == "comparator" "$comparator: no function generated"
ftype_attr = MLIR.IR.attr(func, "function_type")
ftype = MLIR.IR.Type(ftype_attr)
@assert MLIR.IR.result(ftype) == MLIR.IR.TensorType((), MLIR.IR.Type(Bool)) error(
"$comparator return type is not tensor<i1>"
)

comparator = MLIR.IR.Region()
MLIR.API.mlirRegionTakeBody(comparator, MLIR.IR.region(func, 1))
MLIR.IR.rmfromparent!(func)

dimension = MLIR.IR.Attribute(dimension - 1)
is_stable = MLIR.IR.Attribute(is_stable)

res = MLIR.IR.result(
stablehlo.sort(
[x.mlir_data];
result_0=[mlir_type(TracedRArray{T,N}, size(x))],
dimension,
is_stable,
comparator,
location,
),
)
return TracedRArray{T,N}((), res, size(x))
end

@noinline function top_k(
x::TracedRArray{T,N}, k; location=mlir_stacktrace("top_k", @__FILE__, @__LINE__)
Expand Down
13 changes: 13 additions & 0 deletions test/ops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,19 @@ end
end
end

@testset "sort" begin
basic_sort(x, dimension) = Reactant.Ops.sort(x; comparator=(a, b) -> a < b, dimension)
for i in 1:3
t_size = tuple(fill(10, (i,))...)
x = randn(t_size)
xa = Reactant.to_rarray(x)

for j in 1:i
@test (i == 1 ? sort(x) : sort(x; dims=j)) == @jit basic_sort(xa, j)
end
end
end

@testset "slice" begin
x = ConcreteRArray([1, 2, 3, 4])
@test [2, 3] == @jit Ops.slice(x, [2], [3])
Expand Down

0 comments on commit eeaf86c

Please sign in to comment.