-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Fix: Override hparams via CLI #21455
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Fix: Override hparams via CLI #21455
Conversation
for more information, see https://pre-commit.ci
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.
mauvilsa
left a comment
There was a problem hiding this 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.
src/lightning/pytorch/cli.py
Outdated
| except KeyError: | ||
| sys.stderr.write("Parsing of ckpt_path hyperparameters failed!\n") | ||
| raise |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is changing the prior behavior, going from an exit that prints the usage, to an ugly exception with a long stack trace. The behavior should not change. Maybe it would work correctly by doing
| except KeyError: | |
| sys.stderr.write("Parsing of ckpt_path hyperparameters failed!\n") | |
| raise | |
| except KeyError as ex: | |
| sys.stderr.write("Parsing of ckpt_path hyperparameters failed!\n") | |
| parser.error(str(ex), ex) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
| except KeyError: | ||
| sys.stderr.write("Parsing of ckpt_path hyperparameters failed!\n") | ||
| raise | ||
| self.parse_arguments(parser, args) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like this the entire parsing is done twice. The speed is not a problem, since training/predicting is normally significantly slower than the parsing. However, there might be some edge case where the first parsing fails, even though this second one would succeed. But, I am fine with leaving it like this. If there are problematic edge cases, then we can fix them when they happen.
| assert isinstance(cli.model, BoringCkptPathSubclass) | ||
| assert not isinstance(cli.model, BoringCkptPathSubclass) | ||
| assert cli.model.hidden_dim == 8 | ||
| assert cli.model.extra is True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did this change? Old behaviors should not change, unless there was bug. Was there a bug? If not, revert back.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the old behavior was effectively a bug. It violated the standard priority Checkpoint < CLI args.
Previously, explicit flags like --model=BoringCkptPathModel were ignored in favor of the checkpoint class. The updated test confirms that the CLI argument now correctly takes precedence.
This also complements PR #21408: while that PR handles parameter adaptation, this ensures the class itself can be overridden. Together, they enable a clean Training -> Inference workflow with different classes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One drawback of setting hparams as defaults is that the typical left-to-right parsing priority of command line arguments is not respected. For example, a command like --model=BoringCkptPathModel --ckpt_path=... can be confusing, because --ckpt_path appears after --model, and arguments to the right usually take precedence. Since the checkpoint specifies a particular model, it would be logical for it to override the model argument. It might make sense to require that if --ckpt_path is provided, it should be the first argument after the subcommand. This would maintain the convention that arguments on the right have higher priority. However, enforcing --ckpt_path as the first argument could be difficult to implement cleanly.
Ignoring the order of command line arguments, the reason for including the --model=BoringCkptPathModel argument in this test is that the model is required. Without this argument, parsing fails due to the missing model. In fact, this is a case where the initial parsing fails, but the second parsing (after applying checkpoint hparams defaults) succeeds. Ideally, if a checkpoint is provided, specifying a model argument should not be mandatory, since the checkpoint already contains one. The model argument could be optional and used to override the checkpoint's model if given, but not required. However, implementing this might be tricky. The parser must still mark the model as required; otherwise, the output of --help would be misleading. One possible solution is to temporarily patch the subparsers during parsing to make the model argument optional, and then, if the model is not provided, call parser.error.
| assert cli.config_init.predict.model.layer.out_features == 3 | ||
|
|
||
| err = StringIO() | ||
| with mock.patch("sys.argv", ["any.py"] + cli_args), redirect_stderr(err), pytest.raises(SystemExit): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| with mock.patch("sys.argv", ["any.py"] + cli_args), redirect_stderr(err), pytest.raises(SystemExit): | |
| with mock.patch("sys.argv", ["any.py"] + cli_args), redirect_stderr(err), pytest.raises(SystemExit): |
Revert this back, since this behavior should not change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
tests/tests_pytorch/test_cli.py
Outdated
| target_classes = 10 | ||
| assert new_cli.model.num_classes == target_classes, ( | ||
| f"Checkpoint restoration failed! Expected num_classes {target_classes}, got {new_cli.model.num_classes}" | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
num_classes=10 is the default of the model and not overridden anywhere. Maybe this assert is unnecessary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Removed the redundant assertion.
- Changed error in _parse_ckpt_path and tests for it
|
I've addressed all the feedback |
Codecov Report✅ All modified and coverable lines are covered by tests.
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 |
| if parser._subcommands_action is None: | ||
| return |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How come you added this? The if condition in line 575 is supposed to already return when no subcommands are defined. Maybe the condition is not robust enough, since it would not work when a model has a subcommand parameter. Still, if there are no subcommands, why execute the lines from 577 up to here? Maybe better instead to change line 575 to if parser._subcommands_action is None or not self.config.get("subcommand"):.
What does this PR do?
Currently, when
LightningCLIloads 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.001instead of the value stored inepoch=1.ckpt: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
jsonargparsepriority order:--config)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
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
📚 Documentation preview 📚: https://pytorch-lightning--21455.org.readthedocs.build/en/21455/