GpuFlatIndex<T>

Models & Types · AiDotNet.RetrievalAugmentedGeneration.VectorSearch.Indexes

Flat (brute-force) index that offloads the query-vs-all similarity computation to the GPU when a device is available, falling back to an exact CPU scan otherwise.

Implements: IVectorIndex<T>

How It Works

Like FlatIndex this index compares the query against every stored vector and returns exact nearest neighbors, but the scoring step is executed as a single batched GPU matrix multiply (see GpuVectorScorer), matching how FAISS-GPU / Milvus-GPU accelerate flat search. This makes large brute-force scans (tens of thousands to millions of vectors) dramatically faster while preserving exact results.

Fallback. The GPU path is used only when: the GPU is available and not disabled via AIDOTNET_DISABLE_GPU; the number of stored vectors is at or above GpuThreshold; and the metric has a GPU closed form (dot-product, cosine, Euclidean). In every other case — and if any GPU error occurs — the index performs the exact same CPU brute-force scan as FlatIndex, producing identical top-k ordering.

Because the GPU path falls back to CPU when no device is present, this type is safe to use in CI and on all target frameworks; it never throws when the GPU is absent.

Constructors

Constructor Summary
GpuFlatIndex(ISimilarityMetric<T> metric, int gpuThreshold) Initializes a new instance of the GpuFlatIndex class.

Properties

Property Summary
int Count { get; }
int GpuThreshold { get; } The minimum number of stored vectors before the GPU path is attempted.

Methods

Method Summary
List<ValueTuple<string, T>> Search(Vector<T> query, int k)
bool Remove(string id)
void Add(string id, Vector<T> vector)
void AddBatch(Dictionary<string, Vector<T>> vectors)
void Clear()