Lightning-AI / Lightning-AI/pytorch-lightning

`configure_model` is incompatible with the `BaseFinetuning` behavior when fitting

Open
#19,658 0 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug callback: finetuning help wanted ver: 2.1.x
Dominant language
Python
Stars
31.4k
Forks
3.8k
Avg merge
6d 7h
Merged PRs (30d)
6

Description

### Bug description

Based on the current callback orders, The `Finetuning` class will always be incompatible with any `LightningModule` that utilize `configure_model` method. The current callback sequence is
`Callback.setup`
-> `LightningModule.configure_model`
-> `LightningModule.configure_optimizers`
-> `Callback.on_fit_start`

However, The `BaseFinetuning` calls `freeze_before_training` at `setup`, where modules inside the `configure_model` has not been instantiated yet.

### What version are you seeing the problem on?

v2.1

### How to reproduce the bug

```python
from lightning import LightningModule
import torch
from torchvision import models
class MyModel(LightningModule):
def configure_model(self):
self.backbone = models.resnet18()
def configure_optimizers(self):
return torch.optim.SGD(lr=1e-3)
```

### Error messages and logs

```
# Error messages and logs here please
```

### Environment

Current environment

```
#- Lightning Component (e.g. Trainer, LightningModule, LightningApp, LightningWork, LightningFlow):
#- PyTorch Lightning Version (e.g., 1.5.0):
#- Lightning App Version (e.g., 0.5.2):
#- PyTorch Version (e.g., 2.0):
#- Python version (e.g., 3.9):
#- OS (e.g., Linux):
#- CUDA/cuDNN version:
#- GPU models and configuration:
#- How you installed Lightning(`conda`, `pip`, source):
#- Running environment of LightningApp (e.g. local, cloud):
```

### More info

_No response_

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 by tracing the listed callback sequence around BaseFinetuning.setup, LightningModule.configure_model, configure_optimizers, and Callback.on_fit_start, then reproduce the issue with the provided MyModel example. Done means a LightningModule that creates modules in configure_model can be used with BaseFinetuning without the setup-time incompatibility.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, pytorch
Domain
machine-learning
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.