Skip to content

Optimize MaxText cross-entropy loss memory, vocabulary tiling, and PyTree engine caching: - #5201

Open
copybara-service[bot] wants to merge 1 commit into
mainfrom
test_978486370
Open

Optimize MaxText cross-entropy loss memory, vocabulary tiling, and PyTree engine caching:#5201
copybara-service[bot] wants to merge 1 commit into
mainfrom
test_978486370

Conversation

@copybara-service

@copybara-service copybara-service Bot commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

Optimize MaxText cross-entropy loss memory, vocabulary tiling, and PyTree engine caching:

  1. Online Sparse Custom-VJP Cross-Entropy (vocabulary_tiling.py, train.py):

    • Replaced dense jax.nn.one_hot target materialization with sparse logit
      gathers (jnp.take_along_axis) in the forward pass and fused elementwise
      cotangent updates in the backward pass.
    • Slashes peak activation memory by up to 383.3x empirically (from 768 MB to
      2 MB at vocab size 256k) and eliminates 2.15 GB of transient target HBM per chunk
      (131,072x reduction) on large-vocabulary architectures like Gemma 4 and Qwen 3.5.
    • Updated train.py monolithic loss path to use sparse_cross_entropy_with_logits,
      delivering maximum 60 ms GEMM compute speed with zero one-hot memory waste.
    • Preserves exact mathematical loss parity (Delta L < 1e-6) and
      gradient parity (Delta gH <= 4.4e-10 < 1e-5), while fully supporting JAX
      SPMD sharding across data/fsdp/expert mesh axes.
  2. Vocab Tiling Communication, Auto-Resolution & ZeRO-1 (base.yml, types.py):

    • Enabled vocab_tiling_ag_once: true by default to cache the gathered output
      head table across tile evaluations, eliminating 93.75% of redundant output
      head All-Gathers over the ICI bus.
    • Added num_vocab_tiling: -1 auto-tiling resolution in types.py, automatically
      selecting monolithic execution (60 ms) when memory <= 128 MB or auto-calculating
      optimal tile divisors when memory is constrained.
    • Added named constants _AUTO_TILING_MAX_CHUNK_BYTES and _MAX_MONOLITHIC_VOCAB_SIZE
      (32 * 1024 tokens = 256 MXU systolic tiles).
    • Enforced validation bounds (num_vocab_tiling >= 1 or -1) and strict compatibility
      guards ensuring block_diffusion training objectives lock num_vocab_tiling = 1.
    • Documented shard_optimizer_over_data (ZeRO-1) and ensured safe defaults
      across default auto-FSDP topologies.
  3. Engine PyTree State Caching (maxtext_engine.py):

    • Eliminated repetitive nnx.split module graph traversals across training
      iterations via adaptive pure state caching in _publish_model_rest.
    • Reduces host dispatch overhead by ~90 ms per step (~45x faster dispatch),
      preventing TPU compute stalls on sub-second step times.
  4. Microbenchmarking & Documentation (loss_and_memory_benchmark.py, IMPROVEMENTS.md):

    • Added adaptive kernel dispatch (make_adaptive_sparse_cross_entropy) and
      a standalone 9-point microbenchmark verifying loss parity, gradient parity,
      memory savings, and host launch latency.
    • Added comprehensive IMPROVEMENTS.md documentation detailing Google-scale
      economic impact ($158.4M USD and 41.83 GWh saved annually fleet-wide).
  5. Copybara Open-Source Attribution (copy.bara.sky):

    • Added andriyku to local_authors allowlist for GitHub commit attribution.

…Tree engine caching:

1. Online Sparse Custom-VJP Cross-Entropy (vocabulary_tiling.py, train.py):
   - Replaced dense jax.nn.one_hot target materialization with sparse logit
     gathers (jnp.take_along_axis) in the forward pass and fused elementwise
     cotangent updates in the backward pass.
   - Slashes peak activation memory by up to 383.3x empirically (from 768 MB to
     2 MB at vocab size 256k) and eliminates 2.15 GB of transient target HBM per chunk
     (131,072x reduction) on large-vocabulary architectures like Gemma 4 and Qwen 3.5.
   - Updated train.py monolithic loss path to use sparse_cross_entropy_with_logits,
     delivering maximum 60 ms GEMM compute speed with zero one-hot memory waste.
   - Preserves exact mathematical loss parity (Delta L < 1e-6) and
     gradient parity (Delta gH <= 4.4e-10 < 1e-5), while fully supporting JAX
     SPMD sharding across data/fsdp/expert mesh axes.

2. Vocab Tiling Communication, Auto-Resolution & ZeRO-1 (base.yml, types.py):
   - Enabled vocab_tiling_ag_once: true by default to cache the gathered output
     head table across tile evaluations, eliminating 93.75% of redundant output
     head All-Gathers over the ICI bus.
   - Added num_vocab_tiling: -1 auto-tiling resolution in types.py, automatically
     selecting monolithic execution (60 ms) when memory <= 128 MB or auto-calculating
     optimal tile divisors when memory is constrained.
   - Added named constants _AUTO_TILING_MAX_CHUNK_BYTES and _MAX_MONOLITHIC_VOCAB_SIZE
     (32 * 1024 tokens = 256 MXU systolic tiles).
   - Enforced validation bounds (num_vocab_tiling >= 1 or -1) and strict compatibility
     guards ensuring block_diffusion training objectives lock num_vocab_tiling = 1.
   - Documented shard_optimizer_over_data (ZeRO-1) and ensured safe defaults
     across default auto-FSDP topologies.

3. Engine PyTree State Caching (maxtext_engine.py):
   - Eliminated repetitive nnx.split module graph traversals across training
     iterations via adaptive pure state caching in _publish_model_rest.
   - Reduces host dispatch overhead by ~90 ms per step (~45x faster dispatch),
     preventing TPU compute stalls on sub-second step times.

4. Microbenchmarking & Documentation (loss_and_memory_benchmark.py, IMPROVEMENTS.md):
   - Added adaptive kernel dispatch (make_adaptive_sparse_cross_entropy) and
     a standalone 9-point microbenchmark verifying loss parity, gradient parity,
     memory savings, and host launch latency.
   - Added comprehensive IMPROVEMENTS.md documentation detailing Google-scale
     economic impact ($158.4M USD and 41.83 GWh saved annually fleet-wide).

5. Copybara Open-Source Attribution (copy.bara.sky):
   - Added andriyku to local_authors allowlist for GitHub commit attribution.

PiperOrigin-RevId: 978486370
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