@wiscale/velesdb-wasm 0.5.2 → 0.6.0
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 +33 -3
- package/package.json +1 -1
- package/velesdb_wasm.d.ts +45 -5
- package/velesdb_wasm.js +92 -9
- package/velesdb_wasm_bg.wasm +0 -0
package/README.md
CHANGED
|
@@ -9,7 +9,8 @@ WebAssembly build of [VelesDB](https://github.com/cyberlife-coder/VelesDB) - vec
|
|
|
9
9
|
|
|
10
10
|
- **In-browser vector search** - No server required
|
|
11
11
|
- **SIMD optimized** - Uses WASM SIMD128 for fast distance calculations
|
|
12
|
-
- **Multiple metrics** - Cosine, Euclidean, Dot Product
|
|
12
|
+
- **Multiple metrics** - Cosine, Euclidean, Dot Product, Hamming, Jaccard
|
|
13
|
+
- **Memory optimization** - SQ8 (4x) and Binary (32x) quantization
|
|
13
14
|
- **Lightweight** - Minimal bundle size
|
|
14
15
|
|
|
15
16
|
## Installation
|
|
@@ -72,7 +73,10 @@ store.reserve(50000);
|
|
|
72
73
|
```typescript
|
|
73
74
|
class VectorStore {
|
|
74
75
|
// Create a new store
|
|
75
|
-
constructor(dimension: number, metric: 'cosine' | 'euclidean' | 'dot');
|
|
76
|
+
constructor(dimension: number, metric: 'cosine' | 'euclidean' | 'dot' | 'hamming' | 'jaccard');
|
|
77
|
+
|
|
78
|
+
// Create with storage mode (sq8/binary for memory optimization)
|
|
79
|
+
static new_with_mode(dimension: number, metric: string, mode: 'full' | 'sq8' | 'binary'): VectorStore;
|
|
76
80
|
|
|
77
81
|
// Create with pre-allocated capacity (performance optimization)
|
|
78
82
|
static with_capacity(dimension: number, metric: string, capacity: number): VectorStore;
|
|
@@ -81,6 +85,7 @@ class VectorStore {
|
|
|
81
85
|
readonly len: number;
|
|
82
86
|
readonly is_empty: boolean;
|
|
83
87
|
readonly dimension: number;
|
|
88
|
+
readonly storage_mode: string; // "full", "sq8", or "binary"
|
|
84
89
|
|
|
85
90
|
// Methods
|
|
86
91
|
insert(id: bigint, vector: Float32Array): void;
|
|
@@ -89,7 +94,7 @@ class VectorStore {
|
|
|
89
94
|
remove(id: bigint): boolean;
|
|
90
95
|
clear(): void;
|
|
91
96
|
reserve(additional: number): void; // Pre-allocate memory
|
|
92
|
-
memory_usage(): number;
|
|
97
|
+
memory_usage(): number; // Accurate for each storage mode
|
|
93
98
|
}
|
|
94
99
|
```
|
|
95
100
|
|
|
@@ -100,6 +105,31 @@ class VectorStore {
|
|
|
100
105
|
| `cosine` | Cosine similarity | Text embeddings (BERT, GPT) |
|
|
101
106
|
| `euclidean` | L2 distance | Image features, spatial data |
|
|
102
107
|
| `dot` | Dot product | Pre-normalized vectors |
|
|
108
|
+
| `hamming` | Hamming distance | Binary vectors, fingerprints |
|
|
109
|
+
| `jaccard` | Jaccard similarity | Set similarity, sparse vectors |
|
|
110
|
+
|
|
111
|
+
## Storage Modes (Memory Optimization)
|
|
112
|
+
|
|
113
|
+
Reduce memory usage with quantization:
|
|
114
|
+
|
|
115
|
+
```javascript
|
|
116
|
+
// Full precision (default) - best recall
|
|
117
|
+
const full = new VectorStore(768, 'cosine');
|
|
118
|
+
|
|
119
|
+
// SQ8: 4x memory reduction (~1% recall loss)
|
|
120
|
+
const sq8 = VectorStore.new_with_mode(768, 'cosine', 'sq8');
|
|
121
|
+
|
|
122
|
+
// Binary: 32x memory reduction (~5-10% recall loss)
|
|
123
|
+
const binary = VectorStore.new_with_mode(768, 'hamming', 'binary');
|
|
124
|
+
|
|
125
|
+
console.log(sq8.storage_mode); // "sq8"
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
| Mode | Memory (768D) | Compression | Use Case |
|
|
129
|
+
|------|---------------|-------------|----------|
|
|
130
|
+
| `full` | 3080 bytes | 1x | Default, max precision |
|
|
131
|
+
| `sq8` | 784 bytes | **4x** | Scale, RAM-constrained |
|
|
132
|
+
| `binary` | 104 bytes | **32x** | Edge, IoT, mobile PWA |
|
|
103
133
|
|
|
104
134
|
## IndexedDB Persistence
|
|
105
135
|
|
package/package.json
CHANGED
package/velesdb_wasm.d.ts
CHANGED
|
@@ -1,6 +1,24 @@
|
|
|
1
1
|
/* tslint:disable */
|
|
2
2
|
/* eslint-disable */
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* Storage mode for vector quantization.
|
|
6
|
+
*/
|
|
7
|
+
export enum StorageMode {
|
|
8
|
+
/**
|
|
9
|
+
* Full f32 precision (4 bytes per dimension)
|
|
10
|
+
*/
|
|
11
|
+
Full = 0,
|
|
12
|
+
/**
|
|
13
|
+
* SQ8: 8-bit scalar quantization (1 byte per dimension, 4x compression)
|
|
14
|
+
*/
|
|
15
|
+
SQ8 = 1,
|
|
16
|
+
/**
|
|
17
|
+
* Binary: 1-bit quantization (1 bit per dimension, 32x compression)
|
|
18
|
+
*/
|
|
19
|
+
Binary = 2,
|
|
20
|
+
}
|
|
21
|
+
|
|
4
22
|
export class VectorStore {
|
|
5
23
|
free(): void;
|
|
6
24
|
[Symbol.dispose](): void;
|
|
@@ -23,6 +41,22 @@ export class VectorStore {
|
|
|
23
41
|
* Returns memory usage estimate in bytes.
|
|
24
42
|
*/
|
|
25
43
|
memory_usage(): number;
|
|
44
|
+
/**
|
|
45
|
+
* Creates a new vector store with specified storage mode for memory optimization.
|
|
46
|
+
*
|
|
47
|
+
* # Arguments
|
|
48
|
+
*
|
|
49
|
+
* * `dimension` - Vector dimension
|
|
50
|
+
* * `metric` - Distance metric
|
|
51
|
+
* * `mode` - Storage mode: "full", "sq8", or "binary"
|
|
52
|
+
*
|
|
53
|
+
* # Storage Modes
|
|
54
|
+
*
|
|
55
|
+
* - `full`: Best recall, 4 bytes/dimension
|
|
56
|
+
* - `sq8`: 4x compression, ~1% recall loss
|
|
57
|
+
* - `binary`: 32x compression, ~5-10% recall loss
|
|
58
|
+
*/
|
|
59
|
+
static new_with_mode(dimension: number, metric: string, mode: string): VectorStore;
|
|
26
60
|
/**
|
|
27
61
|
* Creates a new vector store with pre-allocated capacity.
|
|
28
62
|
*
|
|
@@ -193,6 +227,10 @@ export class VectorStore {
|
|
|
193
227
|
* * `additional` - Number of additional vectors to reserve space for
|
|
194
228
|
*/
|
|
195
229
|
reserve(additional: number): void;
|
|
230
|
+
/**
|
|
231
|
+
* Returns the storage mode.
|
|
232
|
+
*/
|
|
233
|
+
readonly storage_mode: string;
|
|
196
234
|
/**
|
|
197
235
|
* Returns the number of vectors in the store.
|
|
198
236
|
*/
|
|
@@ -224,16 +262,18 @@ export interface InitOutput {
|
|
|
224
262
|
readonly vectorstore_load: (a: number, b: number) => number;
|
|
225
263
|
readonly vectorstore_memory_usage: (a: number) => number;
|
|
226
264
|
readonly vectorstore_new: (a: number, b: number, c: number, d: number) => void;
|
|
265
|
+
readonly vectorstore_new_with_mode: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
|
|
227
266
|
readonly vectorstore_remove: (a: number, b: bigint) => number;
|
|
228
267
|
readonly vectorstore_reserve: (a: number, b: number) => void;
|
|
229
268
|
readonly vectorstore_save: (a: number, b: number, c: number) => number;
|
|
230
269
|
readonly vectorstore_search: (a: number, b: number, c: number, d: number, e: number) => void;
|
|
270
|
+
readonly vectorstore_storage_mode: (a: number, b: number) => void;
|
|
231
271
|
readonly vectorstore_with_capacity: (a: number, b: number, c: number, d: number, e: number) => void;
|
|
232
|
-
readonly
|
|
233
|
-
readonly
|
|
234
|
-
readonly
|
|
235
|
-
readonly
|
|
236
|
-
readonly
|
|
272
|
+
readonly __wasm_bindgen_func_elem_97: (a: number, b: number, c: number) => void;
|
|
273
|
+
readonly __wasm_bindgen_func_elem_96: (a: number, b: number) => void;
|
|
274
|
+
readonly __wasm_bindgen_func_elem_390: (a: number, b: number, c: number) => void;
|
|
275
|
+
readonly __wasm_bindgen_func_elem_389: (a: number, b: number) => void;
|
|
276
|
+
readonly __wasm_bindgen_func_elem_437: (a: number, b: number, c: number, d: number) => void;
|
|
237
277
|
readonly __wbindgen_export: (a: number, b: number) => number;
|
|
238
278
|
readonly __wbindgen_export2: (a: number, b: number, c: number, d: number) => number;
|
|
239
279
|
readonly __wbindgen_export3: (a: number) => void;
|
package/velesdb_wasm.js
CHANGED
|
@@ -251,16 +251,16 @@ if (!('encodeInto' in cachedTextEncoder)) {
|
|
|
251
251
|
|
|
252
252
|
let WASM_VECTOR_LEN = 0;
|
|
253
253
|
|
|
254
|
-
function
|
|
255
|
-
wasm.
|
|
254
|
+
function __wasm_bindgen_func_elem_97(arg0, arg1, arg2) {
|
|
255
|
+
wasm.__wasm_bindgen_func_elem_97(arg0, arg1, addHeapObject(arg2));
|
|
256
256
|
}
|
|
257
257
|
|
|
258
|
-
function
|
|
259
|
-
wasm.
|
|
258
|
+
function __wasm_bindgen_func_elem_390(arg0, arg1, arg2) {
|
|
259
|
+
wasm.__wasm_bindgen_func_elem_390(arg0, arg1, addHeapObject(arg2));
|
|
260
260
|
}
|
|
261
261
|
|
|
262
|
-
function
|
|
263
|
-
wasm.
|
|
262
|
+
function __wasm_bindgen_func_elem_437(arg0, arg1, arg2, arg3) {
|
|
263
|
+
wasm.__wasm_bindgen_func_elem_437(arg0, arg1, addHeapObject(arg2), addHeapObject(arg3));
|
|
264
264
|
}
|
|
265
265
|
|
|
266
266
|
const __wbindgen_enum_IdbTransactionMode = ["readonly", "readwrite", "versionchange", "readwriteflush", "cleanup"];
|
|
@@ -269,6 +269,25 @@ const VectorStoreFinalization = (typeof FinalizationRegistry === 'undefined')
|
|
|
269
269
|
? { register: () => {}, unregister: () => {} }
|
|
270
270
|
: new FinalizationRegistry(ptr => wasm.__wbg_vectorstore_free(ptr >>> 0, 1));
|
|
271
271
|
|
|
272
|
+
/**
|
|
273
|
+
* Storage mode for vector quantization.
|
|
274
|
+
* @enum {0 | 1 | 2}
|
|
275
|
+
*/
|
|
276
|
+
export const StorageMode = Object.freeze({
|
|
277
|
+
/**
|
|
278
|
+
* Full f32 precision (4 bytes per dimension)
|
|
279
|
+
*/
|
|
280
|
+
Full: 0, "0": "Full",
|
|
281
|
+
/**
|
|
282
|
+
* SQ8: 8-bit scalar quantization (1 byte per dimension, 4x compression)
|
|
283
|
+
*/
|
|
284
|
+
SQ8: 1, "1": "SQ8",
|
|
285
|
+
/**
|
|
286
|
+
* Binary: 1-bit quantization (1 bit per dimension, 32x compression)
|
|
287
|
+
*/
|
|
288
|
+
Binary: 2, "2": "Binary",
|
|
289
|
+
});
|
|
290
|
+
|
|
272
291
|
/**
|
|
273
292
|
* A vector store for in-memory vector search.
|
|
274
293
|
*
|
|
@@ -277,6 +296,12 @@ const VectorStoreFinalization = (typeof FinalizationRegistry === 'undefined')
|
|
|
277
296
|
* Uses contiguous memory layout for optimal cache locality and fast
|
|
278
297
|
* serialization. Vector data is stored in a single buffer rather than
|
|
279
298
|
* individual Vec allocations.
|
|
299
|
+
*
|
|
300
|
+
* # Storage Modes
|
|
301
|
+
*
|
|
302
|
+
* - `Full`: f32 precision, best recall
|
|
303
|
+
* - `SQ8`: 4x memory reduction, ~1% recall loss
|
|
304
|
+
* - `Binary`: 32x memory reduction, ~5-10% recall loss
|
|
280
305
|
*/
|
|
281
306
|
export class VectorStore {
|
|
282
307
|
static __wrap(ptr) {
|
|
@@ -332,6 +357,64 @@ export class VectorStore {
|
|
|
332
357
|
const ret = wasm.vectorstore_memory_usage(this.__wbg_ptr);
|
|
333
358
|
return ret >>> 0;
|
|
334
359
|
}
|
|
360
|
+
/**
|
|
361
|
+
* Returns the storage mode.
|
|
362
|
+
* @returns {string}
|
|
363
|
+
*/
|
|
364
|
+
get storage_mode() {
|
|
365
|
+
let deferred1_0;
|
|
366
|
+
let deferred1_1;
|
|
367
|
+
try {
|
|
368
|
+
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
|
|
369
|
+
wasm.vectorstore_storage_mode(retptr, this.__wbg_ptr);
|
|
370
|
+
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
|
|
371
|
+
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
|
|
372
|
+
deferred1_0 = r0;
|
|
373
|
+
deferred1_1 = r1;
|
|
374
|
+
return getStringFromWasm0(r0, r1);
|
|
375
|
+
} finally {
|
|
376
|
+
wasm.__wbindgen_add_to_stack_pointer(16);
|
|
377
|
+
wasm.__wbindgen_export4(deferred1_0, deferred1_1, 1);
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
/**
|
|
381
|
+
* Creates a new vector store with specified storage mode for memory optimization.
|
|
382
|
+
*
|
|
383
|
+
* # Arguments
|
|
384
|
+
*
|
|
385
|
+
* * `dimension` - Vector dimension
|
|
386
|
+
* * `metric` - Distance metric
|
|
387
|
+
* * `mode` - Storage mode: "full", "sq8", or "binary"
|
|
388
|
+
*
|
|
389
|
+
* # Storage Modes
|
|
390
|
+
*
|
|
391
|
+
* - `full`: Best recall, 4 bytes/dimension
|
|
392
|
+
* - `sq8`: 4x compression, ~1% recall loss
|
|
393
|
+
* - `binary`: 32x compression, ~5-10% recall loss
|
|
394
|
+
* @param {number} dimension
|
|
395
|
+
* @param {string} metric
|
|
396
|
+
* @param {string} mode
|
|
397
|
+
* @returns {VectorStore}
|
|
398
|
+
*/
|
|
399
|
+
static new_with_mode(dimension, metric, mode) {
|
|
400
|
+
try {
|
|
401
|
+
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
|
|
402
|
+
const ptr0 = passStringToWasm0(metric, wasm.__wbindgen_export, wasm.__wbindgen_export2);
|
|
403
|
+
const len0 = WASM_VECTOR_LEN;
|
|
404
|
+
const ptr1 = passStringToWasm0(mode, wasm.__wbindgen_export, wasm.__wbindgen_export2);
|
|
405
|
+
const len1 = WASM_VECTOR_LEN;
|
|
406
|
+
wasm.vectorstore_new_with_mode(retptr, dimension, ptr0, len0, ptr1, len1);
|
|
407
|
+
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
|
|
408
|
+
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
|
|
409
|
+
var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
|
|
410
|
+
if (r2) {
|
|
411
|
+
throw takeObject(r1);
|
|
412
|
+
}
|
|
413
|
+
return VectorStore.__wrap(r0);
|
|
414
|
+
} finally {
|
|
415
|
+
wasm.__wbindgen_add_to_stack_pointer(16);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
335
418
|
/**
|
|
336
419
|
* Creates a new vector store with pre-allocated capacity.
|
|
337
420
|
*
|
|
@@ -900,7 +983,7 @@ function __wbg_get_imports() {
|
|
|
900
983
|
const a = state0.a;
|
|
901
984
|
state0.a = 0;
|
|
902
985
|
try {
|
|
903
|
-
return
|
|
986
|
+
return __wasm_bindgen_func_elem_437(a, state0.b, arg0, arg1);
|
|
904
987
|
} finally {
|
|
905
988
|
state0.a = a;
|
|
906
989
|
}
|
|
@@ -1021,12 +1104,12 @@ function __wbg_get_imports() {
|
|
|
1021
1104
|
};
|
|
1022
1105
|
imports.wbg.__wbindgen_cast_5672704bc77a612c = function(arg0, arg1) {
|
|
1023
1106
|
// Cast intrinsic for `Closure(Closure { dtor_idx: 44, function: Function { arguments: [Externref], shim_idx: 45, ret: Unit, inner_ret: Some(Unit) }, mutable: true }) -> Externref`.
|
|
1024
|
-
const ret = makeMutClosure(arg0, arg1, wasm.
|
|
1107
|
+
const ret = makeMutClosure(arg0, arg1, wasm.__wasm_bindgen_func_elem_389, __wasm_bindgen_func_elem_390);
|
|
1025
1108
|
return addHeapObject(ret);
|
|
1026
1109
|
};
|
|
1027
1110
|
imports.wbg.__wbindgen_cast_9cdd8d1a79740578 = function(arg0, arg1) {
|
|
1028
1111
|
// Cast intrinsic for `Closure(Closure { dtor_idx: 6, function: Function { arguments: [NamedExternref("Event")], shim_idx: 7, ret: Unit, inner_ret: Some(Unit) }, mutable: true }) -> Externref`.
|
|
1029
|
-
const ret = makeMutClosure(arg0, arg1, wasm.
|
|
1112
|
+
const ret = makeMutClosure(arg0, arg1, wasm.__wasm_bindgen_func_elem_96, __wasm_bindgen_func_elem_97);
|
|
1030
1113
|
return addHeapObject(ret);
|
|
1031
1114
|
};
|
|
1032
1115
|
imports.wbg.__wbindgen_cast_d6cd19b81560fd6e = function(arg0) {
|
package/velesdb_wasm_bg.wasm
CHANGED
|
Binary file
|