Repository navigation
Closed
Labels
Description
Issue type
Bug
Have you reproduced the bug with TensorFlow Nightly?
Yes
Source
source
TensorFlow version
tf-2.21
Custom code
Yes
OS platform and distribution
No response
Mobile device
No response
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?
I encountered a bug in TensorFlow when I used the API tf.raw_ops.BlockLSTMGrad with an input tensor where one of the spatial dimensions (batch size) is set to zero. Also, tf.raw_ops.LSTMBlockCellGrad crashes when the input tensor x is provided as an empty tensor. The code is as follows.
Standalone code to reproduce the issue
import os
os.environ['TF_ENABLE_ONEDNN_OPTS'] = '1'
import random
import numpy as np
import tensorflow as tf
random.seed(42)
np.random.seed(42)
tf.random.set_seed(42)
import tensorflow as tf
import numpy as np
# Set random seed for reproducibility
tf.random.set_seed(42)
# Define input parameters
seq_len_max = 3
batch_size = 2
input_size = 4
num_units = 5
# Create random input tensors with one spatial dimension set to zero
x = tf.random.normal([seq_len_max, 0, input_size], dtype=tf.float32) # Mutated here
# x = tf.constant([], dtype=tf.float32)
cs_prev = tf.random.normal([batch_size, num_units], dtype=tf.float32)
h_prev = tf.random.normal([batch_size, num_units], dtype=tf.float32)
w = tf.random.normal([input_size + num_units, 4 * num_units], dtype=tf.float32)
wci = tf.random.normal([num_units], dtype=tf.float32)
wcf = tf.random.normal([num_units], dtype=tf.float32)
wco = tf.random.normal([num_units], dtype=tf.float32)
b = tf.random.normal([4 * num_units], dtype=tf.float32)
# Create forward LSTM outputs for gradient computation
i = tf.random.normal([seq_len_max, batch_size, num_units], dtype=tf.float32)
cs = tf.random.normal([seq_len_max, batch_size, num_units], dtype=tf.float32)
f = tf.random.normal([seq_len_max, batch_size, num_units], dtype=tf.float32)
o = tf.random.normal([seq_len_max, batch_size, num_units], dtype=tf.float32)
ci = tf.random.normal([seq_len_max, batch_size, num_units], dtype=tf.float32)
co = tf.random.normal([seq_len_max, batch_size, num_units], dtype=tf.float32)
h = tf.random.normal([seq_len_max, batch_size, num_units], dtype=tf.float32)
# Create gradient outputs
cs_grad = tf.random.normal([seq_len_max, batch_size, num_units], dtype=tf.float32)
h_grad = tf.random.normal([seq_len_max, batch_size, num_units], dtype=tf.float32)
# Use forget bias
forget_bias = 1.0
cell_clip = 0.0
use_peephole = True
# Invoke BlockLSTMGrad
result = tf.raw_ops.BlockLSTMGrad(
seq_len_max=seq_len_max,
x=x,
cs_prev=cs_prev,
h_prev=h_prev,
w=w,
wci=wci,
wcf=wcf,
wco=wco,
b=b,
i=i,
cs=cs,
f=f,
o=o,
ci=ci,
co=co,
h=h,
cs_grad=cs_grad,
h_grad=h_grad,
use_peephole=use_peephole
)
# Execute to ensure no errors
print("BlockLSTMGrad executed successfully")
print(f"Number of outputs: {len(result)}")
import os
os.environ['TF_ENABLE_ONEDNN_OPTS'] = '1'
import random
import numpy as np
import tensorflow as tf
random.seed(42)
np.random.seed(42)
tf.random.set_seed(42)
import tensorflow as tf
batch_size = 2
input_size = 2
cell_size = 2
x = tf.constant([], dtype=tf.float32)
cs_prev = tf.constant([[0.1, 0.2], [0.3, 0.4]], dtype=tf.float32)
h_prev = tf.constant([[0.5, 0.6], [0.7, 0.8]], dtype=tf.float32)
w = tf.constant([[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8],
[0.9, 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6],
[1.7, 1.8, 1.9, 2.0, 2.1, 2.2, 2.3, 2.4],
[2.5, 2.6, 2.7, 2.8, 2.9, 3.0, 3.1, 3.2]], dtype=tf.float32)
wci = tf.constant([0.01, 0.02], dtype=tf.float32)
wcf = tf.constant([0.03, 0.04], dtype=tf.float32)
wco = tf.constant([0.05, 0.06], dtype=tf.float32)
b = tf.constant([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8], dtype=tf.float32)
i = tf.constant([[0.9, 0.8], [0.7, 0.6]], dtype=tf.float32)
cs = tf.constant([[1.1, 1.2], [1.3, 1.4]], dtype=tf.float32)
f = tf.constant([[0.5, 0.4], [0.3, 0.2]], dtype=tf.float32)
o = tf.constant([[0.6, 0.5], [0.4, 0.3]], dtype=tf.float32)
ci = tf.constant([[0.8, 0.7], [0.6, 0.5]], dtype=tf.float32)
co = tf.constant([[0.9, 0.8], [0.7, 0.6]], dtype=tf.float32)
cs_grad = tf.constant([[0.01, 0.02], [0.03, 0.04]], dtype=tf.float32)
h_grad = tf.constant([[0.05, 0.06], [0.07, 0.08]], dtype=tf.float32)
use_peephole = True
result = tf.raw_ops.LSTMBlockCellGrad(
x=x,
cs_prev=cs_prev,
h_prev=h_prev,
w=w,
wci=wci,
wcf=wcf,
wco=wco,
b=b,
i=i,
cs=cs,
f=f,
o=o,
ci=ci,
co=co,
cs_grad=cs_grad,
h_grad=h_grad,
use_peephole=use_peephole
)
print(result)Relevant log output
crash
Activity
dhantule commentedon Mar 20, 2026
@Venkat6871,
I have reproduced this issue with TF 2.22.0 and TF Nightly, please refer this attached gist.
Kayyuri commentedon Aug 24, 2026
Hi @KnightGOKU,
Thank you so much for filing this issue! We truly appreciate your support. At the moment, this request is not prioritized and mark it for community contributions. If you would like to contribute on a fix, please feel free to fork the repository and submit a pull request in accordance with the TensorFlow CONTRIBUTION GUIDELINES and link this issue in your PR to ensure the maintainers can easily review and collaborate.
To avoid duplicate work, please leave a comment here if you decide to take it on!
Thank you again for your interest and we look forward to seeing your contributions!
vishwakt commentedon Aug 24, 2026
I've opened PR #126028 with a fix for both crashes.
For BlockLSTMGrad, the kernel takes timelen and batch_size from x and cell_size from w, then slices every other input with them, but never checks that those inputs actually agree. Sweeping the batch sizes shows the crash needs a zero batch on one side and a non-zero batch on the other, since the empty tensor has nothing to read; a mismatch between two non-zero batch sizes does not crash but silently computes against the wrong shape. There is also a second reachable crash in the same op: a shorter time dimension on one of the per step inputs, for example i with timelen 1 while x has 3, trips a fatal check while slicing.
For LSTMBlockCellGrad, x->dim_size(1) is read before any shape check runs, so a rank 1 x aborts. The forward LSTMBlockCellOp already validates the ranks of the same inputs; the gradient did not.
The PR adds the missing dimension checks to BlockLSTMGradOp and the missing rank checks to LSTMBlockCellGradOp, with a test that consistent shapes, including a consistent zero batch size, keep working.
Kayyuri commentedon Aug 26, 2026
Hi @KnightGOKU, A PR has been submitted to fix this issue, and it will be resolved once merged.
Thank you to @vishwakt for working on the fix contributions.
google-ml-butler commentedon Sep 25, 2026
Are you satisfied with the resolution of your issue?
Yes
No