-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Fix LightningCLI docs after overhaul of the documentation #14976
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
Merged
lexierule
merged 12 commits into
Lightning-AI:master
from
mauvilsa:cli-fix-docs-after-restructure
Nov 9, 2022
Merged
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
82a50db
- Fix diverse issues introduced when the documentation was restructured.
mauvilsa a304f17
Update docs/source-pytorch/cli/lightning_cli.rst
mauvilsa edcf800
Merge branch 'master' into cli-fix-docs-after-restructure
mauvilsa a5cbdd3
- Fix diverse issues introduced when the documentation was restructured.
mauvilsa e5915e6
Fixes based on review.
mauvilsa d3c0aa0
Merge branch 'master' into cli-fix-docs-after-restructure
Borda 9f8d000
Apply suggestions from code review
mauvilsa ce8fa53
Fix wrap width.
mauvilsa ad4bb5c
Move save_hyperparameters and load_from_checkpoint to lightning_modul…
mauvilsa 51b99a1
Merge branch 'master' into cli-fix-docs-after-restructure
mauvilsa 46eda56
Merge branch 'master' into cli-fix-docs-after-restructure
carmocca 7ced431
Merge branch 'master' into cli-fix-docs-after-restructure
Borda File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,113 +1,169 @@ | ||
:orphan: | ||
|
||
####################################### | ||
Eliminate config boilerplate (Advanced) | ||
####################################### | ||
################################################# | ||
Configure hyperparameters from the CLI (Advanced) | ||
################################################# | ||
**Audience:** Users looking to modularize their code for a professional project. | ||
|
||
**Pre-reqs:** You must have read :doc:`(Control it all from the CLI) <lightning_cli_intermediate>`. | ||
**Pre-reqs:** You must have read :doc:`(Mix models and datasets) <lightning_cli_intermediate_2>`. | ||
|
||
As a project becomes more complex, the number of configurable options becomes very large, making it inconvenient to | ||
control through individual command line arguments. To address this, CLIs implemented using | ||
:class:`~pytorch_lightning.cli.LightningCLI` always support receiving input from configuration files. The default format | ||
used for config files is yaml. | ||
|
||
.. tip:: | ||
|
||
If you are unfamiliar with yaml, have a look at the short introduction at `realpython.com#yaml-syntax | ||
<https://realpython.com/python-yaml/#yaml-syntax>`__. | ||
|
||
|
||
---- | ||
|
||
*************************** | ||
What is a yaml config file? | ||
*************************** | ||
mauvilsa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
A yaml is a standard configuration file that describes parameters for sections of a program. It is a common tool in engineering, and it has recently started to gain popularity in machine learning. | ||
*********************** | ||
Run using a config file | ||
*********************** | ||
To run the CLI using a yaml config, do: | ||
|
||
.. code:: yaml | ||
.. code:: bash | ||
# file.yaml | ||
car: | ||
max_speed:100 | ||
max_passengers:2 | ||
plane: | ||
fuel_capacity: 50 | ||
class_3: | ||
option_1: 'x' | ||
option_2: 'y' | ||
python main.py fit --config config.yaml | ||
Individual arguments can be given to override options in the config file: | ||
|
||
.. code:: bash | ||
python main.py fit --config config.yaml --trainer.max_epochs 100 | ||
---- | ||
|
||
************************ | ||
Automatic save of config | ||
************************ | ||
|
||
********************* | ||
Print the config used | ||
********************* | ||
Before or after you run a training routine, you can print the full training spec in yaml format using ``--print_config``: | ||
To ease experiment reporting and reproducibility, by default ``LightningCLI`` automatically saves the full yaml | ||
configuration in the log directory. After multiple fit runs with different hyperparameters, each one will have in its | ||
respective log directory a ``config.yaml`` file. These files can be used to trivially reproduce an experiment, e.g.: | ||
|
||
.. code:: bash | ||
python main.py fit --config lightning_logs/version_7/config.yaml | ||
The automatic saving of the config is done by the special callback :class:`~pytorch_lightning.cli.SaveConfigCallback`. | ||
This callback is automatically added to the ``Trainer``. To disable the save of the config instantiate ``LightningCLI`` | ||
with ``save_config_callback=None``. | ||
mauvilsa marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
---- | ||
|
||
********************************* | ||
Prepare a config file for the CLI | ||
********************************* | ||
The ``--help`` option of the CLIs can be used learn which configuration options are available and how to use them. | ||
However, writing a config from scratch can be time consuming and error prone. To alleviate this, the CLIs have the | ||
``--print_config`` argument, which prints to stdout the configuration without running the command. | ||
|
||
For a CLI implemented as ``LightningCLI(DemoModel, BoringDataModule)``, executing: | ||
mauvilsa marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
.. code:: bash | ||
python main.py fit --print_config | ||
which generates the following config: | ||
generates a config with all default values like the following: | ||
|
||
.. code:: bash | ||
seed_everything: null | ||
trainer: | ||
logger: true | ||
... | ||
terminate_on_nan: null | ||
logger: true | ||
... | ||
model: | ||
out_dim: 10 | ||
learning_rate: 0.02 | ||
out_dim: 10 | ||
learning_rate: 0.02 | ||
data: | ||
data_dir: ./ | ||
data_dir: ./ | ||
ckpt_path: null | ||
---- | ||
|
||
******************************** | ||
Write a config yaml from the CLI | ||
******************************** | ||
To have a copy of the configuration that produced this model, save a *yaml* file from the *--print_config* outputs: | ||
Other command line arguments can be given and will be considered in the printed configuration. A use case for this is CLIs | ||
that accept multiple models. By default no model is selected, which means that the printed config will not include model | ||
settings. To get a config with the default values of a particular model would be: | ||
mauvilsa marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
.. code:: bash | ||
python main.py fit --model.learning_rate 0.001 --print_config > config.yaml | ||
python main.py fit --model DemoModel --print_config | ||
---- | ||
|
||
********************** | ||
Run from a single yaml | ||
********************** | ||
To run from a yaml, pass a yaml produced with ``--print_config`` to the ``--config`` argument: | ||
which generates a config like: | ||
|
||
.. code:: bash | ||
python main.py fit --config config.yaml | ||
seed_everything: null | ||
trainer: | ||
... | ||
model: | ||
class_path: pytorch_lightning.demos.boring_classes.DemoModel | ||
init_args: | ||
out_dim: 10 | ||
learning_rate: 0.02 | ||
ckpt_path: null | ||
when using a yaml to run, you can still pass in inline arguments | ||
.. tip:: | ||
|
||
.. code:: bash | ||
A standard procedure to run experiments can be: | ||
|
||
python main.py fit --config config.yaml --trainer.max_epochs 100 | ||
.. code:: bash | ||
# Print a configuration to have as reference | ||
python main.py fit --print_config > config.yaml | ||
# Modify the config to your liking - you can remove all default arguments | ||
nano config.yaml | ||
# Fit your model using the edited configuration | ||
python main.py fit --config config.yaml | ||
---- | ||
|
||
****************** | ||
Compose yaml files | ||
****************** | ||
For production or complex research projects it's advisable to have each object in its own config file. To compose all the configs, pass them all inline: | ||
******************** | ||
Compose config files | ||
******************** | ||
Multiple config files can be provided and they will be parsed sequentially. Let's say we have two configs with common | ||
settings: | ||
mauvilsa marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
.. code:: yaml | ||
# config_1.yaml | ||
trainer: | ||
num_epochs: 10 | ||
... | ||
# config_2.yaml | ||
trainer: | ||
num_epochs: 20 | ||
... | ||
The value from the last config will be used, ``num_epochs = 20`` in this case: | ||
|
||
.. code-block:: bash | ||
$ python trainer.py fit --config trainer.yaml --config datamodules.yaml --config models.yaml ... | ||
$ python main.py fit --config config_1.yaml --config config_2.yaml | ||
The configs will be parsed sequentially. Let's say we have two configs with the same args: | ||
---- | ||
|
||
********************* | ||
Use groups of options | ||
********************* | ||
Groups of options can also be given as independent config files. For configs like: | ||
|
||
.. code:: yaml | ||
# trainer.yaml | ||
trainer: | ||
num_epochs: 10 | ||
num_epochs: 10 | ||
# model.yaml | ||
out_dim: 7 | ||
# trainer_2.yaml | ||
trainer: | ||
num_epochs: 20 | ||
# data.yaml | ||
data_dir: ./data | ||
the ones from the last config will be used (num_epochs = 20) in this case: | ||
a fit command can be run as: | ||
|
||
.. code-block:: bash | ||
$ python trainer.py fit --config trainer.yaml --config trainer_2.yaml | ||
$ python main.py fit --trainer trainer.yaml --model model.yaml --data data.yaml [...] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.