Segmentation fault for TFLite Interpreter
Hi @CorbinFoucart 

The representative dataset might be causing the problem. Usually, this is a small subset of a few hundred samples randomly chosen. I have tried with random input data and I was succesfully able to invoke the interpreter and allocate the tensors.

Please find this [gist](https://colab.research.google.com/gist/pjpratik/0943560e00d3834ab6c9ee8e637114be/61828.ipynb).

Thanks. I appreciate the fast response @pjpratik -- Interesting, how would the nature of the dataset itself be causing a segfault? Does the representative dataset need to be a certain size?

And correct me if I'm wrong, isn't a segfault under any representative dataset size considered a bug?

Thank you!   Hi @CorbinFoucart 

Sorry for the delayed response.

As far as I know, there is not strict rule for representative dataset. It needs to be a small portion of the data samples to calibrate the ranges. 

@pkgoogle is this an intended behaviour? 

Thanks.


 Hi @CorbinFoucart, can you resolve what tflite means in this gist? https://colab.sandbox.google.com/gist/pkgoogle/2667494c5beeb55a4e47da0b942e4a0f/61828.ipynb It seems like you installed or defined it somewhere else as it's not a known package to me, the next line redefines it but I am unable to resolve the original symbol. Sure, this is the tflite library built which I compiled from source as a wheel file.
```python
import tflite as tfl
print(tfl)
``` 
prints 
` module 'tflite' from '/home/_my_path_to_/tflite/__init__.py`

which I built from source via docker with a command like 
```
docker run --rm -w /opt/tensorflow_src -v ${SCRIPT_DIR}/third_party/tensorflow_src:/opt/tensorflow_src -t tflite_wheel:latest \
    bash -c "PYTHON=python3 tensorflow/lite/tools/pip_package/build_pip_package_with_cmake.sh native"
``` Hi @CorbinFoucart, I'm not familiar with your environment/workflow, where is the image "tflite_wheel:latest" coming from? How is $SCRIPT_DIR defined? Are you using a Dockerfile? Are you following a particular resource/link? Yes, I'm using a Dockerfile. Unfortunately, I can't share the process by which I'm doing the build. However, the last line is the relevant one--I'm just compiling tensorflow from source. So tflite is simply the tflite module as is typically packaged by tensorflow. Hi @CorbinFoucart, ok understood, can you help me understand where Model is coming from (The one where you call GetRootAsModel)?

[gist](https://colab.sandbox.google.com/gist/pkgoogle/2987b4cd34bd29b2d33b51cf0b0488ce/61828)

Your original code is not resolving for me and I tried a couple of different things, using tf.keras.Model, tflite.tflite.Model, tflite.Interpreter.Model. None of these work. In this case, that line is irrelevant (it does some flatbuffer reading) and can be commented out; I should have removed it earlier. I reproduce the segfault with that line removed--I can write the TfL model to file, instantiate the Interpreter object, and `interpreter.allocate_tensors()` still results in the segfault.


```python
# ...
_tflite_quant_model = converter.convert()
#tfl_model = Model.Model.GetRootAsModel(_tflite_quant_model, 0) 

# write model to file
model_file = "segfault_test.tfl"
with open(model_file, "wb") as fp:
# ... 
...

```


My apologies for the confusion! Thanks!, I was able to replicate with and without tf-nightly, [gist](https://colab.sandbox.google.com/gist/pkgoogle/2987b4cd34bd29b2d33b51cf0b0488ce/61828.ipynb#scrollTo=mqwm4Z2KzbBa).

@yijie-yang, can you please take a look? Thanks! Sorry I'm not very familiar with the representation data related problem. I think MOT team will be more helpful with this issue. Jaesung, could you take a look at this plz? @abattery Hi everyone,

@abattery I wanted to follow up with this in case it's dropped off your radar. Thank you! Hi everyone, is there any update on how to fix this error? I'm also facing the same problem ... I'm looking more into this... currently it is failing this check which effectively throws an ABORT, double_multiplier=243.18696458117677 for the model that gets saved in the above gist, https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/kernels/internal/quantization_util.cc#L117 @CorbinFoucart, the original code sample uses identical tensors, so effectively in your representative datasets, this operation produces zero tensors for the output. Our automatic quantization scaling ends up giving the output a scale of 7.84313681e-09. This is effectively too small and causes the assert to trigger. This gist replaces the identical tensors with random tensors and it is able to run through correctly: [gist](https://colab.sandbox.google.com/gist/pkgoogle/02a7dc93b9f89eda92eb65102e2a5af5/61828.ipynb). I'll work on fixing this (as it shouldn't crash) but I wanted to let you know the information for now.

@HajarAva, If what I just said doesn't apply to you, please create a new issue with your specific problem. @pkgoogle : Thanks for your quick response, I've made a new issue and explained my problem [here](https://github.com/tensorflow/tensorflow/issues/64296).  Hi @CorbinFoucart, this should now be resolved .. here's a gist showing it no longer crashes with tf-nightly: [gist](https://colab.sandbox.google.com/gist/pkgoogle/02a7dc93b9f89eda92eb65102e2a5af5/61828.ipynb#scrollTo=mqwm4Z2KzbBa). The change is here: https://github.com/tensorflow/tensorflow/commit/ea158b22e6e3449cf73e784ca8fbce5ec6c1656f. Please test with nightly and let us know if it resolves your issue. Thanks. @pkgoogle -- yes, this resolves the crash. Thanks for the patch. Are you satisfied with the resolution of your issue?
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=Yes&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/61828">Yes</a>
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=No&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/61828">No</a>

### Issue type

Bug

### Have you reproduced the bug with TensorFlow Nightly?

Yes

### Source

source

### TensorFlow version

2.13.0

### Custom code

Yes

### OS platform and distribution

Linux Ubuntu 22.04.03

### Mobile device

_No response_

### Python version

3.8.17

### Bazel version

_No response_

### GCC/compiler version

_No response_

### CUDA/cuDNN version

_No response_

### GPU model and memory

_No response_

### Current behavior?

I create a simple tensorflow model and convert it to a TFLite model. 

Upon calling the TFLite interpreter's allocate_tensors() method, I find that TFLite segfaults, perhaps similar to [this issue](https://github.com/tensorflow/tensorflow/issues/47996). 

### Standalone code to reproduce the issue

```shell
import numpy as np
import tensorflow as tf
from tflite import Model
import tflite_runtime.interpreter as tflite


if __name__ == '__main__':

    data1 = np.arange(-128, 128).reshape((16, 16, 1)).astype(np.float32)
    data2 = np.arange(-128, 128).reshape((16, 16, 1)).astype(np.float32)

    _merge = tf.keras.layers.Subtract()
    def _dataset_generator():
        for _ in range(1):
            arr1 = data1.reshape(1, *data1.shape)
            arr2 = data2.reshape(1, *data2.shape)
            yield [arr1.astype(np.float32), arr2.astype(np.float32)]

    # base TF model
    _inputs1 = tf.keras.layers.Input(shape=data1.shape, dtype=np.float32)
    _inputs2 = tf.keras.layers.Input(shape=data2.shape, dtype=np.float32)
    _merge = _merge([_inputs1, _inputs2])
    tf_model = tf.keras.Model([_inputs1, _inputs2], _merge)

    # TFL model
    converter = tf.lite.TFLiteConverter.from_keras_model(tf_model)
    converter.optimizations = [tf.lite.Optimize.DEFAULT]
    converter.representative_dataset = _dataset_generator
    _tflite_quant_model = converter.convert()
    tfl_model = Model.GetRootAsModel(_tflite_quant_model, 0)

    # write model to file
    model_file = "segfault_test.tfl"
    with open(model_file, "wb") as fp:
        fp.write(_tflite_quant_model)

    # create interpreter
    interpreter = tflite.Interpreter(
        model_path=model_file,
        experimental_op_resolver_type=tflite.OpResolverType.BUILTIN_REF)

    # this call causes a segfault
    interpreter.allocate_tensors()
```


### Relevant log output

```shell
2023-09-11 11:38:16.876852: I tensorflow/core/util/port.cc:110] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
2023-09-11 11:38:16.877887: I tensorflow/tsl/cuda/cudart_stub.cc:28] Could not find cuda drivers on your machine, GPU will not be used.
2023-09-11 11:38:16.894424: E tensorflow/compiler/xla/stream_executor/cuda/cuda_dnn.cc:7630] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered
2023-09-11 11:38:16.894443: E tensorflow/compiler/xla/stream_executor/cuda/cuda_fft.cc:609] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered
2023-09-11 11:38:16.894454: E tensorflow/compiler/xla/stream_executor/cuda/cuda_blas.cc:1518] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered
2023-09-11 11:38:16.898344: I tensorflow/tsl/cuda/cudart_stub.cc:28] Could not find cuda drivers on your machine, GPU will not be used.
2023-09-11 11:38:16.898500: 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 AVX512F AVX512_VNNI AVX512_BF16 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
2023-09-11 11:38:17.196991: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT
WARNING:tensorflow:From /home/miniconda3/envs/tf_nightly/lib/python3.8/site-packages/tensorflow/python/ops/distributions/distribution.py:259: ReparameterizationType.__init__ (from tensorflow.python.ops.distributions.distribution) is deprecated and will be removed after 2019-01-01.
Instructions for updating:
The TensorFlow Distributions library has moved to TensorFlow Probability (https://github.com/tensorflow/probability). You should update all references to use `tfp.distributions` instead of `tf.distributions`.
WARNING:tensorflow:From /home/miniconda3/envs/tf_nightly/lib/python3.8/site-packages/tensorflow/python/ops/distributions/bernoulli.py:165: RegisterKL.__init__ (from tensorflow.python.ops.distributions.kullback_leibler) is deprecated and will be removed after 2019-01-01.
Instructions for updating:
The TensorFlow Distributions library has moved to TensorFlow Probability (https://github.com/tensorflow/probability). You should update all references to use `tfp.distributions` instead of `tf.distributions`.
2023-09-11 11:38:17.461212: I tensorflow/compiler/xla/stream_executor/cuda/cuda_gpu_executor.cc:894] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
2023-09-11 11:38:17.461403: W tensorflow/core/common_runtime/gpu/gpu_device.cc:2158] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform.
Skipping registering GPU devices...
2023-09-11 11:38:17.559681: I tensorflow/cc/saved_model/reader.cc:83] Reading SavedModel from: /tmp/tmpnwirm5mk
/home/miniconda3/envs/tf_nightly/lib/python3.8/site-packages/tensorflow/lite/python/convert.py:928: UserWarning: Statistics for quantized inputs were expected, but not specified; continuing anyway.
  warnings.warn(
2023-09-11 11:38:17.578426: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:365] Ignored output_format.
2023-09-11 11:38:17.578436: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:368] Ignored drop_control_dependency.
2023-09-11 11:38:17.578732: I tensorflow/cc/saved_model/reader.cc:83] Reading SavedModel from: /tmp/tmpnwirm5mk
2023-09-11 11:38:17.578885: I tensorflow/cc/saved_model/reader.cc:51] Reading meta graph with tags { serve }
2023-09-11 11:38:17.578889: I tensorflow/cc/saved_model/reader.cc:146] Reading SavedModel debug info (if present) from: /tmp/tmpnwirm5mk
2023-09-11 11:38:17.579556: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:382] MLIR V1 optimization pass is not enabled
2023-09-11 11:38:17.579648: I tensorflow/cc/saved_model/loader.cc:233] Restoring SavedModel bundle.
2023-09-11 11:38:17.582018: I tensorflow/cc/saved_model/loader.cc:217] Running initialization op on SavedModel bundle at path: /tmp/tmpnwirm5mk
2023-09-11 11:38:17.583376: I tensorflow/cc/saved_model/loader.cc:316] SavedModel load for tags { serve }; Status: success: OK. Took 4645 microseconds.
2023-09-11 11:38:17.586236: I tensorflow/compiler/mlir/tensorflow/utils/dump_mlir_util.cc:269] disabling MLIR crash reproducer, set env var `MLIR_CRASH_REPRODUCER_DIRECTORY` to enable.
fully_quantize: 0, inference_type: 6, input_inference_type: FLOAT32, output_inference_type: FLOAT32
Aborted (core dumped)
```
