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

Obey _recursive_ parameter on non-target nodes #2577

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

michaelhball
Copy link

@michaelhball michaelhball commented Feb 7, 2023

Motivation

When recursively instantiating a config, the recursive parameter is ignored unless the config is a target node (i.e. has a _target_ parameter). It would be nice if the _recursive_ flag were considered for non-target nodes as well so that a recursive flow of instantiation can be "broken" (i.e. all nodes from a certain level in the config "tree" remain as DictConfig objects). The reason why there is an upstream _recursive_=True is because the parent node has other children who we do want to recursively instantiate. As a concrete example, consider the following set of configs and corresponding classes to be instantiated (I tried to keep them as minimal as possible)

@dataclass
class MyArchitecture:
    _target_: str = ".../Architecture"


@dataclass
class MyLRScheduler:
    _target_: str = ".../LRScheduler"


@dataclass
class MyOptimizer:
    _recursive_: bool = False
    scheduler: MyLRScheduler = field(default_factory=MyLRScheduler)


@dataclass
class MyNN:
    _target_: str = ".../NN"
    _recursive_: bool = True

    architecture: MyArchitecture = field(default_factory=MyArchitecture)
    optimizer: MyOptimizer = field(default_factory=MyOptimizer)


@dataclass
class MyConfig:
    nn: MyNN = field(default_factory=MyNN)


cs = ConfigStore.instance()
cs.store(name="config", node=MyConfig)


class LRScheduler:
    ...


class Architecture:
    ...


class NN:
    def __init__(self, architecture: Architecture, optimizer: MyOptimizer) -> None:
        # NB: the `optimizer` expected here is a DictConfig (duck-typed as MyOptimizer)
        self.architecture = architecture
        self.optimizer = optimizer


@hydra.main(config_name="config")
def my_app(cfg: MyConfig) -> None:
    instantiated_config: NN = instantiate(cfg.nn)
    print(f"Scheduler was instantiated: {not isinstance(instantiated_config.optimizer.scheduler, DictConfig)}")

if __name__ == "__main__":
    my_app()

The output of the print statement above is "Scheduler was instantiated: True", while the idea behind this PR is that it should be False (due to the setting of the _recursive_=False parameter in the MyOptimizer config).

Have you read the Contributing Guidelines on pull requests?

Yes

Test Plan

Added an additional test in tests/instantiate/test_instantiate and verified that no existing tests are broken. As far as I can tell, none of the failing CI tests are due to this PR.

Related Issues and PRs

(Is this PR part of a group of changes? Link the other relevant PRs and Issues here. Use https://help.github.com/en/articles/closing-issues-using-keywords for help on GitHub syntax)

@facebook-github-bot
Copy link
Contributor

Hi @michaelhball!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Feb 7, 2023
@facebook-github-bot
Copy link
Contributor

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@michaelhball michaelhball force-pushed the always_consider_recursive_flag branch from 1e5740f to e94ac76 Compare February 7, 2023 12:58
@michaelhball michaelhball marked this pull request as ready for review February 7, 2023 12:59
@michaelhball michaelhball changed the title Allow _recursive_ parameter on non-target nodes Obey _recursive_ parameter on non-target nodes Feb 7, 2023
@jdrudolph
Copy link

@michaelhball I've found a workaround for this issue:

@dataclass
class MyOptimizer:
    _target_: str = "MyOptimizer"  # self-referential target to break recursive instantiation
    _recursive_: bool = False
    scheduler: MyLRScheduler = field(default_factory=MyLRScheduler)

would love to see this PR merged though!

@Zacharias030
Copy link

Could we please have this PR considered? Tagging people with a release bit for help @omry @Jasha10 @jieru-hu @pixelb!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants