unsupported operand type error in tokenizer.batch_encode_plus
Author: Guitaricet
Pull request: no
Labels: name: wontfix

## 🐛 Bug

<!-- Important information -->

Model I am using (Bert, XLNet....): any model

Language I am using the model on (English, Chinese....): any language

The problem arise when using: tokenizer object

The tasks I am working on is: my own tasks

## To Reproduce

Steps to reproduce the behavior:
use `tokenizer.batch_encode_plus(batch_of_strings, return_attention_masks=True)`

```python
tokenizer = AutoTokenizer.from_pretrained('bert-base-uncased')
tokenizer.batch_encode_plus(['this text is longer than the next', 'short text'], return_attention_masks=True)
```

```python
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-31-0f744584ae65> in <module>
----> 1 tokenizer.batch_encode_plus(['this text is longer than the next', 'short text'], return_attention_masks=True)

/opt/conda/lib/python3.6/site-packages/transformers/tokenization_utils.py in batch_encode_plus(self, batch_text_or_text_pairs, add_special_tokens, max_length, stride, truncation_strategy, return_tensors, return_input_lengths, return_attention_masks, **kwargs)
    971         if return_attention_masks:
    972             if is_tf_available():
--> 973                 batch_outputs['attention_mask'] = tf.abs(batch_outputs['attention_mask'] - 1)
    974             else:
    975                 batch_outputs['attention_mask'] = torch.abs(batch_outputs['attention_mask'] - 1)

TypeError: unsupported operand type(s) for -: 'list' and 'int'
```

## Expected behavior

tokenizer.batch_encode_plus does not crash and returns valid attention masks

## Environment

* OS: Ubuntu 18.04
* Python version: Python 3.6
* PyTorch version: 1.3.0
* PyTorch Transformers version (or branch): 2.3.0
* Using GPU ? No
* Distributed or parallel setup ? No
* Any other relevant information:

## Additional context

The problem is in the lines

```python
batch_outputs['attention_mask'] = tf.abs(batch_outputs['attention_mask'] - 1)
```
and
```python
batch_outputs['attention_mask'] = torch.abs(batch_outputs['attention_mask'] - 1)
```

where we assume that batch_outputs['attention_mask'] is a vectorized object, however it is just a list of lists

```python
batch_outputs["attention_mask"] = [[0] * len(v) for v in batch_outputs["input_ids"]]
```



Comments:
I am fixing this issue and just understood that `return_attention_masks` is supposed to work only if  `return_tensors is not None`. But it is not mentioned in the docstring nor raises an error. Also, in the case of `is_tf_available` and `return_tensors == 'pt'` current code would return tensorflow maks.
I'd suggest to increase the identation of this block

```python
# encoder_attention_mask requires 1 for real token, 0 for padding, just invert value
if return_attention_masks:
    if is_tf_available():
        batch_outputs["attention_mask"] = tf.abs(batch_outputs["attention_mask"] - 1)
    else:
        batch_outputs["attention_mask"] = torch.abs(batch_outputs["attention_mask"] - 1)
```

and to raise ValueError if `return_attention_masks` and  `return_tensors is None`

This [PR](https://github.com/huggingface/transformers/pull/2552) is my proposed fix of the issue.


It seems that tf2.0 can trigger `is_tf_available()`. I've documented the effects of tf version on this functionality [here](https://colab.research.google.com/drive/1a4qmiiZpPXu4mhscJkN_Q0tbd_-HR4jY)

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.