Unable to run quantized Bert model
@yd2102 Thanks for reporting the issue.

I was able to reproduce this issue in TF 2.11. Please find the gist [here](https://colab.research.google.com/gist/pjpratik/3dbd22039d8453458cc37addfaea7637/59730_2-11.ipynb).

The issue seems to be resolved in TF nightly version. I was able to run the quantized bert model with out any error. Please find the gist [here](https://colab.research.google.com/gist/pjpratik/3606190d65dd55f689907ce8bbbfb3f0/59730_tfnightly.ipynb) and let us know if it helps. 

Thank you.



 @pjpratik Thank you for the update. I tried your suggestion and updated TF to nightly version, on my end it still failed. Although I'm not sure what caused that, it appears there's some invalid memory access? I originally observed this issue on aarch64, but now I'm also seeing the same issue on x86_64 instance.

To illustrate the problem, I ran the same python script multiple times. And sometimes it failed on the Gather operation:
```
INFO: Created TensorFlow Lite XNNPACK delegate for CPU.
Traceback (most recent call last):
  File "/home/ubuntu/alexa/tf.py", line 53, in <module>
    interpreter.invoke() # Seg Fault
  File "/usr/local/lib/python3.10/dist-packages/tensorflow/lite/python/interpreter.py", line 941, in invoke
    self._interpreter.Invoke()
RuntimeError: tensorflow/lite/kernels/gather.cc:132 indices_has_only_positive_elements was not true.gather index out of boundsNode number 17 (GATHER) failed to invoke.
```

while other times the model output just seems to be random (because I've used fixed random seed in the script). Note that the printed elements below are emitted by [this](https://www.tensorflow.org/api_docs/python/tf/debugging/assert_near) API:
```
tensorflow.python.framework.errors_impl.InvalidArgumentError: Expected 'tf.Tensor(False, shape=(), dtype=bool)' to be true. Summarized data: b''
b'x and y not equal to tolerance rtol = tf.Tensor(1.1920929e-06, shape=(), dtype=float32), atol = tf.Tensor(0.05, shape=(), dtype=float32)'
b'x (shape=(1, 10, 768) dtype=float32) = '
0.33638424, -0.8871238, 0.74096835, ...
b'y (shape=(1, 10, 768) dtype=float32) = '
-0.45434994, -0.16998653, 1.0446085, ...
```
```
tensorflow.python.framework.errors_impl.InvalidArgumentError: Expected 'tf.Tensor(False, shape=(), dtype=bool)' to be true. Summarized data: b''
b'x and y not equal to tolerance rtol = tf.Tensor(1.1920929e-06, shape=(), dtype=float32), atol = tf.Tensor(0.05, shape=(), dtype=float32)'
b'x (shape=(1, 10, 768) dtype=float32) = '
-0.4937328, 1.9754344, 0.4033151, ...
b'y (shape=(1, 10, 768) dtype=float32) = '
-1.236057, 0.8411898, 1.2963096, ...
```

I've also reproduced the issue on your [colab](https://colab.research.google.com/gist/yd2102/df1b6e4904a139b7a2b8386b03915094/59730_tfnightly.ipynb) session by running the script multiple times.
 @yd2102 Thanks for the information. I was able to reproduce after multiple runs. Please find the gist [here](https://colab.research.google.com/gist/pjpratik/265e5c9c7698c8dfee41c1db163fb93e/59730_tfnightly.ipynb).

@sachinprasadhs Could you please look into this? Thanks! Hi @yd2102, It looks like we are calling `resize_tensor_input` on what looks to be output related arguments:
```
interpreter.resize_tensor_input(output0_index, [BATCH_SIZE, SEQUENCE_LEGNTH, 768], strict=True)
interpreter.resize_tensor_input(output1_index, [BATCH_SIZE, 768], strict=True)
```

We shouldn't need to resize output tensors, this may be a mistake. Could you remove these lines and try again? This issue is stale because it has been open for 7 days with no activity. It will be closed if no further activity occurs. Thank you. This issue was closed because it has been inactive for 7 days since being marked as stale. Please reopen if you'd like to work on this further.
### 1. System information

- OS Platform and Distribution: Ubuntu 22.04, aarch64
- TensorFlow installation (pip package or built from source): pip
- TensorFlow library (version, if pip package or github SHA, if built from source): tensorflow-cpu-aws==2.11.0
- Model library: transformers==4.26.1

### 2. Code

```
import tensorflow as tf
from transformers import BertConfig, TFBertModel

BATCH_SIZE = 1
SEQUENCE_LEGNTH = 10
VOCAB_SIZE = 150000

tf.random.set_seed(0)
tf.config.threading.set_intra_op_parallelism_threads(4)

bert_config = BertConfig(vocab_size=VOCAB_SIZE,
                         hidden_size=768,
                         num_hidden_layers=4,
                         num_attention_heads=12,
                         intermediate_size=1200,
                         hidden_act='gelu',
                         hidden_dropout_prob=0.1,
                         attention_probs_dropout_prob=0.1,
                         max_position_embeddings=512,
                         type_vocab_size=2,
                         initializer_range=0.02,
                         layer_norm_eps=1e-12,
                         position_embedding_type='absolute',
                         return_dict=False)

model = TFBertModel(bert_config)
input = [
    tf.random.uniform(shape=(BATCH_SIZE, SEQUENCE_LEGNTH), minval=0, maxval=VOCAB_SIZE, dtype=tf.int32),
    tf.ones(shape=(BATCH_SIZE, SEQUENCE_LEGNTH), dtype=tf.int32)
]

# Run original model
model(input)
print("1")

converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
model2 = converter.convert()

interpreter = tf.lite.Interpreter(model_content=model2)
input0_index = interpreter.get_input_details()[0]["index"]
input1_index = interpreter.get_input_details()[1]["index"]
output0_index = interpreter.get_output_details()[0]["index"]
output1_index = interpreter.get_output_details()[1]["index"]

interpreter.resize_tensor_input(input0_index, input[0].shape, strict=True)
interpreter.resize_tensor_input(input1_index, input[1].shape, strict=True)
interpreter.resize_tensor_input(output0_index, [BATCH_SIZE, SEQUENCE_LEGNTH, 768], strict=True)
interpreter.resize_tensor_input(output1_index, [BATCH_SIZE, 768], strict=True)

interpreter.allocate_tensors()
interpreter.set_tensor(input0_index, input[0])
interpreter.set_tensor(input1_index, input[1])

# Run converted model
interpreter.invoke() # Seg Fault
print("2")
```

### 3. Failure after conversion

The conversion is successful, but interpreter's invoke() fails with segmentation fault.

### 4. (optional) RNN conversion support
N/ A

### 5. (optional) Any other info / logs
I was mainly following the example code [here](https://www.tensorflow.org/lite/performance/post_training_quant#test_the_model_on_one_image), and tried to convert Bert model to a quantized one. The interpreter failed with the following error:

```
/usr/local/lib/python3.10/dist-packages/tensorflow_io/python/ops/__init__.py:98: UserWarning: unable to load libtensorflow_io_plugins.so: unable to open file: libtensorflow_io_plugins.so, from paths: ['/usr/local/lib/python3.10/dist-packages/tensorflow_io/python/ops/libtensorflow_io_plugins.so']
caused by: ["[Errno 2] The file to load file system plugin from does not exist.: '/usr/local/lib/python3.10/dist-packages/tensorflow_io/python/ops/libtensorflow_io_plugins.so'"]
  warnings.warn(f"unable to load libtensorflow_io_plugins.so: {e}")
/usr/local/lib/python3.10/dist-packages/tensorflow_io/python/ops/__init__.py:104: UserWarning: file system plugins are not loaded: unable to open file: libtensorflow_io.so, from paths: ['/usr/local/lib/python3.10/dist-packages/tensorflow_io/python/ops/libtensorflow_io.so']
caused by: ['/usr/local/lib/python3.10/dist-packages/tensorflow_io/python/ops/libtensorflow_io.so: cannot open shared object file: No such file or directory']
  warnings.warn(f"file system plugins are not loaded: {e}")
1
2023-02-17 18:55:23.466676: I tensorflow/core/grappler/devices.cc:75] Number of eligible GPUs (core count >= 8, compute capability >= 0.0): 0 (Note: TensorFlow was not compiled with CUDA or ROCm support)
2023-02-17 18:55:23.466855: I tensorflow/core/grappler/clusters/single_machine.cc:358] Starting new session
2023-02-17 18:55:29.150726: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:362] Ignored output_format.
2023-02-17 18:55:29.150768: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:365] Ignored drop_control_dependency.
INFO: Created TensorFlow Lite XNNPACK delegate for CPU.
Segmentation fault (core dumped)
```
