Open
[DSv4] fused_cpp MoE integration patch (fused_cpp_cpu_moe.py) #33
[DSv4] fused_cpp MoE integration patch (fused_cpp_cpu_moe.py) #33
Open
fangjijiaAdd on 9 days ago
fangjijia
fangjijiaCollaborators
9 days ago

vLLM 侧 fused_cpp MoE 集成层补丁(patch/0014)。

  • vllm/model_executor/layers/fused_moe/experts/fused_cpp_cpu_moe.py:算子库与 vLLM 的唯一集成点(Plan V2 后端、权重预打包缓存、W8A16/W8A8 开关、启动校准)
  • 718 行,取自交付分支 dsv4-arm-cpu-v0.28.0(137b573d)
  • 与基座补丁集(MR !32)的 0009 补丁共同完成 MoE 集成
  • 配套算子库:boostkit/kdnn-pre MR !7、!8、!9、!10

验证:920B Pro W8A8 平均 3.043s / 673 tok/s;950 W8A8 44 核 3.490s / 586.8 tok/s;精度验收均已验收合格 24/24。

likedislike
Merge blocked
fangjijiafangjijiaCollaborators
9 days ago Created pull request, commit 7307eb9c
atomgit-bot
atomgit-bot
9 days ago Comments:

变更摘要

本 PR 新增补丁 patch/0014-vllm_0.28.0-dsv4-cpu-fused-cpp-moe.patch,为 vLLM 新增 vllm/model_executor/layers/fused_moe/experts/fused_cpp_cpu_moe.py,作为 fused_cpp 算子库与 vLLM 模块化 MoE 流水线的唯一集成点。该文件实现基于 Arm CPU 的 Plan V2 后端专家类,覆盖 BF16、W8A16、W8A8 三种量化路径,并配套权重预打包缓存、启动期 Planner 校准、运行时单例管理与张量释放等机制。

主要改动

  • 新增 MoE 专家类族: 新增 FusedCppArmExperts(继承 mk.FusedMoEExpertsModular),以及派生类 FusedCppArmW8A16Experts 和 FusedCppArmW8A8Experts,通过 quant_mode、prepare_name、execute_name 区分量化模式,并分别声明对 kInt8StaticChannelSym / kInt8DynamicTokenSym 量化方案的支持。
  • 算子可用性与能力约束: _load_fused_cpp_moe 校验 fused_cpp.moe 模块、必需符号及 arm_sve_bf16 后端是否可用;各专家类的 _supports_current_device、_supports_activation、_supports_routing_method、_supports_parallel_config 限定为 Arm CPU、SILU 激活、RoutingMethodType.DeepseekV4 路由且不支持 EP/DP/序列并行。
  • Plan V2 运行时与启动校准: 通过 _runtime_for 以线程锁和签名校验维护进程级 MoePlannerRuntime 单例,强制要求 VLLM_CPU_OMP_THREADS_BIND 显式绑定且线程数与亲和核数一致;_runtime_profile / _calibrate_temp_profile 支持按 rank 读取 FUSED_CPP_MOE_PLANNER_PROFILE 指定 profile,缺失时在文件锁保护下自动校准并生成临时 profile。
  • 权重预打包缓存: 新增 _load_prepacked / _export_prepacked / _cache_key / _cache_path,按层、TP size/rank 落盘,并基于 _PREPACK_SCHEMA、_BACKEND_ABI、权重形状、dtype 与 quant_mode 校验缓存键;支持 VLLM_CPU_MOE_PREPACKED_DIR 读取、VLLM_CPU_MOE_PREPACK_EXPORT_DIR 导出续跑以及 VLLM_CPU_MOE_PREPACK_PREFAULT 预触页。
  • 权重释放与执行路径: process_weights_after_loading 校验共享专家存在、swiglu_limit == 10.0、无 expert_map 且权重为 CPU 目标 dtype 后,调用算子库预处理接口,并用 replace_parameter 将路由与共享专家权重量替换为空张量以释放内存;apply 复用 vLLM 输出缓冲区并校验执行前后 planner 运行时未被替换。
likedislike
Incorrect?
atomgit-bot
atomgit-bot
9 days ago Comments:

代码审查

评审总结

已评审文件

  • patch/0014-vllm_0.28.0-dsv4-cpu-fused-cpp-moe.patch(唯一变更文件,718 行新增;内容为新增的 vLLM fused_cpp MoE 集成层文件,已逐段评审):发现下述 7 条问题。

问题计数:P0 0 条 / P1 0 条 / P2 1 条 / P3 6 条。

主要问题

  • P2:预打包缓存 key 只含 shape/路径等元信息、不含权重内容指纹,配合"export 目录命中即直接加载"的续跑分支,权重被替换但形状不变时会静默沿用旧权重。
  • P3:导出缓存使用固定 .tmp 文件名(多进程写同一 rank 文件会交错写坏或 os.replace 失败);导出侧与加载侧对"payload 是否含 scale"使用两套判据;int8 分支缺少 bf16 分支那样的 .contiguous() 归一化;apply 热路径每步重复 import/后端探测;并行配置判定未排除 PP 而 CPU 绑定与缓存名按 TP rank 索引;int8 子类的 _supports_current_device 与基类重复。

整体风险判断:主链路的形状契约、量化开关与 runtime 单例守卫写得较为严谨,正常 BF16/W8A16/W8A8 部署路径未发现确定性错误;风险集中在"缓存生命周期与并发"以及 int8 输入契约这两类非 happy-path 场景,其中 P2 的静默错误最需要在上线前补一道内容指纹或版本标签。

类型 数量
🔴 阻塞 0
🟡 建议 7

💬 仅评论

likedislike
Incorrect?
BoostKit-botBoostKit-botCollaborators
9 days ago Set lauyarn,zeuscht,hellonbxx,Jiangyingxin,gobyte as approver
BoostKit-botBoostKit-botCollaborators
9 days ago Added label: sig/BoostAIInfra
BoostKit-bot
BoostKit-botCollaborators
9 days ago Comments:

欢迎来到 BoostKit 社区

Hey @fangjijia , 感谢你对社区的贡献.

机器人使用手册

有关指令的使用,可以点击 此处 查看详情。开发人员可以在每个PR或Issue下方评论特定指令来触发机器人任务。您可以自助配置此仓库的 PR 合并规则,更多详情,请参阅此处。

联系指引

有疑问可以联系 SIG: BoostAIInfra ,
维护者是: @Jiangyingxin, @gobyte, @hellonbxx, @lauyarn, @zeuscht ,
优秀贡献者是: @yuyufeng .

likedislike
BoostKit-botBoostKit-botCollaborators
9 days ago Added label: boostkit-cla/yes
atomgit-bot
atomgit-bot9 days agoStart code review1
patch/0014-vllm_0.28.0-dsv4-cpu-fused-cpp-moe.patch
@@ -0,0 +606,4 @@
606++ or _profile_path(get_tensor_model_parallel_rank()) is not None
607++ )
608++ and _rank_cpu_ids(get_tensor_model_parallel_rank()) is not None
609++ )
atomgit-bot
atomgit-bot9 days agoComments:

🔵 Low Priority

第 597–609 行 _FusedCppArmInt8Experts._supports_current_device 与基类第 388–399 行 FusedCppArmExperts._supports_current_device 逐项等价:同样判断 is_cpu()、CpuArchEnum.ARM、_load_fused_cpp_moe() is not None、_PROFILE_ENV 与 _profile_path(...)、_rank_cpu_ids(...) is not None,唯一差别只是把 moe 提到局部变量。基类实现已被 W8A16/W8A8 通过继承覆盖同一语义,因此这个重写是纯冗余,删掉后行为完全一致;保留它的维护风险在于两处判定条件将来容易单向修改而分叉(例如基类新增 profile/绑定的前置条件而子类漏改),导致 int8 与 bf16 后端的可选中条件悄悄不同。

建议:删除 _FusedCppArmInt8Experts._supports_current_device 这个等价重写,直接继承基类实现;若确实需要差异,应显式复用 super()._supports_current_device() 再叠加差异条件。此为可选清理,不是必须修复项。

likedislike
Incorrect?
atomgit-bot
atomgit-bot9 days agoStart code review1
patch/0014-vllm_0.28.0-dsv4-cpu-fused-cpp-moe.patch
@@ -0,0 +249,4 @@
249++ "shared_w2_scale_shape": tuple(shared.down_proj.weight_scale.shape),
250++ }
251++ )
252++ return key
atomgit-bot
atomgit-bot9 days agoComments:

🟡 Medium Priority

建议:在 _cache_key 中补充权重内容指纹(对路由/共享权重及其 scale 做抽样 checksum),或引入用户可显式递增的 VLLM_CPU_MOE_PREPACK_TAG 参与 key,使"权重被替换但 shape 不变"的情况也能命中 key 不匹配并快速失败。

likedislike
Incorrect?
atomgit-bot
atomgit-bot9 days agoStart code review1
patch/0014-vllm_0.28.0-dsv4-cpu-fused-cpp-moe.patch
@@ -0,0 +355,4 @@
355++ }
356++ if len(packed.w13) == 4:
357++ payload["w13_scale"] = packed.w13[3]
358++ payload["w2_scale"] = packed.w2[3]
atomgit-bot
atomgit-bot9 days agoComments:

🔵 Low Priority

建议:让导出侧的 scale 判定与加载侧统一为 quant_mode,并在 packed 元数不符合预期时报出明确错误,避免缓存缺 scale 只能在下一次加载时以 KeyError 暴露。

likedislike
Incorrect?
atomgit-bot
atomgit-bot9 days agoStart code review1
patch/0014-vllm_0.28.0-dsv4-cpu-fused-cpp-moe.patch
@@ -0,0 +650,4 @@
650++ self.prepared = _load_prepacked(export_dir, layer, moe, self.quant_mode)
651++ else:
652++ prepare = getattr(moe, self.prepare_name)
653++ self.prepared = prepare(*values)
atomgit-bot
atomgit-bot9 days agoComments:

🔵 Low Priority

建议:在调用量化 prepare 之前对 values 中每个张量做 .contiguous(),与 bf16 分支(第 461–467 行)的输入归一化保持一致。

likedislike
Incorrect?
atomgit-bot
atomgit-bot9 days agoStart code review1
patch/0014-vllm_0.28.0-dsv4-cpu-fused-cpp-moe.patch
@@ -0,0 +358,4 @@
358++ payload["w2_scale"] = packed.w2[3]
359++ temporary_path = path.with_suffix(f"{path.suffix}.tmp")
360++ torch.save(payload, temporary_path)
361++ os.replace(temporary_path, path)
atomgit-bot
atomgit-bot9 days agoComments:

🔵 Low Priority

建议:改用进程内唯一的临时文件(mkstemp)写入后再 os.replace,异常时删除临时文件,避免两个导出进程争用同一 .tmp 路径。

likedislike
Incorrect?
atomgit-bot
atomgit-bot9 days agoStart code review1
patch/0014-vllm_0.28.0-dsv4-cpu-fused-cpp-moe.patch
@@ -0,0 +427,4 @@
427++ and config.dp_size == 1
428++ and config.pcp_size == 1
429++ and not config.is_sequence_parallel
430++ )
atomgit-bot
atomgit-bot9 days agoComments:

🔵 Low Priority

建议:在并行配置判定中显式拒绝 pp_size>1(或改为按全局 rank 取 CPU 绑定与缓存文件名),避免多 PP stage 绑定同一组核且静默通过线程数校验。

likedislike
Incorrect?
atomgit-bot
atomgit-bot9 days agoStart code review1
patch/0014-vllm_0.28.0-dsv4-cpu-fused-cpp-moe.patch
@@ -0,0 +569,4 @@
569++ raise RuntimeError("fused_cpp Plan V2 requires TP-local routing")
570++ moe = _load_fused_cpp_moe()
571++ if moe is None or self.runtime is not moe.get_default_moe_planner_runtime():
572++ raise RuntimeError("process-default fused_cpp planner runtime was replaced")
atomgit-bot
atomgit-bot9 days agoComments:

🔵 Low Priority

建议:在 prepare 阶段把 moe 模块句柄缓存在实例上,apply 热路径只做一次属性读取 + runtime 一致性比较,避免每步重复 import 与后端能力探测。

likedislike
Incorrect?
fangjijiafangjijiaCollaborators
8 days ago Modify description of pull request
5 event messages are collapsed here。 See more
BoostKit-botBoostKit-botCollaborators
8 days ago Added label: boostkit-cla/yes
BoostKit-bot
BoostKit-botCollaborators
8 days ago Comments:

CLA 签署成功

fangjijia, 感谢您的代码贡献。 所有的代码贡献者都已完成了 CLA 签署。 👍

likedislike
fangjijiafangjijiaCollaborators
8 days ago Modify description of pull request