Skip to content

Commit 214865f

Browse files
committed
Removed encoding of non-numeric parameters in favour of index-based
1 parent ac87d79 commit 214865f

File tree

1 file changed

+0
-60
lines changed

1 file changed

+0
-60
lines changed

kernel_tuner/strategies/common.py

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
"""Module for functionality that is commonly used throughout the strategies."""
22

33
import logging
4-
import numbers
54
import sys
65
from time import perf_counter
76

87
import numpy as np
98
from scipy.spatial import distance
10-
import numbers
119

1210
from kernel_tuner import util
1311
from kernel_tuner.searchspace import Searchspace
@@ -62,11 +60,6 @@ def get_options(strategy_options, options, unsupported=None):
6260
return [strategy_options.get(opt, default) for opt, (_, default) in options.items()]
6361

6462

65-
def is_number(value) -> bool:
66-
"""Check if a value is a real number (false on booleans and complex numbers)."""
67-
return isinstance(value, numbers.Real) and not isinstance(value, bool)
68-
69-
7063
class CostFunc:
7164
"""Class encapsulating the CostFunc method."""
7265

@@ -78,7 +71,6 @@ def __init__(
7871
*,
7972
scaling=False,
8073
snap=True,
81-
encode_non_numeric=None,
8274
return_invalid=False,
8375
return_raw=None,
8476
):
@@ -90,7 +82,6 @@ def __init__(
9082
runner: the runner to use.
9183
scaling: whether to internally scale parameter values. Defaults to False.
9284
snap: whether to snap given configurations to their closests equivalent in the space. Defaults to True.
93-
encode_non_numeric: whether to encode non-numeric parameter values. Defaults to None, meaning it is applied when necessary.
9485
return_invalid: whether to return the util.ErrorConfig of an invalid configuration. Defaults to False.
9586
return_raw: returns (result, results[raw]). Key inferred from objective if set to True. Defaults to None.
9687
"""
@@ -103,29 +94,13 @@ def __init__(
10394
self.runner = runner
10495
self.scaling = scaling
10596
self.snap = snap
106-
self.encode_non_numeric = encode_non_numeric if encode_non_numeric is not None else not all([all(is_number(v) for v in param_values) for param_values in self.searchspace.params_values])
10797
self.return_invalid = return_invalid
10898
self.return_raw = return_raw
10999
if return_raw is True:
110100
self.return_raw = f"{tuning_options['objective']}s"
111101
self.results = []
112102
self.budget_spent_fraction = 0.0
113103

114-
# if enabled, encode non-numeric parameter values as a numeric value
115-
# NOTE careful, this shouldn't conflict with Searchspace tensorspace
116-
if self.encode_non_numeric:
117-
self._map_param_to_encoded = {}
118-
self._map_encoded_to_param = {}
119-
self.encoded_params_values = []
120-
for i, param_values in enumerate(self.searchspace.params_values):
121-
encoded_values = param_values
122-
if not all(is_number(v) for v in param_values):
123-
encoded_values = np.arange(
124-
len(param_values)
125-
) # NOTE when changing this, adjust the rounding in encoded_to_params
126-
self._map_param_to_encoded[i] = dict(zip(param_values, encoded_values))
127-
self._map_encoded_to_param[i] = dict(zip(encoded_values, param_values))
128-
self.encoded_params_values.append(encoded_values)
129104

130105
def __call__(self, x, check_restrictions=True):
131106
"""Cost function used by almost all strategies."""
@@ -254,41 +229,6 @@ def get_bounds(self):
254229
bounds.append((values[0], values[-1]))
255230
return bounds
256231

257-
def encoded_to_params(self, config):
258-
"""Convert from an encoded configuration to the real parameters."""
259-
if not self.encode_non_numeric:
260-
raise ValueError("'encode_non_numeric' must be set to true to use this function.")
261-
params = []
262-
for i, v in enumerate(config):
263-
# params.append(self._map_encoded_to_param[i][v] if i in self._map_encoded_to_param else v)
264-
if i in self._map_encoded_to_param:
265-
encoding = self._map_encoded_to_param[i]
266-
if v in encoding:
267-
param = encoding[v]
268-
elif isinstance(v, float):
269-
# try to resolve a rounding error due to floating point arithmetic / continous solver
270-
param = encoding[round(v)]
271-
else:
272-
raise ValueError(f"Encoded value {v} not found in {self._map_encoded_to_param[i]}")
273-
else:
274-
param = v
275-
params.append(param)
276-
assert len(params) == len(config)
277-
return params
278-
279-
def params_to_encoded(self, config):
280-
"""Convert from a parameter configuration to the encoded configuration."""
281-
if not self.encode_non_numeric:
282-
raise ValueError("'encode_non_numeric' must be set to true to use this function.")
283-
encoded = []
284-
for i, v in enumerate(config):
285-
try:
286-
encoded.append(self._map_param_to_encoded[i][v] if i in self._map_param_to_encoded else v)
287-
except KeyError:
288-
raise KeyError(f"{config} parameter value {v} not found in {self._map_param_to_encoded} for parameter {i}.")
289-
assert len(encoded) == len(config)
290-
return encoded
291-
292232

293233
def setup_method_arguments(method, bounds):
294234
"""Prepare method specific arguments."""

0 commit comments

Comments
 (0)