Skip to content

Commit 166e5a6

Browse files
committed
update
1 parent ec91613 commit 166e5a6

2 files changed

Lines changed: 108 additions & 25 deletions

File tree

README.md

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ enVector supports two types of benchmark cases:
6363
├── README.md
6464
├── scripts
6565
│ ├── get_kmeans_centroids.py # create kmeans centroids
66+
│ ├── prepare_dataset.py # download and prepare ground truth neighbors for GAS dataset
67+
│ ├── prepare_random_dataset.py # download and prepare ground truth neighbors for random dataset
6668
│ ├── requirements.txt # python requirements
67-
│ ├── prepare_dataset.py # download and prepare ground truth neighbors for dataset
6869
│ └── run_benchmark.sh # benchmark script
6970
└── vectordb_bench/config-files # benchmark config file
7071
└── envector_{benchmark_case}_config.yml
@@ -116,7 +117,7 @@ Run the following commands to run enVector with VectorDBBench's built-in benchma
116117
./scripts/run_benchmark.sh --index-type IVF_FLAT --config-file envector_{benchmark_case}_config.yml # IVF-FLAT
117118
```
118119

119-
For more details, please refer to `envector_{benchmark_case}_config.yml` in scripts directory for benchmarks with enVector, or you can use the following command:
120+
For more details, please refer to `envector_{benchmark_case}_config.yml` in `vectordb_bench/config-files` directory for benchmarks with enVector, or you can use the following command:
120121

121122
```bash
122123
python -m vectordb_bench.cli.vectordbbench envectorflat \
@@ -147,10 +148,11 @@ Prepare the following artifacts for the ANN benchmark with `scripts/prepare_data
147148
- prepare ground-truth neighbors
148149
- download centroids for the GAS index for corresponding to the embedding model
149150

150-
For the ANN benchmark, we provide two datasets via HuggingFace:
151-
- `PUBMED768D400K`: [cryptolab-playground/pubmed-arxiv-abstract-embedding-gemma-300m](https://huggingface.co/datasets/cryptolab-playground/pubmed-arxiv-abstract-embedding-gemma-300m)
152-
- `BLOOMBERG768D368K`: [cryptolab-playground/Bloomberg-Financial-News-embedding-gemma-300m](https://huggingface.co/datasets/cryptolab-playground/Bloomberg-Financial-News-embedding-gemma-300m)
153-
- `PRODUCTS512D400K`: [cryptolab-playground/amazon-products-clip-vit-b-32](https://huggingface.co/datasets/cryptolab-playground/amazon-products-clip-vit-b-32)
151+
For the ANN benchmark, we provide four datasets via HuggingFace:
152+
- `pubmed768d400k`: [cryptolab-playground/pubmed-arxiv-abstract-embedding-gemma-300m](https://huggingface.co/datasets/cryptolab-playground/pubmed-arxiv-abstract-embedding-gemma-300m)
153+
- `bloomberg768d368k`: [cryptolab-playground/Bloomberg-Financial-News-embedding-gemma-300m](https://huggingface.co/datasets/cryptolab-playground/Bloomberg-Financial-News-embedding-gemma-300m)
154+
- `products512d400k`: [cryptolab-playground/amazon-products-clip-vit-b-32](https://huggingface.co/datasets/cryptolab-playground/amazon-products-clip-vit-b-32)
155+
- `food512d101k`: [cryptolab-playground/food101-clip-vit-b-32](https://huggingface.co/datasets/cryptolab-playground/food101-clip-vit-b-32)
154156

155157
Also, we provide centroids for the corresponding embedding model used in the ANN benchmark:
156158
- GAS Centroids: [cryptolab-playground/gas-centroids](https://huggingface.co/datasets/cryptolab-playground/gas-centroids)
@@ -161,20 +163,9 @@ To prepare dataset, run the following command as example:
161163
# Install dependencies for preparing dataset
162164
pip install -r ./scripts/requirements.txt
163165

164-
# Prepare GAS dataset: PUBMED768D400K
166+
# Prepare GAS dataset
165167
python ./scripts/prepare_dataset.py \
166-
-d cryptolab-playground/pubmed-arxiv-abstract-embedding-gemma-300m \
167-
-e embeddinggemma-300m
168-
169-
# Prepare GAS dataset: BLOOMBERG768D368K
170-
python ./scripts/prepare_dataset.py \
171-
-d cryptolab-playground/Bloomberg-Financial-News-embedding-gemma-300m \
172-
-e embeddinggemma-300m
173-
174-
# Prepare GAS dataset: PRODUCTS512D400K
175-
python ./scripts/prepare_dataset.py \
176-
-d playground/amazon-products-clip-vit-b-32 \
177-
-e clip-vit-b-32
168+
-d pubmed768d400k
178169
```
179170

180171
Then, you can find the generated files as follows:
@@ -204,24 +195,21 @@ Run the provided shell scripts (`./scripts/run_benchmark.sh`) as the following:
204195
For more details, please refer to `run_benchmark.sh` or `envector_{benchmark_case}_config.yml` in scripts directory for benchmarks with enVector with ANN (GAS), or you can use the following command:
205196

206197
```bash
207-
python -m vectordb_bench.cli.vectordbbench envectorivfflat \
198+
python -m vectordb_bench.cli.vectordbbench envectorivfgas \
208199
--config-file envector_pubmed_config.yml
209200

210201
# or
211202

212-
python -m vectordb_bench.cli.vectordbbench envectorivfflat \
203+
python -m vectordb_bench.cli.vectordbbench envectorivfgas \
213204
--uri "localhost:50050" \
214205
--eval-mode mm \
215206
... \
216207
--train-centroids True \
217208
--centroids-path "./centroids/embeddinggemma-300m/centroids.npy" \
218-
--nlist 1024 \
209+
--nlist 32768 \
219210
--nprobe 6
220211
```
221212

222-
Note that, **`NUM_PER_BATCH` should be set to the database size** when using IVF-based ANN index for enVector currently.
223-
We will support adjustable `NUM_PER_BATCH` for ANN soon.
224-
225213
## 🎯 Advanced Usage
226214

227215
### Prepare Other Datasets

scripts/get_kmeans_centroids.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
"""
2+
Get KMeans centroids for a given dataset.
3+
"""
4+
5+
import argparse
6+
import os
7+
8+
import faiss
9+
import numpy as np
10+
import pandas as pd
11+
from numpy.linalg import norm
12+
13+
14+
def get_args():
15+
parser = argparse.ArgumentParser(description="KMeans Centroid Calculation")
16+
parser.add_argument(
17+
"--nlist",
18+
type=int,
19+
default=256,
20+
help="Number of clusters for KMeans",
21+
)
22+
parser.add_argument(
23+
"--file-path",
24+
type=str,
25+
default="/tmp/vectordb_bench/dataset/openai/openai_medium_500k",
26+
help="Path to the dataset directory",
27+
)
28+
parser.add_argument(
29+
"--out-path",
30+
type=str,
31+
default="/tmp/vectordb_bench/centroids/kmeans-centroids/openai_medium_500k",
32+
help="Path to the output directory",
33+
)
34+
parser.add_argument(
35+
"--dim",
36+
type=int,
37+
default=512,
38+
help="Dimension of the embeddings.",
39+
)
40+
return parser.parse_args()
41+
42+
43+
def load_dataset(file_path):
44+
print("Loading dataset from:", file_path)
45+
46+
# load parquet files
47+
train_vectors = pd.read_parquet(f"{file_path}/train.parquet")
48+
49+
# sort by id
50+
train_vectors.sort_values(by="id", inplace=True)
51+
train_ids = train_vectors["id"].to_numpy(dtype=np.int64)
52+
train_vectors = np.vstack(train_vectors["emb"].values)
53+
train_vectors /= norm(train_vectors, axis=1, keepdims=True)
54+
print(f"train_vectors shape: {train_vectors.shape}")
55+
56+
return train_vectors.astype(np.float32)
57+
58+
59+
def main():
60+
args = get_args()
61+
62+
nlist = args.nlist
63+
seed = 42
64+
65+
# prepare dataset
66+
train_vectors = load_dataset(args.file_path)
67+
dim = train_vectors.shape[1]
68+
assert dim == args.dim, f"Expected dimension {args.dim}, but got {dim}"
69+
print("✅ Load dataset complete.")
70+
71+
# kmeans using faiss
72+
kmeans = faiss.Kmeans(dim, nlist, niter=25, seed=seed, verbose=True, gpu=True)
73+
kmeans.train(train_vectors)
74+
print("✅ KMeans training complete.")
75+
76+
# allocate
77+
_, labels = kmeans.index.search(train_vectors, 1)
78+
labels = labels.flatten()
79+
centroids = kmeans.centroids
80+
print(f"Labels shape: {labels.shape}")
81+
print(f"Centroids shape: {centroids.shape}")
82+
83+
# normalize
84+
centroids /= norm(centroids, axis=1, keepdims=True)
85+
print(f"Norm: {norm(centroids, axis=1)}")
86+
87+
# save centroids
88+
os.makedirs(args.out_path, exist_ok=True)
89+
file_name = os.path.join(args.out_path, f"centroids_{nlist}.npy")
90+
np.save(file_name, centroids)
91+
print(f"✅ Centroids saved to {file_name}")
92+
93+
94+
if __name__ == "__main__":
95+
main()

0 commit comments

Comments
 (0)