-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TOPI][OP] Use Thrust sort for argsort and topk (#5097)
* [TOPI][OP] Use Thrust sort for argsort and topk The current GPU sort implementation (odd-even transposition sort) is too slow when the number of elements is large. This PR introduces Thrust implementation of sort which is much faster. Note that this change requires CMake 3.8 or later since we have to use nvcc to compile a thrust code. * cmake: make CUDA optional * allow .cu file to be into the repository * pylint fix and cleanup * require cmake 3.8 only when thrust is enabled * fix nvcc compiler error when passing -pthread * add missing include * add USE_THRUST option in config.cmake * retrigger CI * retrigger CI
- Loading branch information
Showing
7 changed files
with
271 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/* | ||
* 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 Use external Thrust library call | ||
*/ | ||
|
||
#include <thrust/device_ptr.h> | ||
#include <thrust/sort.h> | ||
|
||
#include <tvm/runtime/registry.h> | ||
#include <dlpack/dlpack.h> | ||
#include <algorithm> | ||
#include <vector> | ||
|
||
namespace tvm { | ||
namespace contrib { | ||
|
||
using namespace runtime; | ||
|
||
// Performs sorting along axis -1 and returns both sorted values and indices. | ||
template<typename DataType, typename IndicesType> | ||
void thrust_sort(DLTensor* input, | ||
DLTensor* out_values, | ||
DLTensor* out_indices, | ||
bool is_ascend) { | ||
thrust::device_ptr<DataType> data_ptr(static_cast<DataType *>(input->data)); | ||
thrust::device_ptr<DataType> values_ptr(static_cast<DataType *>(out_values->data)); | ||
thrust::device_ptr<IndicesType> indices_ptr(static_cast<IndicesType *>(out_indices->data)); | ||
|
||
int n_values = input->shape[input->ndim - 1]; | ||
int n_iter = 1; | ||
for (int i = 0; i < input->ndim - 1; ++i) { | ||
n_iter *= input->shape[i]; | ||
} | ||
|
||
thrust::copy(data_ptr, data_ptr + n_iter * n_values, values_ptr); | ||
|
||
for (int i = 0 ; i < n_iter; ++i) { | ||
thrust::sequence(indices_ptr, indices_ptr + n_values); | ||
if (is_ascend) { | ||
thrust::sort_by_key(values_ptr, values_ptr + n_values, indices_ptr); | ||
} else { | ||
thrust::sort_by_key(values_ptr, values_ptr + n_values, indices_ptr, | ||
thrust::greater<DataType>()); | ||
} | ||
values_ptr += n_values; | ||
indices_ptr += n_values; | ||
} | ||
} | ||
|
||
TVM_REGISTER_GLOBAL("tvm.contrib.thrust.sort") | ||
.set_body([](TVMArgs args, TVMRetValue* ret) { | ||
CHECK_GE(args.num_args, 4); | ||
DLTensor* input = args[0]; | ||
DLTensor* values_out = args[1]; | ||
DLTensor* indices_out = args[2]; | ||
bool is_ascend = args[3]; | ||
|
||
auto data_dtype = DLDataType2String(input->dtype); | ||
auto out_dtype = DLDataType2String(indices_out->dtype); | ||
|
||
if (data_dtype == "float32") { | ||
if (out_dtype == "int32") { | ||
thrust_sort<float, int32_t>(input, values_out, indices_out, is_ascend); | ||
} else if (out_dtype == "int64") { | ||
thrust_sort<float, int64_t>(input, values_out, indices_out, is_ascend); | ||
} else if (out_dtype == "float32") { | ||
thrust_sort<float, float>(input, values_out, indices_out, is_ascend); | ||
} else if (out_dtype == "float64") { | ||
thrust_sort<float, double>(input, values_out, indices_out, is_ascend); | ||
} else { | ||
LOG(FATAL) << "Unsupported output dtype: " << out_dtype; | ||
} | ||
} else if (data_dtype == "float64") { | ||
if (out_dtype == "int32") { | ||
thrust_sort<double, int32_t>(input, values_out, indices_out, is_ascend); | ||
} else if (out_dtype == "int64") { | ||
thrust_sort<double, int64_t>(input, values_out, indices_out, is_ascend); | ||
} else if (out_dtype == "float32") { | ||
thrust_sort<double, float>(input, values_out, indices_out, is_ascend); | ||
} else if (out_dtype == "float64") { | ||
thrust_sort<double, double>(input, values_out, indices_out, is_ascend); | ||
} else { | ||
LOG(FATAL) << "Unsupported output dtype: " << out_dtype; | ||
} | ||
} else if (data_dtype == "int32") { | ||
if (out_dtype == "int32") { | ||
thrust_sort<int32_t, int32_t>(input, values_out, indices_out, is_ascend); | ||
} else if (out_dtype == "int64") { | ||
thrust_sort<int32_t, int64_t>(input, values_out, indices_out, is_ascend); | ||
} else if (out_dtype == "float32") { | ||
thrust_sort<int32_t, float>(input, values_out, indices_out, is_ascend); | ||
} else if (out_dtype == "float64") { | ||
thrust_sort<int32_t, double>(input, values_out, indices_out, is_ascend); | ||
} else { | ||
LOG(FATAL) << "Unsupported output dtype: " << out_dtype; | ||
} | ||
} else if (data_dtype == "int64") { | ||
if (out_dtype == "int32") { | ||
thrust_sort<int64_t, int32_t>(input, values_out, indices_out, is_ascend); | ||
} else if (out_dtype == "int64") { | ||
thrust_sort<int64_t, int64_t>(input, values_out, indices_out, is_ascend); | ||
} else if (out_dtype == "float32") { | ||
thrust_sort<int64_t, float>(input, values_out, indices_out, is_ascend); | ||
} else if (out_dtype == "float64") { | ||
thrust_sort<int64_t, double>(input, values_out, indices_out, is_ascend); | ||
} else { | ||
LOG(FATAL) << "Unsupported output dtype: " << out_dtype; | ||
} | ||
} else { | ||
LOG(FATAL) << "Unsupported input dtype: " << data_dtype; | ||
} | ||
}); | ||
|
||
} // namespace contrib | ||
} // namespace tvm |
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 |
---|---|---|
|
@@ -42,6 +42,7 @@ | |
"pxi", | ||
"pyd", | ||
"pyx", | ||
"cu", | ||
# relay text format | ||
"rly", | ||
# configurations | ||
|
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