Skip to content

Commit 6313143

Browse files
committed
Change how (strictly) adjacent neighboor is implemented in the search space object.
This changes the adjacent and strictly-adjacent neighborhood in the search space object such that it returns the neighbors having AT MOST one parameter different, instead of allowing each parameter position to be different
1 parent 2451e2d commit 6313143

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

kernel_tuner/searchspace.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -563,8 +563,8 @@ def __get_neighbors_indices_strictlyadjacent(
563563
)
564564
# calculate the absolute difference between the parameter value indices
565565
abs_index_difference = np.abs(self.params_values_indices - param_config_value_indices)
566-
# get the param config indices where the difference is one or less for each position
567-
matching_indices = (np.max(abs_index_difference, axis=1) <= 1).nonzero()[0]
566+
# get the param config indices where the difference is one for at most one position and zero for the other positions
567+
matching_indices = np.where(abs_index_difference.sum(axis=1) <= 1)[0]
568568
# as the selected param config does not differ anywhere, remove it from the matches
569569
if param_config_index is not None:
570570
matching_indices = np.setdiff1d(matching_indices, [param_config_index], assume_unique=False)
@@ -581,26 +581,29 @@ def __get_neighbors_indices_adjacent(self, param_config_index: int = None, param
581581
index_difference = self.params_values_indices - param_config_value_indices
582582
# transpose to get the param indices difference per parameter instead of per param config
583583
index_difference_transposed = index_difference.transpose()
584+
# find 'hamming' neighbors for which at most one position is different
585+
hamming_mask = (index_difference != 0).sum(axis=1) <= 1
584586
# for each parameter get the closest upper and lower parameter (absolute index difference >= 1)
585587
# np.PINF has been replaced by 1e12 here, as on some systems np.PINF becomes np.NINF
586588
upper_bound = tuple(
587589
np.min(
588-
index_difference_transposed[p][(index_difference_transposed[p] > 0).nonzero()],
590+
index_difference_transposed[p],
589591
initial=1e12,
592+
where=hamming_mask,
590593
)
591594
for p in range(self.num_params)
592595
)
593596
lower_bound = tuple(
594597
np.max(
595-
index_difference_transposed[p][(index_difference_transposed[p] < 0).nonzero()],
598+
index_difference_transposed[p],
596599
initial=-1e12,
600+
where=hamming_mask,
597601
)
598602
for p in range(self.num_params)
599603
)
600604
# return the indices where each parameter is within bounds
601-
matching_indices = (
602-
np.logical_and(index_difference <= upper_bound, index_difference >= lower_bound).all(axis=1).nonzero()[0]
603-
)
605+
within_bounds = np.logical_and(index_difference <= upper_bound, index_difference >= lower_bound).all(axis=1)
606+
matching_indices = (hamming_mask & within_bounds).nonzero()[0]
604607
# as the selected param config does not differ anywhere, remove it from the matches
605608
if param_config_index is not None:
606609
matching_indices = np.setdiff1d(matching_indices, [param_config_index], assume_unique=False)

0 commit comments

Comments
 (0)