Skip to content

Fix: synthesize segment_ids in TunixMaxTextAdapter to mask pad attention - #4016

Merged
copybara-service[bot] merged 1 commit into
mainfrom
pr/segment-ids-fix
May 29, 2026
Merged

copybara-service[bot] merged 1 commit into
mainfrom
pr/segment-ids-fix

Conversation

@py4

@py4 py4 commented May 29, 2026 •

Copy link
Copy Markdown
Collaborator

When tunix invokes the model with decoder_segment_ids=None, MaxText falls back to causal-only attention masking. With left/right-padded batches this silently lets queries at non-pad positions attend to pad-position keys, producing corrupted log-probs in the trainer forward (rollout-side vLLM is fine — its scheduler avoids padded sequences).

Add an optional pad_id constructor arg to TunixMaxTextAdapter. When set AND tunix passes decoder_segment_ids=None, synthesize per-token segment_ids = (input_tokens != pad_id), so MaxText's segment-based mask blocks attention across pad/non-pad boundaries.

Plumb the tokenizer's pad_token_id through model_creation_utils so the RL training path (train_rl.py) picks it up automatically.

Behavior is opt-in: existing callers that omit pad_id see no change.

Empirical impact (Qwen3-1.7B GRPO on GSM8K, GPU recipe LR=2e-6 beta=0.04):

  • with this fix: step-50 vtc_mean_reward 0.5601 -> 0.6970 (+0.137)
  • without this fix: step-50 vtc_mean_reward 0.5601 -> 0.5545 (-0.005)
    The fix is load-bearing for matching GPU's training trajectory.

Checklist

  • Tested locally on TPU v6e 8×8 with Qwen3-1.7B GSM8K (100 outer steps, multi-seed)
  • Verified max_seq_mult_prob_error drops from ~200k–1.4M to ~1.02 after fix
  • Verified end-to-end RL recipe reproduces GPU reference (mean_reward ~0.81 at step 100)
  • Backward compatible: callers that don't pass pad_id see no behavior change
  • No effect on non-RL paths (only TunixMaxTextAdapter integration code touched)

@A9isha A9isha left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Pooya!

@codecov

codecov Bot commented May 29, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 50.00000% with 5 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/maxtext/utils/model_creation_utils.py 20.00% 4 Missing ⚠️
src/maxtext/trainers/post_train/rl/train_rl.py 0.00% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@py4
py4 force-pushed the pr/segment-ids-fix branch 4 times, most recently from 454f01f to 4dc2ecd Compare May 29, 2026 21:39
When tunix invokes the model with `decoder_segment_ids=None`, MaxText falls
back to causal-only attention masking. With left/right-padded batches this
silently lets queries at non-pad positions attend to pad-position keys,
producing corrupted log-probs in the trainer forward (rollout-side vLLM is
fine — its scheduler avoids padded sequences).

Add an optional `pad_id` constructor arg to TunixMaxTextAdapter. When set
AND tunix passes `decoder_segment_ids=None`, synthesize per-token
segment_ids = (input_tokens != pad_id), so MaxText's segment-based mask
blocks attention across pad/non-pad boundaries.

Plumb the tokenizer's pad_token_id through model_creation_utils so the
RL training path (train_rl.py) picks it up automatically.

Behavior is opt-in: existing callers that omit `pad_id` see no change.

Empirical impact (Qwen3-1.7B GRPO on GSM8K, GPU recipe LR=2e-6 beta=0.04):
- with this fix:    step-50 vtc_mean_reward 0.5601 -> 0.6970 (+0.137)
- without this fix: step-50 vtc_mean_reward 0.5601 -> 0.5545 (-0.005)
The fix is load-bearing for matching GPU's training trajectory.
@py4
py4 force-pushed the pr/segment-ids-fix branch from 4dc2ecd to 17665dd Compare May 29, 2026 22:04
@copybara-service
copybara-service Bot merged commit 3db4c3f into main May 29, 2026
38 checks passed
@copybara-service
copybara-service Bot deleted the pr/segment-ids-fix branch May 29, 2026 23:47
@d0t2 d0t2 mentioned this pull request Oct 26, 2026
4 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants