ROBERTa model wrong padding for token_type_ids field if return_tensors=True
Author: AlexanderKUA
Pull request: no
Labels: name: wontfix

## 🐛 Bug

<!-- Important information -->

Model I am using ROBERTa
ROBERTa model wrong padding for token_type_ids field if return_tensors=True.

Language I am using the model on English:

The problem arise when using:
* [ ] the official example scripts: (give details)
* [* ] my own modified scripts: (give details)

The tasks I am working on is:
* [ ] an official GLUE/SQUaD task: (give the name)
* [ *] my own task or dataset: (give details)

## To Reproduce

Please run following code
```
from transformers import pipeline, AutoModel, AutoTokenizer
import torch

model_name = 'roberta-base'
tokenizer = AutoTokenizer.from_pretrained(model_name)
corpus = ['this is a test', 'this is another test example', 'one']

toks = tokenizer.batch_encode_plus(corpus, add_special_tokens=True, max_length=128)
print(toks)
encoded = model(**{k:v.cuda() for k, v in toks.items()}) #crash will be here
```


Steps to reproduce the behavior:

1. Run code.

<!-- If you have a code sample, error messages, stack traces, please provide it here as well. -->
```
RuntimeError                              Traceback (most recent call last)
<ipython-input-14-7ba1420d7b7f> in <module>
----> 1 encoded = model(**{k:v.cuda() for k, v in toks.items()})

~/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    539             result = self._slow_forward(*input, **kwargs)
    540         else:
--> 541             result = self.forward(*input, **kwargs)
    542         for hook in self._forward_hooks.values():
    543             hook_result = hook(self, input, result)

~/anaconda3/lib/python3.6/site-packages/transformers/modeling_bert.py in forward(self, input_ids, attention_mask, token_type_ids, position_ids, head_mask, inputs_embeds, encoder_hidden_states, encoder_attention_mask)
    733             head_mask = [None] * self.config.num_hidden_layers
    734 
--> 735         embedding_output = self.embeddings(input_ids=input_ids, position_ids=position_ids, token_type_ids=token_type_ids, inputs_embeds=inputs_embeds)
    736         encoder_outputs = self.encoder(embedding_output,
    737                                        attention_mask=extended_attention_mask,

~/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    539             result = self._slow_forward(*input, **kwargs)
    540         else:
--> 541             result = self.forward(*input, **kwargs)
    542         for hook in self._forward_hooks.values():
    543             hook_result = hook(self, input, result)

~/anaconda3/lib/python3.6/site-packages/transformers/modeling_roberta.py in forward(self, input_ids, token_type_ids, position_ids, inputs_embeds)
     68                                                       token_type_ids=token_type_ids,
     69                                                       position_ids=position_ids,
---> 70                                                       inputs_embeds=inputs_embeds)
     71 
     72 

~/anaconda3/lib/python3.6/site-packages/transformers/modeling_bert.py in forward(self, input_ids, token_type_ids, position_ids, inputs_embeds)
    188         token_type_embeddings = self.token_type_embeddings(token_type_ids)
    189 
--> 190         embeddings = inputs_embeds + position_embeddings + token_type_embeddings
    191         embeddings = self.LayerNorm(embeddings)
    192         embeddings = self.dropout(embeddings)

RuntimeError: CUDA error: device-side assert triggered
```
## Expected behavior

<!-- A clear and concise description of what you expected to happen. -->
Crash happens because to convert lists to tensors it makes padding with value 1.
`padded_value = [v + [self.pad_token_id if key == 'input_ids' else 1] * (max_seq_len - len(v)) for v in padded_value]` 
It's probably wrong strategy for BERT like models for field `token_type_ids` where 1 means next sentence token. 

It might be wrong logic for attention_mask also because it should be 0 for non meaningful tokens. You should use return_attention_masks which is not enabled by default and also crashes on my machine.

```
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-3-ede112fd760a> in <module>
      1 corpus = ['this is a test', 'this is another test example', 'one']
----> 2 toks = tokenizer.batch_encode_plus(corpus, add_special_tokens=True, max_length=128, return_attention_masks=True, return_tensors='pt')
      3 toks

~/anaconda3/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)

~/anaconda3/lib/python3.6/site-packages/tensorflow_core/python/util/dispatch.py in wrapper(*args, **kwargs)
    178     """Call target, and fall back on dispatchers if there is a TypeError."""
    179     try:
--> 180       return target(*args, **kwargs)
    181     except (TypeError, ValueError):
    182       # Note: convert_to_eager_tensor currently raises a ValueError, not a

~/anaconda3/lib/python3.6/site-packages/tensorflow_core/python/ops/math_ops.py in abs(x, name)
    273   """
    274   with ops.name_scope(name, "Abs", [x]) as name:
--> 275     x = ops.convert_to_tensor(x, name="x")
    276     if x.dtype.is_complex:
    277       return gen_math_ops.complex_abs(x, Tout=x.dtype.real_dtype, name=name)

~/anaconda3/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py in convert_to_tensor(value, dtype, name, preferred_dtype, dtype_hint)
   1182   preferred_dtype = deprecation.deprecated_argument_lookup(
   1183       "dtype_hint", dtype_hint, "preferred_dtype", preferred_dtype)
-> 1184   return convert_to_tensor_v2(value, dtype, preferred_dtype, name)
   1185 
   1186 

~/anaconda3/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py in convert_to_tensor_v2(value, dtype, dtype_hint, name)
   1240       name=name,
   1241       preferred_dtype=dtype_hint,
-> 1242       as_ref=False)
   1243 
   1244 

~/anaconda3/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py in internal_convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, ctx, accept_composite_tensors)
   1294 
   1295     if ret is None:
-> 1296       ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
   1297 
   1298     if ret is NotImplemented:

~/anaconda3/lib/python3.6/site-packages/tensorflow_core/python/framework/constant_op.py in _constant_tensor_conversion_function(v, dtype, name, as_ref)
    284                                          as_ref=False):
    285   _ = as_ref
--> 286   return constant(v, dtype=dtype, name=name)
    287 
    288 

~/anaconda3/lib/python3.6/site-packages/tensorflow_core/python/framework/constant_op.py in constant(value, dtype, shape, name)
    225   """
    226   return _constant_impl(value, dtype, shape, name, verify_shape=False,
--> 227                         allow_broadcast=True)
    228 
    229 

~/anaconda3/lib/python3.6/site-packages/tensorflow_core/python/framework/constant_op.py in _constant_impl(value, dtype, shape, name, verify_shape, allow_broadcast)
    233   ctx = context.context()
    234   if ctx.executing_eagerly():
--> 235     t = convert_to_eager_tensor(value, ctx, dtype)
    236     if shape is None:
    237       return t

~/anaconda3/lib/python3.6/site-packages/tensorflow_core/python/framework/constant_op.py in convert_to_eager_tensor(value, ctx, dtype)
     94       dtype = dtypes.as_dtype(dtype).as_datatype_enum
     95   ctx.ensure_initialized()
---> 96   return ops.EagerTensor(value, ctx.device_name, dtype)
     97 
     98 

ValueError: Attempt to convert a value (tensor([[-1, -1, -1, -1, -1, -1,  0],
        [-1, -1, -1, -1, -1, -1, -1],
        [-1, -1, -1,  0,  0,  0,  0]])) with an unsupported type (<class 'torch.Tensor'>) to a Tensor.
```

## Environment

* OS: Ubuntu 18.04
* Python version: Python 3.6.5 :: Anaconda, Inc.
* PyTorch version: 1.3.1
* PyTorch Transformers version (or branch): 2.3.0
* Using GPU ? Yes
* Distributed or parallel setup ? No
* Any other relevant information:

## Additional context

<!-- Add any other context about the problem here. -->


Comments:
I've been able to fix the `return_attention_masks` error manually defining 
`tokenization_utils.is_tf_available = lambda: False`
It seems that tf2.0 can enable `_tf_available` in src/transformers/file_utils.py,
which triggers the problematic branch (second stack in the second trace)

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.