Could `compute_loss` in Trainer double calculate the loss of the model?
Author: minhtriet
Pull request: no

### System Info

```
- `adapter-transformers` version: 3.2.1    
- Platform: Windows-10-10.0.19045-SP0      
- Python version: 3.10.9                   
- Huggingface_hub version: 0.14.1          
- PyTorch version (GPU?): 2.0.1+cpu (False)
- Tensorflow version (GPU?): 2.12.0 (False)
- Flax version (CPU?/GPU?/TPU?): not installed (NA)
- Jax version: not installed
- JaxLib version: not installed
- Using GPU in script?: Yes
- Using distributed or parallel set-up in script?: Yes
```
Code is run in a remote server

### Who can help?

@sgugger 

### Information

- [X] The official example scripts
- [ ] My own modified scripts

### Tasks

- [ ] An officially supported task in the `examples` folder (such as GLUE/SQuAD, ...)
- [X] My own task or dataset (give details below)

### Reproduction

This is the code to subclass `Trainer`
```python
class CustomTrainer(Trainer):
    def compute_loss(self, model, inputs, return_outputs=False):
        labels = inputs.get("labels")
        # forward pass
        outputs = model(**inputs)
        logits = outputs.get("logits")
        # compute custom loss (suppose one has 3 labels with different weights)
        loss_fct = nn.CrossEntropyLoss(weight=torch.tensor([1.0, 2.0, 3.0], device=model.device))
        loss = loss_fct(logits.view(-1, self.model.config.num_labels), labels.view(-1))
        return (loss, outputs) if return_outputs else loss
```

### Expected behavior

However, should `labels = inputs.get("labels")` becomes `labels = inputs.pop("labels")` because in HuggingFace model, as long as the `labels` key is in `inputs`, the model will calculate the loss separately. See this example from `RobertaForSequenceClassification`.

```python
        if labels is not None:
            if self.config.problem_type is None:
                if self.num_labels == 1:
                    self.config.problem_type = "regression"
                elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
                    self.config.problem_type = "single_label_classification"
                else:
                    self.config.problem_type = "multi_label_classification"

            if self.config.problem_type == "regression":
                loss_fct = MSELoss()
                if self.num_labels == 1:
                    loss = loss_fct(logits.squeeze(), labels.squeeze())
                else:
                    loss = loss_fct(logits, labels)
            elif self.config.problem_type == "single_label_classification":
                loss_fct = CrossEntropyLoss()
                loss = loss_fct(logits.view(-1, self.num_labels), labels.view(-1))
            elif self.config.problem_type == "multi_label_classification":
                loss_fct = BCEWithLogitsLoss()
                loss = loss_fct(logits, labels)
```

Which part of the `trainer` overwrite the code above?


Comments:
Indeed, do you want to open a PR with a fix?

How do you think we should solve it. A quick and dirty way I am using is to pop() the labels instead of get().

> On 16. Aug 2023, at 22:40, Sylvain Gugger ***@***.***> wrote:
> 
> ﻿
> Indeed, do you want to open a PR with a fix?
> 
> —
> Reply to this email directly, view it on GitHub, or unsubscribe.
> You are receiving this because you authored the thread.


The doc example should use pop instead of get, yes.