Fix train loss to be normal regardless of num_items_in_batch
Author: petil777
Pull request: yes

# What does this PR do?
This PR tries to fix train loss scale bug.

Currently, num_items_in_batch is calculated by sum of labels having no IGNORE_INDEX.
In case labels having some no IGNORE_INDEX, num_items_in_batch will be not None, leading to grad_acc not applied.

(Ex. Any kinds of generative loss training having labels to be trained to remember some format)

#35438 also pointed out that this num_items_in_batch can make train loss weird.
Also, #35203 says grad_norm should be fixed by manipulating num_items_in_batch.

I'll close this PR if there is any better fixing logic or this can affect other side effects.

Please fix train loss scale bug in no time.

+) Current code also makes accelerator backward loss bigger if accumulation step is larger with same global_batch settings.


## 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](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md#create-a-pull-request),
      Pull Request section?
- [X] Was this discussed/approved via a Github issue or the [forum](https://discuss.huggingface.co/)? 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](https://github.com/huggingface/transformers/tree/main/docs), and
      [here are tips on formatting docstrings](https://github.com/huggingface/transformers/tree/main/docs#writing-source-documentation).
- [ ] Did you write any new necessary tests?


## Who can review?

@muellerzr and @SunMarc



Comments:
I think this fix is wrong, you can simply perform some experiments on Llama models with [micro bsz=1, grad accu=4] and [micro bsz=4, grad accu=1]

----

We have already found abnormal loss in Llama3 model using [micro bsz=1, grad accu=4] (correct value is ~1.0)
![image](https://github.com/user-attachments/assets/85add5d0-e3b0-4101-8825-03a188d1613e)


> I think this fix is wrong, you can simply perform some experiments on Llama models with [micro bsz=1, grad accu=4] and [micro bsz=4, grad accu=1]
> 
> We have already found abnormal loss in Llama3 model using [micro bsz=1, grad accu=4] (correct value is ~1.0) ![image](https://private-user-images.githubusercontent.com/16256802/400654676-85add5d0-e3b0-4101-8825-03a188d1613e.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MzYyNDAwMDcsIm5iZiI6MTczNjIzOTcwNywicGF0aCI6Ii8xNjI1NjgwMi80MDA2NTQ2NzYtODVhZGQ1ZDAtZTNiMC00MTAxLTg4MjUtMDNhMTg4ZDE2MTNlLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAxMDclMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMTA3VDA4NDgyN1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTNiNjQ4NjZhMzFlZGFlYTA4Mzc2Yjg5YTM5NzA3ZThkZTFjZGI5ODhhMWRlMGI4ODQ2OTJhZjkzZDc2NzA2YjQmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.r36FKrcq0kpjNK8gZx0lqAvhXIzsGwQwU-cZnJTK32w)

Thanks for the reply. Due to lack of resource, I couldn't fully experiment (Still hard to experiment...). Seems like the code snippet in #35438 may have loss error as you pasted.(Which is kind of default training code, no need to set labels IGNORE_INDEX selectively).
Since all of them are affected by num_items_in_batch, as #35438 suggested, if we carefully handle num_items_in_batch and use that value to adjust the right value when calculating loss/grad (Ex. loss / grad_acc * num_items_in_batch), do you think this kind of access can solve this problem?
 

@petil777 No, we didn't use the code from #35438, instead, we performed experiments based this PR (#35527 ). We just want to validate that the code based on this PR is incorrect.
Please be responsible for your code. Results without experimental verification are not convincing. You can choose a smaller model to verify the results (such as TinyLlama).

cc @muellerzr @SunMarc