-
Notifications
You must be signed in to change notification settings - Fork 51
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
Subgroups destination cannot be a substring of other destinations #191
Comments
Hello, thank you for your prompt reply. I am confused 😕 Does it really work for you? It actually does not work for me from the current master, the example above gives me:
If I make the dataclasses hashable: @dataclass(unsafe_hash=True)
class ModelAConfig(ModelConfig):
lr: float = 3e-4
optimizer: str = "Adam"
betas: tuple[float, float] = 0.9, 0.999
@dataclass(unsafe_hash=True)
class ModelBConfig(ModelConfig):
lr: float = 1e-3
optimizer: str = "SGD"
momentum: float = 1.234 I get
so I have to change the default to a string key: @dataclass
class Config:
# Which model to use
model: ModelConfig = subgroups(
{"model_a": ModelAConfig, "model_b": ModelBConfig},
default="model_a",
) But I lost the ability to set some parameters in the default, for example |
Please refere to #186 and You can use the default_factory as indicated by Here are some examples to achieve your goal: @dataclass
class Config:
# Which model to use
model: ModelConfig = subgroups(
{"model_a": ModelAConfig, "model_b": ModelBConfig},
default_factory=ModelAConfig,
)
@dataclass
class Config:
# Which model to use
model: ModelConfig = subgroups(
{"model_a": ModelAConfig, "model_b": ModelBConfig},
default_factory=lambda : ModelAConfig(lr=0.1),
) |
Very nice 🙂 Is it supposed to work out of the box? Asking because there seems to be this check (so the factory itself has to be a value in the dict so I do not see the way to set different arguments here): SimpleParsing/simple_parsing/helpers/fields.py Lines 418 to 421 in 5042cb4
So @dataclass
class Config:
# Which model to use
model: ModelConfig = subgroups(
{"model_a": ModelAConfig, "model_b": ModelBConfig},
default_factory=lambda : ModelAConfig(lr=0.1),
) actually results in an Maybe something like this could work? if default_factory is not MISSING and type(default_factory()) not in subgroups.values():
... |
What version of simple-parsing are you working on? The default_factory is not implemented in 0.0.21.post1 as per #187, but it is now implemented in |
I hope it is the current master (5042cb4) aka |
Thanks for pointing this out. That is very interesting I overlooked this part. Please ignore my above reply. The question is why do we need to set default values which are different from corresponding dataclasses defaults? |
Okay, my situation is the following 🙂 My dataclasses are auto-generated from "serializable" classes, or better said from arguments of their constructors. For example: class AdamW(TorchAdamW, SerializableObject, ignore_args=["self", "params"]):
"""Wrapper around AdamW to name its parameters for serialization."""
def __init__(
self,
params,
lr: float = 1e-3,
eps: float = 1e-6,
weight_decay: float = 0.01,
amsgrad: bool = False,
):
super().__init__(
params,
lr=lr,
eps=eps,
weight_decay=weight_decay,
amsgrad=amsgrad,
) produces a dataclass like this one: @dataclass
class AdamWConfig:
lr: float = 0.001
eps: float = 1e-06
weight_decay: float = 0.01
amsgrad: bool = False This config dataclass is used from different different projects, so I do not want to change the defaults here. class OptimizerConfig(Config):
opt: Union[AdamW.get_config_class(), FusedLAMB.get_config_class()] = subgroups(
{
"adamw": AdamW.get_config_class(),
"fusedlamb": FusedLAMB.get_config_class(),
},
# default_factory=lambda: AdamW.get_config_class()(lr=0.005),
) and I believe that here, in this optimizer config, is a suitable place for overriding the project-related default setting. Does it make sense or is this setup weird? |
Hi @Tomiinek , thanks for posting this. Yes you're right, I've temporarily restricted what can be passed to the By the way, your dynamic generated config classes look a lot like what I've been doing here: #156 Just hang tight for now 😅 I'll figure out a solution to this soon, and get back to you. |
In the case of my # Dynamically create a dataclass that will be used for the above type:
# NOTE: We could use Partial[Adam] or Partial[Optimizer], however this would treat `params` as a
# required argument.
# AdamConfig = Partial[Adam] # would treat 'params' as a required argument.
# SGDConfig = Partial[SGD] # same here
AdamConfig: type[Partial[Adam]] = config_dataclass_for(Adam, ignore_args="params")
SGDConfig: type[Partial[SGD]] = config_dataclass_for(SGD, ignore_args="params")
@dataclass
class Config:
# Which optimizer to use.
optimizer: Partial[Optimizer] = subgroups(
{
"sgd": SGDConfig,
"adam": AdamConfig,
},
default_factory=lambda: AdamConfig(lr=3e-4),
) |
Well I'll actually first add a test to make sure that this issue is fixed. THEN, I'll close this issue :) |
Great! Thank you both guys. |
Fixes #191: Uses the same example from the issue, and the problem doesn't occur. I consider that convincing enough to close the issue. Signed-off-by: Fabrice Normandin <fabrice.normandin@gmail.com>
Fixes #191: Uses the same example from the issue, and the problem doesn't occur. I consider that convincing enough to close the issue. Signed-off-by: Fabrice Normandin <fabrice.normandin@gmail.com> Signed-off-by: Fabrice Normandin <fabrice.normandin@gmail.com>
Hey @Tomiinek , I've now made this issue #195 and PR here #196 that adds the ability to use Let me know what you think! :) |
Hey, that's a really nice solution! 🙂 Pls merge ASAP 😅 |
Describe the bug
I would like to have two subgroups with a different destinations, let's say
config
andconfig2
. However, if one of them is a substring of another, an exception is raised saying:To Reproduce
Expected behavior
Not to rise the exception 🙂
I am not sure, but maybe this condition
SimpleParsing/simple_parsing/parsing.py
Lines 768 to 771 in 545aa46
should be changed to:
The text was updated successfully, but these errors were encountered: