fix bug in group_texts function, that was inserting short batches
Author: bodasadallah
Pull request: yes

# What does this PR do?

<!--
Congratulations! You've made it this far! You're not quite done yet though.

Once merged, your PR is going to appear in the release notes with the title you set, so make sure it's a great title that fully reflects the extent of your awesome contribution.

Then, please replace this with a description of the change and which issue is fixed (if applicable). Please also include relevant motivation and context. List any dependencies (if any) that are required for this change.

Once you're done, someone will review your PR shortly (see the section "Who can review?" below to tag some potential reviewers). They may suggest changes to make the code even better. If no one reviewed your PR after a week has passed, don't hesitate to post a new comment @-mentioning the same persons---sometimes notifications get lost.
-->

<!-- Remove if not applicable -->

Fixes #23424


## 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#start-contributing-pull-requests),
      Pull Request section?
- [ ] 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?

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.

<!-- Your PR will be replied to more quickly if you can figure out the right person to tag with @

 If you know how to use git blame, that is the easiest way, otherwise, here is a rough guide of **who to tag**.
 Please tag fewer than 3 people.

Models:

- text models: @ArthurZucker and @younesbelkada
- vision models: @amyeroberts
- speech models: @sanchit-gandhi
- graph models: @clefourrier

Library:

- flax: @sanchit-gandhi
- generate: @gante
- pipelines: @Narsil
- tensorflow: @gante and @Rocketknight1
- tokenizers: @ArthurZucker
- trainer: @sgugger

Integrations:

- deepspeed: HF Trainer: @stas00, Accelerate: @pacman100
- ray/raytune: @richardliaw, @amogkam

Documentation: @sgugger, @stevhliu and @MKhalusova

HF projects:

- accelerate: [different repo](https://github.com/huggingface/accelerate)
- datasets: [different repo](https://github.com/huggingface/datasets)
- diffusers: [different repo](https://github.com/huggingface/diffusers)
- rust tokenizers: [different repo](https://github.com/huggingface/tokenizers)

Maintained examples (not research project or legacy):

- Flax: @sanchit-gandhi
- PyTorch: @sgugger
- TensorFlow: @Rocketknight1

 -->
@sgugger @ArthurZucker  

Comments:
_The documentation is not available anymore as the PR was closed or merged._

You are right, my bad!

What about if we made it like this:
```python 
 k: [t[i : i + block_size] for i in range(0, total_length - block_size + 1, block_size)]
```
this way, we will not drop an entire batch when `total_length >= block_size` , and at the same time we will return an empty batch, in the case where `total_length < block_size`.

the issue with the code now is that it allows for some samples to have `sequence_length < block_size`, which throws an error when the `data_collator` tries to convert the batches into tensors. 

`batch[k] = torch.tensor([f[k] for f in features])
ValueError: expected sequence of length 128 at dim 1 (got 94)`

Won't the empty block cause you the same error though?

The empty entries are removed and aren't there anymore after the mapping with `group_texts` happens. Even though I don't know why the `map` function removes empty entries. 

I tried to replicate it here: 

```python 
def gen():
    yield {"id": [1,2,3]}
    yield {"id": [1]}
ds = datasets.Dataset.from_generator(gen)
def remove_short(example):
    if len(example['id']) < 2:
        example['id'] =  []
    return example
c = ds.map(remove_short)
for cc in c:
    print (cc)
```
but got the output: 

```
{'id': [1, 2, 3]}                                                                                                                                                
{'id': []} 
```
I don't know why does it removes the empty batch in the training script, and not here. 



We can also just remove the test which is how the script was originally written. The problem is that it then caused issues with users having a small dataset (see #12438).

So all in all there is always going to be one group of users not happy since there is no good solution. This script is primarily intended for training, so it just shouldn't be executed on a small dataset :man_shrugging: 

This will only fail when the dataset has only one batch, and the total_length  < block_size.  But I don't think that's a valid case. So, I think we should just adjust for the normal case where we have many batches, but sometimes one batch would be too short that we need to exclude it,  instead of returning a batch with shorter length, that will cause an error. 

True. So let's go with removing the test at line 496 (and unindent `total_length = (total_length // block_size) * block_size`).

Great! 
Should I create a new PR, or add a new commit to this one? 
Should I do on for the rest of Language modeling examples? 

This PR is fine, and yes please treat other examples the same way.

yes, I did it pushing now