|
| 1 | +// @lint-ignore-every LICENSELINT |
| 2 | +/* |
| 3 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | +/* |
| 9 | + * Copyright (c) 2026, NVIDIA CORPORATION. |
| 10 | + * |
| 11 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 12 | + * you may not use this file except in compliance with the License. |
| 13 | + * You may obtain a copy of the License at |
| 14 | + * |
| 15 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 16 | + * |
| 17 | + * Unless required by applicable law or agreed to in writing, software |
| 18 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 19 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 20 | + * See the License for the specific language governing permissions and |
| 21 | + * limitations under the License. |
| 22 | + */ |
| 23 | + |
| 24 | +#include <faiss/gpu/GpuIndexIVFRaBitQ.h> |
| 25 | + |
| 26 | +#include <faiss/gpu/utils/DeviceUtils.h> |
| 27 | +#include <faiss/gpu/impl/CuvsIVFRaBitQ.cuh> |
| 28 | + |
| 29 | +#include <algorithm> |
| 30 | +#include <limits> |
| 31 | + |
| 32 | +namespace faiss { |
| 33 | +namespace gpu { |
| 34 | + |
| 35 | +GpuIndexIVFRaBitQ::GpuIndexIVFRaBitQ( |
| 36 | + GpuResourcesProvider* provider, |
| 37 | + int dims, |
| 38 | + idx_t nlist, |
| 39 | + faiss::MetricType metric, |
| 40 | + GpuIndexIVFRaBitQConfig config) |
| 41 | + : GpuIndex(provider->getResources(), dims, metric, 0.0f, config), |
| 42 | + nlist_(nlist), |
| 43 | + ivfRabitqConfig_(config) { |
| 44 | + FAISS_THROW_IF_NOT_MSG(nlist > 0, "GpuIndexIVFRaBitQ requires nlist > 0"); |
| 45 | + FAISS_THROW_IF_NOT_MSG( |
| 46 | + nlist <= std::numeric_limits<uint32_t>::max(), |
| 47 | + "GpuIndexIVFRaBitQ nlist must fit in uint32_t"); |
| 48 | + FAISS_THROW_IF_NOT_MSG( |
| 49 | + config.bitsPerDim >= 1 && config.bitsPerDim <= 9, |
| 50 | + "GpuIndexIVFRaBitQ bitsPerDim must be in [1, 9]"); |
| 51 | + FAISS_THROW_IF_NOT_MSG( |
| 52 | + metric == METRIC_L2, "GpuIndexIVFRaBitQ supports METRIC_L2 only"); |
| 53 | + FAISS_THROW_IF_NOT_MSG( |
| 54 | + should_use_cuvs(config), |
| 55 | + "GpuIndexIVFRaBitQ requires a supported GPU and " |
| 56 | + "GpuIndexIVFRaBitQConfig::use_cuvs = true"); |
| 57 | + this->is_trained = false; |
| 58 | +} |
| 59 | + |
| 60 | +GpuIndexIVFRaBitQ::~GpuIndexIVFRaBitQ() = default; |
| 61 | + |
| 62 | +void GpuIndexIVFRaBitQ::train(idx_t n, const float* x) { |
| 63 | + DeviceScope scope(config_.device); |
| 64 | + FAISS_THROW_IF_NOT_MSG( |
| 65 | + n > 0, "GpuIndexIVFRaBitQ cannot train on an empty dataset"); |
| 66 | + |
| 67 | + if (is_trained) { |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + index_ = std::make_shared<CuvsIVFRaBitQ>( |
| 72 | + resources_.get(), d, nlist_, metric_type, ivfRabitqConfig_); |
| 73 | + index_->train(n, x); |
| 74 | + ntotal = n; |
| 75 | + is_trained = true; |
| 76 | +} |
| 77 | + |
| 78 | +void GpuIndexIVFRaBitQ::add(idx_t n, const float* x) { |
| 79 | + FAISS_THROW_IF_MSG( |
| 80 | + is_trained, |
| 81 | + "GpuIndexIVFRaBitQ does not support incremental additions; " |
| 82 | + "call reset() before building a new index"); |
| 83 | + train(n, x); |
| 84 | +} |
| 85 | + |
| 86 | +bool GpuIndexIVFRaBitQ::addImplRequiresIDs_() const { |
| 87 | + return false; |
| 88 | +} |
| 89 | + |
| 90 | +void GpuIndexIVFRaBitQ::addImpl_(idx_t, const float*, const idx_t*) { |
| 91 | + FAISS_THROW_MSG( |
| 92 | + "GpuIndexIVFRaBitQ does not support incremental additions; " |
| 93 | + "build the index with train() or the first add() call"); |
| 94 | +} |
| 95 | + |
| 96 | +void GpuIndexIVFRaBitQ::searchImpl_( |
| 97 | + idx_t n, |
| 98 | + const float* x, |
| 99 | + int k, |
| 100 | + float* distances, |
| 101 | + idx_t* labels, |
| 102 | + const SearchParameters* search_params) const { |
| 103 | + FAISS_ASSERT(is_trained && index_); |
| 104 | + FAISS_THROW_IF_NOT_MSG(k > 0, "GpuIndexIVFRaBitQ requires k > 0"); |
| 105 | + |
| 106 | + const auto* params = search_params |
| 107 | + ? dynamic_cast<const SearchParametersIVFRaBitQ*>(search_params) |
| 108 | + : nullptr; |
| 109 | + FAISS_THROW_IF_NOT_MSG( |
| 110 | + !search_params || params, |
| 111 | + "GpuIndexIVFRaBitQ requires SearchParametersIVFRaBitQ"); |
| 112 | + FAISS_THROW_IF_MSG( |
| 113 | + params && params->sel, |
| 114 | + "GpuIndexIVFRaBitQ does not support IDSelector filtering"); |
| 115 | + |
| 116 | + uint32_t nprobe = params ? params->nprobe : ivfRabitqConfig_.nprobe; |
| 117 | + const auto searchMode = |
| 118 | + params ? params->searchMode : ivfRabitqConfig_.searchMode; |
| 119 | + FAISS_THROW_IF_NOT_MSG( |
| 120 | + nprobe > 0, "GpuIndexIVFRaBitQ nprobe must be greater than zero"); |
| 121 | + nprobe = std::min(nprobe, static_cast<uint32_t>(nlist_)); |
| 122 | + |
| 123 | + Tensor<float, 2, true> queries(const_cast<float*>(x), {n, d}); |
| 124 | + Tensor<float, 2, true> outDistances(distances, {n, k}); |
| 125 | + Tensor<idx_t, 2, true> outLabels(labels, {n, k}); |
| 126 | + index_->search(queries, k, outDistances, outLabels, nprobe, searchMode); |
| 127 | +} |
| 128 | + |
| 129 | +void GpuIndexIVFRaBitQ::reset() { |
| 130 | + DeviceScope scope(config_.device); |
| 131 | + index_.reset(); |
| 132 | + ntotal = 0; |
| 133 | + is_trained = false; |
| 134 | +} |
| 135 | + |
| 136 | +} // namespace gpu |
| 137 | +} // namespace faiss |
0 commit comments