Skip to content

Commit 74dc997

Browse files
authored
Faster Transformer cmake promote (PaddlePaddle#879)
* update cmake * rm python deps when on_infer is no * refine encoder code
1 parent de4fc9d commit 74dc997

File tree

6 files changed

+124
-55
lines changed

6 files changed

+124
-55
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ __pycache__/
99
# Distribution / packaging
1010
.Python
1111
build/
12+
build*
1213
develop-eggs/
1314
dist/
1415
downloads/

paddlenlp/ops/CMakeLists.txt

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,16 @@ INCLUDE(ExternalProject)
2020

2121
set(CXX_STD "14" CACHE STRING "C++ standard")
2222

23-
option(ON_INFER "Compile with inference. " OFF)
24-
option(WITH_GPU "Compile with GPU/CPU, default use CPU." ON)
25-
option(USE_TENSORRT "Compile with TensorRT." OFF)
26-
option(WITH_TRANSFORMER "Compile with Transformer" ON)
27-
option(WITH_GPT "Compile with GPT" OFF)
28-
option(WITH_UNIFIED "Compile with Unified Transformer" ON)
29-
option(WITH_DECODER "Compile with Transformer Decoder" ON)
30-
option(WITH_ENCODER "Compile with Transformer Encoder" ON)
23+
option(ON_INFER "Compiled with inference. " OFF)
24+
option(WITH_GPU "Compiled with GPU/CPU, default use CPU." ON)
25+
option(WITH_MKL "Compile with MKL. Only works when ON_INFER is ON." ON)
26+
option(USE_TENSORRT "Compiled with TensorRT." OFF)
27+
option(WITH_TRANSFORMER "Compiled with Transformer." ON)
28+
option(WITH_GPT "Compiled with GPT." OFF)
29+
option(WITH_UNIFIED "Compiled with Unified Transformer." ON)
30+
option(WITH_SP "Compiled with sentencepiece. Only works when WITH_GPT and ON_INFER is ON." ON)
31+
option(WITH_DECODER "Compile with Transformer Decoder" ON)
32+
option(WITH_ENCODER "Compile with Transformer Encoder" ON)
3133

3234
if(NOT WITH_GPU)
3335
message(FATAL_ERROR "Faster transformer custom op doesn't support CPU. Please add the flag -DWITH_GPU=ON to use GPU. ")
@@ -259,7 +261,7 @@ ExternalProject_Add(
259261
PATCH_COMMAND ${FT_PATCH_COMMAND}
260262
BINARY_DIR ${THIRD_PATH}/build/${THIRD_PARTY_NAME}
261263
INSTALL_COMMAND ""
262-
CMAKE_ARGS -DCMAKE_BUILD_TYPE=Release -DSM=${SM} -DBUILD_PD=ON -DPY_CMD=${PY_CMD}
264+
CMAKE_ARGS -DCMAKE_BUILD_TYPE=Release -DSM=${SM} -DBUILD_PD=ON -DPY_CMD=${PY_CMD} -DON_INFER=${ON_INFER} -DPADDLE_LIB=${PADDLE_LIB} -DWITH_MKL=${WITH_MKL} -DWITH_WITH_STATIC_LIB=${WITH_STATIC_LIB}
263265
)
264266
ExternalProject_Get_property(extern_${THIRD_PARTY_NAME} BINARY_DIR)
265267
ExternalProject_Get_property(extern_${THIRD_PARTY_NAME} SOURCE_DIR)
@@ -276,7 +278,7 @@ link_directories(
276278
${FT_LIB_PATH}
277279
)
278280

279-
if(ON_INFER AND WITH_GPT)
281+
if(ON_INFER AND WITH_GPT AND WITH_SP)
280282
ExternalProject_Add(
281283
extern_sentencepiece
282284
GIT_REPOSITORY https://github.com/google/sentencepiece.git
@@ -293,6 +295,8 @@ if(ON_INFER AND WITH_GPT)
293295
link_directories(
294296
${THIRD_PATH}/build/sentencepiece/src/
295297
)
298+
299+
add_definitions(-DGPT_ON_SENTENCEPIECE)
296300
endif()
297301

298302
add_subdirectory(faster_transformer)

paddlenlp/ops/faster_transformer/src/CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,9 @@ if(ON_INFER)
145145
set(DEPS ${DEPS} shlwapi.lib)
146146
endif(NOT WIN32)
147147

148-
cuda_add_library(pd_infer_custom_op ${decoding_op_files} ${decoder_op_files} SHARED)
148+
cuda_add_library(decoding_infer_op ${decoding_op_files} ${decoder_op_files} SHARED)
149+
add_dependencies(decoding_infer_op extern_${THIRD_PARTY_NAME})
149150

150-
add_dependencies(pd_infer_custom_op extern_${THIRD_PARTY_NAME})
151151
string(REPLACE "/" ";" DEMO_PATH ${DEMO})
152152

153153
list(LENGTH DEMO_PATH PATH_LEN)
@@ -157,9 +157,9 @@ if(ON_INFER)
157157
string(REPLACE "." ";" DEMO_NAME ${DEMO_NAME})
158158
list(GET DEMO_NAME 0 DEMO_NAME)
159159
add_executable(${DEMO_NAME} ${DEMO})
160-
set(DEPS pd_infer_custom_op ${ft_lib_link} boost ${DEPS} cublas cudart)
160+
set(DEPS decoding_infer_op ${ft_lib_link} boost ${DEPS} cublas cudart)
161161

162-
if(WITH_GPT)
162+
if(WITH_GPT AND WITH_SP)
163163
set(DEPS ${DEPS} sentencepiece)
164164
endif()
165165

paddlenlp/ops/faster_transformer/src/demo/gpt.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include <pthread.h>
2-
#include <sentencepiece_processor.h>
32
#include <algorithm>
43
#include <atomic>
54
#include <codecvt>
@@ -12,6 +11,10 @@
1211
#include <thread>
1312
#include <unordered_map>
1413

14+
#ifdef GPT_ON_SENTENCEPIECE
15+
#include <sentencepiece_processor.h>
16+
#endif
17+
1518
#include "helper.h"
1619

1720
#include <sys/time.h>

paddlenlp/ops/faster_transformer/src/fusion_encoder_op.cc

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -143,20 +143,7 @@ std::vector<paddle::DataType> EncoderInferDtype(
143143
// const paddle::DataType& sequence_id_offset,
144144
// const paddle::DataType& trt_seqlen_offset,
145145
// const paddle::DataType& amax_list) {
146-
switch (input) {
147-
case paddle::DataType::FLOAT16: {
148-
return {input};
149-
}
150-
case paddle::DataType::FLOAT32: {
151-
return {input};
152-
}
153-
default: {
154-
PD_THROW(
155-
"NOT supported data type. "
156-
"Only float16 and float32 are supported. ");
157-
break;
158-
}
159-
}
146+
return {input};
160147
}
161148

162149
PD_BUILD_OP(fusion_encoder)

paddlenlp/ops/patches/FasterTransformer/CMakeLists.txt

Lines changed: 100 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -110,33 +110,107 @@ endif()
110110
if(BUILD_PD)
111111
add_definitions(-DPADDLE_WITH_CUDA)
112112

113-
execute_process(COMMAND ${PYTHON_PATH} "-c" "from __future__ import print_function; import paddle; print(paddle.sysconfig.get_include())"
114-
RESULT_VARIABLE _INC_PYTHON_SUCCESS
115-
OUTPUT_VARIABLE _INC_PYTHON_VALUES)
116-
if (NOT _INC_PYTHON_SUCCESS MATCHES 0)
117-
message(FATAL_ERROR "Python config Error.")
118-
endif()
119-
string(REGEX REPLACE ";" "\\\\;" _INC_PYTHON_VALUES ${_INC_PYTHON_VALUES})
120-
string(REGEX REPLACE "\n" ";" _INC_PYTHON_VALUES ${_INC_PYTHON_VALUES})
121-
list(GET _INC_PYTHON_VALUES 0 PY_INCLUDE_DIR)
122-
123-
list(APPEND COMMON_HEADER_DIRS ${PY_INCLUDE_DIR})
124-
list(APPEND COMMON_HEADER_DIRS ${PY_INCLUDE_DIR}/third_party)
125-
126-
execute_process(COMMAND ${PYTHON_PATH} "-c" "from __future__ import print_function; import paddle; print(paddle.sysconfig.get_lib())"
127-
RESULT_VARIABLE _LIB_PYTHON_SUCCESS
128-
OUTPUT_VARIABLE _LIB_PYTHON_VALUES)
129-
if (NOT _LIB_PYTHON_SUCCESS MATCHES 0)
130-
message(FATAL_ERROR "Python config Error.")
131-
endif()
132-
string(REGEX REPLACE ";" "\\\\;" _LIB_PYTHON_VALUES ${_LIB_PYTHON_VALUES})
133-
string(REGEX REPLACE "\n" ";" _LIB_PYTHON_VALUES ${_LIB_PYTHON_VALUES})
134-
list(GET _LIB_PYTHON_VALUES 0 PY_LIB_DIR)
135-
list(APPEND COMMON_LIB_DIRS ${PY_LIB_DIR})
136-
137-
include_directories(${PY_INCLUDE_DIR})
138-
include_directories(${PY_INCLUDE_DIR}\third_party)
113+
if(ON_INFER)
114+
add_definitions(-DPADDLE_ON_INFERENCE)
115+
116+
link_directories(${COMMON_LIB_DIRS})
117+
118+
if(NOT WITH_STATIC_LIB)
119+
add_definitions("-DPADDLE_WITH_SHARED_LIB")
120+
else()
121+
# PD_INFER_DECL is mainly used to set the dllimport/dllexport attribute in dynamic library mode.
122+
# Set it to empty in static library mode to avoid compilation issues.
123+
add_definitions("/DPD_INFER_DECL=")
124+
endif()
125+
126+
macro(safe_set_static_flag)
127+
foreach(flag_var
128+
CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
129+
CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
130+
if(${flag_var} MATCHES "/MD")
131+
string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
132+
endif(${flag_var} MATCHES "/MD")
133+
endforeach(flag_var)
134+
endmacro()
135+
136+
if(NOT DEFINED PADDLE_LIB)
137+
message(FATAL_ERROR "please set PADDLE_LIB with -DPADDLE_LIB=/path/paddle/lib")
138+
endif()
139+
140+
include_directories("${PADDLE_LIB}/")
141+
set(PADDLE_LIB_THIRD_PARTY_PATH "${PADDLE_LIB}/third_party/install/")
142+
include_directories("${PADDLE_LIB_THIRD_PARTY_PATH}protobuf/include")
143+
include_directories("${PADDLE_LIB_THIRD_PARTY_PATH}glog/include")
144+
include_directories("${PADDLE_LIB_THIRD_PARTY_PATH}gflags/include")
145+
include_directories("${PADDLE_LIB_THIRD_PARTY_PATH}xxhash/include")
146+
147+
link_directories("${PADDLE_LIB_THIRD_PARTY_PATH}protobuf/lib")
148+
link_directories("${PADDLE_LIB_THIRD_PARTY_PATH}glog/lib")
149+
link_directories("${PADDLE_LIB_THIRD_PARTY_PATH}gflags/lib")
150+
link_directories("${PADDLE_LIB_THIRD_PARTY_PATH}xxhash/lib")
151+
link_directories("${PADDLE_LIB}/paddle/lib")
152+
153+
if(WITH_MKL)
154+
set(FLAG_OPENMP "-fopenmp")
155+
endif()
156+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 ${FLAG_OPENMP}")
157+
158+
if (USE_TENSORRT AND WITH_GPU)
159+
set(TENSORRT_ROOT "" CACHE STRING "The root directory of TensorRT library")
160+
if("${TENSORRT_ROOT}" STREQUAL "")
161+
message(FATAL_ERROR "The TENSORRT_ROOT is empty, you must assign it a value with CMake command. Such as: -DTENSORRT_ROOT=TENSORRT_ROOT_PATH ")
162+
endif()
163+
set(TENSORRT_INCLUDE_DIR ${TENSORRT_ROOT}/include)
164+
set(TENSORRT_LIB_DIR ${TENSORRT_ROOT}/lib)
165+
endif()
166+
167+
if (USE_TENSORRT AND WITH_GPU)
168+
include_directories("${TENSORRT_INCLUDE_DIR}")
169+
link_directories("${TENSORRT_LIB_DIR}")
170+
endif()
171+
172+
if(WITH_MKL)
173+
set(MATH_LIB_PATH "${PADDLE_LIB_THIRD_PARTY_PATH}mklml")
174+
include_directories("${MATH_LIB_PATH}/include")
175+
set(MKLDNN_PATH "${PADDLE_LIB_THIRD_PARTY_PATH}mkldnn")
176+
if(EXISTS ${MKLDNN_PATH})
177+
include_directories("${MKLDNN_PATH}/include")
178+
set(MKLDNN_LIB ${MKLDNN_PATH}/lib/libmkldnn.so.0)
179+
endif()
180+
else()
181+
set(OPENBLAS_LIB_PATH "${PADDLE_LIB_THIRD_PARTY_PATH}openblas")
182+
include_directories("${OPENBLAS_LIB_PATH}/include/openblas")
183+
endif()
184+
185+
else()
186+
execute_process(COMMAND ${PYTHON_PATH} "-c" "from __future__ import print_function; import paddle; print(paddle.sysconfig.get_include())"
187+
RESULT_VARIABLE _INC_PYTHON_SUCCESS
188+
OUTPUT_VARIABLE _INC_PYTHON_VALUES)
189+
if (NOT _INC_PYTHON_SUCCESS MATCHES 0)
190+
message(FATAL_ERROR "Python config Error.")
191+
endif()
192+
string(REGEX REPLACE ";" "\\\\;" _INC_PYTHON_VALUES ${_INC_PYTHON_VALUES})
193+
string(REGEX REPLACE "\n" ";" _INC_PYTHON_VALUES ${_INC_PYTHON_VALUES})
194+
list(GET _INC_PYTHON_VALUES 0 PY_INCLUDE_DIR)
195+
196+
list(APPEND COMMON_HEADER_DIRS ${PY_INCLUDE_DIR})
197+
list(APPEND COMMON_HEADER_DIRS ${PY_INCLUDE_DIR}/third_party)
198+
199+
execute_process(COMMAND ${PYTHON_PATH} "-c" "from __future__ import print_function; import paddle; print(paddle.sysconfig.get_lib())"
200+
RESULT_VARIABLE _LIB_PYTHON_SUCCESS
201+
OUTPUT_VARIABLE _LIB_PYTHON_VALUES)
202+
if (NOT _LIB_PYTHON_SUCCESS MATCHES 0)
203+
message(FATAL_ERROR "Python config Error.")
204+
endif()
205+
string(REGEX REPLACE ";" "\\\\;" _LIB_PYTHON_VALUES ${_LIB_PYTHON_VALUES})
206+
string(REGEX REPLACE "\n" ";" _LIB_PYTHON_VALUES ${_LIB_PYTHON_VALUES})
207+
list(GET _LIB_PYTHON_VALUES 0 PY_LIB_DIR)
208+
list(APPEND COMMON_LIB_DIRS ${PY_LIB_DIR})
209+
210+
include_directories(${PY_INCLUDE_DIR})
211+
include_directories(${PY_INCLUDE_DIR}\third_party)
139212

213+
endif()
140214
endif()
141215

142216
include_directories(

0 commit comments

Comments
 (0)