Shortcuts

cli

Classes

LightningArgumentParser

Extension of jsonargparse’s ArgumentParser for pytorch-lightning

LightningCLI

Implementation of a configurable command line tool for pytorch-lightning

SaveConfigCallback

Saves a LightningCLI config to the log_dir when training starts

class pytorch_lightning.utilities.cli.LightningArgumentParser(*args, parse_as_dict=True, **kwargs)[source]

Bases: jsonargparse.

Extension of jsonargparse’s ArgumentParser for pytorch-lightning

Initialize argument parser that supports configuration file input

For full details of accepted arguments see ArgumentParser.__init__.

add_lightning_class_args(lightning_class, nested_key, subclass_mode=False)[source]

Adds arguments from a lightning class to a nested key of the parser

Parameters
Return type

None

class pytorch_lightning.utilities.cli.LightningCLI(model_class, datamodule_class=None, save_config_callback=<class 'pytorch_lightning.utilities.cli.SaveConfigCallback'>, trainer_class=<class 'pytorch_lightning.trainer.trainer.Trainer'>, trainer_defaults=None, seed_everything_default=None, description='pytorch-lightning trainer command line tool', env_prefix='PL', env_parse=False, parser_kwargs=None, subclass_mode_model=False, subclass_mode_data=False)[source]

Bases: object

Implementation of a configurable command line tool for pytorch-lightning

Receives as input pytorch-lightning classes, which are instantiated using a parsed configuration file and/or command line args and then runs trainer.fit. Parsing of configuration from environment variables can be enabled by setting env_parse=True. A full configuration yaml would be parsed from PL_CONFIG if set. Individual settings are so parsed from variables named for example PL_TRAINER__MAX_EPOCHS.

Example, first implement the trainer.py tool as:

from mymodels import MyModel
from pytorch_lightning.utilities.cli import LightningCLI
LightningCLI(MyModel)

Then in a shell, run the tool with the desired configuration:

$ python trainer.py --print_config > config.yaml
$ nano config.yaml  # modify the config as desired
$ python trainer.py --cfg config.yaml

Warning

LightningCLI is in beta and subject to change.

Parameters
  • model_class (Type[LightningModule]) – The LightningModule class to train on.

  • datamodule_class (Optional[Type[LightningDataModule]]) – An optional LightningDataModule class.

  • save_config_callback (Type[SaveConfigCallback]) – A callback class to save the training config.

  • trainer_class (Type[Trainer]) – An optional extension of the Trainer class.

  • trainer_defaults (Optional[Dict[str, Any]]) – Set to override Trainer defaults or add persistent callbacks.

  • seed_everything_default (Optional[int]) – Default value for seed_everything argument.

  • description (str) – Description of the tool shown when running –help.

  • env_prefix (str) – Prefix for environment variables.

  • env_parse (bool) – Whether environment variable parsing is enabled.

  • parser_kwargs (Optional[Dict[str, Any]]) – Additional arguments to instantiate LightningArgumentParser.

  • subclass_mode_model (bool) – Whether model can be any subclass of the given class.

  • subclass_mode_data (bool) –

    Whether datamodule can be any subclass of the given class.

add_arguments_to_parser(parser)[source]

Implement to add extra arguments to parser or link arguments

Parameters

parser (LightningArgumentParser) – The argument parser object to which arguments can be added

Return type

None

add_core_arguments_to_parser()[source]

Adds arguments from the core classes to the parser

Return type

None

after_fit()[source]

Implement to run some code after fit has finished

Return type

None

before_fit()[source]

Implement to run some code before fit is started

Return type

None

before_instantiate_classes()[source]

Implement to run some code before instantiating the classes

Return type

None

fit()[source]

Runs fit of the instantiated trainer class and prepared fit keyword arguments

Return type

None

init_parser()[source]

Method that instantiates the argument parser

Return type

None

instantiate_classes()[source]

Instantiates the classes using settings from self.config

Return type

None

instantiate_datamodule()[source]

Instantiates the datamodule using self.config_init[‘data’] if given

Return type

None

instantiate_model()[source]

Instantiates the model using self.config_init[‘model’]

Return type

None

instantiate_trainer()[source]

Instantiates the trainer using self.config_init[‘trainer’]

Return type

None

parse_arguments()[source]

Parses command line arguments and stores it in self.config

Return type

None

prepare_fit_kwargs()[source]

Prepares fit_kwargs including datamodule using self.config_init[‘data’] if given

Return type

None

class pytorch_lightning.utilities.cli.SaveConfigCallback(parser, config, config_filename='config.yaml')[source]

Bases: pytorch_lightning.callbacks.base.Callback

Saves a LightningCLI config to the log_dir when training starts

on_train_start(trainer, pl_module)[source]

Called when the train begins.

Return type

None