This repository has been archived by the owner on Nov 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
[params] Support options depending on options in parse_kwargs. #3900
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1203,62 +1203,81 @@ def _kwargs_to_str_args(self, **kwargs): | |
), f"No duplicate names! ({kwname}, {kwname_to_action[kwname]}, {action})" | ||
kwname_to_action[kwname] = action | ||
|
||
string_args = [] | ||
for kwname, value in kwargs.items(): | ||
if kwname not in kwname_to_action: | ||
# best guess, we need to delay it. hopefully this gets added | ||
# during add_kw_Args | ||
continue | ||
action = kwname_to_action[kwname] | ||
last_option_string = action.option_strings[-1] | ||
if isinstance(action, argparse._StoreTrueAction): | ||
if bool(value): | ||
# since we can have options that depend on options, repeat until convergence | ||
unparsed_args = set(kwargs.keys()) | ||
while unparsed_args: | ||
string_args = [] | ||
for kwname, value in kwargs.items(): | ||
if kwname not in kwname_to_action: | ||
# best guess, we need to delay it. hopefully this gets added | ||
# during add_kw_Args | ||
continue | ||
action = kwname_to_action[kwname] | ||
last_option_string = action.option_strings[-1] | ||
if isinstance(action, argparse._StoreTrueAction): | ||
if bool(value): | ||
string_args.append(last_option_string) | ||
elif isinstance(action, argparse._StoreAction) and action.nargs is None: | ||
string_args.append(last_option_string) | ||
elif isinstance(action, argparse._StoreAction) and action.nargs is None: | ||
string_args.append(last_option_string) | ||
string_args.append(self._value2argstr(value)) | ||
elif isinstance(action, argparse._StoreAction) and action.nargs in '*+': | ||
string_args.append(last_option_string) | ||
string_args.extend([self._value2argstr(value) for v in value]) | ||
else: | ||
raise TypeError(f"Don't know what to do with {action}") | ||
|
||
# become aware of any extra args that might be specified if the user | ||
# provides something like model="transformer/generator". | ||
self.add_extra_args(string_args) | ||
|
||
# do it again, this time knowing about ALL args. | ||
kwname_to_action = {} | ||
for action in self._actions: | ||
if action.dest == 'help': | ||
# no help allowed | ||
continue | ||
for option_string in action.option_strings: | ||
kwname = option_string.lstrip('-').replace('-', '_') | ||
assert (kwname not in kwname_to_action) or ( | ||
kwname_to_action[kwname] is action | ||
), f"No duplicate names! ({kwname}, {kwname_to_action[kwname]}, {action})" | ||
kwname_to_action[kwname] = action | ||
|
||
string_args = [] | ||
for kwname, value in kwargs.items(): | ||
# note we don't have the if kwname not in kwname_to_action here. | ||
# it MUST appear, or else we legitimately should be throwing a KeyError | ||
# because user has provided an unspecified option | ||
action = kwname_to_action[kwname] | ||
last_option_string = action.option_strings[-1] | ||
if isinstance(action, argparse._StoreTrueAction): | ||
if bool(value): | ||
string_args.append(self._value2argstr(value)) | ||
elif isinstance(action, argparse._StoreAction) and action.nargs in '*+': | ||
string_args.append(last_option_string) | ||
elif isinstance(action, argparse._StoreAction) and action.nargs is None: | ||
string_args.append(last_option_string) | ||
string_args.append(self._value2argstr(value)) | ||
elif isinstance(action, argparse._StoreAction) and action.nargs in '*+': | ||
string_args.append(last_option_string) | ||
# Special case: Labels | ||
string_args.extend([str(v) for v in value]) | ||
string_args.extend([self._value2argstr(value) for v in value]) | ||
else: | ||
raise TypeError(f"Don't know what to do with {action}") | ||
|
||
# become aware of any extra args that might be specified if the user | ||
# provides something like model="transformer/generator". | ||
self.add_extra_args(string_args) | ||
|
||
# do it again, this time knowing about ALL args. | ||
kwname_to_action = {} | ||
for action in self._actions: | ||
if action.dest == 'help': | ||
# no help allowed | ||
continue | ||
for option_string in action.option_strings: | ||
kwname = option_string.lstrip('-').replace('-', '_') | ||
assert (kwname not in kwname_to_action) or ( | ||
kwname_to_action[kwname] is action | ||
), f"No duplicate names! ({kwname}, {kwname_to_action[kwname]}, {action})" | ||
kwname_to_action[kwname] = action | ||
|
||
new_unparsed_args = set() | ||
string_args = [] | ||
for kwname, value in kwargs.items(): | ||
# note we don't have the if kwname not in kwname_to_action here. | ||
# it MUST appear, or else we legitimately should be throwing a KeyError | ||
# because user has provided an unspecified option | ||
if kwname not in kwname_to_action: | ||
new_unparsed_args.add(kwname) | ||
continue | ||
Comment on lines
+1250
to
+1252
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These lines are new |
||
|
||
action = kwname_to_action[kwname] | ||
last_option_string = action.option_strings[-1] | ||
if isinstance(action, argparse._StoreTrueAction): | ||
if bool(value): | ||
string_args.append(last_option_string) | ||
elif isinstance(action, argparse._StoreAction) and action.nargs is None: | ||
string_args.append(last_option_string) | ||
string_args.append(self._value2argstr(value)) | ||
elif isinstance(action, argparse._StoreAction) and action.nargs in '*+': | ||
string_args.append(last_option_string) | ||
# Special case: Labels | ||
string_args.extend([str(v) for v in value]) | ||
else: | ||
raise TypeError(f"Don't know what to do with {action}") | ||
|
||
if new_unparsed_args == unparsed_args: | ||
# if we have converged to a fixed point with no improvements, we | ||
# truly found some unreachable args | ||
raise KeyError( | ||
f'Failed to parse one or more kwargs: {", ".join(new_unparsed_args)}' | ||
) | ||
else: | ||
raise TypeError(f"Don't know what to do with {action}") | ||
# We've seen some improvements on the number of unparsed args, | ||
# iterate again | ||
unparsed_args = new_unparsed_args | ||
Comment on lines
+1269
to
+1278
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These lines are new |
||
|
||
return string_args | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These 3 lines are the real change (the rest is just indentation)