Skip to content

[maxtext] Add generalized block-diffusion CFT and SFT - #4587

Open
ethannnnnn wants to merge 2 commits into
AI-Hypercomputer:mainfrom
ethannnnnn:block-diffusion-maxtext-pr2-cft-sft
Open

[maxtext] Add generalized block-diffusion CFT and SFT#4587
ethannnnnn wants to merge 2 commits into
AI-Hypercomputer:mainfrom
ethannnnnn:block-diffusion-maxtext-pr2-cft-sft

Conversation

@ethannnnnn

@ethannnnnn ethannnnnn commented Jul 23, 2026

Copy link
Copy Markdown
Collaborator

Motivation

Block-diffusion CFT/SFT needs one explicit definition of target alignment, completion scope, corruption, and loss weights. Reusing segmentation fields for these different meanings can leak clean targets or bias training.

Scope

  • Add a default-off block_diffusion training objective while retaining causal_lm as the default.
  • Emit distinct validity, completion, corruption, and loss masks from the data pipeline.
  • Support the public same_position/all_masked and shifted/seed_and_mask model contracts.
  • Consume explicit weighted targets in Linen, NNX, native gradient accumulation, and the SFT adapter.

Design

Targets remain physically unshifted. A shared scoring utility maps model logits to physical targets according to the configured alignment. Corruption samples independently per eligible block, protects prompts and padding, guarantees progress, and handles partial blocks.

completion_mask, corruption_mask, and targets_loss_mask remain separate through shaped batches and context-parallel reordering. Loss normalization uses the sum of active weights, including under gradient accumulation; an all-zero batch returns finite zero loss and zero gradients.

The Tunix-backed diffusion SFT entry point constructs the stock Tunix PeftTrainer and configures it through configure_diffusion_sft, a raw-batch adapter, and a target-aligned logits function. MaxText's existing causal-SFT compatibility subclass remains on the causal path only; diffusion does not depend on that overridden train step.

Compatibility

The causal pipeline keeps its existing trainer selection, next-token shift, segmentation-derived mask, configuration defaults, and checkpoint shape. Diffusion refuses missing explicit masks instead of guessing. Through the paired Tunix weighted-reduction dependency, the Tunix-backed path supports denominator-aware gradient accumulation and evaluation instead of averaging independently normalized microbatches.

Extensibility

The objective is selected by stable configuration rather than a model name. Additional models can implement one of the declared alignment/canvas contracts or add a new validated contract without changing trainer ownership.

Tests

  • Focused MaxText suite: 196 passed, 14 skipped, 3 deselected, and 66 subtests passed.
  • Tunix-backed post-training SFT suite: 11 passed.
  • Tests cover partial blocks, prompt protection, deterministic corruption, reordered alignment, strict mask requirements, zero-weight gradients, and causal no-regression.
  • Pylint for new scoring/loss tests, Pyink, YAML lint, codespell, Python compilation, and git diff --check passed.

Known limitations

The Tunix-backed SFT path does not support optimizer-state host offload because the underlying PeftTrainer does not provide that execution mode. Native MaxText SFT remains available for the same diffusion objective.

Stack

Depends on the preceding upstream PR: #4586

Cross-repository dependency: google/tunix#1745 (block-diffusion-tunix-pr3-sft at 8dc0dd39951a).

MaxText block-diffusion design document

Checklist

  • Added or updated tests for the changed behavior.
  • Ran the relevant local tests and code-quality checks.

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

seed_first_token=config.block_diffusion_canvas_policy == "seed_and_mask",
include_seed_in_loss=config.block_diffusion_logit_alignment == "shifted",
)
if objective != "causal_lm":

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.

instead, use elif for clarity.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

thanks resolved with latest version

@ethannnnnn
ethannnnnn force-pushed the block-diffusion-maxtext-pr2-cft-sft branch from 1822108 to f35aded Compare July 31, 2026 05:14
@ethannnnnn

ethannnnnn commented Jul 31, 2026

Copy link
Copy Markdown
Collaborator Author

Addressed the current review feedback and rebased the complete seven-PR stack onto the latest main.

Key changes in this update:

  • make causal-vs-diffusion objective dispatch explicit with elif;
  • metadata fields now zero-pad independently of a tokenizer's token pad_id;
  • full-target CFT uses target validity, while completion-only SFT uses the explicit completion mask;
  • block-diffusion SFT evaluation no longer divides an already averaged loss twice;
  • focused regression tests cover nonzero pad IDs, CFT/SFT mask ownership, and eval aggregation.

Validation: 206 focused tests passed, 44 skipped, with 71 parameterized subtests; Pyink, compile, whitespace, and public-text checks passed. This PR retains its documented dependency on Tunix PR #1745.

Design: https://docs.google.com/document/d/1N7KcCoAIErB2CV9EJ2G1u_mdN-AQgqwNUYSvM0mtqMI/edit

Vanilla public reproduction guide: https://docs.google.com/document/d/1rjRSopy888ZqLnGh_KUseW8h1_ZqPGEuSee5MM2Q9ac/edit

@entrpn, could you please take another look?

@ethannnnnn
ethannnnnn force-pushed the block-diffusion-maxtext-pr2-cft-sft branch from f35aded to adf7fe4 Compare July 31, 2026 19:48
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
@ethannnnnn
ethannnnnn force-pushed the block-diffusion-maxtext-pr2-cft-sft branch from adf7fe4 to ab7bb88 Compare July 31, 2026 22:43
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.

2 participants