-
Notifications
You must be signed in to change notification settings - Fork 16
[Memory optm] loss using torch + compile #337
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
22e50d0
loss using torch
294dd29
add long to pass tests
9b6f3b5
Update src/forge/util/ops.py
felipemello1 8723a5a
Update src/forge/util/ops.py
felipemello1 d8adf06
Merge branch 'main' of https://github.com/meta-pytorch/forge into com…
6b065d6
Merge branch 'compile_loss' of https://github.com/felipemello1/forge …
c3a4586
update arg name
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,6 +56,7 @@ def compute_logprobs( | |
) -> torch.Tensor: | ||
""" | ||
Computes the log probabilities of the input tokens given the model logits and temperature. | ||
Always converts inputs to fp32 for numerical stability | ||
|
||
Args: | ||
logits (`torch.Tensor`): | ||
|
@@ -65,10 +66,23 @@ def compute_logprobs( | |
temperature (`float`, *optional*, defaults to 1.0): | ||
The temperature value for scaling logits before computing log probabilities. | ||
|
||
Returns: | ||
logprobs: [batch, seq_len] log probabilities for each token | ||
""" | ||
# Ignore the last token from logits because it predicts the next token (-1) | ||
# And align logits with the input tokens length. | ||
logits = logits[:, -input_ids.size(1) - 1 : -1, :].to(input_ids.device) | ||
scaled_logits = logits / temperature | ||
logprobs = selective_log_softmax(scaled_logits, input_ids) | ||
return logprobs | ||
|
||
# Convert to fp32 for numerical stability | ||
felipemello1 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
scaled_logits_fp32 = scaled_logits.float() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Noob question: what's the dtype for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. float becomes torch.float32 |
||
|
||
# get per-token log probs | ||
batch_size, seq_len, vocab_size = scaled_logits_fp32.shape | ||
log_probs = -F.cross_entropy( | ||
felipemello1 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
scaled_logits_fp32.reshape(-1, vocab_size), | ||
input_ids.reshape(-1), | ||
reduction="none", | ||
) | ||
|
||
return log_probs.reshape(batch_size, seq_len) |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any circumstance under which this command would fail?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cant think of one in our scenario, but if/when this happens, we can fix it