-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improvements in
matrix::gather
: test coverage, compilation errors, …
…performance (#1126) In order to deprecate `copy_selected` from `ann_utils.cuh`, I wanted to make sure that the performance of `matrix::gather` was on par. But in the process I discovered that: - Map transforms and conditional copy were not tested at all. - In fact, most of the API in `gather.cuh` wasn't covered in tests and some of the functions didn't even compile. - The same type `MatrixIteratorT` was used for the input and output iterators, which made it impossible to take advantage of custom iterators, as is needed in `kmeans_balanced` to convert the dataset from `T` to `float` and gather in a single step. - The performance was really poor when `D` is small because the kernel assigns one block per row (so a block could be working on only 2 or 3 elements...) This PR addresses all the aforementioned issues. Authors: - Louis Sugy (https://github.com/Nyrio) Approvers: - Tamas Bela Feher (https://github.com/tfeher) - Corey J. Nolet (https://github.com/cjnolet) URL: #1126
- Loading branch information
Showing
8 changed files
with
578 additions
and
410 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* Copyright (c) 2023, NVIDIA CORPORATION. | ||
* | ||
* Licensed 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. | ||
*/ | ||
|
||
#include <common/benchmark.hpp> | ||
#include <raft/matrix/gather.cuh> | ||
#include <raft/random/rng.cuh> | ||
#include <raft/util/itertools.hpp> | ||
|
||
#include <rmm/device_uvector.hpp> | ||
|
||
namespace raft::bench::matrix { | ||
|
||
template <typename IdxT> | ||
struct GatherParams { | ||
IdxT rows, cols, map_length; | ||
}; | ||
|
||
template <typename IdxT> | ||
inline auto operator<<(std::ostream& os, const GatherParams<IdxT>& p) -> std::ostream& | ||
{ | ||
os << p.rows << "#" << p.cols << "#" << p.map_length; | ||
return os; | ||
} | ||
|
||
template <typename T, typename MapT, typename IdxT, bool Conditional = false> | ||
struct Gather : public fixture { | ||
Gather(const GatherParams<IdxT>& p) : params(p) {} | ||
|
||
void allocate_data(const ::benchmark::State& state) override | ||
{ | ||
matrix = raft::make_device_matrix<T, IdxT>(handle, params.rows, params.cols); | ||
map = raft::make_device_vector<MapT, IdxT>(handle, params.map_length); | ||
out = raft::make_device_matrix<T, IdxT>(handle, params.map_length, params.cols); | ||
stencil = raft::make_device_vector<T, IdxT>(handle, Conditional ? params.map_length : IdxT(0)); | ||
|
||
raft::random::RngState rng{1234}; | ||
raft::random::uniform( | ||
rng, matrix.data_handle(), params.rows * params.cols, T(-1), T(1), stream); | ||
raft::random::uniformInt( | ||
handle, rng, map.data_handle(), params.map_length, (MapT)0, (MapT)params.rows); | ||
if constexpr (Conditional) { | ||
raft::random::uniform(rng, stencil.data_handle(), params.map_length, T(-1), T(1), stream); | ||
} | ||
handle.sync_stream(stream); | ||
} | ||
|
||
void run_benchmark(::benchmark::State& state) override | ||
{ | ||
std::ostringstream label_stream; | ||
label_stream << params; | ||
state.SetLabel(label_stream.str()); | ||
|
||
loop_on_state(state, [this]() { | ||
auto matrix_const_view = raft::make_device_matrix_view<const T, IdxT, row_major>( | ||
matrix.data_handle(), matrix.extent(0), matrix.extent(1)); | ||
auto map_const_view = | ||
raft::make_device_vector_view<const MapT, IdxT>(map.data_handle(), map.extent(0)); | ||
if constexpr (Conditional) { | ||
auto stencil_const_view = | ||
raft::make_device_vector_view<const T, IdxT>(stencil.data_handle(), stencil.extent(0)); | ||
auto pred_op = raft::plug_const_op(T(0.0), raft::greater_op()); | ||
raft::matrix::gather_if( | ||
handle, matrix_const_view, out.view(), map_const_view, stencil_const_view, pred_op); | ||
} else { | ||
raft::matrix::gather(handle, matrix_const_view, map_const_view, out.view()); | ||
} | ||
}); | ||
} | ||
|
||
private: | ||
GatherParams<IdxT> params; | ||
raft::device_matrix<T, IdxT> matrix, out; | ||
raft::device_vector<T, IdxT> stencil; | ||
raft::device_vector<MapT, IdxT> map; | ||
}; // struct Gather | ||
|
||
template <typename T, typename MapT, typename IdxT> | ||
using GatherIf = Gather<T, MapT, IdxT, true>; | ||
|
||
const std::vector<GatherParams<int64_t>> gather_inputs_i64 = | ||
raft::util::itertools::product<GatherParams<int64_t>>( | ||
{1000000}, {10, 20, 50, 100, 200, 500}, {1000, 10000, 100000, 1000000}); | ||
|
||
RAFT_BENCH_REGISTER((Gather<float, uint32_t, int64_t>), "", gather_inputs_i64); | ||
RAFT_BENCH_REGISTER((Gather<double, uint32_t, int64_t>), "", gather_inputs_i64); | ||
RAFT_BENCH_REGISTER((GatherIf<float, uint32_t, int64_t>), "", gather_inputs_i64); | ||
RAFT_BENCH_REGISTER((GatherIf<double, uint32_t, int64_t>), "", gather_inputs_i64); | ||
} // namespace raft::bench::matrix |
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
Oops, something went wrong.