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

👨‍👨‍👧‍👧 GRPO #2565

Merged
merged 66 commits into from
Jan 20, 2025
Merged

👨‍👨‍👧‍👧 GRPO #2565

merged 66 commits into from
Jan 20, 2025

Conversation

qgallouedec
Copy link
Member

@qgallouedec qgallouedec commented Jan 13, 2025

What does this PR do?

from datasets import load_dataset
from peft import LoraConfig
from trl import GRPOConfig, GRPOTrainer

# Load the dataset
dataset = load_dataset("trl-lib/tldr", split="train")

training_args = GRPOConfig(
    output_dir="Qwen2-0.5B-GRPO",
    learning_rate=1e-5,
    logging_steps=10,
    gradient_accumulation_steps=16,
    max_completion_length=128,
)
trainer = GRPOTrainer(
    model="Qwen/Qwen2-0.5B-Instruct",
    reward_model="weqweasdas/RM-Gemma-2B",
    args=training_args,
    train_dataset=dataset,
    peft_config=LoraConfig(task_type="CAUSAL_LM"),
)

trainer.train()

Fixes # (issue)

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a GitHub issue? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines.
  • Did you write any new necessary tests?

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.

@HuggingFaceDocBuilderDev

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.

advantages = (rewards - mean_grouped_rewards) / (std_grouped_rewards + 1e-4)

# x - x.detach() allows for preserving gradients from x
advatages = torch.exp(per_token_logps - per_token_logps.detach()) * advantages.unsqueeze(1)
Copy link
Collaborator

Choose a reason for hiding this comment

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

typo advatages

Copy link
Collaborator

@edbeeching edbeeching left a comment

Choose a reason for hiding this comment

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

Clean implementation, I had to check the KL term as it if different from PPO. But it appears to match the paper.

@@ -9,7 +9,7 @@ concurrency:

jobs:
build:
uses: huggingface/doc-builder/.github/workflows/build_pr_documentation.yml@main
uses: huggingface/doc-builder/.github/workflows/build_pr_documentation.yml@e4fcf608695cf4bddb8c7f4f72aa15fa14110a94
Copy link
Member Author

Choose a reason for hiding this comment

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

temporary pinning to a previous version, the latest one doesn't work

@qgallouedec qgallouedec merged commit 0f5ffad into main Jan 20, 2025
11 of 14 checks passed
@qgallouedec qgallouedec deleted the grpo branch January 20, 2025 18:02
@SonuDixit
Copy link

SonuDixit commented Feb 5, 2025

Hi Team,

  1. Initially KL loss is zero(its minimum), since the ref and initial model are the same. So, the backward_gradient = 0 from this loss

  2. If we update only once, the first loss term (= sum of advantage for all generations) is zero. In this case, the gradient of loss(=prob*(sum_of_advantages)) with respect to the prob is zero. Hence, all the backward gradients should be zero for SGD(without weight decay) optimisation.

From 1,2 above, the backward gradient is zero, if we update policy only once. The network may not update if we use simple optimiser like SGD.

Correction -
First objective term $l= \sum_i p_i a_i$ , where $\sum_i a_i = 0$ and $p_i = \frac{exp(x_i)}{\sum_j exp(x_j)}$ is the softmax output.
$\frac{\partial l}{\partial x_1} = p_1 (a_1- \sum_i a_i p_i) = p_1(a_1 - l)$ where I have used $\frac{\partial p_i}{\partial x_1} = p_i(\mathbb{1}{(i = 1)}-p_1)$
Now, even if the loss term is zero, one such case all $p_i$ are equal, we still have the gradient. We should be able to train with SGD.

@XiaofengZHOU
Copy link

Hi Team,

  1. Initially KL loss is zero(its minimum), since the ref and initial model are the same. So, the backward_gradient = 0 from this loss
  2. If we update only once, the first loss term (= sum of advantage for all generations) is zero. In this case, the gradient of loss(=prob*(sum_of_advantages)) with respect to the prob is zero. Hence, all the backward gradients should be zero for SGD(without weight decay) optimisation.

From 1,2 above, the backward gradient is zero, if we update policy only once. The network may not update if we use simple optimiser like SGD.

I have the same question

@qgallouedec qgallouedec mentioned this pull request Feb 12, 2025
5 tasks
@Natyren Natyren mentioned this pull request Feb 14, 2025
5 tasks
@zachluo
Copy link

zachluo commented Feb 21, 2025

@SonuDixit

  1. Initially KL loss is zero(its minimum), since the ref and initial model are the same. But after that, the initial policy model will be updated (let's assume the loss is not zero) and the reference model (initialized from SFT model) will not be updated at all.
  2. the first loss term is not zero as pi / pi_old = 1 or torch.exp(per_token_logps - per_token_logps.detach()) = 1. Also, advantage is not zero neither so that per_token_loss = -torch.exp(per_token_logps - per_token_logps.detach()) * advantages.unsqueeze(1) will not become zero.

@zachluo
Copy link

zachluo commented Feb 21, 2025

The policy model only has a single update following each exploration stage.

Are we going to implement the case that the policy model has multiple updates following each exploration stage?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants