Skip to content

[maxtext] Add exact block-diffusion policy replay#4591

Open
ethannnnnn wants to merge 6 commits into
AI-Hypercomputer:mainfrom
ethannnnnn:block-diffusion-maxtext-pr6-exact-replay
Open

[maxtext] Add exact block-diffusion policy replay#4591
ethannnnnn wants to merge 6 commits into
AI-Hypercomputer:mainfrom
ethannnnnn:block-diffusion-maxtext-pr6-exact-replay

Conversation

@ethannnnnn

Copy link
Copy Markdown

Motivation

Live, reference, and old policies must score the same sampled diffusion actions under the same target-alignment and denoising-state semantics. Autoregressive token scoring is not interchangeable with diffusion trace scoring.

Scope

  • Add an in-process rollout adapter that exports the complete stochastic denoising trace.
  • Reconstruct every pre-commit canvas and score the recorded action at its original step.
  • Return a prepared Tunix diffusion policy batch while keeping user-visible output truncated at the first stop token.

Design

The adapter separates prompt validity, policy actions, and visible output. Exact replay groups positions by recorded action step, rebuilds the corresponding canvas, and evaluates target-aligned logits for the sampled tokens. Training and evaluation use independent RNG streams, with generation contexts derived from the resumable global step.

Invalid stop IDs, unsupported sampling filters, unresolved masks, non-finite action scores, and non-addressable host traces fail closed.

Compatibility

The adapter is opt-in. It does not import or register the vLLM rollout path, and the existing autoregressive rollout remains the default.

Extensibility

The prepared scoring callable is consumed through Tunix's model-agnostic JAX diffusion contract. A future native TPU-inference trace exporter can replace the correctness-first in-process rollout without changing learner semantics.

Tests

  • tests/post_training/unit/diffusion_rl_test.py covers rollout/replay parity, alignment contracts, stop handling, RNG resumption, invalid configurations, and failure cases.
  • Included in the cumulative focused diffusion-RL suite: 83 passed plus 3 subtests.
  • Pyink, changed-file Pylint 10.00/10, Python compilation, and git diff --check passed.

Known limitations

Exact replay performs a model forward for each distinct action step and materializes dense logits. It is a correctness baseline; production throughput and memory require TPU measurement and likely grouped/native scoring.

Stack

Depends on the preceding upstream PR: #4590

Cross-repository dependency: google/tunix#1748 (block-diffusion-tunix-pr6-policy-scoring at da94a2b41ca2).

MaxText block-diffusion design document

Add block_diffusion as a default-off attention type with one namespaced block-size setting. Resolve it in the shared attention layer so ordinary global layers need no model-name dispatch while explicit specialized attention remains intact.

Implement matching dense, Splash, Tokamax, packed-sequence, and load-balanced context-parallel masks. Preserve the autoregressive path and support partial final blocks.

Tests: attention 42 passed/37 skipped; config 18 passed plus 21 subtests; Tokamax 3 passed; pyink, yamllint, git diff --check.
Add a model-agnostic block-diffusion training objective without changing the
causal LM default. The data pipeline emits separate validity, completion,
corruption, and loss masks, supports same-position/all-masked and
shifted/seeded canvases, and preserves those fields through context-parallel
reordering and shaped batches.

Align model logits to physical targets in a shared scoring utility and consume
explicit loss weights in Linen, NNX, native gradient accumulation, and the
Tunix SFT adapter. The adapter composes the stock Tunix PeftTrainer and its
LossOutput contract, including denominator-aware accumulation and evaluation;
the existing MaxText AR trainer path remains unchanged.

Tests cover partial blocks, prompt protection, deterministic corruption,
alignment after sequence reordering, strict mask requirements, zero-weight
gradients, causal no-regression, weighted accumulation/evaluation acceptance,
and SFT adapter validation.

Test Plan:
- 196 passed, 14 skipped, 3 deselected; 66 subtests passed in focused MaxText unit suite
- 11 passed in Tunix-backed post-training SFT suite
- Pylint 10.00/10 on new scoring and loss/GA tests
- Pyink, yamllint, codespell, pycompile, and git diff checks pass
Add a model-independent low-confidence block rollout that consumes target-
aligned logits. It supports the two public model contracts, logical-position
block boundaries after context-parallel reordering, heterogeneous batches,
partial final blocks, confidence-threshold commits, and forced-argmax progress.

The initial OPD scope validates a single contiguous completion suffix so clean
future turns cannot leak through bidirectional intra-block attention. Shifted
rollouts also require logical position zero to remain prompt context.

Test Plan:
- 7 passed in tests/unit/diffusion_denoise_test.py, including jax.jit execution
- Pylint 10.00/10
- Pyink, pycompile, and git diff checks pass
Add a default-off student-rollout distillation source that prepares fresh
block-diffusion rollouts in MaxText and delegates the weighted prepared loss
to Tunix. Keep completion, corruption, validity, and loss ownership explicit;
score clean generated tokens with a causal teacher; and exclude positions after
model-specific stop tokens.

Canonicalize multi-turn examples around the final assistant span. Earlier
turns become prompt context, while any later conversation turns are truncated
from tokens and both input/target segmentation before rollout. Rows without a
completion span or prompt still fail closed, so the teacher and student cannot
observe future context.

Make resume fail closed. Persist the tokenizer, model, objective, optimizer,
data-stream, topology, and stop-token contract; replay deterministic HF input
before global device placement; reject early exhaustion and incompatible
standard-distillation checkpoints. Honor each decoder family's native sharding
mode and limit OPD to one inflight computation to control dense-logit memory.

The existing dataset-driven distillation path remains the default and lazily
avoids the new Tunix diffusion APIs.

Test plan:
- 209 passed, 14 skipped, 3 deselected, 69 subtests in the focused MaxText suite
- 26 passed, 25 skipped, 22 subtests against the paired Tunix OPD head
- 4 standard checkpoint restore tests passed through unittest
- 45 focused MaxText OPD/input-pipeline tests and 21 subtests passed
- Pyink 24.10.1, Ruff, Pylint 10/10, and git diff --check
Extend the low-confidence block denoiser with a stochastic, per-row rollout
that records the sampled token, action step, and action log probability for
every completion position.

The trace keeps the full fixed-horizon denoising schedule even after a visible
EOS, isolates RNG streams between batch rows, excludes the mask token from
sampling, and forces progress when no token clears the confidence threshold.
Existing deterministic generation remains unchanged.

This trace is the minimal model-independent contract needed to replay the same
diffusion actions under live, reference, and old policies during policy
optimization.

Test Plan:
- pytest tests/unit/diffusion_denoise_test.py
- included in the 83-test focused diffusion-RL suite
- Pyink, Pylint, py_compile, and git diff --check
Add a correctness-first MaxText/Tunix adapter for block-diffusion policy
optimization.

The in-process rollout exports the full stochastic denoising trace while
keeping user-visible completions truncated at the first stop token. The scorer
reconstructs each pre-commit canvas and evaluates the recorded action at its
original denoising step, so rollout, live, reference, and old-policy log
probabilities share one target-aligned contract.

Generation uses independent train/eval RNG streams with resumable step
contexts. Invalid stop IDs, non-finite logits, unresolved masks, unsupported
sampling filters, and partial/non-addressable host traces fail closed. The
adapter is opt-in and does not register or import the vLLM path.

Test Plan:
- pytest tests/post_training/unit/diffusion_rl_test.py
- included in the 83-test focused diffusion-RL suite
- Pyink and changed-file Pylint 10.00/10
- py_compile and git diff --check
@gemini-code-assist

Copy link
Copy Markdown

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

@ethannnnnn
ethannnnnn requested a review from dipannita08 as a code owner July 23, 2026 22:44
@google-cla

google-cla Bot commented Jul 23, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

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