-
-
Notifications
You must be signed in to change notification settings - Fork 52
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
Add set_choices
to define a list of classes to choose from
#84
Comments
Questions:
|
which would be equivalent to using
Perhaps both
I'd say not supported. In that case, or when you want to pass a subclass, the users should use |
For future reference, this has been implemented here: Lightning-AI/pytorch-lightning#9565 by modifying the The |
add_class_choices
as a stricter add_subclass_arguments
alternativeset_choices
to define a list of classes to choose from
Since getting all of the subclasses for a given class is trivial (see get_all_subclasses), adding a Both in command line and in config, giving just the class name would work, e.g. |
So if this is the case, that's good with me!
What about input configs? Would they support this "shorthand" notation? |
Yes, like the json examples I wrote. |
…lasses #84. - add_argument with subclass type now also adds --*.help option. - Shorter subclass command line arguments by not requiring to have .init_args. - class_path's on parse are now normalized to shortest form. - Fixes for latest version of mypy.
I committed an initial version of this to master and created a pre-release v4.6.0.dev1. |
One issue of all imported subclasses supported is that there could be multiple with the same class name. Still, I think is preferable to do this than having to explicitly register with
Thus I am thinking of something like |
I am considering this feature implemented and closing this issue. My previous comment would be a different feature and I don't consider it important. If multiple subclasses have the same name and you specify a class path with this name, a warning is shown telling which class was used and recommending to use the full class path. Though I wonder, instead of a warning should it be a parser error? This feature will be part of jsonargparse v4.6.0. |
Pitch
add_subclass_arguments
provides a lot of flexibility for the cases where you don't know which implementation will be used, however, this flexibility can come with an increase in boilerplate as the users need to specify:--an_argument.class_path=the.full.class.path.to.object --an_argument.init_args.the_arg=value
Additionally, knowing the full class path can be non-trivial if the object does not come from an installed package.
For the cases where you want to provide a set of potential classes to choose from but without the requirement of supporting subclasses of those, a
parser.set_choices()
method could be implemented. This method would simplify the input arguments to:--an_argument.class=object --an_argument.the_arg=value
Code
Notes:
add_class_choices
wasn't good enough because it meant this could only be done to arguments added withadd_subclass_arguments
. However, this also needs to be supported:Additional context:
We are implementing this in Lightning-AI/pytorch-lightning#9565
Almost all Lightning users will choose an optimizer already provided by
torch
. This method would enable changing them seamlessly with minimal boilerplate.In that PR, we use a
Registry
object which holds the list of classes to be used as choices foradd_subclass_arguments
.I don't think
jsonargparse
needs to expose this. We would use it to generate the list of classes passed toadd_class_choices
:The text was updated successfully, but these errors were encountered: