From 5b6c4b40c80733fa955a660c2173db328ecd7d79 Mon Sep 17 00:00:00 2001 From: Matthijs Douze Date: Thu, 7 Dec 2023 06:40:34 -0800 Subject: [PATCH] Back out "printf -> fmt::print in files inc faiss/IndexBinaryHNSW.cpp" (#3164) Summary: Pull Request resolved: https://github.com/facebookresearch/faiss/pull/3164 The original diff breaks the open-source Faiss compiles. Original commit changeset: 230540b26ec8 Original Phabricator Diff: D51486397 Reviewed By: algoriddle Differential Revision: D51938993 fbshipit-source-id: a57433c4267493d2fe2249e8f4191612c0f1da59 --- faiss/IndexBinaryHNSW.cpp | 21 +++++----- faiss/IndexBinaryHash.cpp | 7 ++-- faiss/IndexBinaryIVF.cpp | 22 +++++------ faiss/IndexFastScan.cpp | 3 +- faiss/IndexHNSW.cpp | 37 ++++++++---------- faiss/IndexIVF.cpp | 43 +++++++++------------ faiss/IndexIVFAdditiveQuantizerFastScan.cpp | 20 +++++----- faiss/IndexIVFFastScan.cpp | 14 +++---- faiss/IndexIVFFlat.cpp | 34 +++++++--------- faiss/IndexIVFIndependentQuantizer.cpp | 12 ++---- 10 files changed, 91 insertions(+), 122 deletions(-) diff --git a/faiss/IndexBinaryHNSW.cpp b/faiss/IndexBinaryHNSW.cpp index b673de1fd1..e6fda8e4bf 100644 --- a/faiss/IndexBinaryHNSW.cpp +++ b/faiss/IndexBinaryHNSW.cpp @@ -31,7 +31,6 @@ #include #include #include -#include namespace faiss { @@ -52,18 +51,17 @@ void hnsw_add_vertices( size_t ntotal = n0 + n; double t0 = getmillisecs(); if (verbose) { - fmt::print( - "hnsw_add_vertices: adding {} elements on top of {} " - "(preset_levels={})\n", - n, - n0, - int(preset_levels)); + printf("hnsw_add_vertices: adding %zd elements on top of %zd " + "(preset_levels=%d)\n", + n, + n0, + int(preset_levels)); } int max_level = hnsw.prepare_level_tab(n, preset_levels); if (verbose) { - fmt::print(" max_level = {}\n", max_level); + printf(" max_level = %d\n", max_level); } std::vector locks(ntotal); @@ -110,8 +108,7 @@ void hnsw_add_vertices( int i0 = i1 - hist[pt_level]; if (verbose) { - fmt::print( - "Adding {} elements at level {}\n", i1 - i0, pt_level); + printf("Adding %d elements at level %d\n", i1 - i0, pt_level); } // random permutation to get rid of dataset order bias @@ -138,7 +135,7 @@ void hnsw_add_vertices( if (prev_display >= 0 && i - i0 > prev_display + 10000) { prev_display = i - i0; - fmt::print(" {} / {}\r", i - i0, i1 - i0); + printf(" %d / %d\r", i - i0, i1 - i0); fflush(stdout); } } @@ -148,7 +145,7 @@ void hnsw_add_vertices( FAISS_ASSERT(i1 == 0); } if (verbose) { - fmt::print("Done in {:.3f} ms\n", getmillisecs() - t0); + printf("Done in %.3f ms\n", getmillisecs() - t0); } for (int i = 0; i < ntotal; i++) diff --git a/faiss/IndexBinaryHash.cpp b/faiss/IndexBinaryHash.cpp index 4f5d5dbeaf..86a6d52ded 100644 --- a/faiss/IndexBinaryHash.cpp +++ b/faiss/IndexBinaryHash.cpp @@ -20,7 +20,6 @@ #include #include #include -#include namespace faiss { @@ -269,12 +268,12 @@ size_t IndexBinaryHash::hashtable_size() const { void IndexBinaryHash::display() const { for (auto it = invlists.begin(); it != invlists.end(); ++it) { - fmt::print("%" PRId64 ": [", it->first); + printf("%" PRId64 ": [", it->first); const std::vector& v = it->second.ids; for (auto x : v) { - fmt::print("%" PRId64 " ", x); + printf("%" PRId64 " ", x); } - fmt::print("]\n"); + printf("]\n"); } } diff --git a/faiss/IndexBinaryIVF.cpp b/faiss/IndexBinaryIVF.cpp index 69e1b4509a..f49127f6b2 100644 --- a/faiss/IndexBinaryIVF.cpp +++ b/faiss/IndexBinaryIVF.cpp @@ -23,7 +23,6 @@ #include #include #include -#include namespace faiss { @@ -88,11 +87,10 @@ void IndexBinaryIVF::add_core( n_add++; } if (verbose) { - fmt::print( - "IndexBinaryIVF::add_with_ids: added " - "%" PRId64 " / %" PRId64 " vectors\n", - n_add, - n); + printf("IndexBinaryIVF::add_with_ids: added " + "%" PRId64 " / %" PRId64 " vectors\n", + n_add, + n); } ntotal += n_add; } @@ -235,17 +233,16 @@ size_t IndexBinaryIVF::remove_ids(const IDSelector& sel) { void IndexBinaryIVF::train(idx_t n, const uint8_t* x) { if (verbose) { - fmt::print("Training quantizer\n"); + printf("Training quantizer\n"); } if (quantizer->is_trained && (quantizer->ntotal == nlist)) { if (verbose) { - fmt::print("IVF quantizer does not need training.\n"); + printf("IVF quantizer does not need training.\n"); } } else { if (verbose) { - fmt::print( - "Training quantizer on %" PRId64 " vectors in {}D\n", n, d); + printf("Training quantizer on %" PRId64 " vectors in %dD\n", n, d); } Clustering clus(d, nlist, cp); @@ -254,9 +251,8 @@ void IndexBinaryIVF::train(idx_t n, const uint8_t* x) { IndexFlatL2 index_tmp(d); if (clustering_index && verbose) { - fmt::print( - "using clustering_index of dimension {} to do the clustering\n", - clustering_index->d); + printf("using clustering_index of dimension %d to do the clustering\n", + clustering_index->d); } // LSH codec that is able to convert the binary vectors to floats. diff --git a/faiss/IndexFastScan.cpp b/faiss/IndexFastScan.cpp index 76486f7c54..02840767d1 100644 --- a/faiss/IndexFastScan.cpp +++ b/faiss/IndexFastScan.cpp @@ -26,7 +26,6 @@ #include #include #include -#include namespace faiss { @@ -74,7 +73,7 @@ void IndexFastScan::add(idx_t n, const float* x) { for (idx_t i0 = 0; i0 < n; i0 += bs) { idx_t i1 = std::min(n, i0 + bs); if (verbose) { - fmt::print("IndexFastScan::add {}/{}\n", size_t(i1), size_t(n)); + printf("IndexFastScan::add %zd/%zd\n", size_t(i1), size_t(n)); } add(i1 - i0, x + i0 * d); } diff --git a/faiss/IndexHNSW.cpp b/faiss/IndexHNSW.cpp index f81f42fcf3..8c0e0afde8 100644 --- a/faiss/IndexHNSW.cpp +++ b/faiss/IndexHNSW.cpp @@ -33,7 +33,6 @@ #include #include #include -#include extern "C" { @@ -135,12 +134,11 @@ void hnsw_add_vertices( size_t ntotal = n0 + n; double t0 = getmillisecs(); if (verbose) { - fmt::print( - "hnsw_add_vertices: adding {} elements on top of {} " - "(preset_levels={})\n", - n, - n0, - int(preset_levels)); + printf("hnsw_add_vertices: adding %zd elements on top of %zd " + "(preset_levels=%d)\n", + n, + n0, + int(preset_levels)); } if (n == 0) { @@ -150,7 +148,7 @@ void hnsw_add_vertices( int max_level = hnsw.prepare_level_tab(n, preset_levels); if (verbose) { - fmt::print(" max_level = {}\n", max_level); + printf(" max_level = %d\n", max_level); } std::vector locks(ntotal); @@ -198,8 +196,7 @@ void hnsw_add_vertices( int i0 = i1 - hist[pt_level]; if (verbose) { - fmt::print( - "Adding {} elements at level {}\n", i1 - i0, pt_level); + printf("Adding %d elements at level %d\n", i1 - i0, pt_level); } // random permutation to get rid of dataset order bias @@ -235,7 +232,7 @@ void hnsw_add_vertices( if (prev_display >= 0 && i - i0 > prev_display + 10000) { prev_display = i - i0; - fmt::print(" {} / {}\r", i - i0, i1 - i0); + printf(" %d / %d\r", i - i0, i1 - i0); fflush(stdout); } if (counter % check_period == 0) { @@ -254,7 +251,7 @@ void hnsw_add_vertices( FAISS_ASSERT(i1 == 0); } if (verbose) { - fmt::print("Done in {:.3f} ms\n", getmillisecs() - t0); + printf("Done in %.3f ms\n", getmillisecs() - t0); } for (int i = 0; i < ntotal; i++) { @@ -541,13 +538,13 @@ void IndexHNSW::init_level_0_from_entry_points( *dis, pt_id, nearest, (*dis)(nearest), 0, locks.data(), vt); if (verbose && i % 10000 == 0) { - fmt::print(" {} / {}\r", i, n); + printf(" %d / %d\r", i, n); fflush(stdout); } } } if (verbose) { - fmt::print("\n"); + printf("\n"); } for (int i = 0; i < ntotal; i++) @@ -589,7 +586,7 @@ void IndexHNSW::reorder_links() { } void IndexHNSW::link_singletons() { - fmt::print("search for singletons\n"); + printf("search for singletons\n"); std::vector seen(ntotal); @@ -614,12 +611,10 @@ void IndexHNSW::link_singletons() { } } - fmt::print( - " Found {} / %" PRId64 - " singletons ({} appear in a level above)\n", - n_sing, - ntotal, - n_sing_l1); + printf(" Found %d / %" PRId64 " singletons (%d appear in a level above)\n", + n_sing, + ntotal, + n_sing_l1); std::vector recons(singletons.size() * d); for (int i = 0; i < singletons.size(); i++) { diff --git a/faiss/IndexIVF.cpp b/faiss/IndexIVF.cpp index 726ad5f767..a1fa8cd16b 100644 --- a/faiss/IndexIVF.cpp +++ b/faiss/IndexIVF.cpp @@ -28,7 +28,6 @@ #include #include #include -#include namespace faiss { @@ -63,10 +62,10 @@ void Level1Quantizer::train_q1( size_t d = quantizer->d; if (quantizer->is_trained && (quantizer->ntotal == nlist)) { if (verbose) - fmt::print("IVF quantizer does not need training.\n"); + printf("IVF quantizer does not need training.\n"); } else if (quantizer_trains_alone == 1) { if (verbose) - fmt::print("IVF quantizer trains alone...\n"); + printf("IVF quantizer trains alone...\n"); quantizer->train(n, x); quantizer->verbose = verbose; FAISS_THROW_IF_NOT_MSG( @@ -74,8 +73,7 @@ void Level1Quantizer::train_q1( "nlist not consistent with quantizer size"); } else if (quantizer_trains_alone == 0) { if (verbose) - fmt::print( - "Training level-1 quantizer on {} vectors in {}D\n", n, d); + printf("Training level-1 quantizer on %zd vectors in %zdD\n", n, d); Clustering clus(d, nlist, cp); quantizer->reset(); @@ -88,11 +86,10 @@ void Level1Quantizer::train_q1( quantizer->is_trained = true; } else if (quantizer_trains_alone == 2) { if (verbose) { - fmt::print( - "Training L2 quantizer on {} vectors in {}D{}\n", - n, - d, - clustering_index ? "(user provided index)" : ""); + printf("Training L2 quantizer on %zd vectors in %zdD%s\n", + n, + d, + clustering_index ? "(user provided index)" : ""); } // also accept spherical centroids because in that case // L2 and IP are equivalent @@ -108,11 +105,11 @@ void Level1Quantizer::train_q1( clus.train(n, x, *clustering_index); } if (verbose) { - fmt::print("Adding centroids to quantizer\n"); + printf("Adding centroids to quantizer\n"); } if (!quantizer->is_trained) { if (verbose) { - fmt::print("But training it first on centroids table...\n"); + printf("But training it first on centroids table...\n"); } quantizer->train(nlist, clus.centroids.data()); } @@ -213,10 +210,9 @@ void IndexIVF::add_core( for (idx_t i0 = 0; i0 < n; i0 += bs) { idx_t i1 = std::min(n, i0 + bs); if (verbose) { - fmt::print( - " IndexIVF::add_with_ids %" PRId64 ":%" PRId64 "\n", - i0, - i1); + printf(" IndexIVF::add_with_ids %" PRId64 ":%" PRId64 "\n", + i0, + i1); } add_core( i1 - i0, @@ -265,11 +261,10 @@ void IndexIVF::add_core( } if (verbose) { - fmt::print( - " added {} / %" PRId64 " vectors ({} -1s)\n", - nadd, - n, - nminus1); + printf(" added %zd / %" PRId64 " vectors (%zd -1s)\n", + nadd, + n, + nminus1); } ntotal += n; @@ -1133,13 +1128,13 @@ void IndexIVF::update_vectors(int n, const idx_t* new_ids, const float* x) { void IndexIVF::train(idx_t n, const float* x) { if (verbose) { - fmt::print("Training level-1 quantizer\n"); + printf("Training level-1 quantizer\n"); } train_q1(n, x, verbose, metric_type); if (verbose) { - fmt::print("Training IVF residual\n"); + printf("Training IVF residual\n"); } // optional subsampling @@ -1176,7 +1171,7 @@ void IndexIVF::train_encoder( const idx_t* assign) { // does nothing by default if (verbose) { - fmt::print("IndexIVF: no residual training\n"); + printf("IndexIVF: no residual training\n"); } } diff --git a/faiss/IndexIVFAdditiveQuantizerFastScan.cpp b/faiss/IndexIVFAdditiveQuantizerFastScan.cpp index 222b24dd2e..25c3aa2b06 100644 --- a/faiss/IndexIVFAdditiveQuantizerFastScan.cpp +++ b/faiss/IndexIVFAdditiveQuantizerFastScan.cpp @@ -25,7 +25,6 @@ #include #include #include -#include namespace faiss { @@ -146,17 +145,16 @@ void IndexIVFAdditiveQuantizerFastScan::train_encoder( } if (verbose) { - fmt::print("training additive quantizer on {} vectors\n", int(n)); + printf("training additive quantizer on %d vectors\n", int(n)); } if (verbose) { - fmt::print( - "training {}x{} additive quantizer on " - "%" PRId64 " vectors in {}D\n", - aq->M, - ksub, - n, - d); + printf("training %zdx%zd additive quantizer on " + "%" PRId64 " vectors in %dD\n", + aq->M, + ksub, + n, + d); } aq->verbose = verbose; aq->train(n, x); @@ -227,8 +225,8 @@ void IndexIVFAdditiveQuantizerFastScan::estimate_norm_scale( norm_scale = (int)std::roundf(std::max(scale, 1.0f)); if (verbose) { - fmt::print("estimated norm scale: {:f}\n", scale); - fmt::print("rounded norm scale: {}\n", norm_scale); + printf("estimated norm scale: %lf\n", scale); + printf("rounded norm scale: %d\n", norm_scale); } } diff --git a/faiss/IndexIVFFastScan.cpp b/faiss/IndexIVFFastScan.cpp index 41b02c6a40..0b9c4e0992 100644 --- a/faiss/IndexIVFFastScan.cpp +++ b/faiss/IndexIVFFastScan.cpp @@ -27,7 +27,6 @@ #include #include #include -#include namespace faiss { @@ -110,13 +109,12 @@ void IndexIVFFastScan::add_with_ids( } size_t mem = get_mem_usage_kb() / (1 << 10); - fmt::print( - "IndexIVFFastScan::add_with_ids {}/{}, time {:.2f}/{:.2f}, RSS {}MB\n", - size_t(i1), - size_t(n), - elapsed_time, - total_time, - mem); + printf("IndexIVFFastScan::add_with_ids %zd/%zd, time %.2f/%.2f, RSS %zdMB\n", + size_t(i1), + size_t(n), + elapsed_time, + total_time, + mem); } add_with_ids(i1 - i0, x + i0 * d, xids ? xids + i0 : nullptr); } diff --git a/faiss/IndexIVFFlat.cpp b/faiss/IndexIVFFlat.cpp index c86471a905..07f410440d 100644 --- a/faiss/IndexIVFFlat.cpp +++ b/faiss/IndexIVFFlat.cpp @@ -22,7 +22,6 @@ #include #include #include -#include namespace faiss { @@ -82,11 +81,10 @@ void IndexIVFFlat::add_core( } if (verbose) { - fmt::print( - "IndexIVFFlat::add_core: added %" PRId64 " / %" PRId64 - " vectors\n", - n_add, - n); + printf("IndexIVFFlat::add_core: added %" PRId64 " / %" PRId64 + " vectors\n", + n_add, + n); } ntotal += n; } @@ -265,12 +263,11 @@ void IndexIVFFlatDedup::train(idx_t n, const float* x) { } } if (verbose) { - fmt::print( - "IndexIVFFlatDedup::train: train on %" PRId64 - " points after dedup " - "(was %" PRId64 " points)\n", - n2, - n); + printf("IndexIVFFlatDedup::train: train on %" PRId64 + " points after dedup " + "(was %" PRId64 " points)\n", + n2, + n); } IndexIVFFlat::train(n2, x2.get()); } @@ -333,13 +330,12 @@ void IndexIVFFlatDedup::add_with_ids( } } if (verbose) { - fmt::print( - "IndexIVFFlat::add_with_ids: added %" PRId64 " / %" PRId64 - " vectors" - " (out of which %" PRId64 " are duplicates)\n", - n_add, - na, - n_dup); + printf("IndexIVFFlat::add_with_ids: added %" PRId64 " / %" PRId64 + " vectors" + " (out of which %" PRId64 " are duplicates)\n", + n_add, + na, + n_dup); } ntotal += n_add; } diff --git a/faiss/IndexIVFIndependentQuantizer.cpp b/faiss/IndexIVFIndependentQuantizer.cpp index 6b187a5551..2073dd2ee4 100644 --- a/faiss/IndexIVFIndependentQuantizer.cpp +++ b/faiss/IndexIVFIndependentQuantizer.cpp @@ -9,7 +9,6 @@ #include #include #include -#include namespace faiss { @@ -114,8 +113,7 @@ void IndexIVFIndependentQuantizer::train(idx_t n, const float* x) { // train the VectorTransform if (vt && !vt->is_trained) { if (verbose) { - fmt::print( - "IndexIVFIndependentQuantizer: train the VectorTransform\n"); + printf("IndexIVFIndependentQuantizer: train the VectorTransform\n"); } vt->train(n, x); } @@ -123,16 +121,14 @@ void IndexIVFIndependentQuantizer::train(idx_t n, const float* x) { // get the centroids from the quantizer, transform them and // add them to the index_ivf's quantizer if (verbose) { - fmt::print( - "IndexIVFIndependentQuantizer: extract the main quantizer centroids\n"); + printf("IndexIVFIndependentQuantizer: extract the main quantizer centroids\n"); } std::vector centroids(nlist * d); quantizer->reconstruct_n(0, nlist, centroids.data()); VTransformedVectors tcent(vt, nlist, centroids.data()); if (verbose) { - fmt::print( - "IndexIVFIndependentQuantizer: add centroids to the secondary quantizer\n"); + printf("IndexIVFIndependentQuantizer: add centroids to the secondary quantizer\n"); } if (!index_ivf->quantizer->is_trained) { index_ivf->quantizer->train(nlist, tcent.x); @@ -152,7 +148,7 @@ void IndexIVFIndependentQuantizer::train(idx_t n, const float* x) { VTransformedVectors tv(vt, n, sv.x); if (verbose) { - fmt::print("IndexIVFIndependentQuantizer: train encoder\n"); + printf("IndexIVFIndependentQuantizer: train encoder\n"); } if (index_ivf->by_residual) {