TFLite GPU delegate: Broadcast output incorrect
Hi @MichaelTylerArm 

As per this [comment](https://github.com/tensorflow/tensorflow/issues/60043#issuecomment-1487821343), for broadcasting the values on GPU delegate, we need to be more explicit by restructuring the data.

Please check this similar issue #60043 and let us know if it helps.

Thanks. Hi @pjpratik ,

I've tried swapping the inputs to MUL so that the LHS tensor is the larger one as per that issue, but I get the same result.  Both tensors have 2 dimensions, and I believe the problem is well formed (the non-broadcast dimensions are equal and the smaller tensor has size 1 along the broadcast dimension).  That issue suggests avoiding GPU broadcast altogether, although I am unclear as to what an appropriate replacement would be.  I've tried replacing the multiply by 1 with add 0, but get the same result. @MichaelTylerArm Thanks for the information.

@pkgoogle Could you please look into this issue?

Thanks. Hi @sirakiin, can you please take a look? Thanks.  I've found the issue, on line 813 of `tensorflow/lite/delegates/gpu/common/selectors/operation_selector.cc`:
`Value* output = inputs[0];  // Should be outputs[0]`

If I make this change, my test case starts working. @sirakiin 
This issue should be resolved by changing that one line, would someone on the TensorFlow team be able to implement this please? Hi @MichaelTylerArm, we are actively working on it, it will take some time to past all tests/regressions/review. Thanks for your help! In the future please feel free to submit your own PR to contribute! We will review the changes. Hi @MichaelTylerArm the fix is in this commit: https://github.com/tensorflow/tensorflow/commit/ec0540a5a70ef22d3db915d198db22e296879f39 please try again with master or nightly and let us know if it is fixed. Hi @pkgoogle it appears to be working on master now, thanks for your help! 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/61594">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/61594">No</a>

### Issue type

Bug

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

Yes

### Source

source

### TensorFlow version

Nightly at 09cf1b2a39023e617e003a51be39d419702c2d36

### Custom code

Yes

### OS platform and distribution

Android 12 2023-03-01

### Mobile device

Vivo X80

### Python version

_No response_

### Bazel version

CMake 3.19.0

### GCC/compiler version

Android NDK r25

### CUDA/cuDNN version

_No response_

### GPU model and memory

Mali-G710 MC10

### Current behavior?

TFLite model file:  [model.tflite.zip](https://github.com/tensorflow/tensorflow/files/12360271/model.tflite.zip)

The provided TFLite model contains a single broadcast operation, which when executed by the provided C++ program, should produce the following output:

```
1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  
2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  
3  3  3  3  3  3  3  3  3  3  3  3  3  3  3  3  
4  4  4  4  4  4  4  4  4  4  4  4  4  4  4  4  
5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  
6  6  6  6  6  6  6  6  6  6  6  6  6  6  6  6  
7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  
8  8  8  8  8  8  8  8  8  8  8  8  8  8  8  8 
```

However, when using the GPU delegate the following output is produced:

```
1  0  0  1  1  0  0  1  1  0  0  1  1  0  0  1  
2  0  0  1  2  0  0  1  2  0  0  1  2  0  0  1  
3  0  0  1  3  0  0  1  3  0  0  1  3  0  0  1  
4  0  0  1  4  0  0  1  4  0  0  1  4  0  0  1  
5  0  0  1  5  0  0  1  5  0  0  1  5  0  0  1  
6  0  0  1  6  0  0  1  6  0  0  1  6  0  0  1  
7  0  0  1  7  0  0  1  7  0  0  1  7  0  0  1  
8  0  0  1  8  0  0  1  8  0  0  1  8  0  0  1 
```

The correct output is produced when using the CPU and not the GPU delegate.  We are concerned that this could be a security issue if memory is being accessed incorrectly.

Content of `model.tflite`:

```
Your TFLite model has '1' subgraph(s). In the subgraph description below,
T# represents the Tensor numbers. For example, in Subgraph#0, the MUL op takes
tensor #0 and tensor #1 as input and produces tensor #2 as output.

Subgraph#0 main(T#0) -> [T#2]
  Op#0 MUL(T#0, T#1) -> [T#2]

Tensors of Subgraph#0
  T#0(serving_default_input:0) shape:[8, 1], type:FLOAT32
  T#1(BroadcastTo) shape:[8, 16], type:FLOAT32 RO 512 bytes, buffer: 2, data:[1, 1, 1, 1, 1, ...]
  T#2(PartitionedCall:0) shape:[8, 16], type:FLOAT32

---------------------------------------------------------------
Your TFLite model has '1' signature_def(s).

Signature#0 key: 'serving_default'
- Subgraph: Subgraph#0
- Inputs: 
    'input' : T#0
- Outputs: 
    'output' : T#2

---------------------------------------------------------------
              Model size:       1400 bytes
    Non-data buffer size:        780 bytes (55.71 %)
  Total data buffer size:        620 bytes (44.29 %)
    (Zero value buffers):          0 bytes (00.00 %)
```

### Standalone code to reproduce the issue

```shell
#include <memory>
#include <stdio.h>

#include "tensorflow/lite/kernels/register.h"
#include "tensorflow/lite/model.h"
#include "tensorflow/lite/delegates/gpu/delegate.h"

int main() {
    std::unique_ptr<tflite::FlatBufferModel> model =
        tflite::FlatBufferModel::BuildFromFile("model.tflite");
    
    tflite::ops::builtin::BuiltinOpResolver resolver;
    tflite::InterpreterBuilder builder(*model, resolver);
    std::unique_ptr<tflite::Interpreter> interpreter;
    builder(&interpreter);

    TfLiteGpuDelegateOptionsV2 options = TfLiteGpuDelegateOptionsV2Default();
    auto* delegate = TfLiteGpuDelegateV2Create(&options);
    if (interpreter->ModifyGraphWithDelegate(delegate) != kTfLiteOk) return 1;

    const TfLiteTensor *inTensor = interpreter->input_tensor(0);
    const TfLiteIntArray *inShape = inTensor->dims;
    float *input = inTensor->data.f;

    for (int i = 0; i < inShape->data[0]; i++) {
        input[i] = i + 1;
    }

    if (interpreter->Invoke() != kTfLiteOk) return 1;

    std::vector<unsigned long> outShapeVec;
    const TfLiteTensor *outTensor = interpreter->output_tensor(0);
    const TfLiteIntArray *outShape = outTensor->dims;
    const float *output = outTensor->data.f;

    const float *outPtr = output;
    for (size_t i = 0; i < outShape->data[0]; i++) {
        for (size_t j = 0; j < outShape->data[1]; j++) {
            printf("%.2g  ", *(outPtr++));
        }
        printf("\n");
    }
}
```


### Relevant log output

_No response_