Skip to content

Commit 2138b6f

Browse files
committed
Reduced cognitive complexity of 'check_restrictions', expanded documentation
1 parent d36a5eb commit 2138b6f

File tree

2 files changed

+43
-47
lines changed

2 files changed

+43
-47
lines changed

CONTRIBUTING.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ Steps with :bash:`sudo` access (e.g. on a local device):
6161
* Do not forget to make sure the paths are set correctly. If you're using CUDA, the desired CUDA version should be in :bash:`$PATH`, :bash:`$LD_LIBARY_PATH` and :bash:`$CPATH`.
6262
* Re-open the shell for changes to take effect.
6363
#. Check if the environment is setup correctly by running :bash:`pytest` and :bash:`nox`. All tests should pass, except if one or more extras has been left out in the previous step, then these tests will skip gracefully.
64+
* [Note]: sometimes, changing the NVIDIA driver privileges is required to read program counters and energy measurements. Check if :bash:`cat /proc/driver/nvidia/params | grep RmProfilingAdminOnly` is set to 1. If so, `follow these steps <https://developer.nvidia.com/nvidia-development-tools-solutions-err_nvgpuctrperm-permission-issue-performance-counters>`__
6465

6566

6667
Cluster setup

kernel_tuner/util.py

Lines changed: 42 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -250,55 +250,50 @@ def check_block_size_params_names_list(block_size_names, tune_params):
250250

251251

252252
def check_restrictions(restrictions, params: dict, verbose: bool) -> bool:
253-
"""Check whether a specific instance meets the search space restrictions."""
254-
valid = True
253+
"""Check whether a specific configuration meets the search space restrictions."""
255254
if callable(restrictions):
256255
valid = restrictions(params)
257-
else:
258-
for restrict in restrictions:
259-
try:
260-
# if it's a python-constraint, convert to function and execute
261-
if isinstance(restrict, Constraint):
262-
restrict = convert_constraint_restriction(restrict)
263-
if not restrict(params.values()):
264-
valid = False
265-
break
266-
continue
267-
# if it's a string, fill in the parameters and evaluate
268-
elif isinstance(restrict, str):
269-
if not eval(replace_param_occurrences(restrict, params)):
270-
valid = False
271-
break
272-
continue
273-
# if it's a function, call it
274-
elif callable(restrict):
275-
if not restrict(**params):
276-
valid = False
277-
break
278-
continue
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 not restrict(**selected_params):
288-
valid = False
289-
break
290-
continue
291-
# otherwise, raise an error
292-
else:
293-
raise ValueError(f"Unkown restriction type {type(restrict)} ({restrict})")
294-
except ZeroDivisionError:
295-
pass
296-
if not valid and verbose:
297-
print(
298-
"skipping config",
299-
get_instance_string(params),
300-
"reason: config fails restriction",
301-
)
256+
if not valid and verbose is True:
257+
print(f"skipping config {get_instance_string(params)}, reason: config fails restriction")
258+
return valid
259+
valid = True
260+
for restrict in restrictions:
261+
# Check the type of each restriction and validate accordingly. Re-implement as a switch when Python >= 3.10.
262+
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(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 not restrict(**selected_params):
288+
valid = False
289+
break
290+
# otherwise, raise an error
291+
else:
292+
raise ValueError(f"Unkown restriction type {type(restrict)} ({restrict})")
293+
except ZeroDivisionError:
294+
logging.debug(f"Restriction {restrict} with configuration {get_instance_string(params)} divides by zero.")
295+
if not valid and verbose is True:
296+
print(f"skipping config {get_instance_string(params)}, reason: config fails restriction")
302297
return valid
303298

304299

0 commit comments

Comments
 (0)