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

Add singleton option #3013

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

Conversation

vladoovtcharov
Copy link

Motivation

Adds a singleton option similar to that proposed in #1393

Instead of _singleton_: true a unique name is given to each singleton, _singleton_: "my_singletons_name" this allows the config file to be resolved and still have singletons work.

Have you read the Contributing Guidelines on pull requests?

Yes

Test Plan

Can add tests that nested singletons are working correctly. Not sure how to set up tests to see if this will break any interactions with the other flags like recursive or partial

Related Issues and PRs

resolves #1393

Todo

Add tests and documentation

@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 Jan 22, 2025
@jesszzzz
Copy link
Contributor

Seems like the test and doc github workflows aren't configured to run on PRs from forks, mind also adding pull_request to
their triggers (eg. https://fburl.com/46zzxr9z) so they'll run on this PR instead of only on push?

@@ -22,6 +22,7 @@ class _Keys(str, Enum):
RECURSIVE = "_recursive_"
ARGS = "_args_"
PARTIAL = "_partial_"
SINGLETON = "_singleton_"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: "singleton_id" feels more descriptive that it should be a string value, not bool

@@ -239,6 +246,7 @@ def instantiate(
if is_structured_config(config) or isinstance(config, (dict, list)):
config = OmegaConf.structured(config, flags={"allow_objects": True})

singleton_registry = {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you consider making the registry more global so they can be saved across instantiate calls? Would be interested to know if anyone has strong opinions on which behavior is preferred

if is_singleton:
singleton_name = instantiate_node(node.get(_Keys.SINGLETON),
singleton_registry=singleton_registry)
if singleton_name in singleton_registry:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can/should we enforce singleton_name is a string?

singleton_name = instantiate_node(node.get(_Keys.SINGLETON),
singleton_registry=singleton_registry)
if singleton_name in singleton_registry:
return singleton_registry[singleton_name]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm concerned this could get confusing if two different dicts have the same singleton_name and it's not totally obvious which will be used. Since this primarily seems to be wanted with omegaconf resolution, the dicts should be identical - what do you think of storing the original node in singleton_registry and doing an extra check that this node's fields also match the original?

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.

[Feature Request] Consider supporting singleton pattern in recursive instantiation
3 participants