-
-
Notifications
You must be signed in to change notification settings - Fork 625
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
[Bug] Cannot override a default config group that has been set up in a Structured Config #2039
Comments
Hello again @jzazo! First, let me visualize the tree of input configs that can be accessed by Hydra:
Your first example:
Yes indeed. In this case, the following four input configs are being merged:
This can be confirmed by using the
Note that the files Your second example:
This is expected. The issue is that an instance of >>> input1 = Config # base_config
>>> input2 = {"model": RegressionConf} # ml/regresssion@model
>>> input3 = {"model": {"criterion": BCEConf}} # ml/bce@model.criterion
>>> input4 = {} # config
>>> OmegaConf.merge(input1, input2, input3, input4)
Traceback (most recent call last):
...
omegaconf.errors.ValidationError: Merge error: BCEConf is not a subclass of CrossEntropyConf. value: {'_target_': 'torch.nn.BCELoss'}
full_key:
object_type=Config Note that the error message exactly the same. Your third example:
This is also expected. Basically, writing Instead of searching for a config at @dataclass
class RegressionConf:
defaults: List[Any] = field(default_factory=lambda: [{"/ml@criterion": "cross_entropy"}])
criterion: CriterionConf = MISSING The For this to work, you will also need to change your defaults:
- base_config
- /ml/regression@model
- override /ml@model.criterion: bce $ ./example.py
model:
criterion:
_target_: torch.nn.BCELoss You can find more documentation on how this works in the docs on the defaults list. Note that once again the files A final example:Below is a strategy for storing @dataclass
class RegressionConf:
defaults: List[Any] = field(default_factory=lambda: [{"criterion": "cross_entropy"}])
criterion: CriterionConf = MISSING
...
cs = ConfigStore.instance()
cs.store(name="base_config", node=Config)
cs.store(group="model", name="regression", node=RegressionConf)
cs.store(group="model/criterion", name="cross_entropy", node=CrossEntropyConf)
cs.store(group="model/criterion", name="bce", node=BCEConf) With this setup, the tree of accessible input configs looks like this:
Now, change defaults:
- base_config
- model: regression
- override model/criterion: bce Running the example: $ ./example.py
model:
criterion:
_target_: torch.nn.BCELoss |
Thank you @Jasha10 for this awesome answer. Everything is quite clear, although I am still struggling a bit (in my real problem, see message below). Regading your proposed fixes, if I try to reproduce the third example with:
My output becomes:
In your final example, I get the following output as well:
It seems that the defaults list is persistent in the returned config, why does this happen? I cannot instantiate objects with such attribute automatically. |
I have finally identified the source of the problems I was getting in my original problem. If I change the type of The following works:
resulting in:
The follwoing breaks:
fails with:
This is the config store in both cases:
It seems to me that the behavior is different if the default list is on yaml or in the structured config. In the yaml case the inferred type is of Thanks for all the support! |
🐛 Bug
Description
I set up structured config schemas with nested config groups as attributes. I want to set one config group as a default value in the structured config schema, and override it in the yaml files. I get different errors depending if I set the default in the defaults list, or directly as an initialized config.
Checklist
To reproduce
This is my
example.py
file:This is my
conf/config.yaml
file:This is my
conf/criterion/bce.yaml
:This is my
conf/criterion/cross_entropy.yaml
:If I run
./example.py
(no default list yet), everything works and I get:If I modify the
RegressionConf
with an initialized config, e.g.,I get the following error:
If I modify
RegressionConf
with a defaults list, e.g.,I get the following error:
Expected Behavior
In the first case I would expect that the type of
criterion
remainsCriterionConf
and does not switch toCrossEntropyLoss
.In the second case, I would expect that the group config
/ml/cross/entropy
is global, rather than relative, i.e.,ml/criterion//ml/cross_entropy
, as the error seems to indicate.System information
The text was updated successfully, but these errors were encountered: