Skip to content

[tunix] Add target-aligned diffusion SFT adapter#1745

Open
ethannnnnn wants to merge 3 commits into
google:mainfrom
ethannnnnn:block-diffusion-tunix-pr3-sft
Open

[tunix] Add target-aligned diffusion SFT adapter#1745
ethannnnnn wants to merge 3 commits into
google:mainfrom
ethannnnnn:block-diffusion-tunix-pr3-sft

Conversation

@ethannnnnn

Copy link
Copy Markdown

Motivation

Tunix needs a reusable SFT adapter for already-prepared diffusion examples without owning model-specific corruption, role parsing, or target alignment.

Scope

  • Add configure_diffusion_sft for the canonical diffusion batch.
  • Bind an external raw-batch adapter and target-aligned model logits function through existing PeftTrainer extension points.
  • Compute weighted float32 cross entropy without an autoregressive target shift.

Design

The adapter validates the canonical batch, obtains target-aligned logits, sanitizes inactive targets, and returns LossOutput with the explicit token weights. Zero-weight positions remain numerically inert even if their placeholder values are invalid.

Compatibility

Existing SFT continues to use its current data adapter, model loss, and next-token semantics unless the diffusion adapter is explicitly configured.

Extensibility

Tunix remains independent of MaxText batch keys and tokenizer policy. Any integration that can produce DiffusionTokenBatch and target-aligned logits can use the same SFT objective.

Tests

  • 17 focused diffusion contract/SFT tests plus 6 weighted-gradient tests at this stack point.
  • Coverage includes real PeftTrainer wiring, JIT/gradient behavior, float32 math, no-shift alignment, inactive-value safety, and zero weights.
  • Pyink, isort, Pyrefly, Pylint, Python compilation, and git diff --check passed.

Known limitations

The adapter does not create corrupted examples or infer completion scope. Those remain responsibilities of the model-aware integration.

Stack

Depends on the preceding upstream PR: #1744

Tunix block-diffusion design document

Define a target-aligned, batch-major diffusion batch contract and typed adapter/scorer protocols without depending on MaxText or a specific training algorithm.

Validate shapes and dtypes at construction and scoring boundaries, while preserving JAX pytree, JIT, and sharding compatibility.

Tests: 10 diffusion contract tests; pyink/isort; pylint; pyrefly; py_compile.
Accumulate LossOutput gradients as unreduced sums and normalize once by the
total denominator across microbatches. Preserve denominator-one behavior for
scalar losses and return zero gradients when every weight is zero.

Select auxiliary-metric reducers by value type in training and evaluation:
globally combine weighted metrics while averaging ordinary scalar metrics.
Reject per-key type changes across microbatches and preserve consistent
epsilon and minimum-denominator bounds during global reduction.

Preserve the dtype selected by each Optax optimizer-state initializer across
conditional update and skip branches. This keeps explicit bf16 moments in
bf16, retains explicit fp32 moments, and prevents Flax NNX branch-type
mismatches without special-casing a particular accumulation count.

Tests cover weighted and fractional denominators, zero-weight batches, mixed
weighted/plain train and eval metrics, reducer invariants, and a real
PeftTrainer + nnx.jit matrix over direct/injected AdamW and gradient
accumulation counts 1 and 2. The complete PeftTrainer suite passes 56 tests;
the cumulative focused validation passes 119 tests with six optional engine
tests deselected. Ruff and git diff checks pass.
Provide a typed PeftTrainer adapter for canonical diffusion batches and target-aligned score functions. Compute weighted float32 cross entropy without autoregressive shifting, sanitize inactive targets, and preserve zero-weight numerical safety.

Tests: 17 diffusion contract and SFT tests; 6 focused weighted-gradient tests; pyink, isort, pyrefly, pylint, py_compile, and diff checks.
@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.

2 participants