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

random sampling of dataset rows with improved memory utilization #2155

Merged
merged 19 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add mdspan input API, fix cmakelists
  • Loading branch information
tfeher committed Mar 13, 2024
commit eb7e6d14c677fa7507527811c92b558ec178fc27
9 changes: 6 additions & 3 deletions cpp/include/raft/matrix/detail/sample_rows.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,24 @@

#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 {
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,
const T* input,
IdxT n_rows_input,
raft::device_matrix_view<T, IdxT> output,
RngState random_state)
random::RngState random_state)
{
IdxT n_dim = output.extent(1);
IdxT n_samples = output.extent(0);
Expand All @@ -51,4 +54,4 @@ void sample_rows(raft::resources const& res,
raft::matrix::detail::gather(res, dataset, make_const_mdspan(train_indices.view()), output);
}
}
} // namespace raft::matrix
} // namespace raft::matrix::detail
36 changes: 19 additions & 17 deletions cpp/include/raft/matrix/sample_rows.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,38 @@

#pragma once

#include <raft/core/device_mdarray.hpp>
#include <raft/core/device_mdspan.hpp>
#include <raft/core/logger.hpp>
#include <raft/matrix/gather.cuh>
#include <raft/core/resources.hpp>
#include <raft/matrix/detail/sample_rows.cuh>
#include <raft/random/rng.cuh>
#include <raft/util/cuda_utils.cuh>
#include <raft/util/cudart_utils.hpp>

namespace raft::matrix {

/** Select rows randomly from input and copy to output. */
template <typename T, typename IdxT = int64_t>
template <typename T, typename IdxT = int64_t, typename accessor>
void sample_rows(raft::resources const& res,
const T* input,
IdxT n_rows_input,
raft::device_matrix_view<T, IdxT> output,
RngState random_state)
random::RngState random_state,
mdspan<const T, matrix_extent<int64_t>, row_major, accessor> dataset,
raft::device_matrix_view<T, IdxT> output)
{
detail::sample_rows(res, input, n_rows_input, output, random_state);

detail::sample_rows(res, dataset.data_handle(), dataset.extent(0), output, random_state);
}

/** Subsample the dataset to create a training set*/
template <typename T, typename IdxT = int64_t>
raft::device_matrix<T, IdxT> sample_rows(raft::resources const& res,
const T* input,
IdxT n_rows_input,
IdxT n_train,
IdxT n_dim,
RngState random_state)
template <typename T, typename IdxT = int64_t, typename accessor>
raft::device_matrix<T, IdxT> sample_rows(
raft::resources const& res,
random::RngState random_state,
mdspan<const T, matrix_extent<int64_t>, row_major, accessor> dataset,
IdxT n_samples)
{
auto output = raft::make_device_matrix<T, IdxT>(res, n_train, n_dim);
detail::sample_rows(res, input, n_rows_input, output, random_state);
auto output = raft::make_device_matrix<T, IdxT>(res, n_samples, dataset.extent(1));
detail::sample_rows(res, random_state, dataset.data_handle(), dataset.extent(0), output);
return output;
}

} // namespace raft::matrix
Loading
Loading