Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Phi] Support kps backend and kernel registry #39941

Merged
merged 8 commits into from
Mar 1, 2022

Conversation

chenwhql
Copy link
Contributor

@chenwhql chenwhql commented Feb 25, 2022

PR types

New features

PR changes

Others

Describe

[Phi] Support kps backend and kernel registry

本PR支持基于kernel primitives api实现的kernel能够在phi中实现并被正确调度:

  1. 新增KPS backend:kps的kernel同时服务于GPU与XPU,因此KPS应该被视为类似cudnn,mkldnn的一种高性能库,所以其定位是一种新的backend。
  2. 新增kernels/kps子目录:phi中基础kernel的管理根据backend不同,通过不同子目录管理,因此基于kps实现的kernel均放置于kps中,整体kernel实现方式规范与cpu、gpu、gpudnn等其他backend一致,同时增加了相应的自动化编译支持。

kps注册kernel写法为:

PD_REGISTER_KERNEL(
    dummy, KPS, ALL_LAYOUT, phi::DummyKernel, float, double, int, int64_t) {}

注意,第二个参数为KPS

// on GPU and XPU at the same time, so we need KPSContext when registering
// KPS Kernel. Note: XPU and GPU cannot be compiled at the same time!
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
using KPSContext = GPUContext;
Copy link
Contributor

Choose a reason for hiding this comment

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

其它硬件是不是加一个 elif 就可以?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

这里没有可以elif的对象?我的想法是同质化的context放在同一个文件中,便于include,这里是在gpu_context.h中的

cmake/phi.cmake Outdated
@@ -137,6 +140,11 @@ function(kernel_library TARGET)
list(APPEND xpu_srcs ${CMAKE_CURRENT_SOURCE_DIR}/xpu/${TARGET}.cc)
endif()
endif()
if (WITH_GPU OR WITH_ROCM OR WITH_XPU_KP)
Copy link
Contributor

Choose a reason for hiding this comment

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

目前要求kps目录下文件是.cu后缀是吗

Copy link
Contributor Author

Choose a reason for hiding this comment

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

是的,按那天晚上讨论的结论是这样;而且,使用cu的话,其实不需要再额外支持kps拷贝成cu了

cp ${kernel_path}/${kernel_name}.kps kernel_build/${kernel_name}.xpu
[[ -e ${kernel_path}/${kernel_name}.kps ]] && cp ${kernel_path}/${kernel_name}.kps kernel_build/${kernel_name}.xpu
COMMAND
[[ -e ${kernel_path}/${kernel_name}.cu ]] && cp ${kernel_path}/${kernel_name}.cu kernel_build/${kernel_name}.xpu
Copy link
Contributor

Choose a reason for hiding this comment

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

这是支持.cu 和 .kps两种吗

Copy link
Contributor Author

Choose a reason for hiding this comment

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

只是暂时没删掉原来的,这个需要你们确认一下,如果可以的话,我也可以将kps删除

Copy link
Member

Choose a reason for hiding this comment

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

只是暂时没删掉原来的,这个需要你们确认一下,如果可以的话,我也可以将kps删除

这个还是先别删。

cmake/phi.cmake Outdated
@@ -137,6 +140,11 @@ function(kernel_library TARGET)
list(APPEND xpu_srcs ${CMAKE_CURRENT_SOURCE_DIR}/xpu/${TARGET}.cc)
endif()
endif()
if (WITH_GPU OR WITH_ROCM OR WITH_XPU_KP)
Copy link
Member

Choose a reason for hiding this comment

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

.cu->.xpu文件这种重命名规则,这个目前只会影响phi/kernel/里面的.cu文件吗?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

xpu_library才会改,但是xpu_library只会处理kps目录下的cu文件


} // namespace phi

PD_REGISTER_KERNEL(
Copy link
Member

Choose a reason for hiding this comment

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

目前XPU2 KP的CI还没有建起来,这个注册逻辑是否在XPU2硬件上开with_xpu_kp进行验证了呢

Copy link
Contributor Author

@chenwhql chenwhql Feb 28, 2022

Choose a reason for hiding this comment

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

验证通过,可正确编译
image

cp ${kernel_path}/${kernel_name}.kps kernel_build/${kernel_name}.xpu
[[ -e ${kernel_path}/${kernel_name}.kps ]] && cp ${kernel_path}/${kernel_name}.kps kernel_build/${kernel_name}.xpu
COMMAND
[[ -e ${kernel_path}/${kernel_name}.cu ]] && cp ${kernel_path}/${kernel_name}.cu kernel_build/${kernel_name}.xpu
Copy link
Member

Choose a reason for hiding this comment

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

只是暂时没删掉原来的,这个需要你们确认一下,如果可以的话,我也可以将kps删除

这个还是先别删。

Copy link
Member

@Liu-xiandong Liu-xiandong left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@AnnaTrainingG AnnaTrainingG left a comment

Choose a reason for hiding this comment

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

LGTM

@chenwhql chenwhql merged commit 08b43cc into PaddlePaddle:develop Mar 1, 2022
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.

4 participants