Skip to content

fix: preserve dtype across pipeline parallel communication - #255

Merged
chen2021673 merged 3 commits into
masterfrom
fix/pp-dtype-communication
Nov 24, 2026
Merged

chen2021673 merged 3 commits into
masterfrom
fix/pp-dtype-communication

Conversation

@JYMiracle305

@JYMiracle305 JYMiracle305 commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

背景

Pipeline Parallel 在 stage 间接收 activation 和 gradient 时,此前固定使用 float32 创建接收张量。当训练启用 bfloat16 等非 FP32 dtype 时,通信两端的 buffer dtype 可能不一致,导致 PP 通信失败或程序崩溃。

修改内容

  • 在 ModelParallelConfig 中引入 pipeline_dtype,作为 PP stage 间统一的通信 dtype,语义对齐 Megatron。
  • GPT-2、LLaMA3、Mixtral、Qwen3 示例新增 --pipeline_dtype 配置,默认 float32,可显式选择 bfloat16 以降低通信带宽。
  • Forward activation 在发送前统一 cast 到 pipeline_dtype;接收端使用 pipeline_dtype 创建 buffer,接收后交给当前 stage,由既有 autocast 机制处理算子级 dtype。
  • Backward gradient 发送前统一 cast 到 pipeline_dtype;上游使用 pipeline_dtype 创建接收 buffer,并在接收后恢复为前向原始 input dtype。
  • 增加发送 tensor 数量检查,避免配置 shape 数量与实际 tensor 数量不一致时进入通信路径。
  • 已 rebase 到最新 master 并解决冲突。

验证

  • clang++ 18 -fsyntax-only 覆盖 send_recv.cc、pipeline_schedule.cc、datatype.cc 以及 GPT-2 / LLaMA3 / Mixtral / Qwen3 示例。
  • clang-format --dry-run --Werror 覆盖全部改动源文件。
  • 推送后由 GitHub Actions 执行仓库现有 format / IWYU / clang-tidy 检查。

@JYMiracle305
JYMiracle305 force-pushed the fix/pp-dtype-communication branch 6 times, most recently from fa16795 to 6cf4962 Compare November 10, 2026 05:45
Comment thread example/gpt2/main.cc Resolved
Comment thread example/gpt2/main.cc Resolved
@JYMiracle305
JYMiracle305 force-pushed the fix/pp-dtype-communication branch from be3e354 to fdaa719 Compare November 19, 2026 06:05
Comment thread example/gpt2/main.cc
// precision
DEFINE_string(dtype, "float32",
"precision used in training (float32/bfloat16)");
DEFINE_string(pipeline_dtype, "float32",

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.

现在写死了 --pipeline_dtype=fp32,有没有验证过--pipeline_dtype=bf16 的正确性?test_config 里可以配一个测例。

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

在dtype = bf16 场景下,设置 pipeline_dtype=bf16,会在stage之间进行 fp32 到 bf16、bf16 到 fp32 的两次转换,这会导致精度下降,在LLaMA3、Qwen3 都会因为误差累积出现异常,所以现在 pipeline_dtype 默认用 fp32

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.

确实。看了一下 Megatron的 params_dtype 和 pipeline_dtype 逻辑,是训练精度指定的 dtype 可以直接控制权重和激活的精度(params_dtype)、op 计算精度 、pp边界精度(pipeline_dtype 默认跟随该 params_dtype);但也同样支持在自定义 transformer_config 的时候传入。但 InfiniTrain 目前只有 op 级 autocast,--dtype 不改激活精度,没有 params_dtype 这一参数,PP 边界始终是 fp32,所以设置 --pipeline_dtype bf16 会有问题。
可以加个 TODO 来描述这个问题。以后如果需要进一步优化,需要把现在的 --dtype=bf16 从”只控制 op 级 autocast”扩展到“完整的“bf16 栈”,统一控制 params/计算精度/wire。

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

已新增TODO

@JYMiracle305

Copy link
Copy Markdown
Contributor Author

精度对比:

image

##性能对比:
image

@JYMiracle305
JYMiracle305 force-pushed the fix/pp-dtype-communication branch from 668f0a9 to 011e382 Compare November 23, 2026 10:20
@chen2021673
chen2021673 removed the request for review from Chamberlain0w0 November 24, 2026 08:59
@chen2021673
chen2021673 merged commit 278053f into master Nov 24, 2026
3 checks passed
@chen2021673
chen2021673 deleted the fix/pp-dtype-communication branch November 24, 2026 09:01
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