Skip to content
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

Fix grad accum + FSDP CPU offload, pass None via CLI #1941

Merged
merged 1 commit into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions torchtune/config/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ def _merge_yaml_and_cli_args(yaml_args: Namespace, cli_args: List[str]) -> DictC
# key string to reflect this
if k in yaml_kwargs and _has_component(yaml_kwargs[k]):
k += "._component_"

# None passed via CLI will be parsed as string, but we really want OmegaConf null
if v == "None":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be v.lower() == “none”? To avoid the None/none case

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might leave it as is.. I know it's not a heavily-used API in the library as of today, but e.g.

ac_mode (str): Activation checkpointing mode. ['none', 'full', 'selective']

Since we already have a case that's using 'none' as a string, I don't wanna mess with that

v = "!!null"

# TODO: this is a hack but otherwise we can't pass strings with leading zeroes
# to define the checkpoint file format. We manually override OmegaConf behavior
# by prepending the value with !!str to force a string type
Expand Down
5 changes: 5 additions & 0 deletions torchtune/training/_grad_scaler.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ def scale_grads(model: nn.Module, scaler: torch.Tensor) -> None:
Outputs:
None (grad fields are modified in place)
"""
device = None
for p in model.parameters():
# First ensure scaler is on the same device as the model
if not device:
device = p.device
scaler = scaler.to(device)
if p.grad is not None:
p.grad *= scaler
Loading