Skip to content

[DMD2 10/10, alternative] feat(dmd2): --dmd2-critic-scoring original, scored by a forward-only batch inside the critic's last step - #416

Open
zhihengy wants to merge 37 commits into
mainfrom
dmd2/07c-original-scoring-in-trunk
Open

zhihengy wants to merge 37 commits into
mainfrom
dmd2/07c-original-scoring-in-trunk

Conversation

@zhihengy

@zhihengy zhihengy commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

Alternative to #375 for the same flag, --dmd2-critic-scoring original: tianweiy/DMD2's scoring, a separate noise draw and critic forward on the last step's samples before the critic trains on them. Both follow the review suggestion on #375 to score inside the critic's own train call, so neither adds train_dmd2.py, compute / async_compute, the optim_steps range or the wake_up guard. This PR takes the suggestion literally: on its last step the critic's prepare hook also returns the scoring batch, and the trunk forwards it without grad before the training forward. #375 runs a forward-only pass before the step instead. One of the two gets closed; draft until then.

What

  • --dmd2-critic-scoring {reuse-critic-step, original}, default reuse-critic-step (no behavior change).
  • PreparedBatch.forward_only_batch: a second prepared batch on the same pairs. _forward_train_pair_batch runs the teacher it asks for and its model on it without grad, before the training batch's forwards, and passes the formula forward_only_teacher_pred and forward_only_pred. A formula whose prepare sets no such batch receives neither, so the other formulas and the custom-formula contract are unchanged. The trunk's forwards of one batch (input dtype policy, the teacher, the model) become a local function called once per batch.
  • DiffusionLossContext.num_microbatches_per_optim_step: set by _train_core once the schedule exists, so the train-input hook still sees 0.
  • loss_hub.dmd2: the scoring batch and the kept x0 are factored into prepare_scoring_batch and keep_scores, which both modes use. Under original the last step is plain SFT (full grid, no teacher) and carries prepare_scoring_batch(..., microbatch_id=microbatch_id + m) as its forward-only batch; critic_loss_formula keeps the x0 of that batch's forwards and returns the SFT loss of the training forward.

Why

Compared with #375

#375: a pass before the step this PR: a batch inside the step
Who asks for the scoring forward the group's hooks, LossHooks.forward_only, with a predicate over the step the prepare hook, per micro-batch, PreparedBatch.forward_only_batch
Where it runs _train_core: a no-grad loop over the step's micro-batches, calling the trunk with the scoring hooks _forward_train_pair_batch: two batches per call
Trunk gains a hooks argument its forward block is re-indented into a local function; every loss type's forward goes through it
Who numbers the scoring draw the loop, which owns the schedule the hook, so ctx has to carry the micro-batch count and is rebuilt after the schedule exists
Formula a separate scoring_formula with no loss; critic_loss_formula unchanged critic_loss_formula takes two more predictions
Order at step K−1 all scoring forwards, then the training forwards interleaved per micro-batch
miles/ diff against the base +137 −20 +156 −63

Tests

  • test_reference_models.py: a forward-only batch runs through its teacher and the model without grad before the training forward, and the formula receives both predictions.
  • test_train_input_hook.py: the ctx of the forwards and the train-output hook carries the micro-batch count of the preprocessed pairs; the train-input hook's does not.
  • test_loss_hub_dmd2.py: under original only the last step carries a scoring batch and it stays plain SFT; that batch equals the reuse mode's last-step batch at the id one step of micro-batches later (draw, sigmas, teacher scale, conds); the formula keeps x_s − σ v of the forward-only forwards and returns the SFT loss; each mode scores the last step exactly once.

Validation

Checklist

  • pre-commit hooks pass on the changed files
  • Added/updated tests for new behaviour
  • python3 train_diffusion.py --help parses; --dmd2-critic-scoring is listed
  • The public flag appears in the CLI reference docs
  • No example added

zhihengy and others added 30 commits November 11, 2026 06:53
`train` returned its train output directly, so the driver's ray.get on the
train refs pulled every rank's output into the controller before the next
group read it again from the object store. Return `Box(ray.put(output))`
like the rollout shards: the driver gets a small box, the payload stays in
the store until the paired rank of a later group unboxes it.

Also note the train-output hook as a `--teacher-load` consumer in
docs/fsdp_model_roles.md, which cli-reference.md already did.

Co-Authored-By: Claude Mythos 5.1 <noreply@anthropic.com>
train returns Box(ray.put(output)); the driver discarded its ray.get and
passed the task refs on, so the consumer's box.inner hit an ObjectRef.
Keep the boxes the way the rollout shards are kept.

Co-Authored-By: Claude Mythos 5.1 <noreply@anthropic.com>
…tprocess

Review on #338: `custom_train_input_postprocess_func` says what the hook
does, a post-processing step on the converted train pairs. The flag
follows the prepare hook's pattern: `--custom-train-input-postprocess-path`
sets `args.custom_train_input_postprocess_path`, loaded into
`self.custom_train_input_postprocess_func`. Docs and the hook test follow.

Co-Authored-By: Claude Mythos 5.1 <noreply@anthropic.com>
#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>
miles/ray/rollout.py imports imageio_ffmpeg to mux the eval audio; the
GPU image had it only as a transitive dependency and the hosted CPU
suite not at all, so tests/fast/rollout/test_eval_media.py failed to
import there.

Co-Authored-By: Claude Mythos 5.1 <noreply@anthropic.com>
Whether a hook forwards the teacher is the hook's business; the argument
layer cannot tell from --loss-type and the hook paths who will read it,
so the check only ever guessed. --teacher-load is now accepted under every
loss type (dmd2 still requires it).
Stop filling the --custom-*-path arguments on behalf of nft / dmd2 and
stop overriding them per trainer role: a group's prepare, formula,
train-input and train-output hooks come from loss_hub.LOSS_HOOKS keyed by
(loss_type, role), the way miles core's get_loss_function does. Only
custom_loss reads the path flags. The critic overrides shrink to the six
values that really differ (checkpoint, lr, load/save dirs, steps, batch).
A path given under any other loss type would be silently ignored now that
the table selects the hooks, so fail at parse time instead.
DMD2 has no log-prob ratio to recompute, and once the recompute loop
passes its real step index the critic's last window would run its
scoring prepare and formula a second time. sft_loss and nft already
reject the flag.
microbatch_id and optim_step_idx were written onto DiffusionLossContext
before every forward and read back by the hooks; the planned micro-batch
prefetch would race on them. They are now required keyword arguments of
every prepare and formula hook, passed by the two trunk loops; the
numbering is unchanged (training from 0, the old-log-prob recompute of
windows 1.. reuses their training forwards' numbers).
The DMD2 critic formula appended its scoring inputs to a list on the
context and the train-output hook read them back: a hook-to-hook channel
invisible in the call graph. The trunk now owns one list per train call
and passes it to the formulas and to the train-output hook.
Nothing writes the context after construction any more: it is the
rollout-level, read-only view the hooks share. eq=False keeps identity
comparison; a synthesized hash would fail on the dict fields.
prepare_critic_batch did three things in one body: build the SFT batch,
pick the scoring step's grid range, and collate the teacher's CFG inputs.
The teacher part is its own function now, _request_teacher_forward, and
the two helpers it shared with #351 are back, so the critic's prepare
reads as SFT plus the last step's teacher request. No behaviour change.
The DMD2 recipe only knew its 2 x 8 topology. Give it the knobs an e2e
needs: --num-nodes / --num-gpus-per-node (one node starts its own ray
cluster), --critic-steps-per-rollout, --eval-interval 0 / --save-interval 0
to drop eval and checkpoints (0 used to reach should_run_periodic_action's
modulo), --fsdp-cpu-offload and --master-dtype. The three 31B models do not
fit two GPUs with their optimizers, so the proxy keeps parameters, gradients
and optimizer states in host memory with a bf16 master and steps on the CPU.

tests/e2e/short/test_h3_t2va_dmd2_2xGPU.py runs 2 rollouts of 4 prompts
(2 critic steps of 2, 1 student step on the last 2) under --deterministic-mode
and checks the critic loss and grad norm, the student grad norm and the DMD2
metrics against the recorded standard. The 16-GPU defaults are unchanged.

Co-Authored-By: Claude Mythos 5.1 <noreply@anthropic.com>
…roxy

The standard comes from record-e2e-standards.yml on the 3gpu runner
(run 42809959116, sglang zhihengy/sglang:dmd2/h3-dmd2-e2e), not from a
local run. The test is renamed to the 16xGPU_single_node_2xGPU_proxy
form the wan22 and krea2 proxies use, since it runs the 16-GPU recipe
shrunk to two GPUs.

Co-Authored-By: Claude Mythos 5.1 <noreply@anthropic.com>
record-e2e-standards.yml run 43032161294 on the 3gpu runner, this branch
at daff1b5 on sglang-miles 47930723 (the tip with sgl-project/sglang#49440
and #49143, so no ci-sglang-pr override). All 22 metric points match the
two records on the previous combo branch bit for bit; only meta.commit moves.

Co-Authored-By: Claude Mythos 5.1 <noreply@anthropic.com>
zhihengy and others added 3 commits November 12, 2026 00:08
Co-Authored-By: Claude Mythos 5.1 <noreply@anthropic.com>
…d before the training forward

A prepare hook can now return a second batch on the same pairs, `PreparedBatch.forward_only_batch`. The trunk runs
the teacher it asks for and its model on it without grad, before the training batch's forwards, and hands the two
predictions to the formula as `forward_only_teacher_pred` and `forward_only_pred`; a formula whose prepare sets no
such batch receives neither. The trunk's forwards of one batch move into a local function it calls once per batch.

For the second batch's draw, the hooks' ctx carries the rank's micro-batches per optimizer step once the schedule
exists: a hook that adds it to its micro-batch id gets an id no training forward of its own or a later step has
when its step is the last.
…ate scoring forward

`reuse-critic-step`, the default, keeps the current behavior: the critic's last training forward, drawn in the
scoring range, is its score. Under `original` the last step trains plain SFT on the full grid and its prepare hook
also returns the scoring batch, a separate draw in the scoring range with the teacher asked for, as the step's
forward-only batch; the formula keeps the x0 of that batch's teacher and critic forwards, which the trunk runs
before the step's training forward and so before its update, as tianweiy/DMD2 does. Both modes score the same
samples and hand the actor the same structure, through train_diffusion.py.

The scoring batch and the kept x0 are factored into `prepare_scoring_batch` and `keep_scores`, which both modes use.

Suggested by Rockdu on #375.
@zhihengy zhihengy added the run-ci-basic Authorize and run only basic unlabeled CI tests label Nov 20, 2026
@zhihengy
zhihengy marked this pull request as ready for review November 23, 2026 23:27
@Rockdu
Rockdu deleted the branch dmd2/06b-h3-dmd2-e2e November 25, 2026 06:22
@Rockdu Rockdu closed this Nov 25, 2026
@Rockdu Rockdu reopened this Nov 25, 2026
@Rockdu
Rockdu changed the base branch from dmd2/06b-h3-dmd2-e2e to main November 25, 2026 06:23
@Rockdu
Rockdu force-pushed the dmd2/07c-original-scoring-in-trunk branch from f2f18e9 to 3c339bf Compare November 25, 2026 07:19
@Rockdu Rockdu added the run-ci-e2e Run e2e metric-regression tests on this PR label Nov 25, 2026
…he trunk runs them off PreparedBatches

Each stage of a micro-batch is a model and the batch it is sent: the training forward (the EMA and reference
forwards take its batch too), the frozen teacher on its own batch (its own CFG fields and conds), and the
actor's extra no-grad forward on a second batch. The prepare hook returns them as PreparedBatches(actor,
teacher, actor_forward); PreparedBatch is one stage's input and holds no batch of its own type. The trunk runs
the stages in order through one _compute_noise_pred(model, batch) and derives nothing; the formula receives
each stage's prediction next to its batch (teacher_pred / teacher_batch, actor_forward_pred /
actor_forward_batch), its positional `prepared` staying the training batch.

Each loss module keeps a builder that returns a PreparedBatch (sft_batch, nft_batch, flow_grpo_batch,
generator_batch, scoring_batch) and registers the prepare_*_batch hook that wraps it; DMD2 composes its
stages from sft_batch and scoring_batch, so the DMD2-only knobs (grid_index_range, draw) stay off the public
hook.

The family's input_dtype_policy is resolved once per run (InputDtypes, in the actor's __init__) and each
stage's inputs are cast with it as they are sent: PreparedBatch.model_inputs(input_dtypes) returns
compute_noise_pred's kwargs in the boundary dtypes, and the batch's own fields keep the rollout dtypes for the
formulas. No stage prepares again inside the trunk and no batch carries a cast copy.

DMD2's scoring draw under --dmd2-critic-scoring original is a named draw on the same micro-batch
(sft_batch's draw="scoring" enters the seed) instead of a micro-batch id offset, so the hook needs no count of
micro-batches per step and DiffusionLossContext.num_microbatches_per_optim_step goes away.
@Rockdu
Rockdu force-pushed the dmd2/07c-original-scoring-in-trunk branch from 3c339bf to 07a6f9d Compare November 25, 2026 07:25
@Rockdu

Rockdu commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

Added two commits on top of yours (untouched): 07a6f9d reworks the trunk, 3debb87 takes the DMD2 proxy recipe / test / standard as #366 merged them, so the branch merges cleanly. #418 is folded in; an earlier force-push of this branch is undone.

The idea behind the rework: batches are not grouped by whether a forward has grad, but by which model is fed. The teacher gets its own batch (its own CFG and conds), the actor gets its training batch, and when the critic needs a second forward of its own model it gets a second batch; the EMA and reference forwards take the actor's batch, since their inputs must be exactly the actor's. So:

  • The prepare hook returns PreparedBatches(actor, teacher=None, actor_forward=None), one batch per model that forwards; PreparedBatch is flat again, no forward_only_batch. The trunk forwards each through one _compute_noise_pred(model, batch) and derives nothing; the formula gets teacher_pred / teacher_batch and actor_forward_pred / actor_forward_batch instead of forward_only_*.
  • sft_batch etc. build one PreparedBatch; prepare_*_batch wrap them. DMD2 composes its batches from sft_batch / scoring_batch.
  • input_dtype_policy is resolved once (InputDtypes); PreparedBatch.model_inputs(input_dtypes) casts on send.
  • The scoring draw is draw="scoring" in the seed instead of microbatch_id + m; num_microbatches_per_optim_step is gone.

Default mode is numerically unchanged; run-ci-e2e on this head is the check. 292 CPU tests pass. The prepared -> prepared_batch rename is a follow-up PR.

@zhihengy zhihengy added run-ci-e2e Run e2e metric-regression tests on this PR and removed run-ci-e2e Run e2e metric-regression tests on this PR labels Nov 25, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

run-ci-basic Authorize and run only basic unlabeled CI tests run-ci-e2e Run e2e metric-regression tests on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants