Loss should be averaged over all samples instead of tokens
- Dominant language
- Python
- Stars
- 4.8k
- Forks
- 487
- PR merge metrics
- No merged PRs in 30d
Description
### 🐛 Describe the bug
Would it be more reasonable to calculate the average loss over the batch dim, instead of over all tokens? Now it seems that sequences of different lengths in a mini-batch are affecting each other, but I guess samples in a batch should be independent.
https://github.com/CarperAI/trlx/blob/07c962e13cbf91509f35c1a67c368393eac2333e/trlx/models/modeling_ppo.py#L195
A possible solution might be:
```py
vf_loss = 0.5 * (torch.sum(torch.max(vf_loss1, vf_loss2) * mask, dim=1) / mask.sum(dim=1)).mean()
```
Please correct me if I'm wrong.
### Which trlX version are you using?
trlx==0.6.0
### Additional system and package information
Python 3.9.16, transformers==4.28.1, Linux
Contributor guide
Research direction
Start at trlx/models/modeling_ppo.py around line 195 and inspect how the value loss is reduced with the mask. Compare the current token-level averaging with the proposed per-sample reduction, then verify that sequences of different lengths do not affect one another and that the final loss is averaged across batch samples.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python, pytorch
- Domain
- machine-learning
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100