Skip to content
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

provide an option to ignore not exists attribute when apply_*_links #129

Closed
tshu-w opened this issue Feb 26, 2022 · 4 comments
Closed

provide an option to ignore not exists attribute when apply_*_links #129

tshu-w opened this issue Feb 26, 2022 · 4 comments
Labels
enhancement New feature or request

Comments

@tshu-w
Copy link
Contributor

tshu-w commented Feb 26, 2022

I'm using one pytorchlightning cli for multiple data and models, which are not always have the same attribute. I suggest to provide an option to ignore not exists attribute when apply_*_links.

In other words, I hope to be able to have an option to no raise error in apply_instantiation_links if source_object doesn't have the attr

def apply_instantiation_links(parser, cfg, source):
    if not hasattr(parser, "_links_group"):
        return
    for action in parser._links_group._group_actions:
        if action.apply_on != "instantiate" or source != action.source[0][1].dest:
            continue
        source_object = cfg[source]
        if action.source[0][0] == action.source[0][1].dest:
            value = action.compute_fn(source_object)
        else:
            attr = split_key_leaf(action.source[0][0])[1]
            if hasattr(source_object, attr):
                value = getattr(source_object, attr)
                if action.compute_fn is not None:
                    value = action.compute_fn(value)
                _ActionLink.set_target_value(action, value, cfg)
            else: 
                ... # logger warning
@mauvilsa mauvilsa added the enhancement New feature or request label Feb 28, 2022
mauvilsa added a commit that referenced this issue Mar 29, 2022
…ameter #129.

- Swapped order of argument links in help to source --> target.
@mauvilsa
Copy link
Member

This has been implemented in commit 60420ce. There is no option to disable this behavior. Always for subclasses if the source or target does not have the parameter, the linking is skipped without error. If logging is enabled at debug level, a message is shown when this happens.

tshu-w added a commit to tshu-w/lightning-template that referenced this issue Mar 29, 2022
@tshu-w
Copy link
Contributor Author

tshu-w commented Mar 30, 2022

@mauvilsa Hi, I'm facing new error when update to 4.5.0. Have not looked at why, it would be much appreciated if you could take a look at it.

Here is my sample cli.py:

from pytorch_lightning.utilities.cli import LightningCLI

class LitCLI(LightningCLI):
    def add_arguments_to_parser(self, parser):
        parser.link_arguments(
            "data.init_args.batch_size",
            "model.init_args.batch_size",
            apply_on="instantiate",
        )

cli = LitCLI()

If I run python cli.py --help:

Traceback (most recent call last):
  File "/home1/wangtianshu/lightning-template/test.py", line 7, in <module>
    cli = LitCLI()
  File "/home1/wangtianshu/.local/share/conda/envs/template/lib/python3.9/site-packages/pytorch_lightning/utilities/cli.py", line 551, in __init__
    self.setup_parser(run, main_kwargs, subparser_kwargs)
  File "/home1/wangtianshu/.local/share/conda/envs/template/lib/python3.9/site-packages/pytorch_lightning/utilities/cli.py", line 587, in setup_parser
    self._add_subcommands(self.parser, **subparser_kwargs)
  File "/home1/wangtianshu/.local/share/conda/envs/template/lib/python3.9/site-packages/pytorch_lightning/utilities/cli.py", line 661, in _add_subcommands
    subcommand_parser = self._prepare_subcommand_parser(trainer_class, subcommand, **kwargs.get(subcommand, {}))
  File "/home1/wangtianshu/.local/share/conda/envs/template/lib/python3.9/site-packages/pytorch_lightning/utilities/cli.py", line 669, in _prepare_subcommand_parser
    self._add_arguments(parser)
  File "/home1/wangtianshu/.local/share/conda/envs/template/lib/python3.9/site-packages/pytorch_lightning/utilities/cli.py", line 626, in _add_arguments
    self.add_arguments_to_parser(parser)
  File "/home1/wangtianshu/lightning-template/test.py", line 5, in add_arguments_to_parser
    parser.link_arguments("data.init_args.batch_size", "model.init_args.batch_size", apply_on="instantiate")
  File "/home1/wangtianshu/.local/share/conda/envs/template/lib/python3.9/site-packages/jsonargparse/core.py", line 617, in link_arguments
    _ActionLink(self, source, target, compute_fn, apply_on)
  File "/home1/wangtianshu/.local/share/conda/envs/template/lib/python3.9/site-packages/jsonargparse/actions.py", line 423, in __init__
    raise ValueError('Links applied on instantiation require source to be a subclass action or a class group.')
ValueError: Links applied on instantiation require source to be a subclass action or a class group.

and

Traceback (most recent call last):
  File "/home1/wangtianshu/lightning-template/test.py", line 7, in <module>
    cli = LitCLI()
  File "/home1/wangtianshu/.local/share/conda/envs/template/lib/python3.9/site-packages/pytorch_lightning/utilities/cli.py", line 551, in __init__
    self.setup_parser(run, main_kwargs, subparser_kwargs)
  File "/home1/wangtianshu/.local/share/conda/envs/template/lib/python3.9/site-packages/pytorch_lightning/utilities/cli.py", line 587, in setup_parser
    self._add_subcommands(self.parser, **subparser_kwargs)
  File "/home1/wangtianshu/.local/share/conda/envs/template/lib/python3.9/site-packages/pytorch_lightning/utilities/cli.py", line 661, in _add_subcommands
    subcommand_parser = self._prepare_subcommand_parser(trainer_class, subcommand, **kwargs.get(subcommand, {}))
  File "/home1/wangtianshu/.local/share/conda/envs/template/lib/python3.9/site-packages/pytorch_lightning/utilities/cli.py", line 669, in _prepare_subcommand_parser
    self._add_arguments(parser)
  File "/home1/wangtianshu/.local/share/conda/envs/template/lib/python3.9/site-packages/pytorch_lightning/utilities/cli.py", line 626, in _add_arguments
    self.add_arguments_to_parser(parser)
  File "/home1/wangtianshu/lightning-template/test.py", line 5, in add_arguments_to_parser
    parser.link_arguments("data.init_args.batch_size", "model.init_args.batch_size")
  File "/home1/wangtianshu/.local/share/conda/envs/template/lib/python3.9/site-packages/jsonargparse/core.py", line 617, in link_arguments
    _ActionLink(self, source, target, compute_fn, apply_on)
  File "/home1/wangtianshu/.local/share/conda/envs/template/lib/python3.9/site-packages/jsonargparse/actions.py", line 435, in __init__
    raise ValueError(f'No action for key "{key}".')
ValueError: No action for key "data.init_args.batch_size".

when apply_on="instantiate" and "parse" respectively.

@mauvilsa
Copy link
Member

If you debug that script, break inside you add_arguments_to_parser and do parser.print_help() you will see that the parser does not have data. Without data there, you can't link it. If you look at

https://github.com/PyTorchLightning/pytorch-lightning/blob/6109b0b49cc1c94164bf2f40a46511096e5f655c/pytorch_lightning/utilities/cli.py#L613-L620

data will only be defined if you specify a data module class, e.g. LitCLI(datamodule_class=MyDataModule) or you register data modules.

I was not the one who extended LightningCLI to have registries or have the option to instantiate without specifying the module classes. If this is not clear in the documentation please create an issue in the pytorch-lightning repo or better yet, create a pull request improving the documentation where needed.

@tshu-w
Copy link
Contributor Author

tshu-w commented Mar 30, 2022

Thank you very much for investigating! I will check the documentation and determine if feedback is needed.

tshu-w added a commit to tshu-w/lightning-template that referenced this issue Jul 12, 2023
tshu-w added a commit to tshu-w/lightning-template that referenced this issue Jul 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants