Lightning-AI / Lightning-AI/pytorch-lightning

Hook `configure_model` is not called from the CLI

Open
#19,110 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

### Bug description

The hook `configure_model` is recommended to create modules in a strategy and precision aware context.

When trigger a run from the CLI, the `LightningCLI` try to initialise the optimisers without calling the `configure_model` hook:

https://github.com/Lightning-AI/pytorch-lightning/blob/520c1e4713340f5bbf66de215471c2863e8fbdf2/src/lightning/pytorch/cli.py#L659-L660

Thus, if the model was defined in the `configure_model` hook, it will be never called so the optimiser will get an empty set of parameters.

As a solution, we only have to call the `configure_model` before the optimiser.

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

v2.1, master

### How to reproduce the bug

_No response_

### Error messages and logs

```
ValueError: optimizer got an empty parameter list
```

### 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_

cc @mauvilsa

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 in src/lightning/pytorch/cli.py around lines 659-660 and trace the CLI's optimizer initialization order. Ensure configure_model is called before optimizer setup so parameters created by the hook are available, then verify that the optimizer no longer receives an empty parameter list.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
cli, machine-learning
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.