Faiss

- Main Page
- Namespaces
- Classes
- Files

- File List

All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
CompareIVFPQ.cu
1
2 /**
3 * Copyright (c) 2015-present, Facebook, Inc.
4 * All rights reserved.
5 *
6 * This source code is licensed under the CC-by-NC license found in the
7 * LICENSE file in the root directory of this source tree.
8 */
9
10 // Copyright 2004-present Facebook. All Rights Reserved.
11
12 #include "../../IndexIVFPQ.h"
13 #include "../../index_io.h"
14 #include "../../utils.h"
15
16 #include "../GpuIndexIVFPQ.h"
17 #include "IndexWrapper.h"
18 #include "../test/TestUtils.h"
19 #include "../utils/DeviceTensor.cuh"
20 #include "../utils/DeviceUtils.h"
21 #include "../utils/HostTensor.cuh"
22 #include "../utils/Timer.h"
23
24 #include <cuda_profiler_api.h>
25 #include <gflags/gflags.h>
26 #include <map>
27 #include <memory>
28 #include <vector>
29
30 DEFINE_int32(nprobe, 5, "number of coarse centroids to probe");
31 DEFINE_int32(k, 3, "final number of closest results returned");
32 DEFINE_int32(num_queries, 3, "number of query vectors");
33 DEFINE_string(in, "/home/jhj/local/index.out", "index file for input");
34 DEFINE_bool(diff, true, "show exact distance + index output discrepancies");
35 DEFINE_bool(use_precomputed, true, "enable or disable precomputed codes");
36 DEFINE_bool(float16_lookup, false, "use float16 residual distance tables");
37 DEFINE_int64(seed, -1, "specify random seed");
38 DEFINE_int32(num_gpus, 1, "number of gpus to use");
39 DEFINE_int32(index, 2, "0 = no indices on GPU; 1 = 32 bit, 2 = 64 bit on GPU");
40
41 using namespace faiss::gpu;
42
43 int main(int argc, char** argv) {
44 google::ParseCommandLineFlags(&argc, &argv, true);
45
46 CUDA_VERIFY(cudaProfilerStop());
47
48 auto seed = FLAGS_seed != -1L ? FLAGS_seed : time(nullptr);
49 printf("using seed %ld\n", seed);
50
51 auto numQueries = FLAGS_num_queries;
52
53 auto index = std::unique_ptr<faiss::IndexIVFPQ>(
54 dynamic_cast<faiss::IndexIVFPQ*>(faiss::read_index(FLAGS_in.c_str())));
55 FAISS_ASSERT((bool) index);
56 index->nprobe = FLAGS_nprobe;
57
58 if (!FLAGS_use_precomputed) {
59 index->use_precomputed_table = 0;
60 }
61
62 auto dim = index->d;
63 auto codes = index->pq.M;
64 auto bitsPerCode = index->pq.nbits;
65
66 printf("Database: dim %d num vecs %ld\n", dim, index->ntotal);
67 printf("Coarse centroids: %ld\n", index->quantizer->ntotal);
68 printf("PQ centroids: codes %ld bits per code %ld\n", codes, bitsPerCode);
69 printf("L2 lookup: %d queries, nprobe %d, total k %d, "
70 "precomputed codes %d\n\n",
71 numQueries, FLAGS_nprobe, FLAGS_k,
72 FLAGS_use_precomputed);
73
74 // Convert to GPU index
75 printf("Copying index to %d GPU(s)...\n", FLAGS_num_gpus);
76
77 auto precomp = FLAGS_use_precomputed;
78 auto indicesOpt = (faiss::gpu::IndicesOptions) FLAGS_index;
79 auto useFloat16Lookup = FLAGS_float16_lookup;
80
81 auto initFn = [precomp, indicesOpt, useFloat16Lookup, &index]
82 (faiss::gpu::GpuResources* res, int dev) ->
83 std::unique_ptr<faiss::gpu::GpuIndexIVFPQ> {
84 auto p = std::unique_ptr<faiss::gpu::GpuIndexIVFPQ>(
85 new faiss::gpu::GpuIndexIVFPQ(res,
86 dev,
87 indicesOpt,
88 useFloat16Lookup,
89 index.get()));
90 p->setPrecomputedCodes(precomp);
91
92 return p;
93 };
94
95 IndexWrapper<faiss::gpu::GpuIndexIVFPQ> gpuIndex(FLAGS_num_gpus, initFn);
96 gpuIndex.setNumProbes(FLAGS_nprobe);
97 printf("copy done\n");
98
99 // Build query vectors
100 HostTensor<float, 2, true> cpuQuery({numQueries, dim});
101 faiss::float_rand(cpuQuery.data(), cpuQuery.numElements(), seed);
102
103 // Time faiss CPU
104 HostTensor<float, 2, true> cpuDistances({numQueries, FLAGS_k});
105 HostTensor<faiss::Index::idx_t, 2, true> cpuIndices({numQueries, FLAGS_k});
106
107 float cpuTime = 0.0f;
108
109 {
110 CpuTimer timer;
111 index->search(numQueries,
112 cpuQuery.data(),
113 FLAGS_k,
114 cpuDistances.data(),
115 cpuIndices.data());
116
117 cpuTime = timer.elapsedMilliseconds();
118 }
119
120 printf("CPU time %.3f ms\n", cpuTime);
121
122 HostTensor<float, 2, true> gpuDistances({numQueries, FLAGS_k});
123 HostTensor<faiss::Index::idx_t, 2, true> gpuIndices({numQueries, FLAGS_k});
124
125 CUDA_VERIFY(cudaProfilerStart());
126 faiss::gpu::synchronizeAllDevices();
127
128 float gpuTime = 0.0f;
129
130 // Time GPU
131 {
132 CpuTimer timer;
133
134 gpuIndex.getIndex()->search(cpuQuery.getSize(0),
135 cpuQuery.data(),
136 FLAGS_k,
137 gpuDistances.data(),
138 gpuIndices.data());
139
140 // There is a device -> host copy above, so no need to time
141 // additional synchronization with the GPU
142 gpuTime = timer.elapsedMilliseconds();
143 }
144
145 CUDA_VERIFY(cudaProfilerStop());
146 printf("GPU time %.3f ms\n", gpuTime);
147
148 compareLists(cpuDistances.data(), cpuIndices.data(),
149 gpuDistances.data(), gpuIndices.data(),
150 numQueries, FLAGS_k,
151 "", true, FLAGS_diff, false);
152
153 CUDA_VERIFY(cudaDeviceSynchronize());
154 // printf("\ncudaMalloc usage %zd\n",
155 // resources.getMemoryManager().getHighWaterCudaMalloc());
156
157 return 0;
158 }
faiss::gpu::CpuTimer::elapsedMilliseconds
float elapsedMilliseconds()
Returns elapsed time in milliseconds.
Definition: Timer.cpp:53
faiss::read_index
Index * read_index(FILE *f, bool try_mmap)
Definition: index_io.cpp:476
faiss::gpu::GpuResources
Definition: GpuResources.h:24
faiss::gpu::CpuTimer
CPU wallclock elapsed timer.
Definition: Timer.h:43
faiss::IndexIVFPQ
Definition: IndexIVFPQ.h:30
faiss::gpu::GpuIndexIVFPQ
IVFPQ index for the GPU.
Definition: GpuIndexIVFPQ.h:25
faiss::gpu::HostTensor
Definition: HostTensor.cuh:23
faiss::gpu::IndexWrapper
Definition: IndexWrapper.h:26