Skip to content

Commit 259fa85

Browse files
limqiyingmeta-codesync[bot]
authored andcommitted
black -l 80: gpu/test formatting (1/3) (#5426)
Summary: Pull Request resolved: #5426 # this is a no-op change Part 1 of a 3-diff split of the faiss `black -l 80` reformat into small, reviewable diffs (each 3-4 files, <100 changed lines). Follow-up to D109736099 (landed), which formatted the faiss Python tree; these files drifted or are new. Ran `black -l 80` (line length 80, matching faiss's `CONTRIBUTING.md`: "80 character line length (both for C++ and Python)") over the GPU test files: `gpu/test/bench_approaches.py`, `gpu/test/test_cagra.py`, `gpu/test/test_gpu_index_ivfsq.py`. Pure-formatting only. `black`'s AST-equivalence safety check guarantees no identifier, string/numeric value, operator, or control-flow change — only whitespace, wrapping, trailing commas, and quote normalization. Independently verified: `ast.dump` equality (docstrings normalized) and comment-content preservation. `fbcode/faiss/` is excluded from Meta's standard Python autoformatter (it mirrors to public GitHub), so `black -l 80` is the correct tool. ___ Reviewed By: trang-nm-nguyen Differential Revision: D112191715 fbshipit-source-id: 7bc36a1a4a954a266a916717c810c433db12ccc3
1 parent 0a27700 commit 259fa85

3 files changed

Lines changed: 39 additions & 37 deletions

File tree

faiss/gpu/test/bench_approaches.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def load_from_hive(
8484
if xb is None:
8585
xb = np.empty((n, arr.shape[1]), dtype=np.float32)
8686
m = min(arr.shape[0], n - i)
87-
xb[i:i+m] = arr[:m]
87+
xb[i : i + m] = arr[:m]
8888
i += m
8989
if i % (batch_size * 50) == 0:
9090
elapsed = time.time() - t0
@@ -398,12 +398,10 @@ def main():
398398
"(not needed when --hive-table is set)",
399399
)
400400
parser.add_argument(
401-
"--n", type=int, default=0,
402-
help="Use first N vectors (0=all)"
401+
"--n", type=int, default=0, help="Use first N vectors (0=all)"
403402
)
404403
parser.add_argument(
405-
"--nq", type=int, default=1000,
406-
help="Number of queries"
404+
"--nq", type=int, default=1000, help="Number of queries"
407405
)
408406
parser.add_argument("--num-gpus", type=int, default=0)
409407
parser.add_argument("--graph-degree", type=int, default=32)
@@ -413,7 +411,7 @@ def main():
413411
"--n-clusters",
414412
type=int,
415413
default=0,
416-
help="all_neighbors n_clusters (0=auto)"
414+
help="all_neighbors n_clusters (0=auto)",
417415
)
418416
parser.add_argument(
419417
"--overlap-factor",
@@ -462,8 +460,10 @@ def main():
462460
"--approaches",
463461
type=str,
464462
default="A,B,C,D",
465-
help=("A (IndexShards), B (GPU stitch), "
466-
"C (CPU stitch), D (all_neighbors)"),
463+
help=(
464+
"A (IndexShards), B (GPU stitch), "
465+
"C (CPU stitch), D (all_neighbors)"
466+
),
467467
)
468468
parser.add_argument(
469469
"--index-dir",

faiss/gpu/test/test_cagra.py

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -246,11 +246,11 @@ def test_IDMapCagra_IP_Int8(self):
246246

247247

248248
@unittest.skipIf(
249-
"CUVS" not in faiss.get_compile_options(),
250-
"only if cuVS is compiled in")
249+
"CUVS" not in faiss.get_compile_options(), "only if cuVS is compiled in"
250+
)
251251
@unittest.skipIf(
252-
faiss.get_num_gpus() < 2,
253-
"need at least 2 GPUs for multi-GPU test")
252+
faiss.get_num_gpus() < 2, "need at least 2 GPUs for multi-GPU test"
253+
)
254254
class TestMultiGpuCagra(unittest.TestCase):
255255

256256
def test_multi_gpu_build_and_search(self):
@@ -285,11 +285,12 @@ def test_multi_gpu_build_and_search(self):
285285
cpu_index.hnsw.efSearch = 128
286286
Dnew, Inew = cpu_index.search(xq, k)
287287

288-
recall = np.mean([
289-
len(set(Inew[i]) & set(Iref[i])) / k for i in range(ds.nq)
290-
])
291-
self.assertGreater(recall, 0.80,
292-
f"Multi-GPU recall@{k} too low: {recall:.4f}")
288+
recall = np.mean(
289+
[len(set(Inew[i]) & set(Iref[i])) / k for i in range(ds.nq)]
290+
)
291+
self.assertGreater(
292+
recall, 0.80, f"Multi-GPU recall@{k} too low: {recall:.4f}"
293+
)
293294

294295
# Serialization roundtrip
295296
data = faiss.serialize_index(cpu_index)
@@ -317,11 +318,10 @@ def test_all_neighbors_build(self):
317318
config = faiss.GpuIndexCagraConfig()
318319
config.graph_degree = 32
319320
config.intermediate_graph_degree = 48
320-
index = faiss.GpuIndexCagra(
321-
res, ds.d, faiss.METRIC_L2, config)
321+
index = faiss.GpuIndexCagra(res, ds.d, faiss.METRIC_L2, config)
322322
index.trainAllNeighbors(
323-
ds.nb, faiss.swig_ptr(xb), devices,
324-
0, 0, True, 0)
323+
ds.nb, faiss.swig_ptr(xb), devices, 0, 0, True, 0
324+
)
325325

326326
cpu_index = faiss.IndexHNSWCagra()
327327
cpu_index.base_level_only = True
@@ -332,11 +332,8 @@ def test_all_neighbors_build(self):
332332
Dnew, Inew = cpu_index.search(xq, k)
333333

334334
recall = np.mean(
335-
[
336-
len(set(Inew[i]) & set(Iref[i])) / k
337-
for i in range(ds.nq)
338-
]
335+
[len(set(Inew[i]) & set(Iref[i])) / k for i in range(ds.nq)]
339336
)
340337
self.assertGreater(
341-
recall, 0.70,
342-
f"all_neighbors recall@{k} too low: {recall:.4f}")
338+
recall, 0.70, f"all_neighbors recall@{k} too low: {recall:.4f}"
339+
)

faiss/gpu/test/test_gpu_index_ivfsq.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,8 @@ def test_8bit_direct(self):
243243

244244

245245
@unittest.skipIf(
246-
"CUVS" not in faiss.get_compile_options(),
247-
"only if CUVS is compiled in")
246+
"CUVS" not in faiss.get_compile_options(), "only if CUVS is compiled in"
247+
)
248248
class TestCuvsSQ8(unittest.TestCase):
249249

250250
def make_gpu_index(self, metric):
@@ -261,8 +261,8 @@ def make_gpu_index(self, metric):
261261
config.indicesOptions = faiss.INDICES_64_BIT
262262

263263
idx_gpu = faiss.GpuIndexIVFScalarQuantizer(
264-
res, d, nlist, faiss.ScalarQuantizer.QT_8bit,
265-
metric, True, config)
264+
res, d, nlist, faiss.ScalarQuantizer.QT_8bit, metric, True, config
265+
)
266266
idx_gpu.train(xt)
267267
idx_gpu.add(xb)
268268
idx_gpu.nprobe = nprobe
@@ -278,8 +278,13 @@ def check_metric(self, metric):
278278

279279
quantizer = faiss.IndexFlat(idx_gpu.d, metric)
280280
idx_cpu = faiss.IndexIVFScalarQuantizer(
281-
quantizer, idx_gpu.d, nlist,
282-
faiss.ScalarQuantizer.QT_8bit, metric, True)
281+
quantizer,
282+
idx_gpu.d,
283+
nlist,
284+
faiss.ScalarQuantizer.QT_8bit,
285+
metric,
286+
True,
287+
)
283288
idx_gpu.copyTo(idx_cpu)
284289
idx_cpu.nprobe = nprobe
285290

@@ -288,18 +293,18 @@ def check_metric(self, metric):
288293
self.assertEqual(idx_cpu.sq.trained.size(), 2 * idx_gpu.d)
289294
do_test_with_index(idx_cpu, idx_gpu, nprobe, k, False, 0.8)
290295

291-
idx_gpu_copy = faiss.GpuIndexIVFScalarQuantizer(
292-
res, idx_cpu, config)
296+
idx_gpu_copy = faiss.GpuIndexIVFScalarQuantizer(res, idx_cpu, config)
293297
idx_gpu_copy.nprobe = nprobe
294298
self.assertEqual(idx_gpu_copy.ntotal, idx_cpu.ntotal)
295299
do_test_with_index(idx_cpu, idx_gpu_copy, nprobe, k, False, 0.8)
296300

297301
xq_nan = make_t(2, idx_gpu.d)
298302
xq_nan[1, 0] = np.nan
299303
D_nan, I_nan = idx_gpu.search(xq_nan, k)
300-
np.testing.assert_array_equal(I_nan[1], -np.ones(k, dtype='int64'))
304+
np.testing.assert_array_equal(I_nan[1], -np.ones(k, dtype="int64"))
301305
np.testing.assert_allclose(
302-
D_nan[1], np.full(k, np.finfo('float32').max, dtype='float32'))
306+
D_nan[1], np.full(k, np.finfo("float32").max, dtype="float32")
307+
)
303308

304309
idx_gpu.reset()
305310
self.assertEqual(idx_gpu.ntotal, 0)

0 commit comments

Comments
 (0)