CMakeLists.txt 12.6 KB
Newer Older
L
Luo Tao 已提交
1 2
file(GLOB GENERAL_OPS RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "*_op.cc")
string(REPLACE ".cc" "" GENERAL_OPS "${GENERAL_OPS}")
M
mozga-intel 已提交
3 4 5 6 7 8 9 10 11
if(WITH_MKLDNN)
    string(REPLACE "_mkldnn" "" GENERAL_OPS "${GENERAL_OPS}")
else()
    foreach(item ${GENERAL_OPS})
        if(${item} MATCHES ".*_mkldnn_op")
            list(REMOVE_ITEM GENERAL_OPS ${item})
        endif()
    endforeach(item)
endif()
12
list(REMOVE_DUPLICATES GENERAL_OPS)
13
set(DEPS_OPS "")
14
set(pybind_file ${PADDLE_SOURCE_DIR}/paddle/fluid/pybind/pybind.h)
L
Luo Tao 已提交
15
file(WRITE ${pybind_file} "// Generated by the paddle/operator/CMakeLists.txt.  DO NOT EDIT!\n\n")
Y
Yu Yang 已提交
16 17 18 19
function(op_library TARGET)
    # op_library is a function to create op library. The interface is same as
    # cc_library. But it handle split GPU/CPU code and link some common library
    # for ops.
L
Luo Tao 已提交
20
    set(OP_LIBRARY ${TARGET} ${OP_LIBRARY} PARENT_SCOPE)
Y
Yu Yang 已提交
21 22
    set(cc_srcs)
    set(cu_srcs)
S
sabreshao 已提交
23 24
    set(hip_cu_srcs)
    set(miopen_hip_cc_srcs)
25
    set(cu_cc_srcs)
C
chengduoZH 已提交
26
    set(cudnn_cu_cc_srcs)
C
chengduoZH 已提交
27
    set(CUDNN_FILE)
28 29
    set(mkldnn_cc_srcs)
    set(MKLDNN_FILE)
Y
update  
Yancey1989 已提交
30
    set(op_common_deps operator op_registry math_function)
Y
Yu Yang 已提交
31 32 33
    set(options "")
    set(oneValueArgs "")
    set(multiValueArgs SRCS DEPS)
L
Luo Tao 已提交
34
    set(pybind_flag 0)
Y
Yu Yang 已提交
35 36 37
    cmake_parse_arguments(op_library "${options}" "${oneValueArgs}"
            "${multiValueArgs}" ${ARGN})

38 39 40 41
    list(LENGTH op_library_SRCS op_library_SRCS_len)
    if (${op_library_SRCS_len} EQUAL 0)
        if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${TARGET}.cc)
            list(APPEND cc_srcs ${TARGET}.cc)
Y
Yu Yang 已提交
42
        endif()
43 44 45
        if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${TARGET}.cu.cc)
            list(APPEND cu_cc_srcs ${TARGET}.cu.cc)
        endif()
46
        if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${TARGET}.cu)
47
            list(APPEND cu_srcs ${TARGET}.cu)
48
        endif()
S
sabreshao 已提交
49 50 51
        if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${TARGET}.hip.cu)
            list(APPEND hip_cu_srcs ${TARGET}.hip.cu)
        endif()
C
chengduoZH 已提交
52 53 54 55
        string(REPLACE "_op" "_cudnn_op" CUDNN_FILE "${TARGET}")
        if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${CUDNN_FILE}.cu.cc)
            list(APPEND cudnn_cu_cc_srcs ${CUDNN_FILE}.cu.cc)
        endif()
S
sabreshao 已提交
56 57 58 59 60 61
        if(WITH_AMD_GPU)
            string(REPLACE "_op" "_miopen_op" MIOPEN_FILE "${TARGET}")
            if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${MIOPEN_FILE}.hip.cc)
                list(APPEND miopen_hip_cc_srcs ${MIOPEN_FILE}.hip.cc)
            endif()
        endif()
62 63 64 65 66 67
        if(WITH_MKLDNN)
            string(REPLACE "_op" "_mkldnn_op" MKLDNN_FILE "${TARGET}")
            if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${MKLDNN_FILE}.cc)
                list(APPEND mkldnn_cc_srcs ${MKLDNN_FILE}.cc)
            endif()
        endif()
68 69
    else()
        foreach(src ${op_library_SRCS})
S
sabreshao 已提交
70 71 72
            if (${src} MATCHES ".*\\.hip.cu$")
                list(APPEND hip_cu_srcs ${src})
            elseif (${src} MATCHES ".*\\.cu$")
73
                list(APPEND cu_srcs ${src})
C
chengduoZH 已提交
74 75
            elseif(${src} MATCHES ".*_cudnn_op.cu.cc$")
                list(APPEND cudnn_cu_cc_srcs ${src})
S
sabreshao 已提交
76 77
            elseif(WITH_AMD_GPU AND ${src} MATCHES ".*_miopen_op.hip.cc$")
                list(APPEND miopen_hip_cc_srcs ${src})
78 79
            elseif(WITH_MKLDNN AND ${src} MATCHES ".*_mkldnn_op.cc$")
                list(APPEND mkldnn_cc_srcs ${src})
80 81
            elseif(${src} MATCHES ".*\\.cu.cc$")
                list(APPEND cu_cc_srcs ${src})
82 83 84 85 86 87 88
            elseif(${src} MATCHES ".*\\.cc$")
                list(APPEND cc_srcs ${src})
            else()
                message(FATAL_ERROR "${TARGET} Source file ${src} should only be .cc or .cu")
            endif()
        endforeach()
    endif()
Y
Yu Yang 已提交
89 90

    list(LENGTH cc_srcs cc_srcs_len)
M
mozga-intel 已提交
91 92 93 94 95 96
    if(WITH_MKLDNN)
        list(LENGTH mkldnn_cc_srcs mkldnn_cc_srcs_len)
        if (${cc_srcs_len} EQUAL 0 AND ${mkldnn_cc_srcs_len} EQUAL 0)
            message(FATAL_ERROR "The op library ${TARGET} should contains at least one .cc file")
        endif()
    elseif(${cc_srcs_len} EQUAL 0)
Y
Yu Yang 已提交
97 98 99
        message(FATAL_ERROR "The op library ${TARGET} should contains at least one .cc file")
    endif()

100 101 102 103
    list(LENGTH op_library_DEPS op_library_DEPS_len)
    if (${op_library_DEPS_len} GREATER 0)
        set(DEPS_OPS ${TARGET} ${DEPS_OPS} PARENT_SCOPE)
    endif()
Y
Yu Yang 已提交
104
    if (WITH_GPU)
105
        nv_library(${TARGET} SRCS ${cc_srcs} ${cu_cc_srcs} ${cudnn_cu_cc_srcs} ${mkldnn_cc_srcs} ${cu_srcs} DEPS ${op_library_DEPS}
Y
Yu Yang 已提交
106
                ${op_common_deps})
107
    elseif (WITH_AMD_GPU)
S
sabreshao 已提交
108 109
        hip_library(${TARGET} SRCS ${cc_srcs} ${hip_cu_srcs} ${miopen_hip_cc_srcs} ${mkldnn_cc_srcs} DEPS ${op_library_DEPS}
                ${op_common_deps})
Y
Yu Yang 已提交
110
    else()
111 112
        cc_library(${TARGET} SRCS ${cc_srcs} ${mkldnn_cc_srcs} DEPS ${op_library_DEPS}
            ${op_common_deps})
Y
Yu Yang 已提交
113
    endif()
L
Luo Tao 已提交
114

115
    # Define operators that don't need pybind here.
116
    foreach(manual_pybind_op "net_op" "compare_op" "logical_op" "nccl_op" "tensor_array_read_write_op")
117 118 119 120
        if ("${TARGET}" STREQUAL "${manual_pybind_op}")
            set(pybind_flag 1)
        endif()
    endforeach()
Q
qijun 已提交
121

S
sabreshao 已提交
122
    # The registration of USE_OP, please refer to paddle/fluid/framework/op_registry.h.
123 124
    # Note that it's enough to just adding one operator to pybind in a *_op.cc file.
    # And for detail pybind information, please see generated paddle/pybind/pybind.h.
M
mozga-intel 已提交
125 126 127 128 129 130 131 132 133 134
    # This replacing is needed, when the CPU's kernel doesn't exist.
    string(REPLACE "_op" "_mkldnn_op" target_mkldnn_file "${TARGET}")
    if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${TARGET}.cc)
        file(READ ${TARGET}.cc TARGET_CONTENT)
    elseif(WITH_MKLDNN AND EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${target_mkldnn_file}.cc)
        file(READ ${target_mkldnn_file}.cc TARGET_CONTENT)
    else() 
        message(FATAL_ERROR "Cannot read the ${TARGET} file from ${CMAKE_CURRENT_SOURCE_DIR}")
    endif()

135 136 137 138 139 140 141
    string(REGEX MATCH "REGISTER_OP\\(.*REGISTER_OP\\(" multi_register "${TARGET_CONTENT}")
    string(REGEX MATCH "REGISTER_OP\\([a-z0-9_]*," one_register "${multi_register}")
    if (one_register STREQUAL "")
        string(REPLACE "_op" "" TARGET "${TARGET}")
    else ()
        string(REPLACE "REGISTER_OP(" "" TARGET "${one_register}")
        string(REPLACE "," "" TARGET "${TARGET}")
Y
Yu Yang 已提交
142 143
    endif()

L
Luo Tao 已提交
144
    # pybind USE_NO_KERNEL_OP
T
typhoonzero 已提交
145
    # HACK: if REGISTER_OP_CPU_KERNEL presents the operator must have kernel
T
typhoonzero 已提交
146
    string(REGEX MATCH "REGISTER_OP_CPU_KERNEL" regex_result "${TARGET_CONTENT}")
L
Luo Tao 已提交
147 148 149 150 151 152 153 154
    string(REPLACE "_op" "" TARGET "${TARGET}")
    if (${pybind_flag} EQUAL 0 AND regex_result STREQUAL "")
        file(APPEND ${pybind_file} "USE_NO_KERNEL_OP(${TARGET});\n")
        set(pybind_flag 1)
    endif()

    # pybind USE_CPU_ONLY_OP
    list(LENGTH cu_srcs cu_srcs_len)
155
    list(LENGTH cu_cc_srcs cu_cc_srcs_len)
156
    list(LENGTH mkldnn_cc_srcs mkldnn_cc_srcs_len)
S
sabreshao 已提交
157 158 159 160
    list(LENGTH hip_cu_srcs hip_cu_srcs_len)
    list(LENGTH miopen_hip_cc_srcs miopen_hip_cc_srcs_len)
    if (${pybind_flag} EQUAL 0 AND ${mkldnn_cc_srcs_len} EQUAL 0 AND ${cu_srcs_len} EQUAL 0 AND ${cu_cc_srcs_len} EQUAL 0 AND
        ${hip_cu_srcs_len} EQUAL 0 AND ${miopen_hip_cc_srcs_len} EQUAL 0)
L
Luo Tao 已提交
161 162 163 164
        file(APPEND ${pybind_file} "USE_CPU_ONLY_OP(${TARGET});\n")
        set(pybind_flag 1)
    endif()

C
chengduoZH 已提交
165 166
    # pybind USE_OP_DEVICE_KERNEL for CUDNN
    list(LENGTH cudnn_cu_cc_srcs cudnn_cu_cc_srcs_len)
C
chengduoZH 已提交
167
    if (WITH_GPU AND ${cudnn_cu_cc_srcs_len} GREATER 0)
C
chengduoZH 已提交
168 169 170
        file(APPEND ${pybind_file} "USE_OP_DEVICE_KERNEL(${TARGET}, CUDNN);\n")
    endif()

S
sabreshao 已提交
171 172 173 174 175
    # pybind USE_OP_DEVICE_KERNEL for MIOPEN
    if (WITH_AMD_GPU AND ${miopen_hip_cc_srcs_len} GREATER 0)
        file(APPEND ${pybind_file} "USE_OP_DEVICE_KERNEL(${TARGET}, MIOPEN);\n")
    endif()

176 177
    # pybind USE_OP_DEVICE_KERNEL for MKLDNN
    if (WITH_MKLDNN AND ${mkldnn_cc_srcs_len} GREATER 0)
178 179 180 181
      # Append first implemented MKLDNN activation operator
      if (${MKLDNN_FILE} STREQUAL "activation_mkldnn_op")
        file(APPEND ${pybind_file} "USE_OP_DEVICE_KERNEL(relu, MKLDNN);\n")
      else()
182
        file(APPEND ${pybind_file} "USE_OP_DEVICE_KERNEL(${TARGET}, MKLDNN);\n")
183
      endif()
184 185
    endif()

L
Luo Tao 已提交
186 187 188 189
    # pybind USE_OP
    if (${pybind_flag} EQUAL 0)
        file(APPEND ${pybind_file} "USE_OP(${TARGET});\n")
    endif()
Y
Yu Yang 已提交
190 191
endfunction()

Q
qijun 已提交
192
add_subdirectory(math)
D
Dong Zhihong 已提交
193
add_subdirectory(nccl)
194

L
Luo Tao 已提交
195 196
if(WITH_GPU)
    op_library(nccl_op DEPS nccl_common)
197
    file(APPEND ${pybind_file} "USE_CUDA_ONLY_OP(ncclAllReduce);\n")
L
Luo Tao 已提交
198 199 200
else()
    set(DEPS_OPS ${DEPS_OPS} nccl_op)
endif()
Q
QI JUN 已提交
201

202
add_subdirectory(detail)
T
typhoonzero 已提交
203
if(WITH_DISTRIBUTE)
T
typhoonzero 已提交
204
    set(DISTRIBUTE_DEPS sendrecvop_grpc grpc++_unsecure grpc_unsecure gpr cares zlib protobuf)
205 206 207
    set(DISTRIBUTE_COMPILE_FLAGS "-Wno-non-virtual-dtor -Wno-error=non-virtual-dtor -Wno-error=delete-non-virtual-dtor")
    op_library(send_op DEPS ${DISTRIBUTE_DEPS})
    set_source_files_properties(send_op.cc PROPERTIES COMPILE_FLAGS ${DISTRIBUTE_COMPILE_FLAGS})
Q
Qiao Longfei 已提交
208 209
    op_library(prefetch_op DEPS ${DISTRIBUTE_DEPS})
    set_source_files_properties(prefetch_op.cc PROPERTIES COMPILE_FLAGS ${DISTRIBUTE_COMPILE_FLAGS})
210 211
    op_library(recv_op DEPS ${DISTRIBUTE_DEPS})
    set_source_files_properties(recv_op.cc PROPERTIES COMPILE_FLAGS ${DISTRIBUTE_COMPILE_FLAGS})
212 213
    op_library(listen_and_serv_op DEPS ${DISTRIBUTE_DEPS})
    set_source_files_properties(listen_and_serv_op.cc PROPERTIES COMPILE_FLAGS ${DISTRIBUTE_COMPILE_FLAGS})
214 215 216 217
    op_library(send_vars_op DEPS ${DISTRIBUTE_DEPS})
    set_source_files_properties(send_vars_op.cc PROPERTIES COMPILE_FLAGS ${DISTRIBUTE_COMPILE_FLAGS})
    op_library(send_barrier_op DEPS ${DISTRIBUTE_DEPS})
    set_source_files_properties(send_barrier_op.cc PROPERTIES COMPILE_FLAGS ${DISTRIBUTE_COMPILE_FLAGS})
Q
Qiao Longfei 已提交
218
    cc_test(test_send_recv SRCS send_recv_op_test.cc DEPS prefetch_op send_op listen_and_serv_op sum_op executor)
219
else()
Q
Qiao Longfei 已提交
220
    set(DEPS_OPS ${DEPS_OPS} send_op prefetch_op recv_op listen_and_serv_op send_vars_op send_barrier_op)
T
typhoonzero 已提交
221 222
endif()

223
op_library(cond_op DEPS framework_proto tensor net_op)
224 225
op_library(cross_entropy_op DEPS cross_entropy)
op_library(softmax_with_cross_entropy_op DEPS cross_entropy softmax)
226 227
op_library(softmax_op DEPS softmax)
op_library(sequence_softmax_op DEPS softmax)
Q
QI JUN 已提交
228 229
op_library(sum_op DEPS selected_rows_functor)
op_library(sgd_op DEPS selected_rows_functor)
Y
Yan Chunwei 已提交
230
op_library(print_op DEPS lod_tensor)
Q
QI JUN 已提交
231
op_library(adagrad_op DEPS selected_rows_functor)
W
wanghaox 已提交
232
op_library(maxout_op DEPS maxouting)
S
sweetsky0901 已提交
233
op_library(unpool_op DEPS unpooling)
234
op_library(pool_op DEPS pooling)
C
chengduoZH 已提交
235
op_library(pool_with_index_op DEPS pooling)
236 237 238 239
op_library(lod_rank_table_op DEPS lod_rank_table)
op_library(lod_tensor_to_array_op DEPS lod_rank_table_op)
op_library(array_to_lod_tensor_op DEPS lod_rank_table_op)
op_library(max_sequence_len_op DEPS lod_rank_table)
C
chengduoZH 已提交
240
op_library(sequence_conv_op DEPS context_project)
241
op_library(sequence_pool_op DEPS sequence_pooling)
D
dangqingqing 已提交
242
op_library(lstm_op DEPS sequence2batch lstm_compute)
243
op_library(lstmp_op DEPS sequence2batch lstm_compute)
G
guosheng 已提交
244
op_library(gru_op DEPS sequence2batch gru_compute)
245
op_library(recurrent_op DEPS executor)
C
chengduoZH 已提交
246
op_library(warpctc_op DEPS dynload_warpctc sequence_padding sequence_scale)
C
chengduoZH 已提交
247
op_library(cos_sim_op DEPS cos_sim_functor)
Y
Yang Yang 已提交
248
op_library(parallel_do_op DEPS executor)
249
if (WITH_GPU)
250
    op_library(conv_op DEPS vol2col depthwise_conv im2col)
251
else()
252
    op_library(conv_op DEPS vol2col im2col)
253
endif()
254
op_library(conv_transpose_op DEPS vol2col im2col)
255

武毅 已提交
256 257 258
# FIXME(typhoonzero): save/load depends lodtensor serialization functions
op_library(save_op DEPS lod_tensor)
op_library(load_op DEPS lod_tensor)
259 260
op_library(save_combine_op DEPS lod_tensor)
op_library(load_combine_op DEPS lod_tensor)
261
op_library(concat_op DEPS concat)
武毅 已提交
262

T
Thuan Nguyen 已提交
263 264 265 266 267
# FIXME(thuan): Move CSP operators to paddle/fluid/framework/operators/concurrency
add_subdirectory(concurrency)
op_library(channel_send_op DEPS concurrency)
op_library(channel_recv_op DEPS concurrency)

268
list(REMOVE_ITEM GENERAL_OPS ${DEPS_OPS})
L
Luo Tao 已提交
269
foreach(src ${GENERAL_OPS})
270
    op_library(${src})
L
Luo Tao 已提交
271
endforeach()
272
file(APPEND ${pybind_file} "USE_OP(less_than);\nUSE_OP(logical_and);\nUSE_NO_KERNEL_OP(read_from_array);\n")
273

Y
FIX CI  
Yu Yang 已提交
274 275 276 277
add_subdirectory(reader)
foreach(src ${READER_LIBRARY})
    set(OP_LIBRARY ${src} ${OP_LIBRARY})
endforeach()
278

L
Luo Tao 已提交
279 280
set(GLOB_OP_LIB ${OP_LIBRARY} CACHE INTERNAL "Global OP library")

281 282 283
cc_test(gather_test SRCS gather_test.cc DEPS tensor)
cc_test(net_op_test SRCS net_op_test.cc DEPS net_op)
cc_test(scatter_test SRCS scatter_test.cc DEPS tensor)
Q
Qiao Longfei 已提交
284
cc_test(beam_search_decode_op_test SRCS beam_search_decode_op_test.cc DEPS lod_tensor)
Y
Yan Chunwei 已提交
285
cc_test(beam_search_op_test SRCS beam_search_op_test.cc DEPS lod_tensor beam_search_op)
286
cc_test(strided_memcpy_test SRCS strided_memcpy_test.cc DEPS tensor paddle_memory)
Y
Yu Yang 已提交
287
cc_test(save_load_op_test SRCS save_load_op_test.cc DEPS save_op load_op)
288
cc_test(save_load_combine_op_test SRCS save_load_combine_op_test.cc DEPS save_combine_op load_combine_op)
Q
QI JUN 已提交
289
nv_test(nccl_op_test SRCS nccl_op_test.cu.cc DEPS nccl_op gpu_info device_context)
G
gongweibao 已提交
290
nv_test(dropout_op_test SRCS dropout_op_test.cc DEPS dropout_op tensor)