feat: pack 2 fc layers in mlp with swiglu - #218
Open
Chamberlain0w0 wants to merge 2 commits into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
背景
当前 SwiGLU MLP 使用两个独立的
ColumnParallelLinear分别计算 gate projection 和 up projection,随后执行 SiLU 和逐元素乘法:该实现会产生两次独立 GEMM 调用,并分别保存中间结果。对于 MoE,大 batch 下各 expert 的中间激活会带来较明显的性能和显存开销。Megatron 中常将两个 FC1 投影打包为单个 GEMM,本 PR 也做了类似的修改,并新增融合的 SwiGLU kernel。
主要改动
Packed FC1
将 SwiGLU 的 gate/up projection 合并为一个输出维度为
2H的ColumnParallelLinear:packed tensor 采用 Megatron-LM 的
[gate, up]布局,便于后续对齐 Megatron checkpoint 和相关 fused kernel。Dense MLP 和 MoE expert 共用该实现,因此均切换为 packed SwiGLU。GELU MLP 路径保持不变。
SwiGLU kernel
新增独立的 SwiGLU kernel 文件:
infini_train/src/kernels/cpu/swiglu.ccinfini_train/src/kernels/cuda/swiglu.cukernel 融合执行 SiLU 和逐元素乘法,并实现对应反向传播:
支持范围:
本 PR 合并了两个 FC1 GEMM,并融合了
SiLU + multiply。Autograd 与 Module
[gate, up]的 packed tensor。[gate, up]顺序写回。Checkpoint loader
同步调整 LLMC checkpoint loader:
gate_proj写入 packed FC1 前半部分,将up_proj写入后半部分。w1/gate_proj写入前半部分,将w3/up_proj写入后半部分。[2H_local]布局分别加载 gate/up shard。参数布局变化
SwiGLU MLP 的 state dict 从:
调整为:
c_fc2.weight不再存在。并且将 Packed SwiGLU 设置为 SwiGLU MLP 的默认实现,不需要额外配置开关。
MoE 性能提升
统计时排除 step 1 warm-up,使用 step 2–10 的平均耗时。baseline 和 packed 使用相同启动参数及构建配置。
结果表明: