-
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.
random sampling of dataset rows with improved memory utilization (#2155)
The random sampling of IVF methods was reverted (#2144) due to large memory utilization #2141. This PR improves the memory consumption of subsamling: it is O(n_train) where n_train is the size of the subsampled dataset. This PR adds the following new APIs: - random::excess_sampling (todo may just call as sample_without_replacement) - matrix::sample_rows - matrix::gather for host input matrix Authors: - Tamas Bela Feher (https://github.com/tfeher) Approvers: - Artem M. Chirkin (https://github.com/achirkin) - Ben Frederickson (https://github.com/benfred) URL: #2155
- Loading branch information
Showing
11 changed files
with
786 additions
and
5 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,112 @@ | ||
/* | ||
* Copyright (c) 2024, 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/core/device_mdarray.hpp> | ||
#include <raft/core/device_resources.hpp> | ||
#include <raft/core/host_mdarray.hpp> | ||
#include <raft/core/operators.hpp> | ||
#include <raft/random/permute.cuh> | ||
#include <raft/random/rng.cuh> | ||
#include <raft/random/sample_without_replacement.cuh> | ||
#include <raft/spatial/knn/detail/ann_utils.cuh> | ||
#include <raft/util/cudart_utils.hpp> | ||
|
||
#include <rmm/device_scalar.hpp> | ||
#include <rmm/mr/device/pool_memory_resource.hpp> | ||
|
||
#include <cub/cub.cuh> | ||
|
||
namespace raft::bench::random { | ||
|
||
struct sample_inputs { | ||
int n_samples; | ||
int n_train; | ||
int method; | ||
}; // struct sample_inputs | ||
|
||
inline auto operator<<(std::ostream& os, const sample_inputs& p) -> std::ostream& | ||
{ | ||
os << p.n_samples << "#" << p.n_train << "#" << p.method; | ||
return os; | ||
} | ||
|
||
// Sample with replacement. We use this as a baseline. | ||
template <typename IdxT> | ||
auto bernoulli_subsample(raft::resources const& res, IdxT n_samples, IdxT n_subsamples, int seed) | ||
-> raft::device_vector<IdxT, IdxT> | ||
{ | ||
RAFT_EXPECTS(n_subsamples <= n_samples, "Cannot have more training samples than dataset vectors"); | ||
|
||
auto indices = raft::make_device_vector<IdxT, IdxT>(res, n_subsamples); | ||
raft::random::RngState state(123456ULL); | ||
raft::random::uniformInt( | ||
res, state, indices.data_handle(), n_subsamples, IdxT(0), IdxT(n_samples)); | ||
return indices; | ||
} | ||
|
||
template <typename T> | ||
struct sample : public fixture { | ||
sample(const sample_inputs& p) | ||
: params(p), | ||
old_mr(rmm::mr::get_current_device_resource()), | ||
pool_mr(rmm::mr::get_current_device_resource(), 2 * GiB), | ||
in(make_device_vector<T, int64_t>(res, p.n_samples)), | ||
out(make_device_vector<T, int64_t>(res, p.n_train)) | ||
{ | ||
rmm::mr::set_current_device_resource(&pool_mr); | ||
raft::random::RngState r(123456ULL); | ||
} | ||
|
||
~sample() { rmm::mr::set_current_device_resource(old_mr); } | ||
void run_benchmark(::benchmark::State& state) override | ||
{ | ||
std::ostringstream label_stream; | ||
label_stream << params; | ||
state.SetLabel(label_stream.str()); | ||
|
||
raft::random::RngState r(123456ULL); | ||
loop_on_state(state, [this, &r]() { | ||
if (params.method == 1) { | ||
this->out = | ||
bernoulli_subsample<T>(this->res, this->params.n_samples, this->params.n_train, 137); | ||
} else if (params.method == 2) { | ||
this->out = raft::random::excess_subsample<T, int64_t>( | ||
this->res, r, this->params.n_samples, this->params.n_train); | ||
} | ||
}); | ||
} | ||
|
||
private: | ||
float GiB = 1073741824.0f; | ||
raft::device_resources res; | ||
rmm::mr::device_memory_resource* old_mr; | ||
rmm::mr::pool_memory_resource<rmm::mr::device_memory_resource> pool_mr; | ||
sample_inputs params; | ||
raft::device_vector<T, int64_t> out, in; | ||
}; // struct sample | ||
|
||
const std::vector<sample_inputs> input_vecs = {{100000000, 10000000, 1}, | ||
{100000000, 50000000, 1}, | ||
{100000000, 100000000, 1}, | ||
{100000000, 10000000, 2}, | ||
{100000000, 50000000, 2}, | ||
{100000000, 100000000, 2}}; | ||
|
||
RAFT_BENCH_REGISTER(sample<int64_t>, "", input_vecs); | ||
|
||
} // namespace raft::bench::random |
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,57 @@ | ||
/* | ||
* Copyright (c) 2024, 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. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <raft/core/device_mdarray.hpp> | ||
#include <raft/core/device_mdspan.hpp> | ||
#include <raft/core/logger.hpp> | ||
#include <raft/core/resources.hpp> | ||
#include <raft/matrix/gather.cuh> | ||
#include <raft/random/rng.cuh> | ||
#include <raft/util/cuda_utils.cuh> | ||
#include <raft/util/cudart_utils.hpp> | ||
|
||
namespace raft::matrix::detail { | ||
|
||
/** Select rows randomly from input and copy to output. */ | ||
template <typename T, typename IdxT = int64_t> | ||
void sample_rows(raft::resources const& res, | ||
random::RngState random_state, | ||
const T* input, | ||
IdxT n_rows_input, | ||
raft::device_matrix_view<T, IdxT> output) | ||
{ | ||
IdxT n_dim = output.extent(1); | ||
IdxT n_samples = output.extent(0); | ||
|
||
raft::device_vector<IdxT, IdxT> train_indices = | ||
raft::random::excess_subsample<IdxT, int64_t>(res, random_state, n_rows_input, n_samples); | ||
|
||
cudaPointerAttributes attr; | ||
RAFT_CUDA_TRY(cudaPointerGetAttributes(&attr, input)); | ||
T* ptr = reinterpret_cast<T*>(attr.devicePointer); | ||
if (ptr != nullptr) { | ||
raft::matrix::gather(res, | ||
raft::make_device_matrix_view<const T, IdxT>(ptr, n_rows_input, n_dim), | ||
raft::make_const_mdspan(train_indices.view()), | ||
output); | ||
} else { | ||
auto dataset = raft::make_host_matrix_view<const T, IdxT>(input, n_rows_input, n_dim); | ||
raft::matrix::detail::gather(res, dataset, make_const_mdspan(train_indices.view()), output); | ||
} | ||
} | ||
} // namespace raft::matrix::detail |
Oops, something went wrong.