vxdb 0.1.0__tar.gz → 0.2.0__tar.gz
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.
- {vxdb-0.1.0 → vxdb-0.2.0}/Cargo.lock +17 -17
- {vxdb-0.1.0 → vxdb-0.2.0}/Cargo.toml +1 -1
- {vxdb-0.1.0 → vxdb-0.2.0}/PKG-INFO +6 -38
- {vxdb-0.1.0 → vxdb-0.2.0}/README.md +4 -34
- {vxdb-0.1.0 → vxdb-0.2.0}/crates/vxdb-core/src/distance/cosine.rs +36 -15
- {vxdb-0.1.0 → vxdb-0.2.0}/crates/vxdb-core/src/distance/dot.rs +24 -6
- {vxdb-0.1.0 → vxdb-0.2.0}/crates/vxdb-core/src/distance/euclidean.rs +31 -6
- vxdb-0.2.0/crates/vxdb-core/src/distance/mod.rs +49 -0
- {vxdb-0.1.0 → vxdb-0.2.0}/crates/vxdb-core/src/index/flat.rs +4 -5
- {vxdb-0.1.0 → vxdb-0.2.0}/crates/vxdb-core/src/index/hnsw.rs +137 -18
- {vxdb-0.1.0 → vxdb-0.2.0}/crates/vxdb-python/Cargo.toml +1 -1
- {vxdb-0.1.0 → vxdb-0.2.0}/crates/vxdb-python/src/lib.rs +23 -1
- {vxdb-0.1.0 → vxdb-0.2.0}/pyproject.toml +3 -5
- vxdb-0.1.0/crates/vxdb-core/src/distance/mod.rs +0 -25
- {vxdb-0.1.0 → vxdb-0.2.0}/crates/vxdb-core/Cargo.toml +0 -0
- {vxdb-0.1.0 → vxdb-0.2.0}/crates/vxdb-core/src/collection.rs +0 -0
- {vxdb-0.1.0 → vxdb-0.2.0}/crates/vxdb-core/src/error.rs +0 -0
- {vxdb-0.1.0 → vxdb-0.2.0}/crates/vxdb-core/src/filter.rs +0 -0
- {vxdb-0.1.0 → vxdb-0.2.0}/crates/vxdb-core/src/hybrid.rs +0 -0
- {vxdb-0.1.0 → vxdb-0.2.0}/crates/vxdb-core/src/index/mod.rs +0 -0
- {vxdb-0.1.0 → vxdb-0.2.0}/crates/vxdb-core/src/lib.rs +0 -0
- {vxdb-0.1.0 → vxdb-0.2.0}/crates/vxdb-core/src/storage/metadata.rs +0 -0
- {vxdb-0.1.0 → vxdb-0.2.0}/crates/vxdb-core/src/storage/mmap.rs +0 -0
- {vxdb-0.1.0 → vxdb-0.2.0}/crates/vxdb-core/src/storage/mod.rs +0 -0
- {vxdb-0.1.0 → vxdb-0.2.0}/crates/vxdb-core/src/storage/persistent.rs +0 -0
- {vxdb-0.1.0 → vxdb-0.2.0}/crates/vxdb-core/src/storage/wal.rs +0 -0
- {vxdb-0.1.0 → vxdb-0.2.0}/crates/vxdb-core/src/types.rs +0 -0
- {vxdb-0.1.0 → vxdb-0.2.0}/python/vxdb/__init__.py +0 -0
- {vxdb-0.1.0 → vxdb-0.2.0}/python/vxdb/_vxdb.pyi +0 -0
- {vxdb-0.1.0 → vxdb-0.2.0}/python/vxdb/client.py +0 -0
- {vxdb-0.1.0 → vxdb-0.2.0}/python/vxdb/embedding.py +0 -0
- {vxdb-0.1.0 → vxdb-0.2.0}/python/vxdb/py.typed +0 -0
|
@@ -714,9 +714,9 @@ checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682"
|
|
|
714
714
|
|
|
715
715
|
[[package]]
|
|
716
716
|
name = "js-sys"
|
|
717
|
-
version = "0.3.
|
|
717
|
+
version = "0.3.103"
|
|
718
718
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
719
|
-
checksum = "
|
|
719
|
+
checksum = "53b44bfcdb3f8d5837a46dae1ca9660a837176eee74a28b229bc626816589102"
|
|
720
720
|
dependencies = [
|
|
721
721
|
"cfg-if",
|
|
722
722
|
"futures-util",
|
|
@@ -1761,7 +1761,7 @@ checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
|
|
|
1761
1761
|
|
|
1762
1762
|
[[package]]
|
|
1763
1763
|
name = "vxdb-core"
|
|
1764
|
-
version = "0.
|
|
1764
|
+
version = "0.2.0"
|
|
1765
1765
|
dependencies = [
|
|
1766
1766
|
"memmap2",
|
|
1767
1767
|
"parking_lot",
|
|
@@ -1775,7 +1775,7 @@ dependencies = [
|
|
|
1775
1775
|
|
|
1776
1776
|
[[package]]
|
|
1777
1777
|
name = "vxdb-python"
|
|
1778
|
-
version = "0.
|
|
1778
|
+
version = "0.2.0"
|
|
1779
1779
|
dependencies = [
|
|
1780
1780
|
"pyo3",
|
|
1781
1781
|
"serde_json",
|
|
@@ -1784,7 +1784,7 @@ dependencies = [
|
|
|
1784
1784
|
|
|
1785
1785
|
[[package]]
|
|
1786
1786
|
name = "vxdb-server"
|
|
1787
|
-
version = "0.
|
|
1787
|
+
version = "0.2.0"
|
|
1788
1788
|
dependencies = [
|
|
1789
1789
|
"axum",
|
|
1790
1790
|
"clap",
|
|
@@ -1817,9 +1817,9 @@ checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b"
|
|
|
1817
1817
|
|
|
1818
1818
|
[[package]]
|
|
1819
1819
|
name = "wasm-bindgen"
|
|
1820
|
-
version = "0.2.
|
|
1820
|
+
version = "0.2.126"
|
|
1821
1821
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
1822
|
-
checksum = "
|
|
1822
|
+
checksum = "4b067c0c11094aef6b7a801c1e34a26affafdf3d051dba08456b868789aaf9a4"
|
|
1823
1823
|
dependencies = [
|
|
1824
1824
|
"cfg-if",
|
|
1825
1825
|
"once_cell",
|
|
@@ -1830,9 +1830,9 @@ dependencies = [
|
|
|
1830
1830
|
|
|
1831
1831
|
[[package]]
|
|
1832
1832
|
name = "wasm-bindgen-futures"
|
|
1833
|
-
version = "0.4.
|
|
1833
|
+
version = "0.4.76"
|
|
1834
1834
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
1835
|
-
checksum = "
|
|
1835
|
+
checksum = "c62df1340f32221cb9c54d6a27b030e3dba64361d4a95bed55f9aacb44da291d"
|
|
1836
1836
|
dependencies = [
|
|
1837
1837
|
"js-sys",
|
|
1838
1838
|
"wasm-bindgen",
|
|
@@ -1840,9 +1840,9 @@ dependencies = [
|
|
|
1840
1840
|
|
|
1841
1841
|
[[package]]
|
|
1842
1842
|
name = "wasm-bindgen-macro"
|
|
1843
|
-
version = "0.2.
|
|
1843
|
+
version = "0.2.126"
|
|
1844
1844
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
1845
|
-
checksum = "
|
|
1845
|
+
checksum = "167ce5e579f6bcf889c4f7175a8a5a585de84e8ff93976ce393efa5f2837aab1"
|
|
1846
1846
|
dependencies = [
|
|
1847
1847
|
"quote",
|
|
1848
1848
|
"wasm-bindgen-macro-support",
|
|
@@ -1850,9 +1850,9 @@ dependencies = [
|
|
|
1850
1850
|
|
|
1851
1851
|
[[package]]
|
|
1852
1852
|
name = "wasm-bindgen-macro-support"
|
|
1853
|
-
version = "0.2.
|
|
1853
|
+
version = "0.2.126"
|
|
1854
1854
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
1855
|
-
checksum = "
|
|
1855
|
+
checksum = "f3997c7839262f4ef12cf90b818d6340c18e80f263f1a94bf157d0ec4420380e"
|
|
1856
1856
|
dependencies = [
|
|
1857
1857
|
"bumpalo",
|
|
1858
1858
|
"proc-macro2",
|
|
@@ -1863,18 +1863,18 @@ dependencies = [
|
|
|
1863
1863
|
|
|
1864
1864
|
[[package]]
|
|
1865
1865
|
name = "wasm-bindgen-shared"
|
|
1866
|
-
version = "0.2.
|
|
1866
|
+
version = "0.2.126"
|
|
1867
1867
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
1868
|
-
checksum = "
|
|
1868
|
+
checksum = "dc1b4cb0cc549fcf58d7dfc081778139b3d283a081644e833e84682ad71cea24"
|
|
1869
1869
|
dependencies = [
|
|
1870
1870
|
"unicode-ident",
|
|
1871
1871
|
]
|
|
1872
1872
|
|
|
1873
1873
|
[[package]]
|
|
1874
1874
|
name = "web-sys"
|
|
1875
|
-
version = "0.3.
|
|
1875
|
+
version = "0.3.103"
|
|
1876
1876
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
1877
|
-
checksum = "
|
|
1877
|
+
checksum = "8622dcb61c0bcc9fffa6938bed81210af2da9a7e4a1a834b2e37a59b6dfb6141"
|
|
1878
1878
|
dependencies = [
|
|
1879
1879
|
"js-sys",
|
|
1880
1880
|
"wasm-bindgen",
|
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: vxdb
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.2.0
|
|
4
4
|
Classifier: Development Status :: 4 - Beta
|
|
5
5
|
Classifier: Intended Audience :: Developers
|
|
6
6
|
Classifier: License :: OSI Approved :: Apache Software License
|
|
7
7
|
Classifier: Programming Language :: Python :: 3
|
|
8
|
-
Classifier: Programming Language :: Python :: 3.9
|
|
9
|
-
Classifier: Programming Language :: Python :: 3.10
|
|
10
8
|
Classifier: Programming Language :: Python :: 3.11
|
|
11
9
|
Classifier: Programming Language :: Python :: 3.12
|
|
12
10
|
Classifier: Programming Language :: Python :: 3.13
|
|
@@ -22,7 +20,7 @@ Summary: A lightweight, high-performance vector database with a Rust backend. Hy
|
|
|
22
20
|
Keywords: vector-database,embeddings,search,rust,hnsw,bm25,hybrid-search,pyo3
|
|
23
21
|
Author-email: Yusuf Khan <yusuf@getmykhan.com>
|
|
24
22
|
License-Expression: Apache-2.0
|
|
25
|
-
Requires-Python: >=3.
|
|
23
|
+
Requires-Python: >=3.11
|
|
26
24
|
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
|
|
27
25
|
Project-URL: Homepage, https://github.com/getmykhan/vxdb
|
|
28
26
|
Project-URL: Issues, https://github.com/getmykhan/vxdb/issues
|
|
@@ -98,39 +96,7 @@ vxdb's embedded mode is the **real Rust engine** compiled directly into a Python
|
|
|
98
96
|
|
|
99
97
|
## The full picture
|
|
100
98
|
|
|
101
|
-
|
|
102
|
-
┌─────────────────────────────────────────────────┐
|
|
103
|
-
│ Your Python Code │
|
|
104
|
-
└─────────────┬───────────────────┬───────────────┘
|
|
105
|
-
│ │
|
|
106
|
-
┌─────────────▼──────┐ ┌────────▼────────────┐
|
|
107
|
-
│ Embedded (PyO3) │ │ Server (REST API) │
|
|
108
|
-
│ In-process, │ │ Axum, async, │
|
|
109
|
-
│ no serialize, │ │ multi-client │
|
|
110
|
-
│ <1µs overhead │ │ │
|
|
111
|
-
└─────────────┬──────┘ └────────┬────────────┘
|
|
112
|
-
│ │
|
|
113
|
-
┌─────────────▼───────────────────▼───────────────┐
|
|
114
|
-
│ Rust Core Engine │
|
|
115
|
-
│ │
|
|
116
|
-
│ ┌──────────┐ ┌──────────┐ ┌─────────────────┐ │
|
|
117
|
-
│ │ HNSW │ │ Flat │ │ BM25 Keyword │ │
|
|
118
|
-
│ │ Index │ │ Index │ │ Index │ │
|
|
119
|
-
│ └──────────┘ └──────────┘ └─────────────────┘ │
|
|
120
|
-
│ ┌──────────────────┐ ┌──────────────────────┐ │
|
|
121
|
-
│ │ Distance Metrics │ │ Metadata Filtering │ │
|
|
122
|
-
│ │ cosine/L2/dot │ │ 10 operators, SQL │ │
|
|
123
|
-
│ └──────────────────┘ └──────────────────────┘ │
|
|
124
|
-
│ ┌──────────────────────────────────────────┐ │
|
|
125
|
-
│ │ Hybrid Search (Reciprocal Rank Fusion) │ │
|
|
126
|
-
│ └──────────────────────────────────────────┘ │
|
|
127
|
-
└─────────────────────┬───────────────────────────┘
|
|
128
|
-
│
|
|
129
|
-
┌─────────────────────▼───────────────────────────┐
|
|
130
|
-
│ Storage │
|
|
131
|
-
│ mmap vectors │ SQLite metadata │ Write-Ahead Log│
|
|
132
|
-
└─────────────────────────────────────────────────┘
|
|
133
|
-
```
|
|
99
|
+

|
|
134
100
|
|
|
135
101
|
---
|
|
136
102
|
|
|
@@ -195,7 +161,7 @@ Every result returns `{"id", "score", "metadata", "document"}`.
|
|
|
195
161
|
pip install vxdb
|
|
196
162
|
```
|
|
197
163
|
|
|
198
|
-
That's the whole thing. Works on **macOS, Linux, Windows**. Python 3.
|
|
164
|
+
That's the whole thing. Works on **macOS, Linux, Windows**. Python 3.11+.
|
|
199
165
|
|
|
200
166
|
For the HTTP client (talking to a remote vxdb server):
|
|
201
167
|
|
|
@@ -351,6 +317,8 @@ collection.delete(ids)
|
|
|
351
317
|
collection.count()
|
|
352
318
|
```
|
|
353
319
|
|
|
320
|
+
`vectors` accepts a `list[list[float]]` or a 2-D `float32` NumPy array — NumPy arrays are read zero-copy via the buffer protocol, and NumPy is never imported or required.
|
|
321
|
+
|
|
354
322
|
### REST API
|
|
355
323
|
|
|
356
324
|
|
|
@@ -68,39 +68,7 @@ vxdb's embedded mode is the **real Rust engine** compiled directly into a Python
|
|
|
68
68
|
|
|
69
69
|
## The full picture
|
|
70
70
|
|
|
71
|
-
|
|
72
|
-
┌─────────────────────────────────────────────────┐
|
|
73
|
-
│ Your Python Code │
|
|
74
|
-
└─────────────┬───────────────────┬───────────────┘
|
|
75
|
-
│ │
|
|
76
|
-
┌─────────────▼──────┐ ┌────────▼────────────┐
|
|
77
|
-
│ Embedded (PyO3) │ │ Server (REST API) │
|
|
78
|
-
│ In-process, │ │ Axum, async, │
|
|
79
|
-
│ no serialize, │ │ multi-client │
|
|
80
|
-
│ <1µs overhead │ │ │
|
|
81
|
-
└─────────────┬──────┘ └────────┬────────────┘
|
|
82
|
-
│ │
|
|
83
|
-
┌─────────────▼───────────────────▼───────────────┐
|
|
84
|
-
│ Rust Core Engine │
|
|
85
|
-
│ │
|
|
86
|
-
│ ┌──────────┐ ┌──────────┐ ┌─────────────────┐ │
|
|
87
|
-
│ │ HNSW │ │ Flat │ │ BM25 Keyword │ │
|
|
88
|
-
│ │ Index │ │ Index │ │ Index │ │
|
|
89
|
-
│ └──────────┘ └──────────┘ └─────────────────┘ │
|
|
90
|
-
│ ┌──────────────────┐ ┌──────────────────────┐ │
|
|
91
|
-
│ │ Distance Metrics │ │ Metadata Filtering │ │
|
|
92
|
-
│ │ cosine/L2/dot │ │ 10 operators, SQL │ │
|
|
93
|
-
│ └──────────────────┘ └──────────────────────┘ │
|
|
94
|
-
│ ┌──────────────────────────────────────────┐ │
|
|
95
|
-
│ │ Hybrid Search (Reciprocal Rank Fusion) │ │
|
|
96
|
-
│ └──────────────────────────────────────────┘ │
|
|
97
|
-
└─────────────────────┬───────────────────────────┘
|
|
98
|
-
│
|
|
99
|
-
┌─────────────────────▼───────────────────────────┐
|
|
100
|
-
│ Storage │
|
|
101
|
-
│ mmap vectors │ SQLite metadata │ Write-Ahead Log│
|
|
102
|
-
└─────────────────────────────────────────────────┘
|
|
103
|
-
```
|
|
71
|
+

|
|
104
72
|
|
|
105
73
|
---
|
|
106
74
|
|
|
@@ -165,7 +133,7 @@ Every result returns `{"id", "score", "metadata", "document"}`.
|
|
|
165
133
|
pip install vxdb
|
|
166
134
|
```
|
|
167
135
|
|
|
168
|
-
That's the whole thing. Works on **macOS, Linux, Windows**. Python 3.
|
|
136
|
+
That's the whole thing. Works on **macOS, Linux, Windows**. Python 3.11+.
|
|
169
137
|
|
|
170
138
|
For the HTTP client (talking to a remote vxdb server):
|
|
171
139
|
|
|
@@ -321,6 +289,8 @@ collection.delete(ids)
|
|
|
321
289
|
collection.count()
|
|
322
290
|
```
|
|
323
291
|
|
|
292
|
+
`vectors` accepts a `list[list[float]]` or a 2-D `float32` NumPy array — NumPy arrays are read zero-copy via the buffer protocol, and NumPy is never imported or required.
|
|
293
|
+
|
|
324
294
|
### REST API
|
|
325
295
|
|
|
326
296
|
|
|
@@ -1,24 +1,45 @@
|
|
|
1
1
|
use super::DistanceMetric;
|
|
2
2
|
|
|
3
|
+
/// Cosine distance (1 - cosine similarity). Free function shared by the trait
|
|
4
|
+
/// impl and the monomorphized `Metric` enum. Multiple accumulators let LLVM
|
|
5
|
+
/// auto-vectorize the three reductions (see `euclidean` for the rationale).
|
|
6
|
+
#[inline]
|
|
7
|
+
pub(crate) fn cosine(a: &[f32], b: &[f32]) -> f32 {
|
|
8
|
+
const LANES: usize = 8;
|
|
9
|
+
let mut adot = [0.0f32; LANES];
|
|
10
|
+
let mut anorm_a = [0.0f32; LANES];
|
|
11
|
+
let mut anorm_b = [0.0f32; LANES];
|
|
12
|
+
let mut ca = a.chunks_exact(LANES);
|
|
13
|
+
let mut cb = b.chunks_exact(LANES);
|
|
14
|
+
for (x, y) in ca.by_ref().zip(cb.by_ref()) {
|
|
15
|
+
let x: &[f32; LANES] = x.try_into().unwrap();
|
|
16
|
+
let y: &[f32; LANES] = y.try_into().unwrap();
|
|
17
|
+
for j in 0..LANES {
|
|
18
|
+
adot[j] += x[j] * y[j];
|
|
19
|
+
anorm_a[j] += x[j] * x[j];
|
|
20
|
+
anorm_b[j] += y[j] * y[j];
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
let mut dot: f32 = adot.iter().sum();
|
|
24
|
+
let mut norm_a: f32 = anorm_a.iter().sum();
|
|
25
|
+
let mut norm_b: f32 = anorm_b.iter().sum();
|
|
26
|
+
for (x, y) in ca.remainder().iter().zip(cb.remainder()) {
|
|
27
|
+
dot += x * y;
|
|
28
|
+
norm_a += x * x;
|
|
29
|
+
norm_b += y * y;
|
|
30
|
+
}
|
|
31
|
+
let denom = norm_a.sqrt() * norm_b.sqrt();
|
|
32
|
+
if denom == 0.0 {
|
|
33
|
+
return 1.0;
|
|
34
|
+
}
|
|
35
|
+
1.0 - (dot / denom)
|
|
36
|
+
}
|
|
37
|
+
|
|
3
38
|
pub struct CosineDistance;
|
|
4
39
|
|
|
5
40
|
impl DistanceMetric for CosineDistance {
|
|
6
41
|
fn distance(&self, a: &[f32], b: &[f32]) -> f32 {
|
|
7
|
-
|
|
8
|
-
let mut norm_a = 0.0f32;
|
|
9
|
-
let mut norm_b = 0.0f32;
|
|
10
|
-
|
|
11
|
-
for i in 0..a.len() {
|
|
12
|
-
dot += a[i] * b[i];
|
|
13
|
-
norm_a += a[i] * a[i];
|
|
14
|
-
norm_b += b[i] * b[i];
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
let denom = norm_a.sqrt() * norm_b.sqrt();
|
|
18
|
-
if denom == 0.0 {
|
|
19
|
-
return 1.0;
|
|
20
|
-
}
|
|
21
|
-
1.0 - (dot / denom)
|
|
42
|
+
cosine(a, b)
|
|
22
43
|
}
|
|
23
44
|
}
|
|
24
45
|
|
|
@@ -1,15 +1,33 @@
|
|
|
1
1
|
use super::DistanceMetric;
|
|
2
2
|
|
|
3
|
+
/// Negative dot product (so lower = more similar). Free function shared by the
|
|
4
|
+
/// trait impl and the monomorphized `Metric` enum. Multiple accumulators let
|
|
5
|
+
/// LLVM auto-vectorize the reduction (see `euclidean` for the rationale).
|
|
6
|
+
#[inline]
|
|
7
|
+
pub(crate) fn dot(a: &[f32], b: &[f32]) -> f32 {
|
|
8
|
+
const LANES: usize = 8;
|
|
9
|
+
let mut acc = [0.0f32; LANES];
|
|
10
|
+
let mut ca = a.chunks_exact(LANES);
|
|
11
|
+
let mut cb = b.chunks_exact(LANES);
|
|
12
|
+
for (x, y) in ca.by_ref().zip(cb.by_ref()) {
|
|
13
|
+
let x: &[f32; LANES] = x.try_into().unwrap();
|
|
14
|
+
let y: &[f32; LANES] = y.try_into().unwrap();
|
|
15
|
+
for j in 0..LANES {
|
|
16
|
+
acc[j] += x[j] * y[j];
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
let mut sum: f32 = acc.iter().sum();
|
|
20
|
+
for (x, y) in ca.remainder().iter().zip(cb.remainder()) {
|
|
21
|
+
sum += x * y;
|
|
22
|
+
}
|
|
23
|
+
-sum
|
|
24
|
+
}
|
|
25
|
+
|
|
3
26
|
pub struct DotProductDistance;
|
|
4
27
|
|
|
5
28
|
impl DistanceMetric for DotProductDistance {
|
|
6
|
-
/// Returns negative dot product so that lower = more similar.
|
|
7
29
|
fn distance(&self, a: &[f32], b: &[f32]) -> f32 {
|
|
8
|
-
|
|
9
|
-
for i in 0..a.len() {
|
|
10
|
-
dot += a[i] * b[i];
|
|
11
|
-
}
|
|
12
|
-
-dot
|
|
30
|
+
dot(a, b)
|
|
13
31
|
}
|
|
14
32
|
}
|
|
15
33
|
|
|
@@ -1,15 +1,40 @@
|
|
|
1
1
|
use super::DistanceMetric;
|
|
2
2
|
|
|
3
|
+
/// L2 distance. Free function so both the `DistanceMetric` trait impl and the
|
|
4
|
+
/// monomorphized `Metric` enum share one `#[inline]` code path.
|
|
5
|
+
///
|
|
6
|
+
/// Uses `LANES` independent accumulators so LLVM can auto-vectorize the
|
|
7
|
+
/// reduction (NEON/SSE are baseline on arm64/x86-64) — a single running sum
|
|
8
|
+
/// can't vectorize because IEEE f32 addition isn't associative. Zero deps, no
|
|
9
|
+
/// `unsafe`, no intrinsics. Vectors shorter than `LANES` fall to the scalar
|
|
10
|
+
/// remainder and match the naive result exactly.
|
|
11
|
+
#[inline]
|
|
12
|
+
pub(crate) fn euclidean(a: &[f32], b: &[f32]) -> f32 {
|
|
13
|
+
const LANES: usize = 8;
|
|
14
|
+
let mut acc = [0.0f32; LANES];
|
|
15
|
+
let mut ca = a.chunks_exact(LANES);
|
|
16
|
+
let mut cb = b.chunks_exact(LANES);
|
|
17
|
+
for (x, y) in ca.by_ref().zip(cb.by_ref()) {
|
|
18
|
+
let x: &[f32; LANES] = x.try_into().unwrap();
|
|
19
|
+
let y: &[f32; LANES] = y.try_into().unwrap();
|
|
20
|
+
for j in 0..LANES {
|
|
21
|
+
let d = x[j] - y[j];
|
|
22
|
+
acc[j] += d * d;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
let mut sum: f32 = acc.iter().sum();
|
|
26
|
+
for (x, y) in ca.remainder().iter().zip(cb.remainder()) {
|
|
27
|
+
let d = x - y;
|
|
28
|
+
sum += d * d;
|
|
29
|
+
}
|
|
30
|
+
sum.sqrt()
|
|
31
|
+
}
|
|
32
|
+
|
|
3
33
|
pub struct EuclideanDistance;
|
|
4
34
|
|
|
5
35
|
impl DistanceMetric for EuclideanDistance {
|
|
6
36
|
fn distance(&self, a: &[f32], b: &[f32]) -> f32 {
|
|
7
|
-
|
|
8
|
-
for i in 0..a.len() {
|
|
9
|
-
let diff = a[i] - b[i];
|
|
10
|
-
sum += diff * diff;
|
|
11
|
-
}
|
|
12
|
-
sum.sqrt()
|
|
37
|
+
euclidean(a, b)
|
|
13
38
|
}
|
|
14
39
|
}
|
|
15
40
|
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
mod cosine;
|
|
2
|
+
mod dot;
|
|
3
|
+
mod euclidean;
|
|
4
|
+
|
|
5
|
+
pub use cosine::CosineDistance;
|
|
6
|
+
pub use dot::DotProductDistance;
|
|
7
|
+
pub use euclidean::EuclideanDistance;
|
|
8
|
+
|
|
9
|
+
use crate::types::DistanceMetricKind;
|
|
10
|
+
|
|
11
|
+
/// All distance functions return a *distance* (lower = more similar).
|
|
12
|
+
/// For cosine: distance = 1 - cosine_similarity
|
|
13
|
+
/// For dot product: distance = -dot_product (negate so lower = higher similarity)
|
|
14
|
+
/// For euclidean: distance = L2 distance
|
|
15
|
+
pub trait DistanceMetric: Send + Sync {
|
|
16
|
+
fn distance(&self, a: &[f32], b: &[f32]) -> f32;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/// Monomorphized distance dispatch for the hot path.
|
|
20
|
+
///
|
|
21
|
+
/// Unlike `Box<dyn DistanceMetric>`, this `Copy` enum lets the compiler inline
|
|
22
|
+
/// `distance` into the index inner loops (a vtable call cannot be inlined),
|
|
23
|
+
/// which in turn lets LLVM auto-vectorize the per-dimension reduction. Same
|
|
24
|
+
/// math as the trait impls — both share the `#[inline]` free functions.
|
|
25
|
+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
26
|
+
pub enum Metric {
|
|
27
|
+
Cosine,
|
|
28
|
+
Euclidean,
|
|
29
|
+
DotProduct,
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
impl Metric {
|
|
33
|
+
#[inline]
|
|
34
|
+
pub fn distance(&self, a: &[f32], b: &[f32]) -> f32 {
|
|
35
|
+
match self {
|
|
36
|
+
Metric::Cosine => cosine::cosine(a, b),
|
|
37
|
+
Metric::Euclidean => euclidean::euclidean(a, b),
|
|
38
|
+
Metric::DotProduct => dot::dot(a, b),
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
pub fn metric_for_kind(kind: DistanceMetricKind) -> Metric {
|
|
44
|
+
match kind {
|
|
45
|
+
DistanceMetricKind::Cosine => Metric::Cosine,
|
|
46
|
+
DistanceMetricKind::Euclidean => Metric::Euclidean,
|
|
47
|
+
DistanceMetricKind::DotProduct => Metric::DotProduct,
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
use std::collections::HashMap;
|
|
2
2
|
|
|
3
|
-
use crate::distance::
|
|
3
|
+
use crate::distance::Metric;
|
|
4
4
|
use crate::error::{VexError, VexResult};
|
|
5
5
|
use crate::types::{Metadata, SearchResult, VectorData};
|
|
6
6
|
|
|
@@ -8,7 +8,7 @@ use super::VectorIndex;
|
|
|
8
8
|
|
|
9
9
|
pub struct FlatIndex {
|
|
10
10
|
dimension: usize,
|
|
11
|
-
metric:
|
|
11
|
+
metric: Metric,
|
|
12
12
|
vectors: Vec<VectorData>,
|
|
13
13
|
ids: Vec<String>,
|
|
14
14
|
metadata: Vec<Metadata>,
|
|
@@ -16,7 +16,7 @@ pub struct FlatIndex {
|
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
impl FlatIndex {
|
|
19
|
-
pub fn new(dimension: usize, metric:
|
|
19
|
+
pub fn new(dimension: usize, metric: Metric) -> Self {
|
|
20
20
|
Self {
|
|
21
21
|
dimension,
|
|
22
22
|
metric,
|
|
@@ -117,11 +117,10 @@ impl VectorIndex for FlatIndex {
|
|
|
117
117
|
#[cfg(test)]
|
|
118
118
|
mod tests {
|
|
119
119
|
use super::*;
|
|
120
|
-
use crate::distance::CosineDistance;
|
|
121
120
|
use std::collections::HashMap;
|
|
122
121
|
|
|
123
122
|
fn make_index() -> FlatIndex {
|
|
124
|
-
FlatIndex::new(3,
|
|
123
|
+
FlatIndex::new(3, Metric::Cosine)
|
|
125
124
|
}
|
|
126
125
|
|
|
127
126
|
#[test]
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
use std::collections::{BinaryHeap, HashMap
|
|
1
|
+
use std::collections::{BinaryHeap, HashMap};
|
|
2
2
|
use std::cmp::Ordering;
|
|
3
3
|
|
|
4
4
|
use rand::Rng;
|
|
5
5
|
|
|
6
|
-
use crate::distance::
|
|
6
|
+
use crate::distance::Metric;
|
|
7
7
|
use crate::error::{VexError, VexResult};
|
|
8
8
|
use crate::types::{Metadata, SearchResult, VectorData};
|
|
9
9
|
|
|
@@ -63,6 +63,43 @@ impl Ord for FarthestCandidate {
|
|
|
63
63
|
}
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
+
/// Reused "visited" set using version stamps instead of a `HashSet`.
|
|
67
|
+
///
|
|
68
|
+
/// `search_layer` is called millions of times during a build; allocating and
|
|
69
|
+
/// hashing into a fresh `HashSet` each time dominates the non-distance cost.
|
|
70
|
+
/// Here marking visited is an array write and "clearing" between searches is a
|
|
71
|
+
/// single counter bump (`start`), with a rare full clear only on u32 wraparound.
|
|
72
|
+
#[derive(Default)]
|
|
73
|
+
struct VisitedList {
|
|
74
|
+
marks: Vec<u32>,
|
|
75
|
+
cur: u32,
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
impl VisitedList {
|
|
79
|
+
/// Begin a fresh logical visited-set covering `n` nodes (O(1) amortized).
|
|
80
|
+
fn start(&mut self, n: usize) {
|
|
81
|
+
if self.marks.len() < n {
|
|
82
|
+
self.marks.resize(n, 0);
|
|
83
|
+
}
|
|
84
|
+
self.cur = self.cur.wrapping_add(1);
|
|
85
|
+
if self.cur == 0 {
|
|
86
|
+
self.marks.iter_mut().for_each(|m| *m = 0);
|
|
87
|
+
self.cur = 1;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/// Mark `idx` visited; returns true if it was not already visited.
|
|
92
|
+
#[inline]
|
|
93
|
+
fn visit(&mut self, idx: usize) -> bool {
|
|
94
|
+
if self.marks[idx] == self.cur {
|
|
95
|
+
false
|
|
96
|
+
} else {
|
|
97
|
+
self.marks[idx] = self.cur;
|
|
98
|
+
true
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
66
103
|
pub struct HnswConfig {
|
|
67
104
|
pub m: usize,
|
|
68
105
|
pub m_max0: usize,
|
|
@@ -78,7 +115,7 @@ impl Default for HnswConfig {
|
|
|
78
115
|
m,
|
|
79
116
|
m_max0: m * 2,
|
|
80
117
|
ef_construction: 200,
|
|
81
|
-
ef_search:
|
|
118
|
+
ef_search: 150,
|
|
82
119
|
ml: 1.0 / (m as f64).ln(),
|
|
83
120
|
}
|
|
84
121
|
}
|
|
@@ -106,16 +143,18 @@ struct Node {
|
|
|
106
143
|
|
|
107
144
|
pub struct HnswIndex {
|
|
108
145
|
dimension: usize,
|
|
109
|
-
metric:
|
|
146
|
+
metric: Metric,
|
|
110
147
|
config: HnswConfig,
|
|
111
148
|
nodes: Vec<Node>,
|
|
112
149
|
id_to_idx: HashMap<String, usize>,
|
|
113
150
|
entry_point: Option<usize>,
|
|
114
151
|
max_layer: usize,
|
|
152
|
+
/// Reused across inserts on the (single-threaded) build path.
|
|
153
|
+
scratch_visited: VisitedList,
|
|
115
154
|
}
|
|
116
155
|
|
|
117
156
|
impl HnswIndex {
|
|
118
|
-
pub fn new(dimension: usize, metric:
|
|
157
|
+
pub fn new(dimension: usize, metric: Metric, config: HnswConfig) -> Self {
|
|
119
158
|
Self {
|
|
120
159
|
dimension,
|
|
121
160
|
metric,
|
|
@@ -124,6 +163,7 @@ impl HnswIndex {
|
|
|
124
163
|
id_to_idx: HashMap::new(),
|
|
125
164
|
entry_point: None,
|
|
126
165
|
max_layer: 0,
|
|
166
|
+
scratch_visited: VisitedList::default(),
|
|
127
167
|
}
|
|
128
168
|
}
|
|
129
169
|
|
|
@@ -143,13 +183,14 @@ impl HnswIndex {
|
|
|
143
183
|
entry_points: Vec<usize>,
|
|
144
184
|
ef: usize,
|
|
145
185
|
layer: usize,
|
|
186
|
+
visited: &mut VisitedList,
|
|
146
187
|
) -> Vec<Candidate> {
|
|
147
|
-
|
|
188
|
+
visited.start(self.nodes.len());
|
|
148
189
|
let mut candidates: BinaryHeap<Candidate> = BinaryHeap::new();
|
|
149
190
|
let mut results: BinaryHeap<FarthestCandidate> = BinaryHeap::new();
|
|
150
191
|
|
|
151
192
|
for &ep in &entry_points {
|
|
152
|
-
visited.
|
|
193
|
+
visited.visit(ep);
|
|
153
194
|
let d = self.dist(query, &self.nodes[ep].vector);
|
|
154
195
|
let c = Candidate { idx: ep, distance: d };
|
|
155
196
|
candidates.push(c);
|
|
@@ -166,10 +207,9 @@ impl HnswIndex {
|
|
|
166
207
|
let neighbors = &self.nodes[closest.idx].neighbors;
|
|
167
208
|
if layer < neighbors.len() {
|
|
168
209
|
for &neighbor_idx in &neighbors[layer] {
|
|
169
|
-
if visited.
|
|
210
|
+
if !visited.visit(neighbor_idx) {
|
|
170
211
|
continue;
|
|
171
212
|
}
|
|
172
|
-
visited.insert(neighbor_idx);
|
|
173
213
|
|
|
174
214
|
let d = self.dist(query, &self.nodes[neighbor_idx].vector);
|
|
175
215
|
let farthest_dist = results.peek().map(|f| f.0.distance).unwrap_or(f32::MAX);
|
|
@@ -286,10 +326,15 @@ impl VectorIndex for HnswIndex {
|
|
|
286
326
|
let mut ep = self.entry_point.unwrap();
|
|
287
327
|
let query = &self.nodes[node_idx].vector.clone();
|
|
288
328
|
|
|
329
|
+
// Reuse the visited buffer across this insert's layer searches (and
|
|
330
|
+
// across inserts, since we move it back). `mem::take` sidesteps the
|
|
331
|
+
// borrow conflict between `&self` search_layer and `&mut self`.
|
|
332
|
+
let mut visited = std::mem::take(&mut self.scratch_visited);
|
|
333
|
+
|
|
289
334
|
// Traverse from top layer down to node_level + 1 (greedy search)
|
|
290
335
|
let mut current_layer = self.max_layer;
|
|
291
336
|
while current_layer > node_level {
|
|
292
|
-
let results = self.search_layer(query, vec![ep], 1, current_layer);
|
|
337
|
+
let results = self.search_layer(query, vec![ep], 1, current_layer, &mut visited);
|
|
293
338
|
if let Some(nearest) = results.first() {
|
|
294
339
|
ep = nearest.idx;
|
|
295
340
|
}
|
|
@@ -303,7 +348,7 @@ impl VectorIndex for HnswIndex {
|
|
|
303
348
|
let start_layer = node_level.min(self.max_layer);
|
|
304
349
|
for layer in (0..=start_layer).rev() {
|
|
305
350
|
let candidates =
|
|
306
|
-
self.search_layer(query, vec![ep], self.config.ef_construction, layer);
|
|
351
|
+
self.search_layer(query, vec![ep], self.config.ef_construction, layer, &mut visited);
|
|
307
352
|
|
|
308
353
|
let neighbors = self.select_neighbors_simple(&candidates, self.config.m);
|
|
309
354
|
self.connect_neighbors(node_idx, &neighbors, layer);
|
|
@@ -313,6 +358,8 @@ impl VectorIndex for HnswIndex {
|
|
|
313
358
|
}
|
|
314
359
|
}
|
|
315
360
|
|
|
361
|
+
self.scratch_visited = visited;
|
|
362
|
+
|
|
316
363
|
if node_level > self.max_layer {
|
|
317
364
|
self.max_layer = node_level;
|
|
318
365
|
self.entry_point = Some(node_idx);
|
|
@@ -334,10 +381,14 @@ impl VectorIndex for HnswIndex {
|
|
|
334
381
|
|
|
335
382
|
let mut ep = self.entry_point.unwrap();
|
|
336
383
|
|
|
384
|
+
// Queries can run concurrently under a read lock, so the buffer is local
|
|
385
|
+
// (one allocation per query) rather than the shared build-path scratch.
|
|
386
|
+
let mut visited = VisitedList::default();
|
|
387
|
+
|
|
337
388
|
// Greedy descent from top layer to layer 1
|
|
338
389
|
let mut current_layer = self.max_layer;
|
|
339
390
|
while current_layer > 0 {
|
|
340
|
-
let results = self.search_layer(query, vec![ep], 1, current_layer);
|
|
391
|
+
let results = self.search_layer(query, vec![ep], 1, current_layer, &mut visited);
|
|
341
392
|
if let Some(nearest) = results.first() {
|
|
342
393
|
ep = nearest.idx;
|
|
343
394
|
}
|
|
@@ -346,7 +397,7 @@ impl VectorIndex for HnswIndex {
|
|
|
346
397
|
|
|
347
398
|
// Search layer 0 with ef_search
|
|
348
399
|
let ef = self.config.ef_search.max(top_k);
|
|
349
|
-
let candidates = self.search_layer(query, vec![ep], ef, 0);
|
|
400
|
+
let candidates = self.search_layer(query, vec![ep], ef, 0, &mut visited);
|
|
350
401
|
|
|
351
402
|
let results: Vec<SearchResult> = candidates
|
|
352
403
|
.into_iter()
|
|
@@ -386,14 +437,13 @@ impl VectorIndex for HnswIndex {
|
|
|
386
437
|
#[cfg(test)]
|
|
387
438
|
mod tests {
|
|
388
439
|
use super::*;
|
|
389
|
-
use crate::distance::{CosineDistance, EuclideanDistance};
|
|
390
440
|
use crate::index::flat::FlatIndex;
|
|
391
|
-
use std::collections::HashMap;
|
|
441
|
+
use std::collections::{HashMap, HashSet};
|
|
392
442
|
|
|
393
443
|
fn make_hnsw(dim: usize) -> HnswIndex {
|
|
394
444
|
HnswIndex::new(
|
|
395
445
|
dim,
|
|
396
|
-
|
|
446
|
+
Metric::Cosine,
|
|
397
447
|
HnswConfig::new(16, 200, 50),
|
|
398
448
|
)
|
|
399
449
|
}
|
|
@@ -448,10 +498,10 @@ mod tests {
|
|
|
448
498
|
let mut rng = rand::thread_rng();
|
|
449
499
|
let mut hnsw = HnswIndex::new(
|
|
450
500
|
dim,
|
|
451
|
-
|
|
501
|
+
Metric::Euclidean,
|
|
452
502
|
HnswConfig::new(16, 200, 100),
|
|
453
503
|
);
|
|
454
|
-
let mut flat = FlatIndex::new(dim,
|
|
504
|
+
let mut flat = FlatIndex::new(dim, Metric::Euclidean);
|
|
455
505
|
|
|
456
506
|
// Insert same vectors into both
|
|
457
507
|
let mut vectors: Vec<Vec<f32>> = Vec::new();
|
|
@@ -488,6 +538,75 @@ mod tests {
|
|
|
488
538
|
);
|
|
489
539
|
}
|
|
490
540
|
|
|
541
|
+
#[test]
|
|
542
|
+
fn test_default_ef_search_lifts_recall() {
|
|
543
|
+
// Motivates the default ef_search = 150 (raised from 50). On a harder,
|
|
544
|
+
// higher-dimensional regime than `test_recall_against_flat`, the old
|
|
545
|
+
// default left a lot of recall on the table; raising ef_search trades
|
|
546
|
+
// some of vxdb's large query-latency headroom for materially better
|
|
547
|
+
// recall. This guards against regressing the default back down.
|
|
548
|
+
use rand::rngs::StdRng;
|
|
549
|
+
use rand::SeedableRng;
|
|
550
|
+
|
|
551
|
+
let dim = 128;
|
|
552
|
+
let n = 3000;
|
|
553
|
+
let n_queries = 100;
|
|
554
|
+
let top_k = 10;
|
|
555
|
+
|
|
556
|
+
let mut rng = StdRng::seed_from_u64(0xC0FFEE);
|
|
557
|
+
let mut hnsw = HnswIndex::new(
|
|
558
|
+
dim,
|
|
559
|
+
Metric::Euclidean,
|
|
560
|
+
HnswConfig::new(16, 200, 50),
|
|
561
|
+
);
|
|
562
|
+
let mut flat = FlatIndex::new(dim, Metric::Euclidean);
|
|
563
|
+
|
|
564
|
+
let mut queries: Vec<Vec<f32>> = Vec::new();
|
|
565
|
+
for i in 0..n {
|
|
566
|
+
let v: Vec<f32> = (0..dim).map(|_| rng.gen::<f32>()).collect();
|
|
567
|
+
let id = format!("v{}", i);
|
|
568
|
+
hnsw.insert(id.clone(), v.clone(), HashMap::new()).unwrap();
|
|
569
|
+
flat.insert(id, v.clone(), HashMap::new()).unwrap();
|
|
570
|
+
}
|
|
571
|
+
for _ in 0..n_queries {
|
|
572
|
+
queries.push((0..dim).map(|_| rng.gen::<f32>()).collect());
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
let recall_at = |hnsw: &HnswIndex| -> f64 {
|
|
576
|
+
let mut total = 0.0f64;
|
|
577
|
+
for q in &queries {
|
|
578
|
+
let h: HashSet<String> =
|
|
579
|
+
hnsw.search(q, top_k).unwrap().into_iter().map(|r| r.id).collect();
|
|
580
|
+
let f: HashSet<String> =
|
|
581
|
+
flat.search(q, top_k).unwrap().into_iter().map(|r| r.id).collect();
|
|
582
|
+
total += h.intersection(&f).count() as f64 / top_k as f64;
|
|
583
|
+
}
|
|
584
|
+
total / n_queries as f64
|
|
585
|
+
};
|
|
586
|
+
|
|
587
|
+
hnsw.config.ef_search = 50;
|
|
588
|
+
let recall_old = recall_at(&hnsw);
|
|
589
|
+
hnsw.config.ef_search = HnswConfig::default().ef_search; // 150
|
|
590
|
+
let recall_new = recall_at(&hnsw);
|
|
591
|
+
eprintln!(
|
|
592
|
+
"recall@{} dim={}: ef=50 -> {:.3}, ef={} -> {:.3}",
|
|
593
|
+
top_k, dim, recall_old, HnswConfig::default().ef_search, recall_new
|
|
594
|
+
);
|
|
595
|
+
|
|
596
|
+
// The new default must be a clear improvement and a high-quality result.
|
|
597
|
+
assert!(
|
|
598
|
+
recall_new > recall_old + 0.05,
|
|
599
|
+
"raising ef_search should lift recall materially: {:.3} -> {:.3}",
|
|
600
|
+
recall_old,
|
|
601
|
+
recall_new
|
|
602
|
+
);
|
|
603
|
+
assert!(
|
|
604
|
+
recall_new > 0.85,
|
|
605
|
+
"recall at default ef_search = {:.3}, expected > 0.85",
|
|
606
|
+
recall_new
|
|
607
|
+
);
|
|
608
|
+
}
|
|
609
|
+
|
|
491
610
|
#[test]
|
|
492
611
|
fn test_single_element() {
|
|
493
612
|
let mut idx = make_hnsw(2);
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
use std::collections::HashMap;
|
|
4
4
|
use std::path::Path;
|
|
5
5
|
|
|
6
|
+
use pyo3::buffer::PyBuffer;
|
|
6
7
|
use pyo3::exceptions::PyValueError;
|
|
7
8
|
use pyo3::prelude::*;
|
|
8
9
|
use pyo3::types::{PyDict, PyList};
|
|
@@ -110,6 +111,26 @@ fn results_to_py(py: Python<'_>, results: Vec<vxdb_core::types::SearchResult>) -
|
|
|
110
111
|
.collect()
|
|
111
112
|
}
|
|
112
113
|
|
|
114
|
+
/// Convert the `vectors` argument into `Vec<Vec<f32>>`.
|
|
115
|
+
///
|
|
116
|
+
/// Fast path: if the object exposes the Python buffer protocol as a 2-D,
|
|
117
|
+
/// C-contiguous f32 buffer (numpy/torch/jax arrays, `array.array`), read its raw
|
|
118
|
+
/// memory directly — no per-element Python float boxing, no giant intermediate
|
|
119
|
+
/// list. This lets vxdb ingest numpy data efficiently while staying
|
|
120
|
+
/// dependency-free: numpy is never imported or required, we only read the
|
|
121
|
+
/// standard buffer interface it (and others) implement. Otherwise fall back to
|
|
122
|
+
/// extracting a `List[List[float]]`.
|
|
123
|
+
fn extract_vectors(vectors: &Bound<'_, PyAny>) -> PyResult<Vec<Vec<f32>>> {
|
|
124
|
+
if let Ok(buf) = PyBuffer::<f32>::get_bound(vectors) {
|
|
125
|
+
if buf.dimensions() == 2 && buf.is_c_contiguous() {
|
|
126
|
+
let dim = buf.shape()[1];
|
|
127
|
+
let flat = buf.to_vec(vectors.py())?;
|
|
128
|
+
return Ok(flat.chunks(dim).map(<[f32]>::to_vec).collect());
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
vectors.extract::<Vec<Vec<f32>>>()
|
|
132
|
+
}
|
|
133
|
+
|
|
113
134
|
#[pyclass]
|
|
114
135
|
struct Collection {
|
|
115
136
|
name: String,
|
|
@@ -122,10 +143,11 @@ impl Collection {
|
|
|
122
143
|
fn upsert(
|
|
123
144
|
&self,
|
|
124
145
|
ids: Vec<String>,
|
|
125
|
-
vectors:
|
|
146
|
+
vectors: &Bound<'_, PyAny>,
|
|
126
147
|
metadata: Option<Bound<'_, PyList>>,
|
|
127
148
|
documents: Option<Vec<String>>,
|
|
128
149
|
) -> PyResult<()> {
|
|
150
|
+
let vectors = extract_vectors(vectors)?;
|
|
129
151
|
let metas: Vec<HashMap<String, serde_json::Value>> = match metadata {
|
|
130
152
|
Some(list) => {
|
|
131
153
|
let mut result = Vec::new();
|
|
@@ -4,10 +4,10 @@ build-backend = "maturin"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "vxdb"
|
|
7
|
-
version = "0.
|
|
7
|
+
version = "0.2.0"
|
|
8
8
|
description = "A lightweight, high-performance vector database with a Rust backend. Hybrid search, embedded + server modes, zero dependencies."
|
|
9
9
|
readme = "README.md"
|
|
10
|
-
requires-python = ">=3.
|
|
10
|
+
requires-python = ">=3.11"
|
|
11
11
|
license = "Apache-2.0"
|
|
12
12
|
authors = [
|
|
13
13
|
{ name = "Yusuf Khan", email = "yusuf@getmykhan.com" },
|
|
@@ -18,8 +18,6 @@ classifiers = [
|
|
|
18
18
|
"Intended Audience :: Developers",
|
|
19
19
|
"License :: OSI Approved :: Apache Software License",
|
|
20
20
|
"Programming Language :: Python :: 3",
|
|
21
|
-
"Programming Language :: Python :: 3.9",
|
|
22
|
-
"Programming Language :: Python :: 3.10",
|
|
23
21
|
"Programming Language :: Python :: 3.11",
|
|
24
22
|
"Programming Language :: Python :: 3.12",
|
|
25
23
|
"Programming Language :: Python :: 3.13",
|
|
@@ -48,7 +46,7 @@ features = ["pyo3/extension-module"]
|
|
|
48
46
|
testpaths = ["tests"]
|
|
49
47
|
|
|
50
48
|
[tool.ruff]
|
|
51
|
-
target-version = "
|
|
49
|
+
target-version = "py311"
|
|
52
50
|
line-length = 120
|
|
53
51
|
|
|
54
52
|
[tool.ruff.lint]
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
mod cosine;
|
|
2
|
-
mod dot;
|
|
3
|
-
mod euclidean;
|
|
4
|
-
|
|
5
|
-
pub use cosine::CosineDistance;
|
|
6
|
-
pub use dot::DotProductDistance;
|
|
7
|
-
pub use euclidean::EuclideanDistance;
|
|
8
|
-
|
|
9
|
-
use crate::types::DistanceMetricKind;
|
|
10
|
-
|
|
11
|
-
/// All distance functions return a *distance* (lower = more similar).
|
|
12
|
-
/// For cosine: distance = 1 - cosine_similarity
|
|
13
|
-
/// For dot product: distance = -dot_product (negate so lower = higher similarity)
|
|
14
|
-
/// For euclidean: distance = L2 distance
|
|
15
|
-
pub trait DistanceMetric: Send + Sync {
|
|
16
|
-
fn distance(&self, a: &[f32], b: &[f32]) -> f32;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
pub fn metric_for_kind(kind: DistanceMetricKind) -> Box<dyn DistanceMetric> {
|
|
20
|
-
match kind {
|
|
21
|
-
DistanceMetricKind::Cosine => Box::new(CosineDistance),
|
|
22
|
-
DistanceMetricKind::Euclidean => Box::new(EuclideanDistance),
|
|
23
|
-
DistanceMetricKind::DotProduct => Box::new(DotProductDistance),
|
|
24
|
-
}
|
|
25
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|