Repository navigation
refactor(fsdp): run the DMD2 teacher forward in the actor, requested by the critic's prepare hook - #372
Merged
Conversation
`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>
…re micro-batching
…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>
…e the student's samples
… from the critic group's scores
#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>
…metrics to judge it
… reward reads the media
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>
Merged
5 of 6 tasks
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.
…by the critic's prepare hook
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.
Rockdu
force-pushed
the
dmd2/refactor-c-ctx-loop-state
branch
from
November 11, 2026 13:03
b6aee00 to
60acc02
Compare
Rockdu
force-pushed
the
dmd2/08-teacher-in-trunk
branch
from
November 11, 2026 13:03
9ea9eb3 to
b9e206a
Compare
4 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stacked on #368 (the ctx-loop-state PR), which sits on #367 and #343: the diff here is the 2 commits above it. It does what #368 left to the DMD2 stack's author: the teacher forward moves out of the hooks into the trunk.
What
PreparedBatch.teacher_guidance_scale(defaultNone): a prepare hook asks the trunk to run the frozen teacher (--teacher-load) on this micro-batch at that CFG scale;Noneruns no teacher._forward_train_pair_batchruns it the way it runs the reference: without grad, with CFG overprepared.neg_cond/joint_condand the family'scfg_batching(andtrue_cfg_scale=None, since several families let it overrideguidance_scale), resharded right after, and hands the prediction to every loss formula asteacher_pred(Nonewhen not requested). It runs before the training forward, so the teacher's gathered parameters and its forward temporaries never share the GPU with the training activations. A request without--teacher-loadis aValueError.prepare_critic_batchsets the scale on the scoring step and collates the rollout's negative cond for it (the missing-negative-cond check moves here, before any forward);critic_loss_formulakeeps(sample_index, x0_real, x0_fake)fromteacher_predand the detachednew_pred, before the x0-loss conversion;score_last_step_samplesonly packs them onto the host._predict_velocityand_score_with_teacher's second copy of the forward wrapper are deleted.DiffusionLossContext.teacher_modelsis removed: no hook forwards the teacher any more.flow_grpo,sft,nft,dmd2) taketeacher_pred=None.Why
The hook contract is "DiT forward always stays in the FSDP actor; the loss hook only computes the objective" (customization.md): prepare turns data into model inputs, the loss turns model outputs into the objective, the train-output hook post-processes. The teacher forward in
score_last_step_sampleswas the one forward a hook ran, with its own copy of the dtype-policy / autocast /compute_noise_predwrapper. The teacher now follows the reference: the prepare hook declares the forward's inputs, the actor executes it, the loss consumes the prediction.Behaviour ledger: the teacher forward count is unchanged (only the scoring step asks for it); it runs before the critic's forward instead of after the critic's steps; the scores are bitwise the same (below). A custom formula without
**kwargsand withoutteacher_predfails withTypeError(the documented signature has**kwargs); readingctx.teacher_modelsis anAttributeError.Validation
CPU, on this head:
tests/fast(backends,utils,rollout,test_train_diffusion.py)test_sft_read_mediacases that fetch a sample from the Hub (this environment is offline), failing the same way on the basetest_loss_hub_dmd2.pyx_s - sigma vof each forward, carry no grad, and the critic still gets its gradient; a missing negative cond is rejected at preparetest_reference_models.py_forward_train_pair_batchruns the teacher first, without grad, with(use_cfg, cfg_batching, guidance_scale, true_cfg_scale)of its own and the prepared batch's for the actor, reshards it, handsteacher_predto the formula; no forward when the prepare hook does not ask; a request without a teacher is rejectedpre-commit run --filesGPU, 2x H200, the #366 e2e (
test_h3_t2va_dmd2_16xGPU_single_node_2xGPU_proxy: 2 rollouts of 4 prompts, 2 critic steps,--deterministic-mode), this PR's base plus the e2e recipe commits vs this PR plus the same commits:train/critic-peak_memory_allocated_gbon the critic's 4 steps,train/peak_memory_allocated_gbon the actor's 2train/critic-device_memory_used_gbon the two scoring steps (device-wide, after the step)Both trees ran with this environment's torch 2.13 port of the FSDP param-dtype patch and the H3 snapshot path, which are not part of this PR.
Files
miles/backends/fsdp_utils/loss_hub/types.py:PreparedBatch.teacher_guidance_scale;teacher_modelsoff the contextmiles/backends/fsdp_utils/actor.py: the teacher forward in the trunk,teacher_predto the formulamiles/backends/fsdp_utils/loss_hub/dmd2.py: prepare requests, loss keeps, train-output packsmiles/backends/fsdp_utils/loss_hub/{flow_grpo,sft,nft}.py:teacher_pred=Nonedocs/fsdp_model_roles.md,docs/user-guide/customization.md,docs/user-guide/cli-reference.mdtests/fast/backends/fsdp_utils/test_loss_hub_dmd2.py,test_reference_models.py,test_train_input_hook.py,tests/fast/utils/test_reference_arguments.pyStack
#351 (
--dmd2-critic-scoring original) is rebased on this next: itsscore_held_out_samplescompute phase runs the critic and the teacher itself and becomes a forward-only train phase.Checklist
pre-commithooks pass on the changed filespytest tests/fastis green except the known HF-offline media cases of this environmentpython3 train_diffusion.py --helpstill parses (no flag changed)--teacher-load's row updated)