kci-vec 0.0.0-stage → 0.2.31
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +100 -2
- package/build.js +36 -0
- package/example.js +38 -0
- package/index.d.ts +83 -0
- package/index.js +274 -0
- package/install.js +21 -0
- package/kci_vec_node.c +378 -0
- package/package.json +46 -4
- package/prebuilds/darwin-arm64/kci_vec.node +0 -0
- package/prebuilds/linux-x64/kci_vec.node +0 -0
- package/prebuilds/win32-x64/kci_vec.node +0 -0
- package/src/kci_crypt.h +302 -0
- package/src/kci_sparse.c +1995 -0
- package/src/kci_sparse.h +208 -0
- package/src/kci_vec.c +5954 -0
- package/src/kci_vec.h +573 -0
- package/src/kci_win_compat.h +381 -0
- package/store.js +225 -0
- package/where.js +93 -0
package/src/kci_vec.h
ADDED
|
@@ -0,0 +1,573 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* kci_vec.h — KCI-Vec, embeddable approximate nearest-neighbour search (public API)
|
|
3
|
+
*
|
|
4
|
+
* Encrypted, embeddable, dimension-agnostic int8 HNSW.
|
|
5
|
+
* - int8 vectors: 1/4 the memory of float32, NEON SDOT distance.
|
|
6
|
+
* - HNSW graph (Malkov & Yashunin): O(log N) approximate search.
|
|
7
|
+
* - Multithreaded build AND batch search (per-node spinlocks on build;
|
|
8
|
+
* lock-free graph reads on search).
|
|
9
|
+
* - Cosine similarity on unit-normalized vectors (int8 dot, fixed scale 127).
|
|
10
|
+
* - Optional encryption of the on-disk snapshot: AES-256-CTR with an
|
|
11
|
+
* AES-256-CMAC tag over the whole file (damage / tampering is refused).
|
|
12
|
+
*
|
|
13
|
+
* One embeddable library, no server. No domain assumptions: the dimension is a
|
|
14
|
+
* runtime parameter and ids are opaque u64 keys chosen by the caller.
|
|
15
|
+
*
|
|
16
|
+
* Thread-safety: build and search take an explicit thread count and manage
|
|
17
|
+
* their own workers. A KciVec is otherwise single-writer; do not call _add or
|
|
18
|
+
* _build concurrently with searches on the same handle.
|
|
19
|
+
*/
|
|
20
|
+
#ifndef KCI_VEC_H
|
|
21
|
+
#define KCI_VEC_H
|
|
22
|
+
|
|
23
|
+
#include <stdint.h>
|
|
24
|
+
#include <stddef.h>
|
|
25
|
+
|
|
26
|
+
#ifdef __cplusplus
|
|
27
|
+
extern "C" {
|
|
28
|
+
#endif
|
|
29
|
+
|
|
30
|
+
typedef struct KciVec KciVec;
|
|
31
|
+
|
|
32
|
+
/* Create an empty index. `dim` is the vector dimension: any number above 0. M / efc are HNSW build parameters
|
|
33
|
+
* (M=16, efc=200 are good defaults; pass 0 to use them). Returns NULL on bad args / OOM. */
|
|
34
|
+
KciVec *kci_vec_new(int dim, int M, int efc);
|
|
35
|
+
/* An index by L2 (Euclidean) distance instead of cosine. Vectors keep their length (not normalized); every search
|
|
36
|
+
* returns the nearest by |q - x| and out_scores holds the squared distance |q - x|^2, smallest first. Inside it is
|
|
37
|
+
* the cosine index of dim+2 dims (each vector gets two more coordinates that turn the L2 order into a cosine order
|
|
38
|
+
* exactly), so the graph, the lists, the re-rank, save and load all work as for cosine. max_norm: the largest |x|
|
|
39
|
+
* that will ever be added. That first build also fixes the range of lengths: afterwards, adding or inserting a
|
|
40
|
+
* vector much longer or shorter than those (outside twice their spread of |x|^2) returns -1; build a new index for
|
|
41
|
+
* it. A max_norm far above the real lengths wastes precision. Not for L2: kci_vec_add_i8 and
|
|
42
|
+
* kci_vec_set_float_source (both return -1). kci_vec_dim returns dim, and kci_vec_get_float the vector as given.
|
|
43
|
+
* With max_norm 0 the index keeps its rows in a form of its own, chosen at the first build: no distance, answer or
|
|
44
|
+
* score of a search with the float pass changes, and kci_vec_get_float still gives the vector as given. "Longer or
|
|
45
|
+
* shorter" above is then counted from the centre of the vectors of that build. Such an index is saved under file
|
|
46
|
+
* marks of its own, which versions before 0.2.22 refuse. A search without the float pass (kci_vec_search,
|
|
47
|
+
* kci_vec_search_r, or an index that keeps no floats) can score a vector far longer than most of the others too
|
|
48
|
+
* small. Returns NULL on bad args (dim <= 0, max_norm < 0 or not finite) / OOM. */
|
|
49
|
+
KciVec *kci_vec_new_l2(int dim, int M, int efc, float max_norm);
|
|
50
|
+
int kci_vec_is_l2(const KciVec *v); /* 1 = made by kci_vec_new_l2 (or loaded from such a file) */
|
|
51
|
+
/* An index by inner product (maximum inner product search) instead of cosine. Vectors keep their length; every
|
|
52
|
+
* search returns the vectors with the largest q.x and out_scores holds q.x, largest first. Inside it is the cosine
|
|
53
|
+
* index of dim+1 dims: each vector is scaled to length <= 1 and gets one more coordinate that brings its length to
|
|
54
|
+
* 1, the query gets a 0 there (the published reduction of Bachrach et al. 2014). It changes no ranking (every
|
|
55
|
+
* score moves by the same q.mean, which is added back) and lets the int8 rows and the codes describe what differs
|
|
56
|
+
* between the vectors instead of what they share. Measure both with your own queries. 0 / -1 (not an inner-product
|
|
57
|
+
* index, or already built). max_norm: the largest |x| that will ever be added. Afterwards a longer vector
|
|
58
|
+
* (centred: one farther from the mean than the farthest at that build; with max_norm: any vector longer than
|
|
59
|
+
* max_norm) returns -1 from add / insert. Not for inner product: kci_vec_add_i8 and kci_vec_set_float_source (both
|
|
60
|
+
* return -1). kci_vec_dim returns dim, kci_vec_get_float the vector as given. Not in a KCI_VEC_NO_L2 build.
|
|
61
|
+
* Returns NULL on bad args (dim <= 0, max_norm < 0 or not finite) / OOM. */
|
|
62
|
+
KciVec *kci_vec_new_ip(int dim, int M, int efc, float max_norm);
|
|
63
|
+
int kci_vec_set_ip_centre(KciVec *v, int on);
|
|
64
|
+
int kci_vec_metric(const KciVec *v); /* 0 = cosine, 1 = L2, 2 = inner product; -1 = NULL */
|
|
65
|
+
void kci_vec_free(KciVec *v);
|
|
66
|
+
|
|
67
|
+
/* Reserve capacity for `n` vectors up front (avoids realloc churn). */
|
|
68
|
+
int kci_vec_reserve(KciVec *v, uint32_t n);
|
|
69
|
+
|
|
70
|
+
/* Append one vector. `id` is the caller's opaque key returned by searches. _add takes float32 (length dim) and
|
|
71
|
+
* L2-normalizes + quantizes to int8. _add_i8 takes a pre-quantized unit-norm int8 vector (length dim). Returns the
|
|
72
|
+
* new vector count, or -1 on error. Adding invalidates any previously built graph (call kci_vec_build again). With
|
|
73
|
+
* kci_vec_set_dedup, an id that shared another id's row and is added again gets its new vector. Ids are keys: the last row added for an id counts. */
|
|
74
|
+
int64_t kci_vec_add(KciVec *v, uint64_t id, const float *vec);
|
|
75
|
+
int64_t kci_vec_add_i8(KciVec *v, uint64_t id, const int8_t *vec);
|
|
76
|
+
|
|
77
|
+
/* The graph and traversal stay int8 (1/4 RAM); in production the float store is memory-mapped from disk so RAM
|
|
78
|
+
* stays int8 — only a handful of float rows are read per query. */
|
|
79
|
+
int kci_vec_keep_float(KciVec *v, int on);
|
|
80
|
+
|
|
81
|
+
/* Move the retained float store to `path` and mmap it read-only, so the resident set stays int8 (codes + graph,
|
|
82
|
+
* ~1/4 of float32) and only the float rows touched by re-rank fault in. Call once after kci_vec_build (with
|
|
83
|
+
* kci_vec_keep_float on). The float store becomes read-only afterwards (no insert/compact). Returns 0 on success,
|
|
84
|
+
* -1 otherwise. */
|
|
85
|
+
int kci_vec_float_mmap(KciVec *v, const char *path);
|
|
86
|
+
|
|
87
|
+
/* Pass 0 to leave a value unchanged. Stored vectors are unaffected. */
|
|
88
|
+
int kci_vec_set_params(KciVec *v, int M, int efc);
|
|
89
|
+
|
|
90
|
+
/* Build (or rebuild) the HNSW graph over all added vectors.
|
|
91
|
+
* threads <= 0 selects a sensible default (cpu cores). Returns 0 on success,
|
|
92
|
+
* and writes the wall-clock build seconds to *out_secs if non-NULL. */
|
|
93
|
+
int kci_vec_build(KciVec *v, int threads, double *out_secs);
|
|
94
|
+
/* The graph is of the same kind (same file format, same searches, rows can be added and deleted after); efc is
|
|
95
|
+
* used for the upper layers only. Below 20,000 rows it is the same as kci_vec_build. Returns as kci_vec_build. */
|
|
96
|
+
int kci_vec_build_fast(KciVec *v, int threads, double *out_secs);
|
|
97
|
+
|
|
98
|
+
/* Approximate top-k search via HNSW. Fills out_ids[0..ret) and (if non-NULL) out_scores[] with cosine similarity,
|
|
99
|
+
* best first. Returns the number of results (<= k), or -1 if no graph is built. */
|
|
100
|
+
int kci_vec_search(KciVec *v, const float *query, int k, int ef,
|
|
101
|
+
uint64_t *out_ids, float *out_scores);
|
|
102
|
+
|
|
103
|
+
/* Reusable per-thread search context. Create one per worker thread; the
|
|
104
|
+
* visited-set and heaps are allocated once and reused across queries. Multiple
|
|
105
|
+
* searchers may run concurrently on one (already-built, read-only) KciVec.
|
|
106
|
+
* A searcher stays valid across kci_vec_insert and kci_vec_delete, so it can be kept for the life of the index. Make
|
|
107
|
+
* new ones after kci_vec_build, kci_vec_build_fast, kci_vec_compact, kci_vec_forget and kci_vec_build_ivf*.
|
|
108
|
+
* Searchers do not make the index safe to write while it is read: insert / delete / compact must not run at the same
|
|
109
|
+
* time as any search on that KciVec (use a reader-writer lock in the caller). */
|
|
110
|
+
typedef struct KciVecSearcher KciVecSearcher;
|
|
111
|
+
KciVecSearcher *kci_vec_searcher_new(KciVec *v);
|
|
112
|
+
void kci_vec_searcher_free(KciVecSearcher *s);
|
|
113
|
+
/* The graph search of kci_vec_search on a searcher (no allocation per call): the int8 rows only, no float pass (for
|
|
114
|
+
* that: kci_vec_search_rerank). ef >= k: the width of the search (larger = higher recall, slower); with
|
|
115
|
+
* kci_vec_searcher_set_eps a small ef (k..3k). out_ids needs room for k ids; out_scores (k floats) may be NULL.
|
|
116
|
+
* Scores as kci_vec_search (cosine, |q-x|^2 for L2, q.x for inner product), best first. Returns the number of
|
|
117
|
+
* results (<= k), or -1 (no graph built, or a search-only index: it has no int8 rows, use kci_vec_search_rerank). */
|
|
118
|
+
int kci_vec_search_r(KciVecSearcher *s, const float *query,
|
|
119
|
+
int k, int ef, uint64_t *out_ids, float *out_scores);
|
|
120
|
+
|
|
121
|
+
/* Two-stage search: int8 HNSW shortlist of `rerank_n` candidates → full- precision float re-rank → top-k. Requires
|
|
122
|
+
* kci_vec_keep_float. Returns -2 if no float store is present. For ef >= 2k on the plain graph, a query that
|
|
123
|
+
* falls in a group of nearly equal vectors larger than ef is searched wider than the ef given (up to 16,384),
|
|
124
|
+
* and takes longer. */
|
|
125
|
+
int kci_vec_search_rerank(KciVecSearcher *s, const float *query,
|
|
126
|
+
int k, int ef, int rerank_n,
|
|
127
|
+
uint64_t *out_ids, float *out_scores);
|
|
128
|
+
|
|
129
|
+
/* Exact top-k (ground truth / small-N fallback / a search that must not miss a row), multithreaded. Does not
|
|
130
|
+
* require a built graph. threads <= 0 = default. With a built graph and the floats kept it costs less than a scan
|
|
131
|
+
* over the floats (4 bytes a row more memory, made at the first call) and returns the same answer
|
|
132
|
+
* (tests/test_sure.c). An index with no vectors: 0 results. */
|
|
133
|
+
int kci_vec_search_exact(KciVec *v, const float *query, int k, int threads,
|
|
134
|
+
uint64_t *out_ids, float *out_scores);
|
|
135
|
+
/* Brute force over the ids that pass filt (filt(id, ctx) != 0 keeps id; the filtered searches below take the same kind
|
|
136
|
+
* of function): the exact top-k under a condition, and the fast way when the condition leaves few rows. Scores and
|
|
137
|
+
* return value as kci_vec_search_exact; fewer than k when fewer ids pass. With threads != 1 filt is called from several
|
|
138
|
+
* threads at once, so it must only read. filt == NULL: the same as kci_vec_search_exact. */
|
|
139
|
+
typedef int (*KciVecFilter)(uint64_t id, void *ctx);
|
|
140
|
+
int kci_vec_search_exact_filtered(KciVec *v, const float *query, int k, int threads, KciVecFilter filt, void *ctx,
|
|
141
|
+
uint64_t *out_ids, float *out_scores);
|
|
142
|
+
/* The scores the exact search would give n vectors that are not in v (xs: n x dim floats, as for kci_vec_add): for
|
|
143
|
+
* merging rows kept elsewhere (added since the index was saved) with an answer of v. A vector that is stored in v
|
|
144
|
+
* and given here gets the same score as kci_vec_search_exact returns for it, so the two copies are a tie. Scores
|
|
145
|
+
* are as the searches give them (cosine, squared distance, inner product). A vector the index could not take (NaN,
|
|
146
|
+
* or longer than an L2 / inner-product index allows) gets NaN. Needs the floats (kci_vec_keep_float, not halves).
|
|
147
|
+
* Returns the number of NaN scores (0 normally), -1 on error. */
|
|
148
|
+
int kci_vec_score_exact(KciVec *v, const float *query, const float *xs, int n, float *out_scores);
|
|
149
|
+
/* A ready-made KciVecFilter for ids that are small numbers (row numbers): ctx points to a KciVecIdBits; id passes when
|
|
150
|
+
* id < nbits and bit (id & 63) of bits[id >> 6] is set. Usable with every filtered search. */
|
|
151
|
+
typedef struct { const uint64_t *bits; uint64_t nbits; } KciVecIdBits;
|
|
152
|
+
int kci_vec_filter_id_bits(uint64_t id, void *ctx);
|
|
153
|
+
|
|
154
|
+
/* ── Incremental update (single-writer: do NOT call concurrently with searches;
|
|
155
|
+
* pause serving, batch updates, resume). All require a built graph; insert also
|
|
156
|
+
* requires kci_vec_keep_float. Bulk loads should still use add() + build(). */
|
|
157
|
+
/* Insert one vector into the live graph. Returns the new count, -1 on error, -2 if floats were not retained
|
|
158
|
+
* (kci_vec_keep_float). Ids are keys: insert of an id that has a live row replaces it (as delete then insert), and
|
|
159
|
+
* at a build the last row added with an id is kept, the earlier ones are tombstoned. */
|
|
160
|
+
int64_t kci_vec_insert(KciVec *v, uint64_t id, const float *vec);
|
|
161
|
+
/* Tombstone the vector with caller id (excluded from results, kept as a routing hop). Returns 0, or -1 if id not
|
|
162
|
+
* found. Update = delete then insert. -1 also when the index is not built (never, or rows were added with
|
|
163
|
+
* kci_vec_add after the build): build first, then delete. */
|
|
164
|
+
int kci_vec_delete(KciVec *v, uint64_t id);
|
|
165
|
+
/* Forget id for good: every row of it (also tombstoned earlier versions) is zeroed and removed, its list entries
|
|
166
|
+
* cleared, and the centroid of every list it was in computed again from the remaining live members (their codes
|
|
167
|
+
* encoded again): nothing of the vector stays in RAM or in a later save, except its share in the global 4-bit
|
|
168
|
+
* codebooks (trained on a sample of up to 16,384 residuals). -2 (rebuild instead) for a graph, mmap'd floats,
|
|
169
|
+
* low-rank lists, or an index with neither floats nor a float source. Rows removed (0 = absent), -1 on error.
|
|
170
|
+
* Recreate searchers afterwards. */
|
|
171
|
+
int kci_vec_forget(KciVec *v, uint64_t id);
|
|
172
|
+
/* Rebuild over only the live vectors, reclaiming tombstoned slots. Returns the new
|
|
173
|
+
* live count. Requires retained floats; a no-op (returns count) if nothing dead. */
|
|
174
|
+
uint32_t kci_vec_compact(KciVec *v);
|
|
175
|
+
|
|
176
|
+
/* ── Filtered search (metadata / ACL at the engine layer) ──────────────────
|
|
177
|
+
* Predicate over the caller's id: return non-zero to keep a vector eligible. The
|
|
178
|
+
* graph is traversed through non-matching nodes (connectivity preserved) but only
|
|
179
|
+
* matching nodes enter the result set, so selective filters still find their
|
|
180
|
+
* nearest matches — unlike post-filtering a plain top-k. Bounded by a visit budget
|
|
181
|
+
* (~64*ef) so a very selective filter degrades to best-effort rather than scanning
|
|
182
|
+
* the whole graph; for tiny matching sets prefer brute force + your own filter.
|
|
183
|
+
* rerank_n>0 adds the float re-rank (needs kci_vec_keep_float). filt==NULL behaves
|
|
184
|
+
* like kci_vec_search_rerank. */
|
|
185
|
+
/* Float source. A list index (no graph) can drop its resident floats and fetch a candidate's float from the caller
|
|
186
|
+
* during the re-rank instead: fn(id, out, ctx) fills out[dim] (any length, it is normalized) and returns 0, or
|
|
187
|
+
* nonzero if the id is gone (it then ranks last). An application that keeps every vector in its own store (for
|
|
188
|
+
* example an encrypted key-value store) saves dim*4 bytes per vector of RAM, and no plaintext copy of the vectors
|
|
189
|
+
* stays resident. Costs rerank_n calls per query. After it: kci_vec_insert works from the given vector; exact
|
|
190
|
+
* search ranks by the int8 rows; kci_vec_compact does nothing (rebuild from your store instead). ctx_free(ctx), if
|
|
191
|
+
* given, runs when the index is freed or the source replaced. Returns 0, -1 (no lists, a graph, or mmap'd floats). */
|
|
192
|
+
typedef int (*KciVecFloatFn)(uint64_t id, float *out, void *ctx);
|
|
193
|
+
int kci_vec_set_float_source(KciVec *v, KciVecFloatFn fn, void *ctx, void (*ctx_free)(void *));
|
|
194
|
+
/* Filtered search: described under "Filtered search" above (KciVecFilter). filt(id, ctx) != 0 keeps the id eligible;
|
|
195
|
+
* ctx is handed to filt unchanged. k, ef, out_ids, out_scores as kci_vec_search_r; rerank_n > 0 adds the exact pass
|
|
196
|
+
* over the best rerank_n when the floats (or the halves) are kept, otherwise the scores are the int8 ones. Returns
|
|
197
|
+
* the number of results (<= k, fewer when fewer ids pass within the visit budget), or -1 (no graph built; a
|
|
198
|
+
* search-only index without the quantized graph and an exact pass). */
|
|
199
|
+
int kci_vec_search_filtered(KciVecSearcher *s, const float *query, int k, int ef,
|
|
200
|
+
int rerank_n, KciVecFilter filt, void *ctx,
|
|
201
|
+
uint64_t *out_ids, float *out_scores);
|
|
202
|
+
|
|
203
|
+
/* Needs kci_vec_keep_float(v, 1). Build it after the vectors are added — and after kci_vec_build if you also want
|
|
204
|
+
* the graph, because that build renumbers the vectors and drops this index. nlist = number of lists (0:
|
|
205
|
+
* 2*sqrt(count); 2,000 suits ~1 M vectors). Inserts (kci_vec_insert) and deletes work on it; kci_vec_compact
|
|
206
|
+
* rebuilds it; kci_vec_save / _load keep it. Returns 0, or -1 (no floats, out of memory). */
|
|
207
|
+
int kci_vec_build_ivf(KciVec *v, int nlist, int threads);
|
|
208
|
+
/* Same, with a second list per vector (SOAR, as in ScaNN; each vector is also coded in the list of a second
|
|
209
|
+
* centroid chosen so that its error is unlike the first one's (weight soar_lambda; 1.5 is ScaNN's value; 0 = one
|
|
210
|
+
* list only, which is what kci_vec_build_ivf does). Saved with the index. */
|
|
211
|
+
int kci_vec_build_ivf_ex(KciVec *v, int nlist, int threads, float soar_lambda);
|
|
212
|
+
/* All build options. code_dims > 0: the 4-bit codes describe the residual projected onto its top code_dims
|
|
213
|
+
* principal directions (PCA of a sample of residuals) instead of all dims, so the scan reads code_dims/2 bytes per
|
|
214
|
+
* vector instead of dim/2; the re-rank (float, or int8 with kci_vec_searcher_set_rerank_int8) restores the
|
|
215
|
+
* precision. Even, at least 8, below dim; 0 = all dims. Zero the struct for the defaults. Known weak spot: a
|
|
216
|
+
* vector that differs from the rest only outside the projected directions looks like the ordinary vectors to the
|
|
217
|
+
* codes, and the shortlist can leave it out even when it is the query itself (16 dims, 8 projected, one such
|
|
218
|
+
* vector among 4,000: missed in 120 of 120 builds, every list probed, rerank_n 200). */
|
|
219
|
+
/* lowrank > 0 low-rank scoring instead of the 4-bit tables (the LoRANN idea). Each list keeps the top-lowrank
|
|
220
|
+
* principal directions of its own residuals (int8) and every vector lowrank int8 coefficients on them; a query is
|
|
221
|
+
* projected onto a probed list's directions once, then each vector costs a lowrank-term int8 dot — lowrank bytes
|
|
222
|
+
* per vector instead of dim/2. Multiples of 16, at most 256, below dim; 64 suits 384-768 dims. Use a larger
|
|
223
|
+
* rerank_n (400 at k=100) and the int8 or float re-rank. Overrides code_dims. */
|
|
224
|
+
typedef struct { int nlist; int threads; float soar; int code_dims; int lowrank; } KciVecIvfParams;
|
|
225
|
+
int kci_vec_build_ivf_p(KciVec *v, const KciVecIvfParams *params);
|
|
226
|
+
/* Its weight (dims-1) t^2 / (1-t^2) is off below 1: at 0.2 that is below 26 dims. Kept with the index (save /
|
|
227
|
+
* load, compact). Returns 0, or -1 on bad args. */
|
|
228
|
+
int kci_vec_set_ivf_codes(KciVec *v, int dims_per_code, float avq_threshold);
|
|
229
|
+
/* How the next kci_vec_build_ivf* trains the centroids: k-means iterations (0 = 10) over a sample of at most
|
|
230
|
+
* `sample` vectors (0 = 250,000; UINT32_MAX = all). The codes and the search are unchanged. Returns 0, or -1
|
|
231
|
+
* (iterations outside 0..1000). */
|
|
232
|
+
int kci_vec_set_ivf_training(KciVec *v, int iterations, uint32_t sample);
|
|
233
|
+
/* kci_vec_search_ivf re-ranks the shortlist with the int8 rows instead of the floats 1/4 of the bytes to read. Off
|
|
234
|
+
* by default. Scores stay cosine. */
|
|
235
|
+
int kci_vec_searcher_set_rerank_int8(KciVecSearcher *s, int on);
|
|
236
|
+
/* Probe gap for kci_vec_search_ivf of the best nprobe lists, scan only those whose centroid score (cosine) is
|
|
237
|
+
* within `gap` of the nearest one's, but at least `min_probe`. nprobe stays the upper bound. The best gap depends
|
|
238
|
+
* on the data: try 0.15-0.35. Returns 0, or -1 (no searcher, gap NaN). */
|
|
239
|
+
int kci_vec_searcher_set_probe_gap(KciVecSearcher *s, float gap, int min_probe);
|
|
240
|
+
/* Exploration radius for the graph search, the stop rule NGT-qg uses. eps > 0: the result set is kept at `ef`
|
|
241
|
+
* entries (pass a small ef, k..3k) and the search goes on while the next candidate's cosine distance is within
|
|
242
|
+
* (1+eps) x the ef-th best; eps <= 0 (the default) is plain HNSW (stop when the next candidate is worse than the
|
|
243
|
+
* ef-th best). Sweep per data set. Applies to kci_vec_search_r / _rerank / _filtered. Returns 0, or -1 (no
|
|
244
|
+
* searcher, eps NaN). */
|
|
245
|
+
int kci_vec_searcher_set_eps(KciVecSearcher *s, float eps);
|
|
246
|
+
/* Quantized graph layer 0 of the graph laid out the way NGT-qg / qsgngt search it. Each node keeps its neighbours'
|
|
247
|
+
* 4-bit codes beside it, so one hop reads one contiguous block and scores every neighbour with SIMD table lookups
|
|
248
|
+
* (the lists' kernel) instead of reading M0 int8 rows at random. The upper layers and the exact re-score of the
|
|
249
|
+
* result set still use the rows. The anisotropic threshold of kci_vec_set_ivf_codes applies if set. Not saved;
|
|
250
|
+
* dropped by kci_vec_insert, kci_vec_compact and kci_vec_build (build it again after). Returns 0, or -1 (no graph,
|
|
251
|
+
* bad args, out of memory). */
|
|
252
|
+
int kci_vec_build_qg(KciVec *v, int dims_per_code, int threads);
|
|
253
|
+
/* The same with the neighbours' codes over the top code_dims principal directions only (even, 8..dim-1; 0 = all
|
|
254
|
+
* dimensions, as kci_vec_build_qg). A smaller block per node: less memory and a cheaper hop, coarser estimates.
|
|
255
|
+
* Returns 0, or -1 (as kci_vec_build_qg, or a bad code_dims). */
|
|
256
|
+
int kci_vec_build_qg_ex(KciVec *v, int dims_per_code, int code_dims, int threads);
|
|
257
|
+
/* As kci_vec_build_qg_ex, with the neighbours' codes over the first gate_dims of the code_dims directions only
|
|
258
|
+
* (even, 8..code_dims; 0 = code_dims, the same as kci_vec_build_qg_ex). The short rows of
|
|
259
|
+
* kci_vec_searcher_set_qg_exact(s, 2) keep all code_dims coordinates, so the block per node shrinks while the
|
|
260
|
+
* ranking inside the search stays as fine. Use it for the memory, with set_qg_exact(s, 2). Returns 0, or -1 (as
|
|
261
|
+
* kci_vec_build_qg_ex, or a bad gate_dims). */
|
|
262
|
+
int kci_vec_build_qg_gate(KciVec *v, int dims_per_code, int code_dims, int gate_dims, int threads);
|
|
263
|
+
/* A quantized graph with residual codes: the memory-first form. Each neighbour is kept as its difference from the
|
|
264
|
+
* node on the first gate_dims of the code_dims principal directions (2 bits a dimension; gate_dims 0 = code_dims),
|
|
265
|
+
* with one scale per neighbour, so a neighbour takes gate_dims / 4 + 4 bytes. The search always scores with the
|
|
266
|
+
* short rows (as kci_vec_searcher_set_qg_exact(s, 2)); use it with kci_vec_search_rerank. code_dims as
|
|
267
|
+
* kci_vec_build_qg_ex (required, > 0). Saved and loaded like the other forms; a library older than this one
|
|
268
|
+
* refuses such a file. Returns 0, or -1 (as kci_vec_build_qg_ex, code_dims 0, or no memory for the short rows). */
|
|
269
|
+
int kci_vec_build_qg_res(KciVec *v, int code_dims, int gate_dims, int threads);
|
|
270
|
+
/* Bytes the quantized graph takes (0 = none). */
|
|
271
|
+
int64_t kci_vec_qg_bytes(const KciVec *v);
|
|
272
|
+
/* Search over the quantized graph (on = 1): kci_vec_search_r re-scores the result set with the int8 rows,
|
|
273
|
+
* kci_vec_search_rerank with the floats. Use a small ef (k..3k) with kci_vec_searcher_set_eps, since the ef-th
|
|
274
|
+
* best is an estimate now. kci_vec_search_filtered uses it too rows that fail the filter are walked through and
|
|
275
|
+
* left out of the result, as on the plain graph); the plain kci_vec_search does not. Returns 0, -1 (no searcher),
|
|
276
|
+
* -2 (the index has no quantized graph: kci_vec_build_qg first). on = -1: automatic, and the default of a new
|
|
277
|
+
* searcher: the quantized graph is used whenever the index has one (built, loaded from a file or attached from an
|
|
278
|
+
* image), with the short rows if it has them (as kci_vec_searcher_set_qg_exact(s, 2); that switch takes -1 for
|
|
279
|
+
* automatic too). */
|
|
280
|
+
int kci_vec_searcher_set_qg(KciVecSearcher *s, int on);
|
|
281
|
+
/* Worth it where the estimates are coarse (high dimensions, 2 dims a code); off by default. Returns 0, or -1 (no
|
|
282
|
+
* searcher). on = 2: with a graph built by kci_vec_build_qg_ex (code_dims > 0), score them with short int8 rows
|
|
283
|
+
* instead (the code_dims coordinates, code_dims bytes a row; the graph keeps them, count x code_dims bytes).
|
|
284
|
+
* Without code_dims, 2 is the same as 1. */
|
|
285
|
+
int kci_vec_searcher_set_qg_exact(KciVecSearcher *s, int on);
|
|
286
|
+
/* Search the lists: score the centroids, scan the best `nprobe` lists with the 4-bit tables, keep the best
|
|
287
|
+
* `rerank_n` and re-score them with the float vectors. Scores are cosine. Returns the hit count, or -1 if the
|
|
288
|
+
* index has no lists. */
|
|
289
|
+
int kci_vec_search_ivf(KciVecSearcher *s, const float *query, int k, int nprobe, int rerank_n,
|
|
290
|
+
uint64_t *out_ids, float *out_scores);
|
|
291
|
+
/* The same with a filter over the caller's ids (metadata / ACL; only vectors with filt(id, ctx) != 0 enter the
|
|
292
|
+
* shortlist and the answer. The predicate is asked only for candidates that beat the shortlist's worst (a few
|
|
293
|
+
* hundred per query), so a Python callback is fine. A selective filter needs a larger nprobe / rerank_n to find
|
|
294
|
+
* the best k matches. Fewer than k answers: when this search or kci_vec_search_filtered finds fewer than k, the
|
|
295
|
+
* index answers again by brute force over the ids that pass (kci_vec_search_exact_filtered, one thread), so every
|
|
296
|
+
* passing id is returned when fewer than k exist. This calls filt once per vector for such a query;
|
|
297
|
+
* kci_vec_searcher_set_filter_fill(s, 0) turns it off (the earlier behaviour). When you know that few ids pass,
|
|
298
|
+
* call kci_vec_search_exact_filtered directly. */
|
|
299
|
+
int kci_vec_search_ivf_filtered(KciVecSearcher *s, const float *query, int k, int nprobe, int rerank_n,
|
|
300
|
+
KciVecFilter filt, void *ctx, uint64_t *out_ids, float *out_scores);
|
|
301
|
+
int kci_vec_searcher_set_filter_fill(KciVecSearcher *s, int on); /* default on; see above */
|
|
302
|
+
|
|
303
|
+
/* On-disk snapshot. If key != NULL (keylen bytes), the serialized index is encrypted at rest (format KCIVEC05:
|
|
304
|
+
* AES-256-CTR, key from PBKDF2-HMAC-SHA256 with a per-save salt, and an AES-256-CMAC tag over the whole file);
|
|
305
|
+
* pass the same key to _load. With key=NULL the snapshot is plaintext (KCIVEC04). The graph is saved too, so _load
|
|
306
|
+
* needs no rebuild. Older KCIVEC04 files encrypted with ChaCha20 (no tag) still load. The quantized graph
|
|
307
|
+
* (kci_vec_build_qg*, with its short rows) and the halves (kci_vec_half_floats) are saved too, inside the
|
|
308
|
+
* encrypted and authenticated part: the loaded index answers kci_vec_search_rerank through the quantized graph
|
|
309
|
+
* without building it again (kci_vec_searcher_set_qg as usual). */
|
|
310
|
+
int kci_vec_save(KciVec *v, const char *path, const uint8_t *key, int keylen);
|
|
311
|
+
KciVec *kci_vec_load(const char *path, const uint8_t *key, int keylen);
|
|
312
|
+
/* kci_vec_save with options. KCI_VEC_SAVE_NO_FLOAT leaves the retained floats out of the file (for a caller that
|
|
313
|
+
* stores the vectors elsewhere, e.g. an application's own store): a smaller file, less to decrypt at load. */
|
|
314
|
+
#define KCI_VEC_SAVE_NO_FLOAT 1
|
|
315
|
+
/* Such a file loads only with kci_vec_load_ex(..., KCI_VEC_LOAD_RAW_KEY), and a passphrase file only without it.
|
|
316
|
+
* Use it only with a key of full strength (32 random bytes). */
|
|
317
|
+
#define KCI_VEC_SAVE_RAW_KEY 2
|
|
318
|
+
#define KCI_VEC_LOAD_RAW_KEY 2
|
|
319
|
+
/* KCI_VEC_LOAD_AUTH_ONLY: kci_vec_load_ex accepts only a file that is encrypted with the authentication tag
|
|
320
|
+
* (AES-256-CTR + CMAC: saved with a key by this library). A plaintext file and an old ChaCha20 file (no tag)
|
|
321
|
+
* return NULL, whatever key is given. For indexes that arrive from outside: a file without a tag can be altered
|
|
322
|
+
* without the load noticing. Combine with KCI_VEC_LOAD_RAW_KEY by |. */
|
|
323
|
+
#define KCI_VEC_LOAD_AUTH_ONLY 4
|
|
324
|
+
int kci_vec_save_ex(KciVec *v, const char *path, const uint8_t *key, int keylen, int flags);
|
|
325
|
+
KciVec *kci_vec_load_ex(const char *path, const uint8_t *key, int keylen, int flags);
|
|
326
|
+
/* Set the retained float of the vector with this id (after a KCI_VEC_SAVE_NO_FLOAT load). 0, or -1 (unknown id, no
|
|
327
|
+
* float store). */
|
|
328
|
+
int kci_vec_set_float(KciVec *v, uint64_t id, const float *vec);
|
|
329
|
+
|
|
330
|
+
/* Lists learned from a query sample, for data whose queries come from another distribution than the vectors (text
|
|
331
|
+
* queries over images, the queries and keys of an attention head) and whose answers concentrate on few vectors.
|
|
332
|
+
* kci_vec_learn: `queries` = nq sample queries (dim floats each, the caller's dim), `answers` = their true or
|
|
333
|
+
* approximate nearest ids, per_query each (an id not in the index is skipped). Needs a built index (graph or
|
|
334
|
+
* lists). 0 / -1. The learned lists name rows: kci_vec_build and kci_vec_compact drop them (learn again), inserts
|
|
335
|
+
* keep them (new vectors are just not in them), they are not saved. kci_vec_search_learned: the `nclusters`
|
|
336
|
+
* (1..64) nearest clusters, the first `per_list` vectors of each, then ef = 0 : those are scored exactly on the
|
|
337
|
+
* kept floats and the best k returned. Fast; a query unlike the sample gets a poor answer, there is no fallback;
|
|
338
|
+
* ef > 0 : they are added as starting points of the graph search (ef as in kci_vec_search_rerank, re-rank of the
|
|
339
|
+
* ef best), so the graph completes what the lists miss. Needs the graph (kci_vec_build). From 0.2.13, when the
|
|
340
|
+
* index also has a quantized graph (kci_vec_build_qg and its forms) and the searcher uses it, the walk goes over
|
|
341
|
+
* that graph; such an index answers differently from 0.2.12 (kci_vec_searcher_set_qg(s, 0) gives the earlier
|
|
342
|
+
* walk). ef < 0 : needs
|
|
343
|
+
* kci_vec_learn_pack. The lists' own int8 copies are scanned in order and only the -ef best (150 - 500) are scored
|
|
344
|
+
* exactly. The fastest form when a query reads thousands of list entries; like ef = 0 it has no fallback for a
|
|
345
|
+
* query unlike the sample. kci_vec_learn_pack (after kci_vec_learn): gives every list an int8 copy of its vectors
|
|
346
|
+
* (dim bytes per list entry), centred on the mean of all vectors, which changes no ranking and is what lets one
|
|
347
|
+
* byte per dimension tell them apart. 0 / -1. Dropped with the lists. Needs kci_vec_keep_float. Returns the count,
|
|
348
|
+
* -1 on error, -2 if nothing is learned (or it was dropped; ef < 0: not packed). Same scores as the other searches
|
|
349
|
+
* (cosine, |q-x|^2 for L2, q.x for inner product). */
|
|
350
|
+
int kci_vec_learn(KciVec *v, const float *queries, uint32_t nq, const uint64_t *answers, int per_query,
|
|
351
|
+
int nclusters, int list_cap, int threads, float *out_top5);
|
|
352
|
+
int kci_vec_has_learned(const KciVec *v); /* 1 = kci_vec_search_learned can be used */
|
|
353
|
+
/* Described above with kci_vec_search_learned (ef < 0). Call after kci_vec_learn. Returns 0, or -1 (nothing learned
|
|
354
|
+
* or it was dropped, no floats kept, an attached image, out of memory). */
|
|
355
|
+
int kci_vec_learn_pack(KciVec *v);
|
|
356
|
+
/* For ef > 0 on packed lists: start the graph from the `best` rows of the int8 scan of the list prefixes instead
|
|
357
|
+
* of from all of them (0 = all, the default). Fewer, better starting points. 0 / -1. */
|
|
358
|
+
int kci_vec_searcher_set_learned_seeds(KciVecSearcher *s, int best);
|
|
359
|
+
/* Choose the lists in two stages: kci_vec_learn also groups its clusters into sqrt(nclusters) pages; with pages >
|
|
360
|
+
* 0 a search compares the query with the pages, then only with the clusters of the nearest `pages` of them (0 =
|
|
361
|
+
* compare with every cluster, the default). Cheaper when few lists are read; with too few pages a list is missed.
|
|
362
|
+
* No effect below 64 clusters. 0 / -1. */
|
|
363
|
+
int kci_vec_searcher_set_learned_pages(KciVecSearcher *s, int pages);
|
|
364
|
+
/* Also read the first `rows` of the hot rows: kci_vec_learn keeps, besides the lists, the rows most often an
|
|
365
|
+
* answer over the whole sample (at most a quarter of the index, most frequent first; packed with the lists). For
|
|
366
|
+
* the last answers that the nearest lists miss, on data whose answers are concentrated. 0 = off, the default. 0 /
|
|
367
|
+
* -1. */
|
|
368
|
+
int kci_vec_searcher_set_learned_hot(KciVecSearcher *s, int rows);
|
|
369
|
+
int kci_vec_search_learned(KciVecSearcher *s, const float *query, int k, int nclusters, int per_list, int ef,
|
|
370
|
+
uint64_t *out_ids, float *out_scores);
|
|
371
|
+
/* For data whose answers are not concentrated (kci_vec_learn's concentration is low): the sample queries
|
|
372
|
+
* themselves are the index. kci_vec_vote_set keeps the answers of the nq sample queries (ids of this index,
|
|
373
|
+
* per_query each) and an int8 copy of every vector centred on the mean (dim bytes per vector). 0 / -1. Needs
|
|
374
|
+
* kci_vec_keep_float and a built index; dropped by kci_vec_build / kci_vec_compact (set again), not saved.
|
|
375
|
+
* kci_vec_search_voted: `voters` are numbers (0 .. nq-1) of the sample queries nearest to the query, found by the
|
|
376
|
+
* caller (for instance with a second KciVec holding the sample queries). The union of their answers is scanned on
|
|
377
|
+
* the int8 copies and the `rerank` best (at least k, at most 4096) are scored exactly. Returns the count, -1 on
|
|
378
|
+
* error, -2 if nothing is set. A query unlike the sample gets a poor answer; there is no fallback. Related work:
|
|
379
|
+
* MLANN. */
|
|
380
|
+
int kci_vec_vote_set(KciVec *v, const uint64_t *answers, uint32_t nq, int per_query);
|
|
381
|
+
int kci_vec_search_voted(KciVecSearcher *s, const float *query, const uint32_t *voters, int nvoters, int k, int rerank,
|
|
382
|
+
uint64_t *out_ids, float *out_scores);
|
|
383
|
+
/* Optional short codes for the scan of the union, for high dimensions. kci_vec_vote_short (after
|
|
384
|
+
* kci_vec_vote_set): `rows` holds one short vector (sdim floats, at most 4096) for each of the n = count vectors,
|
|
385
|
+
* in the order of `ids`; the caller chooses the map (short query . short row should approximate the score, up to a
|
|
386
|
+
* constant per query). Kept as int8 centred on the mean, sdim bytes per vector. 0 / -1.
|
|
387
|
+
* kci_vec_search_voted_short: the union is ranked by qshort . short row, the `keep` best (at least `rerank`) go
|
|
388
|
+
* through the full int8 rows, and the `rerank` best of those are scored exactly. -2 without short codes. */
|
|
389
|
+
int kci_vec_vote_short(KciVec *v, const uint64_t *ids, const float *rows, uint32_t n, int sdim);
|
|
390
|
+
int kci_vec_search_voted_short(KciVecSearcher *s, const float *query, const float *qshort, int keep, const uint32_t *voters, int nvoters,
|
|
391
|
+
int k, int rerank, uint64_t *out_ids, float *out_scores);
|
|
392
|
+
|
|
393
|
+
/* Plain, not encrypted: keep the file where the tables are. On an attached index the searches work
|
|
394
|
+
* (kci_vec_search_ivf*, kci_vec_search_exact, kci_vec_get_float); anything that writes (add, insert, delete,
|
|
395
|
+
* forget, compact, build*, keep_float, set_float, float_mmap, protect, reserve) returns -1 (compact: the count).
|
|
396
|
+
* An index with a graph (kci_vec_build) takes it into the image, with the quantized graph (kci_vec_build_qg*, its
|
|
397
|
+
* short rows) and the halves (kci_vec_half_floats) if they are there; the attached index then answers
|
|
398
|
+
* kci_vec_search / _search_r / _search_rerank (with kci_vec_searcher_set_qg) as the index it was saved from. An
|
|
399
|
+
* image written by this version is read by an earlier one as an image without a graph. The file is for the machine
|
|
400
|
+
* that wrote it (byte order, alignment). */
|
|
401
|
+
int kci_vec_image_save(const KciVec *v, const char *path);
|
|
402
|
+
KciVec *kci_vec_attach(const char *path);
|
|
403
|
+
KciVec *kci_vec_attach_mem(const void *image, size_t len);
|
|
404
|
+
/* kci_vec_thaw: a writable copy of an attached image (rows, floats, tombstones, lists, same centroids and
|
|
405
|
+
* codebooks), independent of the mapping. Inserts and deletes work on it, and kci_vec_image_save writes the next
|
|
406
|
+
* image: a refresh that applies only the changes (assign + encode each new vector against the existing centroids;
|
|
407
|
+
* no k-means, no pass over the old rows). NULL: not attached, out of memory. The centroids are not retrained:
|
|
408
|
+
* after many refreshes that shift the data, build again. kci_vec_squeeze: drop the tombstoned rows of a lists-only
|
|
409
|
+
* index without retraining: the rows move down, every list is laid out again with its live entries, the id hash is
|
|
410
|
+
* rebuilt. Searchers must be recreated. Returns the rows removed (0 = none dead), -1 on error, -2 for a graph
|
|
411
|
+
* index or one without lists (use kci_vec_compact). */
|
|
412
|
+
KciVec *kci_vec_thaw(const KciVec *attached);
|
|
413
|
+
int64_t kci_vec_squeeze(KciVec *v);
|
|
414
|
+
|
|
415
|
+
/* Buffers grown later stay protected. Call after build or load. Returns the bits that held for every buffer: 1 =
|
|
416
|
+
* out of core dumps, 2 = locked in RAM (0 = neither, e.g. on Windows without a raised working set), -1 error. */
|
|
417
|
+
int kci_vec_protect(KciVec *v);
|
|
418
|
+
|
|
419
|
+
/* One way: the floats are wiped and freed. Returns 0, or -1 (not built, no resident floats, floats mapped from a
|
|
420
|
+
* file, halves made already). */
|
|
421
|
+
int kci_vec_half_floats(KciVec *v);
|
|
422
|
+
/* Search-only: for a built index with a quantized graph (kci_vec_build_qg*) and an exact pass (the halves of
|
|
423
|
+
* kci_vec_half_floats, or the retained floats), give up what a quantized-graph search does not read: the int8 rows
|
|
424
|
+
* (dim bytes a row) and the plain graph's layer-0 links. One way. Afterwards only kci_vec_search_rerank and
|
|
425
|
+
* kci_vec_search_filtered (with rerank_n > 0) work, over the quantized graph (a searcher set to the plain graph
|
|
426
|
+
* gets -1); kci_vec_search, _search_r, _search_exact, insert, delete, save, image_save, protect and every build
|
|
427
|
+
* return -1. Without short rows (no code_dims) the exact pass always covers the whole result set (rerank_n is
|
|
428
|
+
* taken as ef). To get the rest back, build the index again from the vectors. Returns 0, or -1 (not built, no
|
|
429
|
+
* quantized graph, no halves or floats, lists / learned lists / votes present, an attached image). */
|
|
430
|
+
int kci_vec_search_only(KciVec *v);
|
|
431
|
+
|
|
432
|
+
/* How much of the index is plaintext in RAM, in bytes, by kind: out[0] vector codes (int8 rows, 4-bit / low-rank
|
|
433
|
+
* list codes) out[1] floats kept in RAM (0 if mmap'd or absent; or the halves) out[2] derived from the vectors
|
|
434
|
+
* (centroids, projection, codebooks, per-list bases) out[3] graph (neighbour lists, levels, tombstones) out[4]
|
|
435
|
+
* caller ids (and the id lookup table) out[5] total Allocated sizes (capacity, not only live rows): what is in
|
|
436
|
+
* memory. Searchers' scratch (the last query) is not counted. 0 / -1. */
|
|
437
|
+
int kci_vec_plain_bytes(const KciVec *v, uint64_t out[6]);
|
|
438
|
+
|
|
439
|
+
/* Introspection. */
|
|
440
|
+
/* Reconstruct a float (unit-norm) view of stored vector i into out[dim]:
|
|
441
|
+
* the exact float if keep_float was on, else the int8 row dequantized. */
|
|
442
|
+
int kci_vec_get_float(const KciVec *v, uint32_t i, float *out);
|
|
443
|
+
/* The id of the vector in row i, rows counted as kci_vec_get_float counts them (the order of kci_vec_add until a
|
|
444
|
+
* build; a build may reorder rows). UINT64_MAX when i is not a row. With deduplication a row can stand for several
|
|
445
|
+
* ids: this is the first. */
|
|
446
|
+
uint64_t kci_vec_row_id(const KciVec *v, uint32_t i);
|
|
447
|
+
|
|
448
|
+
uint32_t kci_vec_count(const KciVec *v);
|
|
449
|
+
/* How many rows are exact copies of another row? Counted on the int8 rows of a built index (graph or lists),
|
|
450
|
+
* deleted rows left out. out[0] = live rows, out[1] = groups of 2 or more identical rows, out[2] = rows in those
|
|
451
|
+
* groups, out[3] = the largest group, out[4] = groups larger than the layer-0 neighbour list (2 M; such a group
|
|
452
|
+
* cannot even link all of its own members), out[5] = distinct rows (what is left when every group is kept once),
|
|
453
|
+
* out[6], out[7] = 0 (reserved). Returns 0, or -1 (NULL, not built, no int8 rows any more: after
|
|
454
|
+
* kci_vec_search_only; out of memory). Why look: texts that repeat (boilerplate, empty fields) give identical
|
|
455
|
+
* embeddings, and a graph search spends its steps inside such groups. When out[2] is more than a few percent of
|
|
456
|
+
* out[0], put identical rows in once (keep the list of their ids yourself) or search with a larger ef. */
|
|
457
|
+
int kci_vec_dup_stats(const KciVec *v, uint64_t out[8]);
|
|
458
|
+
/* A filter sees every id. kci_vec_delete of an id that shares its vector removes the id only. kci_vec_count counts
|
|
459
|
+
* ids; kci_vec_dup_stats: out[6] = ids kept beside rows, out[7] = rows that have such ids (out[0..5] then count
|
|
460
|
+
* the rows, which hold no duplicates any more). Default 0: nothing changes for an index that does not ask. Needs
|
|
461
|
+
* the floats in memory while building (as the build does); an index with a float source or mmap'd floats is left
|
|
462
|
+
* as it is. kci_vec_save writes such an index with the ids, under a magic of its own (KCIVTW04 .. 09 for KCIVEC04
|
|
463
|
+
* .. An index loaded from such a file has dedup on. NOT YET: kci_vec_image_save refuses (-1) an index that holds
|
|
464
|
+
* such ids (the image has no place for them yet). Returns 0, or -1 (NULL, an attached image). */
|
|
465
|
+
int kci_vec_set_dedup(KciVec *v, int on);
|
|
466
|
+
int kci_vec_searcher_set_unique(KciVecSearcher *s, int on);
|
|
467
|
+
int kci_vec_dim(const KciVec *v);
|
|
468
|
+
int kci_vec_is_built(const KciVec *v);
|
|
469
|
+
/* 1 when the index holds the full float vectors (kci_vec_keep_float, and neither kci_vec_half_floats nor
|
|
470
|
+
* kci_vec_search_only since): kci_vec_search_exact then ranks by the floats. 0 otherwise (it would rank by the int8 rows). */
|
|
471
|
+
int kci_vec_has_floats(const KciVec *v);
|
|
472
|
+
/* 1 when the index holds the vectors as 16-bit halves (kci_vec_half_floats); kci_vec_search_exact then compares
|
|
473
|
+
* every row on them, and no row is missed. */
|
|
474
|
+
int kci_vec_has_halves(const KciVec *v);
|
|
475
|
+
/* 1 when kci_vec_search_exact takes its faster way on this index (floats kept, the graph built over every row,
|
|
476
|
+
* 4,096 rows or more); 0 when it scans the floats. The answer is the same either way. */
|
|
477
|
+
int kci_vec_exact_by_proof(const KciVec *v);
|
|
478
|
+
/* Extra data that makes kci_vec_search_exact faster on vectors such as sentence embeddings. The answer does not
|
|
479
|
+
* change. m: its size in bytes a row (0 = dim / 4; 8 .. dim / 2, at most 1024; m < 0 drops the data). Memory: m + 8
|
|
480
|
+
* bytes a row. out_share (may be NULL): a number between 0 and 1 that tells how well the data suits it - about 0.8
|
|
481
|
+
* and up it helps, well below it does not. Needs what kci_vec_exact_by_proof needs. Dropped when rows are added,
|
|
482
|
+
* deleted or replaced (call it again). Not while searches run. Returns 0, -1 on a bad m, no floats / no graph, or
|
|
483
|
+
* no memory. kci_vec_save writes the data into the file (m + 8 bytes a row more) and kci_vec_load reads it back.
|
|
484
|
+
* Not with KCI_VEC_SAVE_NO_FLOAT and not in an image (kci_vec_image_save). Versions before 0.2.23 load such a file
|
|
485
|
+
* without the data. */
|
|
486
|
+
int kci_vec_exact_short(KciVec *v, int m, int threads, float *out_share);
|
|
487
|
+
/* kci_vec_exact_short, kept only where it helps: when the number it reports is below min_share (0.8 is a good
|
|
488
|
+
* line) nothing is made, at a fraction of the work. Returns 0 made, 1 not made (the index has no such data then),
|
|
489
|
+
* -1 as kci_vec_exact_short. */
|
|
490
|
+
int kci_vec_exact_short_if(KciVec *v, int m, int threads, float min_share, float *out_share);
|
|
491
|
+
/* With or without misses: the caller chooses. on = 1: kci_vec_search, kci_vec_search_r, kci_vec_search_rerank and
|
|
492
|
+
* kci_vec_search_filtered return what kci_vec_search_exact (_filtered) returns - no row is missed, whatever ef /
|
|
493
|
+
* rerank_n are given (they are not used) - and take longer; the time grows with the number of rows. on = 0, as an
|
|
494
|
+
* index is created or loaded: the searches of the index. threads: for the exact answer, 0 = all cores (one caller
|
|
495
|
+
* at a time), 1 when many threads search at once; with a filter always the calling thread only. The list, learned
|
|
496
|
+
* and voted searches too (kci_vec_search_ivf, _ivf_filtered, _learned, _voted, _voted_short): with it on they give
|
|
497
|
+
* the exact answer, nprobe / per_list / voters are not used, and they answer where the index has no lists or votes
|
|
498
|
+
* (with it off they return a negative number there). Not saved. Returns 0; -1 for an index without the floats
|
|
499
|
+
* (created with keep_float 0, search-only) - its exact score cannot be given. */
|
|
500
|
+
int kci_vec_set_exact(KciVec *v, int on, int threads);
|
|
501
|
+
int kci_vec_get_exact(const KciVec *v);
|
|
502
|
+
/* The m of the data kci_vec_exact_short made, 0 when there is none. */
|
|
503
|
+
int kci_vec_exact_short_m(const KciVec *v);
|
|
504
|
+
/* Choose the search width (ef) for this index from its own vectors, instead of one default for every kind of data.
|
|
505
|
+
* `samples` stored vectors (0 = 128) are used as questions: their true neighbours by brute force, then the graph
|
|
506
|
+
* search (kci_vec_search_rerank with rerank_n) at ef 200 / 400 / 800 / 1600 / 3200. The smallest width that finds the share
|
|
507
|
+
* `target` (0 = 0.98) of the true k (0 = 10) nearest is returned and kept; when none does, the widest is kept.
|
|
508
|
+
* *out_recall (may be NULL) is the share found at the width kept. kci_vec_default_ef gives the width back (0 = never
|
|
509
|
+
* calibrated); kci_vec_save writes it and kci_vec_load restores it.
|
|
510
|
+
* When rerank_n rows are too few for the float pass at a width, the same width is tried with all its rows re-scored;
|
|
511
|
+
* kci_vec_default_rerank then returns that width (0 = your rerank_n is enough).
|
|
512
|
+
* kci_vec_search_rerank and kci_vec_search_filtered take ef <= 0 as "the calibrated width, with its float pass"; on an
|
|
513
|
+
* index that was never calibrated ef <= 0 is as wide as rerank_n.
|
|
514
|
+
* Needs the graph and the full float vectors; -1 otherwise. Call it after kci_vec_build, not while other threads
|
|
515
|
+
* search or write. It measures questions that look like the stored vectors; for questions of another kind use
|
|
516
|
+
* kci_vec_calibrate_queries. Cost: `samples` brute-force searches on `threads` threads. */
|
|
517
|
+
int kci_vec_calibrate(KciVec *v, int k, int rerank_n, float target, int samples, int threads, float *out_recall);
|
|
518
|
+
int kci_vec_default_ef(const KciVec *v);
|
|
519
|
+
/* inner product indexes: a setting made by kci_vec_build for the searches (0 = none); for information. */
|
|
520
|
+
int kci_vec_ip_long_rows(const KciVec *v);
|
|
521
|
+
int kci_vec_default_rerank(const KciVec *v);
|
|
522
|
+
/* The same with the caller's own questions (nq rows of dim floats; 8 to 4,096 of them are used) instead of stored
|
|
523
|
+
* vectors. -1 with fewer than 8.
|
|
524
|
+
* kci_vec_search_filtered with a filter and ef <= 0 gives the exact answer when the index keeps its floats or
|
|
525
|
+
* halves: no allowed row is missed (it costs a scan). */
|
|
526
|
+
int kci_vec_calibrate_queries(KciVec *v, const float *queries, int nq, int k, int rerank_n, float target, int threads, float *out_recall);
|
|
527
|
+
/* Rows the last kci_vec_build could not make reachable from the entry point (0 = every row can be found; not
|
|
528
|
+
* saved, 0 after a load). */
|
|
529
|
+
uint32_t kci_vec_unreached(const KciVec *v);
|
|
530
|
+
int kci_vec_set_default_ef(KciVec *v, int ef); /* 0..32767, 0 = forget it (kci_vec_default_rerank becomes 0 too); 0 on success */
|
|
531
|
+
/* Let the index decide the search width for each query. After kci_vec_judge, a search that leaves the width to the
|
|
532
|
+
* index (kci_vec_search_rerank with ef <= 0) can answer the queries that need little with less work; searches with an
|
|
533
|
+
* explicit ef are unchanged. The rule is prepared from `samples` stored vectors (0 = 4,096), or from the caller's own
|
|
534
|
+
* queries (kci_vec_judge_queries: 200 or more, up to 8,192 are used). k: the number of results the searches will
|
|
535
|
+
* ask for, 2..64 (a search that asks for more is not judged). target: the share of a wide search's answer to keep,
|
|
536
|
+
* 0..1 (0 = 0.99). out_gain (may be NULL): the measured ratio of work against a fixed width.
|
|
537
|
+
* Returns 1 when the rule is in use, 0 when it was measured not to help on this index (searches stay as they
|
|
538
|
+
* were), -1 on error (not built, no float vectors kept, an inner-product index, too small an index, k out of range,
|
|
539
|
+
* too few queries). For graph searches without a filter. The rule is not saved, and a build drops it: call again.
|
|
540
|
+
* Not while other threads search. kci_vec_judged: 1 when a rule is in use. kci_vec_judge_off: forget it. */
|
|
541
|
+
int kci_vec_judge(KciVec *v, int k, float target, int samples, float *out_gain);
|
|
542
|
+
int kci_vec_judge_queries(KciVec *v, const float *queries, int nq, int k, float target, float *out_gain);
|
|
543
|
+
int kci_vec_judged(const KciVec *v);
|
|
544
|
+
int kci_vec_judge_off(KciVec *v);
|
|
545
|
+
/* The graph search's recall@k at n search widths, estimated without queries of your own: `samples` stored vectors
|
|
546
|
+
* (0 = 1,000) are used as questions and compared with brute force. ef: n widths, ascending, each above k and at most
|
|
547
|
+
* the number of vectors; the shortlist is as long as the width. out_recall: n values.
|
|
548
|
+
* Returns the number of samples used, or -1 (not built, no float vectors kept, an inner-product index, k outside
|
|
549
|
+
* 1..64, widths not ascending, n above 64). */
|
|
550
|
+
int kci_vec_graph_recall(KciVec *v, int k, int samples, const int *ef, int n, int threads, float *out_recall);
|
|
551
|
+
|
|
552
|
+
/* The k nearest neighbours of every stored id (a kNN graph), for clustering, UMAP / t-SNE and the like: each stored
|
|
553
|
+
* vector is asked as a question and left out of its own answer.
|
|
554
|
+
* out_self[cap], out_ids[cap * k], out_scores[cap * k] (may be NULL), cap >= kci_vec_count(v). Line i is the id
|
|
555
|
+
* out_self[i], its neighbours out_ids[i*k .. i*k+k-1] best first, their scores as kci_vec_search gives them (cosine,
|
|
556
|
+
* |q-x|^2 for L2, q.x for inner product). Lines come in row order (kci_vec_row_id); deleted ids have no line; with
|
|
557
|
+
* kci_vec_set_dedup every id has its own line, and the ids that share its vector are its first neighbours. Where an
|
|
558
|
+
* id has fewer than k neighbours (k >= the number of ids) the rest of its line is UINT64_MAX with the score -inf
|
|
559
|
+
* (+inf for L2).
|
|
560
|
+
* ef: > 0 the width of the graph search (at least 2(k+1) is used); 0 = 3200, or the calibrated width
|
|
561
|
+
* (kci_vec_default_ef) when that is wider; with the floats kept every row of the width is scored on them. The graph
|
|
562
|
+
* can miss neighbours, fewer the wider it is. ef < 0 = the exact search for every id: no neighbour is missed, the
|
|
563
|
+
* time grows with the square of the number of vectors. threads: 0 = all cores.
|
|
564
|
+
* Needs the graph (kci_vec_build) unless ef < 0. Nothing else may change the index during the call.
|
|
565
|
+
* Returns the number of lines written, 0 for an empty index, -1 (bad arguments, k above 16383, a search-only index,
|
|
566
|
+
* no graph with ef >= 0, out of memory). */
|
|
567
|
+
int64_t kci_vec_knn_graph(KciVec *v, int k, int ef, int threads, uint64_t *out_self, uint64_t *out_ids, float *out_scores);
|
|
568
|
+
|
|
569
|
+
#ifdef __cplusplus
|
|
570
|
+
}
|
|
571
|
+
#endif
|
|
572
|
+
|
|
573
|
+
#endif /* KCI_VEC_H */
|