Sparse Tensor adds more memory size to .tflite file.
Hi @bywmm Thanks for reporting the issue.

I have tried to reproduce this issue and I have observed adding sparse tensors results in `.tflite` model which is of size `~15k` with both TF 2.11 and TF 2.12.

Please find the gist [here](https://colab.research.google.com/gist/pjpratik/cb7b0451f7338c39747f93f5bfdad272/60529.ipynb). Can you please check and let us know If I'm missing anything here?

Thanks. Hi @pjpratik , thanks for your relpy.

I think this difference is caused by the value of `batch_size`.
```python
batch_size = 2700  # 512
X_in = tf.keras.layers.Input(shape=(64,), batch_size=batch_size)
idx = tf.keras.layers.Input(shape=(5,), batch_size=batch_size)
```
The reported 58K memory size is under the settings of `batch_size = 2700`. It seems that the `batch_size` is larger, the size of `.tflite` is larger, in this case. For example, in my test, when `batch_size = 27000`, the size of `.tflite` becomes 532K. Hi @bywmm 

I guess this is intended as the model is filled with zero value buffers with increase in batch size given the tflites design choice to materialise arrays.

TensorFlow Lite [Model Analyzer](https://www.tensorflow.org/lite/guide/model_analyzer) API helps us to analyze models in TensorFlow Lite format by listing a model's structure.

By using `tf.lite.experimental.Analyzer.analyze(model_content=tflite_model)`, I have observed that

```
 Model size:      58580 bytes
    Non-data buffer size:       2660 bytes (04.54 %)
  Total data buffer size:      55920 bytes (95.46 %)
    (Zero value buffers):      54004 bytes (92.19 %)

* Buffers of TFLite model are mostly used for constant tensors.
  And zero value buffers are buffers filled with zeros.
  Non-data buffers area are used to store operators, subgraphs and etc.

```
Also, as given in https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/schema/schema.fbs
https://github.com/tensorflow/tensorflow/blob/d77a84432d779df135613f5272d7e9c1f2ea57b0/tensorflow/lite/schema/schema.fbs#L98-L123

Thanks. Hi @pjpratik 

Thanks for your reply. I have known the major of the data buffer size is caused by the zero buffers (shape: [27000, 5]).
```txt
Tensors of Subgraph#0
  T#0(serving_default_input_1:0) shape:[27000, 64], type:FLOAT32
  T#1(serving_default_input_2:0) shape:[27000, 5], type:FLOAT32
  T#2(model/my_layer/dense_to_sparse/zeros_like) shape:[27000, 5], type:FLOAT32 RO 540000 bytes, buffer: 3, data:[0, 0, 0, 0, 0, ...]
```
But I still don't know what is the purpose of this design. Most importantly, how can I remove these zero buffers in my `.tflite`?

Thank you. Hi @bywmm 

The flatbuffers are designed to have a little dependency on input in such a way that it can be accessed directly without parsing/unpacking. 

Reducing batch size is a way for a smaller TFLite model as TFLite intends the model for inferencing on mobile and edge devices.

Thanks. @pjpratik Thanks for your patient response. I still have one more question. Besides the operators, why `TFLite` saves some data buffers? What kind of data will be saved？ Hi @bywmm 

The data buffers are used to store the tensors like weights, biases, scaling and quantization parameters(if applied) and other parameters which are required by model for inferencing. 

You can also check ways to reduce by the binary size by doing selective build. Please check this [documentation](https://www.tensorflow.org/lite/guide/reduce_binary_size) for the reference.

Thanks. 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. 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/60529">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/60529">No</a>

Hi there, I'm now working to benchmark my neural network with tflite. I implement my NN with Keras and convert it to `.tflite`. However, the actual size of my `.tflite` is much bigger than the theoretical parameter size. I find that the extra size is mostly introduced by the `Sparse Tensor`. I use the following toy model for illustration.

```python
class MyLayer(Layer):
    def __init__(self,
                 **kwargs):
        super(MyLayer, self).__init__(**kwargs)


    def build(self, input_shape):
        self.build = True


    def call(self, inputs, mask=None):
        features = inputs[0]
        idx = inputs[1]
        
        # These two lines add the sparse tensor to the computation graph.
        idx = tf.sparse.from_dense(idx)
        idx = tf.sparse.to_dense(idx)

        output = K.dot(K.dot(idx, tf.transpose(idx)), features)

        return output

    def get_config(self):
        config = {}

        base_config = super(MyLayer, self).get_config()
        return dict(list(base_config.items()) + list(config.items()))


X_in = Input(shape=(64,), batch_size=512)
idx = Input(shape=(5,), batch_size=512)
H = Dropout(0.5)(X_in)
H = MyLayer()([H]+[idx])
Y = tf.keras.layers.Dense(7, activation='softmax')(H)
model = Model(inputs=[X_in, idx], outputs=Y)
```
Once converting the model to tflite, the size of `.tflite` is 58K, while removing the line of `H = MyLayer()([H]+[idx])`, its size becomes 3.0K. It means the non-parameter `MyLayer()` brings about 55K to the `.tflite` file. 
<img src="https://user-images.githubusercontent.com/42718268/236790239-75ca12d7-d39c-46e3-b783-87ffcc4a332b.png" width = "300" align=center />

Then, I remove the `Sparse Tensor` in  `MyLayer()` The new `call()` function is as follows.
```python
    def call(self, inputs, mask=None):
        features = inputs[0]
        idx = inputs[1]

        output = K.dot(K.dot(idx, tf.transpose(idx)), features)

        return output
```
Now, the size of `.tflite` only becomes to 3.5K. So, I'm wondering **why the sparse tensor introduces so much extra size of** `.tflite`, and how I lower it. Besides, I want to implement the `Sparse-Dense Matrix Multiplication` operator, which can be used in TFLite. Can you give me some hint? 

Thank you.

Note: My env is `python=3.8.16`, `tensorflow=2.11.0`.