RNN returns a wrong final carry (or raises) with seq_lengths and more than one batch dimension
I maintainer di solito rispondono entro 3 giorni
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
- Ambito
- machine-learning
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
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di google/flax
-
Difficoltà 1/5 1-3 ore Idoneità per principianti 88/100
I maintainer di solito rispondono entro 3 giorni
-
nnx.Embed resolves dtype=None at construction, so it diverges from linen.EmbedForse già presa @mahdi-shafiei l’ha presa 10 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
I maintainer di solito rispondono entro 3 giorni
-
MultiHeadAttention documentation missing descriptions of `None` values for optional argumentsForse di nuovo libera Una pull request per questa issue è stata chiusa senza essere unita. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
I maintainer di solito rispondono entro 3 giorni
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 42/100
I maintainer di solito rispondono entro 3 giorni
-
nnx.EMA.update fails when the model holds RNG state (e.g. nnx.Dropout with rngs)Forse già presa @Arthur031221 l’ha presa 62 giorni fa. Aperta
Difficoltà 4/5 3-5 giorni Idoneità per principianti 48/100
I maintainer di solito rispondono entro 3 giorni
Issue simili
-
Difficoltà 1/5 1-3 ore Idoneità per principianti 87/100
deepset-ai/haystack-integrations#815 ·
I maintainer di solito rispondono entro 12 giorni
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
tensorflow/datasets#11260 ·
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 86/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 66/100
Niko1221/Strata#7537 · 3 commenti ·
I maintainer di solito rispondono entro 4 giorni
-
load_model_from_checkpoint drops num_out/signal_in_channels for models whose __init__ takes **kwargsAperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
I maintainer di solito rispondono entro 1 giorno