egrecho.core.parser#

class egrecho.core.parser.CommonParser(*args: Any, **kwargs: Any)[source]#

Bases: ArgumentParser

Almost inherit from jsonargparse.ArgumentParser.

add_class_args(theclass, nested_key=None, subclass_mode=False, instantiate=True, default=None, **kwargs)[source]#

A convenient access of add class/subclass arguments.

Parameters:
  • theclass (Type) -- Class from which to add arguments.

  • nested_key (Optional[str]) -- Key for nested namespace.

  • subclass_mode (bool) -- Whether allow any subclass of the given class.

  • instantiate (bool) -- Whether the class group should be instantiated by instantiate_classes().

  • **kwargs -- other args will pass to add_subclass_arguments()/add_class_arguments() of jsonargparser.

class egrecho.core.parser.BaseCommand[source]#

Bases: object

Provides a standardized form of command line class.

This class is intend to build a cli system. Core methods to be implemented:

Workflow follows:

Example:

from egrecho.core.parser import BaseCommand, CommonParser

def hello(name: str):
    print(f"Hello {name}!")


class Hello(BaseCommand):
    @classmethod
    def get_dummy_parser(cls):
        return CommonParser(description='Greet')

    @classmethod
    def setup_parser(cls, parser: CommonParser):
        parser.add_function_arguments(hello)
        return parser

    @staticmethod
    def run_from_args(args, parser=None):
        hello(**args)


if __name__ == "__main__":
    parser = Hello.get_dummy_parser()
    parser = Hello.setup_parser(parser)
    args = parser.parse_args()
    Hello.run_from_args(args, parser)


or


# in ipynb
parser = Hello.get_dummy_parser()
parser = Hello.setup_parser(parser)
args = parser.parse_args(['--name=Word'])
Hello.run_from_args(args, parser)  # Hello Word!
classmethod get_dummy_parser()[source]#

Get a dummy parser to be set in setup_parser()

Return type:

ArgumentParser

classmethod setup_parser(parser)[source]#

Setup a parser.

Return type:

ArgumentParser

static run_from_args(args, parser=None, **kwargs)[source]#

Run this command with args.

PL Parser#

class egrecho.core.pl_parser.LightningParser(*args: Any, **kwargs: Any)[source]#

Bases: CommonParser

A convenient argument parser for pytorch lighting:

add_pl_module_args(pl_class, nested_key, subclass_mode=False, **kwargs)[source]#

Adds pl module {LightningModule, LightningDataModule, Callback} arguments.

Parameters:
  • pl_class (Union[Type[LightningModule], Type[LightningDataModule], Type[Callback]]) -- Subclass of {LightningModule, LightningDataModule, Callback}.

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

  • subclass_mode (bool) -- Whether allow any subclass of the given class.

add_trainer_args(pl_trainer_class=<class 'lightning.pytorch.trainer.trainer.Trainer'>, nested_key='trainer', **kwargs)[source]#

Adds pl trainer arguments.

Parameters:
  • pl_trainer_class (Type[Trainer]) -- class of lightning trainer.

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

class egrecho.core.pl_parser.SaveConfigCallback(parser, config, config_filename='train.yaml', overwrite=True, skip_none=True, multifile=False, save_to_log_dir=True)[source]#

Bases: Callback

Saves a LightningCLI config to the log_dir when training starts.

Parameters:
  • parser (CommonParser) -- The parser object used to parse the configuration.

  • config (Namespace) -- The parsed configuration that will be saved.

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

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

  • skip_none (bool) -- whether skip null while saving.

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

  • save_to_log_dir (bool) -- Whether to save the config to the log_dir.

Raises:

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

save_config(trainer, pl_module, stage)[source]#

Implement to save the config in some other place additional to the standard log_dir.

Return type:

None

Example

def save_config(self, trainer, pl_module, stage):
if isinstance(trainer.logger, Logger):

config = self.parser.dump(self.config, skip_none=False) # Required for proper reproducibility trainer.logger.log_hyperparams({“config”: config})

Note

This method is only called on rank zero. This allows to implement a custom save config without having to worry about ranks or race conditions. Since it only runs on rank zero, any collective call will make the process hang waiting for a broadcast. If you need to make collective calls, implement the setup method instead.