|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, NVIDIA CORPORATION. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include <cstdint> |
| 18 | +#include <dlpack/dlpack.h> |
| 19 | + |
| 20 | +#include <raft/core/copy.hpp> |
| 21 | +#include <raft/core/error.hpp> |
| 22 | +#include <raft/core/mdspan_types.hpp> |
| 23 | +#include <raft/core/resource/cuda_stream.hpp> |
| 24 | +#include <raft/core/resources.hpp> |
| 25 | +#include <raft/core/serialize.hpp> |
| 26 | + |
| 27 | +#include <cuvs/core/c_api.h> |
| 28 | +#include <cuvs/core/exceptions.hpp> |
| 29 | +#include <cuvs/core/interop.hpp> |
| 30 | +#include <cuvs/neighbors/nn_descent.h> |
| 31 | +#include <cuvs/neighbors/nn_descent.hpp> |
| 32 | + |
| 33 | +#include <fstream> |
| 34 | + |
| 35 | +namespace { |
| 36 | + |
| 37 | +template <typename T, typename IdxT = uint32_t> |
| 38 | +void* _build(cuvsResources_t res, |
| 39 | + cuvsNNDescentIndexParams params, |
| 40 | + DLManagedTensor* dataset_tensor, |
| 41 | + DLManagedTensor* graph_tensor) |
| 42 | +{ |
| 43 | + auto res_ptr = reinterpret_cast<raft::resources*>(res); |
| 44 | + auto dataset = dataset_tensor->dl_tensor; |
| 45 | + |
| 46 | + auto build_params = cuvs::neighbors::nn_descent::index_params(); |
| 47 | + build_params.metric = static_cast<cuvs::distance::DistanceType>((int)params.metric), |
| 48 | + build_params.metric_arg = params.metric_arg; |
| 49 | + build_params.graph_degree = params.graph_degree; |
| 50 | + build_params.intermediate_graph_degree = params.intermediate_graph_degree; |
| 51 | + build_params.max_iterations = params.max_iterations; |
| 52 | + build_params.termination_threshold = params.termination_threshold; |
| 53 | + build_params.return_distances = params.return_distances; |
| 54 | + build_params.n_clusters = params.n_clusters; |
| 55 | + |
| 56 | + using graph_type = raft::host_matrix_view<IdxT, int64_t, raft::row_major>; |
| 57 | + std::optional<graph_type> graph; |
| 58 | + if (graph_tensor != NULL) { graph = cuvs::core::from_dlpack<graph_type>(graph_tensor); } |
| 59 | + |
| 60 | + if (cuvs::core::is_dlpack_device_compatible(dataset)) { |
| 61 | + using dataset_type = raft::device_matrix_view<T const, int64_t, raft::row_major>; |
| 62 | + auto dataset = cuvs::core::from_dlpack<dataset_type>(dataset_tensor); |
| 63 | + auto index = cuvs::neighbors::nn_descent::build(*res_ptr, build_params, dataset, graph); |
| 64 | + return new cuvs::neighbors::nn_descent::index<IdxT>(std::move(index)); |
| 65 | + } else if (cuvs::core::is_dlpack_host_compatible(dataset)) { |
| 66 | + using dataset_type = raft::host_matrix_view<T const, int64_t, raft::row_major>; |
| 67 | + auto dataset = cuvs::core::from_dlpack<dataset_type>(dataset_tensor); |
| 68 | + auto index = cuvs::neighbors::nn_descent::build(*res_ptr, build_params, dataset, graph); |
| 69 | + return new cuvs::neighbors::nn_descent::index<IdxT>(std::move(index)); |
| 70 | + } else { |
| 71 | + RAFT_FAIL("dataset must be accessible on host or device memory"); |
| 72 | + } |
| 73 | +} |
| 74 | +} // namespace |
| 75 | + |
| 76 | +extern "C" cuvsError_t cuvsNNDescentIndexCreate(cuvsNNDescentIndex_t* index) |
| 77 | +{ |
| 78 | + return cuvs::core::translate_exceptions([=] { *index = new cuvsNNDescentIndex{}; }); |
| 79 | +} |
| 80 | + |
| 81 | +extern "C" cuvsError_t cuvsNNDescentIndexDestroy(cuvsNNDescentIndex_t index_c_ptr) |
| 82 | +{ |
| 83 | + return cuvs::core::translate_exceptions([=] { |
| 84 | + auto index = *index_c_ptr; |
| 85 | + if ((index.dtype.code == kDLUInt) && (index.dtype.bits == 32)) { |
| 86 | + auto index_ptr = reinterpret_cast<cuvs::neighbors::nn_descent::index<uint32_t>*>(index.addr); |
| 87 | + delete index_ptr; |
| 88 | + } else { |
| 89 | + RAFT_FAIL( |
| 90 | + "Unsupported nn-descent index dtype: %d and bits: %d", index.dtype.code, index.dtype.bits); |
| 91 | + } |
| 92 | + delete index_c_ptr; |
| 93 | + }); |
| 94 | +} |
| 95 | + |
| 96 | +extern "C" cuvsError_t cuvsNNDescentBuild(cuvsResources_t res, |
| 97 | + cuvsNNDescentIndexParams_t params, |
| 98 | + DLManagedTensor* dataset_tensor, |
| 99 | + DLManagedTensor* graph_tensor, |
| 100 | + cuvsNNDescentIndex_t index) |
| 101 | +{ |
| 102 | + return cuvs::core::translate_exceptions([=] { |
| 103 | + index->dtype.code = kDLUInt; |
| 104 | + index->dtype.bits = 32; |
| 105 | + |
| 106 | + auto dtype = dataset_tensor->dl_tensor.dtype; |
| 107 | + |
| 108 | + if ((dtype.code == kDLFloat) && (dtype.bits == 32)) { |
| 109 | + index->addr = reinterpret_cast<uintptr_t>( |
| 110 | + _build<float, uint32_t>(res, *params, dataset_tensor, graph_tensor)); |
| 111 | + } else if ((dtype.code == kDLFloat) && (dtype.bits == 16)) { |
| 112 | + index->addr = reinterpret_cast<uintptr_t>( |
| 113 | + _build<half, uint32_t>(res, *params, dataset_tensor, graph_tensor)); |
| 114 | + } else if ((dtype.code == kDLInt) && (dtype.bits == 8)) { |
| 115 | + index->addr = reinterpret_cast<uintptr_t>( |
| 116 | + _build<int8_t, uint32_t>(res, *params, dataset_tensor, graph_tensor)); |
| 117 | + } else if ((dtype.code == kDLUInt) && (dtype.bits == 8)) { |
| 118 | + index->addr = reinterpret_cast<uintptr_t>( |
| 119 | + _build<uint8_t, uint32_t>(res, *params, dataset_tensor, graph_tensor)); |
| 120 | + } else { |
| 121 | + RAFT_FAIL("Unsupported nn-descent dataset dtype: %d and bits: %d", dtype.code, dtype.bits); |
| 122 | + } |
| 123 | + }); |
| 124 | +} |
| 125 | + |
| 126 | +extern "C" cuvsError_t cuvsNNDescentIndexParamsCreate(cuvsNNDescentIndexParams_t* params) |
| 127 | +{ |
| 128 | + return cuvs::core::translate_exceptions([=] { |
| 129 | + // get defaults from cpp parameters struct |
| 130 | + cuvs::neighbors::nn_descent::index_params cpp_params; |
| 131 | + |
| 132 | + *params = new cuvsNNDescentIndexParams{ |
| 133 | + .metric = cpp_params.metric, |
| 134 | + .metric_arg = cpp_params.metric_arg, |
| 135 | + .graph_degree = cpp_params.graph_degree, |
| 136 | + .intermediate_graph_degree = cpp_params.intermediate_graph_degree, |
| 137 | + .max_iterations = cpp_params.max_iterations, |
| 138 | + .termination_threshold = cpp_params.termination_threshold, |
| 139 | + .return_distances = cpp_params.return_distances, |
| 140 | + .n_clusters = cpp_params.n_clusters}; |
| 141 | + }); |
| 142 | +} |
| 143 | + |
| 144 | +extern "C" cuvsError_t cuvsNNDescentIndexParamsDestroy(cuvsNNDescentIndexParams_t params) |
| 145 | +{ |
| 146 | + return cuvs::core::translate_exceptions([=] { delete params; }); |
| 147 | +} |
| 148 | + |
| 149 | +extern "C" cuvsError_t cuvsNNDescentIndexGetGraph(cuvsNNDescentIndex_t index, |
| 150 | + DLManagedTensor* graph) |
| 151 | +{ |
| 152 | + return cuvs::core::translate_exceptions([=] { |
| 153 | + auto dtype = index->dtype; |
| 154 | + if ((dtype.code == kDLUInt) && (dtype.bits == 32)) { |
| 155 | + auto index_ptr = reinterpret_cast<cuvs::neighbors::nn_descent::index<uint32_t>*>(index->addr); |
| 156 | + using output_mdspan_type = raft::host_matrix_view<uint32_t, int64_t, raft::row_major>; |
| 157 | + auto dst = cuvs::core::from_dlpack<output_mdspan_type>(graph); |
| 158 | + auto src = index_ptr->graph(); |
| 159 | + |
| 160 | + RAFT_EXPECTS(src.extent(0) == dst.extent(0), "Output graph has incorrect number of rows"); |
| 161 | + RAFT_EXPECTS(src.extent(1) == dst.extent(1), "Output graph has incorrect number of cols"); |
| 162 | + std::copy(src.data_handle(), src.data_handle() + dst.size(), dst.data_handle()); |
| 163 | + } else { |
| 164 | + RAFT_FAIL("Unsupported nn-descent index dtype: %d and bits: %d", dtype.code, dtype.bits); |
| 165 | + } |
| 166 | + }); |
| 167 | +} |
0 commit comments