Skip to content

[sglang-miles] [Diffusion] Return the negative cond from a rollout that runs no CFG - #45523

Open
zhihengy wants to merge 9 commits into
sgl-project:sglang-milesfrom
zhihengy:dmd2/neg-cond-without-cfg
Open

zhihengy wants to merge 9 commits into
sgl-project:sglang-milesfrom
zhihengy:dmd2/neg-cond-without-cfg

Conversation

@zhihengy

@zhihengy zhihengy commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

DMD2 distillation in miles-diffusion (radixark/miles_diffusion#333) scores the student's samples with the teacher under CFG, inside the trainer. The trainer has no text encoders, so it replays the cond that the rollout returns in denoising_env. The student samples unguided (guidance 1), and then the engine never encodes the negative prompt: neg_cond_kwargs comes back empty and the teacher can't run CFG.

Modifications

  • RolloutRequest / SamplingParams: new rollout_return_neg_cond_kwargs, default False (no behavior change).
  • Req.needs_negative_cond is true under CFG or for a rollout that sets the field. The stages that produce the negative cond read it instead of do_classifier_free_guidance:
    • TextEncodingStage encodes, appends and checks the negative prompt. Its dedup fingerprint uses the same flag, so a grouped request without the field never hands its empty negative cond to one with it.
    • SD3ConditioningStage merges the negative embeddings.
    • DenoisingStage builds neg_cond_kwargs, which the rollout hook already copies into denoising_env.
  • Sampling still reads do_classifier_free_guidance. CFGPolicy.build adds the unconditional branch only under CFG, so such a request still runs one DiT forward per step.
  • Pipelines with their own text-encoding stages keep their gates and return no negative cond for such a request; the trainer rejects a missing one explicitly.

The flag is independent of #45230, and the two branches merge without conflicts.

Accuracy Tests

FLUX.2-klein-base-4B, 1×B200, 512², 4 steps, cps / noise level 1.0, negative prompt "blurry, low quality", same seed for every request:

request neg_cond_kwargs in denoising_env vs the guidance-1 request without the field
guidance 1 None (the gap) -
guidance 1 + rollout_return_neg_cond_kwargs present; bitwise equal to the guidance-4 request's pos_cond_kwargs, trajectory latents and per-step model outputs bitwise equal; 4 DiT forwards
guidance 4 (CFG) present -

Speed Tests and Profiling

Same setup, median of 6 requests after warmup:

request engine time (inference_time_s) response size
guidance 1 0.052 s 18.3 MiB
guidance 1 + rollout_return_neg_cond_kwargs 0.053 s 26.5 MiB

After the first request the negative embedding comes from the conditioning cache, so the extra cost is the negative cond in the response.

Checklist

  • pre-commit hooks (isort 7.0.0, ruff 0.15.1) are clean on the changed files.
  • Unit tests:
    • test_text_encoding_cache.py: a request with the field gets the negative cond without CFG, also when it is grouped with one without the field. The test fails if the dedup fingerprint ignores the field.
    • test_cfg_gating.py: a negative cond without CFG keeps one branch.
    • test_rollout_api.py: the field is forwarded to the sampling params.
    • 141 passed and 1 skipped across the touched test files. 423 passed across 17 diffusion unit test files that exercise these stages. The 2 failures there (test_ideogram4.py fp8 text encoder: global server args not set) also fail on sglang-miles 203f4bb.
  • Documentation: none needed; the field is documented next to its definition.
  • Accuracy and speed results are above. Validation was run locally.

CI States

Latest PR Test (Base): ⚠️ Run #45276878526
Latest PR Test (Extra): ⚠️ Run #45276879136
Latest PR Test (AMD ROCm 10): ⚠️ Run #45276879784

Tip

Base CI only covers the core cases, and its workload may not exercise your change. Use /rerun-test to run the tests your change touches, or copy this prompt for an agent:

Find the registered tests this PR affects and run them with /rerun-test, following https://github.com/sgl-project/sglang/blob/main/.agents/skills/rerun-test/SKILL.md

@github-actions github-actions Bot added the diffusion SGLang Diffusion label Oct 21, 2026
@zhihengy
zhihengy marked this pull request as ready for review October 24, 2026 00:58
@github-actions

This comment has been minimized.

@zhihengy
zhihengy force-pushed the dmd2/neg-cond-without-cfg branch from 726ee60 to 087964f Compare November 17, 2026 01:28
@github-actions

This comment has been minimized.

…returned pre-connector embeddings

Qwen-Image 2.1 raised KeyError: 'qwen21_negative' for the same request. Declared: Wan, Qwen-Image, SD3, FLUX.2 and Krea2, whose negative cond comes only from stages that read Req.needs_negative_cond.
…on: the old case passed with the gate reverted
@github-actions

Copy link
Copy Markdown
Contributor

Required checks failed on 67ac9bd; this PR cannot be merged until they pass.

PR title (run)

PR title must use "[Type] scope: title" or "[Type] [Hardware] scope: title". Type must be one of: Feat, Fix, Refactor, Perf, Doc, Test, CI, Chore, Misc. Scope is required and may be any module or model name, with unrestricted letter case (letters, numbers, dots, underscores, slashes, or hyphens). Hardware is optional; use a single tag without spaces. Use uppercase hardware tags (AMD, NVIDIA, NPU, MI300X), except for exactly these four spellings: Intel, Apple, Huawei, Ascend. Reverts must use GitHub's default format: Revert "original title". Use one space between fields and after the colon, with a non-empty title and no leading or trailing whitespace. Examples: "[Fix] attention: Fix KV cache indexing", "[Feat] qwen3: Add model support", "[Perf] [AMD] flash-attn: Optimize decode performance".
Edit the title; the check reruns on its own.

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

diffusion SGLang Diffusion

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant