-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmodule.py
More file actions
569 lines (509 loc) · 14.3 KB
/
Copy pathmodule.py
File metadata and controls
569 lines (509 loc) · 14.3 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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
import logging
from typing import Any, Dict, Mapping, Optional
import numpy as np
import tiledb
from tiledb.vector_search import _tiledbvspy as vspy
from tiledb.vector_search._tiledbvspy import *
def load_as_matrix(
path: str,
ctx: "Ctx" = None,
config: Optional[Mapping[str, Any]] = None,
size: Optional[int] = None,
timestamp: int = 0,
):
"""
Load array as Matrix class. We read in all rows (i.e. from 0 to the row domain length).
Parameters
----------
path: str
Array path
ctx: vspy.Ctx
The vspy Context
size: int
Size of vectors to load. If not set we will read from 0 to the column domain length.
"""
# If the user passes a tiledb python Config object convert to a dictionary
if isinstance(config, tiledb.Config):
config = dict(config)
print("[module@load_as_matrix] size", size, type(size))
if ctx is None:
ctx = vspy.Ctx(config)
a = tiledb.ArraySchema.load(path, ctx=tiledb.Ctx(config))
dtype = a.attr(0).dtype
print("[module@load_as_matrix] dtype", dtype)
# Read all rows from column 0 -> `size`. Set no upper_bound. Note that if `size` is None then
# we'll read to the column domain length.
if dtype == np.float32:
m = tdbColMajorMatrix_f32(ctx, path, 0, None, 0, size, 0, timestamp)
elif dtype == np.float64:
m = tdbColMajorMatrix_f64(ctx, path, 0, None, 0, size, 0, timestamp)
elif dtype == np.int32:
m = tdbColMajorMatrix_i32(ctx, path, 0, None, 0, size, 0, timestamp)
elif dtype == np.int32:
m = tdbColMajorMatrix_i64(ctx, path, 0, None, 0, size, 0, timestamp)
elif dtype == np.uint8:
m = tdbColMajorMatrix_u8(ctx, path, 0, None, 0, size, 0, timestamp)
elif dtype == np.int8:
m = tdbColMajorMatrix_i8(ctx, path, 0, None, 0, size, 0, timestamp)
# elif dtype == np.uint64:
# return tdbColMajorMatrix_u64(ctx, path, size, timestamp)
else:
raise ValueError("Unsupported Matrix dtype: {}".format(a.attr(0).dtype))
m.load()
return m
def load_as_array(
path,
return_matrix: bool = False,
ctx: "Ctx" = None,
config: Optional[Mapping[str, Any]] = None,
size: Optional[int] = None,
):
"""
Load array as array class
Parameters
----------
path: str
Array path
return_matrix: bool
Return matrix
config: Dict
TileDB configuration parameters
"""
m = load_as_matrix(path, size=size, ctx=ctx, config=config)
r = np.array(m, copy=False)
# hang on to a copy for testing purposes, for now
if return_matrix:
return r, m
else:
return r
def debug_slice(m: "colMajorMatrix", name: str):
dtype = m.dtype
if dtype == np.float32:
return debug_matrix_f32(m, name)
elif dtype == np.uint8:
return debug_matrix_u8(m, name)
elif dtype == np.int8:
return debug_matrix_i8(m, name)
elif dtype == np.uint64:
return debug_matrix_u64(m, name)
else:
raise TypeError(f"Unsupported type: {dtype}!")
def query_vq_nth(db: "colMajorMatrix", *args):
"""
Run vector query
Parameters
----------
db: colMajorMatrix
Open Matrix class from load_as_matrix
args:
Args for query
"""
if db.dtype == np.float32:
return query_vq_f32(db, *args)
elif db.dtype == np.uint8:
return query_vq_u8(db, *args)
elif db.dtype == np.int8:
return query_vq_i8(db, *args)
else:
raise TypeError("Unknown type!")
def query_vq_heap(db: "colMajorMatrix", *args):
"""
Run vector query
Parameters
----------
db: colMajorMatrix
Open Matrix class from load_as_matrix
args:
Args for query
"""
if db.dtype == np.float32:
return vq_query_heap_f32(db, *args)
elif db.dtype == np.uint8:
return vq_query_heap_u8(db, *args)
elif db.dtype == np.int8:
return vq_query_heap_i8(db, *args)
else:
raise TypeError("Unknown type!")
def query_vq_heap_pyarray(db: "colMajorMatrix", *args):
"""
Run vector query
Parameters
----------
db: colMajorMatrix
Open Matrix class from load_as_matrix
args:
Args for query
"""
if db.dtype == np.float32:
return vq_query_heap_pyarray_f32(db, *args)
elif db.dtype == np.uint8:
return vq_query_heap_pyarray_u8(db, *args)
elif db.dtype == np.int8:
return vq_query_heap_pyarray_i8(db, *args)
else:
raise TypeError("Unknown type!")
def ivf_index_tdb(
dtype: np.dtype,
db_uri: str,
external_ids_uri: str,
deleted_ids: "Vector",
centroids_uri: str,
parts_uri: str,
index_array_uri: str,
id_uri: str,
start: int = 0,
end: int = 0,
partition_start: int = 0,
nthreads: int = 0,
timestamp: int = 0,
config: Dict = None,
):
if config is None:
ctx = vspy.Ctx({})
else:
ctx = vspy.Ctx(config)
args = tuple(
[
ctx,
db_uri,
external_ids_uri,
deleted_ids,
centroids_uri,
parts_uri,
index_array_uri,
id_uri,
start,
end,
nthreads,
timestamp,
partition_start,
]
)
if dtype == np.float32:
return ivf_index_tdb_f32(*args)
elif dtype == np.uint8:
return ivf_index_tdb_u8(*args)
elif dtype == np.int8:
return ivf_index_tdb_i8(*args)
else:
raise TypeError("Unknown type!")
def ivf_index(
dtype: np.dtype,
db: "colMajorMatrix",
external_ids: "Vector",
deleted_ids: "Vector",
centroids_uri: str,
parts_uri: str,
index_array_uri: str,
id_uri: str,
start: int = 0,
end: int = 0,
partition_start: int = 0,
nthreads: int = 0,
timestamp: int = 0,
config: Dict = None,
):
if config is None:
ctx = vspy.Ctx({})
else:
ctx = vspy.Ctx(config)
args = tuple(
[
ctx,
db,
external_ids,
deleted_ids,
centroids_uri,
parts_uri,
index_array_uri,
id_uri,
start,
end,
nthreads,
timestamp,
partition_start,
]
)
if dtype == np.float32:
return ivf_index_f32(*args)
elif dtype == np.uint8:
return ivf_index_u8(*args)
elif dtype == np.int8:
return ivf_index_i8(*args)
else:
raise TypeError("Unknown type!")
def ivf_query_ram(
dtype: np.dtype,
parts_db: "colMajorMatrix",
centroids_db: "colMajorMatrix",
query_vectors: "colMajorMatrix",
indices: "Vector",
ids: "Vector",
nprobe: int,
k_nn: int,
nthreads: int,
ctx: "Ctx" = None,
use_nuv_implementation: bool = False,
):
"""
Run IVF vector query using infinite RAM
Parameters
----------
dtype: numpy.dtype
Type of vector, float32 or uint8
parts_db: colMajorMatrix
Partitioned vectors
centroids_db: colMajorMatrix
Open Matrix class from load_as_matrix for centroids
query_vectors: colMajorMatrix
Open Matrix class from load_as_matrix for queries
indices: Vector
Partition indices
ids: Vector
Vector IDs
nprobe: int
Number of probs
k_nn: int
Number of nn
nthreads: int
Number of theads
ctx: vspy.Ctx
The vspy Context
"""
if ctx is None:
ctx = vspy.Ctx({})
args = tuple(
[
parts_db,
centroids_db,
query_vectors,
indices,
ids,
nprobe,
k_nn,
nthreads,
]
)
# logging.info(f">>>> ivf_query_ram len(indices): {len(indices)}, dtype: {dtype}, use_nuv_implementation: {use_nuv_implementation}")
# pdb.set_trace()
if dtype == np.float32:
if use_nuv_implementation:
return nuv_query_heap_infinite_ram_reg_blocked_f32(*args)
else:
return qv_query_heap_infinite_ram_f32(*args)
elif dtype == np.uint8:
if use_nuv_implementation:
return nuv_query_heap_infinite_ram_reg_blocked_u8(*args)
else:
return qv_query_heap_infinite_ram_u8(*args)
elif dtype == np.int8:
if use_nuv_implementation:
return nuv_query_heap_infinite_ram_reg_blocked_i8(*args)
else:
return qv_query_heap_infinite_ram_i8(*args)
else:
raise TypeError("Unknown type!")
def ivf_query(
dtype: np.dtype,
parts_uri: str,
centroids: "colMajorMatrix",
query_vectors: "colMajorMatrix",
indices: "Vector",
ids_uri: str,
nprobe: int,
k_nn: int,
memory_budget: int,
nthreads: int,
ctx: "Ctx" = None,
use_nuv_implementation: bool = False,
timestamp: int = 0,
):
"""
Run IVF vector query using a memory budget
Parameters
----------
dtype: numpy.dtype
Type of vector, float32 or uint8
parts_uri: str
Partition URI
centroids: colMajorMatrix
Open Matrix class from load_as_matrix for centroids
query_vectors: colMajorMatrix
Open Matrix class from load_as_matrix for queries
indeces: Vector
Vectors
ids_uri: str
URI for id mappings
nprobe: int
Number of probs
k_nn: int
Number of nn
memory_budget: int
Main memory budget
nthreads: int
Number of theads
ctx: vspy.Ctx
The vspy Context
timestamp: int
Read timestamp
"""
if ctx is None:
ctx = vspy.Ctx({})
args = tuple(
[
ctx,
parts_uri,
centroids,
query_vectors,
indices,
ids_uri,
nprobe,
k_nn,
memory_budget,
nthreads,
timestamp,
]
)
logging.info(
f">>>> module.py: ivf_query_ram len(indices): {len(indices)}, dtype: {dtype}, use_nuv_implementation: {use_nuv_implementation}"
)
if dtype == np.float32:
if use_nuv_implementation:
return nuv_query_heap_finite_ram_reg_blocked_f32(*args)
else:
return qv_query_heap_finite_ram_f32(*args)
elif dtype == np.uint8:
if use_nuv_implementation:
return nuv_query_heap_finite_ram_reg_blocked_u8(*args)
else:
return qv_query_heap_finite_ram_u8(*args)
elif dtype == np.int8:
if use_nuv_implementation:
return nuv_query_heap_finite_ram_reg_blocked_i8(*args)
else:
return qv_query_heap_finite_ram_i8(*args)
else:
raise TypeError("Unknown type!")
def partition_ivf_index(centroids, query, nprobe=1, nthreads=0):
if query.dtype == np.float32:
return partition_ivf_index_f32(centroids, query, nprobe, nthreads)
elif query.dtype == np.uint8:
return partition_ivf_index_u8(centroids, query, nprobe, nthreads)
elif query.dtype == np.int8:
return partition_ivf_index_i8(centroids, query, nprobe, nthreads)
else:
raise TypeError("Unsupported type!")
def dist_qv(
dtype: np.dtype,
parts_uri: str,
ids_uri: str,
query_vectors: "colMajorMatrix",
active_partitions: np.array,
active_queries: np.array,
indices: np.array,
k_nn: int,
ctx: "Ctx" = None,
timestamp: int = 0,
upper_bound: int = 0,
):
if ctx is None:
ctx = vspy.Ctx({})
args = tuple(
[
ctx, # 0
parts_uri, # 1
StdVector_u64(active_partitions), # 2
query_vectors, # 3
active_queries, # 4
StdVector_u64(indices),
ids_uri,
k_nn,
timestamp,
upper_bound,
]
)
# logging.info(f">>>> ivf_query_ram len(indices): {len(indices)}, dtype: {dtype},")
# pdb.set_trace()
if dtype == np.float32:
return dist_qv_f32(*args)
elif dtype == np.uint8:
return dist_qv_u8(*args)
elif dtype == np.int8:
return dist_qv_i8(*args)
else:
raise TypeError("Unsupported type!")
def validate_top_k(results: np.ndarray, ground_truth: np.ndarray):
if results.dtype == np.uint64:
return validate_top_k_u64(results, ground_truth)
else:
raise TypeError("Unknown type for validate_top_k!")
def array_to_matrix(array: np.ndarray):
if array.dtype == np.float32:
return pyarray_copyto_matrix_f32(array)
elif array.dtype == np.float64:
return pyarray_copyto_matrix_f64(array)
elif array.dtype == np.uint8:
return pyarray_copyto_matrix_u8(array)
elif array.dtype == np.int32:
return pyarray_copyto_matrix_i32(array)
elif array.dtype == np.uint64:
return pyarray_copyto_matrix_u64(array)
elif array.dtype == np.int8:
return pyarray_copyto_matrix_i8(array)
else:
raise TypeError("Unsupported type!")
def kmeans_fit(
partitions: int,
init: str,
max_iter: int,
verbose: bool,
n_init: int,
sample_vectors: "colMajorMatrix",
tol: Optional[float] = None,
nthreads: Optional[int] = None,
seed: Optional[int] = None,
):
args = tuple(
[
partitions,
init,
max_iter,
verbose,
n_init,
sample_vectors,
tol,
nthreads,
seed,
]
)
if sample_vectors.dtype == np.float32:
return kmeans_fit_f32(*args)
else:
raise TypeError("Unsupported type!")
def kmeans_predict(centroids: "colMajorMatrix", sample_vectors: "colMajorMatrix"):
args = tuple(
[
centroids,
sample_vectors,
]
)
if sample_vectors.dtype == np.float32:
return kmeans_predict_f32(*args)
else:
raise TypeError("Unsupported type!")
# TODO
# def load_partitioned(uri, partitions, dtype: Optional[np.dtype] = None):
# if dtype is None:
# arr = tiledb.open(uri).dtype
# if dtype == np.float32:
# return tdbPartitionedMatrix_f32(uri,
# elif dtype == np.uint8:
# return tdbPartitionedMatrix_f32(uri,
# else:
# raise TypeError("Unknown type!")
# class PartitionedMatrix:
# def __init__(self, uri, partitions):
# self.uri = uri
# self.partitions = partitions
#
# self._m = load_partitioned(uri, partitions)