-
Notifications
You must be signed in to change notification settings - Fork 15
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
Enabling users to add auto-config support for custom types #257
Comments
I like the idea of an I think I'm running into a similar issue where a custom class cannot be converted to Hydra-compatible primitives, but I'm not 100% sure this is what you're getting at. Specifically PyTorch Lightning Flash has import flash
from hydra_zen import builds
from omegaconf import OmegaConf
InputTransformConf = builds(flash.core.data.io.input_transform.InputTransform, populate_full_signature=True)
print(OmegaConf.to_yaml(InputTransformConf)) This causes an error
It sounds like the desired/proposed solution would be to handle the typing for
The second option isn't really available in this case since I'm importing from a 3rd party library, so it is nice to have both approaches |
Thanks for sharing this @addisonklinke. It is very helpful for me to see how you are using hydra-zen. I will need to look into the specific example that you posted (glancing at the source code you linked, I don't see how this error popped up. It looks like the default value for Now that I have more time to work on hydra-zen, I hope to push forward with hydra-zen's auto-config capabilities |
@rsokl Another library class where I ran into issues from hydra_zen import builds
import torch
cfg = builds(torch.optim.SGD, populate_full_signature=True) This raises the unsupported primitive error for |
Their Anyway, we can add auto-config support for this by replacing it with from omegaconf import MISSING
cfg = builds(torch.optim.SGD, lr=MISSING, populate_full_signature=True) |
@addisonklinke 8a8179b adds support for |
@addisonklinke I know this issue is oh-so very old, but I have officially introduced a means for adding auto-config support to all config-creation functions! https://mit-ll-responsible-ai.github.io/hydra-zen/changes.html#rc1-2023-10-23 |
Summary
It would be nice for users to be able to register custom conversion strategies so that hydra-zen's various config-creation functions can know how to convert values of those types into corresponding structured config representations
Motivation
hydra-zen supports values of types that are not natively supported by Hydra.
E.g. we provide custom support for values of
pathlib.Path
andcomplex
. This means that values of these types can be provided, natively, to all of hydra-zen's config-creation functions (just
,builds
, andmake_config
) and utilities (to_yaml
,save_yaml
) (as of #259 )Under the hood , we store a dictionary
ZEN_VALUE_CONVERSION
that mapstype -> conversion_fn
, whereconversion_fn
describes how to take a value of that type and represent that value as a structured config (such that instantiating the structured config returns that value).Think of it as pickling, but in the form of structured configs. Then
instantiate
does the unpickling.It would be nice if users could leverage their own conversion functions!
Implementation Details
There would be a two ways that we can expose this functionality: via a registration API and via an object-oriented API. These are not exclusive; indeed, we would probably enable all of these:
hydra_zen.register_converter
(happy to change the name)We would closely mirror Hypothesis'
register_type_strategy
implementation and enable users to manually register conversion functions:now hydra-zen's config creation functions will understand how to convert
WeirdClass
instances to corresponding configsThese type conversion functions can be registered en masse and automatically using entrypoints. This is again following in Hypothesis' footsteps.
hydra-zen's various utility functions will automatically "understand" these values, so one will be able to write:
and this will simply work!
__as_config__
(also happy to change this name)As an alternative, library authors can also implement a
__as_config__
method that hydra-zen will look for:Now,
make_config
(and other config-creation functions) will seeWeirdClass
's__as_config__
method, and defer to that:Some additional considerations
Presently, hydra-zen will not check for subclass relationships. E.g. we can create configs of type
complex
, but we don't handle values of subclasses ofcomplex
. We may wantregister_converter
to be able to specify whether or not the strategy is valid for subclasses. Fortunately__as_config__
can handle this elegantly.Another thing to consider is that this approach to config-creation should be used with restraint. Even though we could register strategies for all
torch
types, we should not encourage users to instantiate an entire resnet, just to pass it tojust
to be made into a config... Well, maybe this is okay, but it seems awfully slow and "wasteful".Ultimately, this functionality is meant to make it painless to take Python objects that you are working with and to create their structured config representations. This does not accommodate common configuration use cases, like specifying interpolated fields:
Feedback
Does this make sense? Does it seem useful? Are there any pitfalls that I am missing?
The text was updated successfully, but these errors were encountered: