Model conversion crashed when feed data during quantization
Hi @pkgoogle,

I have reproduced the issue. It's getting crashed at the time of conversion. Here is the [gist](https://colab.research.google.com/gist/LakshmiKalaKadali/00d0b47c4bd47b490531314cab18a6bc/-62701.ipynb). The sampling rate recommendation in the error log is fixed by setting ```sample_rate= 16000``` and passed to the processor.

Thank You I was able to reproduce with the same gist, @abattery, can you please take a look? Thanks. Hi all, is there anything can do for helping the progress?
Thank you a lot. Hi @houcheng, thanks for your interest. It probably depends on your familiarity, if you're comfortable with C++ & Bazel please review these BUILD files: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/compiler/mlir/lite/BUILD, https://github.com/tensorflow/tensorflow/blob/master/tensorflow/compiler/mlir/BUILD. The binaries here effectively use the same codepaths as when you do converter.convert, you'll have to figure out how to use them properly for your use case, then you can use lldb/gdb with these binaries to try and debug this.

Alternatively you can research how to attach lldb/gdb to a python process (the one that gets launched when you do `python your_script.py`) and set a breakpoint somewhere above `tensorflow/lite/kernels/reduce.cc:445`, and then dig into why that error is being thrown.

Both of these require good C++ knowledge, so if you don't have that, you'll have to learn at least the basics first. Hi @pkgoogle,
Thank you for kindly reply my questions. I've tried the bazel in my ubuntu before and it can compile okay by the default build script and works well. I think maybe I include too much code into the model code and there is some non primities python code caused the problem. I think I'll either reduce the features of the model or using the gdb breakpoints you mentioned for finding the exact problem, later.
 @houcheng, np, you may also want to directly use the converter CLT: https://www.tensorflow.org/lite/models/convert/convert_models#command_line_tool_, this is essentially an entry point into what converter.convert does but it skips the python wrappers.
### 1. System information

- OS Platform and Distribution: Ubuntu 22.04
- TensorFlow installation (pip package or built from source): pip
- TensorFlow library (version, if pip package or github SHA, if built from source):  install tensorflow==2.15.0

### 2. Code
#### Option B: Paste your code here or provide a link to a custom end-to-end colab

```
import tensorflow as tf
from transformers import WhisperProcessor, WhisperFeatureExtractor, WhisperTokenizer, TFWhisperForConditionalGeneration
import soundfile as sf
import numpy as np
class GenerateModel2(tf.Module):
  def __init__(self, model):
    super(GenerateModel2, self).__init__()
    self.model = model
  @tf.function(
    input_signature=[
      tf.TensorSpec((1, 80, 3000), tf.float32, name="input_ids"),
    ]
  )
  def serving(self, input_features):
    outputs = self.model.generate(input_features)
    return outputs

# Initialize the feature extractor, tokenizer, and processor
feature_extractor = WhisperFeatureExtractor.from_pretrained("openai/whisper-tiny.en")
tokenizer = WhisperTokenizer.from_pretrained("openai/whisper-tiny.en", predict_timestamps=True)
processor = WhisperProcessor(feature_extractor, tokenizer)

# Load the Whisper model
model = TFWhisperForConditionalGeneration.from_pretrained("openai/whisper-tiny.en")
generate_model = GenerateModel2(model=model)

def representative_data_gen():
  audio_input, sample_rate = sf.read("story-1-second-2.wav")
  inputs = processor(audio_input, return_tensors="tf")
  yield {"input_ids": inputs.input_features}

converter = tf.lite.TFLiteConverter.from_concrete_functions([generate_model.serving.get_concrete_function()], generate_model)
converter.target_spec.supported_ops = [
  tf.lite.OpsSet.TFLITE_BUILTINS_INT8,
  tf.lite.OpsSet.SELECT_TF_OPS
]
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.inference_input_type = tf.int8
converter.inference_output_type = tf.int8
converter.representative_dataset = representative_data_gen
tflite_model_bin = converter.convert()    # <---- crash here
```

### 3. Failure during  conversion

* The converter.convert() failed at calibrator.FeedTensor()
* The stacktrace is
    * optimize/calibrator.py", line 254, in calibrate
    * optimize/calibrator.py", line 152, in _feed_tensors
    * RuntimeError: tensorflow/lite/kernels/reduce.cc:445 std::apply(optimized_ops::Mean<T, U>, args) was not true.
    * ....
    * gather index out of boundsNode number 33 (GATHER) failed to invoke.
    * Node number 390 (WHILE) failed to invoke.

Seems the process crashed when the calibrator collect data.

### 5. (optional) Any other info / logs

```
Summary on the non-converted ops:
---------------------------------
 * Accepted dialects: tfl, builtin, func
 * Non-Converted Ops: 317, Total Ops 1095, % non-converted = 28.95 %
 * 314 ARITH ops, 3 TF ops

- arith.constant:  314 occurrences  (f32: 244, i1: 1, i32: 69)

  (i1: 1, i32: 1)


- tf.If:    3 occurrences  (f32: 3)
  (f32: 114, i32: 8)
  (i32: 1)
  (f32: 35)
  (f32: 1, i32: 1)
  (f32: 8)
  (f32: 2)
  (i32: 1)
  (f32: 8, i32: 1)
  (i1: 2)
  (f32: 85)
  (f32: 2)
  (f32: 13)
  (i1: 2)
  (i1: 1)
  (i1: 1)
  (i1: 1)
  (i1: 1)
  (f32: 60)
  (f32: 94, i32: 7)

  (i32: 4)
  (f32: 8)
  (i1: 1)
  (i1: 1)
  (f32: 110, i32: 6)
  (f32: 30)
  (f32: 6, i32: 2)
  (f32: 17)
  (f32: 30)
  (f32: 18, i32: 6)
  (f32: 34, i32: 5)
  (f32: 25)
  (i32: 1)

2023-12-27 22:09:12.621978: I tensorflow/compiler/mlir/lite/flatbuffer_export.cc:2989] Estimated count of arithmetic ops: 30.250 G  ops, equivalently 15.125 G  MACs
It is strongly recommended to pass the `sampling_rate` argument to this function. Failing to do so can result in silent errors that might be hard to debug.
Traceback (most recent call last):
  File "/home/houcheng/workspace/whisper.play/try-forced-ids-quantized-official/try-3-with-gm-conversion-lite.py", line 44, in <module>
    tflite_model_bin = converter.convert()    # <---- crash here
                       ^^^^^^^^^^^^^^^^^^^
  File "/home/houcheng/miniconda3/envs/cuda-transformers/lib/python3.11/site-packages/tensorflow/lite/python/lite.py", line 2185, in convert
    return super(TFLiteConverterV2, self).convert()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/houcheng/miniconda3/envs/cuda-transformers/lib/python3.11/site-packages/tensorflow/lite/python/lite.py", line 1139, in wrapper
    return self._convert_and_export_metrics(convert_func, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/houcheng/miniconda3/envs/cuda-transformers/lib/python3.11/site-packages/tensorflow/lite/python/lite.py", line 1093, in _convert_and_export_metrics
    result = convert_func(self, *args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/houcheng/miniconda3/envs/cuda-transformers/lib/python3.11/site-packages/tensorflow/lite/python/lite.py", line 1780, in convert
    saved_model_convert_result = self._convert_as_saved_model()
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/houcheng/miniconda3/envs/cuda-transformers/lib/python3.11/site-packages/tensorflow/lite/python/lite.py", line 1760, in _convert_as_saved_model
    return self._convert_from_saved_model(graph_def)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/houcheng/miniconda3/envs/cuda-transformers/lib/python3.11/site-packages/tensorflow/lite/python/lite.py", line 1332, in _convert_from_saved_model
    return self._optimize_tflite_model(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/houcheng/miniconda3/envs/cuda-transformers/lib/python3.11/site-packages/tensorflow/lite/python/convert_phase.py", line 215, in wrapper
    raise error from None  # Re-throws the exception.
    ^^^^^^^^^^^^^^^^^^^^^
  File "/home/houcheng/miniconda3/envs/cuda-transformers/lib/python3.11/site-packages/tensorflow/lite/python/convert_phase.py", line 205, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/houcheng/miniconda3/envs/cuda-transformers/lib/python3.11/site-packages/tensorflow/lite/python/lite.py", line 1037, in _optimize_tflite_model
    model = self._quantize(
            ^^^^^^^^^^^^^^^
  File "/home/houcheng/miniconda3/envs/cuda-transformers/lib/python3.11/site-packages/tensorflow/lite/python/lite.py", line 735, in _quantize
    calibrated = calibrate_quantize.calibrate(
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/houcheng/miniconda3/envs/cuda-transformers/lib/python3.11/site-packages/tensorflow/lite/python/convert_phase.py", line 215, in wrapper
    raise error from None  # Re-throws the exception.
    ^^^^^^^^^^^^^^^^^^^^^
  File "/home/houcheng/miniconda3/envs/cuda-transformers/lib/python3.11/site-packages/tensorflow/lite/python/convert_phase.py", line 205, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/houcheng/miniconda3/envs/cuda-transformers/lib/python3.11/site-packages/tensorflow/lite/python/optimize/calibrator.py", line 254, in calibrate
    self._feed_tensors(dataset_gen, resize_input=True)
  File "/home/houcheng/miniconda3/envs/cuda-transformers/lib/python3.11/site-packages/tensorflow/lite/python/optimize/calibrator.py", line 152, in _feed_tensors
    self._calibrator.FeedTensor(input_array)
RuntimeError: tensorflow/lite/kernels/reduce.cc:445 std::apply(optimized_ops::Mean<T, U>, args) was not true.tensorflow/lite/kernels/reduce.cc:445 std::apply(optimized_ops::Mean<T, U>, args) was not true.tensorflow/lite/kernels/reduce.cc:445 std::apply(optimized_ops::Mean<T, U>, args) was not true.tensorflow/lite/kernels/reduce.cc:445 std::apply(optimized_ops::Mean<T, U>, args) was not true.tensorflow/lite/kernels/reduce.cc:445 std::apply(optimized_ops::Mean<T, U>, args) was not true.tensorflow/lite/kernels/reduce.cc:445 std::apply(optimized_ops::Mean<T, U>, args) was not true.tensorflow/lite/kernels/reduce.cc:445 std::apply(optimized_ops::Mean<T, U>, args) was not true.tensorflow/lite/kernels/reduce.cc:445 std::apply(optimized_ops::Mean<T, U>, args) was not true.tensorflow/lite/kernels/reduce.cc:445 std::apply(optimized_ops::Mean<T, U>, args) was not true.tensorflow/lite/kernels/reduce.cc:445 std::apply(optimized_ops::Mean<T, U>, args) was not true.tensorflow/lite/kernels/reduce.cc:445 std::apply(optimized_ops::Mean<T, U>, args) was not true.tensorflow/lite/kernels/reduce.cc:445 std::apply(optimized_ops::Mean<T, U>, args) was not true.tensorflow/lite/kernels/reduce.cc:445 std::apply(optimized_ops::Mean<T, U>, args) was not true.tensorflow/lite/kernels/reduce.cc:445 std::apply(optimized_ops::Mean<T, U>, args) was not true.tensorflow/lite/kernels/reduce.cc:445 std::apply(optimized_ops::Mean<T, U>, args) was not true.tensorflow/lite/kernels/reduce.cc:445 std::apply(optimized_ops::Mean<T, U>, args) was not true.gather index out of boundsNode number 33 (GATHER) failed to invoke.Node number 390 (WHILE) failed to invoke.
(cuda-transformers) ➜  try-forced-ids-quantized-official git:(master) pip list | tensor
```
