Skip to content

Commit e6f5f3e

Browse files
committed
Fix warnings flagged by SonarQube
1 parent 877a01e commit e6f5f3e

6 files changed

Lines changed: 29 additions & 15 deletions

File tree

kernel_tuner/core.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,8 @@ def instantiate_observer(observer, args):
224224
if isinstance(observer, BenchmarkObserver):
225225
return observer
226226
elif callable(observer):
227-
return instantiate_observer(observer(args), args) # Check again if BenchmarkObserver
227+
# Check again if BenchmarkObserver
228+
return instantiate_observer(observer(args), args)
228229
else:
229230
raise TypeError(f"Invalid observer: {observer!r} does not extend BenchmarkObserver")
230231

kernel_tuner/runners/parallel.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
@ray.remote(num_gpus=1)
3030
class WorkerActor:
31+
"""A Ray actor that owns a single GPU and benchmarks kernel configurations on it."""
3132
def __init__(
3233
self, kernel_source, kernel_options, device_options, tuning_options, iterations, observers
3334
):
@@ -67,7 +68,7 @@ def get_environment(self):
6768
return env
6869

6970
def run(self, params):
70-
# TODO: logging.debug("sequential runner started for " + self.kernel_options.kernel_name)
71+
# logging.debug("sequential runner started for " + self.kernel_options.kernel_name)
7172
result = None
7273

7374
# attempt to warmup the GPU by running the first config in the parameter space and ignoring the result
@@ -86,17 +87,20 @@ def run(self, params):
8687
params["ray_actor_id"] = ray.get_runtime_context().get_actor_id()
8788
params["host_name"] = socket.gethostname()
8889

89-
# all visited configurations are added to results to provide a trace for optimization strategies
9090
return params
9191

9292

9393
class Worker:
94+
"""Local handle for a ``WorkerActor`` running in a remote Ray worker process."""
95+
9496
def __init__(self, index, actor):
9597
self.index = index
9698
self.running_jobs = []
9799
self.maximum_running_jobs = 2
98100
self.is_running = True
99101
self.actor = actor
102+
103+
# Note: This will block until the environment is available locally
100104
self.env = ray.get(actor.get_environment.remote())
101105

102106
def __repr__(self):
@@ -108,9 +112,14 @@ def __repr__(self):
108112
return f"{self.index}"
109113

110114
def shutdown(self):
115+
"""Request the remote actor to exit and mark this handle as stopped."""
111116
if not self.is_running:
112117
return
113118

119+
# Wait until running jobs complete
120+
if self.running_jobs:
121+
ray.wait(self.running_jobs)
122+
114123
self.is_running = False
115124

116125
try:
@@ -119,11 +128,14 @@ def shutdown(self):
119128
logger.exception("failed to request actor shutdown: worker %s", self)
120129

121130
def submit(self, config):
131+
"""Submit a kernel configuration for benchmarking."""
122132
job = self.actor.run.remote(config)
123133
self.running_jobs.append(job)
124134
return job
125135

126136
def is_available(self):
137+
"""Return True if this worker can accept another job right now."""
138+
127139
if not self.is_running:
128140
return False
129141

@@ -139,26 +151,26 @@ def launch_workers(n, *args):
139151
workers = []
140152

141153
try:
142-
# Start all actors in parallel
154+
# Start all actors in parallel since `WorkerActor.remote` does not block
143155
for _ in range(n):
144156
actors.append(WorkerActor.remote(*args))
145157

146-
# Create `Worker` objects. This blocks until each worker is ready
158+
# Create local `Worker` objects. This blocks until each worker is ready
147159
for index, actor in enumerate(actors):
148160
worker = Worker(index, actor)
149161
workers.append(worker)
150162
logging.info("connected: worker %s", worker)
151-
152-
return workers
153-
except:
154-
# Attempt to shut down actors
163+
except Exception:
164+
# Attempt to shut down the running actors
155165
for actor in actors:
156166
try:
157167
actor.shutdown.remote()
158168
except:
159169
logger.exception("failed to request actor shutdown: %s", actor)
160170
raise
161171

172+
return workers
173+
162174

163175
class ParallelRunner(Runner):
164176
def __init__(
@@ -228,7 +240,7 @@ def shutdown(self):
228240
for worker in self.workers:
229241
try:
230242
worker.shutdown()
231-
except Exception as err:
243+
except Exception:
232244
logger.exception(f"error while shutting down worker {worker}")
233245

234246
def available_parallelism(self):
@@ -350,8 +362,7 @@ def run(self, parameter_space, tuning_options) -> List[Optional[dict]]:
350362
result = process_metrics(result, metrics)
351363
else:
352364
logging.error(
353-
"kernel configuration {key} was skipped silently due to compile or runtime failure",
354-
key,
365+
f"kernel configuration {key} was skipped silently due to compile or runtime failure"
355366
)
356367

357368
# print configuration to the console

kernel_tuner/runners/runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def add_strategy_time(self, seconds: float):
1818
self.accumulated_strategy_time += seconds
1919

2020
def shutdown(self):
21+
""" Signal to this runner that we are about to shut down. """
2122
pass
2223

2324
def available_parallelism(self):

kernel_tuner/runners/sequential.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ def run(self, parameter_space, tuning_options):
122122
# all visited configurations are added to results to provide a trace for optimization strategies
123123
results.append(params)
124124

125-
num_valid_results = sum(bool(r) for r in results) # Count the number of valid results
125+
# Count the number of valid results
126+
num_valid_results = sum(bool(r) for r in results)
126127

127128
if num_valid_results > 0:
128129
strategy_time = self.accumulated_strategy_time

kernel_tuner/runners/simulation.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ def run(self, parameter_space, tuning_options):
135135
params_dict = dict(zip(tuning_options['tune_params'].keys(), element))
136136
check = util.check_restrictions(tuning_options.restrictions, params_dict, True)
137137
if not check:
138-
result = util.copy_without_benchmark_timings(params_dict) # Set timings to zero
138+
# Set timings to zero
139+
result = util.copy_without_benchmark_timings(params_dict)
139140
result[tuning_options.objective] = util.InvalidConfig()
140141
results.append(result)
141142
warn(f"Configuration {element} not in cache, does not pass restrictions. Will be treated as an InvalidConfig, but make sure you are evaluating the correct cache file.")

kernel_tuner/strategies/common.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ def _run_configs(self, xs, check_restrictions=True):
145145
for index, x in enumerate(xs):
146146
config, is_legal = self._normalize_and_validate_config(x, check_restrictions=check_restrictions)
147147
logging.debug("normalize config: %s -> %s (legal: %s)", str(x), str(config), is_legal)
148-
key = ",".join([str(i) for i in config])
149148

150149
# Not legal, just return `InvalidConfig`
151150
if not is_legal:

0 commit comments

Comments
 (0)