Repository navigation
Allow for str versions of dicts based on typing #30227
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
Merged
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
db72d1a
Bookmark, initial impelemtation. Need to test
muellerzr 4798149
Clean
muellerzr a8e132c
Working fully, woop woop
muellerzr 5d9a39a
I think working version now, testing
muellerzr 16c3bf7
Fin!
muellerzr a483b4b
rm cast, could keep None
muellerzr 98ce2dc
Fix typing issue
muellerzr 9c69c16
rm typehint
muellerzr 1c73ab1
Add test
muellerzr f81dcf0
Add tests and make more rigid
muellerzr 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 hidden or 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 |
|---|---|---|
|
|
@@ -173,6 +173,19 @@ class OptimizerNames(ExplicitEnum): | |
| GALORE_ADAFACTOR_LAYERWISE = "galore_adafactor_layerwise" | ||
|
|
||
|
|
||
| # Sometimes users will pass in a `str` repr of a dict in the CLI | ||
| # We need to track what fields those can be. Each time a new arg | ||
| # has a dict type, it must be added to this list. | ||
| # Important: These should be typed with Optional[Union[dict,str,...]] | ||
| VALID_DICT_FIELDS = [ | ||
|
muellerzr marked this conversation as resolved.
Resolved
|
||
| "accelerator_config", | ||
| "fsdp_config", | ||
| "deepspeed", | ||
| "gradient_checkpointing_kwargs", | ||
| "lr_scheduler_kwargs", | ||
| ] | ||
|
|
||
|
|
||
| # TODO: `TrainingArguments` users rely on it being fully mutable. In the future see if we can narrow this to a few keys: https://github.com/huggingface/transformers/pull/25903 | ||
| @dataclass | ||
| class TrainingArguments: | ||
|
|
@@ -803,11 +816,11 @@ class TrainingArguments: | |
| default="linear", | ||
| metadata={"help": "The scheduler type to use."}, | ||
| ) | ||
| lr_scheduler_kwargs: Optional[Dict] = field( | ||
| lr_scheduler_kwargs: Optional[Union[dict, str]] = field( | ||
| default_factory=dict, | ||
| metadata={ | ||
| "help": ( | ||
| "Extra parameters for the lr_scheduler such as {'num_cycles': 1} for the cosine with hard restarts" | ||
| "Extra parameters for the lr_scheduler such as {'num_cycles': 1} for the cosine with hard restarts." | ||
| ) | ||
| }, | ||
| ) | ||
|
|
@@ -1118,7 +1131,6 @@ class TrainingArguments: | |
| ) | ||
| }, | ||
| ) | ||
| # Do not touch this type annotation or it will stop working in CLI | ||
| fsdp_config: Optional[Union[dict, str]] = field( | ||
| default=None, | ||
| metadata={ | ||
|
|
@@ -1137,8 +1149,7 @@ class TrainingArguments: | |
| ) | ||
| }, | ||
| ) | ||
| # Do not touch this type annotation or it will stop working in CLI | ||
| accelerator_config: Optional[str] = field( | ||
| accelerator_config: Optional[Union[dict, str]] = field( | ||
| default=None, | ||
| metadata={ | ||
| "help": ( | ||
|
|
@@ -1147,8 +1158,7 @@ class TrainingArguments: | |
| ) | ||
| }, | ||
| ) | ||
| # Do not touch this type annotation or it will stop working in CLI | ||
| deepspeed: Optional[str] = field( | ||
| deepspeed: Optional[Union[dict, str]] = field( | ||
| default=None, | ||
| metadata={ | ||
| "help": ( | ||
|
|
@@ -1252,7 +1262,7 @@ class TrainingArguments: | |
| "help": "If True, use gradient checkpointing to save memory at the expense of slower backward pass." | ||
| }, | ||
| ) | ||
| gradient_checkpointing_kwargs: Optional[dict] = field( | ||
| gradient_checkpointing_kwargs: Optional[Union[dict, str]] = field( | ||
| default=None, | ||
| metadata={ | ||
| "help": "Gradient checkpointing key word arguments such as `use_reentrant`. Will be passed to `torch.utils.checkpoint.checkpoint` through `model.gradient_checkpointing_enable`." | ||
|
|
@@ -1380,6 +1390,14 @@ class TrainingArguments: | |
| ) | ||
|
|
||
| def __post_init__(self): | ||
| # Parse in args that could be `dict` sent in from the CLI as a string | ||
| for field in VALID_DICT_FIELDS: | ||
|
Contributor
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. I like just checking for a few possible args! |
||
| passed_value = getattr(self, field) | ||
| # We only want to do this if the str starts with a bracket to indiciate a `dict` | ||
| # else its likely a filename if supported | ||
| if isinstance(passed_value, str) and passed_value.startswith("{"): | ||
| setattr(self, field, json.loads(passed_value)) | ||
|
|
||
| # expand paths, if not os.makedirs("~/bar") will make directory | ||
| # in the current directory instead of the actual home | ||
| # see https://github.com/huggingface/transformers/issues/10628 | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.