Skip to content

Commit 3b04fd1

Browse files
authored
[GCU] Add gcu kernels (#1410)
1 parent aea74a9 commit 3b04fd1

File tree

241 files changed

+16626
-15710
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

241 files changed

+16626
-15710
lines changed

backends/gcu/CMakeLists.txt

Lines changed: 94 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,39 +17,56 @@ cmake_minimum_required(VERSION 3.10)
1717
project(paddle-custom-gcu CXX C)
1818
set(CUSTOM_GCU_NAME "paddle-custom-gcu")
1919

20-
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")
20+
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
2121
message(STATUS "CMAKE_DEBUG CMAKE_MODULE_PATH is: ${CMAKE_MODULE_PATH}")
22+
option(VERSION_WITH_GIT "Use git hash to PACKAGE_VERSION" FALSE)
2223

2324
set(TOPS_RELEASE_VERSION 3.1.0)
2425
if((NOT DEFINED PACKAGE_VERSION)
2526
OR ("${PACKAGE_VERSION}" STREQUAL "")
2627
OR ("${PACKAGE_VERSION}" STREQUAL "123.456"))
27-
# get Paddle-custom git hash
28-
execute_process(
29-
COMMAND git log -1 --abbrev=7 --format=%h
30-
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
31-
OUTPUT_VARIABLE GIT_HASH
32-
OUTPUT_STRIP_TRAILING_WHITESPACE)
33-
message(STATUS "git hash: ${GIT_HASH}")
34-
set(GIT_HASH ".${GIT_HASH}")
35-
else()
36-
set(GIT_HASH "")
37-
set(TOPS_RELEASE_VERSION ${PACKAGE_VERSION})
28+
if(VERSION_WITH_GIT)
29+
# get Paddle-custom git hash
30+
execute_process(
31+
COMMAND git log -1 --abbrev=7 --format=%h
32+
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
33+
OUTPUT_VARIABLE _tag
34+
OUTPUT_STRIP_TRAILING_WHITESPACE)
35+
message(STATUS "git hash: ${_tag}")
36+
else()
37+
string(TIMESTAMP _tag "%Y%m%d")
38+
endif()
39+
set(PACKAGE_VERSION ${TOPS_RELEASE_VERSION}.${_tag})
3840
endif()
39-
set(PACKAGE_VERSION ${GIT_HASH}-${TOPS_RELEASE_VERSION})
4041

41-
message(STATUS "tops release version: ${TOPS_RELEASE_VERSION}")
4242
message(STATUS "package version: ${PACKAGE_VERSION}")
4343

44+
if(DEFINED PY_VERSION)
45+
message(STATUS "User define PY_VERSION: ${PY_VERSION}")
46+
else()
47+
set(PY_VERSION "3.10")
48+
message(STATUS "Use default PY_VERSION: ${PY_VERSION}")
49+
endif()
50+
set(PYTHON_VERSION ${PY_VERSION})
51+
set(Python_EXECUTABLE "python${PY_VERSION}")
52+
message(STATUS "Python_EXECUTABLE: ${Python_EXECUTABLE}")
53+
54+
if(NOT DEFINED ENV{PADDLE_VERSION})
55+
set(ENV{PADDLE_VERSION} 3.0.0-beta1)
56+
endif()
57+
4458
include(paddle)
4559
include(version)
4660
include(generic)
4761
include(external/gcu)
62+
include(external/topscc)
4863

49-
include_directories(${CMAKE_SOURCE_DIR})
64+
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
5065
include_directories(/opt/tops/include)
5166

52-
set(OUTPUT_PADDLE_PACKAGE_VERSION ${PADDLE_VERSION}${PACKAGE_VERSION})
67+
set(OUTPUT_PADDLE_PACKAGE_VERSION ${PADDLE_VERSION}+${PACKAGE_VERSION})
68+
string(REPLACE "-beta" "b" OUTPUT_PADDLE_PACKAGE_VERSION
69+
"${OUTPUT_PADDLE_PACKAGE_VERSION}")
5370
option(WITH_KERNELS "compile with custom kernels" ON)
5471
option(WITH_TESTING "compile with unit testing" OFF)
5572
option(WITH_MKL "compile with mkl support" ON)
@@ -62,12 +79,6 @@ message(STATUS "C compiler: ${CMAKE_C_COMPILER}, version: "
6279
"${CMAKE_C_COMPILER_ID} ${CMAKE_C_COMPILER_VERSION}")
6380
message(STATUS "AR tools: ${CMAKE_AR}")
6481

65-
if(DEFINED PY_VERSION)
66-
message(STATUS "User define PY_VERSION: ${PY_VERSION}")
67-
set(Python_EXECUTABLE "python${PY_VERSION}")
68-
endif()
69-
message(STATUS "Python_EXECUTABLE: ${Python_EXECUTABLE}")
70-
7182
# custom runtime
7283
set(CUSTOM_GCU_SRCS runtime/runtime.cc)
7384
add_definitions(-DPADDLE_WITH_CUSTOM_DEVICE)
@@ -81,12 +92,12 @@ if(WITH_KERNELS)
8192
add_compile_definitions(HLIR_BUILDER_ABI_COMPATIABLE)
8293
file(
8394
GLOB_RECURSE CUSTOM_KERNEL_SRCS
84-
RELATIVE ${CMAKE_SOURCE_DIR}
95+
RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
8596
kernels/*.cc)
8697
list(APPEND CUSTOM_GCU_SRCS ${CUSTOM_KERNEL_SRCS})
8798
file(
8899
GLOB_RECURSE GCU_KERNEL_DEPENDS
89-
RELATIVE ${CMAKE_SOURCE_DIR}
100+
RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
90101
backend/*.cc common/*.cc)
91102
# build static library
92103
set(GCU_DEPENDS_NAME "gcu-kernels-depend")
@@ -99,7 +110,7 @@ if(WITH_KERNELS)
99110
# custom op with kernel
100111
file(
101112
GLOB_RECURSE CUSTOM_CUSTOM_OP_SRCS
102-
RELATIVE ${CMAKE_SOURCE_DIR}
113+
RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
103114
custom_op/*.cc)
104115
list(APPEND CUSTOM_GCU_SRCS ${CUSTOM_CUSTOM_OP_SRCS})
105116
endif()
@@ -108,22 +119,44 @@ if(ON_INFER)
108119
link_directories(${PADDLE_INFERENCE_LIB_DIR})
109120
endif()
110121

122+
# topscc kernels
123+
file(GLOB_RECURSE TOPSCC_KERNEL_SRCS
124+
${CMAKE_CURRENT_SOURCE_DIR}/topscc_custom_kernels/*.cc
125+
${CMAKE_CURRENT_SOURCE_DIR}/topscc_custom_kernels/*.cpp)
126+
list(LENGTH TOPSCC_KERNEL_SRCS TOPSCC_KERNEL_SRCS_LEN)
127+
unset(TOPSCC_LIBS)
128+
if(${TOPSCC_KERNEL_SRCS_LEN} GREATER 0)
129+
topscc_compile(FALSE ${TOPSCC_KERNEL_SRCS} "-shared -fPIC -O3 " TOPSCC_LIBS)
130+
message(STATUS "TOPSCC_LIBS: ${TOPSCC_LIBS}")
131+
endif()
132+
111133
# build shared library
112134
add_library(${CUSTOM_GCU_NAME} SHARED ${CUSTOM_GCU_SRCS})
135+
136+
# for link topscc custom kernels
137+
set_target_properties(
138+
${CUSTOM_GCU_NAME}
139+
PROPERTIES
140+
LINK_FLAGS
141+
"-Wl,-rpath,/usr/local/lib/python${PY_VERSION}/dist-packages/paddle_custom_device/gcu/"
142+
)
143+
113144
target_link_libraries(${CUSTOM_GCU_NAME} PRIVATE ${GCU_LIBS})
114145
if(WITH_KERNELS)
115146
target_link_libraries(${CUSTOM_GCU_NAME} PRIVATE ${GCU_DEPENDS_NAME})
116-
target_link_libraries(
117-
${CUSTOM_GCU_NAME}
118-
PRIVATE -Wl,--whole-archive ${TOPSOP_LIB_DIR}/libtopsop.a
119-
-Wl,--no-whole-archive)
120147
endif()
121148
if(TOPS_MODULE_SOURCE_DIR)
122149
target_include_directories(${CUSTOM_GCU_NAME}
123150
PRIVATE ${TOPS_MODULE_SOURCE_DIR}/usr/include)
124151
target_include_directories(${CUSTOM_GCU_NAME}
125152
PRIVATE ${TOPS_MODULE_SOURCE_DIR}/opt/tops/include)
126153
endif()
154+
# link topscc kernel libs
155+
add_custom_target(CUSTOM_TOPSCC_LIBS ALL DEPENDS ${TOPSCC_LIBS})
156+
if(${TOPSCC_KERNEL_SRCS_LEN} GREATER 0)
157+
add_dependencies(${CUSTOM_GCU_NAME} CUSTOM_TOPSCC_LIBS)
158+
target_link_libraries(${CUSTOM_GCU_NAME} PRIVATE ${TOPSCC_LIBS})
159+
endif()
127160

128161
# link paddle shared library
129162
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/third_party.cmake)
@@ -142,11 +175,12 @@ else()
142175
endif()
143176

144177
# export paddle_gcu interface with version map
145-
set_property(TARGET ${CUSTOM_GCU_NAME}
146-
PROPERTY LINK_DEPENDS ${CMAKE_SOURCE_DIR}/paddle_gcu_export.map)
178+
set_property(
179+
TARGET ${CUSTOM_GCU_NAME}
180+
PROPERTY LINK_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/paddle_gcu_export.map)
147181
target_link_options(
148182
${CUSTOM_GCU_NAME} PRIVATE
149-
-Wl,--version-script=${CMAKE_SOURCE_DIR}/paddle_gcu_export.map)
183+
-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/paddle_gcu_export.map)
150184

151185
# testing
152186
if(WITH_TESTING)
@@ -155,7 +189,7 @@ if(WITH_TESTING)
155189
add_subdirectory(tests)
156190
add_custom_command(
157191
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/tests/.timestamp
158-
COMMAND cp -r ${CMAKE_SOURCE_DIR}/tests ${CMAKE_CURRENT_BINARY_DIR})
192+
COMMAND cp -r ${CMAKE_CURRENT_SOURCE_DIR}/tests ${CMAKE_CURRENT_BINARY_DIR})
159193
add_custom_target(python_tests ALL
160194
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/tests/.timestamp)
161195
endif()
@@ -194,13 +228,35 @@ add_custom_target(
194228
${CMAKE_CURRENT_BINARY_DIR}/python/paddle_custom_device/lib${CUSTOM_GCU_NAME}.so
195229
)
196230

231+
set(topscc_kernel_lib_targets "")
232+
unset(topscc_kernel_lib_targets)
233+
foreach(topscc_kernel_lib ${TOPSCC_LIBS})
234+
get_filename_component(topscc_kernel_lib_name ${topscc_kernel_lib} NAME_WLE)
235+
get_filename_component(topscc_kernel_lib_target_name ${topscc_kernel_lib}
236+
NAME_WE)
237+
message(STATUS "topscc_kernel_lib_name: ${topscc_kernel_lib_name}")
238+
add_custom_command(
239+
OUTPUT
240+
${CMAKE_CURRENT_BINARY_DIR}/python/paddle_custom_device/gcu/${topscc_kernel_lib_name}
241+
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${topscc_kernel_lib}
242+
${CMAKE_CURRENT_BINARY_DIR}/python/paddle_custom_device/gcu
243+
USES_TERMINAL
244+
DEPENDS ${topscc_kernel_lib})
245+
list(
246+
APPEND
247+
topscc_kernel_lib_targets
248+
${CMAKE_CURRENT_BINARY_DIR}/python/paddle_custom_device/gcu/${topscc_kernel_lib_name}
249+
)
250+
endforeach()
251+
add_custom_target(topscc_targets ALL DEPENDS ${topscc_kernel_lib_targets})
252+
197253
set(whl_file
198-
"${CMAKE_BINARY_DIR}/dist/paddle_custom_gcu-${OUTPUT_PADDLE_PACKAGE_VERSION}-cp${PY_VERSION_NO_DOT}-cp${PY_VERSION_NO_DOT}-linux_x86_64.whl"
254+
"${CMAKE_CURRENT_BINARY_DIR}/dist/paddle_custom_gcu-${OUTPUT_PADDLE_PACKAGE_VERSION}-cp${PY_VERSION_NO_DOT}-cp${PY_VERSION_NO_DOT}-linux_x86_64.whl"
199255
)
200256
set(_passes_target_dir
201257
"${CMAKE_CURRENT_BINARY_DIR}/python/paddle_custom_device/gcu/passes")
202258
file(MAKE_DIRECTORY ${_passes_target_dir})
203-
file(GLOB passes_srcs "${CMAKE_SOURCE_DIR}/passes/*")
259+
file(GLOB passes_srcs "${CMAKE_CURRENT_SOURCE_DIR}/passes/*")
204260
foreach(passes_src IN LISTS passes_srcs)
205261
get_filename_component(passes_file_name ${passes_src} NAME)
206262
add_custom_command(
@@ -212,11 +268,11 @@ foreach(passes_src IN LISTS passes_srcs)
212268
endforeach()
213269
add_custom_command(
214270
OUTPUT ${whl_file}
215-
COMMAND ${Python_EXECUTABLE} ${CMAKE_BINARY_DIR}/setup.py bdist_wheel
271+
COMMAND ${Python_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/setup.py bdist_wheel
216272
# COMMAND rename 's/_${TOPS_RELEASE_VERSION}/-${TOPS_RELEASE_VERSION}/g'
217273
# ${CMAKE_CURRENT_BINARY_DIR}/dist/*.whl
218-
DEPENDS ${CUSTOM_GCU_NAME} CUSTOM_GCU_NAME_shared_lib_copy
219-
${CMAKE_BINARY_DIR}/setup.py ${passes_bin_files_list}
274+
DEPENDS ${CUSTOM_GCU_NAME} CUSTOM_GCU_NAME_shared_lib_copy topscc_targets
275+
${CMAKE_CURRENT_BINARY_DIR}/setup.py ${passes_bin_files_list}
220276
COMMENT "Packing whl packages------>>>")
221277

222278
add_custom_target(python_package ALL DEPENDS ${whl_file})

backends/gcu/backend/executor/cast_runner.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@
1313
// limitations under the License.
1414

1515
#pragma once
16+
#include <tops/tops_ext.h>
17+
1618
#include <vector>
1719

1820
#include "backend/executor/gcu_node.h"
1921
#include "backend/utils/utils.h"
20-
#include "tops/tops_ext.h"
2122

2223
namespace backend {
2324

backends/gcu/cmake/external/gcu.cmake

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,14 @@ set(TOPS_INCLUDE_DIR "/usr/include/tops")
1717
set(TOPSTX_INCLUDE_DIR "/usr/include/topstx")
1818
set(GCU_LIB_DIR "/usr/lib")
1919
set(TOPS_LIB_DIR "/opt/tops/lib")
20-
set(OP_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/kernels/topsflame/include/")
21-
set(TOPSOP_LIB_DIR "${CMAKE_CURRENT_SOURCE_DIR}/kernels/topsflame/")
2220

2321
set(TOPS_VERSION "???")
2422

2523
include_directories(${GCU_INCLUDE_DIR} ${TOPS_INCLUDE_DIR}
26-
${TOPSTX_INCLUDE_DIR} ${OP_INCLUDE_DIR})
24+
${TOPSTX_INCLUDE_DIR})
2725

2826
find_library(SDK_LIB NAMES dtu_sdk ${GCU_LIB_DIR})
2927
find_library(ECCL_LIB NAMES eccl ${GCU_LIB_DIR})
30-
find_library(
31-
RUNTIME_LIB
32-
NAMES efrt ${GCU_LIB_DIR}
33-
HINTS ${TOPS_LIB_DIR})
3428
find_library(
3529
TOPS_RT_LIB
3630
NAMES topsrt ${GCU_LIB_DIR}
@@ -40,9 +34,17 @@ find_library(
4034
NAMES topstx ${GCU_LIB_DIR}
4135
HINTS ${TOPS_LIB_DIR})
4236
find_library(TOPSATENOP_LIB NAMES topsaten ${GCU_LIB_DIR})
37+
find_library(TOPSCL_LIB NAMES topscl ${GCU_LIB_DIR})
4338

44-
set(GCU_LIBS ${SDK_LIB} ${ECCL_LIB} ${RUNTIME_LIB} ${TOPS_RT_LIB} ${TOPSTX_LIB}
45-
${TOPSATENOP_LIB})
39+
set(GCU_LIBS
40+
-Wl,--no-as-needed
41+
${SDK_LIB}
42+
${ECCL_LIB}
43+
${TOPS_RT_LIB}
44+
${TOPSTX_LIB}
45+
${TOPSATENOP_LIB}
46+
${TOPSCL_LIB}
47+
-Wl,--as-needed)
4648

4749
set(VERSION_REGEX "( [0-9]+\.[0-9]+\.(RC)?[0-9]*) ")
4850
macro(find_gcu_version component)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4+
# use this file except in compliance with the License. You may obtain a copy of
5+
# the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
# License for the specific language governing permissions and limitations under
13+
# the License.
14+
15+
if(NOT EXISTS /usr/bin/topscc)
16+
message(FATAL_ERROR "TOPSCC not found")
17+
else()
18+
set(TOPSCC /usr/bin/topscc)
19+
endif()
20+
21+
function(topscc_compile is_static_lib topscc_files compile_options out_libs)
22+
message(STATUS "== Start compiling topscc kernels ==")
23+
message(STATUS "[KERNEL FILES]: ${topscc_files}")
24+
message(STATUS "[COMPILE OPTIONS]: ${compile_options}")
25+
message(STATUS "[IS STATIC LIB]: ${is_static_lib}")
26+
27+
set(outs)
28+
# string(CONCAT compile_options ${compile_options} "-ltops")
29+
foreach(file ${topscc_files})
30+
message(STATUS "[TOPSCC FILE]: ${file}")
31+
get_filename_component(file_name ${file} NAME_WE)
32+
message(STATUS "[TOPSCC Name]: ${file_name}")
33+
34+
if(is_static_lib)
35+
set(out ${CMAKE_CURRENT_BINARY_DIR}/lib${file_name}.a)
36+
else()
37+
set(out ${CMAKE_CURRENT_BINARY_DIR}/lib${file_name}.so)
38+
endif()
39+
add_custom_command(
40+
OUTPUT ${out}
41+
COMMAND ${TOPSCC} ${file} ${compile_options} -o ${out} -ltops
42+
DEPENDS ${file}
43+
COMMENT "Compiling ${file} to ${out_file}"
44+
VERBATIM)
45+
list(APPEND outs ${out})
46+
endforeach()
47+
message(STATUS "== Finish compiling topscc kernels ==")
48+
set(${out_libs}
49+
${outs}
50+
PARENT_SCOPE)
51+
endfunction()

backends/gcu/common/gcu_env_list.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ namespace env {
2121
const char *const kUseJitKernels = "PADDLE_GCU_USE_JIT_KERNELS_ONLY";
2222
const char *const kProfiler = "PADDLE_GCU_PROFILE";
2323
const char *const kStreamAsync = "PADDLE_RUN_ASYNC";
24+
const char *const kEnableTransOpt = "PADDLE_ENABLE_TRANSPOSE_OPT";
2425
} // namespace env

backends/gcu/common/utils.cc

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,36 @@ bool LaunchAOTKernel(const phi::DataType& dtype,
142142
}
143143
return (supported_types.count(dtype) > 0);
144144
}
145+
146+
#define FOR_EACH_DATA_TYPE_TO_PRINT(_) \
147+
_(bool, phi::DataType::BOOL) \
148+
_(int8_t, phi::DataType::INT8) \
149+
_(uint8_t, phi::DataType::UINT8) \
150+
_(int16_t, phi::DataType::INT16) \
151+
_(uint16_t, phi::DataType::UINT16) \
152+
_(int32_t, phi::DataType::INT32) \
153+
_(uint32_t, phi::DataType::UINT32) \
154+
_(int64_t, phi::DataType::INT64) \
155+
_(uint64_t, phi::DataType::UINT64) \
156+
_(phi::bfloat16, phi::DataType::BFLOAT16) \
157+
_(phi::float16, phi::DataType::FLOAT16) \
158+
_(float, phi::DataType::FLOAT32) \
159+
_(double, phi::DataType::FLOAT64)
160+
161+
#define CALL_PRINT_TENSOR(cpp_type, data_type) \
162+
case data_type: \
163+
PrintTensor<cpp_type>(os, t); \
164+
break;
165+
166+
std::ostream& operator<<(std::ostream& os, const phi::DenseTensor& t) {
167+
switch (t.dtype()) {
168+
FOR_EACH_DATA_TYPE_TO_PRINT(CALL_PRINT_TENSOR)
169+
default:
170+
VLOG(1) << "PrintTensor unrecognized data type:" << t.dtype();
171+
}
172+
return os;
173+
}
174+
#undef FOR_EACH_DATA_TYPE_TO_PRINT
175+
#undef CALL_PRINT_TENSOR
176+
145177
} // namespace custom_kernel

0 commit comments

Comments
 (0)