From 3f4a66d292175e31547c7de0a50bdc6ee012bcd9 Mon Sep 17 00:00:00 2001 From: Ben van Werkhoven Date: Thu, 10 Sep 2026 09:40:12 +0200 Subject: [PATCH] fix issue #362 a second time --- kernel_tuner/strategies/common.py | 3 ++- kernel_tuner/util.py | 4 ++-- test/strategies/test_dual_annealing.py | 32 ++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 test/strategies/test_dual_annealing.py diff --git a/kernel_tuner/strategies/common.py b/kernel_tuner/strategies/common.py index 80ccf2a8..cbeedd36 100644 --- a/kernel_tuner/strategies/common.py +++ b/kernel_tuner/strategies/common.py @@ -199,7 +199,8 @@ def eval_all(self, xs, check_restrictions=True): # get numerical return value, taking optimization direction into account return_value = util.get_result_cost(result, self.tuning_options.objective, - self.tuning_options.objective_higher_is_better + self.tuning_options.objective_higher_is_better, + self.invalid_return_value ) if len(return_value) == 1: diff --git a/kernel_tuner/util.py b/kernel_tuner/util.py index fc3bbeb9..5ceb3cce 100644 --- a/kernel_tuner/util.py +++ b/kernel_tuner/util.py @@ -85,11 +85,11 @@ def default(self, obj): return super(NpEncoder, self).default(obj) -def get_result_cost(result: dict, objectives: list[str], objective_higher_is_better: list[bool]) -> list[float]: +def get_result_cost(result: dict, objectives: list[str], objective_higher_is_better: list[bool], invalid_value) -> list[float]: """Returns the cost of a result, taking the objective directions into account.""" # return the highest cost for invalid results if "__error__" in result: - return [sys.float_info.max] * len(objectives) + return [invalid_value] * len(objectives) cost_vec = list() for objective, is_maximizer in zip(objectives, objective_higher_is_better): diff --git a/test/strategies/test_dual_annealing.py b/test/strategies/test_dual_annealing.py new file mode 100644 index 00000000..bc815b68 --- /dev/null +++ b/test/strategies/test_dual_annealing.py @@ -0,0 +1,32 @@ +import numpy as np + +import kernel_tuner + + + +def test_dual_annealing_gives_warning(recwarn): + """ Uses pytest built-in fixture recwarn to record any warnings this code might throw. """ + + kernel_tuner.tune_kernel( + kernel_name="foo", + kernel_source="void foo(int) { }", + problem_size=1, + arguments=[np.int32(0)], + tune_params={"block_size_x": range(1, 8)}, + strategy="dual_annealing", + restrictions="block_size_x < 4", + ) + + for warning in recwarn: + # Print the actual Warning object / message string + print(warning.message) + + # Print the category (e.g., ) + print(warning.category) + + # Print file and line where it was raised + print(f"Raised at {warning.filename}:{warning.lineno}") + + # Test that no warnings are thrown + assert len(recwarn) == 0 +