9 #include "GpuIndexFlat.h"
10 #include "../IndexFlat.h"
11 #include "GpuResources.h"
12 #include "impl/FlatIndex.cuh"
13 #include "utils/ConversionOperators.cuh"
14 #include "utils/CopyUtils.cuh"
15 #include "utils/DeviceUtils.h"
16 #include "utils/Float16.cuh"
17 #include "utils/StaticUtils.h"
19 #include <thrust/execution_policy.h>
20 #include <thrust/transform.h>
23 namespace faiss {
namespace gpu {
28 GpuIndex(resources, index->d, index->metric_type, config),
29 config_(std::move(config)),
43 GpuIndex(resources, dims, metric, config),
44 config_(std::move(config)),
55 metric == faiss::METRIC_L2,
62 GpuIndexFlat::~GpuIndexFlat() {
74 FAISS_THROW_IF_NOT_FMT(index->
ntotal <=
76 "GPU index only supports up to %zu indices; "
77 "attempting to copy CPU index with %zu parameters",
78 (
size_t) std::numeric_limits<int>::max(),
109 index->
xb.resize(this->
ntotal * this->
d);
116 fromDevice(vecFloat32, index->
xb.data(), stream);
144 FAISS_THROW_IF_NOT_MSG(this->
is_trained,
"Index not trained");
147 FAISS_THROW_IF_NOT_FMT(n <= (
Index::idx_t) std::numeric_limits<int>::max(),
148 "GPU index only supports up to %d indices",
149 std::numeric_limits<int>::max());
185 FAISS_THROW_IF_NOT_MSG(!ids,
"add_with_ids not supported");
189 FAISS_THROW_IF_NOT_FMT(this->
ntotal + n <=
191 "GPU index only supports up to %zu indices",
192 (
size_t) std::numeric_limits<int>::max());
215 data_->query(queries, k, outDistances, outIntLabels,
true);
218 thrust::transform(thrust::cuda::par.on(stream),
230 FAISS_THROW_IF_NOT_MSG(key < this->
ntotal,
"index out of bounds");
235 fromDevice(vec.data(), out, this->
d, stream);
238 fromDevice(vec.data(), out, this->
d, stream);
248 FAISS_THROW_IF_NOT_MSG(i0 < this->
ntotal,
"index out of bounds");
249 FAISS_THROW_IF_NOT_MSG(i0 + num - 1 < this->ntotal,
"num out of bounds");
254 fromDevice(vec.data(), out, num * this->
d, stream);
257 fromDevice(vec.data(), out, this->
d * num, stream);
262 GpuIndexFlat::verifySettings_()
const {
265 #ifdef FAISS_USE_FLOAT16
267 "useFloat16Accumulator can only be enabled "
270 FAISS_THROW_IF_NOT_FMT(getDeviceSupportsFloat16Math(
config_.
device),
271 "Device %d does not support Hgemm "
272 "(useFloat16Accumulator)",
275 FAISS_THROW_IF_NOT_MSG(
false,
"not compiled with float16 support");
293 GpuIndexFlat(resources, dims, faiss::METRIC_L2, config) {
319 GpuIndexFlat(resources, dims, faiss::METRIC_INNER_PRODUCT, config) {
DeviceTensor< float, 2, true > getVectorsFloat32Copy(cudaStream_t stream)
bool useFloat16Accumulator
void copyFrom(faiss::IndexFlatL2 *index)
void copyTo(faiss::IndexFlat *index) const
void reconstruct_n(faiss::Index::idx_t i0, faiss::Index::idx_t num, float *out) const override
Batch reconstruction method.
int getSize() const
Returns the number of vectors we contain.
Holder of GPU resources for a particular flat index.
void copyTo(faiss::IndexFlatL2 *index)
size_t getNumVecs() const
Returns the number of vectors we contain.
void searchImpl_(int n, const float *x, int k, float *distances, faiss::Index::idx_t *labels) const override
Called from GpuIndex for search.
GpuIndexFlat(GpuResources *resources, const faiss::IndexFlat *index, GpuIndexFlatConfig config=GpuIndexFlatConfig())
bool useFloat16
Whether or not data is stored as float16.
int device
GPU device on which the index is resident.
GpuIndexFlatL2(GpuResources *resources, faiss::IndexFlatL2 *index, GpuIndexFlatConfig config=GpuIndexFlatConfig())
virtual cudaStream_t getDefaultStream(int device)=0
long idx_t
all indices are this type
void reconstruct(faiss::Index::idx_t key, float *out) const override
DeviceMemory & getMemoryManagerCurrentDevice()
Calls getMemoryManager for the current device.
const int device_
The GPU device we are resident on.
void copyTo(faiss::IndexFlatIP *index)
GpuIndexFlatIP(GpuResources *resources, faiss::IndexFlatIP *index, GpuIndexFlatConfig config=GpuIndexFlatConfig())
GpuResources * resources_
Manages streams, cuBLAS handles and scratch memory for devices.
void addImpl_(int n, const float *x, const Index::idx_t *ids) override
Called from GpuIndex for add.
void reserve(size_t numVecs, cudaStream_t stream)
Reserve storage that can contain at least this many vectors.
void add(const float *data, int numVecs, cudaStream_t stream)
idx_t ntotal
total nb of indexed vectors
void add(faiss::Index::idx_t, const float *x) override
void copyFrom(const faiss::IndexFlat *index)
const MemorySpace memorySpace_
The memory space of our primary storage on the GPU.
bool addImplRequiresIDs_() const override
Flat index does not require IDs as there is no storage available for them.
const GpuIndexFlatConfig config_
Our config object.
MetricType metric_type
type of metric this index uses for search
void add(faiss::Index::idx_t, const float *x) override
Overrides to avoid excessive copies.
Tensor< float, 2, true > & getVectorsFloat32Ref()
Returns a reference to our vectors currently in use.
void reset() override
Clears all vectors from this index.
void copyFrom(faiss::IndexFlatIP *index)
bool is_trained
set if the Index does not require training, or if training is done already
void reset()
Free all storage.
std::vector< float > xb
database vectors, size ntotal * d
void train(Index::idx_t n, const float *x) override
This index is not trained, so this does nothing.
MetricType
Some algorithms support both an inner product version and a L2 search version.