|
| 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 | + float distance = uf(rng); |
| 69 | + if (distance >= 0.7f) { |
| 70 | + // add infinity values from time to time |
| 71 | + distance = std::numeric_limits<float>::infinity(); |
| 72 | + } |
| 73 | + mm_heap.push(index, distance); |
| 74 | + } |
| 75 | + |
| 76 | + // clone the heap |
| 77 | + faiss::HNSW::MinimaxHeap cloned_mm_heap = mm_heap; |
| 78 | + |
| 79 | + // takes ones out one by one |
| 80 | + while (mm_heap.size() > 0) { |
| 81 | + // compare heaps |
| 82 | + ASSERT_EQ(mm_heap.n, cloned_mm_heap.n); |
| 83 | + ASSERT_EQ(mm_heap.k, cloned_mm_heap.k); |
| 84 | + ASSERT_EQ(mm_heap.nvalid, cloned_mm_heap.nvalid); |
| 85 | + ASSERT_EQ(mm_heap.ids, cloned_mm_heap.ids); |
| 86 | + ASSERT_EQ(mm_heap.dis, cloned_mm_heap.dis); |
| 87 | + |
| 88 | + // use the reference pop_min for the cloned heap |
| 89 | + float cloned_vmin_dis = std::numeric_limits<float>::quiet_NaN(); |
| 90 | + storage_idx_t cloned_vmin_idx = |
| 91 | + reference_pop_min(cloned_mm_heap, &cloned_vmin_dis); |
| 92 | + |
| 93 | + float vmin_dis = std::numeric_limits<float>::quiet_NaN(); |
| 94 | + storage_idx_t vmin_idx = mm_heap.pop_min(&vmin_dis); |
| 95 | + |
| 96 | + // compare returns |
| 97 | + ASSERT_EQ(vmin_dis, cloned_vmin_dis); |
| 98 | + ASSERT_EQ(vmin_idx, cloned_vmin_idx); |
| 99 | + } |
| 100 | + |
| 101 | + // compare heaps again |
| 102 | + ASSERT_EQ(mm_heap.n, cloned_mm_heap.n); |
| 103 | + ASSERT_EQ(mm_heap.k, cloned_mm_heap.k); |
| 104 | + ASSERT_EQ(mm_heap.nvalid, cloned_mm_heap.nvalid); |
| 105 | + ASSERT_EQ(mm_heap.ids, cloned_mm_heap.ids); |
| 106 | + ASSERT_EQ(mm_heap.dis, cloned_mm_heap.dis); |
| 107 | +} |
| 108 | + |
| 109 | +void test_popmin_identical_distances( |
| 110 | + int heap_size, |
| 111 | + int amount_to_put, |
| 112 | + const float distance) { |
| 113 | + // create a heap |
| 114 | + faiss::HNSW::MinimaxHeap mm_heap(heap_size); |
| 115 | + |
| 116 | + using storage_idx_t = faiss::HNSW::storage_idx_t; |
| 117 | + |
| 118 | + std::default_random_engine rng(123 + heap_size * amount_to_put); |
| 119 | + std::uniform_int_distribution<storage_idx_t> u(0, 65536); |
| 120 | + |
| 121 | + // generate random unique indices |
| 122 | + std::unordered_set<storage_idx_t> indices; |
| 123 | + while (indices.size() < amount_to_put) { |
| 124 | + const storage_idx_t index = u(rng); |
| 125 | + indices.insert(index); |
| 126 | + } |
| 127 | + |
| 128 | + // put ones into the heap |
| 129 | + for (const auto index : indices) { |
| 130 | + mm_heap.push(index, distance); |
| 131 | + } |
| 132 | + |
| 133 | + // clone the heap |
| 134 | + faiss::HNSW::MinimaxHeap cloned_mm_heap = mm_heap; |
| 135 | + |
| 136 | + // takes ones out one by one |
| 137 | + while (mm_heap.size() > 0) { |
| 138 | + // compare heaps |
| 139 | + ASSERT_EQ(mm_heap.n, cloned_mm_heap.n); |
| 140 | + ASSERT_EQ(mm_heap.k, cloned_mm_heap.k); |
| 141 | + ASSERT_EQ(mm_heap.nvalid, cloned_mm_heap.nvalid); |
| 142 | + ASSERT_EQ(mm_heap.ids, cloned_mm_heap.ids); |
| 143 | + ASSERT_EQ(mm_heap.dis, cloned_mm_heap.dis); |
| 144 | + |
| 145 | + // use the reference pop_min for the cloned heap |
| 146 | + float cloned_vmin_dis = std::numeric_limits<float>::quiet_NaN(); |
| 147 | + storage_idx_t cloned_vmin_idx = |
| 148 | + reference_pop_min(cloned_mm_heap, &cloned_vmin_dis); |
| 149 | + |
| 150 | + float vmin_dis = std::numeric_limits<float>::quiet_NaN(); |
| 151 | + storage_idx_t vmin_idx = mm_heap.pop_min(&vmin_dis); |
| 152 | + |
| 153 | + // compare returns |
| 154 | + ASSERT_EQ(vmin_dis, cloned_vmin_dis); |
| 155 | + ASSERT_EQ(vmin_idx, cloned_vmin_idx); |
| 156 | + } |
| 157 | + |
| 158 | + // compare heaps again |
| 159 | + ASSERT_EQ(mm_heap.n, cloned_mm_heap.n); |
| 160 | + ASSERT_EQ(mm_heap.k, cloned_mm_heap.k); |
| 161 | + ASSERT_EQ(mm_heap.nvalid, cloned_mm_heap.nvalid); |
| 162 | + ASSERT_EQ(mm_heap.ids, cloned_mm_heap.ids); |
| 163 | + ASSERT_EQ(mm_heap.dis, cloned_mm_heap.dis); |
| 164 | +} |
| 165 | + |
| 166 | +TEST(HNSW, Test_popmin) { |
| 167 | + std::vector<size_t> sizes = {1, 2, 3, 4, 5, 7, 9, 11, 16, 27, 32, 64, 128}; |
| 168 | + for (const size_t size : sizes) { |
| 169 | + for (size_t amount = size; amount > 0; amount /= 2) { |
| 170 | + test_popmin(size, amount); |
| 171 | + } |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +TEST(HNSW, Test_popmin_identical_distances) { |
| 176 | + std::vector<size_t> sizes = {1, 2, 3, 4, 5, 7, 9, 11, 16, 27, 32}; |
| 177 | + for (const size_t size : sizes) { |
| 178 | + for (size_t amount = size; amount > 0; amount /= 2) { |
| 179 | + test_popmin_identical_distances(size, amount, 1.0f); |
| 180 | + } |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +TEST(HNSW, Test_popmin_infinite_distances) { |
| 185 | + std::vector<size_t> sizes = {1, 2, 3, 4, 5, 7, 9, 11, 16, 27, 32}; |
| 186 | + for (const size_t size : sizes) { |
| 187 | + for (size_t amount = size; amount > 0; amount /= 2) { |
| 188 | + test_popmin_identical_distances( |
| 189 | + size, amount, std::numeric_limits<float>::infinity()); |
| 190 | + } |
| 191 | + } |
| 192 | +} |
0 commit comments