Fix tensor device mismatch in OPT model
Author: mzmssg
Pull request: yes
Labels: name: Good First Issue

# 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.
-->
It's a straightforward fix on opt model when using cuda device


## 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?

@younesbelkada 


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

Error msg before this PR:
![image](https://user-images.githubusercontent.com/11887940/177463291-8b181f91-7c12-43bf-9628-b604d200eaaf.png)


Hi @mzmssg ! thanks for proposing this fix ;) !
I am wondering why the gpu tests are not failing in our case, could you share with us your PyTorch + cudatoolkit version? Sorry fo getting back late on this

> Hi @mzmssg ! thanks for proposing this fix ;) ! I am wondering why the gpu tests are not failing in our case, could you share with us your PyTorch + cudatoolkit version? Sorry fo getting back late on this

Hi @younesbelkada, I tried pytorch 1.6 and 1.12 on cuda10, 1.12 works fine while 1.6 gives errors. I think that's why it pass the test on your end.

And I found that even on pytorch 1.12 the behavior is a bit strange, it allows max operations on cuda tensor and cpu **scalar** but not on cuda tensor and cpu 1-d tensor. So I think it's better to set the device explicitly, your opinion?

Below are my test:
```
torch==1.6

>>> aa = torch.rand([2, 2]).cuda()
>>> bb = torch.rand([])
>>> torch.max(aa, bb)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: iter.device(arg).is_cuda() INTERNAL ASSERT FAILED at "/pytorch/aten/src/ATen/native/cuda/Loops.cuh":61, please report a bug to PyTorch.
>>> cc = torch.rand([1])
>>> torch.max(aa, cc)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!


torch==1.12

>>> aa = torch.rand([2, 2]).cuda()
>>> bb = torch.rand([])
>>> torch.max(aa, bb)
tensor([[0.9149, 0.9149],
        [0.9149, 0.9149]], device='cuda:0')
>>> cc = torch.rand([1])
>>> torch.max(aa, cc)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!
```