Support CINN on XPU#79467
Conversation
PaddlePaddle-bot
left a comment
There was a problem hiding this comment.
🤖 Paddle-CI-Agent | pr_review |
2026-07-13 18:59:48
📋 Review 摘要
PR 概述:为 CINN 增加 XPU target、backend codegen/compiler、runtime module/API/intrinsics 和 CMake 接入。
变更范围:cmake/cinn.cmake、paddle/cinn/common、paddle/cinn/backends/xpu、paddle/cinn/runtime/xpu
影响面 Tag:CINN Environment Adaptation Custom Device
问题
建议拆分方案:
- PR 1: XPU target/架构接入 —
paddle/cinn/common/arch.h,paddle/cinn/common/target.*,paddle/cinn/runtime/backend_api.cc - PR 2: XPU 编译器和 host launch 链路 —
paddle/cinn/backends/xpu/*,paddle/cinn/backends/compiler.*,paddle/cinn/backends/codegen_device_util.* - PR 3: XPU runtime/intrinsics 与 CMake 集成 —
paddle/cinn/runtime/xpu/*,cmake/cinn.cmake
| 级别 | 文件 | 概述 |
|---|---|---|
| 🔴 Bug | paddle/cinn/common/arch.h:30 |
新增 XpuArch 后未补齐现有 arch.Match 分支,CINN 编译/注册/launch 链路不可达且可能直接编译失败 |
| 🔴 Bug | paddle/cinn/backends/xpu/compiler_xpu.cc:23 |
WITH_XPU 路径直接依赖 CUDA/NVRTC,但 XPU 构建与 GPU 互斥且未配置 CUDA include/link |
| 🟡 建议 | PR-level | 变更量较大,建议按 target 接入、编译器链路、runtime/intrinsics 拆分 |
📝 PR 规范检查
标题缺少官方 Tag;描述 section 齐全,但 Description 未说明验证方式。
标题建议(可直接复制):
[CINN] Support CINN on XPU
PR 描述建议(点击展开,可直接复制)
### PR Category
CINN
### PR Types
Improvements
### Description
Support CINN on XPU by adding XPU target arch, XPU backend codegen/compiler scaffolding, XPU runtime BackendAPI/module/intrinsics, and CMake integration.
Impact scope: `paddle/cinn/common`, `paddle/cinn/backends/xpu`, `paddle/cinn/runtime/xpu`, and `cmake/cinn.cmake`.
Verification: XPU build and CINN XPU runtime validation pending due to current build blockers.
### 是否引起精度变化
否总体评价
本轮按风险优先审查了 XPU arch 接入、CMake 依赖、compiler/runtime 注册链路等核心变更,未全量逐行覆盖其余机械迁移的 intrinsic 文件。当前存在构建级阻塞问题,需要先补齐 XPU 分支闭环并修正 XPU 工具链依赖后再继续验证数值和 runtime 行为。
| #define CINN_ARCH_CLASS_NAMES(_macro) \ | ||
| _macro(X86Arch) _macro(ARMArch) _macro(NVGPUArch) _macro(HygonDCUArchHIP) \ | ||
| _macro(HygonDCUArchSYCL) | ||
| _macro(HygonDCUArchSYCL) _macro(XpuArch) |
There was a problem hiding this comment.
🔴 Bug XpuArch is added to ArchBase, but the existing exhaustive arch.Match visitors are not updated.
DEFINE_MATCH_METHOD is a thin wrapper around std::visit(Overloaded{...}), so every compiled visitor must be callable for the new XpuArch alternative. Files that are built with CINN still lack this branch, including paddle/cinn/backends/compiler.cc (Build, GetSourceCode, BuildDefault, RegisterDeviceModuleSymbol) and paddle/cinn/backends/codegen_device_util.*. This means the PR can fail to compile as soon as those visitors instantiate; and even after compilation, XPU codegen/module registration/launch would still be unreachable.
建议修复方式:
补齐所有 Arch::Match / std::visit 位置的 common::XpuArch 分支,串起 CompileXpuModule、RegisterXpuModuleSymbol、CodeGenXpuDevice、XPU host launch intrinsic,以及 GetArchDevice / SetArchDevice。建议用 rg -n "arch\.Match|std::visit" paddle/cinn 做闭环检查。
| #include <glog/logging.h> | ||
|
|
||
| #ifdef CINN_WITH_XPU | ||
| #include <cuda.h> |
There was a problem hiding this comment.
🔴 Bug WITH_XPU 路径直接依赖 CUDA/NVRTC 头和库,但 XPU 构建不会配置这些依赖。
cmake/cinn.cmake 在 WITH_XPU 下定义 CINN_WITH_XPU 并收集该文件;同时顶层 CMake 明确禁止 WITH_GPU && WITH_XPU,而 CUDA include、find_package(CUDA REQUIRED)、${CUDA_NVRTC_LIB} / ${CUDA_LIBRARIES} 的链接都只在 WITH_GPU 分支中执行。因此普通 XPU-only 构建会编译到这里的 <cuda.h> / <cuda_runtime_api.h> / <nvrtc.h> 和后续 CUDA symbols,却没有对应 include/link 设置,构建会失败。
建议修复方式:
改为使用 XPU/Kunlun 的编译器与 runtime API,并在 WITH_XPU CMake 分支链接对应 target;或者引入独立的 CUDA-compatible backend 选项,显式要求 CUDA、添加 CUDA include 和 nvrtc/driver/runtime 链接,并且不要由普通 WITH_XPU 自动开启。
risemeup1111
left a comment
There was a problem hiding this comment.
已完成首轮 review。当前有需要修复后再继续合入的构建/接入问题,详情已放在 inline review comments 中;另外 CI 里 pre-commit 和 approval 检查也显示还需要处理格式与审批项。
| #ifdef CINN_WITH_CUDA | ||
| return DefaultNVGPUTarget(); | ||
| #elif defined(CINN_WITH_XPU) | ||
| return DefaultXpuTarget(); |
There was a problem hiding this comment.
优先级:P0
处理要求:请针对该评论修复并提交新的 commit。
这里把 CINN_WITH_XPU 下的默认设备切到 DefaultXpuTarget() 后,所有 DefaultDeviceTarget().arch.Match(...) 调用都会开始实例化 XpuArch 这个 variant 分支。但当前核心路径还没有接入 XPU:例如 paddle/cinn/backends/compiler.cc 里的 Build / BuildDefault / GetSourceCode / RegisterDeviceModuleSymbol 仍只覆盖 CUDA/HIP/SYCL/CustomDevice,codegen_device_util.{h,cc} 和 codegen_cuda_host.h 的 kernel launch 选择/降低也没有 XpuArch 分支。Arch::Match 基于 std::visit,缺少任一 alternative 的处理会直接导致编译期报错;即使补过编译,也不会生成并降低新注册的 cinn_call_xpu_kernel。
请把 XPU 分支完整接入这些调度点,并实现已声明的 CompileXpuModule / RegisterXpuModuleSymbol。预期形态类似:
[&](common::XpuArch) { CompileXpuModule(module, code); }
...
[&](common::XpuArch) { RegisterXpuModuleSymbol(); }
...
[&](common::XpuArch) {
call_kernel = runtime::intrinsic::call_xpu_kernel; // 或新增等价的 XPU intrinsic 常量
}同时需要在 host lowering 中把 cinn_call_xpu_kernel 走 LowerGPUKernelCall,否则 XPU backend 只有 runtime 注册,编译器不会真正调用它。
|
|
||
| if(WITH_XPU) | ||
| message(STATUS "CINN Compile with XPU support") | ||
| add_definitions(-DCINN_WITH_XPU) |
There was a problem hiding this comment.
优先级:P0
处理要求:请针对该评论修复并提交新的 commit。
这个 WITH_XPU 分支只定义了 CINN_WITH_XPU,但新增的 XPU CINN 源文件会直接包含并调用 CUDA/NVRTC API:paddle/cinn/backends/xpu/compiler_xpu.cc 包含 <cuda.h>、<cuda_runtime_api.h>、<nvrtc.h> 并调用 nvrtcCompileProgram,xpu_module.cc / xpu_util.cc 也调用 CUDA driver/runtime API。当前 CUDA include/link setup 只在 WITH_GPU 分支执行,cmake/cinn/nvrtc.cmake 还在 NOT WITH_GPU 时直接 return,cinnapi / cinncore 也只在 WITH_GPU 时链接 ${CUDA_NVRTC_LIB}、${CUDA_LIBRARIES}、${CUDASTUB}。因此纯 WITH_XPU + WITH_CINN 构建会找不到这些头/符号,或者链接不到 NVRTC/CUDA driver runtime。
请在 WITH_XPU 路径显式接入 XPU 使用的 CUDA-compat 头和库,或者改为使用 XPU SDK 提供的等价动态加载/编译接口,不能依赖 WITH_GPU 才配置这些依赖。预期至少需要类似下面的 CMake 结构,并同步到 cinnapi / cinncore link blocks:
if(WITH_XPU)
add_definitions(-DCINN_WITH_XPU)
include_directories(${XPU_INC_DIR})
# 如仍使用 NVRTC/CUDA driver API,这里需要查找并链接对应库;
# 若使用 XPU 的 libcudart/libxpucuda/libxpujitc,也请把这些目标接入 cinnapi/cinncore。
target_link_libraries(cinnapi xpulib ${XPU_CUDA_RT_LIB} ${CUDA_NVRTC_LIB})
endif()具体库名可以按 XPU SDK 现有变量调整,但要保证 WITH_XPU 单独开启时这些新增文件可以编译并链接通过。
| // XPU uses the CUDA programming model: include the CUDA-compatible | ||
| // cinn_xpu_runtime_source.cuh device header. | ||
| const std::string CodeGenXpuDevice::source_header_ = // NOLINT | ||
| R"(#define CINN_WITH_XPU |
There was a problem hiding this comment.
优先级:P1
处理要求:请针对该评论修复并提交新的 commit。
这里生成的 XPU device source 只定义了 CINN_WITH_XPU,但 cinn_xpu_runtime_source.cuh 中所有 fp16/bf16 device intrinsics 都分别被 #ifdef CINN_XPU_FP16 和 #ifdef CINN_XPU_BF16 包住;当前 preamble 没有定义这两个宏,所以这些函数不会进入 NVRTC 编译单元。与此同时 xpu_intrinsics_float16.cc / xpu_intrinsics_bfloat16.cc 已经把 cinn_xpu_*_fp16、cinn_xpu_*_bf16 注册给 XPU target,lower 出 fp16/bf16 的 elementwise/reduce 时会引用不存在的 device symbol,导致 JIT 编译失败。
请在 device source preamble 中启用这些宏,或移除 runtime header 里的条件保护并保持和注册表一致。例如:
| R"(#define CINN_WITH_XPU | |
| R"(#define CINN_WITH_XPU | |
| #define CINN_XPU_FP16 | |
| #define CINN_XPU_BF16 | |
| #include "float16.h" |
如果 XPU 暂时不支持 bf16/fp16 的某一类 intrinsic,则不要先注册对应 cinn_xpu_* 符号,避免编译器生成运行时无法解析的调用。
|
你的PR提交成功,感谢你对开源项目的贡献! |
CI报告基于以下代码生成(30分钟更新一次): 1 Required任务 : 32/38 通过
2 失败详情🔴 Coverage build — PR问题(置信度: 高)分析器: 通用分析(fallback)
关键日志:
修复建议:
关联变更: 🔴 Linux-build / Build — PR问题(置信度: 高)分析器: 通用分析(fallback)
关键日志:
修复建议:
关联变更: 🔴 Check approval — 需要 Approval(置信度: 高)该 Job 需要人工 Approval,完成审批后 CI 才会继续执行。 🔴 Pre Commit — PR问题(置信度: 高)分析器: 通用分析(fallback)
关键日志:
修复建议:
关联变更: 🔴 Windows-GPU / Build and test — 环境问题(置信度: 中)分析器: 通用分析(fallback)
关键日志:
修复建议:
关联变更: 未发现与本 PR 变更的直接关联。 |
PR Category
CINN
PR Types
Improvements
Description
Support CINN on XPU
是否引起精度变化
否
Pcard-90546