Skip to content

NNX: fix Linen-parity gaps on the default path + unit tests#4255

Draft
ecnal-cienet wants to merge 4 commits into
mainfrom
fix/nnx-linen-parity-gaps
Draft

NNX: fix Linen-parity gaps on the default path + unit tests#4255
ecnal-cienet wants to merge 4 commits into
mainfrom
fix/nnx-linen-parity-gaps

Conversation

@ecnal-cienet

@ecnal-cienet ecnal-cienet commented Jun 24, 2026

Copy link
Copy Markdown
Collaborator

Description

This closes the Linen-only / silently-divergent gaps on that path and adds correctness unit tests.
Stacked on feat/nnx-set-defaults-true.

Fixes

# Issue
1 skip_step_on_spikes: silent no-op on NNX (apply_gradients didn't forward loss/grad_norm). Now forwarded + metric surfaced.
2 loss_fn: NNX checked vocab-tiling before indexer warm-up; reordered to match Linen.
3 NNX decoder logits guards used self.model_mode instead of the call-arg model_mode.
4 routed_bias: updates silently dropped on NNX (Linen "intermediates" prefix absent on NNX dict). Now matched by suffix.
5 record_internal_nn_metrics: KeyError on NNX. Now NNX-aware via suffix collection.
6 qwix: crashed under pure_nnx with the bridged decoder; bridge now skips qwix's non-Variable attrs + a config guard rejects bridged-decoder+qwix.
7 maxengine.set_engine_vars_from_base_engine: AttributeError on NNX; now uses get_kv_cache_annotations_nnx.
8 GRPO gradient_accumulation_steps>1: NotImplementedError on NNX. Implemented; also fixed the GA loss metric (sum/GA, not sum/total_weights).
9 GRPO scan_layers=False: NotImplementedError on NNX. Guard removed (NNX policy already matches the inference layout).
10 GRPO optimizer_memory_host_offload: ignored on NNX; now moves opt state to device before the update.

Also re-declared the legacy GRPO config fields (inference_replicas / inference_devices_per_replica /
inference_rollouts / use_pathways_reshard) in types.py — they were dropped from the schema so
grpo.yml couldn't load (pre-existing on main).

Tests

tests/unit/{train_nnx_test,grpo_nnx_test,maxengine_nnx_test,nnx_quant_guard_test}.py — 27 pass on CPU:

PYTHONPATH=src JAX_PLATFORMS=cpu python3 -m pytest \
  tests/unit/grpo_nnx_test.py tests/unit/train_nnx_test.py \
  tests/unit/maxengine_nnx_test.py tests/unit/nnx_quant_guard_test.py -q

Checklist

Before submitting this PR, please make sure (put X in square brackets):

  • I have performed a self-review of my code. For an optional AI review, add the gemini-review label.
  • I have necessary comments in my code, particularly in hard-to-understand areas.
  • I have run end-to-end tests tests and provided workload links above if applicable.
  • I have made or will make corresponding changes to the doc if needed, including adding new documentation pages to the relevant Table of Contents (toctree directive) as explained in our documentation.

PR6-PR10 promoted every routed-to-Linen feature to NNX-native; PR#2885 lands NNX-native pipeline parallelism. This PR flips the three defaults in base.yml so NNX is the production path, and bundles the NNX-only fixes that surface once pure_nnx=True (DiLoCo merge/checkpoint, Zero-1 input shardings on flat nnx.State, MTP sown-Variable handling, generate_param_only_checkpoint NNX flow, maxengine Linen-parity removal).
@ecnal-cienet ecnal-cienet force-pushed the fix/nnx-linen-parity-gaps branch from 0390217 to aa18ab3 Compare June 24, 2026 14:55
Features that were Linen-only or silently divergent on the now-default NNX path:
- skip_step_on_spikes: forward loss/grad_norm through the NNX optimizer; surface step_skipped
- loss_fn: match Linen indexer-warmup vs vocab-tiling ordering
- decoder logits guards: use the call-arg model_mode, not self.model_mode
- routed_bias: read moe_bias_updates by suffix (NNX intermediates are model-rooted)
- record_internal_nn_metrics: NNX-aware activation-metric collection (was KeyError)
- qwix: skip non-Variable attrs in the ToNNX bridge; guard pure_nnx + bridged decoder + qwix quant
- maxengine.set_engine_vars_from_base_engine: dispatch to NNX kv-cache annotations
- GRPO (NNX): gradient_accumulation_steps>1, scan_layers=False reshard, optimizer_memory_host_offload
@ecnal-cienet ecnal-cienet force-pushed the fix/nnx-linen-parity-gaps branch from aa18ab3 to bdf087f Compare June 24, 2026 16:03
grpo_trainer: the GA loss metric divided sum(total_loss) by total_weights and
re-added moe_lb, but total_loss is already a per-batch mean that includes moe_lb;
report sum(total_loss)/GA so it matches the full-batch step (gradients were already
correct). Tests:
- GRPO grad-accum equivalence: GA=2 reproduces the full-batch params and loss
- skip_step_on_spikes: a real spike is skipped and params frozen
- routed_bias: moe_bias_updates found by suffix on the model-rooted NNX dict
- record_activation_metrics: identical metrics for Linen- and NNX-shaped inputs
- qwix quant guard: rejects bridged-decoder+qwix, accepts the pure-decoder path
…ffload (#10)

Both paths need Pathways/TPU-memory infra at runtime, so the external pieces
(reshard_pytree via pathwaysutils; move_memory_to_device) are mocked and the test
pins our changes:
- #9: scan_layers=False no longer raises and the unscanned policy params are pushed
  to the inference engine (guard removal).
- #10: optimizer_memory_host_offload runs the device_put/update plumbing and yields
  the same params as the no-offload step (memory placement, not math).
@ecnal-cienet ecnal-cienet changed the title Fix/nnx linen parity gaps NNX: fix Linen-parity gaps on the default path + unit tests Jun 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant