Skip to content

[TPU] perf: support multi-tier static_micro_batch_lens to avoid single-bucket overflow cliffs - #180

Open
syangx39 wants to merge 1 commit into
verl-project:mainfrom
syangx39:tpu-static-micro-batch-lens
Open

syangx39 wants to merge 1 commit into
verl-project:mainfrom
syangx39:tpu-static-micro-batch-lens

Conversation

@syangx39

@syangx39 syangx39 commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Status

Rebased onto latest main (7c97a49) and aligned with #171, #172, #188, and #193. The live TPU A/B is complete; results are below.

Motivation

With a single engine_kwargs.tpu.static_micro_batch_len (e.g. 32768), any mini-batch under use_dynamic_bsz=True whose packed tokens slightly exceed 32,768 splits into 2 micro-batches (e.g. ~18k tokens each) and pads both micro-batches all the way up to 32,768 (65,536 padded tokens total — 2× the compute for a ~10% token increase).

In our 32-chip Qwen3.5-35B-A3B RL runs on an older plugin revision (1bfe7fbd, #127, before #171/#172):

  • A +7% increase in response length jumped single-tier 32,768 overflow steps from 1/28 to 18/28, adding +3.88 s/step (+48%) to update_actor.
  • With dual-tier static_micro_batch_lens: [20480, 32768], normal 1-MB steps still snap to 32,768, while 2-MB overflow steps (~18k tokens per MB, 36,000 tokens total) snap each micro-batch to 20,480 (2 × 20,480 = 40,960 padded tokens instead of 2 × 32,768 = 65,536, a -37.5% reduction in padded tokens) with no increase in peak HBM in our A/B below (79.34 GiB vs 83.18 GiB; the largest tier still bounds the shape) and XLA compilations bounded to one shape per tier.

Summary of Changes

Testing (CPU)

  • Added unit tests in test_tpu_options.py, test_tpu_engine.py, test_tpu_gdn_slot_schedule.py, test_tpu_graph_trainer_mode.py, and test_tpu_train_step_precompile.py covering:
    • Option parsing/normalization (list, tuple, string list, comma-separated string, OmegaConf ListConfig), rejection of empty/non-positive/non-int/invalid inputs, and mutual exclusivity with static_micro_batch_len.
    • Per-tier gdn_auto_row_slot_len divisibility and >= checks (valid multi-tier and rejecting smaller or non-divisible tiers).
    • Single-process bug fix (test_align_micro_batch_shapes_single_process_exceeds_static_len): verifies micro-batches longer than static_micro_batch_len receive tpu_padded_seq_len >= bucket_length(n_tokens) for both uninitialized torch.distributed and world_size == 1.
    • Single-process and multi-rank tier snapping, the 36,000-token 2-MB overflow savings (40,960 vs 65,536, -37.5%), single-tier equivalence with static_micro_batch_len, num_seqs_bucketing, gdn_row_slot_len, sort_micro_batches_for_slots, static_log_prob_micro_batch_len override on forward_only=True, FFD bin-packing ([TPU] perf: 64K TpuGraphTrainer CP/bypass-REINFORCE+IS/RS, FFD bin-packing, and async micro-batching #188), TpuGraphTrainer validation, and precompile_train_step ([TPU] perf: opt-in graph-trainer step precompile during initial rollout (VERL_TPU_PRECOMPILE_TRAIN_STEP=1) #193).
  • Executed CPU tests and lint checks locally:
    • Affected core test files (test_tpu_options.py, test_tpu_gdn_slot_schedule.py, test_tpu_graph_trainer_mode.py, test_tpu_engine.py): 229 passed.
    • Full tests/accelerators/tpu/ suite: 589 passed, 1 skipped (excluding the 5 pre-existing verl#8663 core-verl boolean-indexing tests in untouched test_tpu_loss_inputs.py due to the local environment's installed verl version; verified in CI via tpu-loss-core-sha).
    • ruff check, ruff format --check, mypy, license, copyright, naming, and compileall checks all passed.

Live TPU A/B (32 chips, Qwen3.5-35B-A3B GRPO)

Setup: TPU v7x, 32 chips (16 trainer + 16 rollout), verl 453954b3, the same image and SEED=101 in both arms, 30 steps each, both started with empty compile caches. Single tier = main (ffe8860) with static_micro_batch_len=32768; this PR = b08e548 with static_micro_batch_lens=[20480, 32768]. Both runs completed all 30 steps with 240/240 weight-sync parity checks passing and no tracebacks.

Per overflow step (2 micro-batches) Single tier 32768 This PR [20480, 32768] Change
Padded length per micro-batch [32768, 32768] [20480, 20480]
Padded tokens per rank 65,536 40,960 −37.5%
Padding (wasted) tokens per rank 31,416 7,346 −76.6%
update_actor, steady state (no new programs) 13.98 s (median, n = 15) 10.92 s / 10.95 s (n = 2) −3.05 s (−21.8%)
  • Steps without overflow (one micro-batch, padded to 32768 in both arms): update_actor 7.94 s vs 7.92 s.
  • One-time compile cost of the extra tier: the first two-micro-batch step took 87.0 s with 150 new trainer XLA programs, vs 56.6 s with 103 for the single tier (+30.5 s and +47 programs, once). Step 1 is unchanged (164.2 s vs 162.2 s, 254 programs each).
  • Peak HBM: 79.34 GiB vs 83.18 GiB for the single tier (no increase).
  • Quality unchanged: GSM8K (1,319 problems) at steps 0 / 10 / 20 / 30 is 1210 / 1274 / 1273 / 1276 for the single tier and 1211 / 1272 / 1274 / 1275 for this PR. Steps 3–30 means: log_ppl_abs_diff 0.00201 vs 0.00230, IS ESS 0.99793 vs 0.99782.
  • Estimated effect: applying this PR's steady overflow-step cost to the single-tier run's 18 overflow steps in Steps 11–30 gives about −2.7 s per step on average (−19% of update_actor). Rollout dominates step time in this configuration, so that is −3.4% of step time.
  • Caveats:
    • The two arms' responses diverged (mean response length over Steps 3–30: 434 vs 365 tokens), so this PR's run overflowed on only 5 of Steps 3–30 (vs 22). Whole-window means, e.g. Steps 11–30 step time 81.81 s vs 72.06 s, are confounded by that; the per-overflow-step costs above are the like-for-like comparison.
    • Only 2 of this PR's overflow steps are free of compile effects and of the GatedDeltaNet document-budget switch (which adds 3–11 s to the first two-micro-batch step after a one-micro-batch step in both arms, independent of this PR). The two samples agree closely; a replicate would add more.
    • Both arms log the same 3 non-fatal XLA VMEM retry warnings during step-1 compilation.

@syangx39
syangx39 force-pushed the tpu-static-micro-batch-lens branch from b509bcd to b08e548 Compare November 22, 2026 02:47
@syangx39
syangx39 marked this pull request as ready for review November 23, 2026 06:24
…e-bucket overflow cliffs

Adds engine_kwargs.tpu.static_micro_batch_lens (sorted, deduplicated
positive token tiers such as [20480, 32768] or '20480,32768') so each
packed micro-batch snaps to the smallest tier >= its bucketed length
(or keeps its bucketed length above the highest tier).

With single-tier static_micro_batch_len=32768, spilling a step from 1
micro-batch to 2 micro-batches pads both micro-batches to 32,768 tokens
(65,536 total). Dual-tier [20480, 32768] snaps 2-MB spill steps to
2 x 20,480 = 40,960 padded tokens (-37.5%) with zero extra peak HBM
while keeping XLA compilations bounded to one shape per tier.

Also fixes a single-process bug in align_micro_batch_shapes_across_ranks:
without gdn_row_slot_len, the single-process path previously assigned
static_padded regardless of actual length, even when a micro-batch was
longer than static_micro_batch_len. It now uses
max(bucket_length(n_tokens), static_padded), matching the multi-rank path.

Aligned with verl-project#171 and verl-project#172:
- Mutually exclusive with static_micro_batch_len; preserves existing
  single-tier multi-rank behavior when static_micro_batch_lens is unset.
- Validates every tier >= gdn_auto_row_slot_len and divisible by
  gdn_auto_row_slot_len when gdn_auto_row_slot_len is set, and
  suppresses the missing-static-len warning when static_micro_batch_lens
  is configured.
- static_log_prob_micro_batch_len overrides static_micro_batch_lens on
  forward_only passes.
- Shared resolve_static_micro_batch_len helper keeps
  sort_micro_batches_for_slots and align_micro_batch_shapes_across_ranks
  consistent across tiers, static gdn_row_slot_len, and
  num_seqs_bucketing.
- TpuGraphTrainer validation rejects multi-tier static_micro_batch_lens
  clearly while accepting a single-tier list.

Signed-off-by: Summer Yang <xiaotongyang@google.com>
@syangx39
syangx39 force-pushed the tpu-static-micro-batch-lens branch from b08e548 to ede35f6 Compare November 29, 2026 07:56
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