RNN returns a wrong final carry (or raises) with seq_lengths and more than one batch dimension

Aperta
#5,649 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 3 giorni

@bhaskargurram-ai ci sta già lavorando.

Dal 15/11/2026.

  • #5650 di @bhaskargurram-ai — aperta

Valutazione

Difficoltà
3/5
Tempo stimato
Mezza giornata
Idoneità per principianti
72/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python

Direzione di ricerca

Start with _select_last_carry in flax/nnx/nn/recurrent.py and its twin in flax/linen/recurrent.py; the culprit line indexes only the first batch axis via x[last_idx, jnp.arange(x.shape[1])]. Run the repro in the issue to see the (2, 2, 2, 4) carry and the broadcasting ValueError, then find the existing test_rnn_multiple_batch_dims to see how multi-batch inputs are already covered without seq_lengths. Done when the flattened leading batch axes are gathered with take_along_axis-style indexing so the carry is (*batch, hidden) and matches the per-sequence reference, with a test for both batch shapes added for linen and nnx.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

nn.RNN and nnx.RNN accept inputs of shape (*batch, time, *features) and seq_lengths of shape (*batch). With more than one batch dimension, return_carry=True and seq_lengths, the final carry is wrong:

  • if the batch dimensions have different sizes, the call raises a broadcasting error;
  • if they have the same size, the call succeeds but returns a carry with an extra axis, built from the wrong sequences.

With a single batch dimension everything is fine, and without seq_lengths multiple batch dimensions also work (there is already test_rnn_multiple_batch_dims).

Repro:

import jax, jax.numpy as jnp, numpy as np
from flax import nnx

x = jax.random.normal(jax.random.key(0), (2, 2, 5, 3))  # (B1, B2, time, features)
seq_lengths = jnp.array([[2, 5], [3, 1]])                # (B1, B2)

cell = nnx.LSTMCell(3, 4, rngs=nnx.Rngs(0))
(c, h), _ = nnx.RNN(cell, return_carry=True)(x, seq_lengths=seq_lengths)
print(h.shape)  # (2, 2, 2, 4), expected (2, 2, 4)

# reference: run each sequence on its own, truncated to its length
(_, h_ref), _ = nnx.RNN(cell, return_carry=True)(x[0, 1, :5])
print(np.allclose(h[0, 1], h_ref))  # False

x = jax.random.normal(jax.random.key(0), (2, 3, 5, 3))
nnx.RNN(cell, return_carry=True)(x, seq_lengths=jnp.full((2, 3), 4))
# ValueError: Incompatible shapes for broadcasting: shapes=[(2, 3), (2,)]

flax.linen.RNN gives the same (2, 2, 2, 4) carry for the first case.

Expected: a carry of shape (B1, B2, hidden) where entry [i, j] is the carry after seq_lengths[i, j] steps of sequence [i, j].

Tested on flax 0.12.12 (PyPI) and on main (204a819c), jax 0.12.0, CPU.

Cause: _select_last_carry (in both flax/linen/recurrent.py and flax/nnx/nn/recurrent.py) picks the last valid carry with x[last_idx, jnp.arange(x.shape[1])]. That indexes only the first batch axis, so it is only correct when there is exactly one batch dimension.

Lingua principale
Jupyter Notebook
Stelle
7.4k
Fork
855
Merge medio
3g 3h
PR unite (30g)
5

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di google/flax

Tutte le issue di google/flax

Issue simili

Altre issue su Machine Learning

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.