`if not something` is ambiguous
Author: lsz05
Pull request: no
Labels: name: bug

### System Info

- `transformers` version: 4.22.0
- Platform: Linux-4.19.157-1.20201118.el7.x86_64-x86_64-with-glibc2.17
- Python version: 3.8.3
- Huggingface_hub version: 0.9.1
- PyTorch version (GPU?): 1.10.2+cu111 (True)
- Tensorflow version (GPU?): 2.10.0 (True)
- Flax version (CPU?/GPU?/TPU?): 0.6.0 (gpu)
- Jax version: 0.3.17
- JaxLib version: 0.3.15
- Using GPU in script?: YES
- Using distributed or parallel set-up in script?: NO

GPU device: A100 x 1

### Who can help?

@SaulLu 

### Information

- [X] The official example scripts
- [ ] My own modified scripts

### Tasks

- [X] An officially supported task in the `examples` folder (such as GLUE/SQuAD, ...)
- [ ] My own task or dataset (give details below)

### Reproduction

I run the following code
https://github.com/huggingface/transformers/blob/main/examples/flax/language-modeling/run_mlm_flax.py#L296
and an error occurs here:
https://github.com/huggingface/transformers/blob/2c8b508ccabea6638aa463a137852ff3b64be036/src/transformers/tokenization_utils_base.py#L2907

My `required_input` is a tensor (in jax), sometimes I get error `ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()`.

### Expected behavior

https://github.com/huggingface/transformers/blob/2c8b508ccabea6638aa463a137852ff3b64be036/src/transformers/tokenization_utils_base.py#L2907

I suggest to use `if required_input is None` instead of `if not required_input` to avoid error, as the latter one is ambiguous.

Comments:
Thank you for the issue! It's good to know the problems encountered. :hugs: 

On the other hand, I'm not sure the case we want to catch is if `required_input` is equal to None (rather an empty list and maybe something else..). I don't have a typical case in mind but that would be the first thing we would need to find to solve your problem.

Would you happen to know what input was given to the pad method when you got this error?

I examined what happened here with the following method:

I add two lines 
```python
print(f"{required_input=}")
print(f"{self.model_input_names[0]=}")
```
between line 2095 and 2097 to see the values of these two variables, and get the following output
https://github.com/huggingface/transformers/blob/2c8b508ccabea6638aa463a137852ff3b64be036/src/transformers/tokenization_utils_base.py#L2905-L2910

OUTPUT:
```python
required_input=DeviceArray([[[   1, 3091,  459, ...,    3,    3,    3],
              [   1, 1175,   60, ...,    3,    3,    3],
              [   1, 1191,   90, ...,    3,    3,    3],
              ...,
              [   1, 1433,   60, ...,    3,    3,    3],
              [   1,  511,  292, ...,    3,    3,    3],
              [   1,  442,  318, ...,    3,    3,    3]]], dtype=int32)
self.model_input_names[0]='input_ids'
```

Therefore, I believe `required_input` is typically a list or a tensor. If we use `if not required_input`, sometimes it's possible to be processed as `bool` type.

As you said, if we want to catch if `required_input` is an empty list, why don't we consider judging from its shape?

In the following verifications I would like to show that this line (2907) might not be exactly working as how we want.

list
```python
In [1]: empty = [[], []]  # tokenizer([""] * 2, add_special_tokens=False)

In [2]: not_empty = [[1, 2, 3], [4, 5, 6]]

In [3]: not empty, not not_empty
Out[3]: (False, False)
```

numpy
```python
In [4]: import numpy as np

In [5]: not np.array(empty)
<ipython-input-5-b0dbaf8aec3d>:1: DeprecationWarning: The truth value of an empty array is ambiguous. Returning False, but in future this will result in an error. Use `array.size > 0` to check that an array is not empty.
  not np.array(empty)
Out[5]: True

In [6]: not np.array(not_empty)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [6], in <cell line: 1>()
----> 1 not np.array(not_empty)

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
```

pt
```python
In [8]: import torch

In [12]: not torch.Tensor(empty)
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Input In [12], in <cell line: 1>()
----> 1 not torch.Tensor(empty)

RuntimeError: Boolean value of Tensor with no values is ambiguous

In [13]: not torch.Tensor(not_empty)
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Input In [13], in <cell line: 1>()
----> 1 not torch.Tensor(not_empty)

RuntimeError: Boolean value of Tensor with more than one value is ambiguous
```

jax
```python
In [14]: import jax.numpy as jnp

In [15]: not jnp.array(empty)
Out[15]: True

In [16]: not jnp.array(not_empty)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [16], in <cell line: 1>()
----> 1 not jnp.array(not_empty)

File /usr/local/lib/python3.8/functools.py:399, in partialmethod._make_unbound_method.<locals>._method(cls_or_self, *args, **keywords)
    397 def _method(cls_or_self, /, *args, **keywords):
    398     keywords = {**self.keywords, **keywords}
--> 399     return self.func(cls_or_self, *self.args, *args, **keywords)

File ~/env/xxx/lib/python3.8/site-packages/jax/_src/device_array.py:43, in _forward_method(attrname, self, fun, *args)
     42 def _forward_method(attrname, self, fun, *args):
---> 43   return fun(getattr(self, attrname), *args)

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
```

As a conclusion, if the goal is to determine whether `required_input` is a list/array/tensor containing nothing, there is a risk causing error. In this case, I suggest to judge from the shape if it is an array/tensor (if it's a list, it's fine, so it might be necessary to get its type first).

If the goal is just to determine whether it's `None` (as `tokenizer(something)` seems not to return `None` in most cases, I think this line doesn't mean to do this), would `if something is not None` be better?

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) are likely to be ignored.

Sorry this slipped through the cracks. Very much in agreeance here @lsz05 and this is one of the reason we usually avoid relying on Python magic bool conversion but test for explicit values. Would you mind making a PR with a fix?

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) are likely to be ignored.

I almost forgot it.
I'll do something asap.

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) are likely to be ignored.