import contextlib
import io
import logging
import matplotlib.pyplot as plt29.5 Asynchronous Successive Halving
As we have seen in Section 29.3, we can accelerate HPO by distributing the evaluation of hyperparameter configurations across either multiple instances or multiple CPUs / GPUs on a single instance. However, compared to random search, it is not straightforward to run successive halving (SH) asynchronously in a distributed setting. Before we can decide which configuration to run next, we first have to collect all observations at the current rung level. This requires to synchronize workers at each rung level. For example, for the lowest rung level \(r_{\mathrm{min}}\), we first have to evaluate all \(N = \eta^K\) configurations, before we can promote the \(\frac{1}{\eta}\) of them to the next rung level.
In any distributed system, synchronization typically implies idle time for workers. First, we often observe high variations in training time across hyperparameter configurations. For example, assuming the number of filters per layer is a hyperparameter, then networks with less filters finish training faster than networks with more filters, which implies idle worker time due to stragglers. Moreover, the number of slots in a rung level is not always a multiple of the number of workers, in which case some workers may even sit idle for a full batch.
Figure Figure 29.5.1 shows the scheduling of synchronous SH with \(\eta=2\) for four different trials with two workers. We start with evaluating Trial-0 and Trial-1 for one epoch and immediately continue with the next two trials once they are finished. We first have to wait until Trial-2 finishes, which takes substantially more time than the other trials, before we can promote the best two trials, i.e., Trial-0 and Trial-3 to the next rung level. This causes idle time for Worker-1. Then, we continue with Rung 1. Also, here Trial-3 takes longer than Trial-0, which leads to an additional idling time of Worker-0. Once we reach Rung-2, only the best trial, Trial-0, remains which occupies only one worker. To avoid that Worker-1 idles during that time, most implementations of SH continue already with the next round, and start evaluating new trials (e.g Trial-4) on the first rung.
Asynchronous successive halving (ASHA) (Li et al. 2018) adapts SH to the asynchronous parallel scenario. The main idea of ASHA is to promote configurations to the next rung level as soon as we collected at least \(\eta\) observations on the current rung level. This decision rule may lead to suboptimal promotions: configurations can be promoted to the next rung level, which in hindsight do not compare favourably against most others at the same rung level. On the other hand, we get rid of all synchronization points this way. In practice, such suboptimal initial promotions have only a modest impact on performance, not only because the ranking of hyperparameter configurations is often fairly consistent across rung levels, but also because rungs grow over time and reflect the distribution of metric values at this level better and better. If a worker is free, but no configuration can be promoted, we start a new configuration with \(r = r_{\mathrm{min}}\), i.e the first rung level.
Figure 29.5.2 shows the scheduling of the same configurations for ASHA. Once Trial-1 finishes, we collect the results of two trials (i.e Trial-0 and Trial-1) and immediately promote the better of them (Trial-0) to the next rung level. After Trial-0 finishes on rung 1, there are too few trials there in order to support a further promotion. Hence, we continue with rung 0 and evaluate Trial-3. Once Trial-3 finishes, Trial-2 is still pending. At this point we have 3 trials evaluated on rung 0 and one trial evaluated already on rung 1. Since Trial-3 performs worse than Trial-0 at rung 0, and \(\eta=2\), we cannot promote any new trial yet, and Worker-1 starts Trial-4 from scratch instead. However, once Trial-2 finishes and scores worse than Trial-3, the latter is promoted towards rung 1. Afterwards, we collected 2 evaluations on rung 1, which means we can now promote Trial-0 towards rung 2. At the same time, Worker-1 continues with evaluating new trials (i.e., Trial-5) on rung 0.
# Use INFO level so the periodic Syne Tune tuning-status table appears,
# but use a clean format that drops the "INFO:syne_tune.tuner:" prefix.
logging.basicConfig(level=logging.INFO, format="%(message)s", force=True)
# Silence Syne Tune's import-time chatter about optional AWS dependencies
# (sagemaker, s3fs) and Ray Tune. We use the local PythonBackend, so those
# are not needed. Suppress both print() and logging.info() during imports.
_root = logging.getLogger()
_prev_level = _root.level
_root.setLevel(logging.WARNING)
try:
with contextlib.redirect_stdout(io.StringIO()):
from syne_tune.config_space import loguniform, randint
from syne_tune.backend.python_backend.python_backend import PythonBackend
from syne_tune.optimizer.baselines import ASHA
from syne_tune import Tuner, StoppingCriterion
from syne_tune.experiments import load_experiment
finally:
_root.setLevel(_prev_level)
# Silence the per-trial subprocess-command spam from local_backend and
# drop the per-trial scheduling / completion lines from the tuner logger.
# Keep the periodic "tuning status (last metric is reported)" updates so
# the reader can still see progress over time.
class _DropPerTrialNoise(logging.Filter):
_DROP = (
"results of trials will be saved",
"scheduled ",
"Trial trial_id ",
)
def filter(self, record):
msg = record.getMessage()
return not any(s in msg for s in self._DROP)
logging.getLogger("syne_tune.backend.local_backend").setLevel(logging.WARNING)
logging.getLogger("syne_tune.tuner").addFilter(_DropPerTrialNoise())29.5.1 Objective Function
We use Syne Tune with the objective function of Section 29.3, changed in one respect: after every epoch it reports the validation loss as well as the validation error, and ASHA decides on the loss. At each rung, Syne Tune stops a trial whose metric exceeds a quantile of the values recorded there so far (the median at the first rung) and lets every other trial continue, including one whose metric equals the quantile. The error is a poor basis for this rule because it ties. After two epochs, a LeNet with a small learning rate has often not started to learn: it predicts the same class for every image, and since each class makes up a tenth of the validation set, its error is exactly 0.9. When more than half of the trials at the first rung share this value, the median is 0.9 as well, and none of them is stopped. The cross-entropy loss is continuous and does not tie, so the rule stops the intended fraction of the trials at every rung.
def hpo_objective_lenet_synetune(learning_rate, batch_size, max_epochs):
from d2l import torch as d2l
from syne_tune import Reporter
model = d2l.LeNet(lr=learning_rate, num_classes=10)
trainer = d2l.Trainer(max_epochs=1, num_gpus=1)
data = d2l.FashionMNIST(batch_size=batch_size)
model.apply_init([next(iter(data.get_dataloader(True)))[0]], d2l.init_cnn)
report = Reporter()
for epoch in range(1, max_epochs + 1):
if epoch == 1:
# Initialize the state of Trainer
trainer.fit(model=model, data=data)
else:
trainer.fit_epoch()
metrics = trainer.validate()
report(epoch=epoch, validation_loss=metrics['loss'],
validation_error=1 - metrics['acc'])We will also use the same configuration space as before:
min_number_of_epochs = 2
max_number_of_epochs = 10
eta = 2
config_space = {
"learning_rate": loguniform(1e-2, 1),
"batch_size": randint(32, 256),
"max_epochs": max_number_of_epochs,
}
initial_config = {
"learning_rate": 0.1,
"batch_size": 128,
}29.5.2 Asynchronous Scheduler
First, we define the number of workers that evaluate trials concurrently. We also need to specify how long we want to run random search, by defining an upper limit on the total wall-clock time.
# Each LeNet trial fits in well under 7 GB of GPU memory, so we can pack
# multiple trials per device. `PythonBackend(rotate_gpus=True)` (the
# default) round-robins trials across detected GPUs and falls back to
# sharing when `n_workers > num_gpus`. Allocate 7 GB per slot — this
# yields 3 slots on a 24 GB card and 4 slots on a 32 GB card after
# driver overhead, e.g. 4×24 GB → 12 slots; 2×32 GB → 8.
import torch
_GB = 1024 ** 3
n_workers = sum(
torch.cuda.get_device_properties(i).total_memory // (7 * _GB)
for i in range(torch.cuda.device_count())
) or 1
max_wallclock_time = 15 * 60 # 15 minutesThe code for running ASHA is a simple variation of what we did for asynchronous random search.
mode = "min"
metric = "validation_loss"
resource_attr = "epoch"
scheduler = ASHA(
config_space,
metric=metric,
mode=mode,
points_to_evaluate=[initial_config],
max_resource_attr="max_epochs",
resource_attr=resource_attr,
grace_period=min_number_of_epochs,
reduction_factor=eta,
)max_resource_level = 10, as inferred from config_space
Master random_seed = 4080344572
Here, metric and resource_attr specify the key names used with the report callback, and max_resource_attr denotes which input to the objective function corresponds to \(r_{\mathrm{max}}\). Moreover, grace_period provides \(r_{\mathrm{min}}\), and reduction_factor is \(\eta\). We can run Syne Tune as before (this will take about 15 minutes):
trial_backend = PythonBackend(
tune_function=hpo_objective_lenet_synetune,
config_space=config_space,
)
stop_criterion = StoppingCriterion(max_wallclock_time=max_wallclock_time)
tuner = Tuner(
trial_backend=trial_backend,
scheduler=scheduler,
stop_criterion=stop_criterion,
n_workers=n_workers,
print_update_interval=int(max_wallclock_time * 0.6),
)
tuner.run()tuning status (last metric is reported)
trial_id status iter learning_rate batch_size max_epochs epoch validation_loss validation_error worker-time
0 Stopped 3 0.100000 128 10 3 2.205312 0.6707 10.492408
1 Completed 10 0.251925 155 10 10 1.264232 0.4164 21.631078
2 Stopped 3 0.051807 139 10 3 2.301750 0.9000 10.181621
3 Stopped 3 0.058154 230 10 3 2.304288 0.9000 10.091485
4 Stopped 2 0.012241 53 10 2 2.304670 0.9000 12.817183
5 Stopped 3 0.060641 130 10 3 2.300647 0.8261 10.935662
6 Stopped 4 0.187939 249 10 4 1.249727 0.4484 10.711452
7 Stopped 6 0.021582 181 10 6 2.302889 0.9000 15.177434
8 Stopped 5 0.030533 108 10 5 2.302740 0.9000 14.894108
9 Stopped 3 0.154943 156 10 3 2.250644 0.8780 10.396651
10 Stopped 10 0.042457 45 10 10 0.686403 0.2584 45.917642
11 Stopped 9 0.013957 200 10 9 2.302526 0.9000 15.400472
12 Stopped 4 0.020225 171 10 4 2.302546 0.9000 10.485584
13 Stopped 5 0.173419 229 10 5 1.250987 0.4633 10.904811
14 Stopped 5 0.020733 244 10 5 2.302667 0.9000 10.609587
15 Stopped 8 0.010340 159 10 8 2.302570 0.9000 15.968551
16 Completed 10 0.027672 47 10 10 0.822092 0.2903 46.881940
17 Completed 10 0.572317 83 10 10 0.405178 0.1480 29.392934
18 Completed 10 0.070567 41 10 10 0.565909 0.2102 51.651154
19 Stopped 2 0.023484 56 10 2 2.303839 0.9000 13.234095
20 Completed 10 0.185475 152 10 10 0.643400 0.2445 22.780588
21 Stopped 3 0.022301 64 10 3 2.302963 0.9000 15.587487
22 Stopped 3 0.589029 131 10 3 1.402368 0.3626 10.563633
23 Stopped 10 0.081944 66 10 10 0.636439 0.2520 34.964488
24 Completed 10 0.350037 151 10 10 0.542391 0.2049 22.007111
25 Completed 10 0.875904 235 10 10 0.479404 0.1810 18.166647
26 Stopped 4 0.038899 245 10 4 2.302300 0.9000 10.293126
27 Completed 10 0.537073 119 10 10 0.582884 0.2083 23.917212
28 Stopped 5 0.042808 232 10 5 2.303726 0.9000 10.300658
29 Stopped 8 0.174685 133 10 8 0.800284 0.3176 19.539214
30 Stopped 3 0.045402 232 10 3 2.304257 0.9000 10.092218
31 Completed 10 0.564982 52 10 10 0.386495 0.1443 42.989713
32 Stopped 2 0.058889 94 10 2 2.305903 0.9000 10.115277
33 Completed 10 0.493254 239 10 10 1.416286 0.4920 18.696368
34 Completed 10 0.425104 155 10 10 0.583996 0.2173 19.135072
35 Completed 10 0.520376 199 10 10 0.504129 0.1880 19.636274
36 Stopped 7 0.018298 170 10 7 2.302517 0.9000 15.920070
37 Completed 10 0.652076 155 10 10 0.596140 0.2347 22.785090
38 Completed 10 0.265868 181 10 10 0.625975 0.2415 21.671445
39 Stopped 4 0.012236 120 10 4 2.302870 0.9000 10.979479
40 Stopped 7 0.011831 253 10 7 2.303024 0.9000 15.416995
41 Stopped 10 0.452213 49 10 10 0.407236 0.1543 50.717104
42 Stopped 2 0.012734 107 10 2 2.303225 0.9000 9.386621
43 Stopped 10 0.294500 250 10 10 0.641352 0.2452 14.706245
44 Stopped 2 0.024097 106 10 2 2.308731 0.9000 9.372424
45 Completed 10 0.604929 145 10 10 0.443001 0.1598 22.126401
46 Stopped 2 0.085947 106 10 2 2.328517 0.9000 9.214730
47 Completed 10 0.570348 56 10 10 0.378792 0.1396 44.051007
48 Stopped 3 0.020542 135 10 3 2.304046 0.9000 11.049390
49 Stopped 4 0.200606 220 10 4 0.962947 0.3468 10.613618
50 Stopped 2 0.045092 115 10 2 2.302963 0.9000 9.485898
51 Stopped 3 0.036800 65 10 3 2.310088 0.9000 15.031746
52 Stopped 2 0.038681 32 10 2 2.304635 0.9000 16.918590
53 Stopped 4 0.037208 160 10 4 2.302659 0.9000 10.198351
54 Stopped 2 0.061635 98 10 2 2.308015 0.9000 9.966621
55 Completed 10 0.529904 103 10 10 0.416937 0.1534 28.082242
56 Stopped 3 0.069544 96 10 3 2.301423 0.9000 10.638853
57 Stopped 6 0.015855 148 10 6 2.301256 0.8915 15.679200
58 Stopped 4 0.038892 55 10 4 1.163157 0.4270 22.323337
59 Stopped 3 0.042699 70 10 3 2.273236 0.9000 14.155716
60 Stopped 3 0.039162 164 10 3 2.303039 0.9004 10.252579
61 Stopped 9 0.289937 129 10 9 0.606612 0.2300 20.965594
62 Stopped 7 0.036945 221 10 7 2.295650 0.8283 15.626283
63 Stopped 2 0.021913 98 10 2 2.303879 0.9000 10.723158
64 Stopped 4 0.038818 160 10 4 2.304248 0.9000 10.184917
65 Stopped 3 0.048097 189 10 3 2.301573 0.9000 9.841662
66 InProgress 4 0.612826 58 10 4 0.480014 0.1840 20.797104
67 InProgress 8 0.866143 222 10 8 0.502325 0.1826 15.545278
68 InProgress 2 0.288426 123 10 2 0.968514 0.3825 9.491450
3 trials running, 66 finished (17 until the end), 542.61s wallclock-time
reaching max wallclock time (900), stopping there.
Stopping trials that may still be running.
Tuning finished, results of trials can be found on syne-tune/python-entrypoint-2026-11-23-15-51-04-769
--------------------
Resource summary (last result is reported):
trial_id status iter learning_rate batch_size max_epochs epoch validation_loss validation_error worker-time
0 Stopped 3 0.100000 128 10 3.0 2.205312 0.6707 10.492408
1 Completed 10 0.251925 155 10 10.0 1.264232 0.4164 21.631078
2 Stopped 3 0.051807 139 10 3.0 2.301750 0.9000 10.181621
3 Stopped 3 0.058154 230 10 3.0 2.304288 0.9000 10.091485
4 Stopped 2 0.012241 53 10 2.0 2.304670 0.9000 12.817183
5 Stopped 3 0.060641 130 10 3.0 2.300647 0.8261 10.935662
6 Stopped 4 0.187939 249 10 4.0 1.249727 0.4484 10.711452
7 Stopped 6 0.021582 181 10 6.0 2.302889 0.9000 15.177434
8 Stopped 5 0.030533 108 10 5.0 2.302740 0.9000 14.894108
9 Stopped 3 0.154943 156 10 3.0 2.250644 0.8780 10.396651
10 Stopped 10 0.042457 45 10 10.0 0.686403 0.2584 45.917642
11 Stopped 9 0.013957 200 10 9.0 2.302526 0.9000 15.400472
12 Stopped 4 0.020225 171 10 4.0 2.302546 0.9000 10.485584
13 Stopped 5 0.173419 229 10 5.0 1.250987 0.4633 10.904811
14 Stopped 5 0.020733 244 10 5.0 2.302667 0.9000 10.609587
15 Stopped 8 0.010340 159 10 8.0 2.302570 0.9000 15.968551
16 Completed 10 0.027672 47 10 10.0 0.822092 0.2903 46.881940
17 Completed 10 0.572317 83 10 10.0 0.405178 0.1480 29.392934
18 Completed 10 0.070567 41 10 10.0 0.565909 0.2102 51.651154
19 Stopped 2 0.023484 56 10 2.0 2.303839 0.9000 13.234095
20 Completed 10 0.185475 152 10 10.0 0.643400 0.2445 22.780588
21 Stopped 3 0.022301 64 10 3.0 2.302963 0.9000 15.587487
22 Stopped 3 0.589029 131 10 3.0 1.402368 0.3626 10.563633
23 Stopped 10 0.081944 66 10 10.0 0.636439 0.2520 34.964488
24 Completed 10 0.350037 151 10 10.0 0.542391 0.2049 22.007111
25 Completed 10 0.875904 235 10 10.0 0.479404 0.1810 18.166647
26 Stopped 4 0.038899 245 10 4.0 2.302300 0.9000 10.293126
27 Completed 10 0.537073 119 10 10.0 0.582884 0.2083 23.917212
28 Stopped 5 0.042808 232 10 5.0 2.303726 0.9000 10.300658
29 Stopped 8 0.174685 133 10 8.0 0.800284 0.3176 19.539214
30 Stopped 3 0.045402 232 10 3.0 2.304257 0.9000 10.092218
31 Completed 10 0.564982 52 10 10.0 0.386495 0.1443 42.989713
32 Stopped 2 0.058889 94 10 2.0 2.305903 0.9000 10.115277
33 Completed 10 0.493254 239 10 10.0 1.416286 0.4920 18.696368
34 Completed 10 0.425104 155 10 10.0 0.583996 0.2173 19.135072
35 Completed 10 0.520376 199 10 10.0 0.504129 0.1880 19.636274
36 Stopped 7 0.018298 170 10 7.0 2.302517 0.9000 15.920070
37 Completed 10 0.652076 155 10 10.0 0.596140 0.2347 22.785090
38 Completed 10 0.265868 181 10 10.0 0.625975 0.2415 21.671445
39 Stopped 4 0.012236 120 10 4.0 2.302870 0.9000 10.979479
40 Stopped 7 0.011831 253 10 7.0 2.303024 0.9000 15.416995
41 Stopped 10 0.452213 49 10 10.0 0.407236 0.1543 50.717104
42 Stopped 2 0.012734 107 10 2.0 2.303225 0.9000 9.386621
43 Stopped 10 0.294500 250 10 10.0 0.641352 0.2452 14.706245
44 Stopped 2 0.024097 106 10 2.0 2.308731 0.9000 9.372424
45 Completed 10 0.604929 145 10 10.0 0.443001 0.1598 22.126401
46 Stopped 2 0.085947 106 10 2.0 2.328517 0.9000 9.214730
47 Completed 10 0.570348 56 10 10.0 0.378792 0.1396 44.051007
48 Stopped 3 0.020542 135 10 3.0 2.304046 0.9000 11.049390
49 Stopped 4 0.200606 220 10 4.0 0.962947 0.3468 10.613618
50 Stopped 2 0.045092 115 10 2.0 2.302963 0.9000 9.485898
51 Stopped 3 0.036800 65 10 3.0 2.310088 0.9000 15.031746
52 Stopped 2 0.038681 32 10 2.0 2.304635 0.9000 16.918590
53 Stopped 4 0.037208 160 10 4.0 2.302659 0.9000 10.198351
54 Stopped 2 0.061635 98 10 2.0 2.308015 0.9000 9.966621
55 Completed 10 0.529904 103 10 10.0 0.416937 0.1534 28.082242
56 Stopped 3 0.069544 96 10 3.0 2.301423 0.9000 10.638853
57 Stopped 6 0.015855 148 10 6.0 2.301256 0.8915 15.679200
58 Stopped 4 0.038892 55 10 4.0 1.163157 0.4270 22.323337
59 Stopped 3 0.042699 70 10 3.0 2.273236 0.9000 14.155716
60 Stopped 3 0.039162 164 10 3.0 2.303039 0.9004 10.252579
61 Stopped 9 0.289937 129 10 9.0 0.606612 0.2300 20.965594
62 Stopped 7 0.036945 221 10 7.0 2.295650 0.8283 15.626283
63 Stopped 2 0.021913 98 10 2.0 2.303879 0.9000 10.723158
64 Stopped 4 0.038818 160 10 4.0 2.304248 0.9000 10.184917
65 Stopped 3 0.048097 189 10 3.0 2.301573 0.9000 9.841662
66 Completed 10 0.612826 58 10 10.0 0.377928 0.1426 43.325735
67 Completed 10 0.866143 222 10 10.0 0.473422 0.1726 18.375662
68 Completed 10 0.288426 123 10 10.0 0.502528 0.1880 24.183070
69 Stopped 2 0.017845 112 10 2.0 2.303770 0.9000 9.912579
70 Stopped 4 0.048911 249 10 4.0 2.302796 0.9000 10.344501
71 Completed 10 0.312633 35 10 10.0 0.394020 0.1447 64.454283
72 Stopped 3 0.083966 172 10 3.0 2.300390 0.9000 10.279370
73 Stopped 4 0.056706 225 10 4.0 2.302379 0.9000 10.856447
74 Stopped 6 0.014103 149 10 6.0 2.302144 0.9000 15.667215
75 Stopped 2 0.034311 74 10 2.0 2.304136 0.9000 11.096837
76 Completed 10 0.483742 242 10 10.0 0.565489 0.2159 18.141505
77 Stopped 3 0.119516 161 10 3.0 2.301123 0.9000 10.098187
78 Stopped 4 0.015739 232 10 4.0 2.302351 0.9000 10.565896
79 Stopped 3 0.047845 132 10 3.0 2.308325 0.9000 10.845631
80 Stopped 10 0.776079 75 10 10.0 0.384996 0.1454 29.986539
81 Stopped 2 0.041323 55 10 2.0 2.304604 0.8246 13.535902
82 Stopped 4 0.011106 160 10 4.0 2.302692 0.9000 10.106187
83 Stopped 3 0.018517 66 10 3.0 2.303689 0.9000 15.372640
84 Stopped 2 0.012264 60 10 2.0 2.304026 0.9000 10.531683
85 Stopped 4 0.014668 35 10 4.0 2.298194 0.9000 27.838027
86 Completed 10 0.078043 52 10 10.0 0.580623 0.2247 43.795010
87 Stopped 4 0.022015 238 10 4.0 2.302830 0.9000 10.689053
88 Completed 10 0.195959 47 10 10.0 0.431066 0.1558 48.525752
89 Stopped 3 0.041319 59 10 3.0 2.281167 0.9000 15.491545
90 Completed 10 0.744754 160 10 10.0 0.472154 0.1765 21.747544
91 Stopped 4 0.027888 64 10 4.0 2.287714 0.8388 17.845500
92 Stopped 2 0.075136 108 10 2.0 2.313155 0.9000 9.238981
93 Stopped 3 0.013652 61 10 3.0 2.302975 0.9000 15.253514
94 Stopped 4 0.011606 231 10 4.0 2.302733 0.9000 10.841999
95 Completed 10 0.428660 118 10 10.0 0.518165 0.2111 24.294820
96 Stopped 4 0.021828 231 10 4.0 2.302811 0.9000 10.234604
97 Stopped 2 0.011852 106 10 2.0 2.303520 0.9000 9.551224
98 Completed 10 0.079604 42 10 10.0 0.547257 0.2107 52.699317
99 Completed 10 0.976161 72 10 10.0 0.489464 0.1827 33.683920
100 Completed 10 0.906345 234 10 10.0 0.623671 0.2423 18.756596
101 Stopped 10 0.424930 32 10 10.0 0.341438 0.1280 65.521308
102 Stopped 3 0.028962 137 10 3.0 2.302480 0.9000 10.364743
103 Stopped 3 0.010398 164 10 3.0 2.302793 0.9000 10.018759
104 Stopped 3 0.072810 142 10 3.0 2.303423 0.9000 10.928763
105 InProgress 5 0.182388 33 10 5.0 0.496784 0.1867 36.153980
106 Stopped 4 0.018105 218 10 4.0 2.302521 0.9000 10.545406
107 InProgress 6 0.161082 84 10 6.0 0.676020 0.2490 20.002124
108 Stopped 4 0.015980 197 10 4.0 2.303029 0.9000 11.074015
109 InProgress 0 0.024826 214 10 - - - -
3 trials running, 107 finished (29 until the end), 900.37s wallclock-time
validation_loss: best 0.3414376953125 for trial-id 101
--------------------
Note that we are running a variant of ASHA where underperforming trials are stopped early. This is different to our implementation in Section 29.4.1, where each training job is started with a fixed max_epochs. In the latter case, a well-performing trial which reaches the full 10 epochs, first needs to train 1, then 2, then 4, then 8 epochs, each time starting from scratch. This type of pause-and-resume scheduling can be implemented efficiently by checkpointing the training state after each epoch, but we avoid this extra complexity here. After the experiment has finished, we can retrieve the results and plot the best validation loss found so far against wall-clock time.
d2l.set_figsize()
e = load_experiment(tuner.name)
e.plot()29.5.3 Visualize the Optimization Process
Once more, we visualize the learning curves of every trial, in terms of the validation error (each color in the plot represents a trial). Compare this to asynchronous random search in Section 29.3. As we have seen for successive halving in Section 29.4, many of the trials are stopped early, at or shortly after a rung level (\(r_{\mathrm{min}}\), \(\eta r_{\mathrm{min}}\), \(\ldots\)). However, trials do not stop at the same point, because they require different amount of time per epoch. If we ran standard successive halving instead of ASHA, we would need to synchronize our workers, before we can promote configurations to the next rung level.
d2l.set_figsize([6, 2.5])
results = e.results
for trial_id in results.trial_id.unique():
df = results[results["trial_id"] == trial_id]
d2l.plt.plot(
df["st_tuner_time"],
df["validation_error"],
marker="o"
)
d2l.plt.xlabel("wall-clock time")
d2l.plt.ylabel("validation error")Text(0, 0.5, 'validation error')
29.5.4 Summary
Compared to random search, successive halving is not quite as trivial to run in an asynchronous distributed setting. To avoid synchronization points, we promote configurations as quickly as possible to the next rung level, even if this means promoting some wrong ones. In practice, this usually does not hurt much, and the gains of asynchronous versus synchronous scheduling are usually much higher than the loss of the suboptimal decision making.
29.5.5 Exercises
- One round of synchronous successive halving with \(N = \eta^K\) configurations and \(r_{\mathrm{max}} = r_{\mathrm{min}}\eta^K\) trains for \((K+1)r_{\mathrm{max}}\) epochs when every promoted trial restarts from scratch, as in Section 29.4.1.
- Show that the round trains for \(r_{\mathrm{max}}(1 + K(1 - 1/\eta))\) epochs if promoted trials resume from a checkpoint instead, and that restarting costs at most a factor \(\eta/(\eta - 1)\) more.
- In the stopping variant that the code in this section runs, every trial starts with
max_epochsequal to \(r_{\mathrm{max}}\), and at rung \(r_j\) Syne Tune lets the best fraction \(r_j/r_{j+1}\) of the trials continue, where \(r_{j+1}\) is the next rung level or \(r_{\mathrm{max}}\). Assume that exactly these fractions continue. Show that a fraction \(r_{\mathrm{min}}/r_j\) of all trials reaches \(r_j\), and compute the expected number of epochs per trial for the rung levels 2, 4 and 8 and \(r_{\mathrm{max}} = 10\). - How many times as many trials should ASHA then start in 15 minutes as asynchronous random search in Section 29.3, which trains every trial for 10 epochs? Compare with the numbers of trials in the two runs, and name a cost that the count of epochs leaves out.
- In the promotion variant of ASHA described at the start of this section, a trial at a rung is promoted once it is among the best \(\lfloor m/\eta \rfloor\) of the \(m\) results at that rung and has not been promoted before. Let \(r_{\mathrm{max}} = r_{\mathrm{min}}\eta^K\).
- Show that at least \(\eta^K\) trials must finish at rung \(r_{\mathrm{min}}\) before any trial reaches \(r_{\mathrm{max}}\), as in synchronous successive halving.
- In the stopping variant that the code runs, the first trial to reach a rung always continues, since a single recorded value does not determine a cutoff. How many trials must finish at \(r_{\mathrm{min}}\) before the first trial can reach \(r_{\mathrm{max}}\)?
- What do the two answers imply for the incumbent trajectory early in a run?
- Asynchronous decisions are based on the results that have arrived so far. Assume that the metric values of the trials at a rung are independent draws from a continuous distribution, as the validation losses are, and arrive in random order.
- In the promotion variant, the first promotion from a rung promotes the best of the first \(\eta\) results. Show that, for a large rung, this trial belongs to the best fraction \(1/\eta\) of all trials at the rung with probability \(1-(1-1/\eta)^\eta\). Evaluate this probability for \(\eta = 2, 3, 4\) and find its limit as \(\eta \to \infty\).
- In the stopping variant, the \(k\)-th trial to reach rung \(r_j\) continues if its validation loss is at most
np.quantile(values, q), wherevaluesholds the \(k\) losses recorded at the rung so far, its own included, and \(q = r_j/r_{j+1}\). Show that it continues with probability \((\lfloor (k-1)q \rfloor + 1)/k\), and evaluate this for \(q = 1/2\) and \(k = 1, \ldots, 5\). Which trials of a run benefit from this? - From
e.results, compute for each of the rung levels 2, 4 and 8 the fraction of the trials that reached the rung and were not stopped there, leaving out trials that were still running when the time limit was reached. Count a trial as stopped at rung \(r_j\) if its last reported epoch lies between \(r_j\) and the next rung level, since a stop decision can arrive an epoch late. Compare the fractions with \(r_j/r_{j+1}\).
- The text argues that the early decisions of ASHA do little harm because the ranking of configurations is often consistent across rung levels. Test this claim with the learning curves of asynchronous random search in Section 29.3, where every trial was trained for 10 epochs and reported its validation error after every epoch. Load these results with
load_experimentand the name of that tuning run.- For the trials that completed 10 epochs, compute the Spearman rank correlation (
scipy.stats.spearmanr) between the errors after 2 and after 10 epochs, and between the errors after 4 and after 10 epochs. - Replay these trials, in the order in which they reached each rung, through the stopping rule of this section’s run, applied to the validation error, the only metric that run recorded: rung levels 2, 4 and 8, continuation fractions \(1/2\), \(1/2\) and \(4/5\), the first trial at a rung always continues, and a trial whose error equals the cutoff continues. Ignore that stopping a trial would have changed the timing of later trials. How many of the trials whose error after 10 epochs lies within 0.02 of the best would have been stopped, and at which rungs?
- Repeat the replay with
grace_period=4, which gives the rung levels 4 and 8 with continuation fractions \(1/2\) and \(4/5\). Report the number of epochs that each replay trains and whether the best trial survives.
- For the trials that completed 10 epochs, compute the Spearman rank correlation (
- Implement the promotion variant of ASHA in the API of Section 29.2, as a subclass
ASHASchedulerofd2l.HPOSchedulerwith the argumentssearcher,eta,r_minandr_maxand the rung levels ofd2l.SuccessiveHalvingScheduler. Itsupdaterecords each result at its rung. Itssuggestscans the rungs from the top down and returns the first configuration that is among the best \(\lfloor m/\eta \rfloor\) of the \(m\) results at its rung and has not been promoted yet, withmax_epochsset to the next rung level. If there is no such configuration, it returns a new one at \(r_{\mathrm{min}}\).- Test the scheduler with
d2l.HPOTuner,d2l.RandomSearcherand the configuration space of Section 29.4.1, on a synthetic objective that returns at once: an error that depends onlearning_rate, decreases withmax_epochs, and carries a little noise. - Run
ASHASchedulerandd2l.SuccessiveHalvingSchedulerfor 60 trials each on this objective, with \(r_{\mathrm{min}} = 2\), \(\eta = 2\) and \(r_{\mathrm{max}} = 10\). Report the number of trials that each scheduler evaluates at every rung, the trial at which each first evaluates a configuration at \(r_{\mathrm{max}}\), and the noise-free error of the best configuration that each evaluates at \(r_{\mathrm{max}}\).
- Test the scheduler with