-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
[Relay][Topi]Add Sort Op to Relay #6978
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -316,6 +316,92 @@ def argsort_nms_thrust(data, valid_count, axis=-1, is_ascend=1, dtype="float32") | |
return out[1] | ||
|
||
|
||
def sort(data, axis=-1, is_ascend=1): | ||
"""Performs sorting along the given axis and returns an array of | ||
sorted values with the same shape as the input data. | ||
|
||
Parameters | ||
---------- | ||
data: tvm.te.Tensor | ||
The input array. | ||
|
||
axis : int, optional | ||
Axis long which to sort the input tensor. | ||
|
||
is_ascend : boolean, optional | ||
Whether to sort in ascending or descending order. | ||
|
||
Returns | ||
------- | ||
out : tvm.te.Tensor | ||
The output of this function. | ||
""" | ||
dtype = "float32" | ||
value_buf = tvm.tir.decl_buffer(data.shape, data.dtype, "value_buf", data_alignment=8) | ||
indices_buf = tvm.tir.decl_buffer(data.shape, dtype, "out_buf", data_alignment=8) | ||
out = te.extern( | ||
[data.shape, data.shape], | ||
[data], | ||
lambda ins, outs: sort_ir(ins[0], outs[0], axis, is_ascend, indices_out=outs[1]), | ||
out_buffers=[value_buf, indices_buf], | ||
name="sort_gpu", | ||
tag="sort_gpu", | ||
)[0] | ||
return out | ||
|
||
|
||
def sort_thrust(data, axis=-1, is_ascend=1): | ||
"""Performs sorting along the given axis and returns an array of | ||
sorted values with the same shape as the input data. | ||
|
||
Parameters | ||
---------- | ||
data: tvm.te.Tensor | ||
The input array. | ||
|
||
axis : int, optional | ||
Axis long which to sort the input tensor. | ||
|
||
is_ascend : boolean, optional | ||
Whether to sort in ascending or descending order. | ||
|
||
Returns | ||
------- | ||
out : tvm.te.Tensor | ||
The output of this function. | ||
""" | ||
dtype = "float32" | ||
|
||
ndim = len(data.shape) | ||
axis = ndim + axis if axis < 0 else axis | ||
|
||
if axis != ndim - 1: | ||
# Prepare for sorting along axis -1. | ||
axes = swap(list(range(ndim)), axis) | ||
data = transpose(data, axes) | ||
|
||
value_buf = tvm.tir.decl_buffer(data.shape, data.dtype, "value_buf", data_alignment=8) | ||
indices_buf = tvm.tir.decl_buffer(data.shape, dtype, "out_buf", data_alignment=8) | ||
out = te.extern( | ||
[data.shape, data.shape], | ||
[data], | ||
## TODO(mbrookhart): This thrust function is actually doing argsort, not sort | ||
## For performance, we should probably rename the contrib function and add | ||
## a pure sort | ||
lambda ins, outs: tvm.tir.call_packed( | ||
"tvm.contrib.thrust.sort", ins[0], outs[0], outs[1], is_ascend | ||
), | ||
out_buffers=[value_buf, indices_buf], | ||
name="sort_gpu", | ||
tag="sort_gpu", | ||
)[0] | ||
|
||
if axis != ndim - 1: | ||
axes = swap(list(range(ndim)), axis) | ||
out = transpose(out, axes) | ||
return out | ||
|
||
|
||
def argsort(data, valid_count=None, axis=-1, is_ascend=1, dtype="float32"): | ||
"""Performs sorting along the given axis and returns an array of indicies | ||
having same shape as an input array that index data in sorted order. | ||
|
@@ -408,6 +494,23 @@ def argsort_thrust(data, valid_count=None, axis=-1, is_ascend=1, dtype="float32" | |
return out | ||
|
||
|
||
def schedule_sort(outs): | ||
"""Schedule for sort operator. | ||
|
||
Parameters | ||
---------- | ||
outs: Array of Tensor | ||
The computation graph description of argsort | ||
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. typo: argsort -> sort |
||
in the format of an array of tensors. | ||
|
||
Returns | ||
------- | ||
s: Schedule | ||
The computation schedule for the op. | ||
""" | ||
return _schedule_sort(outs) | ||
|
||
|
||
def schedule_argsort(outs): | ||
"""Schedule for argsort operator. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
/*! | ||
* \file sort.cc | ||
* \brief Sort operators | ||
*/ | ||
#include <tvm/relay/attrs/algorithm.h> | ||
#include <tvm/relay/op.h> | ||
|
||
namespace tvm { | ||
namespace relay { | ||
|
||
bool SortRel(const Array<Type>& types, int num_inputs, const Attrs& attrs, | ||
const TypeReporter& reporter) { | ||
// `types` contains: [data, result] | ||
ICHECK_EQ(types.size(), 2); | ||
const auto* data = types[0].as<TensorTypeNode>(); | ||
if (data == nullptr) { | ||
ICHECK(types[0].as<IncompleteTypeNode>()) | ||
<< "Sort: expect input type to be TensorType but get " << types[0]; | ||
return false; | ||
} | ||
reporter->Assign(types[1], TensorType(data->shape, data->dtype)); | ||
return true; | ||
} | ||
|
||
Expr MakeSort(Expr data, int axis, bool is_ascend) { | ||
auto attrs = make_object<ArgsortAttrs>(); | ||
attrs->axis = axis; | ||
attrs->is_ascend = is_ascend; | ||
static const Op& op = Op::Get("sort"); | ||
return Call(op, {data}, Attrs(attrs), {}); | ||
} | ||
|
||
TVM_REGISTER_GLOBAL("relay.op._make.sort").set_body_typed(MakeSort); | ||
|
||
RELAY_REGISTER_OP("sort") | ||
.describe(R"doc(Returns the indices that would sort an | ||
input array along the given axis. | ||
)doc" TVM_ADD_FILELINE) | ||
.set_num_inputs(1) | ||
.set_attrs_type<ArgsortAttrs>() | ||
.add_argument("data", "Tensor", "Input data.") | ||
.set_support_level(6) | ||
.add_type_rel("Sort", SortRel); | ||
|
||
} // namespace relay | ||
} // namespace tvm |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
There is no need to pass
indices_buf
for normal sort, buttvm.contrib.thrust.sort
always expectsindices_buf
to be passed in... What we calltvm.contrib.thrust.sort
is really argsort.For optimal performance we should have the true
tvm.contrib.thrust.sort
someday.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.
Hmm, you're right, we don't strictly need that. Want me to add a todo?
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.
Yeah that would be nice