Faiss

- Main Page
- Namespaces
- Classes
- Files

- File List

All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
PerfIVFPQ.cu
1 /**
2 * Copyright (c) 2015-present, Facebook, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD+Patents license found in the
6 * LICENSE file in the root directory of this source tree.
7 */
8
9
10 #include "../../IndexIVFPQ.h"
11 #include "../../index_io.h"
12 #include "../../utils.h"
13
14 #include "../GpuIndexIVFPQ.h"
15 #include "IndexWrapper.h"
16 #include "../test/TestUtils.h"
17 #include "../utils/DeviceTensor.cuh"
18 #include "../utils/DeviceUtils.h"
19 #include "../utils/HostTensor.cuh"
20 #include "../utils/Timer.h"
21
22 #include <cuda_profiler_api.h>
23 #include <gflags/gflags.h>
24 #include <map>
25 #include <memory>
26 #include <vector>
27
28 DEFINE_int32(nprobe, 5, "number of coarse centroids to probe");
29 DEFINE_int32(k, 3, "final number of closest results returned");
30 DEFINE_int32(num_queries, 3, "number of query vectors");
31 DEFINE_string(in, "/home/jhj/local/index.out", "index file for input");
32 DEFINE_bool(diff, true, "show exact distance + index output discrepancies");
33 DEFINE_bool(use_precomputed, true, "enable or disable precomputed codes");
34 DEFINE_bool(float16_lookup, false, "use float16 residual distance tables");
35 DEFINE_int64(seed, -1, "specify random seed");
36 DEFINE_int32(num_gpus, 1, "number of gpus to use");
37 DEFINE_int32(index, 2, "0 = no indices on GPU; 1 = 32 bit, 2 = 64 bit on GPU");
38
39 using namespace faiss::gpu;
40
41 int main(int argc, char** argv) {
42 gflags::ParseCommandLineFlags(&argc, &argv, true);
43
44 CUDA_VERIFY(cudaProfilerStop());
45
46 auto seed = FLAGS_seed != -1L ? FLAGS_seed : time(nullptr);
47 printf("using seed %ld\n", seed);
48
49 auto numQueries = FLAGS_num_queries;
50
51 auto index = std::unique_ptr<faiss::IndexIVFPQ>(
52 dynamic_cast<faiss::IndexIVFPQ*>(faiss::read_index(FLAGS_in.c_str())));
53 FAISS_ASSERT((bool) index);
54 index->nprobe = FLAGS_nprobe;
55
56 if (!FLAGS_use_precomputed) {
57 index->use_precomputed_table = 0;
58 }
59
60 auto dim = index->d;
61 auto codes = index->pq.M;
62 auto bitsPerCode = index->pq.nbits;
63
64 printf("Database: dim %d num vecs %ld\n", dim, index->ntotal);
65 printf("Coarse centroids: %ld\n", index->quantizer->ntotal);
66 printf("PQ centroids: codes %ld bits per code %ld\n", codes, bitsPerCode);
67 printf("L2 lookup: %d queries, nprobe %d, total k %d, "
68 "precomputed codes %d\n\n",
69 numQueries, FLAGS_nprobe, FLAGS_k,
70 FLAGS_use_precomputed);
71
72 // Convert to GPU index
73 printf("Copying index to %d GPU(s)...\n", FLAGS_num_gpus);
74
75 auto precomp = FLAGS_use_precomputed;
76 auto indicesOpt = (faiss::gpu::IndicesOptions) FLAGS_index;
77 auto useFloat16Lookup = FLAGS_float16_lookup;
78
79 auto initFn = [precomp, indicesOpt, useFloat16Lookup, &index]
80 (faiss::gpu::GpuResources* res, int dev) ->
81 std::unique_ptr<faiss::gpu::GpuIndexIVFPQ> {
82
83 faiss::gpu::GpuIndexIVFPQConfig config;
84 config.device = dev;
85 config.usePrecomputedTables = precomp;
86 config.indicesOptions = indicesOpt;
87 config.useFloat16LookupTables = useFloat16Lookup;
88
89 auto p = std::unique_ptr<faiss::gpu::GpuIndexIVFPQ>(
90 new faiss::gpu::GpuIndexIVFPQ(res, index.get(), config));
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:51
faiss::gpu::GpuIndexIVFPQConfig::usePrecomputedTables
bool usePrecomputedTables
Definition: GpuIndexIVFPQ.h:35
faiss::gpu::GpuResources
Definition: GpuResources.h:22
faiss::gpu::CpuTimer
CPU wallclock elapsed timer.
Definition: Timer.h:41
faiss::gpu::GpuIndexIVFPQConfig::useFloat16LookupTables
bool useFloat16LookupTables
Definition: GpuIndexIVFPQ.h:31
faiss::gpu::GpuIndexConfig::device
int device
GPU device on which the index is resident.
Definition: GpuIndex.h:26
faiss::gpu::GpuIndexIVFPQConfig
Definition: GpuIndexIVFPQ.h:22
faiss::IndexIVFPQ
Definition: IndexIVFPQ.h:35
faiss::gpu::GpuIndexIVFPQ
IVFPQ index for the GPU.
Definition: GpuIndexIVFPQ.h:39
faiss::gpu::HostTensor
Definition: HostTensor.cuh:21
faiss::gpu::GpuIndexIVFConfig::indicesOptions
IndicesOptions indicesOptions
Index storage options for the GPU.
Definition: GpuIndexIVF.h:30
faiss::gpu::IndexWrapper
Definition: IndexWrapper.h:24