Skip to content
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

stablehlo.sort Ops #374

Merged
merged 8 commits into from
Dec 28, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
66 changes: 44 additions & 22 deletions src/Ops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -914,28 +914,50 @@ 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
function sort(
glou-nes marked this conversation as resolved.
Show resolved Hide resolved
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"

#TODO: move to @trace
(a, b) = (ConcreteRNumber(T(0)), ConcreteRNumber(T(0)))
func = Reactant.make_mlir_fn(comparator, (a, b), (), "main"; 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 == "main" "$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(
Copy link
Collaborator

Choose a reason for hiding this comment

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

wait, calling result on a Type works?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes!

Copy link
Collaborator

Choose a reason for hiding this comment

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

and what are the semantics? like what is the result of a Type??

Copy link
Contributor Author

Choose a reason for hiding this comment

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

From the MLIR type system grammar, function-type ::= (type | type-list-parens) -> (type | type-list-parens). On a function type, it returns the type result of the function.

"$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

function top_k(
x::TracedRArray{T,N}, k; location=mlir_stacktrace("top_k", @__FILE__, @__LINE__)
Expand Down
12 changes: 12 additions & 0 deletions test/ops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,18 @@ 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 = Reactant.to_rarray(randn(t_size))

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

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