Skip to content

[megatron] fix: support FP8 padding for BSHD actor forward - #6884

Merged
wuxibin89 merged 2 commits into
verl-project:release/v0.7.1from
gem-mint:bshd-fp8-actor
Jun 29, 2026
Merged

[megatron] fix: support FP8 padding for BSHD actor forward#6884
wuxibin89 merged 2 commits into
verl-project:release/v0.7.1from
gem-mint:bshd-fp8-actor

Conversation

@gem-mint

@gem-mint gem-mint commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

Enables Megatron actor/ref forward passes in BSHD format to run with Transformer Engine FP8 enabled.

Qwen3.x/GDN-style models can require use_remove_padding=False, which routes Megatron actor updates through the BSHD forward path. Before this change, that path rejected FP8 with an assertion and did not apply the FP8 block-alignment padding that TE kernels require.

Root cause

model_forward_gen(..., data_format="bshd") explicitly asserted that fp8 is None, so actor update/log-prob computation failed as soon as override_transformer_config.fp8=hybrid or e4m3 was enabled.

After removing the assertion, BSHD still needed the same kind of FP8-aware sequence padding used by the packed/no-padding paths: TE block FP8 kernels require the local token extent to be divisible by 128 after TP/CP partitioning.

Design & Code Changes

  • Remove the BSHD FP8 assertion in verl/models/mcore/model_forward.py.
  • Pass use_fp8_padding through BSHD input preprocessing and logits-processor argument preprocessing.
  • Add _align_bshd_max_seqlen_for_fp8 in verl/models/mcore/util.py to align BSHD sequence length for TE FP8 block quantization while preserving TP/CP divisibility constraints.
  • Reuse the helper in preprocess_bshd_no_padding to keep BSHD FP8 alignment logic consistent.
  • Add a CPU regression test covering BSHD FP8 padding shape/alignment and postprocess round-trip behavior.

Duplicate-work check

No linked issue was provided for this fix.

Checks run before opening this PR:

gh issue list --repo verl-project/verl --state open --search '"fp8 is not supported for bshd format yet"' --limit 20
gh issue view 5508 --repo verl-project/verl --comments
gh pr list --repo verl-project/verl --state open --search '"fp8 is not supported for bshd format yet"' --limit 20
gh pr list --repo verl-project/verl --state open --search 'bshd fp8' --limit 20
gh pr list --repo verl-project/verl --state open --search 'Megatron BSHD FP8' --limit 20
gh pr list --repo verl-project/verl --state open --search 'actor fp8 padding' --limit 20
gh pr list --repo verl-project/verl --state open --search 'bshd actor fp8' --limit 20

Results:

Test

Ran successfully:

git diff --check
python -m py_compile verl/models/mcore/util.py verl/models/mcore/model_forward.py tests/models/test_mcore_bshd_fp8_padding.py

Attempted but not run in this local Windows runtime:

python -m pytest tests/models/test_mcore_bshd_fp8_padding.py -q
# failed locally because pytest is not installed in this runtime

pre-commit --version
# failed locally because pre-commit is not installed in this runtime

External validation: the patched v0.7.1 tree was used to start a Qwen3.x-35B-A3B GRPO training run with Megatron + SGLang and actor FP8 enabled.

AI assistance

This PR was prepared with AI assistance. Human review done before marking the PR ready for review.

Co-authored-by: Codex <codex@openai.com>
@CLAassistant

CLAassistant commented Jun 29, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request enables FP8 support for the BSHD format in Megatron-Core (MCore) models. It removes the previous assertion blocking FP8 usage with BSHD, introduces a helper function _align_bshd_max_seqlen_for_fp8 to align sequence lengths for Transformer Engine FP8 block quantization, and integrates this alignment logic into both preprocess_bshd and preprocess_bshd_no_padding. Additionally, a new unit test has been added to verify the FP8 padding alignment and roundtrip processing. There are no review comments, and the changes look solid.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

@gem-mint
gem-mint marked this pull request as ready for review June 29, 2026 07:52
@wuxibin89

wuxibin89 commented Jun 29, 2026

Copy link
Copy Markdown
Collaborator

Qwen3.5 THD format has supported in NVIDIA/Megatron-LM#2645, please upgrade mcore==0.18.0. We may drop BSHD support in future release.

@wuxibin89 wuxibin89 closed this Jun 29, 2026
@wuxibin89 wuxibin89 reopened this Jun 29, 2026
@wuxibin89
wuxibin89 requested a review from sophiayyya June 29, 2026 08:22
@gem-mint

Copy link
Copy Markdown
Contributor Author

Thanks for the pointer. This PR targets release/v0.7.1 specifically, where our current stack still needs BSHD because the mcore 0.18 THD support is not available in that branch/environment yet.

The change is scoped to the existing BSHD path and should not affect THD/remove-padding users. If the project prefers not to take BSHD improvements even for release/v0.7.1, I’m happy to keep this as a downstream patch.

@wuxibin89

wuxibin89 commented Jun 29, 2026

Copy link
Copy Markdown
Collaborator

@gem-mint Got it, please fix pre-commit. Could you submit this PR to main as well?

Co-authored-by: OpenAI Codex <codex@openai.com>
Signed-off-by: gem-mint <146161229+gem-mint@users.noreply.github.com>
@wuxibin89 wuxibin89 changed the title [mcore] fix: support FP8 padding for BSHD actor forward [megatron] fix: support FP8 padding for BSHD actor forward Jun 29, 2026
@wuxibin89
wuxibin89 merged commit 8ece147 into verl-project:release/v0.7.1 Jun 29, 2026
4 of 5 checks passed
@gem-mint

Copy link
Copy Markdown
Contributor Author

@wuxibin89 Thanks! opened the main-branch companion PR here: #6887. The PR carries the same BSHD FP8 actor-padding fix adapted to the current main branch.

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.

3 participants