Skip to content

[cosmos3] Bitwise train/rollout parity: FSDP precision spec + cosmos3_bitwise rollout patch group - #109

Open
zhihengy wants to merge 8 commits into
mainfrom
feat/cosmos3-bitwise-align
Open

[cosmos3] Bitwise train/rollout parity: FSDP precision spec + cosmos3_bitwise rollout patch group#109
zhihengy wants to merge 8 commits into
mainfrom
feat/cosmos3-bitwise-align

Conversation

@zhihengy

@zhihengy zhihengy commented Aug 8, 2026

Copy link
Copy Markdown
Collaborator

Stacked on #25 (feat/cosmos3).

Summary

Makes the Cosmos3 GRPO training forward (miles/diffusers/FSDP2) produce bit-identical tensors to the rollout engine (sglang-d), verified across every aligned module output: 103/103 comparable tensor pairs bit-exact (both CFG branches, UND+GEN towers, first and last denoise steps, embed_tokens through proj_out).

Train side

  • time_embedder fp32 island: diffusers declares _keep_in_fp32_modules = ["time_embedder"] and sglang-d pins it to fp32 at load; the blanket bf16 gather silently downgraded it on the train side. The family FSDPParallelPlan.param_dtype_patterns (the refactor(fsdp)(5/5): compile per-model precision plans onto the dtype patch #106 mechanism) gathers it at fp32, the wrapper runs it with autocast disabled, and identical sinusoid rows are deduplicated before the MLP (cuBLAS fp32 GEMMs are not bitwise M-invariant, measured on the 4096x4096 MLP; sglang-d runs M=1 per request).
  • diffusers RMSNorm → F.rms_norm: eager diffusers RMSNorm rounds to bf16 before the weight mul (two roundings); sglang-d keeps fp32 through the mul. Upgrade the train side (never downgrade) so both run the identical op.

Rollout side (--rollout-patch-group cosmos3_bitwise, per the #97 CLI selection; the recipe sets it)

  • Pin TORCH_SDPA attention backend (matches the train-side SDPA path).
  • Route sglang RMSNorm through the same F.rms_norm.
  • Unfuse MergedColumnParallelLinear (to_qkv, gate_up_proj) into per-slice F.linear GEMMs — fused column blocks are not bitwise equal to the standalone GEMMs diffusers runs. Slices come from output_sizes (with tp=1 init order, output_partition_sizes collapses to one fused slice); the patch fails loud rather than silently falling back fused.
  • Eager SiluAndMul and split (unfused) qk-norm/RoPE path.
  • Sequential batch-1 CFG: uncond/cond as separate forwards — cuBLAS is not batch-invariant on cosmos3 shapes, and CFG batching also changes text padding.

Validation

  • Dump pipeline (per the dumper skills): rollout engine and trainer instrumented with the sglang dumper, records paired by bit-exact latent anchors; pair_metrics shows max_abs_diff = 0 for all 103 aligned pairs; 3 remaining records are benign scalar-vs-broadcast shape mismatches with byte-identical values.
  • Originally validated pre-refactor (e707d30) with a ported PrecisionSpec mechanism; now rebased onto main's own precision machinery (refactor(fsdp)(5/5): compile per-model precision plans onto the dtype patch #106 param_dtype_patterns + ParamDtypeMixedPrecisionPolicy) and the refactor(diffusion): move rollout patch group selection to a CLI arg #97 CLI patch-group selection — same fp32 gather island, no parallel implementation. The dump comparison should be re-run on this base before merge.

Test plan

ci-sglang-pr: 0065fbfae1d22626203c158b9e8c0e8f7126c4cd

--lora-rank 64 \
--lora-alpha 128 \
--diffusion-init-lora-weight gaussian \
--lora-ipc-weight-sync \

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.

seems like this old bash script is conflicting with the latest one

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.

need a careful migration

@zhihengy
zhihengy force-pushed the feat/cosmos3-bitwise-align branch from 6c2fe43 to bffbb7e Compare August 17, 2026 22:34
@zhihengy
zhihengy changed the base branch from feat/cosmos3 to main August 17, 2026 22:34
@zhihengy zhihengy added the run-ci-e2e Run e2e metric-regression tests on this PR label Aug 18, 2026
zhihengy and others added 5 commits August 18, 2026 09:21
Registers the bitwise 4-GPU recipe in stage-c-5-gpu-h200 (opt-in via
run-ci-e2e, nightly). --deterministic-mode joins the recipe so the
standard compares strictly; the model_output parity metrics must be
exactly 0 under the cosmos3_bitwise patch group + dynamic LoRA merge.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Recorded offline on 4xH200 in a replica of the CI image environment
(torch 2.11.0+cu129, sglang main 0065fbfae1, diffusers 0.39.0): the
model_output parity series are exactly 0 on both optimizer steps.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…e-align

Conflicts: the LoRA merge-mode warning in arguments.py (both sides added
it for their own patch group — unified over {qwen_image, cosmos3_bitwise});
the registered-groups test (sgld was renamed qwen_image by #108).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Cosmos3OmniTransformer.forward defaults to a Cosmos3OmniTransformerOutput
since f53d5520 (the requirements pin); unpacking that object yields only
the non-None fields and T2I has neither sound nor action, so the 3-tuple
unpack got 1 value. return_dict=False returns the unchanged tuple.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@zhihengy
zhihengy force-pushed the feat/cosmos3-bitwise-align branch from b4a43ac to 2976fe1 Compare August 18, 2026 16:41
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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