Prhub

#47128 [ROCm] [PyTorch] Move to stable abi since ROCm upgraded to torch 2.11

原始 PR 作者 tjtanaa 合并时间 2026-07-02 18:34 文件变更 6 提交数 2 评论 5 代码增减 +22 / -131

执行摘要

ROCm 切换到 stable ABI,清理 fallback 代码

ROCm 升级到 PyTorch 2.11(PR#45362)后,可以移除之前为适应 ROCm 2.10 而建立的 fallback ABI 循环(PR#44648),统一使用 stable ABI,减少维护负担。

值得精读,特别是关注跨后端 ABI 兼容性管理的读者。展示了在依赖升级后如何系统性地消除技术债务。

讨论亮点
  • Review 中 cleonard530 指出 csrc/torch_bindings.cpp 中注册 get_cuda_view_from_cpu_tensorTORCH_LIBRARY_EXPAND 块已无实际注册,建议删除整个作用域。作者采纳后提交了第二个 commit 清理了该作用域。

实现拆解

  1. 移除旧扩展的歧义注册:在 csrc/torch_bindings.cpp 中,删除了原先为 ROCm 保留的 TORCH_LIBRARY_EXPAND(TORCH_EXTENSION_NAME, ops) 和 TORCH_LIBRARY_EXPAND(CONCAT(TORCH_EXTENSION_NAME, _cuda_utils), cuda_utils) 块。
  2. 将操作转入 stable 扩展:在 csrc/libtorch_stable/torch_bindings.cpp 中,移除了关于 get_cuda_view_from_cpu_tensor 的 #ifndef USE_ROCM 守卫,使其同时为 CUDA/ROCm 注册;同时移除 TORCH_LIBRARY_IMPL 和 TORCH_LIBRARY_FRAGMENT 外层的 #ifndef USE_ROCM,使得 cuda_utils 等操作对 ROCm 也可用。
  3. 统一头文件声明:在 csrc/libtorch_stable/ops.h 中,将 get_cuda_view_from_cpu_tensor 的声明从 #ifndef USE_ROCM 内部移至共享区域,删除 csrc/ops.h 中的同名声明,使声明与 stable 扩展对齐。
  4. 迁移源文件:删除 csrc/cuda_view.cu(其功能已在 csrc/libtorch_stable/cuda_view.cu 中实现);将 cuda_utils_kernels.cu 从旧的扩展源列表移至 stable 扩展源列表。
  5. 简化 CMake 构建:将 TORCH_TARGET_VERSION 从之前的 ROCm 2.10 对应值 0x020A 改为统一的 0x020B,并移除之前为 CUDA 和 ROCm 分别设置的冗余宏定义。
文件 模块 状态 重要度
csrc/torch_bindings.cpp 扩展入口 modified 6.11
csrc/libtorch_stable/torch_bindings.cpp 稳定扩展 modified 5.24
csrc/libtorch_stable/ops.h 稳定扩展 modified 5.11
csrc/cuda_view.cu 扩展入口 removed 5.22
csrc/ops.h 扩展入口 modified 4.98
CMakeLists.txt 构建配置 modified 3.41

关键源码片段

csrc/torch_bindings.cpp dependency-wiring

该文件是旧扩展注册入口,移除了 ROCm 回退注册块,是清理的核心

// Provides torch::Tensor for ops.h
#include <torch/all.h>
#include "ops.h"
#include "core/registration.h"
#include <torch/library.h>
#include <torch/version.h>// 注意:此前这里有两个 TORCH_LIBRARY_EXPAND 块用于 ROCm 的
// get_cuda_view_from_cpu_tensor 和 cuda_utils 注册,现已全部删除。
// 这些函数现在通过 stable ABI 扩展(_C_stable_libtorch)注册。#ifdef USE_ROCM
TORCH_LIBRARY_FRAGMENT(CONCAT(TORCH_EXTENSION_NAME, _custom_ar), custom_ar) {
  // Quick Reduce all-reduce kernels (ROCm-only) — 保留在旧扩展
  custom_ar.def(
      "qr_all_reduce(int fa, Tensor inp, Tensor out, int quant_level, bool "
      "cast_bf2half) -> ()");
  custom_ar.impl("qr_all_reduce", torch::kCUDA, &qr_all_reduce);
  custom_ar.def("init_custom_qr", &init_custom_qr);
  custom_ar.def("qr_destroy", &qr_destroy);
  custom_ar.def("qr_get_handle", &qr_get_handle);
  custom_ar.def("qr_open_handles(int _fa, Tensor[](b!) handles) -> ()");
  custom_ar.impl("qr_open_handles", torch::kCPU, &qr_open_handles);
  custom_ar.def("qr_max_size", &qr_max_size);
}
#endifREGISTER_EXTENSION(TORCH_EXTENSION_NAME)
csrc/libtorch_stable/torch_bindings.cpp core-logic

stable 扩展的注册入口,统一了 get_cuda_view_from_cpu_tensor 的注册条件

#include "ops.h"
#include "cuda_utils.h"
#include "core/registration.h"
#include <torch/csrc/stable/library.h>STABLE_TORCH_LIBRARY_FRAGMENT(_C, ops) {
  // ... 其他 op 注册 ...
  // 现在无条件注册,不再被 #ifndef USE_ROCM 守卫
  ops.def("get_cuda_view_from_cpu_tensor(Tensor cpu_tensor) -> Tensor");
  // 其他 CUDA-only 操作仍然受 #ifndef USE_ROCM 守卫
#ifndef USE_ROCM
  // ...
#endif
}// 之前的 // TODO: Remove this once ROCm upgrade to torch 2.11. // #ifndef USE_ROCM 已移除
STABLE_TORCH_LIBRARY_IMPL(_C, CPU, ops) {
  ops.impl("get_cuda_view_from_cpu_tensor",
           TORCH_BOX(&get_cuda_view_from_cpu_tensor));
}STABLE_TORCH_LIBRARY_FRAGMENT(_C_cuda_utils, cuda_utils) {
  // 现在无条件定义
  cuda_utils.def("get_device_attribute(int attribute, int device_id) -> int");
  cuda_utils.def(
      "get_max_shared_memory_per_block_device_attribute(int device_id) -> int");
}STABLE_TORCH_LIBRARY_IMPL(_C_cuda_utils, CompositeExplicitAutograd,
                          cuda_utils) {
  cuda_utils.impl("get_device_attribute", TORCH_BOX(&get_device_attribute));
  cuda_utils.impl("get_max_shared_memory_per_block_device_attribute",
                  TORCH_BOX(&get_max_shared_memory_per_block_device_attribute));
}
csrc/libtorch_stable/ops.h core-logic

声明文件,移除了守卫以支持 ROCm

// ... AllSpark ops declarations ...#endif// 注:此前 get_cuda_view_from_cpu_tensor 声明被 #ifndef USE_ROCM ... #endif 守卫,
// 仅用于 CUDA 路径。现在移除守卫,对 ROCm 也可见。
// CPU tensor -> CUDA UVA view (shared CUDA/ROCm)
torch::stable::Tensor get_cuda_view_from_cpu_tensor(
    torch::stable::Tensor& cpu_tensor);// Attention kernels (shared CUDA/ROCm)
void merge_attn_states(
    torch::stable::Tensor& output,
    std::optional<torch::stable::Tensor> output_lse,
    // ... 其他参数
);

评论区精华

删除多余的 TORCH_LIBRARY_EXPAND 作用域 设计

cleonard530 指出 csrc/torch_bindings.cpp 中的 TORCH_LIBRARY_EXPAND(TORCH_EXTENSION_NAME, ops) 作用域已无任何注册,建议完全移除。

结论:作者采纳,在第二 commit 中移除了该作用域。 · 已解决

风险与影响

  • 构建风险:若 libtorch_stable/cuda_view.cu 或 cuda_utils_kernels.cu 在 ROCm 下编译失败,会阻塞构建。但测试已覆盖基本功能路径。
  • 运行时风险:get_cuda_view_from_cpu_tensor 的注册路径变化可能导致旧版本 ROCm 用户(仍使用 torch < 2.11)链接失败。此 PR 明确要求 torch >= 2.11。
  • 兼容性风险:统一 TORCH_TARGET_VERSION 为 0x020B 意味着 stable 扩展与 PyTorch 2.11+ 的 C-shim 兼容,与早期版本不兼容。
  • 用户:要求 ROCm 环境必须使用 PyTorch 2.11 或更高版本。
  • 系统:构建系统更简洁,减少条件编译分支,提升可维护性。
  • 团队:不再需要维护两套 ABI 注册,降低了长期维护成本。
依赖版本升级 构建配置调整 ABI 兼容性

关联 Issue

未识别关联 Issue

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

完整报告

参与讨论