Add _resolve_ flag to instantiate.#2269
Conversation
Add _resolve_ flag to instantiate. This is useful for weakly recursive configurations (e.g. instantiate a logger with all the configuration or configuration yaml-string).
Update the documentation of instantiate for optionally disabling the OmegaConf resolution.
|
Hi @kpoeppel! Thank you for your pull request and welcome to our community. Action RequiredIn 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. ProcessIn 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 If you have received this in error or have any questions, please contact us at cla@fb.com. Thanks! |
Jasha10
left a comment
There was a problem hiding this comment.
This PR does not address the use-case outlined in this comment. The assertion in the following script should pass:
from hydra.utils import instantiate
from omegaconf import DictConfig, OmegaConf
def train(cfg: DictConfig, num: int):
return num
cfg = OmegaConf.create(
"""
func:
_target_: __main__.train
num: 123
"""
)
x = instantiate(cfg.func, cfg=cfg, _resolve_=False)
assert x == 124The script instead fails with a TypeError.
| is_resolve = ( | ||
| (node.get("_resolve_", None) is None and resolve) | ||
| or node.get("_resolve_", True)) |
There was a problem hiding this comment.
This logic is tricky -- it would be good to have tests for some nested cases.
_target_: module.Outer
arg:
_target_: module.Inner
_resolve_: true
_resolve_: false_target_: module.Outer
arg:
_target_: module.Inner
_resolve_: false
_resolve_: true_target_: module.Outer
arg:
_target_: module.Inner
# _resolve_ true inherited
_resolve_: true_target_: module.Outer
arg:
_target_: module.Inner
# _resolve_ false inherited
_resolve_: falseThere was a problem hiding this comment.
Sorry for the late reply, as of the now pushed commit e586dc7 your code can work with _recursive_=False inside instantiate.
The problem is how to tackle recursion here, as things become self-referential potentially (-> infinite recursion). There are three options.
- Demanding
_recursive_=Falsein this code (current state). - Treat kwargs inside instantiate differently for the case
_resolve_=False, such that these are excluded from the recursive instantiate. So here the implicit_recursive_=Truewould only apply tocfg.funcand not tocfg. - Introducing another step for resolution (like in the Issue 2268)
What do you think works best?
There was a problem hiding this comment.
I too should apologize for my late response.
The problem is how to tackle recursion here, as things become self-referential potentially (-> infinite recursion).
I am confused. Could you give me an example where infinite recursion would come up? Is this a new problem introduced by this PR, or would the infinite recursion happen even without this PR?
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
|
I've just realized that this PR is quite similar to what is discussed in issue #1758, specifically Omry's suggestion from this comment. |
|
Do you think this can be merged now from a software architecture point of view? |
Not sure. We might need to move the This PR needs more tests before it can be merged: there's only one test for My advice is to use test-driven development:
|
Motivation
See issue #2268. Passing the whole configuration (e.g. via eval) to an instantiated object is impossible as the resolution breaks due to different top-level of the configuration.
With this addition, special objects are not resolved a second time (of course one resolve has to be called beforehand).
Have you read the Contributing Guidelines on pull #requests?
Yes
Test Plan
A simple test was already added. The default behaviour doesn't change.
Related Issues and PRs
Issue #2268