Perplexity calculation in the official tutorial is not correct
Author: balaabhijit
Pull request: no

### System Info

```yaml
Pytorch: 2.1.0+cu121
datasets: 2.17.0
transformers: 4.35.2
```

### Who can help?

@ArthurZucker @stevhliu @younesbelkada 

### Information

- [x] The official example scripts
- [ ] My own modified scripts

### Tasks

- [ ] An officially supported task in the `examples` folder (such as GLUE/SQuAD, ...)
- [ ] My own task or dataset (give details below)

### Reproduction

The colab notebook where the problem is found: [Perplexity of Fixed length models](https://huggingface.co/docs/transformers/perplexity). Especially in the following region of the code:

```python
import torch
from tqdm import tqdm

max_length = model.config.n_positions
stride = 512
seq_len = encodings.input_ids.size(1)

nlls = []
prev_end_loc = 0
for begin_loc in tqdm(range(0, seq_len, stride)):
    end_loc = min(begin_loc + max_length, seq_len)
    trg_len = end_loc - prev_end_loc  # may be different from stride on last loop
    input_ids = encodings.input_ids[:, begin_loc:end_loc].to(device)
    target_ids = input_ids.clone()
    # Edit: This target setting is not right. It should be target_ids[:, :-1] = -100 or target_ids[:, :trg_len-1] = -100.
    # I am guessing the goal of this line is to make every other value except the one we are predicting to be zero.
    # Currently, the below line does not add any -100 to the target_ids tensor at all.
    target_ids[:, :-trg_len] = -100
    #Edit: If the above line is not corrected, the following added line will pass everytime which should not happen.
    assert torch.equal(input_ids, target_ids), f"input_ids: {input_ids}\ntarget_ids: {target_ids}"

    with torch.no_grad():
        outputs = model(input_ids, labels=target_ids)

        # loss is calculated using CrossEntropyLoss which averages over valid labels
        # N.B. the model only calculates loss over trg_len - 1 labels, because it internally shifts the labels
        # to the left by 1.
        neg_log_likelihood = outputs.loss

    nlls.append(neg_log_likelihood)

    prev_end_loc = end_loc
    if end_loc == seq_len:
        break

ppl = torch.exp(torch.stack(nlls).mean())
```

Thus the calculated Perplexity values are wrong. Kindly let me know if I am missing something or kindly correct the example.

### Expected behavior

the expected behavior is something like this:
```python
input_ids = tensor([[1,2,3,4,5,6]])
target_ids = tensor([[-100,-100,-100,-100,-100,6]])
```

The current behavior is:
```python
input_ids = tensor([[1,2,3,4,5,6]])
target_ids = tensor([[1,2,3,4,5,6]])
```

Comments:
Hey! I am not sure why this is incorrect, you want to compute the loss on every token that is predicted. If we use your change, we are only predicting the last token that it's only one term of the perplexity. 

By only masking the first token, you end up doing 
```python3
input_ids = tensor([[1,2]])
target_ids = tensor([[-100, 2]])

input_ids = tensor([[1,2,3]])
target_ids = tensor([[-100,-100, 3]])
...
...
...

input_ids = tensor([[1,2,3,4,5,6]])
target_ids = tensor([[-100,-100,-100,-100,-100,6]])
```
but with a single forward pass
![image](https://github.com/huggingface/transformers/assets/48595927/fbf690da-f064-4f41-9bad-5a218f95b823)

feel free to re-open if this answer is not satisfying enough!