Allow to train dinov2 with different dtypes like bf16
Author: StarCycle
Pull request: yes

I want to train dinov2 with bf16 but I get the following error in https://github.com/huggingface/transformers/blob/bc72b4e2cdcbc80d5f56731f35dbc9c18b4c8de6/src/transformers/models/dinov2/modeling_dinov2.py#L635:

```
RuntimeError: Input type (float) and bias type (c10::BFloat16) should be the same
```

Since the input dtype is torch.float32, the parameter dtype has to be torch.float32...

@LZHgrla and I checked the code of clip vision encoder and found there is an automatic dtype transformation (https://github.com/huggingface/transformers/blob/bc72b4e2cdcbc80d5f56731f35dbc9c18b4c8de6/src/transformers/models/clip/modeling_clip.py#L181-L182).

So I add similar automatic dtype transformation to modeling_dinov2.py.


Comments:
Hi @StarCycle, thanks for this contribution! 

Could you share a small code snippet to show how you're training the model in the PR description and the error? This helps us for anyone coming back to this PR in the future.

The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/transformers/pr_28504). All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

I am using another training framework (xtuner), which is a little complicated. But you can easily reproduce the problem with the following code:

```
import torch
from transformers import AutoImageProcessor, AutoModel
from PIL import Image
import requests

# URL of the image you want to process
url = 'http://images.cocodataset.org/val2017/000000039769.jpg'

# Open the image
image = Image.open(requests.get(url, stream=True).raw)

# Load the image processor and the model
processor = AutoImageProcessor.from_pretrained('facebook/dinov2-base')
model = AutoModel.from_pretrained('facebook/dinov2-base', torch_dtype=torch.bfloat16)

# Prepare the inputs
inputs = processor(images=image, return_tensors="pt")

# Get the model outputs
outputs = model(**inputs)
```
Now there will be an error:
```
[/usr/local/lib/python3.10/dist-packages/transformers/models/dinov2/modeling_dinov2.py](https://localhost:8080/#) in forward(self, pixel_values)
    165                 f" Expected {self.num_channels} but got {num_channels}."
    166             )
--> 167         embeddings = self.projection(pixel_values).flatten(2).transpose(1, 2)
    168         return embeddings
    169 

[/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py](https://localhost:8080/#) in _wrapped_call_impl(self, *args, **kwargs)
   1516             return self._compiled_call_impl(*args, **kwargs)  # type: ignore[misc]
   1517         else:
-> 1518             return self._call_impl(*args, **kwargs)
   1519 
   1520     def _call_impl(self, *args, **kwargs):

[/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py](https://localhost:8080/#) in _call_impl(self, *args, **kwargs)
   1525                 or _global_backward_pre_hooks or _global_backward_hooks
   1526                 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1527             return forward_call(*args, **kwargs)
   1528 
   1529         try:

[/usr/local/lib/python3.10/dist-packages/torch/nn/modules/conv.py](https://localhost:8080/#) in forward(self, input)
    458 
    459     def forward(self, input: Tensor) -> Tensor:
--> 460         return self._conv_forward(input, self.weight, self.bias)
    461 
    462 class Conv3d(_ConvNd):

[/usr/local/lib/python3.10/dist-packages/torch/nn/modules/conv.py](https://localhost:8080/#) in _conv_forward(self, input, weight, bias)
    454                             weight, bias, self.stride,
    455                             _pair(0), self.dilation, self.groups)
--> 456         return F.conv2d(input, weight, bias, self.stride,
    457                         self.padding, self.dilation, self.groups)
    458 

RuntimeError: Input type (float) and bias type (c10::BFloat16) should be the same
```

@StarCycle Thanks for sharing and again for this contribution!