Internal error on Attention over LSTM with ragged input
Hi @diogoff, I was able to replicate the issue in Colab using TF v2.1. Please find the gist [here](https://colab.sandbox.google.com/gist/synandi/7e784fe9c6f992441574968e3d9c153d/60140.ipynb).

As a workaround, if you don't want to keep the output of the LSTM layer at each timestep, you can set `return_sequences=False` in the LSTM layer, it will output only the last hidden state of the LSTM layer, which is a fixed-size vector.  Kindly refer to the gist of working code [here](https://colab.sandbox.google.com/gist/synandi/251766ce5404da9dfba77606cf529e7d/60140_1.ipynb). Thank you!
 For my application, I need to keep the outputs of the LSTM at each time step.
Also, note that with `return_sequences=False` the output shape does not appear to conform to the input shapes required for Attention:

- query tensor of shape `[batch_size, Tq, dim]`
- value tensor of shape `[batch_size, Tv, dim]`

Finally, note that it runs fine if we skip the LSTM layer. In this case, we are providing all the timesteps to Attention. So I don't see a reason why it shouldn't work after the LSTM as well. Hi @diogoff ,

The `keras.layers.Attention` layer supposed to be used with Dense or CNN networks but not for RNN networks.

For source please refer the source code below.Attention class sub-classes the `BaseDenseAttention` class.

https://github.com/keras-team/keras/blob/f9336cc5114b4a9429a242deb264b707379646b7/keras/layers/attention/attention.py#L30-L31

And the build method of the attention class calls `super().build(input_shape)` as per source below.

https://github.com/keras-team/keras/blob/f9336cc5114b4a9429a242deb264b707379646b7/keras/layers/attention/attention.py#L169

From the `BaseDenseAttention` class there is a note saying this would be used for CNN or Dense but not for RNN.

https://github.com/keras-team/keras/blob/f9336cc5114b4a9429a242deb264b707379646b7/keras/layers/attention/base_dense_attention.py#L32-L36

Is that make sense for why we are getting the error ? Please check and come back. Thanks!

 Although I confirm what you say, I don't think the problem has to do with RNN vs. CNN. I think the problem has to do with ragged vs. non-ragged input.
To show this, I've prepared this sample code:
```
import numpy as np
import tensorflow as tf

# -----------------------------------------------------------------------------

samples = np.array([[[0.1], [0.2], [0.3]],
                    [[0.4], [0.5], [0.6]],
                    [[0.7], [0.8], [0.9]]], dtype=np.float32)

targets = np.array([[0.25],
                    [0.50],
                    [0.75]], dtype=np.float32)

# -----------------------------------------------------------------------------

layer_1 = tf.keras.layers.Input(shape=(None, 1), dtype=tf.float32)

layer_2 = tf.keras.layers.LSTM(1, return_sequences=True)(layer_1)

layer_3 = tf.keras.layers.Attention()([layer_2, layer_2])

layer_4 = tf.keras.layers.Lambda(lambda x: tf.reduce_sum(x, axis=1))(layer_3)

model = tf.keras.models.Model([layer_1], [layer_4])

model.summary()

# -----------------------------------------------------------------------------

model.compile(optimizer='adam', loss='mse')

model.fit(samples, targets, batch_size=3, epochs=1, verbose=1)
```
This runs without issue. So why shouldn't it run with ragged input? Hi @diogoff ,

Thanks for pointing the Ragged Tensor Input. The error is arising from calculating gradients for a Ragged Tensor on GPU device. This support is not enabled yet which is still under development stage. Please refer to the code below.The Op is enabled for CPU only now.

https://github.com/tensorflow/tensorflow/blob/25ab98cf17d86a1e2d6fb0c8172f2c8a9ecde110/tensorflow/core/kernels/ragged_tensor_to_variant_op.cc#L343-L354


Workaround is to use CPU runtime when used Ragged tensor within a model. With CPU only runtime the code executes fine.Please refer the attached [gist](https://colab.research.google.com/gist/SuryanarayanaY/37c871eb850c1ccd92265a1f360642c5/60140.ipynb). 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/60140">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/60140">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.11.0

### Custom Code

Yes

### OS Platform and Distribution

Ubuntu 22.04 LTS on Windows WSL2

### Mobile device

_No response_

### Python version

3.10

### Bazel version

_No response_

### GCC/Compiler version

_No response_

### CUDA/cuDNN version

11.7/8.5

### GPU model and memory

_No response_

### Current Behaviour?

```shell
No problem if LSTM layer is skipped. However, if LSTM (or GRU) layer is included, got an internal error.
```


### Standalone code to reproduce the issue

```shell
import numpy as np
import tensorflow as tf

# -----------------------------------------------------------------------------

samples = [[[0.1], [0.2], [0.3]],
           [[0.4], [0.5], [0.6], [0.7]],
           [[0.8], [0.9]]]

targets = np.array([[0.25], [0.50], [0.75]], dtype=np.float32)

# -----------------------------------------------------------------------------

row_lengths = [len(s) for s in samples]

samples = np.concatenate(samples, axis=0)

samples = tf.RaggedTensor.from_row_lengths(samples, row_lengths)

# -----------------------------------------------------------------------------

layer_1 = tf.keras.layers.Input(shape=(None, 1), dtype=tf.float32, ragged=True)

layer_2 = tf.keras.layers.LSTM(1, return_sequences=True)(layer_1)

layer_3 = tf.keras.layers.Attention()([layer_2, layer_2])

layer_4 = tf.keras.layers.Lambda(lambda x: tf.reduce_sum(x, axis=1))(layer_3)

model = tf.keras.models.Model([layer_1], [layer_4])

model.summary()

# -----------------------------------------------------------------------------

model.compile(optimizer='adam', loss='mse')

model.fit(samples, targets, batch_size=3, epochs=1, verbose=1)
```


### Relevant log output

```shell
Traceback (most recent call last):
  File "/home/user/model.py", line 38, in <module>
    model.fit(samples, targets, batch_size=3, epochs=1, verbose=1)
  File "/usr/local/lib/python3.10/dist-packages/keras/utils/traceback_utils.py", line 70, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "/usr/local/lib/python3.10/dist-packages/tensorflow/python/eager/execute.py", line 52, in quick_execute
    tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
tensorflow.python.framework.errors_impl.InternalError: Graph execution error:

Detected at node 'zeros_like_5' defined at (most recent call last):
    File "/home/user/model.py", line 38, in <module>
      model.fit(samples, targets, batch_size=3, epochs=1, verbose=1)
    File "/usr/local/lib/python3.10/dist-packages/keras/utils/traceback_utils.py", line 65, in error_handler
      return fn(*args, **kwargs)
    File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 1650, in fit
      tmp_logs = self.train_function(iterator)
    File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 1249, in train_function
      return step_function(self, iterator)
    File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 1233, in step_function
      outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 1222, in run_step
      outputs = model.train_step(data)
    File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 1027, in train_step
      self.optimizer.minimize(loss, self.trainable_variables, tape=tape)
    File "/usr/local/lib/python3.10/dist-packages/keras/optimizers/optimizer_experimental/optimizer.py", line 526, in minimize
      grads_and_vars = self.compute_gradients(loss, var_list, tape)
    File "/usr/local/lib/python3.10/dist-packages/keras/optimizers/optimizer_experimental/optimizer.py", line 259, in compute_gradients
      grads = tape.gradient(loss, var_list)
Node: 'zeros_like_5'
Detected at node 'zeros_like_5' defined at (most recent call last):
    File "/home/user/model.py", line 38, in <module>
      model.fit(samples, targets, batch_size=3, epochs=1, verbose=1)
    File "/usr/local/lib/python3.10/dist-packages/keras/utils/traceback_utils.py", line 65, in error_handler
      return fn(*args, **kwargs)
    File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 1650, in fit
      tmp_logs = self.train_function(iterator)
    File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 1249, in train_function
      return step_function(self, iterator)
    File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 1233, in step_function
      outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 1222, in run_step
      outputs = model.train_step(data)
    File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 1027, in train_step
      self.optimizer.minimize(loss, self.trainable_variables, tape=tape)
    File "/usr/local/lib/python3.10/dist-packages/keras/optimizers/optimizer_experimental/optimizer.py", line 526, in minimize
      grads_and_vars = self.compute_gradients(loss, var_list, tape)
    File "/usr/local/lib/python3.10/dist-packages/keras/optimizers/optimizer_experimental/optimizer.py", line 259, in compute_gradients
      grads = tape.gradient(loss, var_list)
Node: 'zeros_like_5'
2 root error(s) found.
  (0) INTERNAL:  No unary variant unary_op function found for op ZEROS_LIKE Variant type_name: RaggedTensorVariant for device type: GPU
         [[{{node zeros_like_5}}]]
         [[model/attention/RaggedSoftmax/RowPartitionFromRowLengths_2/assert_non_negative/assert_less_equal/Assert/AssertGuard/pivot_f/_168/_379]]
  (1) INTERNAL:  No unary variant unary_op function found for op ZEROS_LIKE Variant type_name: RaggedTensorVariant for device type: GPU
         [[{{node zeros_like_5}}]]
0 successful operations.
0 derived errors ignored. [Op:__inference_train_function_10894]
```
</details>