TFLite Android GPU delgate cannot run: TfLiteGpuDelegate Init: STRIDED_SLICE: Output batch don't match
Hi @ldfandian 

Can you please try in TF Nightly and let us know if you are still facing the issue?

`implementation 'org.tensorflow:tensorflow-lite-*:0.0.0-nightly-SNAPSHOT'`

Thanks. > Hi @ldfandian
> 
> Can you please try in TF Nightly and let us know if you are still facing the issue?
> 
> `implementation 'org.tensorflow:tensorflow-lite-*:0.0.0-nightly-SNAPSHOT'`
> 
> Thanks.

OK. I will try it.
After some tuning on the ops, now STRIDED_SLICE op is not complained any more.

But I get a new error message like this. What does it mean?

```
01:58:27.544  I  isKeyboardTypeChanged: false
01:58:27.544  I  isKeyboardTypeChanged: false
01:58:27.666  E  call to OpenGL ES API with no current context (logged once per thread)
--------- beginning of crash
01:58:27.685  I  Created 0 GPU delegate kernels.
01:58:27.690  E  FATAL EXCEPTION: Thread-23
                 Process: com.xsmart.recall.android, PID: 22340
                 java.lang.IllegalArgumentException: Internal error: Failed to apply delegate: Unrecognized Read selector
                 Falling back to OpenGL
                 TfLiteGpuDelegate Init: Batch size mismatch, expected 1 but got 49
                 TfLiteGpuDelegate Prepare: delegate is not initialized
                 Node number 409 (TfLiteGpuDelegateV2) failed to prepare.
                 Restored original execution plan after delegate application failure.
                 	at org.tensorflow.lite.NativeInterpreterWrapper.createInterpreter(Native Method)
                 	at org.tensorflow.lite.NativeInterpreterWrapper.init(NativeInterpreterWrapper.java:110)
                 	at org.tensorflow.lite.NativeInterpreterWrapper.<init>(NativeInterpreterWrapper.java:73)
                 	at org.tensorflow.lite.NativeInterpreterWrapperExperimental.<init>(NativeInterpreterWrapperExperimental.java:36)
                 	at org.tensorflow.lite.Interpreter.<init>(Interpreter.java:214)

```

the related node is like:
<img width="300" alt="image" src="https://github.com/tensorflow/tensorflow/assets/5018331/e5a71a84-c510-4808-a333-0db918039442">
 Hi @ldfandian 

As suggested [here](https://github.com/tensorflow/tensorflow/issues/56545#issuecomment-1190924808), the whole path with SHAPE ->STRIDED_SLICE -> PACK is a static thing you can replace with a static tensor to support GPU delegate.

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/60817">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/60817">No</a>

<details><summary>Click to expand!</summary> 
 
 ### Issue Type

Bug

### Have you reproduced the bug with TF nightly?

No

### Source

binary

### Tensorflow Version

2.12.0

### Custom Code

No

### OS Platform and Distribution

Android

### Mobile device

Android 13

### Python version

3.8.10

### Bazel version

_No response_

### GCC/Compiler version

_No response_

### CUDA/cuDNN version

_No response_

### GPU model and memory

_No response_

### Current Behaviour?

I am trying to run a modified resnet-50 (as follows) on Android platform using TFLite GPU Delegate, I already replaced unsupported GATHER ops with STRIDED_SLICE ops. It works fine on my Linux server, Android CPU mode but not able to run on Adroid GPU delegate.

As https://www.tensorflow.org/lite/performance/gpu said, STRIDED_SLICE should be a supported ops on GPU delegate...

Anything can shed some lights on what I can do here?

![image](https://github.com/tensorflow/tensorflow/assets/5018331/613a4b60-93f5-456b-9db3-18c55a8215e3)

==

Analyzer said:
Your model looks compatible with GPU delegate with TFLite runtime version 2.12.0.
But it doesn't guarantee that your model works well with GPU delegate.
There could be some runtime incompatibililty happen.
---------------------------------------------------------------
Your TFLite model has '1' signature_def(s).

==

22:25:53.970  W  Accessing hidden field Ljava/lang/Throwable;->detailMessage:Ljava/lang/String; (unsupported, reflection, allowed)
22:25:53.971  E  java.lang.IllegalArgumentException: Internal error: Failed to apply delegate: TfLiteGpuDelegate Init: STRIDED_SLICE: Output batch don't match
                 TfLiteGpuDelegate Prepare: delegate is not initialized
                 Node number 409 (TfLiteGpuDelegateV2) failed to prepare.
                 Restored original execution plan after delegate application failure.
22:25:53.971  E  	at org.tensorflow.lite.NativeInterpreterWrapper.createInterpreter(Native Method)
22:25:53.971  E  	at org.tensorflow.lite.NativeInterpreterWrapper.init(NativeInterpreterWrapper.java:110)
22:25:53.972  E  	at org.tensorflow.lite.NativeInterpreterWrapper.<init>(NativeInterpreterWrapper.java:58)
22:25:53.972  E  	at org.tensorflow.lite.NativeInterpreterWrapperExperimental.<init>(NativeInterpreterWrapperExperimental.java:32)
22:25:53.972  E  	at org.tensorflow.lite.Interpreter.<init>(Interpreter.java:184)


### Standalone code to reproduce the issue

```shell
// if the device has a supported GPU, add the GPU delegate
                GpuDelegate gpuDelegate = new GpuDelegate(
                    new GpuDelegate.Options()
                        .setPrecisionLossAllowed(false)
                        .setQuantizedModelsAllowed(false)
                );
                options.addDelegate(gpuDelegate);
                Log.d("ImageEncoder", "Device = GPU, hasGPU=" + hasGPU);

                // if the GPU is not supported, run on 4 threads as a backup plan
                options.setNumThreads(4);
                Log.d("ImageEncoder", "Device = CPU");

                interpreter = new Interpreter(file, options);


    implementation 'org.tensorflow:tensorflow-lite:2.12.0'
    implementation 'org.tensorflow:tensorflow-lite-api:2.10.0'
    implementation 'org.tensorflow:tensorflow-lite-gpu:2.12.0'
    implementation 'org.tensorflow:tensorflow-lite-gpu-api:2.12.0'
    implementation 'org.tensorflow:tensorflow-lite-gpu-delegate-plugin:0.4.3'
    implementation 'org.tensorflow:tensorflow-lite-select-tf-ops:2.12.0'
    implementation 'org.tensorflow:tensorflow-lite-support:0.4.2'

```


### Relevant log output

```shell
22:25:53.970  W  Accessing hidden field Ljava/lang/Throwable;->detailMessage:Ljava/lang/String; (unsupported, reflection, allowed)
22:25:53.971  E  java.lang.IllegalArgumentException: Internal error: Failed to apply delegate: TfLiteGpuDelegate Init: STRIDED_SLICE: Output batch don't match
                 TfLiteGpuDelegate Prepare: delegate is not initialized
                 Node number 409 (TfLiteGpuDelegateV2) failed to prepare.
                 Restored original execution plan after delegate application failure.
22:25:53.971  E  	at org.tensorflow.lite.NativeInterpreterWrapper.createInterpreter(Native Method)
22:25:53.971  E  	at org.tensorflow.lite.NativeInterpreterWrapper.init(NativeInterpreterWrapper.java:110)
22:25:53.972  E  	at org.tensorflow.lite.NativeInterpreterWrapper.<init>(NativeInterpreterWrapper.java:58)
22:25:53.972  E  	at org.tensorflow.lite.NativeInterpreterWrapperExperimental.<init>(NativeInterpreterWrapperExperimental.java:32)
22:25:53.972  E  	at org.tensorflow.lite.Interpreter.<init>(Interpreter.java:184)
```
</details>