Shared engineering, JAX/sharding, and testing invariants for authoring and reviewing MaxText changes.
- Single Canonical Path: Reuse established MaxText layers, sharding helpers
(
maybe_shard_with_logical,sharding.constrain), remat tags (checkpoint_name), and logging (max_logging) instead of adding duplicate utilities or hardcoded tunables. Fix issues at the owning layer (config validation, input pipeline, sharding rules, or shared layer) rather than patching callers, and update callers directly when refactoring instead of leaving single-use wrappers or pass-through shims. (Exception: undersrc/maxtext/m3/models/, each model family intentionally defines self-contained attention, MLP, and decoder blocks rather than sharing cross-model layers; seesrc/maxtext/m3/README.md.) - Typed Pydantic Configs (Fail-Fast): Declare config keys in
src/maxtext/configs/orsrc/maxtext/m3/configs/and readconfig.<field>directly — never usegetattr(config, "flag", default)ordict.get(...)fallback defaults (unless indexing dynamic attribute names), redundantisinstance/Nonechecks on guaranteed types, blankettry/except Exceptionblocks, or silent fallbacks. Release files, GCS handles, profiler sessions, and background threads via context managers ortry/finally. - Clean Dependencies & Cross-File Contracts: Use concrete types rather than
Any, untypeddictbags, or lazy-import hacks that disguise upward or circular dependencies. When changing signatures, config keys, or parameter shapes/ranks/names, update all consumers: checkpoint conversion, LoRA and quantization rules, and optimizer metadata such as Muon dimension numbers (muon_utils.get_muon_weight_dimension_numbersandmaxtext_muon_utils.get_maxtext_muon_weight_dimension_numbers). Declare new third-party imports in requirements files (requirements_sync_test.py). - Focused Scope & High-Signal Comments: Keep changes small and
single-concern (~400-500 net logic lines max; split preparatory refactors from
features) without unused speculative parameters. Comment non-obvious why
(
stop_gradient/custom VJPs,with_sharding_constraint, donated buffers,rematpolicies, compiler workarounds), not line-by-line code narration.
- JIT Purity & Multi-Host Safety: Avoid Python branching on traced arrays,
side effects inside
jit, unintended recompilation from missing/wrong static arguments, host-device syncs in training or decode loops, materialized or replicated large intermediates instead of sharded ones, extra collectives, Python loops wherejax.lax.scanapplies, multi-host races (e.g., every host writing the same file), or unsynchronized state shared with async checkpointing or data-loading threads.
-
Spec-Level, Collectible Tests: Name test files
*_test.pyor*_tests.py(pytest.iniignorestest_*.py) withtest*methods. Assert observable end-to-end outcomes and regression cases that fail if the requirement is unmet — never add production shims forSimpleNamespace/MagicMock, assert onlyassert_called/is not None/ shapes / "runs without crashing", or test only disabled/single-device/prefill-only paths when sharding or decode is in scope. -
Execution Environment & JAX Pitfalls: CPU CI runs with 4 fake devices (
--xla_force_host_platform_device_count=4); tests skipping below >4 devices or markedtpu_only/gpu_onlyare skipped on CPU, and CPU op emulators (e.g., forjax.lax.ragged_all_to_all) do not replace atpu_onlytest of the real op. UseassertRaisesRegexrather than bareassertRaises(ValueError)around sharded code, compare gradients as well as outputs in layout/sharding/kernel parity tests, and match tuple-shaped collectives in HLO regexes (e.g.,r"\b(all-gather|all-reduce)(?:-start)?\("). -
CPU & TPU Test Efficiency: Keep unit tests fast by using minimal model dimensions/expert counts/topologies, jitting init and execution (
nnx.jit/jax.jit), usinglinen_wrappers.init_variablesto skip discardedToLinen.initforwards, usingeval_shapefor HLO/jaxpr/dtype checks, caching shared parity references per class, settingmegablox=False(orhardware="cpu") in non-kernel CPU MoE tests, avoidingtpu_onlyon pure-CPUdot_producttests, splitting monolithic TPU classes and>4-device CPU-mesh subprocesses (tests/utils/cpu_mesh_child.py) (see.agents/review/testing.md).
- Style & License: Include the Apache 2.0 license header on
.pyfiles and use 2-space indentation with max line length 122 (pyink,pylintrc). - Domain Guides, Skills & Self-Review: Follow matching checklists in
.agents/review/(workflows.md,config-checkpoint.md,kernels.md,performance.md,testing.md), check.agents/skills/for task workflows (see.agents/README.md), and run a read-only self-review against.agents/review/protocol.mdbefore requesting review.