Skip to content

Fix: Override hparams via CLI#21455

Open
Berezin-Leonid wants to merge 13 commits intoLightning-AI:masterfrom
Berezin-Leonid:issue-21255-cli-hparams-override
Open

Fix: Override hparams via CLI#21455
Berezin-Leonid wants to merge 13 commits intoLightning-AI:masterfrom
Berezin-Leonid:issue-21255-cli-hparams-override

Conversation

@Berezin-Leonid
Copy link
Copy Markdown

@Berezin-Leonid Berezin-Leonid commented Dec 28, 2025

Allow CLI arguments to override checkpoint hyperparameters in LightningCLI

What does this PR do?

Currently, when LightningCLI loads a configuration from a checkpoint (using --ckpt_path), the hyperparameters stored in the checkpoint overwrite any arguments passed via the command line. This behavior prevents users from overriding specific parameters (e.g., changing the learning rate) when resuming training or fine-tuning.

This PR adjusts the configuration merging logic. Now, arguments provided explicitly via the CLI (including parameters defined in config files via --config) take precedence over the hyperparameters loaded from the checkpoint.

Example:
With this change, the following command will correctly use lr=0.001 instead of the value stored in epoch=1.ckpt:

python main.py fit --ckpt_path=epoch=1.ckpt --model.learning_rate=0.001

Implementation Details

To achieve this, I modified the loading logic so that hyperparameters from the checkpoint are applied as parser defaults rather than being merged directly into the configuration object.

This leverages the standard jsonargparse priority order:

  1. Command Line Arguments (Highest priority)
  2. Config Files (e.g., --config)
  3. Defaults (now includes Checkpoint Hyperparameters)

This ensures that any value explicitly provided by the user will correctly override the value stored in the checkpoint.

Addressing the conversation in #21255 about CLI override priority.

Before submitting
  • Was this discussed/agreed via a GitHub issue? (not for typos and docs)
  • Did you read the contributor guideline, Pull Request section?
  • Did you make sure your PR does only one thing, instead of bundling different changes together?
  • Did you make sure to update the documentation with your changes? (if necessary)
  • Did you write any new necessary tests? (not for typos and docs)
  • Did you verify new and existing tests pass locally with your changes?
  • Did you list all the breaking changes introduced by this pull request?
  • Did you update the CHANGELOG? (not for typos, docs, test updates, or minor internal changes/refactors)

PR review

Anyone in the community is welcome to review the PR.
Before you start reviewing, make sure you have read the review guidelines. In short, see the following bullet-list:

Reviewer checklist
  • Is this pull request ready for review? (if not, please submit in draft mode)
  • Check that all items from Before submitting are resolved
  • Make sure the title is self-explanatory and the description concisely explains the PR
  • Add labels and milestones (and optionally projects) to the PR so it can be classified

📚 Documentation preview 📚: https://pytorch-lightning--21455.org.readthedocs.build/en/21455/

    Allow CLI arguments to override checkpoint hyperparameters in LightningCLI
@github-actions github-actions Bot added the pl Generic label for PyTorch Lightning package label Dec 28, 2025
Leonid added 2 commits December 28, 2025 23:54
Updated `test_lightning_cli_ckpt_path_argument_hparams_subclass_mode` to match the new resolution logic.

Previosly, the test expected the model class from the checkpoint to override the `--model` argument provided in the CLI.
With this fix, explicit CLI arguments take precedence. the test now assert that `BoringCkptPathModel` (provided via CLI) is instantiated instead of `BoringCkptPathSubclass` (stored in checkpoint).

Also updated `test_lightning_cli_ckpt_path_argument_hparams` to catch `KeyError` (NSKeyError) instaed of expecting `SystemExit`, as the new `set_defaults` mechanism raises a precise key error on mismatch.
Copy link
Copy Markdown
Contributor

@mauvilsa mauvilsa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea of setting the hparams as defaults. So it seems this wasn't as difficult as I thought. There are a few details I think should change as I comment below.

Please change the pull request description avoiding Fixes #21255. I mean, merging this pull request should not close #21255, since it isn't an actual fix for it. It would be a fix for your specific comment, but not the entire issue. As a side note, the fix for #21255 would be #21408.

Comment thread src/lightning/pytorch/cli.py Outdated
Comment thread src/lightning/pytorch/cli.py
Comment thread tests/tests_pytorch/test_cli.py
Comment thread tests/tests_pytorch/test_cli.py
Comment thread tests/tests_pytorch/test_cli.py Outdated
Comment thread tests/tests_pytorch/test_cli.py Outdated
- Changed error in _parse_ckpt_path and tests for it
@Berezin-Leonid
Copy link
Copy Markdown
Author

Berezin-Leonid commented Jan 12, 2026

I've addressed all the feedback
Ready for review!

@codecov
Copy link
Copy Markdown

codecov Bot commented Jan 14, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79%. Comparing base (9df1910) to head (fb86acf).
⚠️ Report is 1 commits behind head on master.
✅ All tests successful. No failed tests found.

❗ There is a different number of reports uploaded between BASE (9df1910) and HEAD (fb86acf). Click for more details.

HEAD has 717 uploads less than BASE
Flag BASE (9df1910) HEAD (fb86acf)
cpu 196 33
python 18 3
lightning_fabric 53 0
pytest 98 0
python3.12 53 9
python3.10 18 3
lightning 90 15
python3.12.7 54 9
python3.11 36 6
python3.13 17 3
pytorch2.2.2 9 3
pytest-full 98 33
pytorch2.8 18 6
pytorch_lightning 53 18
pytorch2.4.1 9 3
pytorch2.1 18 6
pytorch2.7 9 3
pytorch2.6 9 3
pytorch2.3 9 3
pytorch2.9 8 3
pytorch2.5.1 9 3
Additional details and impacted files
@@            Coverage Diff            @@
##           master   #21455     +/-   ##
=========================================
- Coverage      87%      79%     -8%     
=========================================
  Files         270      267      -3     
  Lines       24059    24008     -51     
=========================================
- Hits        20855    18959   -1896     
- Misses       3204     5049   +1845     

Comment thread src/lightning/pytorch/cli.py Outdated
Leonid and others added 3 commits January 20, 2026 13:40
- Implemented `_relax_model_requirements` to dynamically toggle the mandatory
  status of the model argument during parsing.
- If `--ckpt_path` is present in the CLI arguments, the 'model' key is
  temporarily removed from the subparser's required arguments.
- Updated `test_cli.py` to verify that:
    1. The CLI works without `--model` when a valid checkpoint is used.
    2. A `SystemExit` is still raised with an appropriate error message
       if the checkpoint is invalid/empty.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pl Generic label for PyTorch Lightning package

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants