Faiss

- Main Page
- Namespaces
- Classes
- Files

- File List

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