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

Fix instantiate side effect modifying input config parent #3005

Merged
merged 3 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions hydra/_internal/instantiate/_instantiate2.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,24 @@ def _resolve_target(
return target


def _deep_copy_full_config(subconfig: Any) -> Any:
"""Deep copy full config from root to leaf and return the copied subconfig"""
if not OmegaConf.is_config(subconfig):
return copy.deepcopy(subconfig)

full_key = subconfig._get_full_key(None)
if full_key:
full_config_copy = copy.deepcopy(subconfig._get_root())
if OmegaConf.is_list(subconfig._get_parent()):
# OmegaConf has a bug where _get_full_key doesn't add [] if the parent
# is a list, eg. instead of foo[0], it'll return foo0
index = subconfig._key()
full_key = full_key[: -len(str(index))] + f"[{index}]"
return OmegaConf.select(full_config_copy, full_key)
else:
return copy.deepcopy(subconfig)


def instantiate(config: Any, *args: Any, **kwargs: Any) -> Any:
"""
:param config: An config object describing what to call and what params to use.
Expand Down Expand Up @@ -207,11 +225,11 @@ def instantiate(config: Any, *args: Any, **kwargs: Any) -> Any:

if OmegaConf.is_dict(config):
# Finalize config (convert targets to strings, merge with kwargs)
config_copy = copy.deepcopy(config)
# Create full copy to avoid mutating original
config_copy = _deep_copy_full_config(config)
config_copy._set_flag(
flags=["allow_objects", "struct", "readonly"], values=[True, False, False]
)
config_copy._set_parent(config._get_parent())
config = config_copy

if kwargs:
Expand All @@ -228,11 +246,11 @@ def instantiate(config: Any, *args: Any, **kwargs: Any) -> Any:
)
elif OmegaConf.is_list(config):
# Finalize config (convert targets to strings, merge with kwargs)
config_copy = copy.deepcopy(config)
# Create full copy to avoid mutating original
config_copy = _deep_copy_full_config(config)
config_copy._set_flag(
flags=["allow_objects", "struct", "readonly"], values=[True, False, False]
)
config_copy._set_parent(config._get_parent())
config = config_copy

OmegaConf.resolve(config)
Expand Down
1 change: 1 addition & 0 deletions news/3001.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix unexpected resolution side-effect that caused modifications to the input config parent in `hydra.utils.instantiate`
44 changes: 43 additions & 1 deletion tests/instantiate/test_instantiate.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,8 +419,10 @@ def test_class_instantiate(
recursive: bool,
) -> Any:
passthrough["_recursive_"] = recursive
original_config_str = str(config)
obj = instantiate_func(config, **passthrough)
assert partial_equal(obj, expected)
assert str(config) == original_config_str


def test_partial_with_missing(instantiate_func: Any) -> Any:
Expand All @@ -431,10 +433,12 @@ def test_partial_with_missing(instantiate_func: Any) -> Any:
"b": 20,
"c": 30,
}
original_config_str = str(config)
partial_obj = instantiate_func(config)
assert partial_equal(partial_obj, partial(AClass, b=20, c=30))
obj = partial_obj(a=10)
assert partial_equal(obj, AClass(a=10, b=20, c=30))
assert str(config) == original_config_str


def test_instantiate_with_missing(instantiate_func: Any) -> Any:
Expand Down Expand Up @@ -468,6 +472,7 @@ def test_none_cases(
ListConfig(None),
],
}
original_config_str = str(cfg)
ret = instantiate_func(cfg)
assert ret.kwargs["none_dict"] is None
assert ret.kwargs["none_list"] is None
Expand All @@ -477,8 +482,10 @@ def test_none_cases(
assert ret.kwargs["list"][0] == 10
assert ret.kwargs["list"][1] is None
assert ret.kwargs["list"][2] is None
assert str(cfg) == original_config_str


@mark.parametrize("convert_to_list", [True, False])
@mark.parametrize(
"input_conf, passthrough, expected",
[
Expand Down Expand Up @@ -537,22 +544,57 @@ def test_none_cases(
6,
id="interpolation_from_recursive",
),
param(
{
"my_id": 5,
"node": {
"b": "${foo_b}",
},
"foo_b": {
"unique_id": "${my_id}",
},
},
{},
OmegaConf.create({"b": {"unique_id": 5}}),
id="interpolation_from_parent_with_interpolation",
),
param(
{
"my_id": 5,
"node": "${foo_b}",
"foo_b": {
"unique_id": "${my_id}",
},
},
{},
OmegaConf.create({"unique_id": 5}),
id="interpolation_from_parent_with_interpolation",
),
],
)
def test_interpolation_accessing_parent(
instantiate_func: Any,
input_conf: Any,
passthrough: Dict[str, Any],
expected: Any,
convert_to_list: bool,
) -> Any:
if convert_to_list:
input_conf = copy.deepcopy(input_conf)
input_conf["node"] = [input_conf["node"]]
cfg_copy = OmegaConf.create(input_conf)
input_conf = OmegaConf.create(input_conf)
obj = instantiate_func(input_conf.node, **passthrough)
original_config_str = str(input_conf)
if convert_to_list:
obj = instantiate_func(input_conf.node[0], **passthrough)
else:
obj = instantiate_func(input_conf.node, **passthrough)
if isinstance(expected, partial):
assert partial_equal(obj, expected)
else:
assert obj == expected
assert input_conf == cfg_copy
assert str(input_conf) == original_config_str


@mark.parametrize(
Expand Down
Loading