Shortcuts

cli

Functions

instantiate_class

Instantiates a class with the given args and init.

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, required=True)[source]

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

Parameters
Return type

List[str]

Returns

A list with the names of the class arguments added.

add_lr_scheduler_args(lr_scheduler_class, nested_key='lr_scheduler', link_to='AUTOMATIC')[source]

Adds arguments from a learning rate scheduler class to a nested key of the parser.

Parameters
Return type

None

add_optimizer_args(optimizer_class, nested_key='optimizer', link_to='AUTOMATIC')[source]

Adds arguments from an optimizer class to a nested key of the parser.

Parameters
  • optimizer_class (Union[Type[Optimizer], Tuple[Type[Optimizer], …]]) – Any subclass of torch.optim.Optimizer.

  • nested_key (str) – Name of the nested namespace to store arguments.

  • link_to (str) – Dot notation of a parser key to set arguments or AUTOMATIC.

Return type

None

set_choices(nested_key, classes, is_list=False)[source]

Adds support for shorthand notation for a particular nested key.

Parameters
  • nested_key (str) – The key whose choices will be set.

  • classes (Tuple[Type, …]) – A tuple of classes to choose from.

  • is_list (bool) – Whether the argument is a List[object] type.

Return type

None

class pytorch_lightning.utilities.cli.LightningCLI(model_class=None, datamodule_class=None, save_config_callback=<class 'pytorch_lightning.utilities.cli.SaveConfigCallback'>, save_config_filename='config.yaml', save_config_overwrite=False, save_config_multifile=False, 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, run=True)[source]

Bases: object

Implementation of a configurable command line tool for pytorch-lightning.

Receives as input pytorch-lightning classes (or callables which return pytorch-lightning classes), which are called / instantiated using a parsed configuration file and / or command line args.

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.

For more info, read the CLI docs.

Warning

LightningCLI is in beta and subject to change.

Parameters
add_arguments_to_parser(parser)[source]

Implement to add extra arguments to the parser or link arguments.

Parameters

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

Return type

None

add_core_arguments_to_parser(parser)[source]

Adds arguments from the core classes to the parser.

Return type

None

add_default_arguments_to_parser(parser)[source]

Adds default arguments to the parser.

Return type

None

before_instantiate_classes()[source]

Implement to run some code before instantiating the classes.

Return type

None

init_parser(**kwargs)[source]

Method that instantiates the argument parser.

Return type

LightningArgumentParser

instantiate_classes()[source]

Instantiates the classes and sets their attributes.

Return type

None

instantiate_trainer(**kwargs)[source]

Instantiates the trainer.

Parameters

kwargs (Any) – Any custom trainer arguments.

Return type

Trainer

Creates argument links for optimizers and learning rate schedulers that specified a link_to.

Return type

None

parse_arguments(parser)[source]

Parses command line arguments and stores it in self.config.

Return type

None

setup_parser(add_subcommands, main_kwargs, subparser_kwargs)[source]

Initialize and setup the parser, subcommands, and arguments.

Return type

None

static subcommands()[source]

Defines the list of available subcommands and the arguments to skip.

Return type

Dict[str, Set[str]]

class pytorch_lightning.utilities.cli.SaveConfigCallback(parser, config, config_filename, overwrite=False, multifile=False)[source]

Bases: pytorch_lightning.callbacks.base.Callback

Saves a LightningCLI config to the log_dir when training starts.

Parameters
  • parser (LightningArgumentParser) – The parser object used to parse the configuration.

  • config (Union[Namespace, Dict[str, Any]]) – The parsed configuration that will be saved.

  • config_filename (str) – Filename for the config file.

  • overwrite (bool) – Whether to overwrite an existing config file.

  • multifile (bool) – When input is multiple config files, saved config preserves this structure.

Raises

RuntimeError – If the config file already exists in the directory to avoid overwriting a previous run

setup(trainer, pl_module, stage=None)[source]

Called when fit, validate, test, predict, or tune begins.

Return type

None

pytorch_lightning.utilities.cli.instantiate_class(args, init)[source]

Instantiates a class with the given args and init.

Parameters
  • args (Union[Any, Tuple[Any, …]]) – Positional arguments required for instantiation.

  • init (Dict[str, Any]) – Dict of the form {“class_path”:…,”init_args”:…}.

Return type

Any

Returns

The instantiated class object.