Skip to content

Commit 6e61fd6

Browse files
committed
Implemented getting a random neighbor over the full list where applicable, other improvements
1 parent 7fcfacb commit 6e61fd6

File tree

4 files changed

+13
-22
lines changed

4 files changed

+13
-22
lines changed

kernel_tuner/searchspace.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1044,7 +1044,7 @@ def get_random_neighbor(self, param_config: tuple, neighbor_method=None) -> tupl
10441044
"""Get an approximately random neighbor for a parameter configuration. Much faster than taking a random choice of all neighbors, but does not build cache."""
10451045
if self.are_neighbors_indices_cached(param_config, neighbor_method):
10461046
neighbors = self.get_neighbors(param_config, neighbor_method)
1047-
return choice(neighbors)
1047+
return choice(neighbors) if len(neighbors) > 0 else None
10481048
else:
10491049
# check if there is a neighbor method to use
10501050
if neighbor_method is None:

kernel_tuner/strategies/diff_evo.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -389,13 +389,10 @@ def repair(trial_vector, searchspace):
389389
# search for valid configurations neighboring trial_vector
390390
# start from strictly-adjacent to increasingly allowing more neighbors
391391
for neighbor_method in ["strictly-adjacent", "adjacent", "Hamming"]:
392-
neighbors = searchspace.get_neighbors_no_cache(tuple(trial_vector), neighbor_method=neighbor_method)
393-
394-
# if we have found valid neighboring configurations, select one at random
395-
if len(neighbors) > 0:
396-
new_trial_vector = list(random.choice(neighbors))
392+
new_trial_vector = searchspace.get_random_neighbor(tuple(trial_vector), neighbor_method=neighbor_method)
393+
if new_trial_vector is not None:
397394
print(f"Differential evolution resulted in invalid config {trial_vector=}, repaired to {new_trial_vector=}")
398-
return new_trial_vector
395+
return list(new_trial_vector)
399396

400397
return trial_vector
401398

kernel_tuner/strategies/genetic_algorithm.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -155,17 +155,14 @@ def random_index_weighted(pop_size):
155155
return [population[ind][0] for ind in chosen]
156156

157157

158-
def mutate(self, dna, cache=False):
158+
def mutate(self, dna):
159159
"""Mutate DNA with 1/mutation_chance chance."""
160160
# this is actually a neighbors problem with Hamming distance, choose randomly from returned searchspace list
161161
if int(random.random() * self.mutation_chance) == 0:
162162
if self.constraint_aware:
163-
if cache:
164-
neighbors = self.searchspace.get_neighbors(tuple(dna), neighbor_method="Hamming")
165-
else:
166-
neighbors = self.searchspace.get_neighbors_no_cache(tuple(dna), neighbor_method="Hamming")
167-
if len(neighbors) > 0:
168-
return list(random.choice(neighbors))
163+
neighbor = self.searchspace.get_random_neighbor(tuple(dna), neighbor_method="Hamming")
164+
if neighbor is not None:
165+
return list(neighbor)
169166
else:
170167
# select a tunable parameter at random
171168
mutate_index = random.randint(0, len(self.tune_params)-1)
@@ -187,13 +184,11 @@ def repair(self, dna):
187184
# search for valid configurations neighboring this config
188185
# start from strictly-adjacent to increasingly allowing more neighbors
189186
for neighbor_method in ["strictly-adjacent", "adjacent", "Hamming"]:
190-
neighbors = self.searchspace.get_neighbors(tuple(dna), neighbor_method=neighbor_method)
191-
187+
neighbor = self.searchspace.get_random_neighbor(tuple(dna), neighbor_method=neighbor_method)
192188
# if we have found valid neighboring configurations, select one at random
193-
if len(neighbors) > 0:
194-
new_dna = list(random.choice(neighbors))
195-
# print(f"GA crossover resulted in invalid config {dna=}, repaired dna to {new_dna=}")
196-
return new_dna
189+
if neighbor is not None:
190+
# print(f"GA crossover resulted in invalid config {dna=}, repaired dna to {neighbor=}")
191+
return list(neighbor)
197192

198193
return dna
199194

kernel_tuner/strategies/greedy_ils.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ def tune(searchspace: Searchspace, runner, tuning_options):
6060
tune.__doc__ = common.get_strategy_docstring("Greedy Iterative Local Search (ILS)", _options)
6161

6262
def mutate(indiv, searchspace: Searchspace):
63-
neighbors = searchspace.get_neighbors(tuple(indiv), neighbor_method="Hamming")
64-
return list(random_choice(neighbors))
63+
return list(searchspace.get_random_neighbor(tuple(indiv), neighbor_method="Hamming"))
6564

6665

6766
def random_walk(indiv, permutation_size, no_improve, last_improve, searchspace: Searchspace):

0 commit comments

Comments
 (0)