Skip to content

[H3-Image] feat(h3-image): support full-weight training (per-step head sync and head gradients) - #410

Open
zhihengy wants to merge 17 commits into
h3-image/02-familyfrom
h3-image/04-full-weight
Open

zhihengy wants to merge 17 commits into
h3-image/02-familyfrom
h3-image/04-full-weight

Conversation

@zhihengy

@zhihengy zhihengy commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

[H3-Image] Stacked on #395, beside #400. Full-weight training for MiniMax-H3-Image: the per-step output heads reach the rollout engine, and each head's gradient reduces on its own.

Stack: main <- #393 (step index) <- #394 (loader) <- #395 (family) <- this PR. #400 (LoRA recipe and model page) is the other PR on #395; neither needs the other.

The head sync needs sgl-project/sglang#51654, which is not in sglang-miles yet. It is stacked on sgl-project/sglang#51113, so it carries the image rollout too:

ci-sglang-pr: #51654

What

  • models/diffusers/h3_image/weight_bridge.py, registered for family h3_image, which had no train-to-rollout transform (a LoRA adapter sync does not use one):
    • pdd_proj_out.{step}.{weight,bias} are held until the heads of every step arrived, then leave as one stack per suffix under the name sglang-d's weight update takes them by: final_layer.pdd_fused_heads.video_out.weight [steps, out, in] and .bias [steps, out].
    • Every other weight goes through H3's _to_fl2va.
  • configs/h3_image.py:
    • validate_args no longer refuses a trainer without --use-lora, nor a LoRA synced merged into the DiT's weights. A run whose sync carries the DiT's weights is refused at argument time on an sglang-d that takes no heads, with the two ways out.
    • A trainer without --use-lora is accepted on --fsdp-master-dtype fp32 only (see Why).
    • The refusal of a config named by --train-pipeline-config-path for a syncing run ([H3-Image 3/4] feat(h3-image): add the MiniMax H3 Image train family for t2i Flow-GRPO with LoRA #395) now names the weight transform too: it is looked up by the same unset key, so such a sync would send the Diffusers layout as it is. It comes before the engine check, whose two ways out both lead into it.
    • postprocess_model_after_materialize puts a forward hook on each pdd_proj_out head when the heads are trained: whichever head a forward runs adds every head's parameters to its output at weight zero. A LoRA trainer and a frozen copy get no hook.

Why

  • The bridge. Without one, a weight sync of h3_image sent the Diffusers layout as it is. The engine maps names but not layouts, so the feed-forward's [value, gate] halves would have landed swapped, and pdd_proj_out.* would have been skipped with a warning. [A2]
  • A stack, not six tensors. The engine serves the heads as one stack per suffix, the layout of the release's pdd_fused_heads.safetensors, and its update takes that stack whole: a sender cannot update some steps and leave the others stale. [A5]
  • The prefix comes from sglang (MINIMAX_H3_PDD_FUSED_HEADS_WEIGHT_NAME_PREFIX), so the name has one owner. [A4] It is imported where it is used, not with the module: adaptations imports every family's bridge, and a sglang-miles before #51654 has no such constant. Only a run that sends the heads needs it, and it gets the reason at argument time instead of a rollout that keeps the heads it started with. [A2]
  • The hook. FSDP2 reduces the gradients of one wrap as a single buffer of the parameters whose gradient is not None on that rank, in order (FSDPParamGroup.post_backward). The heads sit in the root wrap and have one shape. A forward runs one head, so when two ranks hold pairs of different steps their buffers line up head against head: head k leaves the reduce with the mean of its own gradient and another head's, and a rank that did not run it has none. With the hook every rank reduces every head, and a head a rank did not run contributes zeros. [A3]
    • Whether a recipe happens to put every rank on the same step at the same micro-batch is a property of how its pairs are ordered, which nothing declares. The hook removes the dependence. [A5]
    • Costs:
      • A head no pair trains (the last step's, under the usual window) has a zero gradient where it had none. AdamW leaves it in place at the default --weight-decay 0, and decays it by lr x weight_decay per optimizer step otherwise.
      • A head trained in some optimizer steps and not in others keeps moving on its momentum in the others.
      • The zero term reads every head's values, so a non-finite value in one head reaches every step's prediction, not only its own.
  • Full-weight needs the fp32 master. Under a lower master dtype diffusers still loads proj_in, the time embedder and the per-step heads in fp32. The root wrap then trains weights of two dtypes, which FSDP2 refuses when the trainer is built, after every rank has loaded the DiT (seen on 8 H200, see Validation). A LoRA trainer on a bf16 master is unaffected: only the adapters are trained. The same pin exists for family h3; a check shared by the families is a follow-up. [A2]
  • Both refusals go because their reasons are gone: [H3-Image 3/4] feat(h3-image): add the MiniMax H3 Image train family for t2i Flow-GRPO with LoRA #395 refused full-weight for the two points above, and a merged LoRA sync because it sends the heads. A merged LoRA sync now rides the same bridge and the same stacks.

[A2] no silent wrongness, [A3] every rank runs the same collective sequence, [A4] one owner per fact, [A5] the structure carries the invariant.

Contract with the engine

sgl-project/sglang#51654: a tensor weight update loads final_layer.pdd_fused_heads.<key> as a head stack, <key> and tensor as in pdd_fused_heads.safetensors. It checks every stack of a payload before it replaces one, narrows each to the TP rank's output shard, and fails the update on a stack it cannot load. H3 Image has no audio, so its keys are video_out.weight and video_out.bias.

Validation

On this box, CI-recipe environment with diffusers 0.43.0 and sglang at sgl-project/sglang#51654 (c299c817b), head b4cb679, except the end-to-end runs, which name their commits.

End to end, 8 H200, FSDP DP=8 colocated with four TP2 engines over CUDA IPC, weights MiniMaxAI/MiniMax-H3-Image at 8ced6006. The LoRA recipe's rollout settings (#400: cps 0.8 on steps 0-4, 512x512, OCR reward, engine debug mode, old log-probs recomputed) on 8 prompts x 2 samples, 2 optimizer steps per rollout, 3 rollouts, lr 1e-4. train/model_output_mean_abs_diff is the trainer's forward against the model output the engine generated the rollout with (mean magnitude about 1):

run rollout 0 rollout 1 rollout 2
full-weight, fp32 master (miles-d e89bd00, sglang 5d670a0b0) first optimizer step after a sync 0.0190 0.0269 0.0201
second optimizer step: the trainer is one step ahead of the engine 0.120 0.401 0.119
LoRA synced merged into the weights, bf16 master (miles-d 5a79901, sglang de2f0c5ce) first optimizer step after a sync 0.0190 0.0240 0.0220
second optimizer step 0.0197 0.0264 0.0227
for scale: the two LoRA runs of #400's settings, adapters synced, same prompts and seeds first optimizer step 0.0208 0.0243, 0.0254 0.0210, 0.0216
  • After every sync the engine and the trainer agree as closely as they do on the base model and in the LoRA runs, rollout by rollout. One full-weight optimizer step without a sync moves the same number to 0.12-0.40, so a sync that missed the backbone or the heads would show.
  • Each run synced 4 times (at startup and after each rollout). On every sync each of the 4 engines logged MiniMax-H3 PDD: head stacks ['video_out.bias', 'video_out.weight'] replaced by a weight update (16 of 16), and no engine logged "Skipping weight update". A sync is 77 buckets per engine full-weight and 37 merged.
  • Both jobs ended SUCCEEDED. Full-weight: a sync takes 42-44 s, the 80 pairs of a rollout train in 9-12 s, a GPU peaks at 89 GB and the host at about 1.39 TB used (1.17 TB of it shared). Merged LoRA: 23-26 s per sync, 78 GB per GPU, 0.94 TB of host memory.
  • The same full-weight launch on --fsdp-master-dtype bf16 failed when the trainer was built, in FSDPParamGroup._init_mp_dtypes ("FSDP expects uniform original parameter dtype"), about three minutes in. That combination is now refused at argument time.
  • lr 1e-4 is not a training setting for 33 B weights: it was chosen so that one step moves the output far past the 0.02 floor (the second step's clip fraction is 0.8-1.0). The rewards of these three rollouts say nothing about training.

Real weights, MiniMaxAI/MiniMax-H3-Image at 8ced6006, no engine process:

check result
every tensor of transformer_diffusers/ (644) through the transform registered for h3_image, against what the engine loads 528 backbone tensors bitwise equal to transformer/ under the emitted name; 2 head stacks bitwise equal to pdd_fused_heads.safetensors; nothing emitted under a name the engine does not load
native tensors the bridge never emits final_layer.video_out.{weight,bias} (the single head, which the engine does not run once the per-step heads are loaded) and the buffer rope.inv_freq
the bridge's two stacks through the engine's WeightsUpdater into a real-size final layer, TP1 and TP2 (ranks built in one process) every rank's stacks bitwise as loaded

GPU, tiny real MiniMaxH3ImageTransformer2DModel with 3 heads:

check result
tests/fast-gpu/.../test_h3_image_head_gradients.py[fp32, bf16]: two ranks under FSDP2, wrapped through apply_fsdp2 with the family's plan, rank 0 on a pair of step 0 and rank 1 on a pair of step 2, in deterministic mode. At a bf16 forward, the default, the heads are fp32 dtype overrides and the root wrap reduces through miles' patched mixed-dtype collectives; at fp32 the plan compiles to nothing and the reduce is torch's own at both: every parameter's gradient shard equals the mean of the two ranks' own gradients (zero where a rank has none), heads included
the same without the hook, fp32 rank 0: pdd_proj_out.0.weight differs from that mean on 1152 of 1152 elements; rank 1: pdd_proj_out.0.weight has no gradient
tests/fast-gpu/.../test_h3_image_weight_sync.py: the DiT's state dict through the updater's weight iterator and bucketing, every tensor in a bucket of its own, plain and PEFT-wrapped with its LoRA merged in the heads leave as the two stacks, equal to the model's; nothing leaves under pdd_proj_out.* or transformer_blocks.*

CPU:

check result
test_h3_image_weight_bridge.py (new) heads arriving out of order are held, then stacked by step under the engine's name; the next sync starts from no held heads; h3_image resolves heads to the stacking transform and everything else to _to_fl2va, h3 is unchanged
test_h3_image.py::test_a_forward_through_one_head_gives_every_head_a_gradient the prediction and every other gradient are bitwise those of the model without the hook; the two heads that did not run have zero gradients where they had none
...::test_a_sync_that_sends_the_heads_needs_an_sglang_that_takes_them (6 cases) full-weight and merged-LoRA syncs are refused on an sglang without the constant; adapter syncs, --train-only and --debug-rollout-only are not
...::test_a_config_named_by_path_cannot_sync_to_an_engine (LoRA adapters, merged LoRA, full-weight) refused through the real parser
...::test_a_config_named_by_path_is_refused_before_the_engine_is_asked_about_heads new: on an sglang without head updates the run gets the config-path refusal, not a remedy that leads into it
...::test_supported_variations_are_accepted, ...::test_unsupported_training_settings_are_rejected full-weight, full-weight --train-only and merged LoRA moved from rejected to accepted. Full-weight on a bf16 master and full-weight --train-only on fp16 are rejected; a LoRA and a rollout-only run on a bf16 master are accepted
...::test_a_run_on_dumped_rollouts_is_not_held_to_a_sync_mode (#395's) now run against an sglang without head updates: with a merged LoRA sync accepted, nothing else refuses the launch whether train_only is derived early or late
tests/ci/run_suite.py --hw cpu --suite stage-a-cpu 706 passed
pre-commit run on the changed files passes

Each new test was checked to fail with its behaviour removed.

Not executed:

Not in this PR

  • A full-weight recipe, its e2e and its curve. The defaults already suit one: --fsdp-master-dtype fp32, --lr 1e-6.
  • Tuning the memory of a full-weight run. The run above peaks at about 1.39 TB of host memory for 8 ranks and 4 engines, against 0.94 TB with a LoRA on a bf16 master.
  • The heads a sync holds live in a module-level dict, as H3's held q, k and v do. Giving a transform per-sync state is a change to the registry's contract, for both families.
  • The weight transform and the LoRA group are looked up by --diffusion-model-family, not by the config class; until they follow the class, a config named by path cannot sync (refused in validate_args).
  • [H3-Image 4/4] feat(h3-image): add the 8-GPU OCR Flow-GRPO recipe with LoRA and the model page #400's model page says LoRA only. Whichever of the two PRs lands second updates it.
  • The opt-in MILES_VERIFY_WEIGHT_SYNC log of the merged-LoRA updater hashes the pairs it pushed and compares with the engine's checksum of its parameters. The pushed pairs now include the two head stacks, which that checksum does not cover, so for H3 Image the log reads a mismatch. It is log-only; whether it matched for H3 before was not established. Covering the stacks on the engine's read side goes with [Feat] diffusion: [sglang-miles] Load MiniMax-H3 PDD head stacks from a weight update sgl-project/sglang#51654's follow-up.

Files

  • miles/backends/fsdp_utils/models/diffusers/h3_image/weight_bridge.py
  • miles/backends/fsdp_utils/adaptations/__init__.py
  • miles/backends/fsdp_utils/configs/h3_image.py
  • tests/fast/backends/fsdp_utils/models/diffusers/h3_image/test_h3_image_weight_bridge.py
  • tests/fast/backends/fsdp_utils/models/diffusers/h3/test_h3_weight_bridge.py
  • tests/fast/backends/fsdp_utils/configs/test_h3_image.py
  • tests/fast-gpu/backends/fsdp_utils/{test_h3_image_head_gradients,_h3_image_head_gradients_worker,test_h3_image_weight_sync}.py

…-step heads as stacks

h3_image had no train-to-rollout transform, so a sync of the DiT's weights sent the Diffusers layout as it
is: the engine maps names but not layouts, and skips pdd_proj_out.* with a warning.

Every weight but the heads goes through H3's transform. The heads of one suffix are held until every step's
arrived and leave as one stack under the name sglang-d's weight update takes it by
(sgl-project/sglang#51654); the prefix is read from sglang when a sync needs it, since a sglang-miles before
that PR has no such constant and every family's bridge is imported with the adaptations package.
A forward runs the output head of one denoising step, so the other heads have no gradient on that rank. FSDP2
reduces the gradients of one wrap as a single buffer of the parameters that have one on that rank, in order,
and the heads share a shape: two ranks with pairs of different steps line the buffers up head against head,
and each head leaves the reduce with the mean of its own gradient and the other's.

When the heads are trained, a forward hook on each head adds every head's parameters to its output at weight
zero, so every rank reduces every head. The two-rank test fails without the hook: head 0's gradient differs
from the mean of its own on every element on one rank and is missing on the other.
Both were refused because the rollout engine could not receive the per-step output heads and because the
heads' gradients were not kept apart across ranks. A sync that carries the DiT's weights is now refused only
on an sglang-d whose weight update takes no heads, at argument time.
@zhihengy
zhihengy added this pull request to stack #396 November 20, 2026 00:04
Conflicts in the family's `validate_args` and its tests: the base added the refusal of a config named by path
beside the merged-LoRA refusal this branch removes. Kept this branch's engine check and the base's new
refusal; the accepted cases keep this branch's full-weight and merged-LoRA ones without the tile case the
base dropped.
…rror with a remedy that holds

The refusal was a RuntimeError raised by a getter called for its side effect, and its remedy named
`--lora-ipc-weight-sync` alone, which is refused without `--colocate`. The bridge now answers whether the
engine takes head stacks, `validate_args` raises the ValueError with the remedy for both layouts, and the
refusal of a config named by path names the weight transform it would also lose.
…'s own FSDP wrapping

The worker wrapped the blocks and the root by hand with stock policies. The hook is right only while every
head is unsharded with the forward that runs one of them, which the family's plan decides: the worker now
wraps through `apply_fsdp2` with that plan, so the patched reduce and a plan change are both in the test.
The expected shard is cut locally (`src_data_rank=None`; the default scatters from rank 0), the tiny model
and its layout come from the CPU test, and the test skips on fewer than two GPUs.
… merged LoRA

The bridge tests call the transform directly. This drives the state dict through the weight iterator and its
bucketing, with every tensor in a bucket of its own: the heads leave as the two stacks, and nothing leaves
under a Diffusers name, also from a PEFT-wrapped model.
Conflict in the family's `validate_args`: the base named the sync condition `syncs_weights_to_rollout` and
kept the merged-LoRA refusal this branch replaces. Took the base's name and this branch's checks, and put the
refusal of a config named by path before the engine check: both ways out the engine check names lead into it,
and by path no transform would send a stack. A test pins that order.
…here the heads are dtype overrides

At fp32 the plan's fp32 patterns equal the default and compile to nothing, so the worker ran torch's stock
reduce. A full-weight run forwards in bf16 by default: the heads, `proj_in` and the time embedder are then
per-parameter overrides in the root wrap, which reduces through the patched mixed-dtype collectives. The test
runs both.

The worker compares in deterministic mode. The per-token AdaLN lookup repeats one timestep index and
back-propagates as an index_add whose order on CUDA is not fixed; in bf16 that moved the time-embedding chain's
gradients by about 1% between the wrapped model and the reference, while every other parameter was equal.

The test puts the repository root on the worker's path itself, as test_reference_models does.
…lready has none, and let the dumped-rollouts case tell again

- `monkeypatch.delattr` raised on an sglang-d from before the head update, before `validate_args` ran.
- With a merged LoRA sync accepted, nothing refused the dumped-rollouts launch whether `train_only` was derived
  early or late. On an engine without head updates the family hook refuses it unless it reads `train_only` set.
…32 at argument time

Under `--fsdp-master-dtype bf16` diffusers still loads `proj_in`, the time embedder and the per-step heads in
fp32. A LoRA trainer does not mind: only the adapters are trained. A full-weight trainer has both dtypes among
the trained weights of the root wrap, and FSDP2 raises "expects uniform original parameter dtype" when the
trainer is built, after every rank has loaded the DiT. Seen on 8 H200 with a bf16 master.
@zhihengy
zhihengy marked this pull request as ready for review November 20, 2026 20:42

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant