hotchpotch
commited on
Commit
•
4646a6d
1
Parent(s):
5baaf56
Add files and update code for generating
Browse files- .gitignore +3 -1
- Generate.md +17 -2
- embs_to_faiss.py +193 -0
.gitignore
CHANGED
@@ -1 +1,3 @@
|
|
1 |
-
*_debug/*
|
|
|
|
|
|
1 |
+
*_debug/*
|
2 |
+
data_no_quantization.index
|
3 |
+
chunked_no_quantization.index
|
Generate.md
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
|
2 |
-
|
3 |
|
4 |
### multilingual-e5-small + passage
|
5 |
|
@@ -29,4 +29,19 @@ python datasets_to_embs.py \
|
|
29 |
--model_name="intfloat/multilingual-e5-small" \
|
30 |
--output_name="multilingual-e5-small-query" \
|
31 |
--input_prefix="query: "
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
|
2 |
+
## create embeddings
|
3 |
|
4 |
### multilingual-e5-small + passage
|
5 |
|
|
|
29 |
--model_name="intfloat/multilingual-e5-small" \
|
30 |
--output_name="multilingual-e5-small-query" \
|
31 |
--input_prefix="query: "
|
32 |
+
```
|
33 |
+
|
34 |
+
|
35 |
+
## create faiss indexes
|
36 |
+
|
37 |
+
```
|
38 |
+
python embs_to_faiss.py \
|
39 |
+
--target="data" \
|
40 |
+
--input_name="multilingual-e5-small-query" \
|
41 |
+
--use_gpu
|
42 |
+
|
43 |
+
```
|
44 |
+
|
45 |
+
# XXX
|
46 |
+
|
47 |
+
- 同じ output があったら skip する
|
embs_to_faiss.py
ADDED
@@ -0,0 +1,193 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from __future__ import annotations
|
2 |
+
from dataclasses import dataclass
|
3 |
+
import numpy as np
|
4 |
+
from tqdm import tqdm
|
5 |
+
import argparse
|
6 |
+
from pathlib import Path
|
7 |
+
import faiss
|
8 |
+
|
9 |
+
parser = argparse.ArgumentParser(description="Convert datasets to embeddings")
|
10 |
+
parser.add_argument(
|
11 |
+
"-t",
|
12 |
+
"--target",
|
13 |
+
type=str,
|
14 |
+
required=True,
|
15 |
+
choices=["data", "chunked"],
|
16 |
+
help="target dataset, data or chunked",
|
17 |
+
)
|
18 |
+
|
19 |
+
parser.add_argument(
|
20 |
+
"-d",
|
21 |
+
"--debug",
|
22 |
+
action="store_true",
|
23 |
+
help="debug mode, use small dataset",
|
24 |
+
)
|
25 |
+
parser.add_argument(
|
26 |
+
"-i",
|
27 |
+
"--input_name",
|
28 |
+
type=str,
|
29 |
+
required=True,
|
30 |
+
help="input dir name",
|
31 |
+
)
|
32 |
+
# m
|
33 |
+
parser.add_argument(
|
34 |
+
"-m",
|
35 |
+
"--m",
|
36 |
+
type=int,
|
37 |
+
required=False,
|
38 |
+
default=8,
|
39 |
+
help="faiss param: m, subvector",
|
40 |
+
)
|
41 |
+
# mbit
|
42 |
+
parser.add_argument(
|
43 |
+
"-b",
|
44 |
+
"--mbit",
|
45 |
+
type=int,
|
46 |
+
required=False,
|
47 |
+
default=8,
|
48 |
+
help="faiss param: mbit, bits_per_idx",
|
49 |
+
)
|
50 |
+
# nlist
|
51 |
+
parser.add_argument(
|
52 |
+
"-n",
|
53 |
+
"--nlist",
|
54 |
+
type=int,
|
55 |
+
required=False,
|
56 |
+
default=None,
|
57 |
+
help="faiss param: nlist, None is auto calc, sqrt(len(ds)))",
|
58 |
+
)
|
59 |
+
# use_gpu
|
60 |
+
parser.add_argument(
|
61 |
+
"-g",
|
62 |
+
"--use_gpu",
|
63 |
+
action="store_true",
|
64 |
+
help="use gpu",
|
65 |
+
)
|
66 |
+
# no quantization
|
67 |
+
parser.add_argument(
|
68 |
+
"--no_quantization",
|
69 |
+
action="store_true",
|
70 |
+
help="no quantization",
|
71 |
+
)
|
72 |
+
# force override
|
73 |
+
parser.add_argument(
|
74 |
+
"--force",
|
75 |
+
action="store_true",
|
76 |
+
help="force override existing index file",
|
77 |
+
)
|
78 |
+
args = parser.parse_args()
|
79 |
+
|
80 |
+
|
81 |
+
@dataclass
|
82 |
+
class FaissConfig:
|
83 |
+
m: int = 8 # subvector
|
84 |
+
mbit: int = 8 # bits_per_idx
|
85 |
+
nlist: int | None = None # nlist, None is auto calc, sqrt(len(ds)))
|
86 |
+
quantization: bool = True
|
87 |
+
|
88 |
+
|
89 |
+
args = parser.parse_args()
|
90 |
+
|
91 |
+
target_local_ds = args.target
|
92 |
+
faiss_config = FaissConfig(
|
93 |
+
m=args.m,
|
94 |
+
mbit=args.mbit,
|
95 |
+
nlist=args.nlist,
|
96 |
+
quantization=not args.no_quantization,
|
97 |
+
)
|
98 |
+
|
99 |
+
embs_dir = "embs"
|
100 |
+
|
101 |
+
input_embs_path = Path("/".join(["embs", args.input_name, target_local_ds]))
|
102 |
+
input_embs_npz = list(input_embs_path.glob("*.npz"))
|
103 |
+
input_embs_npz.sort(key=lambda x: int(x.stem))
|
104 |
+
|
105 |
+
if len(input_embs_npz) == 0:
|
106 |
+
print(f"input embs not found: {input_embs_path}")
|
107 |
+
exit(1)
|
108 |
+
else:
|
109 |
+
print(f"input {len(input_embs_npz)} embs(*.npz) found: {input_embs_path}")
|
110 |
+
|
111 |
+
|
112 |
+
def gen_index_filename(config: FaissConfig, target_local_ds: str) -> str:
|
113 |
+
default_faiss_config = FaissConfig()
|
114 |
+
if (
|
115 |
+
config.m == default_faiss_config.m
|
116 |
+
and config.mbit == default_faiss_config.mbit
|
117 |
+
and config.nlist == default_faiss_config.nlist
|
118 |
+
and config.quantization == default_faiss_config.quantization
|
119 |
+
):
|
120 |
+
return f"{target_local_ds}.index"
|
121 |
+
elif not config.quantization:
|
122 |
+
return f"{target_local_ds}_no_quantization.index"
|
123 |
+
else:
|
124 |
+
return f"{target_local_ds}_m{config.m}_mbit{config.mbit}_nlist_{config.nlist}.index"
|
125 |
+
|
126 |
+
|
127 |
+
def gen_faiss_index(config: FaissConfig, dim: str, use_gpu: bool):
|
128 |
+
flat_l2 = faiss.IndexFlatL2(dim)
|
129 |
+
if not config.quantization:
|
130 |
+
faiss_index = flat_l2
|
131 |
+
else:
|
132 |
+
faiss_index = faiss.IndexIVFPQ(
|
133 |
+
flat_l2,
|
134 |
+
dim,
|
135 |
+
config.nlist,
|
136 |
+
config.m,
|
137 |
+
config.mbit,
|
138 |
+
)
|
139 |
+
if use_gpu:
|
140 |
+
gpu_res = faiss.StandardGpuResources() # use a single GPU
|
141 |
+
faiss_index = faiss.index_cpu_to_gpu(gpu_res, 0, faiss_index)
|
142 |
+
return faiss_index
|
143 |
+
else:
|
144 |
+
return faiss_index
|
145 |
+
|
146 |
+
|
147 |
+
output_faiss_path = Path(
|
148 |
+
"/".join(
|
149 |
+
[
|
150 |
+
"faiss_indexes",
|
151 |
+
args.input_name,
|
152 |
+
gen_index_filename(faiss_config, target_local_ds),
|
153 |
+
]
|
154 |
+
)
|
155 |
+
)
|
156 |
+
output_faiss_path.parent.mkdir(parents=True, exist_ok=True)
|
157 |
+
|
158 |
+
# output_faiss_path がすでにある場合
|
159 |
+
if output_faiss_path.exists():
|
160 |
+
if args.force:
|
161 |
+
print("force override existing index file")
|
162 |
+
print(f"[found] -> {output_faiss_path}")
|
163 |
+
else:
|
164 |
+
print("index file already exists, skip")
|
165 |
+
print(f"[found] -> {output_faiss_path}")
|
166 |
+
exit(0)
|
167 |
+
|
168 |
+
|
169 |
+
pbar = tqdm(total=len(input_embs_npz))
|
170 |
+
emb_total = 0
|
171 |
+
for idx, npz_file in enumerate(input_embs_npz):
|
172 |
+
with np.load(npz_file) as data:
|
173 |
+
embs = data["embs"].astype("float32")
|
174 |
+
if idx == 0:
|
175 |
+
dim = embs.shape[1]
|
176 |
+
if faiss_config.nlist is None:
|
177 |
+
faiss_config.nlist = int(np.sqrt(len(embs) * len(input_embs_npz)))
|
178 |
+
print(f"faiss_config: {faiss_config}")
|
179 |
+
if args.use_gpu:
|
180 |
+
print("use gpu for faiss index")
|
181 |
+
faiss_index = gen_faiss_index(faiss_config, dim, args.use_gpu)
|
182 |
+
faiss_index.train(embs) # type: ignore
|
183 |
+
faiss_index.add(embs) # type: ignore
|
184 |
+
pbar.update(1)
|
185 |
+
emb_total += len(embs)
|
186 |
+
pbar.set_description(f"added embs: {emb_total}")
|
187 |
+
pbar.close()
|
188 |
+
|
189 |
+
faiss_index.nprobe = 10 # type: ignore
|
190 |
+
if args.use_gpu:
|
191 |
+
faiss_index = faiss.index_gpu_to_cpu(faiss_index) # type: ignore
|
192 |
+
faiss.write_index(faiss_index, str(output_faiss_path)) # type: ignore
|
193 |
+
print("output faiss index file:", output_faiss_path)
|