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

Log grad norm aggregated over all ranks, not just rank zero #2248

Merged
merged 1 commit into from
Jan 10, 2025
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
2 changes: 1 addition & 1 deletion recipes/dev/early_exit_finetune_distributed.py
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,7 @@ def train(self) -> None:
grad_norm = torch.nn.utils.clip_grad_norm_(
self._model.parameters(),
max_norm=float(self._clip_grad_norm),
)
).full_tensor()
self._optimizer.step()
self._optimizer.zero_grad(set_to_none=True)

Expand Down
2 changes: 1 addition & 1 deletion recipes/full_finetune_distributed.py
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,7 @@ def train(self) -> None:
grad_norm = torch.nn.utils.clip_grad_norm_(
self._model.parameters(),
max_norm=float(self._clip_grad_norm),
)
).full_tensor()
Copy link
Contributor

@mirceamironenco mirceamironenco Jan 10, 2025

Choose a reason for hiding this comment

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

Do you think it might be a good idea to have the .full_tensor() behind an isintance(grad_norm, DTensor) check? If e.g. DDP ever gets implemented and torchtune takes care of it behind some API (say shard_model allows for different types of parallelisms, or PP gets added), this will no longer be valid, causing every recipe to be updated?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mirceamironenco yes I agree we should have the check when we enable new types of parallelism. But I also don't want to prematurely expose it (our recipes are already more complicated than I would like and adding a check that's currently a no-op is an easy case of more code to read than we currently we need). I think your TP example is a very likely case and when we enable something like that wrapping grad norm logic in an appropriate utility (kinda like what you shared with me over Discord) will be the way to go. But until then I don't think we should do it. Hope that makes sense

self._optimizer.step()
self._optimizer.zero_grad(set_to_none=True)

Expand Down
2 changes: 1 addition & 1 deletion recipes/lora_finetune_distributed.py
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,7 @@ def train(self) -> None:
grad_norm = torch.nn.utils.clip_grad_norm_(
self._model.parameters(),
max_norm=float(self._clip_grad_norm),
)
).full_tensor()
self._optimizer.step()
self._optimizer.zero_grad(set_to_none=True)
self._lr_scheduler.step()
Expand Down
2 changes: 1 addition & 1 deletion recipes/lora_finetune_distributed_multi_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,7 @@ def train(self) -> None:
grad_norm = torch.nn.utils.clip_grad_norm_(
self._model.parameters(),
max_norm=float(self._clip_grad_norm),
)
).full_tensor()
self._optimizer.step()
self._optimizer.zero_grad(set_to_none=True)
self._lr_scheduler.step()
Expand Down
2 changes: 1 addition & 1 deletion recipes/qat_distributed.py
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,7 @@ def train(self) -> None:
grad_norm = torch.nn.utils.clip_grad_norm_(
self._model.parameters(),
max_norm=float(self._clip_grad_norm),
)
).full_tensor()
self._optimizer.step()
self._optimizer.zero_grad(set_to_none=True)

Expand Down
2 changes: 1 addition & 1 deletion recipes/qat_lora_finetune_distributed.py
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,7 @@ def train(self) -> None:
grad_norm = torch.nn.utils.clip_grad_norm_(
self._model.parameters(),
max_norm=float(self._clip_grad_norm),
)
).full_tensor()
self._optimizer.step()
self._optimizer.zero_grad(set_to_none=True)
self._lr_scheduler.step()
Expand Down
Loading