`np.ndarray` not supported anymore for optional arguments at inference for Bert
Author: RobertSamoilescu
Pull request: no

## Environment info
<!-- You can run the command `transformers-cli env` and copy-and-paste its output below.
     Don't forget to fill out the missing fields in that output! -->

- `transformers` version: 4.12.3 (maybe even earlier)
- Python version: >= 3.7
- Tensorflow version (GPU?): >= 2.2

### Who can help
 @ydshieh @patrickvonplaten

## Information

Model I am using: Bert (probably the issue arises for other models too)

The error arises when using when arguments such as `attention_mask` are of type `np.ndarray`.

The conversion to `list` of the `attention_mask` shape fails here:
https://github.com/huggingface/transformers/blob/f5af87361718be29a1d3ddb2d8ef23f85b1c70c3/src/transformers/models/bert/modeling_tf_bert.py#L798-L803 

The method `shape_list` expects a `tf.Tensor` although it should support both `tf.Tensor` and `np.ndarray`.
https://github.com/huggingface/transformers/blob/f5af87361718be29a1d3ddb2d8ef23f85b1c70c3/src/transformers/modeling_tf_utils.py#L1805

The casting fails at the following line:
https://github.com/huggingface/transformers/blob/f5af87361718be29a1d3ddb2d8ef23f85b1c70c3/src/transformers/modeling_tf_utils.py#L1820

as regular tuples (as is the case for `np.array` shape) do not have `as_list`, and work only with `tf.Tensor`.

Note that for version [4.6.1](https://github.com/huggingface/transformers/commit/fb27b276e7babc2249abbf79e6efb23b9611da10), both `tf.Tensor` and `np.ndarray` works as expected.

## To reproduce
Run [this](https://github.com/SeldonIO/alibi/blob/master/examples/integrated_gradients_transformers.ipynb) notebook. The following cells: 

```python

def get_embeddings(X_train, model, batch_size=50):

    args = X_train['input_ids']
    kwargs = {k:v for k, v in  X_train.items() if k != 'input_ids'}
    
    dataset = tf.data.Dataset.from_tensor_slices((args, kwargs)).batch(batch_size)
    dataset = dataset.as_numpy_iterator()
    
    embbedings = []
    for X_batch in dataset:
        args_b, kwargs_b = X_batch
        batch_embeddings = model(args_b, **kwargs_b)
        embbedings.append(batch_embeddings.last_hidden_state.numpy())

    return np.concatenate(embbedings, axis=0)
```

```python
train_embbedings = get_embeddings(X_train, 
                                  modelBert, 
                                  batch_size=100)

test_embbedings = get_embeddings(X_test,
                                 modelBert, 
                                 batch_size=100)
```

The error can be fixed by converting the `kwargs_b` values (i.e., `attention_mask`) to `tf.Tensor` before calling `forward` on the model:
```python
kwargs_b = {k: tf.constant(v) for k, v in kwargs_b.items()}
```

## Expected behavior

<!-- A clear and concise description of what you would expect to happen. -->
The function call should be working with `np.ndarray` as well as with `tf.Tensor` as the typing suggests:

https://github.com/huggingface/transformers/blob/f5af87361718be29a1d3ddb2d8ef23f85b1c70c3/src/transformers/models/bert/modeling_tf_bert.py#L728-L732



Comments:
Link to `alibi` issue: https://github.com/SeldonIO/alibi/issues/527

@RobertSamoilescu Thank you for this, it's definitely a bug. I think the best solution is probably:

1) Rewrite `shape_list` to support `np.ndarray` as well as `tf.Tensor`.
2) Add a test with Numpy inputs for our TF models to ensure this doesn't happen again.

Would you be interested in trying to submit that PR? If not, we can try to get to it ourselves, but things are quite busy right now!

@Rocketknight1, I can submit a PR. I will start working on it later this week or most probably at the beginning of next week.

@RobertSamoilescu That's great, thank you! If you encounter any problems, or you find you don't have time to work on it after all, let me know here and I'll see what I can do.

@Rocketknight1, unfortunately, at the moment I do not have the time to do the PR. 

@RobertSamoilescu No problem! We'll see if anyone can submit a PR, and try to get to it ourselves if not

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/master/CONTRIBUTING.md) are likely to be ignored.

(@Rocketknight1 assigning to me :) )

@RobertSamoilescu fixed with #15074