Faiss

- Main Page
- Namespaces
- Classes
- Files

- File List

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