@memberjunction/ai-vectors-memory 6.2.0-edge.1 → 6.2.0-edge.3
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/README.md +73 -12
- package/dist/index.d.ts +6 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -1
- package/dist/models/MetadataFilterEvaluator.d.ts +37 -0
- package/dist/models/MetadataFilterEvaluator.d.ts.map +1 -0
- package/dist/models/MetadataFilterEvaluator.js +233 -0
- package/dist/models/MetadataFilterEvaluator.js.map +1 -0
- package/dist/models/SimpleVectorDatabase.d.ts +55 -11
- package/dist/models/SimpleVectorDatabase.d.ts.map +1 -1
- package/dist/models/SimpleVectorDatabase.js +148 -35
- package/dist/models/SimpleVectorDatabase.js.map +1 -1
- package/dist/models/SimpleVectorService.d.ts +164 -12
- package/dist/models/SimpleVectorService.d.ts.map +1 -1
- package/dist/models/SimpleVectorService.js +612 -286
- package/dist/models/SimpleVectorService.js.map +1 -1
- package/dist/models/SimpleVectorServiceProvider.d.ts +151 -35
- package/dist/models/SimpleVectorServiceProvider.d.ts.map +1 -1
- package/dist/models/SimpleVectorServiceProvider.js +429 -106
- package/dist/models/SimpleVectorServiceProvider.js.map +1 -1
- package/dist/models/StoredVector.d.ts +35 -0
- package/dist/models/StoredVector.d.ts.map +1 -0
- package/dist/models/StoredVector.js +83 -0
- package/dist/models/StoredVector.js.map +1 -0
- package/dist/models/VectorAccelerator.d.ts +109 -0
- package/dist/models/VectorAccelerator.d.ts.map +1 -0
- package/dist/models/VectorAccelerator.js +97 -0
- package/dist/models/VectorAccelerator.js.map +1 -0
- package/dist/models/VectorKernels.d.ts +117 -0
- package/dist/models/VectorKernels.d.ts.map +1 -0
- package/dist/models/VectorKernels.js +338 -0
- package/dist/models/VectorKernels.js.map +1 -0
- package/dist/models/VectorStore.d.ts +157 -0
- package/dist/models/VectorStore.d.ts.map +1 -0
- package/dist/models/VectorStore.js +376 -0
- package/dist/models/VectorStore.js.map +1 -0
- package/package.json +4 -4
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Numeric kernels behind {@link SimpleVectorService}.
|
|
3
|
+
*
|
|
4
|
+
* Every similarity formula the service exposes lives here exactly once, as a
|
|
5
|
+
* pure function over array-likes with explicit offsets. That lets the same code
|
|
6
|
+
* score a `number[]` passed to the public `CalculateDistance`, a row inside the
|
|
7
|
+
* service's packed `Float32Array`/`Float64Array` store, and a row inside a
|
|
8
|
+
* `SharedArrayBuffer` read by a worker thread.
|
|
9
|
+
*
|
|
10
|
+
* The module has no imports on purpose: it runs unchanged in browsers, in Node,
|
|
11
|
+
* and inside `worker_threads`.
|
|
12
|
+
*
|
|
13
|
+
* **Bit-for-bit compatibility.** Each loop accumulates its terms in the same
|
|
14
|
+
* order as the original per-pair implementations, so a float64 store produces
|
|
15
|
+
* the exact same scores the service always has. Do not unroll or re-associate
|
|
16
|
+
* these loops without accepting that scores move in the last bits.
|
|
17
|
+
*
|
|
18
|
+
* @module @memberjunction/ai-vectors-memory
|
|
19
|
+
*/
|
|
20
|
+
/**
|
|
21
|
+
* Sum of squares of `dims` elements starting at `offset`.
|
|
22
|
+
*/
|
|
23
|
+
export function SumOfSquares(v, offset, dims) {
|
|
24
|
+
let sum = 0;
|
|
25
|
+
for (let i = 0; i < dims; i++) {
|
|
26
|
+
const x = v[offset + i];
|
|
27
|
+
sum += x * x;
|
|
28
|
+
}
|
|
29
|
+
return sum;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Raw cosine similarity in [-1, 1] from precomputed sums of squares.
|
|
33
|
+
* Returns 0 when either vector is all zeros (a zero vector has no direction).
|
|
34
|
+
*/
|
|
35
|
+
export function CosineFromNorms(a, aOffset, aNormSq, b, bOffset, bNormSq, dims) {
|
|
36
|
+
let dot = 0;
|
|
37
|
+
for (let i = 0; i < dims; i++) {
|
|
38
|
+
dot += a[aOffset + i] * b[bOffset + i];
|
|
39
|
+
}
|
|
40
|
+
if (aNormSq === 0 || bNormSq === 0) {
|
|
41
|
+
return 0;
|
|
42
|
+
}
|
|
43
|
+
return dot / (Math.sqrt(aNormSq) * Math.sqrt(bNormSq));
|
|
44
|
+
}
|
|
45
|
+
/** Euclidean distance mapped to a 0-1 similarity: `1 / (1 + distance)`. */
|
|
46
|
+
export function EuclideanScore(a, aOffset, b, bOffset, dims) {
|
|
47
|
+
let sumSquaredDiff = 0;
|
|
48
|
+
for (let i = 0; i < dims; i++) {
|
|
49
|
+
const diff = a[aOffset + i] - b[bOffset + i];
|
|
50
|
+
sumSquaredDiff += diff * diff;
|
|
51
|
+
}
|
|
52
|
+
return 1 / (1 + Math.sqrt(sumSquaredDiff));
|
|
53
|
+
}
|
|
54
|
+
/** Manhattan (L1) distance mapped to a 0-1 similarity: `1 / (1 + distance)`. */
|
|
55
|
+
export function ManhattanScore(a, aOffset, b, bOffset, dims) {
|
|
56
|
+
let sumAbsDiff = 0;
|
|
57
|
+
for (let i = 0; i < dims; i++) {
|
|
58
|
+
sumAbsDiff += Math.abs(a[aOffset + i] - b[bOffset + i]);
|
|
59
|
+
}
|
|
60
|
+
return 1 / (1 + sumAbsDiff);
|
|
61
|
+
}
|
|
62
|
+
/** Dot product squashed to 0-1 with `(tanh(dot / sqrt(dims)) + 1) / 2`. */
|
|
63
|
+
export function DotProductScore(a, aOffset, b, bOffset, dims) {
|
|
64
|
+
let dotProduct = 0;
|
|
65
|
+
for (let i = 0; i < dims; i++) {
|
|
66
|
+
dotProduct += a[aOffset + i] * b[bOffset + i];
|
|
67
|
+
}
|
|
68
|
+
const normalized = Math.tanh(dotProduct / Math.sqrt(dims));
|
|
69
|
+
return (normalized + 1) / 2;
|
|
70
|
+
}
|
|
71
|
+
/** Jaccard similarity treating non-zero elements as set members. Two empty sets score 1. */
|
|
72
|
+
export function JaccardScore(a, aOffset, b, bOffset, dims) {
|
|
73
|
+
let intersection = 0;
|
|
74
|
+
let union = 0;
|
|
75
|
+
for (let i = 0; i < dims; i++) {
|
|
76
|
+
const aPresent = a[aOffset + i] !== 0;
|
|
77
|
+
const bPresent = b[bOffset + i] !== 0;
|
|
78
|
+
if (aPresent && bPresent) {
|
|
79
|
+
intersection++;
|
|
80
|
+
}
|
|
81
|
+
if (aPresent || bPresent) {
|
|
82
|
+
union++;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return union === 0 ? 1 : intersection / union;
|
|
86
|
+
}
|
|
87
|
+
/** Hamming distance mapped to a 0-1 similarity: `1 - differences / dims`. */
|
|
88
|
+
export function HammingScore(a, aOffset, b, bOffset, dims) {
|
|
89
|
+
let differences = 0;
|
|
90
|
+
for (let i = 0; i < dims; i++) {
|
|
91
|
+
if (a[aOffset + i] !== b[bOffset + i]) {
|
|
92
|
+
differences++;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return 1 - (differences / dims);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* The normalized 0-1 similarity for any metric — identical to
|
|
99
|
+
* `SimpleVectorService.CalculateDistance`. Cosine maps [-1, 1] onto [0, 1].
|
|
100
|
+
* The sums of squares are read only by cosine; pass 0 for other metrics.
|
|
101
|
+
*
|
|
102
|
+
* @throws {Error} For an unknown metric
|
|
103
|
+
*/
|
|
104
|
+
export function MetricScore(metric, a, aOffset, aNormSq, b, bOffset, bNormSq, dims) {
|
|
105
|
+
switch (metric) {
|
|
106
|
+
case 'cosine':
|
|
107
|
+
return (CosineFromNorms(a, aOffset, aNormSq, b, bOffset, bNormSq, dims) + 1) / 2;
|
|
108
|
+
case 'euclidean':
|
|
109
|
+
return EuclideanScore(a, aOffset, b, bOffset, dims);
|
|
110
|
+
case 'manhattan':
|
|
111
|
+
return ManhattanScore(a, aOffset, b, bOffset, dims);
|
|
112
|
+
case 'dotproduct':
|
|
113
|
+
return DotProductScore(a, aOffset, b, bOffset, dims);
|
|
114
|
+
case 'jaccard':
|
|
115
|
+
return JaccardScore(a, aOffset, b, bOffset, dims);
|
|
116
|
+
case 'hamming':
|
|
117
|
+
return HammingScore(a, aOffset, b, bOffset, dims);
|
|
118
|
+
default:
|
|
119
|
+
throw new Error(`Unknown distance metric: ${metric}`);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
/** True when the metric is one the kernels implement. */
|
|
123
|
+
export function IsKnownMetric(metric) {
|
|
124
|
+
return metric === 'cosine' || metric === 'euclidean' || metric === 'manhattan'
|
|
125
|
+
|| metric === 'dotproduct' || metric === 'jaccard' || metric === 'hamming';
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Scores rows of a packed store against a query and ranks them.
|
|
129
|
+
*
|
|
130
|
+
* With a numeric `TopK` this keeps a bounded, stable insertion buffer, so the
|
|
131
|
+
* cost is O(rows × dims) with no full sort. With `TopK: null` it returns every
|
|
132
|
+
* row that passes the threshold, fully sorted. NaN scores (only possible from
|
|
133
|
+
* NaN or infinite vector values) are never returned.
|
|
134
|
+
*
|
|
135
|
+
* @throws {Error} For an unknown metric
|
|
136
|
+
*/
|
|
137
|
+
export function SearchRows(view, spec) {
|
|
138
|
+
if (!IsKnownMetric(spec.Metric)) {
|
|
139
|
+
throw new Error(`Unknown distance metric: ${spec.Metric}`);
|
|
140
|
+
}
|
|
141
|
+
const collector = spec.TopK === null ? new AllRowsCollector() : new TopRowsCollector(spec.TopK);
|
|
142
|
+
if (view.Data instanceof Float32Array) {
|
|
143
|
+
scanFloat32Rows(view.Data, view, spec, collector);
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
scanFloat64Rows(view.Data, view, spec, collector);
|
|
147
|
+
}
|
|
148
|
+
return collector.Result();
|
|
149
|
+
}
|
|
150
|
+
// ── Row scans ─────────────────────────────────────────────────────────────
|
|
151
|
+
//
|
|
152
|
+
// scanFloat32Rows and scanFloat64Rows are deliberately identical apart from
|
|
153
|
+
// the element type of `data`. V8 specialises a function to the array types it
|
|
154
|
+
// has seen; one scan shared by both precisions (and by the `number[]` metric
|
|
155
|
+
// methods) turns polymorphic and measured ~2.5x slower once a process holds
|
|
156
|
+
// stores of both precisions. Two copies keep each one monomorphic. Cosine,
|
|
157
|
+
// euclidean and dot product — the metrics used for embeddings — are inlined;
|
|
158
|
+
// the set/categorical metrics go through MetricScore. Keep the two in step.
|
|
159
|
+
function scanFloat32Rows(data, view, spec, collector) {
|
|
160
|
+
const dims = view.Dims;
|
|
161
|
+
const norms = view.Norms;
|
|
162
|
+
const live = view.Live;
|
|
163
|
+
const query = spec.Query;
|
|
164
|
+
const queryNormSq = spec.QueryNormSq;
|
|
165
|
+
const candidates = spec.Candidates;
|
|
166
|
+
const threshold = spec.Threshold;
|
|
167
|
+
const metric = spec.Metric;
|
|
168
|
+
const start = candidates ? 0 : (spec.RowStart ?? 0);
|
|
169
|
+
const end = candidates ? candidates.length : Math.min(spec.RowEnd ?? view.RowCount, view.RowCount);
|
|
170
|
+
for (let i = start; i < end; i++) {
|
|
171
|
+
const row = candidates ? candidates[i] : i;
|
|
172
|
+
if (live[row] !== 1)
|
|
173
|
+
continue;
|
|
174
|
+
const offset = row * dims;
|
|
175
|
+
let score;
|
|
176
|
+
if (metric === 'cosine') {
|
|
177
|
+
let dot = 0;
|
|
178
|
+
for (let d = 0; d < dims; d++)
|
|
179
|
+
dot += query[d] * data[offset + d];
|
|
180
|
+
const rowNormSq = norms[row];
|
|
181
|
+
const cosine = queryNormSq === 0 || rowNormSq === 0 ? 0 : dot / (Math.sqrt(queryNormSq) * Math.sqrt(rowNormSq));
|
|
182
|
+
score = (cosine + 1) / 2;
|
|
183
|
+
}
|
|
184
|
+
else if (metric === 'euclidean') {
|
|
185
|
+
let sumSquaredDiff = 0;
|
|
186
|
+
for (let d = 0; d < dims; d++) {
|
|
187
|
+
const diff = query[d] - data[offset + d];
|
|
188
|
+
sumSquaredDiff += diff * diff;
|
|
189
|
+
}
|
|
190
|
+
score = 1 / (1 + Math.sqrt(sumSquaredDiff));
|
|
191
|
+
}
|
|
192
|
+
else if (metric === 'dotproduct') {
|
|
193
|
+
let dot = 0;
|
|
194
|
+
for (let d = 0; d < dims; d++)
|
|
195
|
+
dot += query[d] * data[offset + d];
|
|
196
|
+
score = (Math.tanh(dot / Math.sqrt(dims)) + 1) / 2;
|
|
197
|
+
}
|
|
198
|
+
else {
|
|
199
|
+
score = MetricScore(metric, query, 0, queryNormSq, data, offset, norms[row], dims);
|
|
200
|
+
}
|
|
201
|
+
if (score !== score)
|
|
202
|
+
continue; // NaN
|
|
203
|
+
if (threshold !== null && !(score >= threshold))
|
|
204
|
+
continue;
|
|
205
|
+
collector.Offer(row, score);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
function scanFloat64Rows(data, view, spec, collector) {
|
|
209
|
+
const dims = view.Dims;
|
|
210
|
+
const norms = view.Norms;
|
|
211
|
+
const live = view.Live;
|
|
212
|
+
const query = spec.Query;
|
|
213
|
+
const queryNormSq = spec.QueryNormSq;
|
|
214
|
+
const candidates = spec.Candidates;
|
|
215
|
+
const threshold = spec.Threshold;
|
|
216
|
+
const metric = spec.Metric;
|
|
217
|
+
const start = candidates ? 0 : (spec.RowStart ?? 0);
|
|
218
|
+
const end = candidates ? candidates.length : Math.min(spec.RowEnd ?? view.RowCount, view.RowCount);
|
|
219
|
+
for (let i = start; i < end; i++) {
|
|
220
|
+
const row = candidates ? candidates[i] : i;
|
|
221
|
+
if (live[row] !== 1)
|
|
222
|
+
continue;
|
|
223
|
+
const offset = row * dims;
|
|
224
|
+
let score;
|
|
225
|
+
if (metric === 'cosine') {
|
|
226
|
+
let dot = 0;
|
|
227
|
+
for (let d = 0; d < dims; d++)
|
|
228
|
+
dot += query[d] * data[offset + d];
|
|
229
|
+
const rowNormSq = norms[row];
|
|
230
|
+
const cosine = queryNormSq === 0 || rowNormSq === 0 ? 0 : dot / (Math.sqrt(queryNormSq) * Math.sqrt(rowNormSq));
|
|
231
|
+
score = (cosine + 1) / 2;
|
|
232
|
+
}
|
|
233
|
+
else if (metric === 'euclidean') {
|
|
234
|
+
let sumSquaredDiff = 0;
|
|
235
|
+
for (let d = 0; d < dims; d++) {
|
|
236
|
+
const diff = query[d] - data[offset + d];
|
|
237
|
+
sumSquaredDiff += diff * diff;
|
|
238
|
+
}
|
|
239
|
+
score = 1 / (1 + Math.sqrt(sumSquaredDiff));
|
|
240
|
+
}
|
|
241
|
+
else if (metric === 'dotproduct') {
|
|
242
|
+
let dot = 0;
|
|
243
|
+
for (let d = 0; d < dims; d++)
|
|
244
|
+
dot += query[d] * data[offset + d];
|
|
245
|
+
score = (Math.tanh(dot / Math.sqrt(dims)) + 1) / 2;
|
|
246
|
+
}
|
|
247
|
+
else {
|
|
248
|
+
score = MetricScore(metric, query, 0, queryNormSq, data, offset, norms[row], dims);
|
|
249
|
+
}
|
|
250
|
+
if (score !== score)
|
|
251
|
+
continue; // NaN
|
|
252
|
+
if (threshold !== null && !(score >= threshold))
|
|
253
|
+
continue;
|
|
254
|
+
collector.Offer(row, score);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Keeps the best `k` rows seen so far, highest score first. A new row only
|
|
259
|
+
* displaces an existing one with a strictly lower score, so among equal
|
|
260
|
+
* scores the earliest row wins — the same result a stable sort would give.
|
|
261
|
+
*/
|
|
262
|
+
class TopRowsCollector {
|
|
263
|
+
constructor(k) {
|
|
264
|
+
this.k = k;
|
|
265
|
+
this.count = 0;
|
|
266
|
+
const size = Math.max(0, Math.trunc(k));
|
|
267
|
+
this.rows = new Int32Array(size);
|
|
268
|
+
this.scores = new Float64Array(size);
|
|
269
|
+
}
|
|
270
|
+
Offer(row, score) {
|
|
271
|
+
const size = this.rows.length;
|
|
272
|
+
if (size === 0)
|
|
273
|
+
return;
|
|
274
|
+
let position;
|
|
275
|
+
if (this.count < size) {
|
|
276
|
+
position = this.count++;
|
|
277
|
+
}
|
|
278
|
+
else if (score > this.scores[size - 1]) {
|
|
279
|
+
position = size - 1;
|
|
280
|
+
}
|
|
281
|
+
else {
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
while (position > 0 && this.scores[position - 1] < score) {
|
|
285
|
+
this.scores[position] = this.scores[position - 1];
|
|
286
|
+
this.rows[position] = this.rows[position - 1];
|
|
287
|
+
position--;
|
|
288
|
+
}
|
|
289
|
+
this.scores[position] = score;
|
|
290
|
+
this.rows[position] = row;
|
|
291
|
+
}
|
|
292
|
+
Result() {
|
|
293
|
+
return { Rows: this.rows.slice(0, this.count), Scores: this.scores.slice(0, this.count) };
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
/** Collects every offered row, then sorts by score descending, row ascending. */
|
|
297
|
+
class AllRowsCollector {
|
|
298
|
+
constructor() {
|
|
299
|
+
this.rows = [];
|
|
300
|
+
this.scores = [];
|
|
301
|
+
}
|
|
302
|
+
Offer(row, score) {
|
|
303
|
+
this.rows.push(row);
|
|
304
|
+
this.scores.push(score);
|
|
305
|
+
}
|
|
306
|
+
Result() {
|
|
307
|
+
const order = this.rows.map((_, i) => i);
|
|
308
|
+
order.sort((a, b) => (this.scores[b] - this.scores[a]) || (this.rows[a] - this.rows[b]));
|
|
309
|
+
const rows = new Int32Array(order.length);
|
|
310
|
+
const scores = new Float64Array(order.length);
|
|
311
|
+
for (let i = 0; i < order.length; i++) {
|
|
312
|
+
rows[i] = this.rows[order[i]];
|
|
313
|
+
scores[i] = this.scores[order[i]];
|
|
314
|
+
}
|
|
315
|
+
return { Rows: rows, Scores: scores };
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Merges several ranked lists (for example, one per worker partition) into
|
|
320
|
+
* one ranked list, keeping at most `topK` rows (`null` keeps all). Ties are
|
|
321
|
+
* broken by ascending row so partitioning never changes the result.
|
|
322
|
+
*/
|
|
323
|
+
export function MergeScoredRows(parts, topK) {
|
|
324
|
+
const collector = topK === null ? new AllRowsCollector() : new TopRowsCollector(topK);
|
|
325
|
+
const merged = [];
|
|
326
|
+
for (const part of parts) {
|
|
327
|
+
for (let i = 0; i < part.Rows.length; i++) {
|
|
328
|
+
merged.push({ row: part.Rows[i], score: part.Scores[i] });
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
// Offer in ascending row order so the collector's tie-breaking stays row-ordered.
|
|
332
|
+
merged.sort((a, b) => a.row - b.row);
|
|
333
|
+
for (const m of merged) {
|
|
334
|
+
collector.Offer(m.row, m.score);
|
|
335
|
+
}
|
|
336
|
+
return collector.Result();
|
|
337
|
+
}
|
|
338
|
+
//# sourceMappingURL=VectorKernels.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"VectorKernels.js","sourceRoot":"","sources":["../../src/models/VectorKernels.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AA2DH;;GAEG;AACH,MAAM,UAAU,YAAY,CAAC,CAAgB,EAAE,MAAc,EAAE,IAAY;IACzE,IAAI,GAAG,GAAG,CAAC,CAAC;IACZ,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,EAAE,CAAC,EAAE,EAAE,CAAC;QAC9B,MAAM,CAAC,GAAG,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;QACxB,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC;IACf,CAAC;IACD,OAAO,GAAG,CAAC;AACb,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,eAAe,CAC7B,CAAgB,EAAE,OAAe,EAAE,OAAe,EAClD,CAAgB,EAAE,OAAe,EAAE,OAAe,EAClD,IAAY;IAEZ,IAAI,GAAG,GAAG,CAAC,CAAC;IACZ,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,EAAE,CAAC,EAAE,EAAE,CAAC;QAC9B,GAAG,IAAI,CAAC,CAAC,OAAO,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,OAAO,GAAG,CAAC,CAAC,CAAC;IACzC,CAAC;IACD,IAAI,OAAO,KAAK,CAAC,IAA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,KAAK,IAAI,CAAC,CAAC,CAAC,IAAI,gBAAgB,EAAE,CAAC,CAAC,CAAC,IAAI,gBAAgB,CAAC,IAAI,CAAC,CAAC;IACrG,MAAM,MAAM,GAA0C,EAAE,CAAC;IACzD,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACzB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YAC1C,MAAM,CAAC,IAAI,CAAC,EAAE,GAAG,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;QAC5D,CAAC;IACH,CAAC;IACD,kFAAkF;IAClF,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC;IACrC,KAAK,MAAM,CAAC,IAAI,MAAM,EAAE,CAAC;QACvB,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC;IAClC,CAAC;IACD,OAAO,SAAS,CAAC,MAAM,EAAE,CAAC;AAC5B,CAAC"}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Packed, row-oriented storage behind {@link SimpleVectorService}.
|
|
3
|
+
*
|
|
4
|
+
* Vectors live in one contiguous typed array instead of one `number[]` per
|
|
5
|
+
* key, with each row's sum of squares cached alongside. That halves memory at
|
|
6
|
+
* float32, removes per-search allocation, and lets a worker thread read the
|
|
7
|
+
* same memory when the buffers are `SharedArrayBuffer`s.
|
|
8
|
+
*
|
|
9
|
+
* Rows are kept in insertion order — the order a `Map` iterates — so ranking
|
|
10
|
+
* ties resolve exactly as they did when the service stored a `Map`:
|
|
11
|
+
* - overwriting an existing key writes its row in place (keeps its position)
|
|
12
|
+
* - removing a key leaves a tombstone; the next add appends at the end
|
|
13
|
+
* - tombstones are reclaimed by an order-preserving compaction
|
|
14
|
+
*
|
|
15
|
+
* **Concurrency contract for readers on other threads.** Growth and compaction
|
|
16
|
+
* allocate NEW buffers and a NEW key array, so a snapshot taken earlier keeps
|
|
17
|
+
* a consistent row→key mapping. An in-place overwrite can be observed half
|
|
18
|
+
* written by a concurrent reader; callers that read from another thread must
|
|
19
|
+
* re-score their final rows against the live store (the service does).
|
|
20
|
+
*
|
|
21
|
+
* @module @memberjunction/ai-vectors-memory
|
|
22
|
+
*/
|
|
23
|
+
import { VectorArray, VectorRowsView } from './VectorKernels.js';
|
|
24
|
+
/** Element precision of a packed store. */
|
|
25
|
+
export type VectorPrecision = 'float64' | 'float32';
|
|
26
|
+
/** Allocates the memory behind a store. Returning a `SharedArrayBuffer` makes the store readable by workers. */
|
|
27
|
+
export type VectorBufferAllocator = (byteLength: number) => ArrayBufferLike;
|
|
28
|
+
/** Everything a reader on another thread needs to score a store, plus version stamps. */
|
|
29
|
+
export interface VectorStoreSnapshot extends VectorRowsView {
|
|
30
|
+
/** Process-unique identity of the store, stable for its lifetime */
|
|
31
|
+
StoreID: number;
|
|
32
|
+
/** Live rows (RowCount minus tombstones) */
|
|
33
|
+
LiveCount: number;
|
|
34
|
+
Precision: VectorPrecision;
|
|
35
|
+
/** Changes when rows are renumbered (compaction, clear) */
|
|
36
|
+
Generation: number;
|
|
37
|
+
/** Changes on every mutation */
|
|
38
|
+
Version: number;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Packed row store keyed by string. Not thread-safe for writers; see the
|
|
42
|
+
* file overview for the reader contract.
|
|
43
|
+
*/
|
|
44
|
+
export declare class VectorStore<TMetadata> {
|
|
45
|
+
private readonly precision;
|
|
46
|
+
private readonly allocate;
|
|
47
|
+
private readOnly;
|
|
48
|
+
private readonly storeID;
|
|
49
|
+
private dims;
|
|
50
|
+
private capacity;
|
|
51
|
+
private rowCount;
|
|
52
|
+
private liveCount;
|
|
53
|
+
private zeroNormRows;
|
|
54
|
+
private generation;
|
|
55
|
+
private version;
|
|
56
|
+
private data;
|
|
57
|
+
private norms;
|
|
58
|
+
private live;
|
|
59
|
+
private keys;
|
|
60
|
+
private metadata;
|
|
61
|
+
private rowByKey;
|
|
62
|
+
/** Rows written or removed, oldest first, with the version each change produced. */
|
|
63
|
+
private changedRows;
|
|
64
|
+
private changedVersions;
|
|
65
|
+
/** Changes at or before this version are no longer in the log. */
|
|
66
|
+
private changeLogFloor;
|
|
67
|
+
constructor(precision?: VectorPrecision, allocate?: VectorBufferAllocator);
|
|
68
|
+
/**
|
|
69
|
+
* Wraps an existing view (for example, shared memory received by a worker)
|
|
70
|
+
* as a read-only store. Each live row in `candidates` (or every live row)
|
|
71
|
+
* gets the synthetic key `String(row)`, so results computed on the
|
|
72
|
+
* adopted store map straight back to source rows.
|
|
73
|
+
*/
|
|
74
|
+
static Adopt<T>(view: VectorRowsView, precision: VectorPrecision, candidates: Int32Array | null): VectorStore<T>;
|
|
75
|
+
get Precision(): VectorPrecision;
|
|
76
|
+
/** Dimensions per row, or 0 before the first vector is written. */
|
|
77
|
+
get Dims(): number;
|
|
78
|
+
/** Live rows. */
|
|
79
|
+
get Size(): number;
|
|
80
|
+
/** Rows written, including tombstones. */
|
|
81
|
+
get RowCount(): number;
|
|
82
|
+
/** Live rows whose vector is all zeros. */
|
|
83
|
+
get ZeroNormRows(): number;
|
|
84
|
+
/** Process-unique identity of this store. */
|
|
85
|
+
get StoreID(): number;
|
|
86
|
+
get Generation(): number;
|
|
87
|
+
get Version(): number;
|
|
88
|
+
/** True when the row buffers are shared with other threads. */
|
|
89
|
+
get IsShared(): boolean;
|
|
90
|
+
/**
|
|
91
|
+
* The current row→key array. It is replaced (never reordered) on
|
|
92
|
+
* compaction, so a captured reference stays valid for the rows it saw.
|
|
93
|
+
*/
|
|
94
|
+
get Keys(): ReadonlyArray<string | null>;
|
|
95
|
+
Has(key: string): boolean;
|
|
96
|
+
RowOf(key: string): number | undefined;
|
|
97
|
+
KeyAt(row: number): string | null;
|
|
98
|
+
MetadataAt(row: number): TMetadata | undefined;
|
|
99
|
+
NormAt(row: number): number;
|
|
100
|
+
IsLive(row: number): boolean;
|
|
101
|
+
/** The live view used by kernels on this thread. */
|
|
102
|
+
View(): VectorRowsView;
|
|
103
|
+
/** A view plus version stamps, for readers on other threads. */
|
|
104
|
+
Snapshot(): VectorStoreSnapshot;
|
|
105
|
+
/**
|
|
106
|
+
* Rows written or removed after `version`, oldest first (a row can repeat).
|
|
107
|
+
* Lets a derived index — a native ANN graph, say — follow the store
|
|
108
|
+
* incrementally. Returns null when it cannot answer: the change log no
|
|
109
|
+
* longer reaches back that far, or rows were renumbered since (generation
|
|
110
|
+
* changed), so the caller must rebuild from scratch.
|
|
111
|
+
*/
|
|
112
|
+
ChangedRowsSince(version: number, generation: number): Int32Array | null;
|
|
113
|
+
/** Live rows in insertion order. */
|
|
114
|
+
LiveRows(): Int32Array;
|
|
115
|
+
/** A row's values as a plain array (a copy). */
|
|
116
|
+
ReadRow(row: number): number[];
|
|
117
|
+
/** A row's values as a typed array in the store's precision (a copy). */
|
|
118
|
+
CopyRow(row: number): VectorArray;
|
|
119
|
+
/**
|
|
120
|
+
* Copies a vector into a Float64Array with each value rounded to the store's
|
|
121
|
+
* precision, so it compares exactly like a stored row (equality-based
|
|
122
|
+
* metrics such as hamming depend on that).
|
|
123
|
+
*/
|
|
124
|
+
ToStorePrecision(vector: ArrayLike<number>): Float64Array;
|
|
125
|
+
/** Reserves room for `additionalRows` more rows so a bulk load allocates once. */
|
|
126
|
+
Reserve(additionalRows: number, dims: number): void;
|
|
127
|
+
/**
|
|
128
|
+
* Writes a vector for `key`, overwriting the existing row in place or
|
|
129
|
+
* appending a new one. Metadata is left to {@link SetMetadata}. The caller
|
|
130
|
+
* is responsible for dimension validation.
|
|
131
|
+
*
|
|
132
|
+
* @returns The row the vector was written to
|
|
133
|
+
*/
|
|
134
|
+
Write(key: string, vector: ArrayLike<number>): number;
|
|
135
|
+
SetMetadata(row: number, metadata: TMetadata | undefined): void;
|
|
136
|
+
/** Removes a key, leaving a tombstone. Compacts when tombstones pile up. */
|
|
137
|
+
Remove(key: string): boolean;
|
|
138
|
+
/** Removes every row. The dimension lock is kept, as the service always has. */
|
|
139
|
+
Clear(): void;
|
|
140
|
+
private recordChange;
|
|
141
|
+
/** Renumbering rows invalidates the change log; readers must rebuild. */
|
|
142
|
+
private resetChangeLog;
|
|
143
|
+
private writeValues;
|
|
144
|
+
private ensureCapacity;
|
|
145
|
+
/** Copies the first `rowsToCopy` rows into freshly allocated buffers of `capacity` rows. */
|
|
146
|
+
private reallocate;
|
|
147
|
+
private allocateData;
|
|
148
|
+
private compactIfWorthwhile;
|
|
149
|
+
/** Order-preserving compaction into new buffers and a new key array. */
|
|
150
|
+
private compact;
|
|
151
|
+
private adoptView;
|
|
152
|
+
private liveRowsOfView;
|
|
153
|
+
/** A private live mask limited to `rows`, so the shared mask is never written. */
|
|
154
|
+
private maskLive;
|
|
155
|
+
private assertWritable;
|
|
156
|
+
}
|
|
157
|
+
//# sourceMappingURL=VectorStore.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"VectorStore.d.ts","sourceRoot":"","sources":["../../src/models/VectorStore.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,OAAO,EAAgB,WAAW,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAE5E,2CAA2C;AAC3C,MAAM,MAAM,eAAe,GAAG,SAAS,GAAG,SAAS,CAAC;AAEpD,gHAAgH;AAChH,MAAM,MAAM,qBAAqB,GAAG,CAAC,UAAU,EAAE,MAAM,KAAK,eAAe,CAAC;AAE5E,yFAAyF;AACzF,MAAM,WAAW,mBAAoB,SAAQ,cAAc;IACzD,oEAAoE;IACpE,OAAO,EAAE,MAAM,CAAC;IAChB,4CAA4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