Working code broke after deploying to new installation. ValueError: When using `stateful=True` in a RNN, the batch size must be static. Found dynamic batch size: sequence.shape=(None, xx, xx)
Hi **@summa-code** ,
- Ensure that your input data has a static batch size. When using stateful=True, the batch size should not change between batches. You can achieve this by padding or truncating sequences to a fixed length.
- When defining your LSTM layer, set stateful=True and specify the batch_input_shape parameter with a fixed batch size and sequence length.
example:
```
model.add(LSTM(units=64, batch_input_shape=(batch_size, sequence_length, input_dim), stateful=True))
```
- If you are using timeseries_dataset_from_array to prepare your data, make sure that all sequences have the same length. You can achieve this by setting the sequence_length parameter when creating the dataset.
example:
```
dataset = timeseries_dataset_from_array(data, targets, sequence_length=desired_length, batch_size=batch_size)
```

Thank you! So that was the working code, but after I updated to latest in fresh ubuntu install, "batch_input_shape" not a recognized attribute. 
This is what I am getting

```ValueError: Unrecognized keyword arguments passed to LSTM: {'batch_input_shape': (xxx, xxx, xxx)}```

And yes, I am using sequence_length, please refer to timeseries example on this Tensorflow site.
https://www.tensorflow.org/tutorials/structured_data/time_series Facing the exact same issue with `tensorflow 2.16.1` and `Python 3.9.16` Looks like either it is a bug or they got rid of the function, but either way, it is like a bug in how they configure the shape internally for statefulness. And also, there are no examples on how to use stateful LSTM in addition to lack of documentation on statefulness. Anyone?  Try using the [input layer](https://www.tensorflow.org/api_docs/python/tf/keras/layers/InputLayer) instead of batch_input_shape. 

`InputLayer(batch_shape=(1,None,4))` If I add a input layer before LSTM layer, I get this,

File "/.local/lib/python3.11/site-packages/keras/src/utils/traceback_utils.py", line 122, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File ".local/lib/python3.11/site-packages/tensorflow/python/eager/execute.py", line 53, in quick_execute
    tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
tensorflow.python.framework.errors_impl.InvalidArgumentError: Graph execution error:

Detected at node sequential_1/lstm_0_1/CudnnRNN defined at (most recent call last):

  File "/.local/lib/python3.11/site-packages/keras/src/utils/traceback_utils.py", line 117, in error_handler

  File "/.local/lib/python3.11/site-packages/keras/src/backend/tensorflow/trainer.py", line 325, in fit

  File "/.local/lib/python3.11/site-packages/keras/src/backend/tensorflow/trainer.py", line 118, in one_step_on_iterator

  File "/.local/lib/python3.11/site-packages/keras/src/backend/tensorflow/trainer.py", line 106, in one_step_on_data

  File "/.local/lib/python3.11/site-packages/keras/src/backend/tensorflow/trainer.py", line 57, in train_step

  File "/.local/lib/python3.11/site-packages/keras/src/utils/traceback_utils.py", line 117, in error_handler

  File "/.local/lib/python3.11/site-packages/keras/src/layers/layer.py", line 814, in __call__

  File "/.local/lib/python3.11/site-packages/keras/src/utils/traceback_utils.py", line 117, in error_handler

  File "/.local/lib/python3.11/site-packages/keras/src/ops/operation.py", line 48, in __call__

  File "/.local/lib/python3.11/site-packages/keras/src/utils/traceback_utils.py", line 156, in error_handler

  File "/.local/lib/python3.11/site-packages/keras/src/models/sequential.py", line 202, in call

  File "/.local/lib/python3.11/site-packages/keras/src/models/functional.py", line 194, in call

  File "/.local/lib/python3.11/site-packages/keras/src/ops/function.py", line 151, in _run_through_graph

  File "/.local/lib/python3.11/site-packages/keras/src/models/functional.py", line 578, in call

  File "/.local/lib/python3.11/site-packages/keras/src/utils/traceback_utils.py", line 117, in error_handler

  File "/.local/lib/python3.11/site-packages/keras/src/layers/layer.py", line 814, in __call__

  File "/.local/lib/python3.11/site-packages/keras/src/utils/traceback_utils.py", line 117, in error_handler

  File "/.local/lib/python3.11/site-packages/keras/src/ops/operation.py", line 48, in __call__

  File "/.local/lib/python3.11/site-packages/keras/src/utils/traceback_utils.py", line 156, in error_handler

  File "/.local/lib/python3.11/site-packages/keras/src/layers/rnn/lstm.py", line 537, in call

  File "/.local/lib/python3.11/site-packages/keras/src/layers/rnn/rnn.py", line 398, in call

  File "/.local/lib/python3.11/site-packages/keras/src/layers/rnn/lstm.py", line 511, in inner_loop

  File "/.local/lib/python3.11/site-packages/keras/src/backend/tensorflow/rnn.py", line 823, in lstm

  File "/.local/lib/python3.11/site-packages/keras/src/backend/tensorflow/rnn.py", line 934, in _cudnn_lstm

Invalid input_h shape: [1,512,48] [1,512,48]
	 [[{{node sequential_1/lstm_0_1/CudnnRNN}}]] [Op:__inference_one_step_on_iterator_4048]
 Hi @summa-code ,
- Apologies for the dealy, In order to expedite the trouble-shooting process, please provide a code snippet to reproduce the issue reported here.

Thank you!
 
### Issue type

Bug

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

Yes

### Source

source

### TensorFlow version

2.16.1

### Custom code

No

### OS platform and distribution

Ubuntu 22.04

### Mobile device

No

### Python version

3.10.12

### Bazel version

_No response_

### GCC/compiler version

_No response_

### CUDA/cuDNN version

12.4

### GPU model and memory

_No response_

### Current behavior?

So the existing code broke, with this error

ValueError: When using `stateful=True` in a RNN, the batch size must be static. Found dynamic batch size: sequence.shape=(None, XX, XX), 

but when changed to (XX, XX, XX) I get this, ValueError: Input 0 of layer "lstm" is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: (None, 1, XX, XX)

If static shape is passed, not sure why additional dimension is added. Need help in resolving this. I tried both the release version and nightly version. The issue persists

And also the input is 3D tensor produced by timeseries_dataset_from_array function. Is there any example on training stateful LSTM using these 3D tensors? Not sure if this is a bug really. I am stuck.

### Standalone code to reproduce the issue

```shell
Dont have stand alone.
```


### Relevant log output

_No response_