NNAPI delegate crashes on kTfLiteBuiltinCos in TF Lite 2.14
Hi @brtal 

Thanks for the finding. 

Is the error reproducible in the nightly pull as well?

Are you interested in creating a PR for this?

Thanks.

 Yes it repros in nightly.

```
o1q:/data/local/tmp $ ./benchmark_model_nightly --use_nnapi=true --graph=models/nnapi_cos_bug.tflite                                                                                                  
INFO: STARTING!
INFO: Log parameter values verbosely: [0]
INFO: Graph: [models/nnapi_cos_bug.tflite]
INFO: Use NNAPI: [1]
INFO: NNAPI accelerators available: [qti-default,qti-dsp,qti-gpu,nnapi-reference]
INFO: Loaded model models/nnapi_cos_bug.tflite
INFO: Initialized TensorFlow Lite runtime.
INFO: Created TensorFlow Lite delegate for NNAPI.
INFO: NNAPI delegate created.
WARNING: NNAPI SL driver did not implement SL_ANeuralNetworksDiagnostic_registerCallbacks!
Segmentation fault 
```

Our group does have some patches we would like to contribute back to tensorflow but we don't have any contacts at Google to help coordinate with on this. We would love to make some contacts so we can help facilitate these.

In the case of this patch, we don't have a fix for it, so I will decline to put up a PR. A proper fix is more involved and I don't have one prepared and don't really have the time at the moment. If NNAPI doesn't support `cos` natively, then it should be mapped to multiple NNAPI ops to translate it into a `sin`. However, in the immediate term, the [breaking change](https://github.com/tensorflow/tensorflow/commit/4aac8c95b7d7827eedca82a76cb71db1525dafc9) should simply be reverted.

Thanks. @brtal Thanks for the information.

@pkgoogle Could you please look into this issue?

Thanks. I was able to reproduce with a Pixel 4 API 33 emulator: 
[Test62091.zip](https://github.com/tensorflow/tensorflow/files/12922233/Test62091.zip)

![image](https://github.com/tensorflow/tensorflow/assets/132095473/6ee0c274-afb0-4713-b8ea-be637ae518dc)

I wasn't able to reproduce on a general linux (debian) benchmark_model build that had --use_nnapi=true

```sh
./benchmark_model --use_nnapi=true --graph=nnapi_cos_bug.tflite
INFO: STARTING!
WARN: Unconsumed cmdline flags: --use_nnapi=true
INFO: Log parameter values verbosely: [0]
INFO: Graph: [nnapi_cos_bug.tflite]
INFO: Loaded model nnapi_cos_bug.tflite
INFO: Created TensorFlow Lite XNNPACK delegate for CPU.
INFO: The input model file size (MB): 0.00066
INFO: Initialized session in 1.257ms.
INFO: Running benchmark for at least 1 iterations and at least 0.5 seconds but terminate if exceeding 150 seconds.
INFO: count=2873432 first=14 curr=1 min=0 max=64 avg=0.0808256 std=0

INFO: Running benchmark for at least 50 iterations and at least 1 seconds but terminate if exceeding 150 seconds.
INFO: count=5724139 first=1 curr=0 min=0 max=70 avg=0.0812042 std=0

INFO: Inference timings in us: Init: 1257, First inference: 14, Warmup (avg): 0.0808256, Inference (avg): 0.0812042
INFO: Note: as the benchmark tool itself affects memory footprint, the following is only APPROXIMATE to the actual memory footprint of the model at runtime. Take the information at your discretion.
INFO: Memory footprint delta from the start of the tool (MB): init=2.625 overall=3
```

Hi @arfaian, can you please take a look? Thanks. @pkgoogle fyi - this won't fail on debian because it's an NNAPI-specific failure, which only exists on Android. Note from your `benchmark_model` output, the model is running on XNNPACK not NNAPI.

This issue is really straight forward. Just revert the broken commit. :) thanks. Hi @brtal, that commit was there for a reason, so just reverting it will likely cause a regression.. that being said the original author might be available, @freedomtan, would you have any insight on how we may fix your commit to resolve both cases? Thanks for any help you can provide. @pkgoogle and @brtal the cos op works fine on my Pixel 7 Pro. I guess it's an NNAPI API level issue. The math `sin` op, which I used to provide `cos` op, in NNAPI is not available before feature level 3. @freedomtan - respectful, the issue is not in NNAPI, it is in the delegate code modified in the originating change. Specifically, it crashes in `TransformCosIntoSupportedOps` because the `TfLiteTensor` being modified has a null data pointer (because the tensor hasn't been allocated yet). Tensor allocation is meant to happen after delegates have modified the graph.

Moreover, as per my original description, I don't believe it's appropriate to mutate input data at this stage.

I suggest you try the `benchmark_model` repro that @pkgoogle supplies above on your device. > @freedomtan - respectful, the issue is not in NNAPI, it is in the delegate code modified in the originating change. Specifically, it crashes in `TransformCosIntoSupportedOps` because the `TfLiteTensor` being modified has a null data pointer (because the tensor hasn't been allocated yet). Tensor allocation is meant to happen after delegates have modified the graph.
> 
> Moreover, as per my original description, I don't believe it's appropriate to mutate input data at this stage.
> 
> I suggest you try the `benchmark_model` repro that @pkgoogle supplies above on your device.

That's interesting. Thanks for the information. I'll check it out. BTW, as far as I can remember. I have unit test for the `cos` op in `nnapi_delegate_test.cc`. I'll check the unit test first, and then check the tensor allocation issue you mentioned. @brtal: you are right. I fixed it in #62139. 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/62091">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/62091">No</a>

### Issue type

Bug (segfault!)

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

No

### Source

source

### TensorFlow version

2.14

### Custom code

No

### OS platform and distribution

Android 13

### Mobile device

Pixel 4

### Python version

_No response_

### Bazel version

_No response_

### GCC/compiler version

_No response_

### CUDA/cuDNN version

_No response_

### GPU model and memory

_No response_

### Current behavior?

In TF Lite 2.14, the NNAPI delegate was changed to support the cos operator. Unfortunately, it does not work as expected and causes a segfault.

The originating change is https://github.com/tensorflow/tensorflow/commit/4aac8c95b7d7827eedca82a76cb71db1525dafc9.

The repro is easy. Use the below network and run with NNAPI.

The error is in `TransformCosIntoSupportedOps`, which makes the assumption that `theta` is non-null. This causes a null-pointer dereference.

But the problem is bigger than this - the NNAPI delegate should _not_ be modifying static data. This is dangerous and will lead to problems if the node is not accepted by the delegate, or if the execution graph is modified again to use a different delegate.

### Standalone code to reproduce the issue

```shell
import tensorflow as tf

class MyModel(tf.Module):
    @tf.function(input_signature=[tf.TensorSpec(shape=[None], dtype=tf.float32)])
    def my_operation(self, x):
        return tf.cos(x)

model = MyModel()

concrete_func = model.my_operation.get_concrete_function()
converter = tf.lite.TFLiteConverter.from_concrete_functions([concrete_func])
tflite_model = converter.convert()

with open("nnapi_cos_bug.tflite", "wb") as f:
    f.write(tflite_model)
```

Convert to tflite and load using NNAPI. The application will segfault.
```


### Relevant log output

_No response_