[RNN] TFLite converter segfaults with GRU models
@CNugteren This issue might not be occurring in TF v2.13 or older but it is a known issue for TF v2.14. Could you try to use TFLite micro converter as it supports GRU models?
Thank you! > Could you try to use TFLite micro converter

Could you elaborate what you mean with this and share a link to where I can find that? As far as I am aware, https://github.com/tensorflow/tflite-micro does not have a converter. In any case, that would be a workaround and does not solve the actual issue. @CNugteren Yes, it is a workaround and it doesn't solve the real issue.
 To use the TFLite Micro converter to convert a GRU model, you can use the following command:
```
tflite_convert --input_format=keras --output_format=tflite --target_spec=micro --enable_mlir_converter --saved_model_dir=my_model
```
This will create a TFLite Micro model file that you can use on your embedded device.

@pjpratik Could you please have a look at this issue and take it forward?
Thank you! I was able to reproduce the issue in TF 2.14 and nightly as well. Please find this [gist](https://colab.research.google.com/gist/pjpratik/5cff9875ce9ef8739f14ab24eac8081e/62281.ipynb).

A similar issue is already being tracked in #61370 

@pkgoogle Could you please look into this?

Thanks. I was able to replicate with the attached gist. @abattery, can you please take a look? Thanks. I could reproduce this with a small STFT layer.

Keras model: [keras_stft.keras.zip](https://github.com/tensorflow/tensorflow/files/13843255/keras_stft.keras.zip)
SavedModel: [keras_stft_tf.zip](https://github.com/tensorflow/tensorflow/files/13843261/keras_stft_tf.zip)

I don't have the TF code to create said STFT, as it was converted from Torch via nobuco. However, when running `tflite_convert` in Tensorflow 2.15.0, I get the following errors:

```
tflite_convert --input_format=keras --output_format=tflite --enable_mlir_converter --saved_model_dir=./export/keras_stft_tf/ --output_file=./export/keras_stft_tfmicro.tflite
2024-01-05 15:15:13.398031: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:378] Ignored output_format.
2024-01-05 15:15:13.398046: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:381] Ignored drop_control_dependency.
2024-01-05 15:15:13.398425: I tensorflow/cc/saved_model/reader.cc:83] Reading SavedModel from: ./export/keras_stft_tf/
2024-01-05 15:15:13.398819: I tensorflow/cc/saved_model/reader.cc:51] Reading meta graph with tags { serve }
2024-01-05 15:15:13.398827: I tensorflow/cc/saved_model/reader.cc:146] Reading SavedModel debug info (if present) from: ./export/keras_stft_tf/
2024-01-05 15:15:13.399504: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:388] MLIR V1 optimization pass is not enabled
2024-01-05 15:15:13.399787: I tensorflow/cc/saved_model/loader.cc:233] Restoring SavedModel bundle.
2024-01-05 15:15:13.404777: I tensorflow/cc/saved_model/loader.cc:217] Running initialization op on SavedModel bundle at path: ./export/keras_stft_tf/
2024-01-05 15:15:13.408131: I tensorflow/cc/saved_model/loader.cc:316] SavedModel load for tags { serve }; Status: success: OK. Took 9707 microseconds.
2024-01-05 15:15:13.412411: I tensorflow/compiler/mlir/tensorflow/utils/dump_mlir_util.cc:269] disabling MLIR crash reproducer, set env var `MLIR_CRASH_REPRODUCER_DIRECTORY` to enable.
2024-01-05 15:15:13.420106: E tensorflow/compiler/mlir/lite/stablehlo/transforms/op_stat_pass.cc:119] Unsupported data type.
2024-01-05 15:15:13.420112: E tensorflow/compiler/mlir/lite/stablehlo/transforms/op_stat_pass.cc:119] Unsupported data type.
2024-01-05 15:15:13.420115: E tensorflow/compiler/mlir/lite/stablehlo/transforms/op_stat_pass.cc:119] Unsupported data type.
Summary on the non-converted ops:
---------------------------------
 * Accepted dialects: tfl, builtin, func
 * Non-Converted Ops: 12, Total Ops 30, % non-converted = 40.00 %
 * 12 ARITH ops

- arith.constant:   12 occurrences  (f32: 1, i32: 11)



  (: 1)
  (f32: 2)
  (f32: 1)
  (f32: 1)
  (f32: 1)
  (f32: 1)
  (: 1, f32: 5)
  (: 1)
  (f32: 1)
2024-01-05 15:15:13.420384: I tensorflow/compiler/mlir/lite/flatbuffer_export.cc:2989] Estimated count of arithmetic ops: 1.051 M  ops, equivalently 0.525 M  MACs
```

It successfully converts without errors in Tensorflow 2.12.0.

This is just to say that this error is not limited to GRU models. Running into similar issues. Segfaults, while trying to convert an LSTM autoencoder model to tflite with representative dataset with `TFLITE_BUILTINS_INT8`. Was having a segfault on `2.14` and `2.15` and `nightly`. Issue is not present in `2.13`.
### 1. System information

- **OS Platform and Distribution** Tested both on x86 Ubuntu 22.04 and x86 macOS 13.6
- **TensorFlow installation:** From pip package
- **TensorFlow library:** Occurs with TF 2.14, 2.15rc0 and tf-nightly

### 2. Code

The following snippet segfaults with TF 2.14 or newer, but runs and works fine with TF 2.13:

```python
import tensorflow as tf

NUM_SAMPLES = 10
SAMPLE_SIZE = 64

def convert(model: tf.keras.Model, dataset_gen):
    converter = tf.lite.TFLiteConverter.from_keras_model(model)
    converter.optimizations = [tf.lite.Optimize.DEFAULT]
    converter.representative_dataset = dataset_gen
    converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
    converter.inference_input_type = tf.int8
    converter.inference_output_type = tf.int8
    return converter.convert()

def gru_dataset():
    for _ in range(100):
        shape = (1, NUM_SAMPLES, SAMPLE_SIZE)
        yield [tf.random.uniform(shape, minval=-1, maxval=1)]

in_layer = tf.keras.layers.Input((NUM_SAMPLES, SAMPLE_SIZE), batch_size=1)
out_layer = tf.keras.layers.GRU(units=16)(in_layer)
gru_model = tf.keras.Model(in_layer, out_layer)
int8_model = convert(gru_model, gru_dataset)
```

### 3. Logs

It runs and converts fine with `tensorflow-cpu==2.13.0`.

With `tensorflow-cpu==2.14.0`:
```
2023-10-30 13:52:41.063290: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
~/venv/lib/python3.10/site-packages/tensorflow/lite/python/convert.py:947: UserWarning: Statistics for quantized inputs were expected, but not specified; continuing anyway.
  warnings.warn(
2023-10-30 13:52:44.353130: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:378] Ignored output_format.
2023-10-30 13:52:44.353146: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:381] Ignored drop_control_dependency.
2023-10-30 13:52:44.353537: I tensorflow/cc/saved_model/reader.cc:83] Reading SavedModel from: /tmp/tmpprk8uurf
2023-10-30 13:52:44.359495: I tensorflow/cc/saved_model/reader.cc:51] Reading meta graph with tags { serve }
2023-10-30 13:52:44.359516: I tensorflow/cc/saved_model/reader.cc:146] Reading SavedModel debug info (if present) from: /tmp/tmpprk8uurf
2023-10-30 13:52:44.372983: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:382] MLIR V1 optimization pass is not enabled
2023-10-30 13:52:44.376101: I tensorflow/cc/saved_model/loader.cc:233] Restoring SavedModel bundle.
2023-10-30 13:52:44.407856: I tensorflow/cc/saved_model/loader.cc:217] Running initialization op on SavedModel bundle at path: /tmp/tmpprk8uurf
2023-10-30 13:52:44.432972: I tensorflow/cc/saved_model/loader.cc:316] SavedModel load for tags { serve }; Status: success: OK. Took 79436 microseconds.
2023-10-30 13:52:44.550427: I tensorflow/compiler/mlir/lite/flatbuffer_export.cc:2245] Estimated count of arithmetic ops: 0.011 M  ops, equivalently 0.005 M  MACs
Segmentation fault (core dumped)
```

With `tensorflow-cpu==2.15.0rc0`:
```
2023-10-30 13:58:11.732055: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
~/venv/lib/python3.10/site-packages/tensorflow/lite/python/convert.py:953: UserWarning: Statistics for quantized inputs were expected, but not specified; continuing anyway.
  warnings.warn(
2023-10-30 13:58:14.849958: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:378] Ignored output_format.
2023-10-30 13:58:14.849973: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:381] Ignored drop_control_dependency.
2023-10-30 13:58:14.850413: I tensorflow/cc/saved_model/reader.cc:83] Reading SavedModel from: /tmp/tmpx9tx89pf
2023-10-30 13:58:14.855818: I tensorflow/cc/saved_model/reader.cc:51] Reading meta graph with tags { serve }
2023-10-30 13:58:14.855848: I tensorflow/cc/saved_model/reader.cc:146] Reading SavedModel debug info (if present) from: /tmp/tmpx9tx89pf
2023-10-30 13:58:14.868133: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:388] MLIR V1 optimization pass is not enabled
2023-10-30 13:58:14.872102: I tensorflow/cc/saved_model/loader.cc:233] Restoring SavedModel bundle.
2023-10-30 13:58:14.905284: I tensorflow/cc/saved_model/loader.cc:217] Running initialization op on SavedModel bundle at path: /tmp/tmpx9tx89pf
2023-10-30 13:58:14.931276: I tensorflow/cc/saved_model/loader.cc:316] SavedModel load for tags { serve }; Status: success: OK. Took 80863 microseconds.
Summary on the non-converted ops:
---------------------------------
 * Accepted dialects: tfl, builtin, func
 * Non-Converted Ops: 14, Total Ops 49, % non-converted = 28.57 %
 * 14 ARITH ops

- arith.constant:   14 occurrences  (f32: 5, i32: 9)

  (i1: 1, i32: 1)


  (f32: 4, i32: 2)
  (f32: 2)
  (f32: 1)
  (i1: 1)
  (f32: 2)
  (f32: 3)

  (f32: 2)
  (f32: 2)
  (f32: 1)
  (f32: 1)
  (f32: 1)
  (i32: 1)

2023-10-30 13:58:15.052025: I tensorflow/compiler/mlir/lite/flatbuffer_export.cc:2989] Estimated count of arithmetic ops: 0.011 M  ops, equivalently 0.005 M  MACs
Segmentation fault (core dumped)
```

With `tf_nightly_cpu==2.16.0.dev20231026`:
```
2023-10-30 13:42:25.667161: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
~/venv/lib/python3.10/site-packages/tensorflow/lite/python/convert.py:953: UserWarning: Statistics for quantized inputs were expected, but not specified; continuing anyway.
  warnings.warn(
2023-10-30 13:42:27.079622: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:378] Ignored output_format.
2023-10-30 13:42:27.079636: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:381] Ignored drop_control_dependency.
2023-10-30 13:42:27.080066: I tensorflow/cc/saved_model/reader.cc:83] Reading SavedModel from: /tmp/tmpppmyiije
2023-10-30 13:42:27.080602: I tensorflow/cc/saved_model/reader.cc:51] Reading meta graph with tags { serve }
2023-10-30 13:42:27.080613: I tensorflow/cc/saved_model/reader.cc:146] Reading SavedModel debug info (if present) from: /tmp/tmpppmyiije
2023-10-30 13:42:27.082323: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:388] MLIR V1 optimization pass is not enabled
2023-10-30 13:42:27.082703: I tensorflow/cc/saved_model/loader.cc:233] Restoring SavedModel bundle.
2023-10-30 13:42:27.095715: I tensorflow/cc/saved_model/loader.cc:217] Running initialization op on SavedModel bundle at path: /tmp/tmpppmyiije
2023-10-30 13:42:27.103319: I tensorflow/cc/saved_model/loader.cc:316] SavedModel load for tags { serve }; Status: success: OK. Took 23253 microseconds.
2023-10-30 13:42:27.120091: I tensorflow/compiler/mlir/tensorflow/utils/dump_mlir_util.cc:269] disabling MLIR crash reproducer, set env var `MLIR_CRASH_REPRODUCER_DIRECTORY` to enable.
loc(fused["ReadVariableOp:", callsite("functional_1_1/gru_1/while/gru_cell_1/ReadVariableOp@functional_1_1_gru_1_while_body_135"("scratch.py":23:1) at callsite("scratch.py":13:1 at callsite("~/venv/lib/python3.10/site-packages/tensorflow/lite/python/lite.py":1139:1 at callsite("~/venv/lib/python3.10/site-packages/tensorflow/lite/python/lite.py":1093:1 at callsite("~/venv/lib/python3.10/site-packages/tensorflow/lite/python/lite.py":1601:1 at callsite("~/venv/lib/python3.10/site-packages/tensorflow/lite/python/lite.py":1579:1 at callsite("~/venv/lib/python3.10/site-packages/tensorflow/lite/python/convert_phase.py":205:1 at callsite("~/venv/lib/python3.10/site-packages/tensorflow/lite/python/lite.py":1502:1 at callsite("~/venv/lib/python3.10/site-packages/keras/src/backend/tensorflow/layer.py":57:1 at callsite("~/venv/lib/python3.10/site-packages/keras/src/backend/tensorflow/layer.py":119:1 at callsite("~/venv/lib/python3.10/site-packages/keras/src/utils/traceback_utils.py":118:1 at callsite("~/venv/lib/python3.10/site-packages/keras/src/layers/layer.py":830:1 at callsite("~/venv/lib/python3.10/site-packages/keras/src/utils/traceback_utils.py":118:1 at callsite("~/venv/lib/python3.10/site-packages/keras/src/ops/operation.py":42:1 at callsite("~/venv/lib/python3.10/site-packages/keras/src/utils/traceback_utils.py":157:1 at callsite("~/venv/lib/python3.10/site-packages/keras/src/models/functional.py":188:1 at callsite("~/venv/lib/python3.10/site-packages/keras/src/ops/function.py":140:1 at callsite("~/venv/lib/python3.10/site-packages/keras/src/models/functional.py":574:1 at callsite("~/venv/lib/python3.10/site-packages/keras/src/utils/traceback_utils.py":118:1 at callsite("~/venv/lib/python3.10/site-packages/keras/src/layers/layer.py":830:1 at callsite("~/venv/lib/python3.10/site-packages/keras/src/utils/traceback_utils.py":118:1 at callsite("~/venv/lib/python3.10/site-packages/keras/src/ops/operation.py":42:1 at callsite("~/venv/lib/python3.10/site-packages/keras/src/utils/traceback_utils.py":157:1 at callsite("~/venv/lib/python3.10/site-packages/keras/src/layers/rnn/gru.py":559:1 at callsite("~/venv/lib/python3.10/site-packages/keras/src/layers/rnn/rnn.py":397:1 at callsite("~/venv/lib/python3.10/site-packages/keras/src/layers/rnn/gru.py":554:1 at callsite("~/venv/lib/python3.10/site-packages/keras/src/layers/rnn/rnn.py":339:1 at callsite("~/venv/lib/python3.10/site-packages/keras/src/backend/tensorflow/rnn.py":423:1 at callsite("~/venv/lib/python3.10/site-packages/keras/src/backend/tensorflow/rnn.py":408:1 at callsite("~/venv/lib/python3.10/site-packages/keras/src/layers/rnn/rnn.py":331:1 at callsite("~/venv/lib/python3.10/site-packages/keras/src/utils/traceback_utils.py":118:1 at callsite("~/venv/lib/python3.10/site-packages/keras/src/layers/layer.py":830:1 at callsite("~/venv/lib/python3.10/site-packages/keras/src/utils/traceback_utils.py":118:1 at callsite("~/venv/lib/python3.10/site-packages/keras/src/ops/operation.py":42:1 at callsite("~/venv/lib/python3.10/site-packages/keras/src/utils/traceback_utils.py":157:1 at callsite("~/venv/lib/python3.10/site-packages/keras/src/layers/rnn/gru.py":194:1 at callsite("~/venv/lib/python3.10/site-packages/keras/src/ops/numpy.py":4654:1 at callsite("~/venv/lib/python3.10/site-packages/keras/src/backend/tensorflow/numpy.py":1057:1 at "~/venv/lib/python3.10/site-packages/keras/src/backend/tensorflow/core.py":49:1))))))))))))))))))))))))))))))))))))))]): error: missing attribute 'value'
LLVM ERROR: Failed to infer result type(s).
Aborted (core dumped)
```
