error when load model
Author: linhui1020
Pull request: no

### System Info

2023-07-19 13:47:33.834732: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT


### Who can help?

_No response_

### Information

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

### Tasks

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

### Reproduction

model = BertForSequenceClassification.from_pretrained('bert-base-uncased',
                                                      num_labels = 2)
model.to(device)
model.load_state_dict(torch.load("my_model.pt", map_location = torch.device("cpu")))

### Expected behavior

When running the code, it shows run time error: _IncompatibleKeys(missing_keys=[], unexpected_keys=['bert.embeddings.position_ids']). The error does not occur yesterday, but appear now. 

Comments:
You will need to drop that key from your state dict, or use the `save_pretrained` and `from_pretrained` method of the library.

I'm having the same issue, except my model is a custom model that includes a transformer model as a part of it so I need to use `torch.load_state_dict` to load the model.

Here's some code to reproduce the issue:

```python
import torch.nn as nn
from transformers import XLMRobertaConfig, XLMRobertaModel


class MyCustomModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.roberta = XLMRobertaModel(
            XLMRobertaConfig.from_pretrained("Unbabel/xlm-roberta-comet-small")
        )
        self.my_thing = nn.Linear(384, 1)  # let's pretend this is way more complicated


def main():
    model = MyCustomModel()

    print(sorted(list(model.state_dict().keys()))[:10])


if __name__ == "__main__":
    main()
```

When running the script with transformers==4.30.0 (which is what I used for training the model) I get the following output:

```
['my_thing.bias', 'my_thing.weight', 'roberta.embeddings.LayerNorm.bias', 'roberta.embeddings.LayerNorm.weight', 'roberta.embeddings.position_embeddings.weight', 'roberta.embeddings.position_ids', 'roberta.embeddings.token_type_embeddings.weight', 'roberta.embeddings.word_embeddings.weight', 'roberta.encoder.layer.0.attention.output.LayerNorm.bias', 'roberta.encoder.layer.0.attention.output.LayerNorm.weight']
```

Note the presence of `roberta.embeddings.position_ids`.

Now when I try to load the model using transformers==4.31.0, it gives me a key error, because the position IDs key seems to have been removed in the new version. Running the same code gives:

```
['my_thing.bias', 'my_thing.weight', 'roberta.embeddings.LayerNorm.bias', 'roberta.embeddings.LayerNorm.weight', 'roberta.embeddings.position_embeddings.weight', 'roberta.embeddings.token_type_embeddings.weight', 'roberta.embeddings.word_embeddings.weight', 'roberta.encoder.layer.0.attention.output.LayerNorm.bias', 'roberta.encoder.layer.0.attention.output.LayerNorm.weight', 'roberta.encoder.layer.0.attention.output.dense.bias']
```

The position IDs key is not there anymore.

Shouldn't this at least be documented as a breaking change in the release notes?

Same issue here

You should use `save_pretrained` and `from_pretained` to save/load your models. There is no breaking changes with those methods, we do not guarantee the same if you choose to save/load your model on your own across different versions of Transformers.

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) are likely to be ignored.