Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions kernel_tuner/strategies/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from time import perf_counter

import numpy as np
import numbers

from kernel_tuner import util
from kernel_tuner.searchspace import Searchspace
Expand Down Expand Up @@ -109,8 +110,15 @@ def __call__(self, x, check_restrictions=True):
self.runner.last_strategy_start_time = perf_counter()

# get numerical return value, taking optimization direction into account
return_value = result[self.tuning_options.objective] or sys.float_info.max
return_value = return_value if not self.tuning_options.objective_higher_is_better else -return_value
return_value = result[self.tuning_options.objective]

if isinstance(return_value, numbers.Number):
if self.tuning_options.objective_higher_is_better:
# flip the sign if higher means better
return_value = -return_value
else:
# this is not a valid configuration, just return max
return_value = sys.float_info.max

return return_value

Expand Down
22 changes: 13 additions & 9 deletions kernel_tuner/strategies/simulated_annealing.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,24 @@ def tune(searchspace: Searchspace, runner, tuning_options):

def acceptance_prob(old_cost, new_cost, T, tuning_options):
"""Annealing equation, with modifications to work towards a lower value."""
error_val = sys.float_info.max if not tuning_options.objective_higher_is_better else -sys.float_info.max
error_val = sys.float_info.max
res = 0.0
# if start pos is not valid, always move
if old_cost == error_val:
return 1.0
res = 1.0
# if we have found a valid ps before, never move to nonvalid pos
if new_cost == error_val:
return 0.0
elif new_cost == error_val:
res = 0.0
# always move if new cost is better
if new_cost < old_cost:
return 1.0
elif new_cost < old_cost:
res = 1.0
# maybe move if old cost is better than new cost depending on T and random value
if tuning_options.objective_higher_is_better:
return np.exp(((new_cost-old_cost)/new_cost)/T)
return np.exp(((old_cost-new_cost)/old_cost)/T)
else:
if tuning_options.objective_higher_is_better:
res = np.exp(((new_cost-old_cost)/new_cost)/T)
else:
res = np.exp(((old_cost-new_cost)/old_cost)/T)
return res


def neighbor(pos, searchspace: Searchspace):
Expand Down