|
| 1 | +import os |
| 2 | +import numpy as np |
| 3 | + |
| 4 | +from neurips23.filter.base import BaseFilterANN |
| 5 | +from benchmark.datasets import DATASETS |
| 6 | + |
| 7 | +import pys2 |
| 8 | + |
| 9 | +class PineconeIndex(BaseFilterANN): |
| 10 | + |
| 11 | + def __init__(self, metric, index_params): |
| 12 | + self._index_params = index_params |
| 13 | + self._metric = metric |
| 14 | + print(index_params) |
| 15 | + self.indexkey = index_params.get("indexkey", "FilterIVFFlatU8") |
| 16 | + self.nt = index_params.get("threads", 1) |
| 17 | + self.qas = {} |
| 18 | + |
| 19 | + def fit(self, dataset): |
| 20 | + ds = DATASETS[dataset]() |
| 21 | + |
| 22 | + if ds.search_type() != "knn_filtered": |
| 23 | + raise NotImplementedError() |
| 24 | + |
| 25 | + print(f"Building index") |
| 26 | + index = pys2.FilterIndexWrapper(ds.d, |
| 27 | + self.indexkey, |
| 28 | + self._index_params, |
| 29 | + ds.get_dataset_fn(), |
| 30 | + os.path.join(ds.basedir, ds.ds_metadata_fn)) |
| 31 | + |
| 32 | + self.index = index |
| 33 | + |
| 34 | + def load_index(self, dataset): |
| 35 | + """ |
| 36 | + Load the index for dataset. Returns False if index |
| 37 | + is not available, True otherwise. |
| 38 | +
|
| 39 | + Checking the index usually involves the dataset name |
| 40 | + and the index build parameters passed during construction. |
| 41 | +
|
| 42 | + If the file does not exist, there is an option to download it from a public url |
| 43 | + """ |
| 44 | + filename = dataset + '.index' |
| 45 | + |
| 46 | + if not os.path.exists(filename): |
| 47 | + return False |
| 48 | + |
| 49 | + print("Loading index from " + filename) |
| 50 | + self.index = pys2.load_filter_ivf_index(filename) |
| 51 | + return True |
| 52 | + |
| 53 | + |
| 54 | + def index_files_to_store(self, dataset): |
| 55 | + """ |
| 56 | + Specify a triplet with the local directory path of index files, |
| 57 | + the common prefix name of index component(s) and a list of |
| 58 | + index components that need to be uploaded to (after build) |
| 59 | + or downloaded from (for search) cloud storage. |
| 60 | +
|
| 61 | + For local directory path under docker environment, please use |
| 62 | + a directory under |
| 63 | + data/indices/track(T1 or T2)/algo.__str__()/DATASETS[dataset]().short_name() |
| 64 | + """ |
| 65 | + raise NotImplementedError() |
| 66 | + |
| 67 | + def query(self, X, k): |
| 68 | + raise NotImplementedError() |
| 69 | + |
| 70 | + def filtered_query(self, X, filter, k): |
| 71 | + |
| 72 | + if (X.dtype.kind == 'f'): |
| 73 | + print('data type of X is ' + str(X.dtype)) |
| 74 | + X = X*10 + 128 |
| 75 | + X = X.astype(np.uint8) |
| 76 | + padding_size = 192 - X.shape[1] |
| 77 | + X = np.pad(X, ((0, 0), (0, padding_size)), mode='constant') |
| 78 | + |
| 79 | + |
| 80 | + results_tuple = self.index.search_parallel(X, filter.indptr, filter.indices, k) # this returns a tuple: (results_array, query_time, post_processing_time) |
| 81 | + self.I = results_tuple[0] |
| 82 | + print("query and postprocessing times: ", results_tuple[1:]) |
| 83 | + |
| 84 | + |
| 85 | + def get_results(self): |
| 86 | + return self.I |
| 87 | + |
| 88 | + def set_query_arguments(self, query_args): |
| 89 | + self.qas = query_args |
| 90 | + print("setting query args:" + str(self.qas)) |
| 91 | + |
| 92 | + if "skip_clustering_threshold" in query_args: |
| 93 | + self.skip_clustering_threshold = query_args['skip_clustering_threshold'] |
| 94 | + self.index.set_search_param('skip_clustering_threshold', str(self.skip_clustering_threshold)) |
| 95 | + self.qas = query_args |
| 96 | + else: |
| 97 | + self.skip_clustering_threshold = 0 |
| 98 | + |
| 99 | + if "fraction_coefficient" in query_args: |
| 100 | + self.fraction_coefficient = query_args['fraction_coefficient'] |
| 101 | + self.index.set_search_param('fraction_coefficient', str(self.fraction_coefficient)) |
| 102 | + self.qas = query_args |
| 103 | + else: |
| 104 | + self.fraction_coefficient = 18.0 |
| 105 | + |
| 106 | + if "fraction_exponent" in query_args: |
| 107 | + self.fraction_exponent = query_args['fraction_exponent'] |
| 108 | + self.index.set_search_param('fraction_exponent', str(self.fraction_exponent)) |
| 109 | + self.qas = query_args |
| 110 | + else: |
| 111 | + self.fraction_coefficient = 0.65 |
| 112 | + |
| 113 | + |
| 114 | + def __str__(self): |
| 115 | + return f'pinecone_filter({self.indexkey, self._index_params, self.qas})' |
| 116 | + |
| 117 | + |
0 commit comments