Skip to content

Commit 6d09ca6

Browse files
committed
Move unique_results from tuning_options to CostFunc
1 parent 8e28f02 commit 6d09ca6

File tree

8 files changed

+22
-16
lines changed

8 files changed

+22
-16
lines changed

kernel_tuner/interface.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,6 @@ def tune_kernel(
616616
kernel_options = Options([(k, opts[k]) for k in _kernel_options.keys()])
617617
tuning_options = Options([(k, opts[k]) for k in _tuning_options.keys()])
618618
device_options = Options([(k, opts[k]) for k in _device_options.keys()])
619-
tuning_options["unique_results"] = {}
620619

621620
# copy some values from strategy_options
622621
searchspace_construction_options = {}

kernel_tuner/runners/sequential.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,5 @@ def run(self, parameter_space, tuning_options):
138138

139139
# all visited configurations are added to results to provide a trace for optimization strategies
140140
results.append(params)
141-
if x_int not in tuning_options.unique_results:
142-
tuning_options.unique_results[x_int] = result
143141

144142
return results

kernel_tuner/strategies/common.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ def __init__(
9898
self.scaling = scaling
9999
self.snap = snap
100100
self.return_invalid = return_invalid
101+
self.unique_results = dict()
101102
self.results = []
102103
self.budget_spent_fraction = 0.0
103104
self.invalid_return_value = invalid_value
@@ -147,7 +148,7 @@ def _run_configs(self, xs, check_restrictions=True):
147148
final_results = [] # List returned to the user
148149

149150
# Loop over all configurations. For each configurations there are four cases:
150-
# 1. The configuration is valid, we can skip it
151+
# 1. The configuration is invalid, we can skip it
151152
# 2. The configuration is in `unique_results`, we can get it from there
152153
# 3. The configuration is in `pending_indices_by_key`, it is duplicate in `xs`
153154
# 4. The configuration must be evaluated by the runner.
@@ -163,8 +164,8 @@ def _run_configs(self, xs, check_restrictions=True):
163164
final_results.append(result)
164165

165166
# 2. Attempt to retrieve from `unique_results`
166-
elif key in self.tuning_options.unique_results:
167-
result = dict(self.tuning_options.unique_results[key])
167+
elif key in self.unique_results:
168+
result = dict(self.unique_results[key])
168169
final_results.append(result)
169170

170171
# 3. We have already seen this config in the current batch
@@ -197,8 +198,12 @@ def _run_configs(self, xs, check_restrictions=True):
197198
result["benchmark_time"] = 0
198199

199200
# Put result in `unique_results`
200-
self.tuning_options.unique_results[key] = result
201-
self.results.append(result)
201+
self.unique_results[key] = result
202+
203+
for result in final_results:
204+
# Skip if None. Result is missing if runner exhausted the budget
205+
if result is not None:
206+
self.results.append(result)
202207

203208
# upon returning from this function control will be given back to the strategy, so reset the start time
204209
self.runner.last_strategy_start_time = perf_counter()
@@ -243,6 +248,12 @@ def eval(self, x, check_restrictions=True):
243248

244249
def __call__(self, x, check_restrictions=True):
245250
return self.eval(x, check_restrictions=check_restrictions)
251+
252+
def get_results(self):
253+
return self.results
254+
255+
def get_num_unique_results(self):
256+
return len(self.unique_results)
246257

247258
def get_start_pos(self):
248259
"""Get starting position for optimization."""

kernel_tuner/strategies/diff_evo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def generate_population(tune_params, max_idx, popsize, searchspace, constraint_a
115115
return population
116116

117117

118-
def differential_evolution(searchspace, cost_func, bounds, popsize, maxiter, F, CR, method, constraint_aware, verbose):
118+
def differential_evolution(searchspace, cost_func: CostFunc, bounds, popsize, maxiter, F, CR, method, constraint_aware, verbose):
119119
"""
120120
A basic implementation of the Differential Evolution algorithm.
121121
@@ -244,7 +244,7 @@ def differential_evolution(searchspace, cost_func, bounds, popsize, maxiter, F,
244244
print(f"Generation {generation + 1}, Best Cost: {best_cost:.6f}")
245245

246246
if verbose:
247-
print(f"Differential Evolution completed fevals={len(cost_func.tuning_options.unique_results)}")
247+
print(f"Differential Evolution completed fevals={cost_func.get_num_unique_results()}")
248248

249249
return {"solution": best_solution, "cost": best_cost}
250250

kernel_tuner/strategies/greedy_ils.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ def tune(searchspace: Searchspace, runner, tuning_options):
3737

3838
last_improvement = 0
3939
while fevals < max_fevals:
40-
4140
try:
4241
candidate = base_hillclimb(candidate, neighbor, max_fevals, searchspace, tuning_options, cost_func, restart=restart, randomize=True)
4342
new_score = cost_func(candidate, check_restrictions=False)
@@ -46,7 +45,7 @@ def tune(searchspace: Searchspace, runner, tuning_options):
4645
print(e)
4746
return cost_func.results
4847

49-
fevals = len(tuning_options.unique_results)
48+
fevals = cost_func.get_num_unique_results()
5049
if new_score < best_score:
5150
last_improvement = 0
5251
else:

kernel_tuner/strategies/greedy_mls.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ def tune(searchspace: Searchspace, runner, tuning_options):
3636
return cost_func.results
3737

3838
candidate = searchspace.get_random_sample(1)[0]
39-
40-
fevals = len(tuning_options.unique_results)
39+
fevals = cost_func.get_num_unique_results()
4140

4241
return cost_func.results
4342

kernel_tuner/strategies/pyatf_strategies.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def tune(searchspace: Searchspace, runner, tuning_options):
8585

8686
try:
8787
# optimization loop (KT-compatible re-implementation of `make_step` from TuningRun)
88-
while len(tuning_options.unique_results) < searchspace.size:
88+
while cost_func.get_num_unique_results() < searchspace.size:
8989

9090
# get new coordinates
9191
if not coordinates_or_indices:

kernel_tuner/strategies/simulated_annealing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def tune(searchspace: Searchspace, runner, tuning_options):
6868
pos = new_pos
6969
old_cost = new_cost
7070

71-
c = len(tuning_options.unique_results)
71+
c = cost_func.get_num_unique_results()
7272
T = T_start * alpha**(max_iter/max_fevals*c)
7373

7474
# check if solver gets stuck and if so restart from random position

0 commit comments

Comments
 (0)