Repository: tensorflow
Issue: TFLite Interpreter `experimental_preserve_all_tensors` yields different output
Labels: stat:awaiting tensorflower,comp:lite,TFLiteConverter,2.17

### 1. System information

- OS Platform and Distribution: Pop!_OS 22.04 LTS
- TensorFlow installation: pip package
- TensorFlow library: 2.17.0

### 2. Code



```
import tensorflow as tf
import numpy as np

from ai_edge_litert.interpreter import Interpreter

IMAGE_SHAPE = (10, 10, 1)
IMAGE_SIZE = IMAGE_SHAPE[0]*IMAGE_SHAPE[1]*IMAGE_SHAPE[2]

tf.random.set_seed(0)
np.random.seed(0)
tf.keras.utils.set_random_seed(0)

inp_layer = tf.keras.layers.InputLayer(shape=IMAGE_SHAPE, batch_size=1)
conv_layer = tf.keras.layers.Conv2D(4, (3, 3), strides=(2, 2), padding="same", use_bias=True, activation="relu")

model = tf.keras.models.Sequential([
  inp_layer,
  conv_layer,
])

w = conv_layer.get_weights()
k = w[0]
b = w[1]

k[:, :, :, 0] = 0
k[:, :, :, 2] = 0
b = np.zeros(shape=w[1].shape)

print("kernel:\n", k)
print("bias:\n", b)

w[0] = k
w[1] = b
conv_layer.set_weights(w)

def representative_dataset():
  for _ in range(10):
    data = np.random.rand(1, *IMAGE_SHAPE)
    yield [data.astype(np.float32)]

converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.representative_dataset = representative_dataset
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
converter.inference_input_type = tf.int8
converter.inference_output_type = tf.int8

tflite_quant_model = converter.convert()

#filename = "example.tflite"
#with open(filename, 'wb') as f:
#  f.write(tflite_quant_model)

#interp = Interpreter(filename)
#exper = Interpreter(filename, experimental_preserve_all_tensors=True)
interp = Interpreter(model_content=tflite_quant_model)
exper = Interpreter(model_content=tflite_quant_model, experimental_preserve_all_tensors=True)


inp=np.zeros(IMAGE_SIZE, dtype=np.int8)
arr = [62, 63, 64, 72, 73, 74, 82, 83, 84]
for i in arr:
  inp[i] = i
t = tf.constant(inp, shape=IMAGE_SHAPE, dtype=tf.int8)

print("input:\n", t, "\n")

def alloc_and_run(interpreter):
  interpreter.allocate_tensors()

  input_details = interpreter.get_input_details()
  output_details = interpreter.get_output_details()

  interpreter.set_tensor(input_details[0]['index'], [t])
  interpreter.invoke()

  return interpreter.get_tensor(output_details[0]['index'])

out_interp = alloc_and_run(interp)
out_exper = alloc_and_run(exper)

print("out_interp:\n", out_interp, "\n")
print("out_exper:\n", out_exper, "\n")

assert(tf.reduce_all(tf.equal(out_interp, out_exper)))
```

### 3. Failure after conversion
Conversion is successful, but interpreter and experimental interpreter yield a different result:
```
out_interp:
 [[[[-128  -19 -128  -68]
   [-128  -19 -128  -68]
   [-128  -19 -128  -68]
   [-128  -19 -128  -68]
   [-128  -32 -128 -128]]

  [[-128  -19 -128  -68]
   [-128  -19 -128  -68]
   [-128  -19 -128  -68]
   [-128  -19 -128  -68]
   [-128  -32 -128 -128]]

  [[-128  -23 -128  -47]
   [-128   28 -128  -61]
   [-128   15 -128  -78]
   [-128  -19 -128  -68]
   [-128  -32 -128 -128]]

  [[-128  -12 -128   -5]
   [-128   57 -128  -34]
   [-128   10 -128  -98]
   [-128  -19 -128  -68]
   [-128  -32 -128 -128]]

  [[-128 -123 -128  -75]
   [-128 -128 -128  -66]
   [-128 -128 -128 -120]
   [-128 -117 -128  -83]
   [-128 -128 -128 -128]]]] 

out_exper:
 [[[[-128  -19 -128  -68]
   [-128  -19 -128  -68]
   [-128  -19 -128  -68]
   [-128  -19 -128  -68]
   [-128  -32 -128 -128]]

  [[-128  -19 -128  -68]
   [-128  -19 -128  -68]
   [-128  -19 -128  -68]
   [-128  -19 -128  -68]
   [-128  -32 -128 -128]]

  [[-128  -23 -128  -47]
   [-128   28 -128  -61]
   [-128   15 -128  -78]
   [-128  -19 -128  -68]
   [-128  -32 -128 -128]]

  [[-128  -12 -128   -5]
   [-128   57 -128  -33]
   [-128   10 -128  -98]
   [-128  -19 -128  -68]
   [-128  -32 -128 -128]]

  [[-128 -123 -128  -75]
   [-128 -128 -128  -66]
   [-128 -128 -128 -120]
   [-128 -117 -128  -83]
   [-128 -128 -128 -128]]]] 

Traceback (most recent call last):
  File "/home/sri/riptools/scratch5/example.py", line 79, in <module>
    assert(tf.reduce_all(tf.equal(out_interp, out_exper)))
AssertionError
```
The difference here is:
`out_interp[0][3][1][3] = -33`
`out_exper[0][3][1][3] = -34`
This "off-by-one" may occur with other sized input tensors and larger non-zero inputs/weights as well.