Skip to content

Commit

Permalink
printf -> fmt::print in files inc faiss/IndexBinaryHNSW.cpp
Browse files Browse the repository at this point in the history
Summary:
One of our [C++ 2024 Modern Code Better Engineering Goals](https://fb.workplace.com/groups/fbcode.fyi/posts/6837863056249437) is to reduce or eliminate the use of printf in our codebase, replacing it with safer and more performant alternatives.

This diff replaces `printf` with its preferred alternative: `fmt::print`.

This diff was auto-generated by fbcode/scripts/rbarnes/printf_upgrader.py, please reach out to r-barnes if you see problems.

 - If you approve of this diff, please use the "Accept & Ship" button :-)

(11 file modified.)

Reviewed By: bcardosolopes

Differential Revision: D51486397

fbshipit-source-id: 230540b26ec8717d8e8aa4ae14d6a34718a964ff
  • Loading branch information
r-barnes authored and facebook-github-bot committed Dec 5, 2023
1 parent eefa391 commit 131adc5
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 91 deletions.
21 changes: 12 additions & 9 deletions faiss/IndexBinaryHNSW.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <faiss/utils/Heap.h>
#include <faiss/utils/hamming.h>
#include <faiss/utils/random.h>
#include <fmt/core.h>

namespace faiss {

Expand All @@ -51,17 +52,18 @@ void hnsw_add_vertices(
size_t ntotal = n0 + n;
double t0 = getmillisecs();
if (verbose) {
printf("hnsw_add_vertices: adding %zd elements on top of %zd "
"(preset_levels=%d)\n",
n,
n0,
int(preset_levels));
fmt::print(
"hnsw_add_vertices: adding {} elements on top of {} "
"(preset_levels={})\n",
n,
n0,
int(preset_levels));
}

int max_level = hnsw.prepare_level_tab(n, preset_levels);

if (verbose) {
printf(" max_level = %d\n", max_level);
fmt::print(" max_level = {}\n", max_level);
}

std::vector<omp_lock_t> locks(ntotal);
Expand Down Expand Up @@ -108,7 +110,8 @@ void hnsw_add_vertices(
int i0 = i1 - hist[pt_level];

if (verbose) {
printf("Adding %d elements at level %d\n", i1 - i0, pt_level);
fmt::print(
"Adding {} elements at level {}\n", i1 - i0, pt_level);
}

// random permutation to get rid of dataset order bias
Expand All @@ -135,7 +138,7 @@ void hnsw_add_vertices(

if (prev_display >= 0 && i - i0 > prev_display + 10000) {
prev_display = i - i0;
printf(" %d / %d\r", i - i0, i1 - i0);
fmt::print(" {} / {}\r", i - i0, i1 - i0);
fflush(stdout);
}
}
Expand All @@ -145,7 +148,7 @@ void hnsw_add_vertices(
FAISS_ASSERT(i1 == 0);
}
if (verbose) {
printf("Done in %.3f ms\n", getmillisecs() - t0);
fmt::print("Done in {:.3f} ms\n", getmillisecs() - t0);
}

for (int i = 0; i < ntotal; i++)
Expand Down
7 changes: 4 additions & 3 deletions faiss/IndexBinaryHash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <faiss/impl/AuxIndexStructures.h>
#include <faiss/impl/FaissAssert.h>
#include <faiss/impl/platform_macros.h>
#include <fmt/core.h>

namespace faiss {

Expand Down Expand Up @@ -268,12 +269,12 @@ size_t IndexBinaryHash::hashtable_size() const {

void IndexBinaryHash::display() const {
for (auto it = invlists.begin(); it != invlists.end(); ++it) {
printf("%" PRId64 ": [", it->first);
fmt::print("%" PRId64 ": [", it->first);
const std::vector<idx_t>& v = it->second.ids;
for (auto x : v) {
printf("%" PRId64 " ", x);
fmt::print("%" PRId64 " ", x);
}
printf("]\n");
fmt::print("]\n");
}
}

Expand Down
22 changes: 13 additions & 9 deletions faiss/IndexBinaryIVF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <faiss/utils/hamming.h>
#include <faiss/utils/sorting.h>
#include <faiss/utils/utils.h>
#include <fmt/core.h>

namespace faiss {

Expand Down Expand Up @@ -87,10 +88,11 @@ void IndexBinaryIVF::add_core(
n_add++;
}
if (verbose) {
printf("IndexBinaryIVF::add_with_ids: added "
"%" PRId64 " / %" PRId64 " vectors\n",
n_add,
n);
fmt::print(
"IndexBinaryIVF::add_with_ids: added "
"%" PRId64 " / %" PRId64 " vectors\n",
n_add,
n);
}
ntotal += n_add;
}
Expand Down Expand Up @@ -233,16 +235,17 @@ size_t IndexBinaryIVF::remove_ids(const IDSelector& sel) {

void IndexBinaryIVF::train(idx_t n, const uint8_t* x) {
if (verbose) {
printf("Training quantizer\n");
fmt::print("Training quantizer\n");
}

if (quantizer->is_trained && (quantizer->ntotal == nlist)) {
if (verbose) {
printf("IVF quantizer does not need training.\n");
fmt::print("IVF quantizer does not need training.\n");
}
} else {
if (verbose) {
printf("Training quantizer on %" PRId64 " vectors in %dD\n", n, d);
fmt::print(
"Training quantizer on %" PRId64 " vectors in {}D\n", n, d);
}

Clustering clus(d, nlist, cp);
Expand All @@ -251,8 +254,9 @@ void IndexBinaryIVF::train(idx_t n, const uint8_t* x) {
IndexFlatL2 index_tmp(d);

if (clustering_index && verbose) {
printf("using clustering_index of dimension %d to do the clustering\n",
clustering_index->d);
fmt::print(
"using clustering_index of dimension {} to do the clustering\n",
clustering_index->d);
}

// LSH codec that is able to convert the binary vectors to floats.
Expand Down
3 changes: 2 additions & 1 deletion faiss/IndexFastScan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <faiss/impl/pq4_fast_scan.h>
#include <faiss/impl/simd_result_handlers.h>
#include <faiss/utils/quantize_lut.h>
#include <fmt/core.h>

namespace faiss {

Expand Down Expand Up @@ -73,7 +74,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) {
printf("IndexFastScan::add %zd/%zd\n", size_t(i1), size_t(n));
fmt::print("IndexFastScan::add {}/{}\n", size_t(i1), size_t(n));
}
add(i1 - i0, x + i0 * d);
}
Expand Down
37 changes: 21 additions & 16 deletions faiss/IndexHNSW.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <faiss/utils/distances.h>
#include <faiss/utils/random.h>
#include <faiss/utils/sorting.h>
#include <fmt/core.h>

extern "C" {

Expand Down Expand Up @@ -134,11 +135,12 @@ void hnsw_add_vertices(
size_t ntotal = n0 + n;
double t0 = getmillisecs();
if (verbose) {
printf("hnsw_add_vertices: adding %zd elements on top of %zd "
"(preset_levels=%d)\n",
n,
n0,
int(preset_levels));
fmt::print(
"hnsw_add_vertices: adding {} elements on top of {} "
"(preset_levels={})\n",
n,
n0,
int(preset_levels));
}

if (n == 0) {
Expand All @@ -148,7 +150,7 @@ void hnsw_add_vertices(
int max_level = hnsw.prepare_level_tab(n, preset_levels);

if (verbose) {
printf(" max_level = %d\n", max_level);
fmt::print(" max_level = {}\n", max_level);
}

std::vector<omp_lock_t> locks(ntotal);
Expand Down Expand Up @@ -196,7 +198,8 @@ void hnsw_add_vertices(
int i0 = i1 - hist[pt_level];

if (verbose) {
printf("Adding %d elements at level %d\n", i1 - i0, pt_level);
fmt::print(
"Adding {} elements at level {}\n", i1 - i0, pt_level);
}

// random permutation to get rid of dataset order bias
Expand Down Expand Up @@ -232,7 +235,7 @@ void hnsw_add_vertices(

if (prev_display >= 0 && i - i0 > prev_display + 10000) {
prev_display = i - i0;
printf(" %d / %d\r", i - i0, i1 - i0);
fmt::print(" {} / {}\r", i - i0, i1 - i0);
fflush(stdout);
}
if (counter % check_period == 0) {
Expand All @@ -251,7 +254,7 @@ void hnsw_add_vertices(
FAISS_ASSERT(i1 == 0);
}
if (verbose) {
printf("Done in %.3f ms\n", getmillisecs() - t0);
fmt::print("Done in {:.3f} ms\n", getmillisecs() - t0);
}

for (int i = 0; i < ntotal; i++) {
Expand Down Expand Up @@ -538,13 +541,13 @@ void IndexHNSW::init_level_0_from_entry_points(
*dis, pt_id, nearest, (*dis)(nearest), 0, locks.data(), vt);

if (verbose && i % 10000 == 0) {
printf(" %d / %d\r", i, n);
fmt::print(" {} / {}\r", i, n);
fflush(stdout);
}
}
}
if (verbose) {
printf("\n");
fmt::print("\n");
}

for (int i = 0; i < ntotal; i++)
Expand Down Expand Up @@ -586,7 +589,7 @@ void IndexHNSW::reorder_links() {
}

void IndexHNSW::link_singletons() {
printf("search for singletons\n");
fmt::print("search for singletons\n");

std::vector<bool> seen(ntotal);

Expand All @@ -611,10 +614,12 @@ void IndexHNSW::link_singletons() {
}
}

printf(" Found %d / %" PRId64 " singletons (%d appear in a level above)\n",
n_sing,
ntotal,
n_sing_l1);
fmt::print(
" Found {} / %" PRId64
" singletons ({} appear in a level above)\n",
n_sing,
ntotal,
n_sing_l1);

std::vector<float> recons(singletons.size() * d);
for (int i = 0; i < singletons.size(); i++) {
Expand Down
43 changes: 24 additions & 19 deletions faiss/IndexIVF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <faiss/impl/CodePacker.h>
#include <faiss/impl/FaissAssert.h>
#include <faiss/impl/IDSelector.h>
#include <fmt/core.h>

namespace faiss {

Expand Down Expand Up @@ -62,18 +63,19 @@ void Level1Quantizer::train_q1(
size_t d = quantizer->d;
if (quantizer->is_trained && (quantizer->ntotal == nlist)) {
if (verbose)
printf("IVF quantizer does not need training.\n");
fmt::print("IVF quantizer does not need training.\n");
} else if (quantizer_trains_alone == 1) {
if (verbose)
printf("IVF quantizer trains alone...\n");
fmt::print("IVF quantizer trains alone...\n");
quantizer->train(n, x);
quantizer->verbose = verbose;
FAISS_THROW_IF_NOT_MSG(
quantizer->ntotal == nlist,
"nlist not consistent with quantizer size");
} else if (quantizer_trains_alone == 0) {
if (verbose)
printf("Training level-1 quantizer on %zd vectors in %zdD\n", n, d);
fmt::print(
"Training level-1 quantizer on {} vectors in {}D\n", n, d);

Clustering clus(d, nlist, cp);
quantizer->reset();
Expand All @@ -86,10 +88,11 @@ void Level1Quantizer::train_q1(
quantizer->is_trained = true;
} else if (quantizer_trains_alone == 2) {
if (verbose) {
printf("Training L2 quantizer on %zd vectors in %zdD%s\n",
n,
d,
clustering_index ? "(user provided index)" : "");
fmt::print(
"Training L2 quantizer on {} vectors in {}D{}\n",
n,
d,
clustering_index ? "(user provided index)" : "");
}
// also accept spherical centroids because in that case
// L2 and IP are equivalent
Expand All @@ -105,11 +108,11 @@ void Level1Quantizer::train_q1(
clus.train(n, x, *clustering_index);
}
if (verbose) {
printf("Adding centroids to quantizer\n");
fmt::print("Adding centroids to quantizer\n");
}
if (!quantizer->is_trained) {
if (verbose) {
printf("But training it first on centroids table...\n");
fmt::print("But training it first on centroids table...\n");
}
quantizer->train(nlist, clus.centroids.data());
}
Expand Down Expand Up @@ -210,9 +213,10 @@ void IndexIVF::add_core(
for (idx_t i0 = 0; i0 < n; i0 += bs) {
idx_t i1 = std::min(n, i0 + bs);
if (verbose) {
printf(" IndexIVF::add_with_ids %" PRId64 ":%" PRId64 "\n",
i0,
i1);
fmt::print(
" IndexIVF::add_with_ids %" PRId64 ":%" PRId64 "\n",
i0,
i1);
}
add_core(
i1 - i0,
Expand Down Expand Up @@ -261,10 +265,11 @@ void IndexIVF::add_core(
}

if (verbose) {
printf(" added %zd / %" PRId64 " vectors (%zd -1s)\n",
nadd,
n,
nminus1);
fmt::print(
" added {} / %" PRId64 " vectors ({} -1s)\n",
nadd,
n,
nminus1);
}

ntotal += n;
Expand Down Expand Up @@ -1128,13 +1133,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) {
printf("Training level-1 quantizer\n");
fmt::print("Training level-1 quantizer\n");
}

train_q1(n, x, verbose, metric_type);

if (verbose) {
printf("Training IVF residual\n");
fmt::print("Training IVF residual\n");
}

// optional subsampling
Expand Down Expand Up @@ -1171,7 +1176,7 @@ void IndexIVF::train_encoder(
const idx_t* assign) {
// does nothing by default
if (verbose) {
printf("IndexIVF: no residual training\n");
fmt::print("IndexIVF: no residual training\n");
}
}

Expand Down
Loading

0 comments on commit 131adc5

Please sign in to comment.