Skip to content

[DMD2] feat(dmd2): fetch the negative cond of unguided rollouts for a teacher that runs CFG - #419

Open
zhihengy wants to merge 8 commits into
fix/qwen-image21-cfg-branch-tailsfrom
dmd2/09-teacher-cfg-neg-cond
Open

zhihengy wants to merge 8 commits into
fix/qwen-image21-cfg-branch-tailsfrom
dmd2/09-teacher-cfg-neg-cond

Conversation

@zhihengy

@zhihengy zhihengy commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

--dmd2-teacher-guidance-scale above 1 could not run: the student's rollouts are unguided, and the engine returns a negative cond only for a request that runs CFG, so the teacher's CFG forward stopped at "needs the rollout's negative cond". With this PR the rollout fetches it once per run and no engine change is needed.

Draft: it is an alternative to sgl-project/sglang#45523 (a request field that makes every unguided sample return its negative cond), opened to compare the two. Stacked on #420, the Qwen-Image 2.1 CFG fix the e2e below needs.

What

  • fetch_neg_cond in sglang_diffusion_rollout.py: one POST /rollout/generate with the run's sampling params, CFG on at the teacher's scale, stopped after step 0, tensors inline. It raises when the reply has no negative cond. generate_rollout_async calls it before the first rollout and keeps the result on GenerateState.
  • generate_and_rm_microgroup attaches it to every training sample whose denoising_env has no negative cond, after the built-in or a custom generate function. A sample that has its own keeps it.
  • args.rollout_neg_cond_guidance_scale, derived in miles_validate_args: --dmd2-teacher-guidance-scale under dmd2 when that is above 1, else None. The rollout reads only this value.
  • A model that trains unguided (supports_cfg_training=False, i.e. LTX and H3) now rejects a teacher scale above 1 at argument validation: its forward drops the negative branch, so the teacher would score unguided.

Why

  • The negative cond is a constant of the run. Measured on the engine, bitwise: the stopped request's negative cond equals the one a full CFG sample returns; it does not change with the prompt, an empty prompt included, or the seed on FLUX.2-klein-base, Wan2.1-T2V-1.3B, Qwen-Image, Qwen-Image 2.1 and Z-Image; it changes with the negative prompt and, where it holds position tables, with the sample size. Cosmos3 pads it to the prompt, and already returns one with every sample, which is why a sample's own is kept.
  • It needs nothing from the engine beyond its CFG path, so sglang-miles works as it is. sglang#45523 needs each pipeline to produce the negative cond without CFG and sets its declaration on 6 pipeline classes; Qwen-Image 2.1 is not among them.
  • Cost on the engine, 4 steps, one H200: the field adds the same tensor to every sample's reply (FLUX.2-klein-base 512²: +8.25 MiB and +29 ms per sample; Wan2.1-1.3B 832x480x17: +8.0 MiB and +41 ms), the fetch is one request per run (85 ms and 153 ms).

Limits

  • Of the families miles-d trains with CFG, the constancy is measured for Qwen-Image and Qwen-Image 2.1 and read from the engine code for SD3 and Krea 2; Wan2.2 was measured on the Wan2.1 pipeline class only. A model whose negative cond follows the prompt and that returns none of its own would get a wrong one without an error; no built-in family is known to be one.
  • Qwen-Image (not 2.1): the teacher's CFG is the plain combine. The trunk calls the teacher with true_cfg_scale=None, and that config rescales to the cond norm only under a true-CFG scale, while the engine and diffusers always rescale. This path could not run before; whether the teacher should rescale is a numerics decision this PR does not take.
  • One sample size per run: the negative cond is fetched once, for the run's --diffusion-height/width.
  • A model whose negative cond depends on the sample (image editing, I2V) is not covered.
  • The engine needs a negative prompt to run CFG. Where the model's default has none (Qwen-Image 2.1), pass --diffusion-negative-prompt; without it the fetch raises.

Tests

  • test_rollout_neg_cond.py: the fetch sends the run's params (negative prompt and size among them) with CFG at the scale, stop index 0 and no output store, leaves the run's params unchanged and returns the reply's negative cond; a reply without one raises; generate_rollout_async fetches before the first rollout only; after the built-in and after a custom generate function, a training sample without a negative cond gets the fetched one and a sample with its own keeps it. Each of these fails when its part of the code is removed.
  • test_loss_type_recipes_parse.py: a DMD2 teacher scale of 4.5 sets the fetch scale, 1.0 leaves it unset and so does another loss type that carries the flag; a model that trains unguided rejects 4.5.

Validation

  • pytest tests/fast in the CI CPU environment at 9e15397: 839 passed, 20 failed; main e6129d0 in the same environment: 830 passed and the same 20 failed (output-store tests).
  • e2e, not part of the PR: DMD2 on Qwen/Qwen-Image-2.1, 2 H200, the CI-recipe environment, engine sglang-miles 0d5ab02 (without sglang#45523), 512², 4 steps, 2 rollouts of 4 samples, 2 critic steps.
    • --dmd2-teacher-guidance-scale 4.0 --diffusion-negative-prompt blurry at 9e15397: the job succeeds in 302 s. The engine serves 9 /rollout/generate requests, against 8 for the same run at scale 1.0.
    • The same run at 5d9c82c (twice) and at 8735c42: the logged values outside perf/* and device memory are bitwise equal across the four runs.
    • Against the scale-1.0 run: the critic's losses of the first rollout are equal and the actor's metrics differ (dmd2_grad_abs_mean 0.681 vs 0.0195, dmd2_teacher_dist_abs_mean 0.819 vs 0.351 at its first step).
    • Scale 4.0 without --diffusion-negative-prompt at 8735c42: the job fails at the fetch, after one request and before any rollout sample, with "returned no negative cond".
  • Not executed: other model families; a run with the output store; a custom generate function, or several outputs per request, on a real engine; CI (the PR carries no run-ci-* label yet).

Checklist

  • black, isort and ruff at the pinned versions pass on the changed files
  • Added/updated tests for new behaviour
  • --dmd2-teacher-guidance-scale in the CLI reference says where the negative cond comes from
  • No new flag, no example added

…r that runs CFG

One request with CFG on, stopped after its first step, per run; its negative cond goes to every training sample that has none. --dmd2-teacher-guidance-scale > 1 could not run before: the student samples unguided, and the engine returns a negative cond only for a request that runs CFG.
…function

A --custom-generate-function-path run fetched it and then dropped it, since only generate_microgroup attached it. The tests now also drive the fetch from generate_rollout_async and check that the run's negative prompt and size reach the request: both parts could be removed without a test failing.
Without a negative prompt the engine runs no CFG, and the run went on through a rollout and the critic's first steps before the trainer raised. The trainer's check names no cause again: a custom rollout function or a saved batch reaches it too.
…ould own a stored copy

The comment said the store deletes it with the first rollout; nothing would delete it.
@zhihengy
zhihengy changed the base branch from main to fix/qwen-image21-cfg-branch-tails November 24, 2026 00:07
…uard is checked

Without the scale in its argv the assertion held with or without the guard.
…d args are set

It sat with the defaults of unset flags, so the tests that pin what those defaults write had to carry it.
…t worth a request field

The reply's negative cond does not depend on it.
… validation rules out

The student never runs CFG under dmd2, so only an engine that always returns a negative cond keeps its own.
@zhihengy
zhihengy marked this pull request as ready for review November 24, 2026 03:12

@Rockdu Rockdu left a comment

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.

LGTM

@Rockdu Rockdu added the run-ci-e2e Run e2e metric-regression tests on this PR label 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-e2e Run e2e metric-regression tests on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants