Lightning-AI / Lightning-AI/pytorch-lightning

lr_find crashes with Deepspeed

Open
#13,370 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug strategy: deepspeed tuner
Dominant language
Python
Stars
31.4k
Forks
3.8k
Avg merge
6d 7h
Merged PRs (30d)
6

Description

## 🐛 Bug

When running Trainer.tuner.lr_find using strategy="deepspeed_stage_2", the procedure fails with following error:
```
AttributeError: 'BoringModel' object has no attribute 'save_checkpoint'
```

Full traceback

```
Traceback (most recent call last):
File "src/tmp.py", line 161, in
run()
File "src/tmp.py", line 154, in run
lr_finder = trainer.tuner.lr_find(
File "/root/envs/star/lib/python3.8/site-packages/pytorch_lightning/tuner/tuning.py", line 185, in lr_find
result = self.trainer.tune(
File "/root/envs/star/lib/python3.8/site-packages/pytorch_lightning/trainer/trainer.py", line 1099, in tune
result = self.tuner._tune(model, scale_batch_size_kwargs=scale_batch_size_kwargs, lr_find_kwargs=lr_find_kwargs)
File "/root/envs/star/lib/python3.8/site-packages/pytorch_lightning/tuner/tuning.py", line 53, in _tune
result["lr_find"] = lr_find(self.trainer, model, **lr_find_kwargs)
File "/root/envs/star/lib/python3.8/site-packages/pytorch_lightning/tuner/lr_finder.py", line 232, in lr_find
trainer.save_checkpoint(str(save_path))
File "/root/envs/star/lib/python3.8/site-packages/pytorch_lightning/trainer/trainer.py", line 1913, in save_checkpoint
self.checkpoint_connector.save_checkpoint(filepath, weights_only)
File "/root/envs/star/lib/python3.8/site-packages/pytorch_lightning/trainer/connectors/checkpoint_connector.py", line 478, in save_checkpoint
self.trainer.training_type_plugin.save_checkpoint(_checkpoint, filepath)
File "/root/envs/star/lib/python3.8/site-packages/pytorch_lightning/plugins/training_type/deepspeed.py", line 751, in save_checkpoint
self.deepspeed_engine.save_checkpoint(filepath, client_state=checkpoint)
File "/root/envs/star/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1177, in __getattr__
raise AttributeError("'{}' object has no attribute '{}'".format(
AttributeError: 'BoringModel' object has no attribute 'save_checkpoint'
```

### To Reproduce

```
import os

import torch
from torch.utils.data import DataLoader, Dataset

from pytorch_lightning import LightningModule, Trainer

class RandomDataset(Dataset):
def __init__(self, size, length):
self.len = length
self.data = torch.randn(length, size)

def __getitem__(self, index):
return self.data[index]

def __len__(self):
return self.len

class BoringModel(LightningModule):
def __init__(self):
super().__init__()
self.lr = 0.1
self.layer = torch.nn.Linear(32, 2)

def forward(self, x):
return self.layer(x)

def training_step(self, batch, batch_idx):
loss = self(batch).sum().abs()
self.log("train_loss", loss)
return {"loss": loss}

def validation_step(self, batch, batch_idx):
loss = self(batch).sum().abs()
self.log("valid_loss", loss)

def test_step(self, batch, batch_idx):
loss = self(batch).sum().abs()
self.log("test_loss", loss)

def configure_optimizers(self):
return torch.optim.SGD(self.layer.parameters(), lr=self.lr)

def run():
train_data = DataLoader(RandomDataset(32, 4), batch_size=8)
val_data = DataLoader(RandomDataset(32, 4), batch_size=8)
test_data = DataLoader(RandomDataset(32, 4), batch_size=8)

model = BoringModel()
trainer = Trainer(
accelerator="gpu", devices=1,
strategy="deepspeed_stage_2",
precision=16,
default_root_dir=os.getcwd(),
limit_train_batches=1,
limit_val_batches=1,
limit_test_batches=1,
num_sanity_val_steps=0,
max_epochs=1,
enable_model_summary=False,
)

lr_finder = trainer.tuner.lr_find(
model, train_dataloaders=train_data, val_dataloaders=val_data)

print(lr_finder.suggestion())

if __name__ == "__main__":
run()
```

### Expected behavior

Learning rate finder should work with deepspeed or else raise an exception stating that deepspeed is not supported.

### Environment

* CUDA:
- GPU:
- Tesla V100-SXM2-32GB
- available: True
- version: 11.3
* Packages:
- numpy: 1.21.0
- pyTorch_debug: False
- pyTorch_version: 1.10.1+cu113
- pytorch-lightning: 1.5.10
- tqdm: 4.62.3
* System:
- OS: Linux
- architecture:
- 64bit
- processor: x86_64
- python: 3.8.10
- version: #1 SMP Tue Apr 13 18:55:45 EDT 2021

### Additional context

It is valuable to be able to run the lr_finder with deepspeed for configurations where large batch sizes matter (e.g., contrative learning).

cc @akihironitta @borda @rohitgr7 @SeanNaren @awaelchli

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with pytorch_lightning/tuner/lr_finder.py and the deepspeed save_checkpoint path in plugins/training_type/deepspeed.py. Run the supplied reproduction with strategy="deepspeed_stage_2" and trace how lr_find saves state. Done means the learning-rate finder works with DeepSpeed or raises an explicit unsupported-feature exception.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
distributed-systems, machine-learning
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.