|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +#pragma once |
| 7 | + |
| 8 | +#include <cuvs/core/export.hpp> |
| 9 | +#include <raft/core/device_mdarray.hpp> |
| 10 | +#include <raft/core/resources.hpp> |
| 11 | + |
| 12 | +#include <cstddef> |
| 13 | +#include <cstdint> |
| 14 | +#include <memory> |
| 15 | + |
| 16 | +namespace CUVS_EXPORT cuvs { |
| 17 | +namespace core { |
| 18 | + |
| 19 | +/** |
| 20 | + * @brief cuVS-owned Bloom filter wrapper with opaque implementation. |
| 21 | + * |
| 22 | + * This class intentionally hides cuCollections types from the cuVS public API. |
| 23 | + * The wrapper supports the expected bulk host APIs used by ANN workflows. |
| 24 | + */ |
| 25 | +class CUVS_EXPORT bloom_filter { |
| 26 | + private: |
| 27 | + struct impl; |
| 28 | + |
| 29 | + public: |
| 30 | + using key_type = std::uint32_t; |
| 31 | + |
| 32 | + /** |
| 33 | + * @brief Construct a Bloom filter with user-facing quality knobs. |
| 34 | + * |
| 35 | + * @p dataset_rows is the number of rows in the indexed dataset. The filter uses it with |
| 36 | + * @p filtering_rate to estimate the number of inserted valid ids and compute a target filter |
| 37 | + * size that satisfies the requested false-positive rate. |
| 38 | + * |
| 39 | + * The primary tuning knobs are: |
| 40 | + * - @p filtering_rate: expected fraction of dataset rows that will be inserted as valid ids. |
| 41 | + * - @p target_false_positive_rate: desired Bloom filter false-positive probability. |
| 42 | + * |
| 43 | + * Sizing math used internally: |
| 44 | + * - `expected_insertions = ceil(dataset_rows * filtering_rate)` |
| 45 | + * - The default policy uses 256-bit blocks split into eight 32-bit words and sets one bit in each |
| 46 | + * word per inserted key. |
| 47 | + * - For each candidate block count, the expected false-positive rate accounts for the binomial |
| 48 | + * distribution of inserted keys across blocks and the fixed eight-bit fingerprint. |
| 49 | + * - The smallest block count whose expected false-positive rate meets |
| 50 | + * @p target_false_positive_rate is selected. |
| 51 | + * |
| 52 | + * Practical knob behavior: |
| 53 | + * - Lower @p target_false_positive_rate -> larger filter, fewer false positives, typically higher |
| 54 | + * filtered-search recall. |
| 55 | + * - Higher @p filtering_rate -> larger filter for the same target false-positive rate. |
| 56 | + */ |
| 57 | + bloom_filter(raft::resources const& res, |
| 58 | + std::size_t dataset_rows, |
| 59 | + float filtering_rate = 1.0f, |
| 60 | + float target_false_positive_rate = 0.01f); |
| 61 | + ~bloom_filter(); |
| 62 | + |
| 63 | + bloom_filter(bloom_filter const&) = delete; |
| 64 | + bloom_filter& operator=(bloom_filter const&) = delete; |
| 65 | + bloom_filter(bloom_filter&&) noexcept; |
| 66 | + bloom_filter& operator=(bloom_filter&&) noexcept; |
| 67 | + |
| 68 | + void clear(raft::resources const& res); |
| 69 | + void clear_async(raft::resources const& res); |
| 70 | + |
| 71 | + void add(raft::resources const& res, raft::device_vector_view<const key_type, int64_t> keys); |
| 72 | + void add_async(raft::resources const& res, |
| 73 | + raft::device_vector_view<const key_type, int64_t> keys); |
| 74 | + |
| 75 | + void contains(raft::resources const& res, |
| 76 | + raft::device_vector_view<const key_type, int64_t> keys, |
| 77 | + raft::device_vector_view<std::uint8_t, int64_t> output) const; |
| 78 | + void contains_async(raft::resources const& res, |
| 79 | + raft::device_vector_view<const key_type, int64_t> keys, |
| 80 | + raft::device_vector_view<std::uint8_t, int64_t> output) const; |
| 81 | + |
| 82 | + [[nodiscard]] std::size_t num_blocks() const noexcept; |
| 83 | + |
| 84 | + /** |
| 85 | + * @brief Return the estimated fraction of dataset rows rejected by this filter. |
| 86 | + * |
| 87 | + * The estimate is derived at construction from the configured valid-row fraction and the |
| 88 | + * expected false-positive rate of the selected filter geometry. It performs no device work. |
| 89 | + */ |
| 90 | + [[nodiscard]] float estimate_filtering_rate() const noexcept; |
| 91 | + |
| 92 | + private: |
| 93 | + friend impl const& get_bloom_filter_impl(bloom_filter const& filter) noexcept; |
| 94 | + |
| 95 | + std::unique_ptr<impl> impl_; |
| 96 | +}; |
| 97 | + |
| 98 | +} // namespace core |
| 99 | +} // namespace CUVS_EXPORT cuvs |
0 commit comments