nnx.EMA.update fails when the model holds RNG state (e.g. nnx.Dropout with rngs)
I maintainer di solito rispondono entro 3 giorni
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 48/100
- Tipo di issue
- Bug
- Chiarezza
- Abbastanza chiara
- Stato di attività
- Attiva
- Stack tecnologico
- python
- Ambito
- machine-learning
Direzione di ricerca
Start in flax/nnx/training/ema.py at _update_ema and inspect tests/nnx/ema_test.py, then compare the EMA docstring and optimization cookbook examples. Reproduce the failure with the provided Model containing Dropout and RNG state, including the jitted update path. Done means EMA updates work without corrupting or averaging RNG state, while the intended handling of integer Variables is covered by tests.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
System information
- OS Platform and Distribution: Linux x86_64
- Flax, jax, jaxlib versions: flax 0.12.10, jax 0.11.2, jaxlib 0.11.2 (PyPI wheels)
- Python version: 3.12.13
- GPU/TPU model and memory: none, run with
JAX_PLATFORMS=cpu - CUDA version (if applicable): n/a
Problem you have encountered:
nnx.EMA defaults to only=..., so it copies every Variable in the model, including the RngKey and RngCount that nnx.Dropout(..., rngs=rngs) keeps. EMA.update then runs decay * ema + (1 - decay) * update on those too. For the uint32 count JAX warns that a float32 value is being written into it, and for the key it raises:
TypeError: multiply does not accept dtypes float32, key<fry>.
A module that stores self.rngs = rngs fails the same way. So the usage in the EMA docstring and the optimization cookbook breaks on the first update once the model carries RNG state. The docstring, the cookbook and tests/nnx/ema_test.py only use nnx.Linear (or an nnx.Sequential of them), which has none.
What you expected to happen:
update works on a model with dropout, averaging the parameters (and batch stats, as the default does today) and leaving RNG state alone.
Passing only=nnx.Param or only=nnx.Not(nnx.RngState) avoids the error today, including inside a jitted step that calls nnx.grad, optimizer.update and ema.update.
Logs, error messages, etc:
Output of the script below on flax 0.12.10 (traceback trimmed):
.../jax/_src/ops/scatter.py:105: FutureWarning: scatter inputs have incompatible types: cannot safely cast value from dtype=float32 to dtype=uint32 with jax_numpy_dtype_promotion=standard. In future JAX releases this will result in an error.
warnings.warn(
Traceback (most recent call last):
File ".../issue_repro.py", line 10, in <module>
ema.update(model)
...
File ".../flax/nnx/training/ema.py", line 138, in _update_ema
ema[...] = self.decay * ema + (1.0 - self.decay) * update
...
TypeError: multiply does not accept dtypes float32, key<fry>.
Steps to reproduce:
from flax import nnx
class Model(nnx.Module):
def __init__(self, rngs):
self.linear = nnx.Linear(2, 2, rngs=rngs)
self.dropout = nnx.Dropout(0.1, rngs=rngs)
model = Model(nnx.Rngs(0))
ema = nnx.EMA(model, decay=0.9)
ema.update(model)
Two ways to fix it that I tried, on a model with Linear, BatchNorm, Dropout, an integer nnx.Variable step counter and an nnx.Param(1.0):
-
Default
onlytonnx.Not(nnx.RngState). Batch stats are still tracked andapply_touses the model's own RNG state. Integer Variables are still averaged, though: the step counter at 10 comes out as 1 in the EMA model, with the same kind of cast warning for int32. -
Keep
only=...and also drop non-floating Variables when choosing what to track:def _is_inexact(path, x) -> bool: if not isinstance(x, variablelib.Variable): return True return jnp.issubdtype(jnp.result_type(x.get_value()), jnp.inexact) # in EMA.__init__ self.filter = filterlib.All(only, _is_inexact) # ... and use self.filter instead of only in the graphlib.state callBatch stats and the Python-float
Paramare still tracked, and RNG state and the step counter aren't, so the EMA model shows the model's live values for them (count and step match the model after a train-mode call). Non-Variable leaves still reach the existingTypeErrorin_to_ema_param. This also applies on top of an explicitonly: withonly=nnx.PathContains('step')the release averages the counter (10 becomes 1, with the int32 cast warning), and with this change nothing is tracked.
I'm not sure which one fits the intended meaning of the default better.
- 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
-
RNN returns a wrong final carry (or raises) with seq_lengths and more than one batch dimensionForse già presa @bhaskargurram-ai l’ha presa 15 giorni fa. Aperta
Difficoltà 3/5 Mezza giornata Idoneità per principianti 72/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