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

Add grad norm logging single device #1451

Merged
merged 7 commits into from
Aug 31, 2024

Conversation

lindawangg
Copy link
Contributor

@lindawangg lindawangg commented Aug 29, 2024

Context

What is the purpose of this PR? Is it to

  • add a new feature
  • fix a bug
  • update tests and/or documentation
  • other (please add here)

Please link to any issues this PR addresses.

Changelog

What are the changes made in this PR?

  • Adds optional clip grad norm
  • Default logs grad norm is clip_grad_norm is set

Test plan

Please make sure to do each of the following if applicable to your PR. (If you're not sure about any one of these just ask and we will happily help. We also have a contributing page for some guidance on contributing.)

  • run pre-commit hooks and linters (make sure you've first installed via pre-commit install)
  • add unit tests for any new functionality
  • update docstrings for any new or updated methods or classes
  • run unit tests via pytest tests
  • run recipe tests via pytest tests -m integration_test
  • manually run any new or modified recipes with sufficient proof of correctness
  • include relevant commands and any other artifacts in this summary (pastes of loss curves, eval results, etc.)
tune run lora_finetune_single_device --config llama2/7B_lora_single_device max_steps_per_epoch=100 clip_grad_norm=100 metric_logger._component_=torchtune.utils.metric_logging.TensorBoardLogger checkpointer.output_dir=/tmp/llama2_w_grad_norm

Did not observe any changes to overall compute time or loss.

  • orange is with log_grad_norm=True, blue is baseline
image image image image
tune run full_finetune_single_device --config llama2/7B_full_low_memory metric_logger._component_=torchtune.utils.metric_logging.TensorBoardLogger checkpointer.output_dir=/tmp/llama2_w_grad_norm_full optimizer_in_bwd=False clip_grad_norm=100

Observed no increase in batch/s. Verified clip_grad_norm clips gradients (in orange)

  • green is baseline, purple is clip_grad_norm='inf', orange is clip_grad_norm=100
image image image

UX

  • I did not change any public API;
  • I have added an example to docs or docstrings;

Copy link

pytorch-bot bot commented Aug 29, 2024

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/torchtune/1451

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit 2d46255 with merge base dfc69e2 (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Aug 29, 2024
@@ -132,6 +136,10 @@ def __init__(self, cfg: DictConfig) -> None:
self._resume_from_checkpoint = cfg.resume_from_checkpoint
self._save_adapter_weights_only = cfg.get("save_adapter_weights_only", False)
self._gradient_accumulation_steps = cfg.gradient_accumulation_steps
self._clip_grad_norm = cfg.get("clip_grad_norm", None)
self._log_grad_norm = cfg.get("log_grad_norm", False)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We don't really need log_grad_norm. If clip_grad_norm is set, we can log grad norm by default. Computing this each step doesn't increase the time for single device.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah based on your results I agree, seems like no negative perf impact to just logging by default. Can you also make the same changes in full_finetune_single_device.py and confirm that no negative perf impact there? (I think we should enable for both recipes in one go.)

Distributed may be a different story if we have to sync, but we can consider that out of scope for this PR.

If the results look similar for full finetune, I agree it makes sense to go ahead and remove the log_grad_norm config. I would even bias towards just always logging grad norm (that way you don't have to awkwardly set it to 'inf' to turn on logging, imo that's a bit unintuitive).

Copy link
Contributor

@ebsmothers ebsmothers left a comment

Choose a reason for hiding this comment

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

This looks good! Apart from my comment around enabling for full finetune, we can also run a version with clip_grad_norm set to some finite value to ensure that we see it reflected correctly in the logs.

@lindawangg lindawangg marked this pull request as ready for review August 30, 2024 04:40
@lindawangg lindawangg changed the title [WIP] Add grad norm logging single device Add grad norm logging single device Aug 30, 2024
Copy link
Contributor

@ebsmothers ebsmothers left a comment

Choose a reason for hiding this comment

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

This looks great, thank you!

@ebsmothers ebsmothers merged commit d3df28b into pytorch:main Aug 31, 2024
20 checks passed
@lindawangg lindawangg deleted the add-feature-grad-norm branch September 1, 2024 04:59
@RdoubleA RdoubleA mentioned this pull request Sep 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants