Skip to content

Add singleton option#3013

Closed
vladoovtcharov wants to merge 1 commit into
facebookresearch:mainfrom
vladoovtcharov:features/singleton
Closed

Add singleton option#3013
vladoovtcharov wants to merge 1 commit into
facebookresearch:mainfrom
vladoovtcharov:features/singleton

Conversation

@vladoovtcharov

Copy link
Copy Markdown

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
Copy Markdown
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?

RECURSIVE = "_recursive_"
ARGS = "_args_"
PARTIAL = "_partial_"
SINGLETON = "_singleton_"

Copy link
Copy Markdown
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

if is_structured_config(config) or isinstance(config, (dict, list)):
config = OmegaConf.structured(config, flags={"allow_objects": True})

singleton_registry = {}

Copy link
Copy Markdown
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

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I though about adding it to hydra global but that caused a circular reference in the modules. It could be passed in as an optional parameter to instantiate so that someone has the option to re-use it

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

Copy link
Copy Markdown
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?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

that makes sense, will update

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
Copy Markdown
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?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

ya that is a bit concerning and tricky to catch, checking the dicts are the same feels wrong but would be good to check to give atleast a warning.

@vladoovtcharov

Copy link
Copy Markdown
Author

Due to some of the issues I also added an alternate method that I think is a bit more robust: #3025 will move to the feature request to get feedback on which approach would fit better

@swamidass

Copy link
Copy Markdown

I made something similar here #3063.

Do you know why this wasn't accepted?

@omry omry added the triage label May 16, 2026
@omry

omry commented Jun 8, 2026

Copy link
Copy Markdown
Collaborator

Thank you for the PR and for exploring this direction.

I am going to close this for now. The PR is incomplete as an implementation proposal: it needs tests, documentation, and a news fragment. More importantly, the API shape and semantics for shared object identity during instantiate still need design discussion before I can review an implementation. Some of the open questions include how the singleton should be declared, naming and collision behavior, initialization order, interaction with interpolation/resolution, and cycle/error semantics.

If you would like to keep pursuing this, please start a design discussion for your proposal first. Once the API and semantics are agreed on, an implementation PR will be much easier to evaluate.

@omry omry closed this Jun 8, 2026
@omry omry removed the triage label Jun 8, 2026
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

5 participants