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

Serialize only knn graph with CAGRA bench #1743

Closed
Closed
Changes from all commits
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
80 changes: 76 additions & 4 deletions cpp/bench/ann/src/raft/raft_cagra_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@
#include <fstream>
#include <iostream>
#include <memory>
#include <raft/core/detail/mdspan_numpy_serializer.hpp>
#include <raft/core/device_mdspan.hpp>
#include <raft/core/device_resources.hpp>
#include <raft/core/logger.hpp>
#include <raft/core/operators.hpp>
#include <raft/core/resource/cuda_stream.hpp>
#include <raft/core/serialize.hpp>
#include <raft/distance/detail/distance.cuh>
#include <raft/distance/distance_types.hpp>
#include <raft/linalg/unary_op.cuh>
Expand Down Expand Up @@ -58,6 +61,7 @@ class RaftCagra : public ANN<T> {

void set_search_param(const AnnSearchParam& param) override;

void set_search_dataset(const T* dataset, size_t nrow) override;
// TODO: if the number of results is less than k, the remaining elements of 'neighbors'
// will be filled with (size_t)-1
void search(const T* queries,
Expand Down Expand Up @@ -85,6 +89,7 @@ class RaftCagra : public ANN<T> {
raft::device_resources handle_;
BuildParam index_params_;
raft::neighbors::cagra::search_params search_params_;
raft::device_matrix<IdxT, int64_t, row_major> graph_;
std::optional<raft::neighbors::cagra::index<T, IdxT>> index_;
int device_;
int dimension_;
Expand All @@ -96,7 +101,8 @@ RaftCagra<T, IdxT>::RaftCagra(Metric metric, int dim, const BuildParam& param)
: ANN<T>(metric, dim),
index_params_(param),
dimension_(dim),
mr_(rmm::mr::get_current_device_resource(), 1024 * 1024 * 1024ull)
mr_(rmm::mr::get_current_device_resource(), 1024 * 1024 * 1024ull),
graph_(make_device_matrix<IdxT, int64_t, row_major>(handle_, 0, 0))
{
rmm::mr::set_current_device_resource(&mr_);
index_params_.metric = parse_metric_type(metric);
Expand Down Expand Up @@ -129,15 +135,81 @@ void RaftCagra<T, IdxT>::set_search_param(const AnnSearchParam& param)
template <typename T, typename IdxT>
void RaftCagra<T, IdxT>::save(const std::string& file) const
{
raft::neighbors::cagra::serialize(handle_, file, *index_);
// 1 orig serialization: save both dataset and knn graph into the file
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@benfred and I were literally just talking about this. We have also agreed in the past that we should be making serializing the dataset optional and I believe we also converged on providing a update_dataset() method right on the index to alleviate the awkwardness of having to accept a dataset at deserialization time.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, we can just construct the index from the two arrays like here
https://github.com/rapidsai/raft/pull/1743/files#diff-5f08a0aeb75c8884f5142d218e97ec859de8cc0cffd98d8c08f0e18a45655da0R211-R212

Saving the knn-graph mdspan is trivial with the current helpers, but we could add the following code block as a helper to make it easy to load an mdspan:
https://github.com/rapidsai/raft/pull/1743/files#diff-5f08a0aeb75c8884f5142d218e97ec859de8cc0cffd98d8c08f0e18a45655da0R175-R181

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a PR up here #1781 which doesn't write out the dataset on serialize (by using the include_dataset param from #1755) and update_dataset to set the dataset after deserialize.

// raft::neighbors::cagra::serialize(handle_, file, *index_);

// 2. Saving only knn graph
// We use numpy serialization format.
// std::ofstream of(file, std::ios::out | std::ios::binary);
// serialize_mdspan(handle_, of, index_->graph());
// of.close();

// 3. Orig CAGRA type of serialization, saving only knn-graph
size_t degree = index_->graph_degree();

std::ofstream of(file, std::ios::out | std::ios::binary);
std::size_t size = index_->size();

of.write(reinterpret_cast<char*>(&size), sizeof(size));
of.write(reinterpret_cast<char*>(&degree), sizeof(degree));

auto graph_h = make_host_matrix<IdxT, int64_t>(size, degree);
raft::copy(graph_h.data_handle(),
index_->graph().data_handle(),
index_->graph().size(),
resource::get_cuda_stream(handle_));
resource::sync_stream(handle_);

of.write(reinterpret_cast<char*>(graph_h.data_handle()), graph_h.size() * sizeof(IdxT));

of.close();
return;
}

template <typename T, typename IdxT>
void RaftCagra<T, IdxT>::load(const std::string& file)
{
index_ = raft::neighbors::cagra::deserialize<T, IdxT>(handle_, file);
return;
// 1. Original index saving method, we load both dataset and index
// index_ = raft::neighbors::cagra::deserialize<T, IdxT>(handle_, file);

// // 2. Read only knn_graph. In theory his could also load file saved withnumpy.save()
// std::ifstream is(file, std::ios::in | std::ios::binary);
// raft::detail::numpy_serializer::header_t header =
// raft::detail::numpy_serializer::read_header(is); is.seekg(0); /* rewind*/ graph_ =
// make_device_matrix<IdxT, int64_t>(handle_, header.shape[0], header.shape[1]);
// deserialize_mdspan(handle_, is, graph_.view());
// is.close();

// 3. Read only knn graph, using Cagra's knn file format
std::ifstream ifs(file, std::ios::in | std::ios::binary);
if (!ifs) {
throw std::runtime_error("File not exist : " + file + " (`" + __func__ + "` in " + __FILE__ +
")");
}

std::size_t size, degree;

ifs.read(reinterpret_cast<char*>(&size), sizeof(size));
ifs.read(reinterpret_cast<char*>(&degree), sizeof(degree));

auto graph_h = make_host_matrix<IdxT, int64_t>(size, degree);
graph_ = make_device_matrix<IdxT, int64_t>(handle_, size, degree);

for (std::size_t i = 0; i < size; i++) {
ifs.read(reinterpret_cast<char*>(graph_h.data_handle() + i * degree), sizeof(IdxT) * degree);
}
ifs.close();
raft::copy(
graph_.data_handle(), graph_h.data_handle(), graph_.size(), resource::get_cuda_stream(handle_));
resource::sync_stream(handle_);
}

template <typename T, typename IdxT>
void RaftCagra<T, IdxT>::set_search_dataset(const T* dataset, size_t nrow)
{
auto dataset_v = raft::make_host_matrix_view<const T, int64_t>(dataset, nrow, this->dim_);
index_.emplace(
handle_, parse_metric_type(this->metric_), dataset_v, make_const_mdspan(graph_.view()));
}

template <typename T, typename IdxT>
Expand Down