Skip to content

Commit c0eef3f

Browse files
committed
Refactored to meet Sonarcloud cognitive complexity
1 parent 380e252 commit c0eef3f

File tree

1 file changed

+33
-38
lines changed

1 file changed

+33
-38
lines changed

kernel_tuner/util.py

Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,37 @@ def check_block_size_params_names_list(block_size_names, tune_params):
248248
UserWarning,
249249
)
250250

251+
def check_restriction(restrict, params: dict) -> bool:
252+
"""Check whether a configuration meets a search space restriction."""
253+
# if it's a python-constraint, convert to function and execute
254+
if isinstance(restrict, Constraint):
255+
restrict = convert_constraint_restriction(restrict)
256+
return restrict(list(params.values()))
257+
# if it's a string, fill in the parameters and evaluate
258+
elif isinstance(restrict, str):
259+
return eval(replace_param_occurrences(restrict, params))
260+
# if it's a function, call it
261+
elif callable(restrict):
262+
return restrict(**params)
263+
# if it's a tuple, use only the parameters in the second argument to call the restriction
264+
elif (isinstance(restrict, tuple) and len(restrict) == 2
265+
and callable(restrict[0]) and isinstance(restrict[1], (list, tuple))):
266+
# unpack the tuple
267+
restrict, selected_params = restrict
268+
# look up the selected parameters and their value
269+
selected_params = dict((key, params[key]) for key in selected_params)
270+
# call the restriction
271+
if isinstance(restrict, Constraint):
272+
restrict = convert_constraint_restriction(restrict)
273+
return restrict(list(selected_params.values()))
274+
else:
275+
return restrict(**selected_params)
276+
# otherwise, raise an error
277+
else:
278+
raise ValueError(f"Unkown restriction type {type(restrict)} ({restrict})")
251279

252280
def check_restrictions(restrictions, params: dict, verbose: bool) -> bool:
253-
"""Check whether a specific configuration meets the search space restrictions."""
281+
"""Check whether a configuration meets the search space restrictions."""
254282
if callable(restrictions):
255283
valid = restrictions(params)
256284
if not valid and verbose is True:
@@ -260,46 +288,13 @@ def check_restrictions(restrictions, params: dict, verbose: bool) -> bool:
260288
for restrict in restrictions:
261289
# Check the type of each restriction and validate accordingly. Re-implement as a switch when Python >= 3.10.
262290
try:
263-
# if it's a python-constraint, convert to function and execute
264-
if isinstance(restrict, Constraint):
265-
restrict = convert_constraint_restriction(restrict)
266-
if not restrict(list(params.values())):
267-
valid = False
268-
break
269-
# if it's a string, fill in the parameters and evaluate
270-
elif isinstance(restrict, str):
271-
if not eval(replace_param_occurrences(restrict, params)):
272-
valid = False
273-
break
274-
# if it's a function, call it
275-
elif callable(restrict):
276-
if not restrict(**params):
277-
valid = False
278-
break
279-
# if it's a tuple, use only the parameters in the second argument to call the restriction
280-
elif (isinstance(restrict, tuple) and len(restrict) == 2
281-
and callable(restrict[0]) and isinstance(restrict[1], (list, tuple))):
282-
# unpack the tuple
283-
restrict, selected_params = restrict
284-
# look up the selected parameters and their value
285-
selected_params = dict((key, params[key]) for key in selected_params)
286-
# call the restriction
287-
if isinstance(restrict, Constraint):
288-
restrict = convert_constraint_restriction(restrict)
289-
if not restrict(list(selected_params.values())):
290-
valid = False
291-
break
292-
else:
293-
if not restrict(**selected_params):
294-
valid = False
295-
break
296-
# otherwise, raise an error
297-
else:
298-
raise ValueError(f"Unkown restriction type {type(restrict)} ({restrict})")
291+
valid = check_restriction(restrict, params)
292+
if not valid:
293+
break
299294
except ZeroDivisionError:
300295
logging.debug(f"Restriction {restrict} with configuration {get_instance_string(params)} divides by zero.")
301296
if not valid and verbose is True:
302-
print(f"skipping config {get_instance_string(params)}, reason: config fails restriction")
297+
print(f"skipping config {get_instance_string(params)}, reason: config fails restriction {restrict}")
303298
return valid
304299

305300

0 commit comments

Comments
 (0)