Skip to content

Commit 9db2430

Browse files
authored
Cleanup IVF PQ test and add better logging on failure (#406)
1 parent 1bab417 commit 9db2430

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

src/include/index/ivf_pq_index.h

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1543,7 +1543,7 @@ class ivf_pq_index {
15431543
if (::num_vectors(lhs) != ::num_vectors(rhs) ||
15441544
::dimensions(lhs) != ::dimensions(rhs)) {
15451545
std::cout << "num_vectors(lhs) != num_vectors(rhs) || dimensions(lhs) != "
1546-
"dimensions(rhs)n"
1546+
"dimensions(rhs)"
15471547
<< std::endl;
15481548
std::cout << "num_vectors(lhs): " << ::num_vectors(lhs)
15491549
<< " num_vectors(rhs): " << ::num_vectors(rhs) << std::endl;
@@ -1578,13 +1578,32 @@ class ivf_pq_index {
15781578
* @return
15791579
*/
15801580
template <feature_vector L, feature_vector R>
1581-
auto compare_feature_vectors(const L& lhs, const R& rhs) const {
1581+
auto compare_feature_vectors(
1582+
const L& lhs, const R& rhs, const std::string& msg = "") const {
15821583
if (::dimensions(lhs) != ::dimensions(rhs)) {
1583-
std::cout << "dimensions(lhs) != dimensions(rhs) (" << ::dimensions(lhs)
1584+
std::cout << "[ivf_pq_index@compare_feature_vectors] " << msg
1585+
<< " dimensions(lhs) != dimensions(rhs) (" << ::dimensions(lhs)
15841586
<< " != " << ::dimensions(rhs) << ")" << std::endl;
15851587
return false;
15861588
}
1587-
return std::equal(begin(lhs), end(lhs), begin(rhs));
1589+
auto equal = std::equal(begin(lhs), end(lhs), begin(rhs));
1590+
if (!equal) {
1591+
std::cout << "[ivf_pq_index@compare_feature_vectors] " << msg
1592+
<< " failed the equality check." << std::endl;
1593+
auto printed = 0;
1594+
for (size_t i = 0; i < ::dimensions(lhs); ++i) {
1595+
if (lhs[i] != rhs[i]) {
1596+
std::cout << " lhs[" << i << "]: " << lhs[i] << " - rhs[" << i
1597+
<< "]: " << rhs[i] << std::endl;
1598+
printed++;
1599+
}
1600+
if (printed > 50) {
1601+
std::cout << " ..." << std::endl;
1602+
break;
1603+
}
1604+
}
1605+
}
1606+
return equal;
15881607
}
15891608

15901609
auto compare_cluster_centroids(const ivf_pq_index& rhs) const {
@@ -1611,7 +1630,8 @@ class ivf_pq_index {
16111630
}
16121631
return compare_feature_vectors(
16131632
partitioned_pq_vectors_->indices(),
1614-
rhs.partitioned_pq_vectors_->indices());
1633+
rhs.partitioned_pq_vectors_->indices(),
1634+
"partitioned_pq_vectors_->indices()");
16151635
}
16161636

16171637
auto compare_ivf_ids(const ivf_pq_index& rhs) const {
@@ -1622,7 +1642,9 @@ class ivf_pq_index {
16221642
return false;
16231643
}
16241644
return compare_feature_vectors(
1625-
partitioned_pq_vectors_->ids(), rhs.partitioned_pq_vectors_->ids());
1645+
partitioned_pq_vectors_->ids(),
1646+
rhs.partitioned_pq_vectors_->ids(),
1647+
"partitioned_pq_vectors_->ids()");
16261648
}
16271649

16281650
auto compare_pq_ivf_vectors(const ivf_pq_index& rhs) const {

src/include/test/unit_ivf_pq_index.cc

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -310,25 +310,22 @@ TEST_CASE("ivf_index write and read", "[ivf_pq_index]") {
310310
vfs.remove_dir(ivf_index_uri);
311311
}
312312

313+
// Create and write an index.
313314
auto training_set = tdbColMajorMatrix<float>(ctx, siftsmall_inputs_uri, 0);
314315
load(training_set);
315316
std::vector<siftsmall_ids_type> ids(num_vectors(training_set));
316317
std::iota(begin(ids), end(ids), 0);
317318
auto idx = ivf_pq_index<float, uint32_t, uint32_t>(
318-
/*dimension,*/ nlist, num_subspaces, max_iters, nthreads);
319+
nlist, num_subspaces, max_iters, nthreads);
319320
idx.train_ivf(training_set, kmeans_init::kmeanspp);
320321
idx.add(training_set, ids);
321-
ivf_index_uri =
322-
(std::filesystem::temp_directory_path() / "second_tmp_ivf_index")
323-
.string();
324-
if (vfs.is_dir(ivf_index_uri)) {
325-
vfs.remove_dir(ivf_index_uri);
326-
}
327-
328322
idx.write_index(ctx, ivf_index_uri);
323+
324+
// Load it from URI.
329325
auto idx2 = ivf_pq_index<float, uint32_t, uint32_t>(ctx, ivf_index_uri);
330326
idx2.read_index_infinite();
331327

328+
// Check that the two indexes are the same.
332329
CHECK(idx.compare_cached_metadata(idx2));
333330
CHECK(idx.compare_cached_metadata(idx2));
334331
CHECK(idx.compare_cluster_centroids(idx2));

0 commit comments

Comments
 (0)