Lightning-AI / Lightning-AI/pytorch-lightning

No action for key "ckpt_path" -> ckpt_path not available for linking

Open
#18,885 7 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

### Bug description

Hi,

Thanks for all the hard work on making it possible to configure Lightning experiments through a simple config!

I want to link my ckpt_path to a callback using link_arguments together wit h LightningCLI (in my case because the callback is used to save out a set of predictions and the ckpt_path is used for naming the prediction set filename, but I would have thought needing your ckpt_path in other places in the config.yaml isn't that uncommon?). This is how I implemented the linking.

```
class MyLightningCLI(LightningCLI):
def add_arguments_to_parser(self, parser):
parser.link_arguments("ckpt_path", "trainer.callbacks.init_args.ckpt_path")

cli = MyLightningCLI(
MyLitModule,
MyLitDataModule,
run=True,
)
```

when running python predict_my_model.py predict --config my_config.yaml I unfortunately get the following error:

ValueError: No action for key "ckpt_path".
Going through the code, it seems like ckpt_path does not have an action attached it, [find_parent_or_child_actions](https://github.com/omni-us/jsonargparse/blob/main/jsonargparse/_link_arguments.py#L28) does not find one.

----

I've first incorrectly raised this on jsonargparse, where I got the following response:

The problem is not in jsonargparse. The error happens because ckpt_path is added in line [cli.py#L497](https://github.com/Lightning-AI/lightning/blob/1210deb5a0ff720b0ad66e38149cd28471d760ca/src/lightning/pytorch/cli.py#L497), which is after add_arguments_to_parser gets called (line [cli.py#L494](https://github.com/Lightning-AI/lightning/blob/1210deb5a0ff720b0ad66e38149cd28471d760ca/src/lightning/pytorch/cli.py#L494)). That is, when the link_arguments is run, ckpt_path does not yet exist in the parser.

How can this be fixed? You could override [_prepare_subcommand_parser](https://github.com/Lightning-AI/lightning/blob/1210deb5a0ff720b0ad66e38149cd28471d760ca/src/lightning/pytorch/cli.py#L492), having the same code, but moving _add_arguments to be after add_method_arguments. Though, note that this method starts with underscore _, so not guaranteed to be stable.

There could be other more proper solutions. But maybe this is not the correct place to discuss it. Please create an issue in lightning.

---

Thanks!

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

v2.0

### How to reproduce the bug

```python
See code above, can make a more complete example if needed.
```

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

cc @carmocca @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 _prepare_subcommand_parser, especially the ordering near lines 494-497, and inspect how jsonargparse link_arguments finds parser actions. Reproduce the provided LightningCLI example and verify that ckpt_path can be linked to trainer.callbacks.init_args.ckpt_path without the "No action" error.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, pytorch
Domain
ai, cli
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.