Skip to content

Qwen38_pp_recipe - #815

Open
fxsc03 wants to merge 2 commits into
redai-studio:mainfrom
fxsc03:qwen_pp_recipe
Open

fxsc03 wants to merge 2 commits into
redai-studio:mainfrom
fxsc03:qwen_pp_recipe

Conversation

@fxsc03

@fxsc03 fxsc03 commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

What

为 Qwen3.8-Flash-Next 新增五套可直接启动的长上下文 SFT 配方,覆盖 128K 纯文本、128K 多模态、256K 多模态、512K 和 1M。五套配方均使用 32 张 B300、EP8,并启用 MoK MXFP8、Muon 和 DoSA 边界选块。

| 训练场景 | 并行配置 | GBS |
| --- | --- | ---: | ---: |
| 128K 纯文本 SFT | TP1 × PP1 × CP4 × DP8 | 64 |
| 128K 多模态 SFT | TP1 × PP1 × CP4 × DP8 | 512 |
| 256K 多模态 SFT | TP1 × PP1 × CP8 × DP4 | 32 |
| 512K SFT | TP1 × PP1 × CP16 × DP2 | 32 |
| 1M SFT | TP1 × PP2 × CP16 × DP1 | 32 |

  • fccda4a 新增两套 128K 配方和 512K 配方,接通 MoK 原生 MXFP8 权重初始化,统一长文档打包对齐规则,并修复 eval 后恢复训练时的参数同步问题。
  • 04c263f 新增 256K 多模态和 1M 配方,并将 512K 默认步数从 20 调整为 200。各入口均支持通过 NUM_STEPS 覆盖默认值。

Why

原有 32 卡 Muon 启动脚本默认使用 TP2 × PP4 × CP1。长上下文实验需要按序列长度调整 CP 和 PP,同时配置专家计算、索引选块和多模态数据处理。将实测配置固化为独立入口,可以直接复现实验,也可以避免手工覆盖参数时遗漏视觉分片或 MoK 缓冲区容量。

实验还暴露了两个正确性问题。首先,启用 MoK MXFP8 后,Relax 规划 microbatch 和 Megatron-Bridge 执行前向时必须采用相同的文档补齐长度。其次,eval 可能跳过已经预取权重的 MTP 层,下一步训练如果提前清空复用的梯度缓冲区,会覆盖尚未写回的权重。

How

  • 并行与选块。 五个入口共用同一套 32 卡启动逻辑,将 CuTe CP 和 DoSA 边界选块配置传入 Ray 训练进程。256K 多模态启用 --vision-dp-when-tp,在 CP 组内分摊视觉计算。1M 使用 PP2,将 48 层按 24 + 24 分配到两个流水线阶段。
  • MoK 容量与权重初始化。 按 seq_length / CP 设置每卡 MoK 本地 token 容量,256K/CP8 为 32,768,1M/CP16 为 65,536。在 TP1、MoK、MXFP8 和 FP8 参数收集同时启用时,由模型构建器直接创建原生 MXFP8 专家权重,支持与计算重叠的参数收集。
  • 文档打包。 在 MoK MXFP8 的 CP 路径中,Relax 和 Bridge 均按 TP × CP × 32 对齐每篇文档的物理长度,使 microbatch 预算、前向输入和 loss mask 使用一致的文档边界。补齐位置不计入 loss。
  • eval 后恢复训练。 Megatron 在重置梯度缓冲区前,检查普通参数组和专家参数组,先完成尚未结束的逐层参数收集并写回权重,再复用缓冲区。

Testing

  • pre-commit run --all-files --show-diff-on-failure 通过,16 项检查全部通过。
  • 定向 CPU 测试通过,110 个用例通过。新增用例覆盖 CP 补齐后的监督 token、按物理长度规划 microbatch,以及模型构建前的 MXFP8 参数配置。
  • 256K 和 1M 新入口通过 Relax/Megatron 参数解析校验。展开后的训练参数和关键 worker 环境配置逐项对齐各自成功完成的 50 步运行记录。
  • 32 张 B300 上完成 128K 多模态 训练 → eval → 继续训练 短回放,三个训练步的 loss 和 grad norm 均与对照一致。
  • 完整 pytest tests/ 套件未运行。
  • 未单独更新文档,运行参数和默认值见各配方入口。

Type of Change

  • Bug 修复
  • 新功能
  • 破坏性变更
  • 文档更新
  • 重构
  • 性能优化
  • CI/CD 或构建流程变更

Screenshots / Logs

下表比较 32 张 B300 上各场景的历史 baseline 与新配方。括号内为相对 baseline 的变化,四舍五入到整数。MFU 的变化按相对百分比计算。

训练场景 Baseline TPS/卡 新配方 TPS/卡 Baseline MFU 新配方 MFU
128K 纯文本 SFT 3,709 4,374(+18%) 7.77% 9.228%(+19%)
128K 多模态 SFT 3,206 4,123(+29%) 6.73% 8.7%(+29%)
256K 多模态 SFT 3,285 3,011.82(−8%) 6.86% 7.503%(+9%)
512K SFT 2,609 2,892(+11%) 5.49% 6.443%(+17%)
1M SFT 2,483 2,800(+13%) 5.25% 6.119%(+17%)

新配方的统计窗口按训练步从 1 开始计数:128K 纯文本取第 2–8 步,128K 多模态取第 6–100 步,256K 和 1M 取第 11–50 步,512K 取第 6–20 步。512K 表中的性能来自已完成的 20 步验证,200 步是本次更新后的脚本默认值。

TPS/卡衡量整步吞吐,MFU 取训练日志中的 actor MFU。128K 多模态的 4,123 是逐步 TPS 的算术平均值,同一窗口按总 token 数除以总耗时计算为 4,114 TPS/卡。256K 多模态的整步吞吐比历史 baseline 低约 8%,尽管 actor MFU 提高了约 9%。以上为完整配方对比,包含并行配置、精度和选块策略的共同影响。

Add the 32-GPU 128K text, 128K multimodal and 512K SFT recipes
with native MoK MXFP8 weights, Muon and boundary block selection.
Match Bridge and Relax document padding for context-parallel MXFP8.

Finish pending LayerWise parameter gathers before clearing gradient
buffers so training after forward-only evaluation retains expert weights.
Use CP8 with vision sharding for 256K multimodal inputs and split the
48 transformer layers evenly across PP2 for 1M CP16 training. Size the
MoK local token buffer for the full context shard in each recipe.

Default both new entrypoints to the validated 50 steps and extend the
512K PP1 default from 20 to 200 steps.
@fxsc03
fxsc03 requested review from a team and NINGBENZHE November 27, 2026 03:45
@rai-studio-bot

rai-studio-bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Thanks for contributing to Relax, @fxsc03! 感谢你为 Relax 做出贡献!

📚 Contribution guide / 贡献指南

Describe the problem, your changes, and how you validated them. Keep each PR focused and run pre-commit run --all-files before submitting.

请说明问题、改动和验证方式,保持 PR 聚焦,并在提交前运行 pre-commit run --all-files。

English contribution guide · 中文贡献指南

🛠️ CI commands / CI 指令
Command / 指令 Usage / 用途
/rerun Retry failed CI / 重跑失败的 CI
/rerun <target> Rerun a workflow or check / 重跑指定 workflow 或检查
/cancel <workflow> Cancel an entire workflow / 取消整个 workflow
/help Show commands and targets / 查看指令和 target
/review Request a code review / 请求代码 review

Put one command on the first line of a new PR comment. Rerun/cancel require PR authorship or repository write access.

在新 PR 评论的首行写一条指令。PR 作者或有仓库写权限的贡献者可以重跑、取消 CI。

CI usage and targets · CI 用法与 target

🔎 Merge requirements / 合入条件

GitHub:⏳ 合入条件未满足

Requirement / 条件 Status / 状态
审批人数 ⏳ 0/1 · 至少 1 位有仓库 write 或更高权限的人 Approve
团队审批(relax-block-reviewers) ⏳ 0/1 · 可联系:Aurelius84, Yangruipis, RexFlux, yuanlehome, SigureMo, NINGBENZHE, Lemon-412
CI ❌ 未通过

@rai-studio-bot

rai-studio-bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Nyanpasu 审查看板

审查状态: 🚧 需要修改

审查版本: 04c263f

目标分支: main

静态 CP 的 MoK MXFP8 前向与 rollout 消费方使用不同的物理长度,常规 colocated GRPO 会在参数更新前因响应 mask 长度不匹配而失败。

审查阶段进度范围与结果
常规审查 ✅ 已完成 已核对五个长上下文启动配方、CPU 回归及 Megatron 补丁。固定版本的 Bridge 和 MCore 源码通过 common、cu13 和 blackwell 三段补丁应用验证。
深度审查 ✅ 已完成 已追踪 LayerWise 参数收集、MoK 精度与 CP 调用链。CPU 定向复现确认 TransferQueue 重组会在通信前失败,常规 colocated GRPO 的指标错误由源码确认。未独立运行 Blackwell 训练。

CI 异常

检查版本: 04c263f · 已分析

失败检查原因与现状证据建议下一步
Unit Tests (CPU) 42 个用例均因测试装载器未加载新增的 _vl_cp_alignment 而报 NameError。 第 1 次运行的 Run unit tests 步骤报告 42 项失败、10,982 项通过、115 项跳过。test_mtp_cp_layout_cpu.py 有 36 项失败,test_dynamic_microbatch_partitions.py 有 6 项失败。两个装载器只提取指定函数,未加载这些函数现在调用的 _vl_cp_alignment。 https://github.com/redai-studio/Relax/actions/runs/45935982935/job/140299238924#step:8:1 让这两个测试文件的装载器加载实际的 _vl_cp_alignment,再定向运行两份 CPU 测试并复查该 CI 检查。

审查发现

待处理
编号 严重性 问题状态规则来源
F1 Medium severity 为长上下文配方保存可恢复的 checkpoint 🚧 未解决 训练脚本的 checkpoint 保存与恢复约定
F2 Medium severity 保留 Ray 入口为训练进程解析出的库路径 🚧 未解决 —
F3 Medium severity 保留原生 MXFP8 路径的精度参数冲突检查 🚧 未解决 —
F4 Low severity [experimental · TST002] 用测试名说明构建前的参数传递契约 🚧 未解决 —
F5 Medium severity 让 MoE 路由辅助损失排除新增的补齐行 🚧 未解决 —
F6 Medium severity 在启动时校验变长 MoK 的固定容量 🚧 未解决 —
F7 High severity 让前向与 rollout 重组共用静态 CP 物理长度 🚧 未解决 —
已解决或已取代
编号 严重性 问题状态规则来源
暂无已解决或已取代的记录。

改进建议(非阻塞)

相关 PR

💡 实现与合入建议

#813 与本 PR 均接入原生 MoK MXFP8 权重,建议让两套配方共用初始化条件和精度校验。

https://github.com/redai-studio/Relax/pull/813 · 7c20557ac7a3d4c324e7e11aadb0da01bc5127b0 · 目标分支 main

实现取舍: 沿用本 PR 在 Bridge 构建前设置 FP8 参数的 TP1 条件,并保留 #813 对 LayerWise Muon 的前置精度校验。需要明确非专家层和 MTP 的精度策略。

当前 PR 调整: 让构建前配置和 configure_fp8 共用同一项条件,只跳过 MoK 已完成的专家权重转换。保留本 PR 的 CP 打包、eval 恢复和 128K–1M 配方。

合入顺序: 两者可独立合入。本 PR 的 CP DoSA 已有独立入口。后合入的 PR 应复用已落地的 MoK 初始化和校验,避免维护两套条件。

依据与验证范围: 源码对比覆盖两个固定提交。尚未验证合并实现的 GPU 数值一致性。 https://github.com/redai-studio/Relax/blob/04c263fd312925bd2877fd449b04f7d09b89a94a/relax/backends/megatron/model_provider.py#L518-L530 https://github.com/redai-studio/Relax/blob/7c20557ac7a3d4c324e7e11aadb0da01bc5127b0/relax/models/qwen3_8/precision.py#L104-L140 https://github.com/redai-studio/Relax/blob/04c263fd312925bd2877fd449b04f7d09b89a94a/relax/models/qwen3_8/ops/qsa_cute/cp.py#L94-L120

Powered by Nyanpasu with gpt-6.2-sol high, please check the suggestions carefully.

@rai-studio-bot rai-studio-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.

需要修改

MoK MXFP8 的前向与 rollout 重组采用不同的静态 CP 补齐长度,常规 GRPO 会在参数更新前失败。CPU 定向复现确认同一偏差会使 TransferQueue 重组在通信前触发断言。固定依赖源码上的镜像补丁应用验证通过。

审查看板

Powered by Nyanpasu with gpt-6.2-sol high, please check the suggestions carefully.

Comment on lines +240 to +242
if [[ -z "${seq_length}" ]]; then
CKPT_ARGS+=(--save "${SAVE_DIR}/${EXP_NAME}" --save-interval "${SAVE_INTERVAL:-50}")
fi

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.

Medium severity P2 · 为长上下文配方保存可恢复的 checkpoint

五个新入口都会设置 seq_length,所以默认只有 --load 和保留数。训练循环在 save 未设置时跳过保存,200 步的 512K 配方跑完时也不会留下可供下次启动恢复的 checkpoint。请让这些入口共享稳定的保存目录和 SAVE_INTERVAL。

Suggested change
if [[ -z "${seq_length}" ]]; then
CKPT_ARGS+=(--save "${SAVE_DIR}/${EXP_NAME}" --save-interval "${SAVE_INTERVAL:-50}")
fi
CKPT_ARGS+=(--save "${SAVE_DIR}/${EXP_NAME}" --save-interval "${SAVE_INTERVAL:-50}")
Powered by Nyanpasu with gpt-6.2-sol high, please check the suggestions carefully.

RELAX_QWEN38_QSA_GROUPED_BACKWARD_BLOCK_K=32
RELAX_QWEN38_QSA_SELECTOR_COMPILE=1
)
if [[ -n "${seq_length}" ]]; then

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.

Medium severity P2 · 保留 Ray 入口为训练进程解析出的库路径

五个新入口经 ray-job.sh 启动时,入口已将 Torch 和 NVSHMEM 的库目录放入 RUNTIME_ENV_JSON。这里生成的 TRAIN_ENV_JSON 仍使用提交 shell 的 LD_LIBRARY_PATH,随后覆盖 driver 的 Ray 环境,也把同一个值传给 Actor。提交端未设置该变量时,两边都会收到空值,丢掉入口为原生扩展补齐的库路径。可沿用同目录 128×H800 配方的做法,在生成 TRAIN_ENV_JSON 时优先采用入口已解析的值。

Powered by Nyanpasu with gpt-6.2-sol high, please check the suggestions carefully.

if getattr(provider, "qsa_indexer_n_heads", None) and getattr(provider, "ple_layer_ids", None):
from relax.models.qwen3_8.precision import configure_fp8
if is_qwen38:
if not native_mok_mxfp8:

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.

Medium severity P2 · 保留原生 MXFP8 路径的精度参数冲突检查

向新配方追加 --te-precision-config-file 后,Bridge 的参数白名单未映射该文件,而原生 MoK 分支又跳过 configure_fp8 中对该组合的显式拒绝。用户指定的逐模块精度配置会被静默忽略,模型仍按全局 MXFP8 配置构造。请在公共精度检查之后再跳过 MoK 已经完成的权重转换,例如复用 #813 将 MoK 分支放到通用校验之后的做法。

Powered by Nyanpasu with gpt-6.2-sol high, please check the suggestions carefully.

assert provider.pad_packed_seq_alignment == alignment


def test_bridge_initializes_qwen_mok_mxfp8_parameters_before_model_construction(monkeypatch):

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.

Low severity P3 · [experimental · TST002] 用测试名说明构建前的参数传递契约

断言确认 fp8_param 在调用 provider 前已设置。测试中的 _FakeProvider.provide 直接返回预建模型,因此无法发现 Bridge/MoK 在创建专家权重时忽略该标志的问题。建议把测试名收窄到已覆盖的构建前配置。

Suggested change
def test_bridge_initializes_qwen_mok_mxfp8_parameters_before_model_construction(monkeypatch):
def test_model_provider_sets_qwen_mok_fp8_param_before_construction(monkeypatch):
Powered by Nyanpasu with gpt-6.2-sol high, please check the suggestions carefully.

+ and self.config.fp8_recipe == "mxfp8"
+ and self.config.moe_megakernel_backend == "mok"
+ ):
+ packed_seq_alignment = self.pg_collection.tp.size() * cp_size * 32

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.

Medium severity P2 · 让 MoE 路由辅助损失排除新增的补齐行

新配方把 --moe-aux-loss-coeff 设为 0。开启该损失时,legacy BSHD 路径会按新对齐补齐后清除 attention_mask,却没有为 MoE 路由生成对应的 padding_mask。例如 TP1/CP4 下的 37-token 文档补到 128,最后一个 CP rank 的 32 行全是 padding,路由仍会把它们计入专家分布和 token 数。请按同一 zigzag 布局从原始 keep mask 生成路由用的 padding mask。

Powered by Nyanpasu with gpt-6.2-sol high, please check the suggestions carefully.

and getattr(args, "fp8", None)
and args.fp8_recipe == "mxfp8"
)
return tp_size * cp_size * (32 if mok_mxfp8 else 2)

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.

Medium severity P2 · 在启动时校验变长 MoK 的固定容量

五个脚本都设置了 fixed_num_local_tokens。直接使用新支持的 TP1/CP4 MoK MXFP8 配置时,该选项默认可省略。37-token 的单文档 microbatch 经这里补齐后每卡只有 32 行,MoK 会直接拿这一形状建 workspace,而固定依赖要求本地容量至少 512 且为 256 的倍数,还要求同一 EP 组使用相同形状。请在变长数据路径启动时要求有效的固定容量,或统一在 MoK workspace 层做补齐。

Powered by Nyanpasu with gpt-6.2-sol high, please check the suggestions carefully.

if needs_unsplit_input and cp_size > 1:
tp_size = mpu.get_tensor_model_parallel_world_size()
align_size = tp_size * cp_size * 2
align_size = _vl_cp_alignment(get_args(), tp_size, cp_size)

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.

High severity P1 · 让前向与 rollout 重组共用静态 CP 物理长度

Qwen3.8 VL 启用 MoK MXFP8 后,这里的前向按 TP × CP × 32 补齐。常规 colocated GRPO 的 log_rollout_data 仍用 maybe_padded_total_lengths 按 TP × CP × 2 重建响应 mask,导致更新前的 torch.split 失败。例如 TP1/CP4、原长 1040、响应长 300 时,rank 0 实际得到 31 个 log-prob,旧 mask 会要求 129 个。CPU 定向复现也确认同一偏差会使 TransferQueue 的 all_gather_with_cp 在通信前触发断言。

请让打包、指标、TransferQueue 和 replay 的静态 CP 消费方共用同一个物理长度计算,或直接传递前向已计算的物理长度。保留 rollout 中原始 token 长度的语义。

Powered by Nyanpasu with gpt-6.2-sol high, please check the suggestions carefully.

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.

2 participants