Skip to content

[DMD2 7/10] feat(dmd2): train the student with the distribution matching gradient from the critic group's scores - #334

Merged
Rockdu merged 2 commits into
mainfrom
dmd2/05-dmd2-loss
Nov 11, 2026
Merged

Rockdu merged 2 commits into
mainfrom
dmd2/05-dmd2-loss

Conversation

@zhihengy

@zhihengy zhihengy commented Oct 19, 2026 •

Copy link
Copy Markdown
Collaborator

Seventh of the DMD2 stacked series, on top of #333. --loss-type dmd2 now trains the actor. It trains on the samples the critic group scored, the critic's last step, and for each the student recomputes its x0 at the rollout's step i, then follows the teacher-minus-critic scores that the critic group handed over.

What

  • select_scored_pairs is the actor's train-input hook ([DMD2 4/10] feat(fsdp): add a train-input hook that preprocesses train pairs before micro-batching #338): it keeps only the train pairs the critic scored, i.e. the last 1/K of its DP shard, before the actor builds its micro-batches. The trainer itself doesn't know about DMD2. Scores for samples outside the shard are rejected, so a broken rank pairing fails instead of shrinking the batch.

  • prepare_generator_batch: the student's unguided forward at (x_i, t_i) with the pair's positive cond.

  • dmd2_loss_formula:

    • x0 = x_i - σ_i·v;
    • per pair, g = nan_to_num((x̂0_fake - x̂0_real) / mean|x0 - x̂0_real|);
    • loss = 0.5·mean((x0 - sg(x0 - g))²), so ∂loss/∂x0 = g / numel.

    This is the reference gradient: tianweiy/DMD2 compute_distribution_matching_loss, with the same batch-mean MSE once the actor divides by the pair count.

  • --loss-type dmd2 fills these two hooks the way nft fills its own, and select_scored_pairs as the train-input hook. The critic group runs without a train-input hook. [DMD2 6/10]'s requirement for user hooks is dropped.

  • Metrics:

    • dmd2_grad_abs_mean;
    • x0_mean_abs_diff, x0_max_abs_diff and x0_rel_max, which compare the trainer's x0 with the rollout's x̂0_i (same step, same weights) through the existing record_rollout_train_abs_diff.
  • Single DiT component only, as with NFT.

Why

  • The actor trains on the last B of the K·B rollout samples only, the batch the critic scored before training on it ([DMD2 6/10]). As in tianweiy/DMD2, each generator step takes one fresh batch the size of a critic step's, and the other (K−1)·B samples feed only the critic.
  • The numerator comes from the critic group, which scores a re-noised rollout x̂0_i. The normalizer and the gradient path use the trainer's x0. The two x0s differ only by rollout/train numerics, because the actor takes one step per rollout on synced weights. The x0_* metrics measure that gap on every step.
  • No log-prob or ratio is involved, so the loss doesn't depend on the rollout's sampler numerics beyond x_i itself.

Tests

  • tests/fast/backends/fsdp_utils/test_loss_hub_dmd2.py:
    • The surrogate's gradient equals the reference implementation's on the same x0 and predictions.
    • When the critic matches the teacher, the loss and gradient are zero.
    • 1-D Gaussian toy with analytic posterior means for the teacher and a perfect critic: SGD on the student's mean moves monotonically toward the teacher's mean, which catches sign and normalization bugs.
    • Run through the critic's K steps on one shard, the actor keeps exactly the last step's pairs; scores outside the shard are rejected.
    • Generator batch fields; multiple components are rejected; the x0 diff metric is recorded.
  • tests/fast/utils/test_dmd2_arguments.py: dmd2 fills the converter, prepare, loss and train-input paths, but never over a user's own.
  • tests/fast/utils/test_trainer_config.py: the critic group runs without the train-input hook.
  • The GPU e2e was not run. SD3.5-Medium is gated on this devbox, and teacher CFG needs the sglang negative-cond change noted in [DMD2 6/10]. The e2e standard will come with the recipe.

Checklist

  • pre-commit hooks (black, isort, ruff at the pinned versions) pass on the changed files
  • Added/updated tests for new behaviour (reference-gradient parity, Gaussian toy, defaults)
  • pytest tests/fast: 559 passed, 7 skipped; the same 4 environment failures as [DMD2 6/10] feat(dmd2): train a fake-score critic group from the teacher and score the student's samples #333
  • If launch flags changed, python3 train_diffusion.py --help still parses (no flags added)
  • If a public flag was added, it appears in the CLI reference docs (no flags added; the customization.md table lists the dmd2 hooks)
  • If an example was added, it has a real walkthrough (no examples added)

Stack

DMD2: distill a flow-matching teacher into a few-step student with distribution matching. Merge in order:

  1. [DMD2 1/10] [DMD2 1/10] refactor(ray): build each trainer's args from a trainer config list #335: per-trainer args from a trainer config list
  2. [DMD2 2/10] [DMD2 2/10] feat(ray): train several colocated trainer groups in the sync driver #329: several colocated trainer groups in the sync driver
  3. [DMD2 3/10] [DMD2 3/10] feat(ray): hand earlier trainer groups' outputs to later groups as external_data #330: hand earlier trainer groups' outputs to later groups as external_data
  4. [DMD2 4/10] [DMD2 4/10] feat(fsdp): add a train-input hook that preprocesses train pairs before micro-batching #338: a train-input hook that preprocesses a trainer group's train pairs before micro-batching
  5. [DMD2 5/10] [DMD2 5/10] feat(rollout): stop the rollout at a random step for DMD2 backward simulation #331: few-step rollout and the DMD2 step strategy (needs [Feat] diffusion-rl: stop a rollout at a requested step sgl-project/sglang#45230)
  6. [DMD2 6/10] [DMD2 6/10] feat(dmd2): train a fake-score critic group from the teacher and score the student's samples #333: DMD2 critic group: critic training and teacher/critic scoring of its last step
  7. [DMD2 7/10] this PR: --loss-type dmd2 on the actor
  8. [DMD2 8/10] [DMD2 8/10] perf(ray): sleep each trainer group right after its train #342: sleep each trainer group right after its train
  9. [DMD2 9/10] [DMD2 9/10] feat(dmd2): add the MiniMax H3 4-step DMD2 recipe with eval media and DMD2 metrics #343: MiniMax H3 4-step DMD2 recipe, eval media and DMD2 metrics

A GAN head and other follow-ups come after this series.

@zhihengy zhihengy changed the title [DMD2 5/7] feat(dmd2): train the student with the distribution matching gradient from the critic group's scores [DMD2 6/8] feat(dmd2): train the student with the distribution matching gradient from the critic group's scores Oct 21, 2026
@zhihengy
zhihengy added this pull request to stack #336 October 21, 2026 21:23
@zhihengy
zhihengy removed this pull request from stack #336 October 24, 2026 00:08
@zhihengy
zhihengy added this pull request to stack #339 October 24, 2026 00:09
@zhihengy zhihengy changed the title [DMD2 6/8] feat(dmd2): train the student with the distribution matching gradient from the critic group's scores [DMD2 7/8] feat(dmd2): train the student with the distribution matching gradient from the critic group's scores Oct 24, 2026
@zhihengy
zhihengy marked this pull request as ready for review October 24, 2026 00:14
@zhihengy zhihengy changed the title [DMD2 7/8] feat(dmd2): train the student with the distribution matching gradient from the critic group's scores [DMD2 7/9] feat(dmd2): train the student with the distribution matching gradient from the critic group's scores Oct 26, 2026
@zhihengy zhihengy changed the title [DMD2 7/9] feat(dmd2): train the student with the distribution matching gradient from the critic group's scores [DMD2 7/10] feat(dmd2): train the student with the distribution matching gradient from the critic group's scores Nov 2, 2026
@Rockdu
Rockdu force-pushed the dmd2/05-dmd2-loss branch from 40a9e8c to 1526969 Compare November 4, 2026 19:41
@zhihengy
zhihengy force-pushed the dmd2/05-dmd2-loss branch 2 times, most recently from ec28a5c to 8b569c1 Compare November 10, 2026 03:59
@Rockdu
Rockdu force-pushed the dmd2/05-dmd2-loss branch 3 times, most recently from b458443 to 573d59a Compare November 11, 2026 15:44
Base automatically changed from dmd2/04-critic to main November 11, 2026 15:44
zhihengy and others added 2 commits November 11, 2026 07:44
#338 renamed the hook to --custom-train-input-postprocess-path; the DMD2
defaults and the critic override still set custom_train_input_function_path,
so --loss-type dmd2 failed in parse_args.

Co-Authored-By: Claude Mythos 5.1 <noreply@anthropic.com>
@Rockdu
Rockdu merged commit 23a795a into main Nov 11, 2026
1 of 2 checks passed
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