-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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
[PTen] Add cmake function for kernels #38311
Merged
chenwhql
merged 10 commits into
PaddlePaddle:develop
from
chenwhql:pten/add_cmake_function
Dec 22, 2021
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
03de6c5
add pten kernel cmake
chenwhql 9077cd2
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
chenwhql e866565
add pten kernel cmake function
chenwhql 117d5bb
fix compile error
chenwhql a73d6b9
add enforce include for full kernel
chenwhql 2268233
fix compile failed
chenwhql 8c61fa8
resolve conflit
chenwhql de27843
change cuda to gpu
chenwhql c5cd43b
fix cmake function error
chenwhql ed172f5
resolve conflit
chenwhql File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
# call kernel_declare need to make sure the target of input is exists | ||
function(kernel_declare TARGET_LIST) | ||
foreach(kernel_path ${TARGET_LIST}) | ||
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 "${kernel_impl}") | ||
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 | ||
# TODO(chenweihang): default declare ALL_LAYOUT for each kernel | ||
if (${kernel_path} MATCHES "./cpu\/") | ||
file(APPEND ${kernel_declare_file} "PT_DECLARE_KERNEL(${kernel_name}, CPU, ALL_LAYOUT);\n") | ||
elseif (${kernel_path} MATCHES "./gpu\/") | ||
file(APPEND ${kernel_declare_file} "PT_DECLARE_KERNEL(${kernel_name}, GPU, 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);\n") | ||
endif() | ||
endif() | ||
endforeach() | ||
endfunction() | ||
|
||
function(kernel_library TARGET) | ||
set(common_srcs) | ||
set(cpu_srcs) | ||
set(gpu_srcs) | ||
set(xpu_srcs) | ||
set(npu_srcs) | ||
|
||
set(oneValueArgs "") | ||
set(multiValueArgs SRCS DEPS) | ||
cmake_parse_arguments(kernel_library "${options}" "${oneValueArgs}" | ||
"${multiValueArgs}" ${ARGN}) | ||
|
||
list(LENGTH kernel_library_SRCS kernel_library_SRCS_len) | ||
# one kernel only match one impl file in each backend | ||
# 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 ${CMAKE_CURRENT_SOURCE_DIR}/${TARGET}.cc) | ||
endif() | ||
if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/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}/gpu/${TARGET}.cu) | ||
list(APPEND gpu_srcs ${CMAKE_CURRENT_SOURCE_DIR}/gpu/${TARGET}.cu) | ||
endif() | ||
endif() | ||
if (WITH_XPU) | ||
if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/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 ${CMAKE_CURRENT_SOURCE_DIR}/npu/${TARGET}.cc) | ||
endif() | ||
endif() | ||
else() | ||
# TODO(chenweihang): impl compile by source later | ||
endif() | ||
|
||
list(LENGTH common_srcs common_srcs_len) | ||
list(LENGTH cpu_srcs cpu_srcs_len) | ||
list(LENGTH gpu_srcs gpu_srcs_len) | ||
list(LENGTH xpu_srcs xpu_srcs_len) | ||
list(LENGTH npu_srcs npu_srcs_len) | ||
|
||
if (${common_srcs_len} GREATER 0) | ||
# If the kernel has a device independent public implementation, | ||
# we will use this implementation and will not adopt the implementation | ||
# under specific devices | ||
if (WITH_GPU) | ||
nv_library(${TARGET} SRCS ${common_srcs} DEPS ${kernel_library_DEPS}) | ||
elseif (WITH_ROCM) | ||
hip_library(${TARGET} SRCS ${common_srcs} DEPS ${kernel_library_DEPS}) | ||
else() | ||
cc_library(${TARGET} SRCS ${common_srcs} DEPS ${kernel_library_DEPS}) | ||
endif() | ||
else() | ||
# If the kernel has a header file declaration, but no corresponding | ||
# implementation can be found, this is not allowed | ||
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_len} GREATER 0 OR ${gpu_srcs_len} GREATER 0) | ||
nv_library(${TARGET} SRCS ${cpu_srcs} ${gpu_srcs} DEPS ${kernel_library_DEPS}) | ||
endif() | ||
elseif (WITH_ROCM) | ||
if (${cpu_srcs_len} GREATER 0 OR ${gpu_srcs_len} GREATER 0) | ||
hip_library(${TARGET} SRCS ${cpu_srcs} ${gpu_srcs} DEPS ${kernel_library_DEPS}) | ||
endif() | ||
else() | ||
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}) | ||
endif() | ||
endif() | ||
endif() | ||
endif() | ||
|
||
if (${common_srcs_len} GREATER 0 OR ${cpu_srcs_len} GREATER 0 OR ${gpu_srcs_len} GREATER 0 OR | ||
${xpu_srcs_len} GREATER 0 OR ${npu_srcs_len} GREATER 0) | ||
# append target into PTEN_KERNELS property | ||
get_property(pten_kernels GLOBAL PROPERTY PTEN_KERNELS) | ||
set(pten_kernels ${pten_kernels} ${TARGET}) | ||
set_property(GLOBAL PROPERTY PTEN_KERNELS ${pten_kernels}) | ||
endif() | ||
|
||
# parse kernel name and auto generate kernel declaration | ||
# here, we don't need to check WITH_XXX, because if not WITH_XXX, the | ||
# xxx_srcs_len will be equal to 0 | ||
if (${common_srcs_len} GREATER 0) | ||
kernel_declare(${common_srcs}) | ||
endif() | ||
if (${cpu_srcs_len} GREATER 0) | ||
kernel_declare(${cpu_srcs}) | ||
endif() | ||
if (${gpu_srcs_len} GREATER 0) | ||
kernel_declare(${gpu_srcs}) | ||
endif() | ||
if (${xpu_srcs_len} GREATER 0) | ||
kernel_declare(${xpu_srcs}) | ||
endif() | ||
if (${npu_srcs_len} GREATER 0) | ||
kernel_declare(${npu_srcs}) | ||
endif() | ||
endfunction() | ||
|
||
function(register_kernels) | ||
set(options "") | ||
set(oneValueArgs "") | ||
set(multiValueArgs EXCLUDES DEPS) | ||
cmake_parse_arguments(register_kernels "${options}" "${oneValueArgs}" | ||
"${multiValueArgs}" ${ARGN}) | ||
|
||
file(GLOB KERNELS RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "*_kernel.h") | ||
string(REPLACE ".h" "" KERNELS "${KERNELS}") | ||
list(LENGTH register_kernels_DEPS register_kernels_DEPS_len) | ||
|
||
foreach(target ${KERNELS}) | ||
list(FIND register_kernels_EXCLUDES ${target} _index) | ||
if (${_index} EQUAL -1) | ||
if (${register_kernels_DEPS_len} GREATER 0) | ||
kernel_library(${target} DEPS ${register_kernels_DEPS}) | ||
else() | ||
kernel_library(${target}) | ||
endif() | ||
endif() | ||
endforeach() | ||
endfunction() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
后续pr 中可以删除注释中的 is
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, thx