Skip to content

Commit 04548ca

Browse files
Merge branch 'master' of github.com:benvanwerkhoven/kernel_tuner into skip_nvml_tests
2 parents f93ac95 + 580075c commit 04548ca

File tree

4 files changed

+45
-49
lines changed

4 files changed

+45
-49
lines changed

kernel_tuner/file_utils.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def store_output_file(output_filename, results, tune_params, objective="time"):
131131
version, _ = output_file_schema("results")
132132
output_json = dict(results=output_data, schema_version=version)
133133
with open(output_filename, 'w+') as fh:
134-
json.dump(output_json, fh)
134+
json.dump(output_json, fh, cls=util.NpEncoder)
135135

136136

137137
def get_dependencies(package='kernel_tuner'):
@@ -184,15 +184,19 @@ def store_metadata_file(metadata_filename):
184184
metadata_filename = filename_ensure_json_extension(metadata_filename)
185185
metadata = {}
186186

187-
# lshw only works on Linux, this intentionally raises a FileNotFoundError when ran on systems that do not have it
188-
lshw_out = subprocess.run(["lshw", "-json"], capture_output=True)
187+
# lshw only works on Linux
188+
try:
189+
lshw_out = subprocess.run(["lshw", "-json"], capture_output=True)
189190

190-
# sometimes lshw outputs a list of length 1, sometimes just as a dict, schema wants a list
191-
lshw_string = lshw_out.stdout.decode('utf-8').strip()
192-
if lshw_string[0] == '{' and lshw_string[-1] == '}':
193-
lshw_string = '[' + lshw_string + ']'
191+
# sometimes lshw outputs a list of length 1, sometimes just as a dict, schema wants a list
192+
lshw_string = lshw_out.stdout.decode('utf-8').strip()
193+
if lshw_string[0] == '{' and lshw_string[-1] == '}':
194+
lshw_string = '[' + lshw_string + ']'
195+
hardware_desc = dict(lshw=json.loads(lshw_string))
196+
except:
197+
hardware_desc = dict(lshw=["lshw error"])
194198

195-
metadata["hardware"] = dict(lshw=json.loads(lshw_string))
199+
metadata["hardware"] = hardware_desc
196200

197201
# attempts to use nvidia-smi or rocm-smi if present
198202
device_query = {}

kernel_tuner/observers/powersensor.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22

33
from kernel_tuner.observers.observer import BenchmarkObserver
44

5-
# check if power_sensor is installed
5+
# check if powersensor is installed
66
try:
7-
import power_sensor
7+
import powersensor
88
except ImportError:
9-
power_sensor = None
9+
powersensor = None
1010

1111

1212
class PowerSensorObserver(BenchmarkObserver):
1313
"""Observer that an external PowerSensor2 device to accurately measure power
1414
15-
Requires PowerSensor2 hardware and power_sensor Python bindings.
15+
Requires PowerSensor2 hardware and powersensor Python bindings.
1616
1717
:param observables: A list of string, containing any of "ps_energy" or "ps_power".
1818
To measure energy in Joules or power consumption in Watt.
@@ -25,8 +25,8 @@ class PowerSensorObserver(BenchmarkObserver):
2525
"""
2626

2727
def __init__(self, observables=None, device=None):
28-
if not power_sensor:
29-
raise ImportError("could not import power_sensor")
28+
if not powersensor:
29+
raise ImportError("could not import powersensor")
3030

3131
supported = ["ps_energy", "ps_power"]
3232
for obs in observables:
@@ -35,18 +35,18 @@ def __init__(self, observables=None, device=None):
3535
self.observables = observables or ["ps_energy"]
3636

3737
device = device or "/dev/ttyACM0"
38-
self.ps = power_sensor.PowerSensor(device)
38+
self.ps = powersensor.PowerSensor(device)
3939

4040
self.begin_state = None
41-
self.results = {"ps_energy": [], "ps_power": []}
41+
self.results = {key: [] for key in self.observables}
4242

4343
def after_start(self):
4444
self.begin_state = self.ps.read()
4545

4646
def after_finish(self):
4747
end_state = self.ps.read()
4848
if "ps_energy" in self.observables:
49-
ps_measured_e = power_sensor.Joules(
49+
ps_measured_e = powersensor.Joules(
5050
self.begin_state, end_state, -1
5151
) # Joules
5252
self.results["ps_energy"].append(ps_measured_e)
@@ -58,5 +58,5 @@ def after_finish(self):
5858

5959
def get_results(self):
6060
averages = {key: np.average(values) for key, values in self.results.items()}
61-
self.results = {"ps_energy": [], "ps_power": []}
61+
self.results = {key: [] for key in self.observables}
6262
return averages

kernel_tuner/strategies/common.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,23 +94,24 @@ def __call__(self, x, check_restrictions=True):
9494
result = params_dict
9595
result[self.tuning_options.objective] = util.InvalidConfig()
9696

97-
# compile and benchmark this instance
98-
if not result:
97+
if legal:
98+
# compile and benchmark this instance
9999
res = self.runner.run([params], self.tuning_options)
100100
result = res[0]
101101

102-
# append to tuning results
103-
if x_int not in self.tuning_options.unique_results:
104-
self.tuning_options.unique_results[x_int] = result
102+
# append to tuning results
103+
if x_int not in self.tuning_options.unique_results:
104+
self.tuning_options.unique_results[x_int] = result
105105

106-
self.results.append(result)
106+
self.results.append(result)
107+
108+
# upon returning from this function control will be given back to the strategy, so reset the start time
109+
self.runner.last_strategy_start_time = perf_counter()
107110

108111
# get numerical return value, taking optimization direction into account
109112
return_value = result[self.tuning_options.objective] or sys.float_info.max
110113
return_value = return_value if not self.tuning_options.objective_higher_is_better else -return_value
111114

112-
# upon returning from this function control will be given back to the strategy, so reset the start time
113-
self.runner.last_strategy_start_time = perf_counter()
114115
return return_value
115116

116117
def get_bounds_x0_eps(self):

kernel_tuner/util.py

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,19 @@ class RuntimeFailedConfig(ErrorConfig):
4949
pass
5050

5151

52+
class NpEncoder(json.JSONEncoder):
53+
""" Class we use for dumping Numpy objects to JSON """
54+
55+
def default(self, obj):
56+
if isinstance(obj, np.integer):
57+
return int(obj)
58+
if isinstance(obj, np.floating):
59+
return float(obj)
60+
if isinstance(obj, np.ndarray):
61+
return obj.tolist()
62+
return super(NpEncoder, self).default(obj)
63+
64+
5265
class TorchPlaceHolder():
5366

5467
def __init__(self):
@@ -725,18 +738,6 @@ def compile_restrictions(restrictions: list, tune_params: dict):
725738
return func
726739

727740

728-
class NpEncoder(json.JSONEncoder):
729-
730-
def default(self, obj):
731-
if isinstance(obj, np.integer):
732-
return int(obj)
733-
if isinstance(obj, np.floating):
734-
return float(obj)
735-
if isinstance(obj, np.ndarray):
736-
return obj.tolist()
737-
return super(NpEncoder, self).default(obj)
738-
739-
740741
def process_cache(cache, kernel_options, tuning_options, runner):
741742
"""cache file for storing tuned configurations
742743
@@ -871,16 +872,6 @@ def close_cache(cache):
871872
def store_cache(key, params, tuning_options):
872873
""" stores a new entry (key, params) to the cachefile """
873874

874-
# create converter for dumping numpy objects to JSON
875-
def JSONconverter(obj):
876-
if isinstance(obj, np.integer):
877-
return int(obj)
878-
if isinstance(obj, np.floating):
879-
return float(obj)
880-
if isinstance(obj, np.ndarray):
881-
return obj.tolist()
882-
return obj.__str__()
883-
884875
#logging.debug('store_cache called, cache=%s, cachefile=%s' % (tuning_options.cache, tuning_options.cachefile))
885876
if isinstance(tuning_options.cache, dict):
886877
if not key in tuning_options.cache:
@@ -894,7 +885,7 @@ def JSONconverter(obj):
894885

895886
if tuning_options.cachefile:
896887
with open(tuning_options.cachefile, "a") as cachefile:
897-
cachefile.write("\n" + json.dumps({ key: output_params }, default=JSONconverter)[1:-1] + ",")
888+
cachefile.write("\n" + json.dumps({ key: output_params }, cls=NpEncoder)[1:-1] + ",")
898889

899890

900891
def dump_cache(obj: str, tuning_options):

0 commit comments

Comments
 (0)