hotchpotch
commited on
Commit
•
f023a57
1
Parent(s):
87e804d
WIP
Browse files- embs_to_faiss.py +67 -25
embs_to_faiss.py
CHANGED
@@ -119,25 +119,55 @@ def gen_index_filename(config: FaissConfig, target_local_ds: str) -> str:
|
|
119 |
|
120 |
|
121 |
def gen_faiss_index(config: FaissConfig, dim: str, use_gpu: bool):
|
122 |
-
|
|
|
|
|
123 |
if not config.quantization:
|
124 |
-
faiss_index =
|
125 |
else:
|
|
|
|
|
|
|
|
|
|
|
|
|
126 |
faiss_index = faiss.IndexIVFPQ(
|
127 |
-
|
128 |
dim,
|
129 |
config.nlist,
|
130 |
config.m,
|
131 |
config.mbit,
|
132 |
)
|
133 |
-
|
134 |
-
|
|
|
|
|
|
|
135 |
faiss_index = faiss.index_cpu_to_gpu(gpu_res, 0, faiss_index)
|
136 |
return faiss_index
|
137 |
else:
|
138 |
return faiss_index
|
139 |
|
140 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
output_faiss_path = Path(
|
142 |
"/".join(
|
143 |
[
|
@@ -160,30 +190,42 @@ if output_faiss_path.exists():
|
|
160 |
exit(0)
|
161 |
|
162 |
|
163 |
-
pbar = tqdm(total=len(input_embs_npz))
|
164 |
-
emb_total = 0
|
165 |
-
|
|
|
166 |
with np.load(npz_file) as data:
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
184 |
|
185 |
faiss_index.nprobe = 10 # type: ignore
|
186 |
if args.use_gpu:
|
187 |
faiss_index = faiss.index_gpu_to_cpu(faiss_index) # type: ignore
|
188 |
faiss.write_index(faiss_index, str(output_faiss_path)) # type: ignore
|
189 |
print("output faiss index file:", output_faiss_path)
|
|
|
|
|
|
|
|
|
|
|
|
119 |
|
120 |
|
121 |
def gen_faiss_index(config: FaissConfig, dim: str, use_gpu: bool):
|
122 |
+
if use_gpu and config.m > 48:
|
123 |
+
return gen_faiss_index_f16_lookup(config, dim, use_gpu)
|
124 |
+
# quantizer = faiss.IndexFlatL2(dim)
|
125 |
if not config.quantization:
|
126 |
+
faiss_index = faiss.IndexFlatL2(dim)
|
127 |
else:
|
128 |
+
# faiss_index = faiss.IndexHNSWSQ(dim, faiss.ScalarQuantizer.QT_8bit, 16)
|
129 |
+
# faiss_index.hnsw.efConstruction = 512
|
130 |
+
# index.hnsw.efSearch = 128
|
131 |
+
# quantizer = faiss.IndexHNSWFlat(dim, 32)
|
132 |
+
quantizer = faiss.IndexFlatL2(dim)
|
133 |
+
|
134 |
faiss_index = faiss.IndexIVFPQ(
|
135 |
+
quantizer,
|
136 |
dim,
|
137 |
config.nlist,
|
138 |
config.m,
|
139 |
config.mbit,
|
140 |
)
|
141 |
+
# faiss_index = faiss.IndexIVFFlat(quantizer, dim, 16384)
|
142 |
+
# faiss_index.cp.min_points_per_centroid = 5 # quiet warning
|
143 |
+
# faiss_index.quantizer_trains_alone = 2
|
144 |
+
if use_gpu and getattr(faiss, "StandardGpuResources", None):
|
145 |
+
gpu_res = faiss.StandardGpuResources()
|
146 |
faiss_index = faiss.index_cpu_to_gpu(gpu_res, 0, faiss_index)
|
147 |
return faiss_index
|
148 |
else:
|
149 |
return faiss_index
|
150 |
|
151 |
|
152 |
+
def gen_faiss_index_f16_lookup(config: FaissConfig, dim: str, use_gpu: bool):
|
153 |
+
# use float16 lookup tables
|
154 |
+
gpu_resource = faiss.StandardGpuResources() # GPUリソースの初期化
|
155 |
+
gpu_index_config = faiss.GpuIndexIVFPQConfig() # IVFPQインデックスの設定用オブジェクト
|
156 |
+
gpu_index_config.useFloat16LookupTables = True # Float16ルックアップテーブルを使用する
|
157 |
+
quantizer = faiss.IndexFlatL2(dim) # 量子化器の定義
|
158 |
+
index = faiss.GpuIndexIVFPQ(
|
159 |
+
gpu_resource,
|
160 |
+
# quantizer,
|
161 |
+
dim,
|
162 |
+
config.nlist,
|
163 |
+
config.m,
|
164 |
+
config.mbit,
|
165 |
+
faiss.METRIC_L2,
|
166 |
+
gpu_index_config, # IVFPQインデックスの設定用オブジェクト
|
167 |
+
) # GPU上のIVFPQインデックスの作成
|
168 |
+
return index
|
169 |
+
|
170 |
+
|
171 |
output_faiss_path = Path(
|
172 |
"/".join(
|
173 |
[
|
|
|
190 |
exit(0)
|
191 |
|
192 |
|
193 |
+
# pbar = tqdm(total=len(input_embs_npz))
|
194 |
+
# emb_total = 0
|
195 |
+
all_embs = []
|
196 |
+
for idx, npz_file in enumerate(tqdm(input_embs_npz)):
|
197 |
with np.load(npz_file) as data:
|
198 |
+
e = data["embs"].astype("float16")
|
199 |
+
all_embs.append(e)
|
200 |
+
embs: np.ndarray = np.concatenate(all_embs, axis=0, dtype="float32")
|
201 |
+
del all_embs
|
202 |
+
|
203 |
+
# if idx == 0:
|
204 |
+
dim = embs.shape[1]
|
205 |
+
if faiss_config.nlist is None:
|
206 |
+
faiss_config.nlist = int(np.sqrt(len(embs) * (len(input_embs_npz) - 1)))
|
207 |
+
if faiss_config.nlist < 1:
|
208 |
+
faiss_config.nlist = 100
|
209 |
+
print(f"faiss_config: {faiss_config}")
|
210 |
+
if args.use_gpu:
|
211 |
+
print("use gpu for faiss index")
|
212 |
+
faiss_index = gen_faiss_index(faiss_config, dim, args.use_gpu)
|
213 |
+
print(f"start training faiss index, shape: {embs.shape}")
|
214 |
+
faiss_index.train(embs) # type: ignore
|
215 |
+
print(f"start adding embs to faiss index")
|
216 |
+
faiss_index.add(embs) # type: ignore
|
217 |
+
# pbar.update(1)
|
218 |
+
print(f"added embs: {embs.shape[0]}")
|
219 |
+
# pbar.set_description(f"added embs: {emb_total}")
|
220 |
+
# pbar.close()
|
221 |
|
222 |
faiss_index.nprobe = 10 # type: ignore
|
223 |
if args.use_gpu:
|
224 |
faiss_index = faiss.index_gpu_to_cpu(faiss_index) # type: ignore
|
225 |
faiss.write_index(faiss_index, str(output_faiss_path)) # type: ignore
|
226 |
print("output faiss index file:", output_faiss_path)
|
227 |
+
# output_faiss_path のファイルサイズを MB で表示
|
228 |
+
print(
|
229 |
+
"output faiss index file size (MB):",
|
230 |
+
int(output_faiss_path.stat().st_size / 1024 / 1024),
|
231 |
+
)
|