Correct way to implement RNN if looping over tensor is yet not allowed
**use **tf.cond** to conditionally execute the loop based on the input length**
In the code, the for loop is iterating over a **tf.Tensor** object created by **tf.range(tf.shape(inputs)[1])**. This tensor has a symbolic shape, which means that its size is not known until runtime. TensorFlow's AutoGraph cannot handle iterating over such symbolic tensors, as it requires knowing the size of the tensor at graph construction time.

```
import tensorflow as tf

class MyModel1(tf.keras.Model):
    def __init__(self, input_text_processor, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.input_text_processor = input_text_processor

    def call(self, data, training=True, max_len=50):
        inputs = self.input_text_processor(data)
        def loop_body(i):
            return i+1
        i = tf.constant(0)
        cond = lambda i: tf.less(i, tf.shape(inputs)[1])
        i = tf.cond(tf.less(tf.shape(inputs)[1], max_len), lambda: tf.shape(inputs)[1], lambda: max_len)
        tf.while_loop(cond, loop_body, [i])
        return []

    def train_step(self, data):
        self.call(data)
        return {"loss" : 0}

dataset = [
    "hi", "what's up", "what's the weather"
]
input_text_processor = tf.keras.layers.TextVectorization()
input_text_processor.adapt(dataset)

with tf.device("/CPU:0"):
    model = MyModel1(input_text_processor)
    model.compile(tf.optimizers.Adam(), loss=tf.keras.losses.SparseCategoricalCrossentropy())
    hist = model.fit(dataset, epochs=5)
```
### OUTPUT
```
Epoch 1/5
1/1 [==============================] - 0s 471ms/step - loss: 0.0000e+00
Epoch 2/5
1/1 [==============================] - 0s 18ms/step - loss: 0.0000e+00
Epoch 3/5
1/1 [==============================] - 0s 49ms/step - loss: 0.0000e+00
Epoch 4/5
1/1 [==============================] - 0s 13ms/step - loss: 0.0000e+00
Epoch 5/5
1/1 [==============================] - 0s 18ms/step - loss: 0.0000e+00
``` thank you so much, finally I was able to do it with your hint... please consider adding more examples in the docs for `tf.while_loop`, in order to make it work I had to fight for a while with `shape_invariants` and what to do if I want more than one input for the body of the loop... Also, I would like to ask what should I use in the `shape_invariants` if I have a list of vector (say for example the state of a LSTM, which is [h_state, m_state])


Anyway, thank you so much @AlbertoSinigaglia It's my pleasure to help.
If you have a list of vectors as the state of a LSTM in TensorFlow, you can use a tuple of TensorShape objects in the **shape_invariants** argument of the **tf.function** decorator to specify the shape of each vector.
 @pat749  that makes total sense, thank you so much 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/60153">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/60153">No</a>

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

Support

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

No

### Source

source

### Tensorflow Version

2.11

### Custom Code

No

### OS Platform and Distribution

MaxOS 12.3.1 

### Mobile device

-

### Python version

3.10

### Bazel version

_No response_

### GCC/Compiler version

_No response_

### CUDA/cuDNN version

_No response_

### GPU model and memory

M1 Max 26 cores GPU 32 GB ram

### Current Behaviour?

```shell
it throws : Iterating over a symbolic `tf.Tensor` is not allowed: AutoGraph did convert this function. This might indicate you are trying to use an unsupported feature. yet the code in my opinion can be easily converted to a graph, just conditioned on the `tf.shape(inputs)[1]`... this would allow building RNNs that accepts long and short inputs, without having to pad it, which is extremely useful if the length of the strings in the dataset is very skewed, and we have many short phrases and few long ones
```


### Standalone code to reproduce the issue

```shell
import tensorflow as tf
class MyModel1(tf.keras.Model):
    def __init__(self, input_text_processor, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.input_text_processor = input_text_processor
    def call(self, data, training=True, max_len=50):
        inputs = self.input_text_processor(data)
        for _ in tf.range(tf.shape(inputs)[1]):
            pass
        return []

    def train_step(self, data):
        self.call(data)
        return {"loss" : 0}
dataset = [
    "hi", "what's up", "what's the weather"
]
input_text_processor = tf.keras.layers.TextVectorization()
input_text_processor.adapt(dataset)
with tf.device("/CPU:0"):
    model = MyModel1(input_text_processor)
    model.compile(tf.optimizers.Adam(), loss=tf.keras.losses.SparseCategoricalCrossentropy())
    hist = model.fit(dataset, epochs=5)
```


### Relevant log output

```shell
Epoch 1/5
Traceback (most recent call last):
  File "/Users/username/ml/tensorflow-journey/39-rnn-enc-dec-attention/rnn-enc-dec-attention.py", line 23, in <module>
    hist = model.fit(dataset, epochs=5)
  File "/opt/homebrew/Caskroom/miniforge/base/envs/ml-apple-metal-3-10/lib/python3.10/site-packages/keras/utils/traceback_utils.py", line 70, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "/opt/homebrew/Caskroom/miniforge/base/envs/ml-apple-metal-3-10/lib/python3.10/site-packages/tensorflow/python/framework/func_graph.py", line 1269, in autograph_handler
    raise e.ag_error_metadata.to_exception(e)
tensorflow.python.framework.errors_impl.OperatorNotAllowedInGraphError: in user code:

    File "/opt/homebrew/Caskroom/miniforge/base/envs/ml-apple-metal-3-10/lib/python3.10/site-packages/keras/engine/training.py", line 1249, in train_function  *
        return step_function(self, iterator)
    File "/opt/homebrew/Caskroom/miniforge/base/envs/ml-apple-metal-3-10/lib/python3.10/site-packages/keras/engine/training.py", line 1233, in step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "/opt/homebrew/Caskroom/miniforge/base/envs/ml-apple-metal-3-10/lib/python3.10/site-packages/keras/engine/training.py", line 1222, in run_step  **
        outputs = model.train_step(data)
    File "/Users/username/ml/tensorflow-journey/39-rnn-enc-dec-attention/rnn-enc-dec-attention.py", line 13, in train_step
        self.call(data)
    File "/Users/username/ml/tensorflow-journey/39-rnn-enc-dec-attention/rnn-enc-dec-attention.py", line 8, in call
        for _ in tf.range(tf.shape(inputs)[1]):

    OperatorNotAllowedInGraphError: Iterating over a symbolic `tf.Tensor` is not allowed: AutoGraph did convert this function. This might indicate you are trying to use an unsupported feature.
```
</details>