Skip to content

Commit 750381d

Browse files
Michael Norrisfacebook-github-bot
Michael Norris
authored andcommitted
Add more unit testing for HNSW [4/n] (facebookresearch#4061)
Summary: Pull Request resolved: facebookresearch#4061 search_level_0 is not unit tested yet. It isn't called anywhere so we can't test from the Index level, but it may be in the future, so adding a quick test for it. Reviewed By: junjieqi Differential Revision: D66800813 fbshipit-source-id: 5f1cf189b43610bc6c40c324343b5a55789d88ab
1 parent b5a5846 commit 750381d

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

tests/test_hnsw.cpp

+43
Original file line numberDiff line numberDiff line change
@@ -581,3 +581,46 @@ TEST_F(HNSWTest, TEST_search_neighbors_to_add) {
581581
reference_link_targets.pop();
582582
}
583583
}
584+
585+
TEST_F(HNSWTest, TEST_search_level_0) {
586+
omp_set_num_threads(1);
587+
std::vector<faiss::idx_t> I(k * nq);
588+
std::vector<float> D(k * nq);
589+
590+
using RH = faiss::HeapBlockResultHandler<faiss::HNSW::C>;
591+
RH bres1(nq, D.data(), I.data(), k);
592+
faiss::HeapBlockResultHandler<faiss::HNSW::C>::SingleResultHandler res1(
593+
bres1);
594+
RH bres2(nq, D.data(), I.data(), k);
595+
faiss::HeapBlockResultHandler<faiss::HNSW::C>::SingleResultHandler res2(
596+
bres2);
597+
598+
faiss::HNSWStats stats1, stats2;
599+
faiss::VisitedTable vt1(index->ntotal);
600+
faiss::VisitedTable vt2(index->ntotal);
601+
auto nprobe = 5;
602+
const faiss::HNSW::storage_idx_t values[] = {1, 2, 3, 4, 5};
603+
const faiss::HNSW::storage_idx_t* nearest_i = values;
604+
const float distances[] = {0.1, 0.2, 0.3, 0.4, 0.5};
605+
const float* nearest_d = distances;
606+
607+
// search_type == 1
608+
res1.begin(0);
609+
index->hnsw.search_level_0(
610+
*dis, res1, nprobe, nearest_i, nearest_d, 1, stats1, vt1, nullptr);
611+
res1.end();
612+
613+
// search_type == 2
614+
res2.begin(0);
615+
index->hnsw.search_level_0(
616+
*dis, res2, nprobe, nearest_i, nearest_d, 2, stats2, vt2, nullptr);
617+
res2.end();
618+
619+
// search_type 1 calls search_from_candidates in a loop nprobe times.
620+
// search_type 2 pushes the candidates and just calls search_from_candidates
621+
// once, so those stats will be much less.
622+
EXPECT_GT(stats1.ndis, stats2.ndis);
623+
EXPECT_GT(stats1.nhops, stats2.nhops);
624+
EXPECT_GT(stats1.n1, stats2.n1);
625+
EXPECT_GT(stats1.n2, stats2.n2);
626+
}

0 commit comments

Comments
 (0)