Out-of-bounds `x[i]` returns a value with `jit_compile=True` but fails in eager mode: `slice index 6 of dimension 0 out of bounds`

Abierto
#128,456 1 comentario 0 reacciones 1 asignado Ver en GitHub

Los mantenedores suelen responder en 2 días

@Venkat6871 ya está trabajando en esto.

Desde el 3/10/2026.

Evaluación

Este issue todavía no se ha evaluado.

Descripción

2.21.0 comp:xla type:bug
Issue type

Bug

Have you reproduced the bug with TensorFlow Nightly?

Yes

Source

binary

TensorFlow version

tf-nightly 2.23.0-dev20261002 (v1.12.1-146002-ged9781322a7); also reproduced with 2.22.0-dev0 built from source at commit 59e32224ffb065b4832b9b4ed9b93235a38aab94

Custom code

Yes

OS platform and distribution

Linux Ubuntu 24.04.3 LTS

Mobile device

No response

Python version

3.11.15

Bazel version

No response

GCC/compiler version

No response

CUDA/cuDNN version

CUDA 12.9 / cuDNN 9.27.0 (from pip install tf-nightly[and-cuda]), driver 580.126.09

GPU model and memory

NVIDIA GeForce RTX 4060 Laptop GPU, 8 GB

Current behavior?

The Keras model below loops i from 0 to 7 over a 6-element input and reads x[i]. In eager mode x[6] fails with INVALID_ARGUMENT: slice index 6 of dimension 0 out of bounds. With jit_compile=True the model runs without an error (3 of 3 runs) and returns [1. 2. 3. 4. 5. 6. 6. 6.]: the out-of-bounds reads return the last element.

Expected: the call fails with the same InvalidArgumentError as in eager mode instead of returning values for the out-of-bounds indices.

To reproduce, save the code below as DusAccumulatorZeroInitElimination_4.py and run python DusAccumulatorZeroInitElimination_4.py on a GPU machine (the program sets jit_compile=True itself). The eager reference runs the same file with tf.config.run_functions_eagerly(True):

python -c "import tensorflow as tf; tf.config.run_functions_eagerly(True); exec(open('DusAccumulatorZeroInitElimination_4.py').read())"

Both outputs are under Relevant log output.

Standalone code to reproduce the issue
import tensorflow as tf

class Model(tf.keras.Model):
    def __init__(self, N):
        """
        N : int  static trip count (e.g., 8).
        """
        super(Model, self).__init__()
        self.N = N

    @tf.function(jit_compile=True)
    def call(self, x):
        N = self.N
        dtype = x.dtype

        # Buffer initialised with zeros, shape [N] (broadcast of scalar zero)
        init_buf = tf.zeros([N], dtype=dtype)

        # Induction variable starts at 0
        i0 = tf.constant(0)
        limit = tf.constant(N, dtype=tf.int32)

        def cond(i, buf):
            return i < limit

        def body(i, buf):
            # Element to write at position i (shape updated to [1])
            update = tf.reshape(x[i], [1])
            # Indices for tensor_scatter_nd_update: [[i]]
            indices = tf.reshape(i, [1, 1])
            # Overwrite buffer at index i (translates to dynamic-update-slice)
            new_buf = tf.tensor_scatter_nd_update(buf, indices, update)
            return i + 1, new_buf

        _, final_buf = tf.while_loop(cond, body, [i0, init_buf])
        return final_buf

# Example usage
N = 8
m = Model(N)
x = tf.constant([1., 2., 3., 4., 5., 6.], shape=[6])
y = m(x)
print(y)
Relevant log output
$ python DusAccumulatorZeroInitElimination_4.py
tf.Tensor([1. 2. 3. 4. 5. 6. 6. 6.], shape=(8,), dtype=float32)

$ python -c "import tensorflow as tf; tf.config.run_functions_eagerly(True); exec(open('DusAccumulatorZeroInitElimination_4.py').read())"
tensorflow.python.framework.errors_impl.InvalidArgumentError: Exception encountered when calling Model.call().

{{function_node __wrapped__StridedSlice_device_/job:localhost/replica:0/task:0/device:GPU:0}} slice index 6 of dimension 0 out of bounds. [Op:StridedSlice] name: model/strided_slice/

Arguments received by Model.call():
  • x=tf.Tensor(shape=(6,), dtype=float32)
Lenguaje dominante
C++
Estrellas
201k
Forks
78.4k
Merge medio
3 d 1 h
PR fusionados (30 d)
433

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de tensorflow/tensorflow

Todos los issues de tensorflow/tensorflow

Issues similares

Más issues de C++

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.