1
1
"""Module for functionality that is commonly used throughout the strategies."""
2
2
3
3
import logging
4
- import numbers
5
4
import sys
6
5
from time import perf_counter
7
6
8
7
import numpy as np
9
8
from scipy .spatial import distance
10
- import numbers
11
9
12
10
from kernel_tuner import util
13
11
from kernel_tuner .searchspace import Searchspace
@@ -62,11 +60,6 @@ def get_options(strategy_options, options, unsupported=None):
62
60
return [strategy_options .get (opt , default ) for opt , (_ , default ) in options .items ()]
63
61
64
62
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
-
70
63
class CostFunc :
71
64
"""Class encapsulating the CostFunc method."""
72
65
@@ -78,7 +71,6 @@ def __init__(
78
71
* ,
79
72
scaling = False ,
80
73
snap = True ,
81
- encode_non_numeric = None ,
82
74
return_invalid = False ,
83
75
return_raw = None ,
84
76
):
@@ -90,7 +82,6 @@ def __init__(
90
82
runner: the runner to use.
91
83
scaling: whether to internally scale parameter values. Defaults to False.
92
84
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.
94
85
return_invalid: whether to return the util.ErrorConfig of an invalid configuration. Defaults to False.
95
86
return_raw: returns (result, results[raw]). Key inferred from objective if set to True. Defaults to None.
96
87
"""
@@ -103,29 +94,13 @@ def __init__(
103
94
self .runner = runner
104
95
self .scaling = scaling
105
96
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 ])
107
97
self .return_invalid = return_invalid
108
98
self .return_raw = return_raw
109
99
if return_raw is True :
110
100
self .return_raw = f"{ tuning_options ['objective' ]} s"
111
101
self .results = []
112
102
self .budget_spent_fraction = 0.0
113
103
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 )
129
104
130
105
def __call__ (self , x , check_restrictions = True ):
131
106
"""Cost function used by almost all strategies."""
@@ -254,41 +229,6 @@ def get_bounds(self):
254
229
bounds .append ((values [0 ], values [- 1 ]))
255
230
return bounds
256
231
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
-
292
232
293
233
def setup_method_arguments (method , bounds ):
294
234
"""Prepare method specific arguments."""
0 commit comments