Add option to normalize loss per target#326
Merged
Muennighoff merged 11 commits intot0loadingfrom Nov 3, 2022
Merged
Conversation
thomasw21
reviewed
Aug 10, 2022
thomasw21
reviewed
Aug 16, 2022
thomasw21
reviewed
Aug 17, 2022
| ) | ||
|
|
||
| if args.norm_target_loss: | ||
| loss_mask = loss_mask.view(-1) |
Member
There was a problem hiding this comment.
There's a fun hack you can do, view have the same storage space as the initial model. so you can probably write something like:
def fast_normalize(loss_mask: torch.Tensor):
"""
Turn loss_mask from [0,0,0,1,1,0,0,1,0,0,1,1,1] > [0,0,0,0.5,0.5,0,0,1,0,0,0.3,0.3,0.3]
"""
flatten_view = loss_mask.view(-1)
_, inverse_indices, counts = torch.unique_consecutive(loss_mask, return_inverse=True, return_counts=True)
counts = torch.gather(dim=0, index=inverse_indices, input=counts)
flatten_view.div_(counts)
return loss_mask
Member
There was a problem hiding this comment.
you could also clone before doing this operation so that you actually don't make fast_normalize a in-place operation.
Collaborator
Author
There was a problem hiding this comment.
Why is
def fast_normalize(loss_mask: torch.Tensor):
"""
Turn loss_mask from [0,0,0,1,1,0,0,1,0,0,1,1,1] > [0,0,0,0.5,0.5,0,0,1,0,0,0.3,0.3,0.3]
"""
flatten_view = loss_mask.view(-1)
_, inverse_indices, counts = torch.unique_consecutive(loss_mask, return_inverse=True, return_counts=True)
counts = torch.gather(dim=0, index=inverse_indices, input=counts)
flatten_view.div_(counts)
return loss_mask
better than
def fast_normalize(loss_mask: torch.Tensor):
"""
Turn loss_mask from [0,0,0,1,1,0,0,1,0,0,1,1,1] > [0,0,0,0.5,0.5,0,0,1,0,0,0.3,0.3,0.3]
"""
_, inverse_indices, counts = torch.unique_consecutive(loss_mask, return_inverse=True, return_counts=True)
counts = torch.gather(dim=0, index=inverse_indices, input=counts)
return loss_mask / counts
?
Member
There was a problem hiding this comment.
Does the latter work if loss_mask is not 1D?
thomasw21
reviewed
Aug 17, 2022
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.