# Help understanding GRPO quick start in docs

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhelp-understanding-grpo-quick-start-in-docs%2F139165%3E%5C
**Category:** Beginners\
**Created:** [February 5, 2025, 4:33pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhelp-understanding-grpo-quick-start-in-docs%2F139165 "2025-02-05T16:33:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![JamesXanda](https://avatars.discourse-cdn.com/v4/letter/j/a5b964/32.png) [@JamesXanda](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FJamesXanda)%5C
**Post date:** [February 5, 2025, 4:33pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhelp-understanding-grpo-quick-start-in-docs%2F139165%2F1 "2025-02-05T16:33:14Z")

</div>

Hi all,

I have been looking at the GRPO training example in the trl docs ([GRPO Trainer](https://huggingface.co/docs/trl/main/en/grpo_trainer)) and am confused about the reward function they define.

In the example they define the reward function to be

```auto
# Define the reward function, which rewards completions that are close to 20 characters
def reward_len(completions, **kwargs):
    return [abs(20 - len(completion)) for completion in completions]

```

To me this suggests that the model is being rewarded for making completions really long since abs(20 - len(completions)) grows without bound as len(completions) grows. The example claims it is training the model to make completion close to 20 characters long.

Can anyone explain what I am missing?

---

<div class="post-metadata">

**Author:** ![junujunu](https://avatars.discourse-cdn.com/v4/letter/j/f9ae1b/32.png) [@junujunu](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fjunujunu)%5C
**Post date:** [February 6, 2025, 3:21am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhelp-understanding-grpo-quick-start-in-docs%2F139165%2F2 "2025-02-06T03:21:24Z")

</div>

I found the same question and raised Issue.

> <https://github.com/huggingface/trl/issues/2771>
>
> https://huggingface.co/docs/trl/main/en/grpo\_trainer
> 
> 
> Am I misunderstanding som…ething about the reward function in the documentation example? 
> 
> Looking at the current example:
> \`\`\`python
> def reward\_len(completions, \*\*kwargs):
> return \[abs(20 - len(completion)) for completion in completions\]
> \`\`\`
> 
> I think this reward function might be working opposite to what we want. Since GRPO tries to maximize the reward, wouldn't this make the model generate text that's far from 20 characters? 
> 
> Shouldn't we use something like:
> \`\`\`python
> def reward\_len(completions, \*\*kwargs):
> return \[-abs(20 - len(completion)) for completion in completions\]
> \`\`\`
> 
> or 
> \`\`\`python
> def reward\_len(completions, \*\*kwargs):
> return \[1 / (1 + abs(20 - len(completion))) for completion in completions\]
> \`\`\`
> 
> so that completions closer to 20 characters get higher rewards?
> 
> Would appreciate if someone could clarify this

---

<div class="post-metadata">

**Author:** ![JamesXanda](https://avatars.discourse-cdn.com/v4/letter/j/a5b964/32.png) [@JamesXanda](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FJamesXanda)%5C
**Post date:** [February 6, 2025, 9:14am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhelp-understanding-grpo-quick-start-in-docs%2F139165%2F3 "2025-02-06T09:14:43Z")

</div>

@junujunu this is great thanks, I thought I was going crazy.
