Prhub

#44365 [10b/n] Migrate custom all-reduce, DeepSeek V4 fused MLA, MiniMax reduce-RMS, and MXFP8 MoE to libtorch stable ABI

原始 PR 作者 cleonard530 合并时间 2026-06-04 00:29 文件变更 18 提交数 3 评论 5 代码增减 +568 / -481

执行摘要

迁移四组 CUDA 内核到 libtorch stable ABI

继续 libtorch stable ABI 迁移,减少 legacy _C 中的不稳定符号,提升二进制兼容性。PR body 指出迁移进度表明 main 分支 _C 有 78 个不稳定符号,而 _C_stable_libtorch 中为 0,此 PR 旨在进一步减少不稳定依赖。

值得精读,特别是 stable ABI 适配的模式(如使用 STD_TORCH_CHECK、STABLE_TORCH_LIBRARY_FRAGMENT 和 TORCH_BOX 宏)。对于贡献者,了解这些模式有助于参与后续迁移步骤。设计上保留 ROCm 专用 QuickReduce 在 legacy 是一个透明决策,需关注后续迁移计划。

讨论亮点

Review 中主要讨论了以下问题:

  • _is_weak_contiguous 实现差异:cleonard530 在 custom_all_reduce.cu 中重新实现了 _is_weak_contiguous 函数,因为 stable Tensor 包装器不暴露 storage(),所以需要改用其他方式判断连续性。这属于 stable ABI 适配中的典型调整。
  • 文件显示为删除/创建:janeyx99 指出 csrc/moe/mxfp8_moe/cutlass_mxfp8_grouped_mm.cumxfp8_experts_quant.cu 显示为删除和新建而非重命名移动。cleonard530 解释这是由于 diff 规模过大,GitHub 无法自动识别为重命名,实际内容已完整移入 csrc/libtorch_stable/ 对应目录。

实现拆解

实现拆解:

  1. 声明迁移:在 csrc/libtorch_stable/ops.h 中新增 fused_deepseek_v4_qnorm_rope_kv_rope_quant_insertminimax_allreduce_rms 等函数声明,并使用 torch::stable::Tensor 类型。同时在 csrc/libtorch_stable/ops.h 中新增自定义 all-reduce 相关函数声明(init_custom_arall_reduce 等)。
  2. 移动与适配实现:将 csrc/fused_deepseek_v4_qnorm_rope_kv_insert_kernel.cucsrc/minimax_reduce_rms_kernel.cucsrc/custom_all_reduce.cu 以及 csrc/moe/mxfp8_moe/ 目录下的实现文件移动到 csrc/libtorch_stable/ 下对应位置,并在其中将 CUDA 错误检查宏(如 CUDA_CHECK)替换为 STD_CUDA_CHECKTORCH_CHECK 替换为 STD_TORCH_CHECK,以适配 stable ABI。关键调整包括在 custom_all_reduce.cu 中重写 _is_weak_contiguous 函数(因 stable 包装器不暴露 storage())。
  3. 注册稳定绑定:在 csrc/libtorch_stable/torch_bindings.cpp 中通过 STABLE_TORCH_LIBRARY_FRAGMENTSTABLE_TORCH_LIBRARY_IMPL 注册上述 ops 的声明和实现。对 custom all-reduce,拆分到单独的 _C_custom_ar 库片段,并按调度键(CUDA、CPU、CompositeExplicitAutograd)分别实现。
  4. 移除旧绑定:在 csrc/torch_bindings.cpp 中删除对应 ops 的 ops.defops.impl 调用(例如 fused_deepseek_v4_qnorm_rope_kv_rope_quant_insertminimax_allreduce_rmsminimax_allreduce_rms_qk),并删除整个 TORCH_LIBRARY_EXPAND(_C_custom_ar, ...)TORCH_LIBRARY_EXPAND(_C_cuda_utils, ...) 片段。
  5. 清理旧头文件:在 csrc/ops.h 中删除迁移的函数声明(fused_deepseek_v4_qnorm_rope_kv_rope_quant_insertinit_custom_arall_reduce 等),避免与 csrc/libtorch_stable/ops.h 冲突。更新 csrc/minimax_reduce_rms_kernel.h 的 include 路径。
  6. 调整 CMake 构建:更新 CMakeLists.txt 和 setup.py 将新添加的源文件纳入 _C_stable_libtorch 编译目标,并从 _C 中排除已迁移文件。
文件 模块 状态 重要度
csrc/torch_bindings.cpp CUDA 绑定 modified 6.47
csrc/libtorch_stable/torch_bindings.cpp 稳定 ABI 绑定 modified 6.27
csrc/libtorch_stable/ops.h 稳定 ABI 接口 modified 6.31
csrc/ops.h CUDA 接口 modified 6.24
csrc/libtorch_stable/minimax_reduce_rms_kernel.cu MiniMax 内核 renamed 5.66

关键符号

fused_deepseek_v4_qnorm_rope_kv_rope_quant_insert minimax_allreduce_rms minimax_allreduce_rms_qk init_custom_ar all_reduce _is_weak_contiguous

关键源码片段

csrc/libtorch_stable/torch_bindings.cpp core-logic

在 stable 库中新增所有迁移 ops 的注册和实现,包括 custom all-reduce 的完整绑定。

// csrc/libtorch_stable/torch_bindings.cpp 中新增的 custom all-reduce 绑定片段// 定义 custom_ar 库片段(STABLE_TORCH_LIBRARY_FRAGMENT 声明)
STABLE_TORCH_LIBRARY_FRAGMENT(_C_custom_ar, custom_ar) {
  custom_ar.def(
      "init_custom_ar(int[] ipc_tensors, Tensor rank_data, "
      "int rank, bool fully_connected) -> int");
  custom_ar.def(
      "all_reduce(int fa, Tensor inp, Tensor! out, int reg_buffer, "
      "int reg_buffer_sz_bytes) -> ()");
  custom_ar.def("dispose(int fa) -> ()");
  custom_ar.def("meta_size() -> int");
  custom_ar.def("register_buffer(int fa, int[] ipc_tensors) -> ()");
  custom_ar.def("get_graph_buffer_ipc_meta(int fa) -> (int[], int[])");
  custom_ar.def(
      "register_graph_buffers(int fa, int[][] handles, int[][] offsets) -> ()");
  custom_ar.def("allocate_shared_buffer_and_handle(int size) -> (int, Tensor)");
  custom_ar.def("open_mem_handle(Tensor mem_handle) -> int");
  custom_ar.def("free_shared_buffer(int ptr) -> ()");
}// 根据设备分别实现
STABLE_TORCH_LIBRARY_IMPL(_C_custom_ar, CUDA, custom_ar) {
  custom_ar.impl("init_custom_ar", TORCH_BOX(&init_custom_ar));
  custom_ar.impl("all_reduce", TORCH_BOX(&all_reduce));
}STABLE_TORCH_LIBRARY_IMPL(_C_custom_ar, CPU, custom_ar) {
  custom_ar.impl("open_mem_handle", TORCH_BOX(&open_mem_handle));
}STABLE_TORCH_LIBRARY_IMPL(_C_custom_ar, CompositeExplicitAutograd, custom_ar) {
  custom_ar.impl("dispose", TORCH_BOX(&dispose));
  custom_ar.impl("meta_size", TORCH_BOX(&meta_size));
  // ... 其他非设备相关 impl
}

评论区精华

_is_weak_contiguous 的 stable ABI 适配 设计

cleonard530 在 custom_all_reduce.cu 中重新实现了 _is_weak_contiguous,因为 stable Tensor 包装器不暴露 storage(),所以改用其他连续性判断方式。

结论:采用重新实现的方式,已在 PR 中解决。 · 已解决

GitHub 未将文件识别为 rename 的问题 question

janeyx99 询问为何 csrc/moe/mxfp8_moe/cutlass_mxfp8_grouped_mm.cu 和 mxfp8_experts_quant.cu 显示为删除 / 新建而非重命名。cleonard530 解释因为 diff 过大,GitHub 无法自动匹配为重命名。

结论:理解,实际是移动,不影响代码审查。 · 已解决

风险与影响

技术风险包括:

  • ABI 兼容性:虽然使用 stable 类型,但若 python 侧或 GPU 侧调用方式不匹配(如期待 torch::Tensor 却传入 torch::stable::Tensor),可能导致运行时错误。现有测试覆盖了多数路径,但边缘情况(如 custom all-reduce 的 _is_weak_contiguous 重写逻辑)可能引入回归。
  • ROCm 遗留分支:QuickReduce 和 cuda_utils 未迁移,导致 platform 条件编译逻辑复杂化,未来若统一移除 legacy _C 时需要特别处理。
  • 构建一致性:文件移动后,若存在外部对旧路径的引用(如其他分支的 CMake 或自定义构建),可能产生编译错误。

对用户而言,功能无变化,但构建产物(.abi3.so)中不稳定符号减少,有利于跨 PyTorch 版本的部署。对开发团队而言,迁移后更易于维护和添加新内核,但短期内需保持对两个编译目标(_C 和 _C_stable_libtorch)的同步更新。QuickReduce 留在 legacy 意味着后续仍有步骤需要处理。

核心路径变更 ROCm 遗留分支 兼容性风险

关联 Issue

未识别关联 Issue

当前没有检测到明确关联的 Issue 链接,后续同步到相关引用后会出现在这里。

完整报告

参与讨论