@@ -26,15 +26,15 @@ class Definition:
26
26
def instantiate_algorithm (definition : Definition ) -> BaseANN :
27
27
"""
28
28
Create a `BaseANN` from a definition.
29
-
29
+
30
30
Args:
31
31
definition (Definition): An object containing information about the algorithm.
32
32
33
33
Returns:
34
34
BaseANN: Instantiated algorithm
35
35
36
36
Note:
37
- The constructors for the algorithm definition are generally located at
37
+ The constructors for the algorithm definition are generally located at
38
38
ann_benchmarks/algorithms/*/module.py.
39
39
"""
40
40
print (f"Trying to instantiate { definition .module } .{ definition .constructor } ({ definition .arguments } )" )
@@ -54,7 +54,7 @@ def algorithm_status(definition: Definition) -> InstantiationStatus:
54
54
"""
55
55
Determine the instantiation status of the algorithm based on its python module and constructor.
56
56
57
- Attempts to find the Python class constructor based on the definition's module path and
57
+ Attempts to find the Python class constructor based on the definition's module path and
58
58
constructor name.
59
59
60
60
Args:
@@ -107,7 +107,7 @@ def _generate_combinations(args: Union[List[Any], Dict[Any, Any]]) -> List[Union
107
107
def _substitute_variables (arg : Any , vs : Dict [str , Any ]) -> Any :
108
108
"""
109
109
Substitutes any string variables present in the argument structure with provided values.
110
-
110
+
111
111
Support for nested substitution in the case `arg` is a List or Dict.
112
112
113
113
Args:
@@ -164,8 +164,8 @@ def _get_definitions(base_dir: str = "ann_benchmarks/algorithms") -> List[Dict[s
164
164
165
165
def _get_algorithm_definitions (point_type : str , distance_metric : str , base_dir : str = "ann_benchmarks/algorithms" ) -> Dict [str , Dict [str , Any ]]:
166
166
"""Get algorithm definitions for a specific point type and distance metric.
167
-
168
- A specific algorithm folder can have multiple algorithm definitions for a given point type and
167
+
168
+ A specific algorithm folder can have multiple algorithm definitions for a given point type and
169
169
metric. For example, `ann_benchmarks.algorithms.nmslib` has two definitions for euclidean float
170
170
data: specifically `SW-graph(nmslib)` and `hnsw(nmslib)`, even though the module is named nmslib.
171
171
@@ -180,7 +180,7 @@ def _get_algorithm_definitions(point_type: str, distance_metric: str, base_dir:
180
180
"disabled": false,
181
181
"docker_tag": ann-benchmarks-nmslib,
182
182
...
183
- },
183
+ },
184
184
'SW-graph(nmslib)': {
185
185
"base_args": ['@metric', sw-graph],
186
186
"constructor": NmslibReuseIndex,
@@ -209,9 +209,9 @@ def _get_algorithm_definitions(point_type: str, distance_metric: str, base_dir:
209
209
def list_algorithms (base_dir : str = "ann_benchmarks/algorithms" ) -> None :
210
210
"""
211
211
Output (to stdout), a list of all algorithms, with their supported point types and metrics.
212
-
212
+
213
213
Args:
214
- base_dir (str, optional): The base directory where the algorithms are stored.
214
+ base_dir (str, optional): The base directory where the algorithms are stored.
215
215
Defaults to "ann_benchmarks/algorithms".
216
216
"""
217
217
all_configs = _get_definitions (base_dir )
@@ -240,7 +240,7 @@ def list_algorithms(base_dir: str = "ann_benchmarks/algorithms") -> None:
240
240
241
241
def generate_arg_combinations (run_group : Dict [str , Any ], arg_type : str ) -> List :
242
242
"""Generate combinations of arguments from a run group for a specific argument type.
243
-
243
+
244
244
Args:
245
245
run_group (Dict[str, Any]): The run group containing argument definitions.
246
246
arg_type (str): The type of argument group to generate combinations for.
@@ -266,10 +266,10 @@ def generate_arg_combinations(run_group: Dict[str, Any], arg_type: str) -> List:
266
266
267
267
268
268
def prepare_args (run_group : Dict [str , Any ]) -> List :
269
- """For an Algorithm's run group, prepare arguments.
270
-
269
+ """For an Algorithm's run group, prepare arguments.
270
+
271
271
An `arg_groups` is preferenced over an `args` key.
272
-
272
+
273
273
Args:
274
274
run_group (Dict[str, Any]): The run group containing argument definitions.
275
275
@@ -287,7 +287,7 @@ def prepare_args(run_group: Dict[str, Any]) -> List:
287
287
288
288
def prepare_query_args (run_group : Dict [str , Any ]) -> List :
289
289
"""For an algorithm's run group, prepare query args/ query arg groups.
290
-
290
+
291
291
Args:
292
292
run_group (Dict[str, Any]): The run group containing argument definitions.
293
293
@@ -303,28 +303,28 @@ def prepare_query_args(run_group: Dict[str, Any]) -> List:
303
303
def create_definitions_from_algorithm (name : str , algo : Dict [str , Any ], dimension : int , distance_metric : str = "euclidean" , count : int = 10 ) -> List [Definition ]:
304
304
"""
305
305
Create definitions from an indvidual algorithm. An algorithm (e.g. annoy) can have multiple
306
- definitions based on various run groups (see config.ymls for clear examples).
307
-
306
+ definitions based on various run groups (see config.ymls for clear examples).
307
+
308
308
Args:
309
309
name (str): Name of the algorithm.
310
310
algo (Dict[str, Any]): Dictionary with algorithm parameters.
311
311
dimension (int): Dimension of the algorithm.
312
312
distance_metric (str, optional): Distance metric used by the algorithm. Defaults to "euclidean".
313
313
count (int, optional): Count of the definitions to be created. Defaults to 10.
314
-
314
+
315
315
Raises:
316
316
Exception: If the algorithm does not define "docker_tag", "module" or "constructor" properties.
317
-
317
+
318
318
Returns:
319
319
List[Definition]: A list of definitions created from the algorithm.
320
320
"""
321
321
required_properties = ["docker_tag" , "module" , "constructor" ]
322
322
missing_properties = [prop for prop in required_properties if prop not in algo ]
323
323
if missing_properties :
324
324
raise ValueError (f"Algorithm { name } is missing the following properties: { ', ' .join (missing_properties )} " )
325
-
325
+
326
326
base_args = algo .get ("base_args" , [])
327
-
327
+
328
328
definitions = []
329
329
for run_group in algo ["run_groups" ].values ():
330
330
args = prepare_args (run_group )
@@ -340,7 +340,7 @@ def create_definitions_from_algorithm(name: str, algo: Dict[str, Any], dimension
340
340
341
341
vs = {"@count" : count , "@metric" : distance_metric , "@dimension" : dimension }
342
342
current_args = [_substitute_variables (arg , vs ) for arg in current_args ]
343
-
343
+
344
344
definitions .append (
345
345
Definition (
346
346
algorithm = name ,
@@ -373,6 +373,6 @@ def get_definitions(
373
373
definitions .extend (
374
374
create_definitions_from_algorithm (name , algo , dimension , distance_metric , count )
375
375
)
376
-
376
+
377
377
378
378
return definitions
0 commit comments