generated from fastai/nbdev_template
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
💾 Reduce memory peak in GRPO by adding max_prompt_length
and loop usage in logp computation
#2598
Merged
Conversation
This file contains 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
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
qgallouedec
commented
Jan 21, 2025
Comment on lines
218
to
+232
def get_per_token_logps(model, input_ids): | ||
logits = model(input_ids).logits | ||
logits = torch.roll(logits, shifts=1, dims=1) # Shape (B*G, L) | ||
per_token_logps = torch.gather(logits.log_softmax(-1), dim=2, index=input_ids.unsqueeze(2)).squeeze(2) | ||
return per_token_logps | ||
logits = model(input_ids).logits # (B, L, V) | ||
logits = logits[:, :-1, :] # (B, L-1, V), exclude the last logit: it corresponds to the next token pred | ||
input_ids = input_ids[:, 1:] # (B, L-1), exclude the first input ID since we don't have logits for it | ||
# Compute the log probabilities for the input tokens. Use a loop to reduce memory peak. | ||
per_token_logps = [] | ||
for logits_row, input_ids_row in zip(logits, input_ids): | ||
log_probs = logits_row.log_softmax(dim=-1) | ||
token_log_prob = torch.gather(log_probs, dim=1, index=input_ids_row.unsqueeze(1)).squeeze(1) | ||
per_token_logps.append(token_log_prob) | ||
return torch.stack(per_token_logps) | ||
|
||
per_token_logps = get_per_token_logps(model, prompt_completion_ids) | ||
per_token_logps = per_token_logps[:, prompt_length:] # get rid of the prompt | ||
# Get rid of the prompt (-1 because of the shift done in get_per_token_logps) | ||
per_token_logps = per_token_logps[:, prompt_length - 1 :] |
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.
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
model_name = "Qwen/Qwen2.5-0.5B"
model = AutoModelForCausalLM.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)
prompt = ["The quick brown fox jumps over the lazy dog."]
prompt_completion = ["The quick brown fox jumps over the lazy dog. Nice to meet you!"]
prompt_ids = tokenizer(prompt, return_tensors="pt").input_ids
prompt_completion_ids = tokenizer(prompt_completion, return_tensors="pt").input_ids
prompt_length = prompt_ids.shape[1]
# Old one
def get_per_token_logps(model, input_ids):
logits = model(input_ids).logits
logits = torch.roll(logits, shifts=1, dims=1) # Shape (B*G, L)
per_token_logps = torch.gather(logits.log_softmax(-1), dim=2, index=input_ids.unsqueeze(2)).squeeze(2)
return per_token_logps
per_token_logps1 = get_per_token_logps(model, prompt_completion_ids)
per_token_logps1 = per_token_logps1[:, prompt_length:] # get rid of the prompt
# New one
def get_per_token_logps(model, input_ids):
logits = model(input_ids).logits # (B, L, V)
logits = logits[:, :-1, :] # (B, L-1, V), exclude the last logit: it corresponds to the next token pred
input_ids = input_ids[:, 1:] # (B, L-1), exclude the first input ID since we don't have logits for it
# Compute the log probabilities for the input tokens. Use a loop to reduce memory peak.
per_token_logps = []
for logits_row, input_ids_row in zip(logits, input_ids):
log_probs = logits_row.log_softmax(dim=-1)
token_log_prob = torch.gather(log_probs, dim=1, index=input_ids_row.unsqueeze(1)).squeeze(1)
per_token_logps.append(token_log_prob)
return torch.stack(per_token_logps)
per_token_logps2 = get_per_token_logps(model, prompt_completion_ids)
# Get rid of the prompt (-1 because of the shift done in get_per_token_logps)
per_token_logps2 = per_token_logps2[:, prompt_length - 1 :]
print(torch.allclose(per_token_logps1, per_token_logps2)) # True
qgallouedec
requested review from
kashif,
edbeeching,
lewtun,
plaguss and
August-murr
January 21, 2025 13:59
kashif
approved these changes
Jan 21, 2025
qgallouedec
changed the title
Reduce memory peak in GRPO
💾 Reduce memory peak in GRPO by adding Jan 21, 2025
max_prompt_length
and loop usage in logp computation.
qgallouedec
changed the title
💾 Reduce memory peak in GRPO by adding
💾 Reduce memory peak in GRPO by adding Jan 21, 2025
max_prompt_length
and loop usage in logp computation.max_prompt_length
and loop usage in logp computation
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.
What does this PR do?
Grey is the old one
Not sure why the grad norm don't perfectly match. Numerical noise probably.
Before submitting
Pull Request section?
to it if that's the case.
documentation guidelines.
Who can review?
Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.