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 a "division by zero" check in chunked loss handling in kd_losses.py #2239

Merged
merged 6 commits into from
Jan 14, 2025

Conversation

insop
Copy link
Contributor

@insop insop commented Jan 9, 2025

Context

What is the purpose of this PR? Is it to

  • add a new feature
  • fix a bug
  • update tests and/or documentation
  • other (add a "division by zero" check in chunked loss handling in kd_losses.py)

Please link to any issues this PR addresses.

Changelog

What are the changes made in this PR?

Test plan

Please make sure to do each of the following if applicable to your PR. If you're unsure 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 pytest -v tests/torchtune/modules/loss/test_kd_losses.py
  • 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.)

UX

If your function changed a public API, please add a dummy example of what the user experience will look like when calling it.
Here is a docstring example
and a tutorial example

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

Copy link

pytorch-bot bot commented Jan 9, 2025

🔗 Helpful Links

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

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

✅ No Failures

As of commit f5befaf with merge base c152248 (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 Jan 9, 2025
@insop
Copy link
Contributor Author

insop commented Jan 9, 2025

@felipemello1 , @joecummings , PTAL

Copy link
Contributor

@joecummings joecummings left a comment

Choose a reason for hiding this comment

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

The tests look awesome - thanks!

Just a couple questions/comments on code clarity.

torchtune/modules/loss/kd_losses.py Outdated Show resolved Hide resolved
@@ -137,4 +137,7 @@ def forward(
student_chunk, teacher_chunk, label_chunk, normalize=False
)

if torch.sum(mask.view(-1), dim=0) == 0:
return torch.tensor(0.0, device=student_logits[0].device)
Copy link
Contributor

Choose a reason for hiding this comment

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

In the discussion on #2094, it seems like if we hit this point, there's probably something wrong with the training data. In that case, should we error out here? Or drop a warning? Or let people shoot themselves in the foot?

cc @lindawangg and @ebsmothers

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 will wait for inputs, and will update #2094 after this PR is resolved.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Any inputs?
@lindawangg , @ebsmothers

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for the bump here. Personally I think this is the right way to handle it. Yes, we could error/raise a warning, but honestly this behavior should be pretty obvious in a loss curve as it will clearly just drop to zero on any such iterations (plus I think zero loss would technically be the "correct" loss value if every token is being ignored). No need to overdo it on handling this edge case here imo

@insop insop requested a review from joecummings January 10, 2025 00:39
@@ -17,7 +17,7 @@ def random():


class TestForwardKLWithChunkedOutputLoss:
def test_forward_kl_loss(self):
def setup_forward_kl_loss(self, set_all_masks_zero: bool = False):
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: I would do something like "ignore_all_tokens" instead of "set_all_masks_zero" as I think it's clearer

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds better, and updated.

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.

Thanks for the PR! Just one minor comment on naming in the test, otherwise this looks good

@insop
Copy link
Contributor Author

insop commented Jan 11, 2025

Thank you @ebsmothers , updated the change.

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.

Thanks for making this change! Once CI is green this should be good to land

@joecummings joecummings changed the title add a "division by zero" check in chunked loss handling in kd_losses.py Add a "division by zero" check in chunked loss handling in kd_losses.py Jan 14, 2025
@joecummings joecummings merged commit 97e857f into pytorch:main Jan 14, 2025
17 checks passed
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.

4 participants