Skip to content
Prev Previous commit
Next Next commit
add pten kernel cmake function
  • Loading branch information
chenwhql committed Dec 21, 2021
commit e866565ae63d402a57e986faa69cf7bd65d44bcc
2 changes: 1 addition & 1 deletion cmake/inference_lib.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ copy(inference_lib_dist
# the header file of pten is copied to the experimental directory,
# the include path of pten needs to be changed to adapt to inference api path
add_custom_command(TARGET inference_lib_dist POST_BUILD
COMMAND ${CMAKE_COMMAND} -P "${PADDLE_SOURCE_DIR}/cmake/header_match.cmake"
COMMAND ${CMAKE_COMMAND} -P "${PADDLE_SOURCE_DIR}/cmake/pten_header.cmake"
COMMENT "Change pten header include path to adapt to inference api path")

# CAPI inference library for only inference
Expand Down
85 changes: 48 additions & 37 deletions cmake/pten_kernel.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,38 @@
# See the License for the specific language governing permissions and
# limitations under the License.

function(kernel_declare TARGET_PATH)
message(DEBUG "TARGET_PATH: ${TARGET_PATH}")
if (EXISTS ${TARGET_PATH})
file(READ ${TARGET_PATH} TARGET_CONTENT)
# call kernel_declare need to make sure the target of input is exists
Copy link
Contributor

Choose a reason for hiding this comment

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

后续pr 中可以删除注释中的 is

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, thx

function(kernel_declare TARGET_LIST)
message(STATUS "TARGET_LIST: ${TARGET_LIST}")
foreach(kernel_path ${TARGET_LIST})
message(STATUS "kernel_path: ${kernel_path}")
file(READ ${kernel_path} kernel_impl)
# TODO(chenweihang): rename PT_REGISTER_CTX_KERNEL to PT_REGISTER_KERNEL
# NOTE(chenweihang): now we don't recommend to use digit in kernel name
string(REGEX MATCH "PT_REGISTER_CTX_KERNEL\\([ \t\r\n]*[a-z_]*," first_registry "${TARGET_CONTENT}")
message(DEBUG "first_registry in ${TARGET_PATH}: ${first_registry}")
string(REGEX MATCH "PT_REGISTER_CTX_KERNEL\\([ \t\r\n]*[a-z_]*," first_registry "${kernel_impl}")
message(STATUS "first_registry in ${kernel_path}: ${first_registry}")
if (NOT first_registry STREQUAL "")
# parse the first kernel name
string(REPLACE "PT_REGISTER_CTX_KERNEL(" "" kernel_name "${first_registry}")
string(REPLACE "," "" kernel_name "${kernel_name}")
string(REGEX REPLACE "[ \t\r\n]+" "" kernel_name "${kernel_name}")
# append kernel declare into declarations.h
message(DEBUG "parse kernel name: ${kernel_name}")
message(STATUS "parse kernel name: ${kernel_name}")
# TODO(chenweihang): default declare ALL_LAYOUT for each kernel
if (${TARGET_PATH} MATCHES "*cpu*")
file(APPEND ${kernel_declare_file} "PT_DECLARE_KERNEL(${kernel_name}, CPU, ALL_LAYOUT)")
elseif (${TARGET_PATH} MATCHES "*cuda*")
file(APPEND ${kernel_declare_file} "PT_DECLARE_KERNEL(${kernel_name}, CUDA, ALL_LAYOUT)")
elseif (${TARGET_PATH} MATCHES "*xpu*")
file(APPEND ${kernel_declare_file} "PT_DECLARE_KERNEL(${kernel_name}, XPU, ALL_LAYOUT)")
elseif (${TARGET_PATH} MATCHES "*npu*")
file(APPEND ${kernel_declare_file} "PT_DECLARE_KERNEL(${kernel_name}, NPU, ALL_LAYOUT)")
if (${kernel_path} MATCHES "./cpu\/")
file(APPEND ${kernel_declare_file} "PT_DECLARE_KERNEL(${kernel_name}, CPU, ALL_LAYOUT);\n")
elseif (${kernel_path} MATCHES "./cuda\/")
file(APPEND ${kernel_declare_file} "PT_DECLARE_KERNEL(${kernel_name}, CUDA, ALL_LAYOUT);\n")
elseif (${kernel_path} MATCHES "./xpu\/")
file(APPEND ${kernel_declare_file} "PT_DECLARE_KERNEL(${kernel_name}, XPU, ALL_LAYOUT);\n")
elseif (${kernel_path} MATCHES "./npu\/*")
file(APPEND ${kernel_declare_file} "PT_DECLARE_KERNEL(${kernel_name}, NPU, ALL_LAYOUT);\n")
else ()
# deal with device independent kernel, now we use CPU temporaary
file(APPEND ${kernel_declare_file} "PT_DECLARE_KERNEL(${kernel_name}, CPU, ALL_LAYOUT)")
file(APPEND ${kernel_declare_file} "PT_DECLARE_KERNEL(${kernel_name}, CPU, ALL_LAYOUT);\n")
endif()
endif()
endif()
endforeach()
endfunction()

function(kernel_library TARGET)
Expand All @@ -61,30 +63,33 @@ function(kernel_library TARGET)
# TODO(chenweihang): parse compile deps by include headers
if (${kernel_library_SRCS_len} EQUAL 0)
if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${TARGET}.cc)
list(APPEND common_srcs ${TARGET}.cc)
list(APPEND common_srcs ${CMAKE_CURRENT_SOURCE_DIR}/${TARGET}.cc)
endif()
if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/cpu/${TARGET}.cc)
list(APPEND cpu_srcs cpu/${TARGET}.cc)
list(APPEND cpu_srcs ${CMAKE_CURRENT_SOURCE_DIR}/cpu/${TARGET}.cc)
endif()
if (WITH_GPU OR WITH_ROCM)
if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/cuda/${TARGET}.cu)
list(APPEND gpu_srcs cuda/${TARGET}.cu)
list(APPEND gpu_srcs ${CMAKE_CURRENT_SOURCE_DIR}/cuda/${TARGET}.cu)
endif()
endif()
if (WITH_XPU)
if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/xpu/${TARGET}.cc)
list(APPEND xpu_srcs xpu/${TARGET}.cc)
list(APPEND xpu_srcs ${CMAKE_CURRENT_SOURCE_DIR}/xpu/${TARGET}.cc)
endif()
endif()
if (WITH_ASCEND_CL)
if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/npu/${TARGET}.cc)
list(APPEND npu_srcs npu/${TARGET}.cc)
list(APPEND npu_srcs ${CMAKE_CURRENT_SOURCE_DIR}/npu/${TARGET}.cc)
endif()
endif()
else()
# TODO(chenweihang): impl compile by source later
endif()

message(STATUS "cpu_srcs: ${cpu_srcs}")
message(STATUS "gpu_srcs: ${gpu_srcs}")

list(LENGTH common_srcs common_srcs_len)
list(LENGTH cpu_srcs cpu_srcs_len)
list(LENGTH gpu_srcs gpu_srcs_len)
Expand All @@ -102,32 +107,32 @@ function(kernel_library TARGET)
else()
cc_library(${TARGET} SRCS ${common_srcs} DEPS ${kernel_library_DEPS})
endif()
kernel_declare(${TARGET}.cc)
kernel_declare(${common_srcs})
else()
# If the kernel has a header file declaration, but no corresponding
# implementation can be found, this is not allowed
if (${cpu_srcs} EQUAL 0 AND ${gpu_srcs} EQUAL 0 AND
${xpu_srcs} EQUAL 0 AND ${npu_srcs} EQUAL 0)
if (${cpu_srcs_len} EQUAL 0 AND ${gpu_srcs_len} EQUAL 0 AND
${xpu_srcs_len} EQUAL 0 AND ${npu_srcs_len} EQUAL 0)
message(FATAL_ERROR "Cannot find any implementation for ${TARGET}")
else()
if (WITH_GPU)
if (${cpu_srcs} GREATER 0 OR ${gpu_srcs} GREATER 0)
if (${cpu_srcs_len} GREATER 0 OR ${gpu_srcs_len} GREATER 0)
nv_library(${TARGET} SRCS ${cpu_srcs} ${gpu_srcs} DEPS ${kernel_library_DEPS})
kernel_declare(cpu/${TARGET}.cc)
kernel_declare(cuda/${TARGET}.cu)
kernel_declare(${cpu_srcs})
kernel_declare(${gpu_srcs})
endif()
elseif (WITH_ROCM)
if (${cpu_srcs} GREATER 0 OR ${gpu_srcs} GREATER 0)
if (${cpu_srcs_len} GREATER 0 OR ${gpu_srcs_len} GREATER 0)
hip_library(${TARGET} SRCS ${cpu_srcs} ${gpu_srcs} DEPS ${kernel_library_DEPS})
kernel_declare(cpu/${TARGET}.cc)
kernel_declare(cuda/${TARGET}.cu)
kernel_declare(${cpu_srcs})
kernel_declare(${gpu_srcs})
endif()
else()
if (${cpu_srcs} GREATER 0 OR ${xpu_srcs} GREATER 0 OR ${npu_srcs} GREATER 0)
if (${cpu_srcs_len} GREATER 0 OR ${xpu_srcs_len} GREATER 0 OR ${npu_srcs_len} GREATER 0)
cc_library(${TARGET} SRCS ${cpu_srcs} ${xpu_srcs} ${npu_srcs} DEPS ${kernel_library_DEPS})
kernel_declare(cpu/${TARGET}.cc)
kernel_declare(xpu/${TARGET}.cc)
kernel_declare(npu/${TARGET}.cc)
kernel_declare(${cpu_srcs})
kernel_declare(${xpu_srcs})
kernel_declare(${npu_srcs})
endif()
endif()
endif()
Expand All @@ -141,7 +146,8 @@ function(register_kernels)
cmake_parse_arguments(register_kernels "${options}" "${oneValueArgs}"
"{multiValueArgs}" ${ARGN})

file(GLOG KERNElS RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "*_kernel.h")
file(GLOB KERNELS RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "*_kernel.h")
message(STATUS "KERNELS: ${KERNELS}")
string(REPLACE ".h" "" KERNELS "${KERNELS}")
list(LENGTH register_kernels_DEPS register_kernels_DEPS_len)

Expand All @@ -153,6 +159,11 @@ function(register_kernels)
else()
kernel_library(${target})
endif()
# append target into PTEN_KERNELS property
get_property(pten_kernels GLOBAL PROPERTY PTEN_KERNELS)
set(pten_kernels ${pten_kernels} ${target})
message(STATUS "pten_kernels: ${pten_kernels}, target: ${target}")
set_property(GLOBAL PROPERTY PTEN_KERNELS ${pten_kernels})
endif()
endforeach()
endfunction()
endfunction()
6 changes: 4 additions & 2 deletions paddle/pten/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ add_subdirectory(tests)

# make an unity target for compile deps
set(PTEN_DEPS convert_utils dense_tensor pten_context kernel_factory kernel_context)
set(PTEN_DEPS ${PTEN_DEPS} math_cpu linalg_cpu manipulation_cpu conj_kernel_cpu scale_kernel_cpu full_kernel_cpu)
get_property(pten_kernels GLOBAL PROPERTY PTEN_KERNELS)
set(PTEN_DEPS ${PTEN_DEPS} ${pten_kernels})
set(PTEN_DEPS ${PTEN_DEPS} math_cpu linalg_cpu manipulation_cpu conj_kernel_cpu)
set(PTEN_DEPS ${PTEN_DEPS} nary unary binary)
if(WITH_GPU OR WITH_ROCM)
set(PTEN_DEPS ${PTEN_DEPS} math_cuda linalg_cuda manipulation_cuda conj_kernel_cuda scale_kernel_cuda full_kernel_cuda)
set(PTEN_DEPS ${PTEN_DEPS} math_cuda linalg_cuda manipulation_cuda conj_kernel_cuda)
endif()
if(WITH_XPU)
set(PTEN_DEPS ${PTEN_DEPS} manipulation_xpu)
Expand Down
4 changes: 0 additions & 4 deletions paddle/pten/api/lib/kernel_declare.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,15 @@ limitations under the License. */
// the kernel declare statement is automatically generated according to the
// file name of the kernel, and this header file will be removed

PT_DECLARE_KERNEL(full_like, CPU, ALL_LAYOUT);
PT_DECLARE_KERNEL(dot, CPU, ALL_LAYOUT);
PT_DECLARE_KERNEL(flatten, CPU, ALL_LAYOUT);
PT_DECLARE_KERNEL(sign, CPU, ALL_LAYOUT);
PT_DECLARE_KERNEL(scale, CPU, ALL_LAYOUT);
PT_DECLARE_KERNEL(conj, CPU, ALL_LAYOUT);

#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
PT_DECLARE_KERNEL(full_like, CUDA, ALL_LAYOUT);
PT_DECLARE_KERNEL(dot, CUDA, ALL_LAYOUT);
PT_DECLARE_KERNEL(flatten, CUDA, ALL_LAYOUT);
PT_DECLARE_KERNEL(sign, CUDA, ALL_LAYOUT);
PT_DECLARE_KERNEL(scale, CUDA, ALL_LAYOUT);
PT_DECLARE_KERNEL(conj, CUDA, ALL_LAYOUT);
#endif

Expand Down
17 changes: 16 additions & 1 deletion paddle/pten/kernels/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ include(pten_kernel)

set(kernel_declare_file ${PADDLE_BINARY_DIR}/paddle/pten/kernels/declarations.h.tmp CACHE INTERNAL "declarations.h file")
set(kernel_declare_file_final ${PADDLE_BINARY_DIR}/paddle/pten/kernels/declarations.h)
file(WRITE ${kernel_declare_file} "// Generated by the paddle/pten/kernels/CMakeLists.txt. DO NOT EDIT!\n\n")
file(WRITE ${kernel_declare_file} "// Generated by the paddle/pten/kernels/CMakeLists.txt. DO NOT EDIT!\n\n#pragma once\n\n")

# kernel primitive api
add_subdirectory(primitive)
Expand All @@ -24,3 +24,18 @@ endif()
if(WITH_XPU)
add_subdirectory(xpu)
endif()

# pten depends all pten kernel targets
set_property(GLOBAL PROPERTY PTEN_KERNELS "")

set(COMMON_KERNEL_DEPS dense_tensor)
set(COMMON_KERNEL_DEPS ${COMMON_KERNEL_DEPS} kernel_context kernel_factory)
set(COMMON_KERNEL_DEPS ${COMMON_KERNEL_DEPS} eigen_function)

# auto build kernel targets by cmake
register_kernels(DEPS ${COMMON_KERNEL_DEPS})

get_property(pten_kernels GLOBAL PROPERTY PTEN_KERNELS)
message(STATUS "PTEN_KERNELS: ${pten_kernels}")

copy_if_different(${kernel_declare_file} ${kernel_declare_file_final})
2 changes: 0 additions & 2 deletions paddle/pten/kernels/cpu/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@ cc_library(math_cpu SRCS math.cc DEPS dense_tensor kernel_context kernel_factory
cc_library(linalg_cpu SRCS linalg.cc DEPS dense_tensor kernel_context kernel_factory)
cc_library(utils_cpu SRCS utils.cc DEPS dense_tensor kernel_context kernel_factory memory convert_utils)
cc_library(manipulation_cpu SRCS manipulation.cc DEPS dense_tensor kernel_context kernel_factory utils_cpu unary)
cc_library(scale_kernel_cpu SRCS scale_kernel.cc DEPS dense_tensor kernel_context kernel_factory eigen_function)
cc_library(full_kernel_cpu SRCS full_kernel.cc DEPS dense_tensor kernel_context kernel_factory eigen_function)
cc_library(conj_kernel_cpu SRCS conj_kernel.cc DEPS dense_tensor kernel_context kernel_factory)
4 changes: 0 additions & 4 deletions paddle/pten/kernels/cuda/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@ if(WITH_GPU)
nv_library(linalg_cuda SRCS linalg.cu DEPS eigen_function dense_tensor kernel_context kernel_factory)
nv_library(utils_cuda SRCS utils.cu DEPS dense_tensor kernel_context kernel_factory memory convert_utils)
nv_library(manipulation_cuda SRCS manipulation.cu DEPS dense_tensor kernel_context kernel_factory utils_cuda unary)
nv_library(scale_kernel_cuda SRCS scale_kernel.cu DEPS dense_tensor kernel_context kernel_factory eigen_function)
nv_library(full_kernel_cuda SRCS full_kernel.cu DEPS dense_tensor kernel_context kernel_factory eigen_function)
nv_library(conj_kernel_cuda SRCS conj_kernel.cu DEPS dense_tensor kernel_context kernel_factory)
elseif(WITH_ROCM)
hip_library(math_cuda SRCS math.cu DEPS eigen_function dense_tensor convert_utils kernel_context kernel_factory pten_transpose_cuda)
hip_library(linalg_cuda SRCS linalg.cu DEPS eigen_function dense_tensor kernel_context kernel_factory)
hip_library(utils_cuda SRCS utils.cu DEPS dense_tensor kernel_context kernel_factory memory convert_utils)
hip_library(manipulation_cuda SRCS manipulation.cu DEPS dense_tensor kernel_context kernel_factory utils_cuda unary)
hip_library(scale_kernel_cuda SRCS scale_kernel.cu DEPS dense_tensor kernel_context kernel_factory eigen_function)
hip_library(full_kernel_cuda SRCS full_kernel.cu DEPS dense_tensor kernel_context kernel_factory eigen_function)
hip_library(conj_kernel_cuda SRCS conj_kernel.cu DEPS dense_tensor kernel_context kernel_factory)
endif()
1 change: 1 addition & 0 deletions python/paddle/utils/code_gen/api_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ def source_include(header_file_path):
#include "paddle/pten/core/kernel_registry.h"
#include "paddle/pten/include/core.h"
#include "paddle/pten/include/infermeta.h"
#include "paddle/pten/kernels/declarations.h"
"""


Expand Down