Lightning module
+Lightning Module interface
A lightning module is a strict superclass of nn.Module, it provides a standard interface for the trainer to interact with the model.
The easiest thing to do is copy this template and modify accordingly.
@@ -590,7 +594,7 @@ def add_model_specific_args(parent_parser, root_dir): diff --git a/LightningModule/methods/index.html b/LightningModule/methods/index.html new file mode 100644 index 00000000..7aa0253f --- /dev/null +++ b/LightningModule/methods/index.html @@ -0,0 +1,274 @@ + + + + + + + + + + +Lightning modules are strict superclasses of torch.nn.Module. A LightningModule offers the following in addition to that API.
++
freeze
+Freeze all params for inference
+model = MyLightningModule(...)
+model.freeze()
+
+
++
load_from_metrics
+This is the easiest/fastest way which uses the meta_tags.csv file from test-tube to rebuild the model. +The meta_tags.csv file can be found in the test-tube experiment save_dir.
+pretrained_model = MyLightningModule.load_from_metrics(
+ weights_path='/path/to/pytorch_checkpoint.ckpt',
+ tags_csv='/path/to/test_tube/experiment/version/meta_tags.csv',
+ on_gpu=True,
+ map_location=None
+)
+
+# predict
+pretrained_model.freeze()
+y_hat = pretrained_model(x)
+
+
+Params
+| Param | +description | +
|---|---|
| weights_path | +Path to a pytorch checkpoint | +
| tags_csv | +Path to meta_tags.csv file generated by the test-tube Experiment | +
| on_gpu | +if True, puts model on GPU. Make sure to use transforms option if model devices have changed | +
| map_location | +A dictionary mapping saved weight GPU devices to new GPU devices | +
Returns
+LightningModule - The pretrained LightningModule
++
unfreeze
+Unfreeze all params for inference
+model = MyLightningModule(...)
+model.unfreeze()
+
+
+ Lightning can automate saving and loading checkpoints.
++
Model saving
+To enable checkpointing, define the checkpoint callback and give it to the trainer.
+from pytorch_lightning.utils.pt_callbacks import ModelCheckpoint
+
+checkpoint_callback = ModelCheckpoint(
+ filepath='/path/to/store/weights.ckpt',
+ save_best_only=True,
+ verbose=True,
+ monitor='val_loss',
+ mode='min'
+)
+
+trainer = Trainer(checkpoint_callback=checkpoint_callback)
+
Lightning supports model training on a cluster managed by SLURM in the following cases:
+-
+
- Training on single or multi-cpus only. +
- Training on single or multi-gpus on the same node. +
- Coming SOON: Training across multiple nodes. +
+
Running grid search on a cluster
+To use lightning to run a hyperparameter search (grid-search or random-search) on a cluster do 4 things:
+(1). Define the parameters for the grid search
+from test_tube import HyperOptArgumentParser
+
+# subclass of argparse
+parser = HyperOptArgumentParser(strategy='random_search')
+parser.add_argument('--learning_rate', default=0.002, type=float, help='the learning rate')
+
+# let's enable optimizing over the number of layers in the network
+parser.opt_list('--nb_layers', default=2, type=int, tunable=True, options=[2, 4, 8])
+
+hparams = parser.parse_args()
+
+
+(2). Define the cluster options in the SlurmCluster object (over 5 nodes and 8 gpus)
+from test_tube.hpc import SlurmCluster
+
+# hyperparameters is a test-tube hyper params object
+# see https://williamfalcon.github.io/test-tube/hyperparameter_optimization/HyperOptArgumentParser/
+hyperparams = args.parse()
+
+# init cluster
+cluster = SlurmCluster(
+ hyperparam_optimizer=hyperparams,
+ log_path='/path/to/log/results/to',
+ python_cmd='python3'
+)
+
+# let the cluster know where to email for a change in job status (ie: complete, fail, etc...)
+cluster.notify_job_status(email='some@email.com', on_done=True, on_fail=True)
+
+# set the job options. In this instance, we'll run 20 different models
+# each with its own set of hyperparameters giving each one 1 GPU (ie: taking up 20 GPUs)
+cluster.per_experiment_nb_gpus = 8
+cluster.per_experiment_nb_nodes = 5
+
+# we'll request 10GB of memory per node
+cluster.memory_mb_per_node = 10000
+
+# set a walltime of 10 minues
+cluster.job_time = '10:00'
+
+
+(3). Give trainer the cluster_manager in your main function:
+from pytorch_lightning import Trainer
+
+def train_fx(trial_hparams, cluster_manager, _):
+ # hparams has a specific set of hyperparams
+
+ my_model = MyLightningModel()
+
+ # give the trainer the cluster object
+ trainer = Trainer(cluster=cluster_manager)
+ trainer.fit(my_model)
+
+
+
+(4). Start the grid search
+# run the models on the cluster
+cluster.optimize_parallel_cluster_gpu(
+ train_fx,
+ nb_trials=20,
+ job_name='my_grid_search_exp_name',
+ job_display_name='my_exp')
+
+
+That's it! The SlurmCluster object will automatically checkpoint the lightning model and resubmit if it runs into the walltime!
++
Walltime auto-resubmit
+Lightning automatically resubmits jobs when they reach the walltime. You get this behavior for free if you give lightning +a slurm cluster object.
+def my_main_fx(hparams, slurm_manager, _):
+ trainer = Trainer(cluster=slurm_manager)
+
+
+(See the grid search example above for cluster configuration). +With this feature lightning will:
+-
+
- automatically checkpoint the model +
- checkpoint the trainer session +
- resubmit a continuation job. +
- load the checkpoint and trainer session in the new model +
Computing cluster (SLURM)
-
-
- Automatic checkpointing -
- Automatic saving, loading -
- Running grid search on a cluster -
- Walltime auto-resubmit +
- Running grid search on a cluster +
- Walltime auto-resubmit
Debugging
PYTORCH-LIGHTNING DOCUMENTATION
Main Docs
New project Quick Start
@@ -190,42 +198,10 @@Training loop
--
-
- Accumulate gradients -
- Check GPU usage -
- Check which gradients are nan -
- Check validation every n epochs -
- Display metrics in progress bar -
- Force training for min or max epochs -
- Inspect gradient norms -
- Hooks -
- Learning rate annealing -
- Make model overfit on subset of data -
- Multiple optimizers (like GANs) -
- Set how much of the training set to check (1-100%) -
- training_step function -
Validation loop
--
-
- Display metrics in progress bar -
- hooks -
- Set how much of the validation set to check (1-100%) -
- Set validation check frequency within 1 training epoch (1-100%) -
- validation_step function -
- Why does validation run first for 5 steps? -
Distributed training
--
-
- Single-gpu -
- Multi-gpu -
- Multi-node -
- 16-bit mixed precision -
Checkpointing
-
-
- Model saving -
- Model loading +
- Model saving +
- Model loading
Computing cluster (SLURM)
-
@@ -233,6 +209,50 @@
- Automatic saving, loading
- Running grid search on a cluster
- Walltime auto-resubmit +
Debugging
+-
+
- Fast dev run +
- Inspect gradient norms +
- Log GPU usage +
- Make model overfit on subset of data +
- Print the parameter count by layer +
- Pring which gradients are nan +
Distributed training
+ +Experiment Logging
+-
+
- Display metrics in progress bar +
- Log arbitrary metrics +
- Log metric row every k batches +
- Process position +
- Save a snapshot of all hyperparameters +
- Snapshot code for a training run +
- Write logs file to csv every k batches +
Training loop
+-
+
- Accumulate gradients +
- Anneal Learning rate +
- Force training for min or max epochs +
- Force disable early stop +
- Use multiple optimizers (like GANs) +
- Set how much of the training set to check (1-100%) +