Skip to content

[maxtext] Export exact stochastic denoising traces#4590

Open
ethannnnnn wants to merge 5 commits into
AI-Hypercomputer:mainfrom
ethannnnnn:block-diffusion-maxtext-pr5-stochastic-trace
Open

[maxtext] Export exact stochastic denoising traces#4590
ethannnnnn wants to merge 5 commits into
AI-Hypercomputer:mainfrom
ethannnnnn:block-diffusion-maxtext-pr5-stochastic-trace

Conversation

@ethannnnnn

Copy link
Copy Markdown

Motivation

Diffusion policy optimization must replay the actions that were actually sampled. A final token sequence alone does not identify the intermediate canvas or denoising step that produced each action.

Scope

  • Extend the low-confidence rollout with stochastic categorical sampling.
  • Record sampled token, action step, and rollout log probability for every completion position.
  • Isolate RNG streams per row and retain the full fixed-horizon trace after visible EOS.
  • Accept scalar or per-row JAX keys in both typed and legacy formats across supported PRNG implementations.

Design

The trace records each action against its pre-commit state. Sampling excludes the mask token, applies the configured temperature, and uses forced argmax only to guarantee progress when the confidence threshold commits nothing. Per-row keys prevent extra denoising work in one batch row from advancing another row's random stream.

RNG normalization derives the scalar key shape from the JAX key dtype instead of assuming a legacy uint32[2] representation. The rollout accepts typed and legacy scalar keys or one key per row, and key selection preserves any implementation-specific trailing dimensions. This supports both threefry2x32 and unsafe_rbg under JIT without changing sampled-action semantics.

Visible output truncation is deliberately separate from policy data: reward text may stop at EOS, while the policy trace retains every scheduled action needed for exact replay.

Compatibility

The existing deterministic rollout and its public wrapper remain unchanged when no RNG is supplied. Existing legacy-key callers continue to work, while typed-key callers no longer depend on one concrete key layout. No trainer or RL algorithm is selected by this PR.

Extensibility

The trace is model independent and can be scored by live, reference, and old policies through a separate adapter. It can also support future grouped/native scoring optimizations without changing sampled-action semantics.

Tests

  • tests/unit/diffusion_denoise_test.py covers stochastic traces, row-independent RNG, EOS/full-trace separation, forced progress, and mask exclusion.
  • A dedicated JIT matrix passed 10 combinations spanning typed and legacy keys, scalar and per-row forms, and the threefry2x32 and unsafe_rbg implementations.
  • Included in the cumulative focused diffusion-RL suite: 83 passed plus 3 subtests.
  • Pyink, Pylint, Python compilation, and git diff --check passed.

Known limitations

This records a fixed old sampling schedule. It does not claim an unbiased estimator over alternative denoising schedules, and it does not add a TPU-optimized grouped replay kernel.

Stack

Depends on the preceding upstream PR: #4589

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
@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.

@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