Inversion of a mask in newer pytorch versions
Author: Shashi456
Pull request: no
Labels: name: wontfix

# 🐛 Bug

## Information

Model I am using (Bert, XLNet ...): XLNet

Language I am using the model on (English, Chinese ...): English

The problem arises when using:
* [x] the official example scripts: (give details below)
* [ ] my own modified scripts: (give details below)

The tasks I am working on is:
* [ ] an official GLUE/SQUaD task: (give the name)
* [x] my own task or dataset: (give details below)
Finetuning on downstream task. 
## To reproduce

Steps to reproduce the behavior:

1.Just run the XLNet Model with a newer pytorch version.


<!-- If you have code snippets, error messages, stack traces please provide them here as well.
     Important! Use code tags to correctly format your code. See https://help.github.com/en/github/writing-on-github/creating-and-highlighting-code-blocks#syntax-highlighting
     Do not use screenshots, as they are hard to read and (more importantly) don't allow others to copy-and-paste your code.-->

## Expected behavior

<!-- A clear and concise description of what you would expect to happen. -->
```
RuntimeError: Subtraction, the `-` operator, with a bool tensor is not supported. If you are trying to invert a mask, use the `~` or `logical_not()` operator instead.
```

## Environment info
<!-- You can run the command `python transformers-cli env` and copy-and-paste its output below.
     Don't forget to fill out the missing fields in that output! -->
     
- `transformers` version: 2.6.0
- Platform: Ubuntu
- Python version: 3.6
- PyTorch version (GPU?): 1.4.0
- Tensorflow version (GPU?):
- Using GPU in script?: Yes
- Using distributed or parallel set-up in script?: parallel

## Additional Comments
This change was made in pytorch 1.2.0, check the release notes [here](https://github.com/pytorch/pytorch/releases/tag/v1.2.0).


Comments:
Do you mind giving a reproducible example so that we may debug easily?

I was following this issue because I ran into the same problem. I won't use the code I ran into the problem with here but here is the gist:

```python
from transformers import XLNetForSequenceClassification

model = XLNetForSequenceClassification.from_pretrained("xlnet-base-cased")
inputs = torch.randint(0, 100, (32, 100))    # 100 words in vocab, batch size 32, seq_len = 100
masks = torch.ones(inputs.size(), dtype=torch.bool)    # none of the tokens are padding
labels = torch.randint(0, 2, (32,))    # binary classification

result = model(inputs, attention_mask=masks, labels=labels)
```

Error:

> Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/torch/nn/modules/module.py", line 532, in __call__
    result = self.forward(*input, **kwargs)
  File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/transformers/modeling_xlnet.py", line 1150, in forward
    inputs_embeds=inputs_embeds,
  File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/torch/nn/modules/module.py", line 532, in __call__
    result = self.forward(*input, **kwargs)
  File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/transformers/modeling_xlnet.py", line 778, in forward
    input_mask = 1.0 - attention_mask
  File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/torch/tensor.py", line 394, in __rsub__
    return _C._VariableFunctions.rsub(self, other)
RuntimeError: Subtraction, the `-` operator, with a bool tensor is not supported. If you are trying to invert a mask, use the `~` or `logical_not()` operator instead.

I use OSX Mojave, torch==1.4.0, if it helps.

Edit: by converting the masks to dtype `torch.uint32`, I was able to get it to work, but I'm not sure if masking using an integer mask is the correct way of handling this.

The `torch.bool` was introduced in `torch==1.2.0` but we're looking to accommodate `torch>=1.0.0`, so we have to handle such cases with a `uint` syntax. All model inputs should be `uint`, and not `bool` for that specific reason.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.