Repository navigation
Expand file tree
/
Copy pathmodule.py
More file actions
326 lines (277 loc) · 12.1 KB
/
Copy pathmodule.py
File metadata and controls
326 lines (277 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import time
from pathlib import Path
import numpy as np
import deglib
# Try relative import when executed inside VIBE framework (vibe/algorithms/deg/module.py)
try:
from ..base.module import BaseANN
except (ImportError, ValueError):
class BaseANN:
pass
_METRIC_MAP = {
"euclidean": (deglib.Metric.FP32_L2, deglib.Metric.Int8_L2, deglib.Metric.FP16_L2),
"cosine": (deglib.Metric.FP32_InnerProduct, deglib.Metric.Int8_InnerProduct, deglib.Metric.FP16_InnerProduct),
"ip": (deglib.Metric.FP32_InnerProduct, deglib.Metric.Int8_InnerProduct, deglib.Metric.FP16_InnerProduct),
"normalized": (deglib.Metric.FP32_InnerProduct, deglib.Metric.Int8_InnerProduct, deglib.Metric.FP16_InnerProduct),
}
def _resolve_graph_cache_path(
metric: str,
k: int,
opt_target: str,
n_vectors: int,
dims: int,
extend_k: int | None = None,
extend_eps: float = 0.1,
cache_dir: Path | None = None,
) -> Path:
"""Resolves standard cache path using get_default_cache_dir()."""
dataset_name = f"corpus_{n_vectors}_{dims}d"
if cache_dir is None:
try:
from dataset import VIBE_DATASETS, get_default_cache_dir
cache_dir = get_default_cache_dir()
for key, meta in VIBE_DATASETS.items():
if meta.get("dim") == dims and abs(meta.get("size", 0) - n_vectors) < 1000:
dataset_name = key
break
except ImportError:
cache_dir = Path.home() / ".cache" / "deg_datasets"
else:
try:
from dataset import VIBE_DATASETS
for key, meta in VIBE_DATASETS.items():
if meta.get("dim") == dims and abs(meta.get("size", 0) - n_vectors) < 1000:
dataset_name = key
break
except ImportError:
pass
deg_dir = cache_dir / dataset_name / "deg"
deg_dir.mkdir(parents=True, exist_ok=True)
build_metric_str = deglib.Metric.FP32_L2.name if metric == "euclidean" else deglib.Metric.FP32_InnerProduct.name
ext_k = extend_k if extend_k is not None else k * 2
# 1. Preferred filename with AddK
filename = f"{dims}D_{build_metric_str}_K{k}_AddK{ext_k}Eps{extend_eps:.1f}_{opt_target}_FLAS.deg"
full_path = deg_dir / filename
if full_path.exists():
return full_path
# 2. Check alternative without AddK
alt_filename = f"{dims}D_{build_metric_str}_K{k}_{opt_target}_FLAS.deg"
if (deg_dir / alt_filename).exists():
return deg_dir / alt_filename
# 3. Check for any AddK variations (e.g. AddK60)
matches = list(deg_dir.glob(f"{dims}D_{build_metric_str}_K{k}_AddK*_{opt_target}_FLAS.deg"))
if matches:
return matches[0]
return full_path
class DEG(BaseANN):
"""
Dynamic Exploration Graph (DEG) using Float32 throughout.
"""
def __init__(
self,
metric: str,
k: int = 30,
opt_target: str = "LowLID",
prune_non_mrng: bool = False,
threads: int = 1,
):
self.metric = metric.lower().strip()
if self.metric not in _METRIC_MAP:
raise ValueError(f"Unsupported metric '{self.metric}'. Choose from: {list(_METRIC_MAP.keys())}")
self.k = int(k)
self.opt_target = opt_target
self.prune_non_mrng = bool(prune_non_mrng)
self.threads = int(threads)
self.eps_or_ef = 0.1
self.metric_enum = _METRIC_MAP[self.metric][0]
self.opt_enum = deglib.builder.OptimizationTarget[self.opt_target]
self.graph = None
self.searcher = None
def fit(self, X: np.ndarray, cache_dir: Path | None = None):
"""Builds or loads the DEG graph in FP32."""
if self.metric == "cosine":
X = X / np.linalg.norm(X, axis=1)[:, np.newaxis]
X = np.ascontiguousarray(X, dtype=np.float32)
n_vectors, dims = X.shape
cache_file = _resolve_graph_cache_path(
metric=self.metric,
k=self.k,
opt_target=self.opt_target,
n_vectors=n_vectors,
dims=dims,
cache_dir=cache_dir,
)
if cache_file.exists():
print(f"Loading cached DEG graph from {cache_file}...", flush=True)
load_fn = deglib.load_mutable_graph if self.prune_non_mrng else deglib.load_readonly_graph
graph = load_fn(str(cache_file))
else:
# 1. FLAS 1D Pre-sorting
print(f"Running FLAS 1D Pre-sorting (threads={self.threads})...", flush=True)
sorted_indices = deglib.optimization.presort(
X,
metric=self.metric_enum,
threads=self.threads,
show_progress=True,
)
# 2. Build graph in FP32
print(f"Building DEG graph (K={self.k}, Opt={self.opt_target}, threads={self.threads})...", flush=True)
graph = deglib.builder.build_from_data(
data=X[sorted_indices],
labels=sorted_indices,
edges_per_vertex=self.k,
metric=self.metric_enum,
seed=7,
optimization_target=self.opt_enum,
thread_count=self.threads,
show_progress=True,
)
print(f"Saving graph to cache {cache_file}...", flush=True)
graph.save_graph(str(cache_file))
# 3. Optional MRNG edge pruning
if self.prune_non_mrng:
deglib.optimization.prune_non_mrng_edges(graph, num_threads=1)
self.graph = graph.to_readonly() if graph.is_mutable() else graph
self.searcher = deglib.search.create_searcher(graph=self.graph)
# 4s. Optimize entry vertices and prefetch values
t_km = time.time()
self.searcher.optimize()
print(f"Optimized Searcher for the provided graph and hardware in {time.time() - t_km:.2f}s", flush=True)
def set_query_arguments(self, eps_or_ef: float | int):
"""Sets query-time parameter: values >= 1.0 are treated as ef, < 1.0 as eps."""
self.eps_or_ef = float(eps_or_ef)
def query(self, v: np.ndarray, n: int) -> np.ndarray:
"""Single query search on 1 thread with Float32 via C++ searcher."""
if self.metric == "cosine":
v = v / np.linalg.norm(v)
return self.searcher.search(
np.ascontiguousarray(v, dtype=np.float32),
k=n,
eps_or_ef=self.eps_or_ef,
threads=1,
return_distances=False,
unsorted=True,
)
def __str__(self) -> str:
if self.eps_or_ef >= 1.0:
return f"DEG(k={self.k}, opt={self.opt_target}, prune_mrng={self.prune_non_mrng}, ef={int(round(self.eps_or_ef))})"
return f"DEG(k={self.k}, opt={self.opt_target}, prune_mrng={self.prune_non_mrng}, eps={self.eps_or_ef})"
class QG(BaseANN):
"""
Quantized DEG (DEG-QG): Graph search with INT8 quantized vectors and FP16 reranking.
"""
def __init__(
self,
metric: str,
k: int = 30,
opt_target: str = "LowLID",
prune_non_mrng: bool = False,
threads: int = 1,
):
self.metric = metric.lower().strip()
if self.metric not in _METRIC_MAP:
raise ValueError(f"Unsupported metric '{self.metric}'. Choose from: {list(_METRIC_MAP.keys())}")
self.k = int(k)
self.opt_target = opt_target
self.prune_non_mrng = bool(prune_non_mrng)
self.threads = int(threads)
self.rerank_size_factor = 1.0
self.search_eps = 0.0
self.ef = 0
self.base_metric, self.int8_metric, self.fp16_metric = _METRIC_MAP[self.metric]
self.opt_enum = deglib.builder.OptimizationTarget[self.opt_target]
self.graph = None
self.searcher = None
self.quantizer = None
self.original_features_fp16 = None
self.rerank_space_fp16 = None
def fit(self, X: np.ndarray, cache_dir: Path | None = None):
"""Builds DEG graph, quantizes vectors to INT8 using ScalarQuantizer, and prepares C++ searcher."""
if self.metric == "cosine":
X = X / np.linalg.norm(X, axis=1)[:, np.newaxis]
X = np.ascontiguousarray(X, dtype=np.float32)
n_vectors, dims = X.shape
self.original_features_fp16 = deglib.distances.floats_to_fp16(X)
self.rerank_space_fp16 = deglib.FloatSpace.create(dim=dims, metric=self.fp16_metric)
cache_file = _resolve_graph_cache_path(
metric=self.metric,
k=self.k,
opt_target=self.opt_target,
n_vectors=n_vectors,
dims=dims,
cache_dir=cache_dir,
)
if cache_file.exists():
print(f"Loading cached DEG graph from {cache_file}...", flush=True)
load_fn = deglib.load_mutable_graph if self.prune_non_mrng else deglib.load_readonly_graph
loaded_graph = load_fn(str(cache_file))
else:
# 1. FLAS 1D Pre-sorting
print(f"Running FLAS 1D Pre-sorting (threads={self.threads})...", flush=True)
sorted_indices = deglib.optimization.presort(
X,
metric=self.base_metric,
threads=self.threads,
show_progress=True,
)
# 2. Build graph in FP32
print(f"Building DEG graph (K={self.k}, Opt={self.opt_target}, threads={self.threads})...", flush=True)
graph = deglib.builder.build_from_data(
data=X[sorted_indices],
labels=sorted_indices,
edges_per_vertex=self.k,
metric=self.base_metric,
seed=7,
optimization_target=self.opt_enum,
thread_count=self.threads,
show_progress=True,
)
print(f"Saving graph to cache {cache_file}...", flush=True)
graph.save_graph(str(cache_file))
loaded_graph = graph
# 3. Optional MRNG edge pruning
if self.prune_non_mrng:
deglib.optimization.prune_non_mrng_edges(loaded_graph, num_threads=1)
# 4. Finalize ReadOnlyGraph with INT8 features using calibrated ScalarQuantizer
self.quantizer = deglib.optimization.make_scalar_quantizer_int8(X)
int8_features = self.quantizer.quantize(X, num_threads=1)
target_space = deglib.FloatSpace.create(dim=dims, metric=self.int8_metric)
self.graph = loaded_graph.to_readonly(target_space, int8_features)
# 5. Initialize C++ Zero-overhead Searcher
self.searcher = deglib.search.create_searcher(
graph=self.graph,
quantizer=self.quantizer,
refine_space=self.rerank_space_fp16,
refine_data=self.original_features_fp16,
)
# 6. Optimize entry vertices and prefetch values
t_km = time.time()
self.searcher.optimize()
print(f"Optimized Searcher for the provided graph and hardware in {time.time() - t_km:.2f}s", flush=True)
def set_query_arguments(self, eps_or_ef: float | int, rerank_size_factor: float = 1.0):
"""Sets query-time parameters: values >= 1.0 are treated as ef, < 1.0 as eps."""
self.rerank_size_factor = float(rerank_size_factor)
self.eps_or_ef = float(eps_or_ef)
def query(self, v: np.ndarray, n: int) -> np.ndarray:
"""Single query search on 1 thread with INT8 search and FP16 reranking directly in C++."""
if self.metric == "cosine":
v = v / np.linalg.norm(v)
return self.searcher.search(
np.ascontiguousarray(v, dtype=np.float32),
k=n,
eps_or_ef=self.eps_or_ef,
rerank_factor=self.rerank_size_factor,
threads=1,
return_distances=False,
unsorted=True,
)
def __str__(self) -> str:
if self.eps_or_ef >= 1.0:
return (
f"DEG-QG(k={self.k}, opt={self.opt_target}, prune_mrng={self.prune_non_mrng}, "
f"rerank_factor={self.rerank_size_factor}, ef={int(round(self.eps_or_ef))})"
)
return (
f"DEG-QG(k={self.k}, opt={self.opt_target}, prune_mrng={self.prune_non_mrng}, "
f"rerank_factor={self.rerank_size_factor}, eps={self.eps_or_ef})"
)