# PPO using TRL: optimal strategy for reward calculation?

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fppo-using-trl-optimal-strategy-for-reward-calculation%2F65988%3E%5C
**Category:** Research\
**Created:** [December 15, 2023, 8:52pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fppo-using-trl-optimal-strategy-for-reward-calculation%2F65988 "2023-12-15T20:52:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![BayesRulez](https://avatars.discourse-cdn.com/v4/letter/b/9de053/32.png) [@BayesRulez](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FBayesRulez)%5C
**Post date:** [December 15, 2023, 8:52pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fppo-using-trl-optimal-strategy-for-reward-calculation%2F65988%2F1 "2023-12-15T20:52:23Z")

</div>

Hi everyone,

Something I’ve been wondering recently and I’d value some input.

I’ve been working with the `trl` library to fine-tune various decoder-only LLMs via RLHF. During the PPO loop, I’ll collect the rewards using something like:

```auto
raw_rewards = ppo_trainer.model.compute_reward_score(input_ids, attention_masks)

```

Which will returns the class logits from the previously trained `AutoModelForSequenceClassification` model. We then need to turn these into a reward. I’ve seen different approaches to this, for example taking the first element of the logit (see [here](https://github.com/huggingface/trl/blob/main/examples/scripts/ppo_multi_adapter.py)) or taking the last element of the logit (see [here](https://github.com/huggingface/trl/issues/820)).

Examining the code for the RewardTrainer we can see how the loss function is constructed (line 238) :

```auto
loss = -nn.functional.logsigmoid(rewards_chosen - rewards_rejected).mean()

```

If we denote the chosen and rejected rewards by the tuples (c1, c2) and (r1, r2) then we can write the above as:

\begin{align} \mathcal{L} &= -\frac{1}{2} \left[log \sigma \left(c\_1-r\_1\right) + log \sigma \left(c\_2 - r\_2\right) \right] \\ &= \frac{1}{2} \left[log \left(e^{-r\_1+c\_1} + 1\right) + log \left(e^{-r\_2+c\_2} + 1\right) \right] \end{align}

And see that the loss function is going to force weight updates that should cause _both_ elements of the reward tuple to be driven towards a high score (for a chosen input) or a low score (for a rejected input). (Hence, presumably, why I have seen code which does both.)

So, here’s the first question: should we be indifferent as to which element of the reward logit we hand to the PPO algorithm, or would it be even better to combine them - eg to sum them?

And here’s the second question: the `PPOTrainer` allows us to apply batch-scaling and reward clipping to help stabilise gradients updates for the policy model, but why not just pass the reward logits through a `logsigmoid()` function first (mirroring the loss function)? That, after all, would mirror what the loss function in the `RewardTrainer` is doing.

Are there any theoretical reasons or implementation details that bear upon the above?

---

<div class="post-metadata">

**Author:** ![BayesRulez](https://avatars.discourse-cdn.com/v4/letter/b/9de053/32.png) [@BayesRulez](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FBayesRulez)%5C
**Post date:** [December 20, 2023, 9:17am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fppo-using-trl-optimal-strategy-for-reward-calculation%2F65988%2F2 "2023-12-20T09:17:48Z")

</div>

Cannot edit the post so correcting the typo in the second eqn above:

\frac{1}{2} \left[log \left(e^{-c\_1+r\_1} + 1\right) + log \left(e^{-c\_2+r\_2} + 1\right) \right]
