Skip to content

Support CINN on XPU#79467

Open
ZhangX-21 wants to merge 1 commit into
PaddlePaddle:developfrom
ZhangX-21:xpu_cinn
Open

Support CINN on XPU#79467
ZhangX-21 wants to merge 1 commit into
PaddlePaddle:developfrom
ZhangX-21:xpu_cinn

Conversation

@ZhangX-21

Copy link
Copy Markdown
Contributor

PR Category

CINN

PR Types

Improvements

Description

Support CINN on XPU

是否引起精度变化


Pcard-90546

@PaddlePaddle-bot PaddlePaddle-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 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.cmakepaddle/cinn/commonpaddle/cinn/backends/xpupaddle/cinn/runtime/xpu
影响面 TagCINN Environment Adaptation Custom Device

问题

⚠️ 本 PR 变更量较大(28 文件 / 2348 行),建议拆分以降低审查难度和合入风险。

建议拆分方案

  • 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 行为。

Comment thread paddle/cinn/common/arch.h
#define CINN_ARCH_CLASS_NAMES(_macro) \
_macro(X86Arch) _macro(ARMArch) _macro(NVGPUArch) _macro(HygonDCUArchHIP) \
_macro(HygonDCUArchSYCL)
_macro(HygonDCUArchSYCL) _macro(XpuArch)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 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 分支,串起 CompileXpuModuleRegisterXpuModuleSymbolCodeGenXpuDevice、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>

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Bug WITH_XPU 路径直接依赖 CUDA/NVRTC 头和库,但 XPU 构建不会配置这些依赖。

cmake/cinn.cmakeWITH_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 risemeup1111 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已完成首轮 review。当前有需要修复后再继续合入的构建/接入问题,详情已放在 inline review comments 中;另外 CI 里 pre-commit 和 approval 检查也显示还需要处理格式与审批项。

Powered by Nyanpasu with gpt-5.5 xhigh, please check the suggestions carefully.

#ifdef CINN_WITH_CUDA
return DefaultNVGPUTarget();
#elif defined(CINN_WITH_XPU)
return DefaultXpuTarget();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P0 优先级: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_kernelLowerGPUKernelCall,否则 XPU backend 只有 runtime 注册,编译器不会真正调用它。

Comment thread cmake/cinn.cmake

if(WITH_XPU)
message(STATUS "CINN Compile with XPU support")
add_definitions(-DCINN_WITH_XPU)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P0 优先级: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> 并调用 nvrtcCompileProgramxpu_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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 优先级: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_*_fp16cinn_xpu_*_bf16 注册给 XPU target,lower 出 fp16/bf16 的 elementwise/reduce 时会引用不存在的 device symbol,导致 JIT 编译失败。

请在 device source preamble 中启用这些宏,或移除 runtime header 里的条件保护并保持和注册表一致。例如:

Suggested change
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_* 符号,避免编译器生成运行时无法解析的调用。

@paddle-bot

paddle-bot Bot commented Jul 14, 2026

Copy link
Copy Markdown

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@PaddlePaddle-bot

PaddlePaddle-bot commented Jul 15, 2026

Copy link
Copy Markdown

🤖 Paddle-CI-Agent | ci_status_monitor | 2026-07-17 05:48:49 UTC+08:00

CI报告基于以下代码生成(30分钟更新一次):
PR commit: b3fa0ce | Merge base: 206a21d (branch: develop)


1 Required任务 : 32/38 通过

总执行(rerun次数) 总任务 ✅ 通过 ❌ 失败 ⏳ 运行中 ⏸️ 等待中 跳过
67(0) 67 53 5 0 0 9
任务 错误类型 置信度 日志
Coverage build PR问题 Job
Linux-build / Build PR问题 Job
Check approval 需要 Approval Job
Pre Commit PR问题 Job
Windows-GPU / Build and test 环境问题 Job

2 失败详情

🔴 Coverage build — PR问题(置信度: 高)

分析器: 通用分析(fallback)
失败用例: 构建阶段

用例 错误摘要
coverage_build.sh bdist_wheel flags.cc 编译失败,XpuArch 没有匹配的 CheckCompileOptionImpl 重载

关键日志:

/paddle/paddle/cinn/runtime/flags.cc:412:66: error: no matching function for call to ‘CheckCompileOptionImpl(const cinn::common::XpuArch&)’
return std::visit([](const auto& impl) { CheckCompileOptionImpl(impl); }, arch.variant());
/paddle/paddle/cinn/runtime/flags.cc:359:6: note: candidate: void CheckCompileOptionImpl(cinn::common::UnknownArch)
no known conversion for argument 1 from const cinn::common::XpuArch
  • 根因摘要: XpuArch 缺少编译选项检查重载
    PR 在 paddle/cinn/common/arch.h 中把 XpuArch 加入 Arch variant,但 paddle/cinn/runtime/flags.ccCheckCompileOptionImpl 仍只覆盖 Unknown/X86/ARM/NVGPU/CustomDevice/HygonDCU。std::visit 对 variant 的所有备选类型做编译期检查,因此即使当前目标不是 XPU,也会因缺少 XpuArch 重载而编译失败。

修复建议:

  1. paddle/cinn/runtime/flags.cc 增加 CheckCompileOptionImpl(cinn::common::XpuArch),按 CINN_WITH_XPU 做编译期开关检查。
  2. 本地至少验证 Linux-build / Build 或 coverage build 的 C++ 编译阶段。

关联变更: paddle/cinn/common/arch.h:28 新增 XpuArchpaddle/cinn/runtime/flags.cc:359412 未补 XPU 重载。

🔴 Linux-build / Build — PR问题(置信度: 高)

分析器: 通用分析(fallback)
失败用例: 构建阶段

用例 错误摘要
PR-CI-Inference / Build 同样因 CheckCompileOptionImpl(const XpuArch&) 缺失导致编译退出 7

关键日志:

/usr/local/gcc-8.2/include/c++/8.2.0/variant:1393:23: required from std::visit
/paddle/paddle/cinn/runtime/flags.cc:412:66: error: no matching function for call to ‘CheckCompileOptionImpl(const cinn::common::XpuArch&)’
/paddle/paddle/cinn/runtime/flags.cc:359:6: note: candidate: void CheckCompileOptionImpl(cinn::common::UnknownArch)
##[error]Process completed with exit code 7.
  • 根因摘要: 与 coverage build 相同的 XpuArch 编译缺口
    该 job 的失败栈与 Coverage build 一致,都是 Arch variant 新增 XpuArch 后,flags.cc 的 visitor 无法实例化对应分支。该失败与本 PR 的 CINN XPU 架构扩展直接相关。

修复建议:

  1. 同步修复 paddle/cinn/runtime/flags.ccXpuArch 处理,并确认所有 std::visit/overload 风格代码对新增架构类型都有覆盖。

关联变更: paddle/cinn/common/arch.h:28 新增 XpuArch;失败点在 paddle/cinn/runtime/flags.cc:412

🔴 Check approval — 需要 Approval(置信度: 高)

该 Job 需要人工 Approval,完成审批后 CI 才会继续执行。

🔴 Pre Commit — PR问题(置信度: 高)

分析器: 通用分析(fallback)
失败用例: 代码格式检查

用例 错误摘要
clang-format hook 修改了 PR 中的 C++ 文件,说明格式化结果未提交

关键日志:

clang-format.............................................................Failed
- hook id: clang-format
- files were modified by this hook
diff --git a/paddle/cinn/runtime/xpu/xpu_module.cc b/paddle/cinn/runtime/xpu/xpu_module.cc
diff --git a/paddle/cinn/runtime/xpu/xpu_util.cc b/paddle/cinn/runtime/xpu/xpu_util.cc
diff --git a/paddle/cinn/runtime/xpu/xpu_util.h b/paddle/cinn/runtime/xpu/xpu_util.h
  • 根因摘要: clang-format 修改未提交
    pre-commit 的 clang-format hook 对新增 XPU runtime 文件生成了格式化 diff,CI 因工作区会被 hook 修改而失败。这是 PR 代码格式问题,不是环境失败。

修复建议:

  1. 运行日志给出的 pre-commit run --files ...,至少提交 paddle/cinn/runtime/xpu/xpu_module.ccpaddle/cinn/runtime/xpu/xpu_util.ccpaddle/cinn/runtime/xpu/xpu_util.h 的格式化结果。

关联变更: paddle/cinn/runtime/xpu/xpu_module.ccpaddle/cinn/runtime/xpu/xpu_util.ccpaddle/cinn/runtime/xpu/xpu_util.h

🔴 Windows-GPU / Build and test — 环境问题(置信度: 中)

分析器: 通用分析(fallback)
失败用例: Windows CMake 生成阶段

用例 错误摘要
ops_api_gen.py / generated_static_prim_api PyYAML 内部异常后 static_prim_api.cc 未生成,CMake 配置失败

关键日志:

TypeError: 'bool' object is not iterable
CMake Error at cmake/generic.cmake:376 (add_library):
  Cannot find source file:
    static_prim_api.cc
No SOURCES given to target: generated_static_prim_api
##[error]Process completed with exit code 7.
  • 根因摘要: Windows 生成文件异常导致 CMake 失败
    日志显示失败发生在 Windows 环境的 Python/YAML 解析与生成文件阶段,随后 CMake 找不到 static_prim_api.cc。本 PR 未修改 op YAML、prim API 生成器或相关 CMake 目标,当前证据更像 Windows CI 环境/偶发生成异常。

修复建议:

  1. 环境问题,请 rerun;若重跑仍稳定复现,再补充检查 Windows runner 的 Python/PyYAML 环境和生成文件写入权限。

关联变更: 未发现与本 PR 变更的直接关联。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants