Skip to content

feat(fsdp): score centering for Flow-GRPO under train/rollout mismatch - #298

Merged
Rockdu merged 20 commits into
radixark:mainfrom
HJSang:feat/flow-grpo-score-centering
Nov 11, 2026
Merged

Rockdu merged 20 commits into
radixark:mainfrom
HJSang:feat/flow-grpo-score-centering

Conversation

@HJSang

@HJSang HJSang commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

What

Adds --diffusion-score-centering (experimental) to the Flow-GRPO loss. It ports score centering from Score Centering Stabilizes Off-policy Reinforcement Learning (arXiv:2609.20807) to SDE transitions.

Why

Rollout transitions are drawn from the engine's N(μ_q, s²) and re-scored by the trainer under N(μ_θ, s²). When the two disagree numerically, E_q[∇θ log p_θ] ≠ 0, so the policy gradient picks up a drift term, E[A] · E_q[∇θ log p_θ]. It distills the trainer toward the rollout, and weight sync feeds the bias back each step. Our own committed e2e standards show the mismatch is real:

Recipe Window-0 log_prob_mean_abs_diff --diffusion-clip-range
SD3.5 OCR ~1e-4 1e-4
Qwen-Image ~3e-5 1e-4
H3 ~4–6e-5 1e-4
Cosmos3 0.0 (recompute hides it; model_output_mean_abs_diff ~0.05) 1e-3

The PPO log-prob is a per-element mean, so exp(log p_θ − log q) is not an importance weight and does not cancel drift. That makes Flow-GRPO behave like uncorrected policy gradient plus a clip mask.

For a Gaussian step with a θ-independent std the correction is exact and needs only μ_q: ∇θ E_q[log p_θ(x')] = ∇θ log p_θ(μ_q). No top-k approximation is needed, unlike the LLM case.

Changes

  • refactor: SdeStepBackend.sde_step_logprob returns a typed SdeStepOutput (a NamedTuple) that also carries the transition noise_std.
  • feat: --diffusion-score-centering (Flow-GRPO only).
    • Requires --diffusion-debug-mode, the only way sglang-diffusion returns rollout_prev_sample_means today.
    • The correction is scaled by each pair's PPO gradient weight, which is 0 where the clip binds.
    • It is zero-valued in the forward pass, so logged losses are unchanged.
    • SdeStepBackend.expected_log_prob is the override point for non-Gaussian backends.
    • It lives in loss_hub/score_centering.py (score_centering_term); the Flow-GRPO loss only adds that term when the flag is on.
  • metric: train/prev_mean_diff_over_noise_std, the RMS trainer-vs-rollout transition-mean gap in noise-std units. It is logged whenever debug tensors are present.
  • recipe: scripts/run_diffusion_grpo_krea2_async.py, Krea-2 async Flow-GRPO + OCR on 4 training + 4 rollout GPUs, 30 prompts × 8 samples per step, 300 rollouts, an fp8 rollout engine with dynamic LoRA, and the output store with a 64gb segment (debug mode stores every step's debug tensors, beyond the 16gb default). --no-score-centering is the plain async baseline.
  • merge: main is merged in for the output store (feat(rollout): add --use-output-store with its Mooncake master and the engines' store config #359–feat(fsdp): read a shard's store slices back from the output store before training on it #361, refactor(rollout): read the output store one field per sample and step #370, test(e2e): run the RL e2e tests with the output store on, keep two inline gates #373).
  • docs: SDE Step Backend §5, the CLI reference, and the dtype-control metrics table. Krea-2 §4.4 (the recipe, now 📈 V) with reference curves in §5.3 (score centering on/off, two seeds, fp8 rollout) and §5.4 (600 rollouts with score centering). Score centering is marked experimental in the CLI help and in the headings of every docs section about it.
  • tests:
    • CPU unit tests for both SDE backends:
      • With a constant advantage, an antithetic-noise check shows the expected update is exactly zero with score centering, and exactly the drift without it.
      • Exact no-op when the means agree.
      • Clip-masked pairs stay masked.
      • Logged loss is unchanged.
      • Argument validation rejects bad combinations.
    • New e2e test_sd3_ocr_grpo_score_centering_2xGPU (label e2e, stage-c-3-gpu-h200). It runs the canonical SD3.5 OCR recipe with --diffusion-score-centering --diffusion-debug-mode --diffusion-recompute-old-log-prob.

Testing

  • Touched tests plus the sibling NFT/SFT loss tests (macOS CPU, torch 2.11.0): 47 passed.
  • Mutation check: flipping the correction's sign fails 2 tests, and dropping the clip mask fails 2 tests.
  • Full tests/fast: 414 passed, 2 failed. test_hybrid_shard_mesh::test_mesh_axes and test_metric_buffer_dist::test_metric_buffer_reduction fail identically on main on that machine (gloo cannot resolve the macOS hostname).
  • ruff, isort, black and autoflake are clean on the changed files.
  • GPU, Krea-2 recipe above, fp8 rollout (trainer-vs-rollout log-prob gap ~3e-5), two seeds per arm:
    • Without score centering both seeds go through repeated clip bursts (clip fraction up to 0.9–1.0), and the reward collapses to 0 near rollout 270 (seed 1) and 200 (seed 2).
    • With score centering the clip fraction stays ≤ 1.7%. The mean reward of the last 10 rollouts at rollout 300 is 0.902 and 0.842. Seed 1 continued to 600 rollouts, ending at 0.953.
    • With a bf16 rollout neither arm collapses within 425–450 rollouts, and score centering learns somewhat slower.
    • These runs used the inline rollout transport. With the output store (64gb segment), the first 3 rollouts of seed 2 with score centering are bitwise identical to the inline transport in every logged metric, and to the seed-2 run above.
  • The new e2e has no standard yet.

Needs a maintainer

I don't have write access, so this is a fork PR. It can't dispatch workflows and gets no CI secrets (the gated SD3.5 download needs HF_TOKEN). To get it onto the H200s:

  1. Push this branch to radixark/miles_diffusion.
  2. Dispatch Record E2E Standards with test=tests/e2e/short/test_sd3_ocr_grpo_score_centering_2xGPU.py and runs_on=["h200", "3gpu"].
  3. Commit the uploaded tests/ci/fixtures/e2e_standards/test_sd3_ocr_grpo_score_centering_2xGPU.json to this PR.
  4. Add run-ci-e2e so stage C verifies the run bit for bit.

Deliberately not in this PR

  • An engine-side flag that returns only the SDE-step prev-sample means. Debug mode ships all four debug tensors for every step.
  • An A/B study with other relaxed parity patches such as fused kernels. The fp8-rollout A/B is in the Krea-2 docs.
  • Model families whose rollout mean layout differs from the trainer's. The correction raises instead of guessing; only SD3.5 is exercised by the new e2e.

🤖 Generated with Claude Code

HJSang and others added 6 commits November 3, 2026 17:36
Callers unpacked a positional 4-tuple, and the transition std that log_prob
scores against (noise_std) never left the backend; flow-SDE keeps it distinct
from the std_dev_t the KL uses. A NamedTuple keeps the reference field order
and adds noise_std so the loss can evaluate log_prob at another mean.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Rollout transitions come from the engine's N(mu_q, s^2) while the trainer scores
them under N(mu_theta, s^2). Any numeric gap makes the expected score nonzero,
so the policy gradient gains a drift term, E[A] times that expected score, that
distills the trainer toward the rollout and compounds through weight sync
(arXiv:2609.20807).

--diffusion-score-centering subtracts the expected score. For a Gaussian SDE
step with a theta-independent std it is exact, grad E_q[log p] = grad log p(mu_q),
so it needs only the rollout's per-step means, which sglang-diffusion returns
under --diffusion-debug-mode. The correction is scaled by each pair's PPO
gradient weight, zero-valued in the forward so logged losses stay comparable,
and a no-op when trainer and rollout agree.

train/prev_mean_diff_over_noise_std logs the trainer-vs-rollout mean gap in
noise-std units whenever debug tensors are present; it flattens per pair like
the model_output diff, so only the correction requires matching layouts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Same recipe, topology and determinism as test_sd3_ocr_grpo_2xGPU, plus
--diffusion-score-centering --diffusion-debug-mode
--diffusion-recompute-old-log-prob, so the rollout -> trainer path for the
per-step means and the correction run on GPU. Labelled e2e so it is opt-in on
PRs and runs nightly. Its standard must be recorded on the 3-GPU H200 runner.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Subtracting E_q[score] and re-weighting by the sample's own clipped ratio
leaves E_q[1{inside} r (score - E_q[score])] in the gradient. Flow-GRPO's
per-element mean log-prob makes log r normal under the rollout Gaussian, so
E_q[clip_A(r)] has a closed form in kappa = |mu_theta - mu_q|^2 / sigma^2; the
loss adds A * (center - center.detach()) with a hand-written d/dkappa that is
finite at kappa == 0. The rollout's noise stds and stored log-probs are checked
against the trainer's before the closed form is trusted, and the argument
checks grow from two to six (sde type, stock backend, no recompute, clip range).
@Rockdu
Rockdu force-pushed the feat/flow-grpo-score-centering branch from 5e86198 to 0d8bea9 Compare November 4, 2026 01:46
The one-rollout overlap does not depend on the loss: under policy_loss the engines sample with the previous step's actor weights instead of the EMA. This is the setting score centering is meant for.
…cipe

Same 6 + 2 pools, batch, LoRA and OCR eval as the Krea-2 async NFT recipe, with SDE rollouts (noise 0.7, SDE steps 3-4 of 10), PPO clip 1e-4, no KL and no EMA. --no-score-centering runs the plain one-step-async baseline with everything else unchanged. Listed as NV: no complete curve has been run on this code.
…tandard

It never had a standard to compare against, so it could not pass in stage-c. The CPU tests cover the closed-form center, the loss integration and the argument checks.

@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, adjusted some formula and settings

@Rockdu Rockdu added the run-ci-e2e Run e2e metric-regression tests on this PR label Nov 4, 2026
@Rockdu
Rockdu merged commit 5823c16 into radixark:main Nov 11, 2026
10 of 13 checks passed
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