|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +#include <gtest/gtest.h> |
| 9 | + |
| 10 | +#include <cstddef> |
| 11 | +#include <cstdint> |
| 12 | +#include <limits> |
| 13 | +#include <random> |
| 14 | +#include <unordered_set> |
| 15 | +#include <vector> |
| 16 | + |
| 17 | +#include <faiss/impl/HNSW.h> |
| 18 | + |
| 19 | +int reference_pop_min(faiss::HNSW::MinimaxHeap& heap, float* vmin_out) { |
| 20 | + assert(heap.k > 0); |
| 21 | + // returns min. This is an O(n) operation |
| 22 | + int i = heap.k - 1; |
| 23 | + while (i >= 0) { |
| 24 | + if (heap.ids[i] != -1) |
| 25 | + break; |
| 26 | + i--; |
| 27 | + } |
| 28 | + if (i == -1) |
| 29 | + return -1; |
| 30 | + int imin = i; |
| 31 | + float vmin = heap.dis[i]; |
| 32 | + i--; |
| 33 | + while (i >= 0) { |
| 34 | + if (heap.ids[i] != -1 && heap.dis[i] < vmin) { |
| 35 | + vmin = heap.dis[i]; |
| 36 | + imin = i; |
| 37 | + } |
| 38 | + i--; |
| 39 | + } |
| 40 | + if (vmin_out) |
| 41 | + *vmin_out = vmin; |
| 42 | + int ret = heap.ids[imin]; |
| 43 | + heap.ids[imin] = -1; |
| 44 | + --heap.nvalid; |
| 45 | + |
| 46 | + return ret; |
| 47 | +} |
| 48 | + |
| 49 | +void test_popmin(int heap_size, int amount_to_put) { |
| 50 | + // create a heap |
| 51 | + faiss::HNSW::MinimaxHeap mm_heap(heap_size); |
| 52 | + |
| 53 | + using storage_idx_t = faiss::HNSW::storage_idx_t; |
| 54 | + |
| 55 | + std::default_random_engine rng(123 + heap_size * amount_to_put); |
| 56 | + std::uniform_int_distribution<storage_idx_t> u(0, 65536); |
| 57 | + std::uniform_real_distribution<float> uf(0, 1); |
| 58 | + |
| 59 | + // generate random unique indices |
| 60 | + std::unordered_set<storage_idx_t> indices; |
| 61 | + while (indices.size() < amount_to_put) { |
| 62 | + const storage_idx_t index = u(rng); |
| 63 | + indices.insert(index); |
| 64 | + } |
| 65 | + |
| 66 | + // put ones into the heap |
| 67 | + for (const auto index : indices) { |
| 68 | + mm_heap.push(index, uf(rng)); |
| 69 | + } |
| 70 | + |
| 71 | + // clone the heap |
| 72 | + faiss::HNSW::MinimaxHeap cloned_mm_heap = mm_heap; |
| 73 | + |
| 74 | + // takes ones out one by one |
| 75 | + while (mm_heap.size() > 0) { |
| 76 | + // compare heaps |
| 77 | + ASSERT_EQ(mm_heap.n, cloned_mm_heap.n); |
| 78 | + ASSERT_EQ(mm_heap.k, cloned_mm_heap.k); |
| 79 | + ASSERT_EQ(mm_heap.nvalid, cloned_mm_heap.nvalid); |
| 80 | + ASSERT_EQ(mm_heap.ids, cloned_mm_heap.ids); |
| 81 | + ASSERT_EQ(mm_heap.dis, cloned_mm_heap.dis); |
| 82 | + |
| 83 | + // use the reference pop_min for the cloned heap |
| 84 | + float cloned_vmin_dis = std::numeric_limits<float>::quiet_NaN(); |
| 85 | + storage_idx_t cloned_vmin_idx = |
| 86 | + reference_pop_min(cloned_mm_heap, &cloned_vmin_dis); |
| 87 | + |
| 88 | + float vmin_dis = std::numeric_limits<float>::quiet_NaN(); |
| 89 | + storage_idx_t vmin_idx = mm_heap.pop_min(&vmin_dis); |
| 90 | + |
| 91 | + // compare returns |
| 92 | + ASSERT_EQ(vmin_dis, cloned_vmin_dis); |
| 93 | + ASSERT_EQ(vmin_idx, cloned_vmin_idx); |
| 94 | + } |
| 95 | + |
| 96 | + // compare heaps again |
| 97 | + ASSERT_EQ(mm_heap.n, cloned_mm_heap.n); |
| 98 | + ASSERT_EQ(mm_heap.k, cloned_mm_heap.k); |
| 99 | + ASSERT_EQ(mm_heap.nvalid, cloned_mm_heap.nvalid); |
| 100 | + ASSERT_EQ(mm_heap.ids, cloned_mm_heap.ids); |
| 101 | + ASSERT_EQ(mm_heap.dis, cloned_mm_heap.dis); |
| 102 | +} |
| 103 | + |
| 104 | +TEST(HNSW, Test_popmin) { |
| 105 | + std::vector<size_t> sizes = {1, 2, 3, 4, 5, 7, 9, 11, 16, 27, 32}; |
| 106 | + for (const size_t size : sizes) { |
| 107 | + for (size_t amount = size; amount > 0; amount /= 2) { |
| 108 | + test_popmin(size, amount); |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments