Skip to content

Commit

Permalink
Fix grad accum + FSDP CPU offload, pass None via CLI (#1941)
Browse files Browse the repository at this point in the history
  • Loading branch information
ebsmothers authored Nov 1, 2024
1 parent f560cbb commit bc4acc1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
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":
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

0 comments on commit bc4acc1

Please sign in to comment.