@wiscale/velesdb-wasm 0.5.2 → 0.8.5
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 +14 -3
- package/velesdb_wasm.d.ts +49 -5
- package/velesdb_wasm.js +96 -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
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wiscale/velesdb-wasm",
|
|
3
3
|
"type": "module",
|
|
4
|
+
"collaborators": [
|
|
5
|
+
"VelesDB Team <contact@wiscale.fr>"
|
|
6
|
+
],
|
|
4
7
|
"description": "VelesDB for WebAssembly - Vector search in the browser",
|
|
5
|
-
"version": "0.5
|
|
6
|
-
"license": "
|
|
8
|
+
"version": "0.8.5",
|
|
9
|
+
"license": "SEE LICENSE IN ../../LICENSE",
|
|
7
10
|
"repository": {
|
|
8
11
|
"type": "git",
|
|
9
|
-
"url": "https://github.com/cyberlife-coder/
|
|
12
|
+
"url": "https://github.com/cyberlife-coder/velesdb"
|
|
10
13
|
},
|
|
11
14
|
"files": [
|
|
12
15
|
"velesdb_wasm_bg.wasm",
|
|
@@ -14,8 +17,16 @@
|
|
|
14
17
|
"velesdb_wasm.d.ts"
|
|
15
18
|
],
|
|
16
19
|
"main": "velesdb_wasm.js",
|
|
20
|
+
"homepage": "https://velesdb.com",
|
|
17
21
|
"types": "velesdb_wasm.d.ts",
|
|
18
22
|
"sideEffects": [
|
|
19
23
|
"./snippets/*"
|
|
24
|
+
],
|
|
25
|
+
"keywords": [
|
|
26
|
+
"vector-database",
|
|
27
|
+
"wasm",
|
|
28
|
+
"webassembly",
|
|
29
|
+
"browser",
|
|
30
|
+
"search"
|
|
20
31
|
]
|
|
21
32
|
}
|
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,26 @@ 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
|
+
* # Errors
|
|
60
|
+
*
|
|
61
|
+
* Returns an error if the metric or storage mode is unknown.
|
|
62
|
+
*/
|
|
63
|
+
static new_with_mode(dimension: number, metric: string, mode: string): VectorStore;
|
|
26
64
|
/**
|
|
27
65
|
* Creates a new vector store with pre-allocated capacity.
|
|
28
66
|
*
|
|
@@ -193,6 +231,10 @@ export class VectorStore {
|
|
|
193
231
|
* * `additional` - Number of additional vectors to reserve space for
|
|
194
232
|
*/
|
|
195
233
|
reserve(additional: number): void;
|
|
234
|
+
/**
|
|
235
|
+
* Returns the storage mode.
|
|
236
|
+
*/
|
|
237
|
+
readonly storage_mode: string;
|
|
196
238
|
/**
|
|
197
239
|
* Returns the number of vectors in the store.
|
|
198
240
|
*/
|
|
@@ -224,16 +266,18 @@ export interface InitOutput {
|
|
|
224
266
|
readonly vectorstore_load: (a: number, b: number) => number;
|
|
225
267
|
readonly vectorstore_memory_usage: (a: number) => number;
|
|
226
268
|
readonly vectorstore_new: (a: number, b: number, c: number, d: number) => void;
|
|
269
|
+
readonly vectorstore_new_with_mode: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
|
|
227
270
|
readonly vectorstore_remove: (a: number, b: bigint) => number;
|
|
228
271
|
readonly vectorstore_reserve: (a: number, b: number) => void;
|
|
229
272
|
readonly vectorstore_save: (a: number, b: number, c: number) => number;
|
|
230
273
|
readonly vectorstore_search: (a: number, b: number, c: number, d: number, e: number) => void;
|
|
274
|
+
readonly vectorstore_storage_mode: (a: number, b: number) => void;
|
|
231
275
|
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
|
|
276
|
+
readonly __wasm_bindgen_func_elem_97: (a: number, b: number, c: number) => void;
|
|
277
|
+
readonly __wasm_bindgen_func_elem_96: (a: number, b: number) => void;
|
|
278
|
+
readonly __wasm_bindgen_func_elem_390: (a: number, b: number, c: number) => void;
|
|
279
|
+
readonly __wasm_bindgen_func_elem_389: (a: number, b: number) => void;
|
|
280
|
+
readonly __wasm_bindgen_func_elem_437: (a: number, b: number, c: number, d: number) => void;
|
|
237
281
|
readonly __wbindgen_export: (a: number, b: number) => number;
|
|
238
282
|
readonly __wbindgen_export2: (a: number, b: number, c: number, d: number) => number;
|
|
239
283
|
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,68 @@ 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
|
+
*
|
|
395
|
+
* # Errors
|
|
396
|
+
*
|
|
397
|
+
* Returns an error if the metric or storage mode is unknown.
|
|
398
|
+
* @param {number} dimension
|
|
399
|
+
* @param {string} metric
|
|
400
|
+
* @param {string} mode
|
|
401
|
+
* @returns {VectorStore}
|
|
402
|
+
*/
|
|
403
|
+
static new_with_mode(dimension, metric, mode) {
|
|
404
|
+
try {
|
|
405
|
+
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
|
|
406
|
+
const ptr0 = passStringToWasm0(metric, wasm.__wbindgen_export, wasm.__wbindgen_export2);
|
|
407
|
+
const len0 = WASM_VECTOR_LEN;
|
|
408
|
+
const ptr1 = passStringToWasm0(mode, wasm.__wbindgen_export, wasm.__wbindgen_export2);
|
|
409
|
+
const len1 = WASM_VECTOR_LEN;
|
|
410
|
+
wasm.vectorstore_new_with_mode(retptr, dimension, ptr0, len0, ptr1, len1);
|
|
411
|
+
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
|
|
412
|
+
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
|
|
413
|
+
var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
|
|
414
|
+
if (r2) {
|
|
415
|
+
throw takeObject(r1);
|
|
416
|
+
}
|
|
417
|
+
return VectorStore.__wrap(r0);
|
|
418
|
+
} finally {
|
|
419
|
+
wasm.__wbindgen_add_to_stack_pointer(16);
|
|
420
|
+
}
|
|
421
|
+
}
|
|
335
422
|
/**
|
|
336
423
|
* Creates a new vector store with pre-allocated capacity.
|
|
337
424
|
*
|
|
@@ -900,7 +987,7 @@ function __wbg_get_imports() {
|
|
|
900
987
|
const a = state0.a;
|
|
901
988
|
state0.a = 0;
|
|
902
989
|
try {
|
|
903
|
-
return
|
|
990
|
+
return __wasm_bindgen_func_elem_437(a, state0.b, arg0, arg1);
|
|
904
991
|
} finally {
|
|
905
992
|
state0.a = a;
|
|
906
993
|
}
|
|
@@ -1021,12 +1108,12 @@ function __wbg_get_imports() {
|
|
|
1021
1108
|
};
|
|
1022
1109
|
imports.wbg.__wbindgen_cast_5672704bc77a612c = function(arg0, arg1) {
|
|
1023
1110
|
// 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.
|
|
1111
|
+
const ret = makeMutClosure(arg0, arg1, wasm.__wasm_bindgen_func_elem_389, __wasm_bindgen_func_elem_390);
|
|
1025
1112
|
return addHeapObject(ret);
|
|
1026
1113
|
};
|
|
1027
1114
|
imports.wbg.__wbindgen_cast_9cdd8d1a79740578 = function(arg0, arg1) {
|
|
1028
1115
|
// 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.
|
|
1116
|
+
const ret = makeMutClosure(arg0, arg1, wasm.__wasm_bindgen_func_elem_96, __wasm_bindgen_func_elem_97);
|
|
1030
1117
|
return addHeapObject(ret);
|
|
1031
1118
|
};
|
|
1032
1119
|
imports.wbg.__wbindgen_cast_d6cd19b81560fd6e = function(arg0) {
|
package/velesdb_wasm_bg.wasm
CHANGED
|
Binary file
|