You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[tune] best practices for parallel hyper-param search on a pytorch-lightning module ? #8976
what does the ray community see as good ways to interface with pytorch lightning?
I'm new to Ray, and to some extent to pytorch lightning (less than a month). My use case was as follows.
I developed a model as a LightningModule. l tried a few hyperparameters that seem relevant. I wanted to do a more thorough hyperparameter search parallelized across trials, and I saw the tutorial on different hyperparameter search schedulers, which seem helpful to me so I would like to use tune for that reason as well.
Ideally, I would take my pytorch lightning module and that would be enough for ray.tune to do the search (perhaps with minor modifications to the dataloader methods, to control number of workers), it doesn’t look like there is a tutorial on this at the moment.
I trained a few things with it, but I’m not sure epoch and learning rate are being adjusted properly. The tensorflow logs at least don’t show the learning rate changing.
Do you recommend some other way of achieving the same goal (without the downsides)? There is probably also a question for the pytorch lightning project in here, about how to use their Trainer for a situations like this, but I will take that up separately.
You should be able to do this with a PTL callback. Create a custom callback and then simply call tune.report inside the callback (see the Keras callback for an example) .
Can you try this out and let me know if that works?
Thanks, that was helpful. I got what I needed working.
About your tutorial, I tried it out, the Asha part worked well.
I checked the population based one, mostly to see what the checkpointing and resuming would look like.
This code worked for me.
class CheckpointCallback(Callback):
def on_validation_end(self, trainer, pl_module):
path = tune.make_checkpoint_dir(trainer.global_step)
trainer.save_checkpoint(os.path.join(path, "checkpoint"))
tune.save_checkpoint(path)
def train_mnist_tune_checkpoint(config, checkpoint=None):
if checkpoint is None or len(checkpoint) == 0: # sometimes gets {} as input.
checkpoint_path = None
else:
checkpoint_path = os.path.join(checkpoint, "checkpoint")
trainer = pl.Trainer(
max_epochs=10,
gpus=1,
progress_bar_refresh_rate=0,
callbacks=[CheckpointCallback(),
TuneReportCallback(),
pl.callbacks.LearningRateLogger()
],
resume_from_checkpoint=checkpoint_path)
model = LightningMNISTClassifier(config)
trainer.fit(model)
By worked I mean it the state was preserved, including optimizer and learning rate scheduler state in the trainer. (not just the epoch). This might be the desirable way to reload for a scheduler that stops and later wants to resume training from an old checkpoint as if nothing happened.
On the other hand, I was logging the learning rates and I did not see jumps on the plot, despite perturbations from PBT being logged by ray.tune. Maybe the config perturbations get overwritten by reloading through the trainer the way I do. Perhaps that's why you were reloading differently, but simply reloading the model without reloading the Trainer could break something else, eg, if we are not perturbing the training rate. I'm not familiar with the pbt scheduler so maybe I'm missing the point.
Here's a notebook of your code, but with modifications as above, and a couple other things people new to pl and or ray.tune may run into, as I did.
type hiccups with tune.choice() (causing tensorboard to not log num layers hyperparam, and dataloader to fail if we modify the batch size, though I saw you made it into an int() there.
type hiccups with output metrics from pytorch lightning (causing dataframe to have tensors in it) also
{} being fed as checkpoint, causing os.path.join() to fail.
showing how to set the gpu stuff (ie, you set your gpu to 1 in trainer)
PS one slight remaining pain point is that there are separate loggers for pl, and for tune (same model effectively gets two log folders and two entries in tensorboard). Ideally I'd give the Trainer the logger from tune (or something to identify it) so that things logged from both sides fall under the same name and color in tensorboard.
Thanks for your feedback, Oscar. I'll look at this closer next week. It seems there are some things we might include in the walkthrough, but other things might have to be addressed in Tune/PBT (like the {} checkpoint paths). I'll get back to this.
Yeah, thanks for looking into it. These two tools are a great combo for people working with pytorch. The trainer callback API in PL may allow for PBT perturbations to be done after all model and trainer state has been restored,and before training resumes, but it is hard to tell without reading PL code what the state of the system is meant to be when each callback method is invoked.
Just as a quick update, we are working on a small PR addressing some of these issues. We will fix the PT tensor conversion with tensor.item() and add GPU support (the former is actually necessary for the latter to work).
For the empty checkpoints, a general if checkpoint: should suffice, so we will use that.
We will discuss the tune.choice() and logging redirection later. It's a bit unfortunate that PTL cannot deal with numpy int64 types. A possible solution would be to use random.choice instead of np.random.choice internally. I'll get back to you on that later.
We decided to use random.choice() for tune.choice(), preserving types like primitive integers. For reference, this is how you can put the PTL logs into the trial root dir, so it doesn't show up twice in tensorboard:
The version="." is actually a bit hacky, since PyTorch lightning tries to force us to use log_dir/version_name for tensorboard logs. By using "." we trick os.path.join in using the root directory.
Lastly I'll look into the logging of perturbed parameters sometime later this or early next week.
Activity
richardliaw commentedon Jun 16, 2020
You should be able to do this with a PTL callback. Create a custom callback and then simply call
tune.reportinside the callback (see the Keras callback for an example) .Can you try this out and let me know if that works?
richardliaw commentedon Jun 30, 2020
Hey @orm011, we just merged a walkthrough for using Tune with Pytorch Lightning. Would you be interested in giving it a try? https://docs.ray.io/en/master/tune/tutorials/tune-pytorch-lightning.html
orm011 commentedon Jul 2, 2020
Thanks, that was helpful. I got what I needed working.
About your tutorial, I tried it out, the Asha part worked well.
I checked the population based one, mostly to see what the checkpointing and resuming would look like.
This code worked for me.
By worked I mean it the state was preserved, including optimizer and learning rate scheduler state in the trainer. (not just the epoch). This might be the desirable way to reload for a scheduler that stops and later wants to resume training from an old checkpoint as if nothing happened.
On the other hand, I was logging the learning rates and I did not see jumps on the plot, despite perturbations from PBT being logged by ray.tune. Maybe the config perturbations get overwritten by reloading through the trainer the way I do. Perhaps that's why you were reloading differently, but simply reloading the model without reloading the Trainer could break something else, eg, if we are not perturbing the training rate. I'm not familiar with the pbt scheduler so maybe I'm missing the point.
Here's a notebook of your code, but with modifications as above, and a couple other things people new to pl and or ray.tune may run into, as I did.
https://github.com/orm011/some_nbs/blob/6999682c36d7ec493c8bd60e66a3b0efcde1c084/ray_pl%20(1).ipynb
PS one slight remaining pain point is that there are separate loggers for pl, and for tune (same model effectively gets two log folders and two entries in tensorboard). Ideally I'd give the Trainer the logger from tune (or something to identify it) so that things logged from both sides fall under the same name and color in tensorboard.
krfricke commentedon Jul 2, 2020
Thanks for your feedback, Oscar. I'll look at this closer next week. It seems there are some things we might include in the walkthrough, but other things might have to be addressed in Tune/PBT (like the {} checkpoint paths). I'll get back to this.
orm011 commentedon Jul 2, 2020
Yeah, thanks for looking into it. These two tools are a great combo for people working with pytorch. The trainer callback API in PL may allow for PBT perturbations to be done after all model and trainer state has been restored,and before training resumes, but it is hard to tell without reading PL code what the state of the system is meant to be when each callback method is invoked.
krfricke commentedon Jul 13, 2020
Just as a quick update, we are working on a small PR addressing some of these issues. We will fix the PT tensor conversion with
tensor.item()and add GPU support (the former is actually necessary for the latter to work).For the empty checkpoints, a general
if checkpoint:should suffice, so we will use that.We will discuss the
tune.choice()and logging redirection later. It's a bit unfortunate that PTL cannot deal with numpy int64 types. A possible solution would be to userandom.choiceinstead ofnp.random.choiceinternally. I'll get back to you on that later.Thanks for your insights, it's very helpful.
krfricke commentedon Jul 13, 2020
We address all your issues in this PR: #9451
We decided to use
random.choice()fortune.choice(), preserving types like primitive integers. For reference, this is how you can put the PTL logs into the trial root dir, so it doesn't show up twice in tensorboard:The
version="."is actually a bit hacky, since PyTorch lightning tries to force us to uselog_dir/version_namefor tensorboard logs. By using"."we trickos.path.joinin using the root directory.Lastly I'll look into the logging of perturbed parameters sometime later this or early next week.
If there's something else, please let me know.