CMakeLists.txt 9.9 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}")
L
Luo Tao 已提交
3 4
set(pybind_file ${PADDLE_SOURCE_DIR}/paddle/pybind/pybind.h)
file(WRITE ${pybind_file} "// Generated by the paddle/operator/CMakeLists.txt.  DO NOT EDIT!\n\n")
Y
Yu Yang 已提交
5 6 7 8
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 已提交
9
    set(OP_LIBRARY ${TARGET} ${OP_LIBRARY} PARENT_SCOPE)
Y
Yu Yang 已提交
10 11
    set(cc_srcs)
    set(cu_srcs)
12
    set(cu_cc_srcs)
Y
update  
Yancey1989 已提交
13
    set(op_common_deps operator op_registry math_function)
Y
Yu Yang 已提交
14 15 16
    set(options "")
    set(oneValueArgs "")
    set(multiValueArgs SRCS DEPS)
L
Luo Tao 已提交
17
    set(pybind_flag 0)
Y
Yu Yang 已提交
18 19 20
    cmake_parse_arguments(op_library "${options}" "${oneValueArgs}"
            "${multiValueArgs}" ${ARGN})

21 22 23 24
    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 已提交
25
        endif()
26 27 28
        if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${TARGET}.cu.cc)
            list(APPEND cu_cc_srcs ${TARGET}.cu.cc)
        endif()
29
        if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${TARGET}.cu)
30
            list(APPEND cu_srcs ${TARGET}.cu)
31 32 33 34 35
        endif()
    else()
        foreach(src ${op_library_SRCS})
            if (${src} MATCHES ".*\\.cu$")
                list(APPEND cu_srcs ${src})
36 37
            elseif(${src} MATCHES ".*\\.cu.cc$")
                list(APPEND cu_cc_srcs ${src})
38 39 40 41 42 43 44
            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 已提交
45 46 47 48 49 50 51

    list(LENGTH cc_srcs cc_srcs_len)
    if (${cc_srcs_len} EQUAL 0)
        message(FATAL_ERROR "The op library ${TARGET} should contains at least one .cc file")
    endif()

    if (WITH_GPU)
52
        nv_library(${TARGET} SRCS ${cc_srcs} ${cu_cc_srcs} ${cu_srcs} DEPS ${op_library_DEPS}
Y
Yu Yang 已提交
53 54 55 56 57
                ${op_common_deps})
    else()
        cc_library(${TARGET} SRCS ${cc_srcs} DEPS ${op_library_DEPS}
                ${op_common_deps})
    endif()
L
Luo Tao 已提交
58 59 60

    # net_op doesn't need pybind
    if ("${TARGET}" STREQUAL "net_op")
L
Luo Tao 已提交
61
        set(pybind_flag 1)
L
Luo Tao 已提交
62 63
    endif()

C
chengduoZH 已提交
64 65 66 67 68 69 70 71 72 73 74 75
    if ("${TARGET}" STREQUAL "compare_op")
        set(pybind_flag 1)
        file(APPEND ${pybind_file} "USE_OP(less_than);\nUSE_OP(equal);\n")
    endif()

    # conv_op contains several operators
    if ("${TARGET}" STREQUAL "conv_op")
        set(pybind_flag 1)
        # It's enough to just adding one operator to pybind
        file(APPEND ${pybind_file} "USE_OP(conv2d);\n")
    endif()

武毅 已提交
76 77 78 79 80 81 82
    # conv_cudnn_op contains several operators
    if ("${TARGET}" STREQUAL "conv_cudnn_op")
        set(pybind_flag 1)
        # It's enough to just adding one operator to pybind
        file(APPEND ${pybind_file} "USE_OP(conv2d_cudnn);\n")
    endif()

C
chengduoZH 已提交
83
    # pool_op contains several operators
84 85 86 87 88 89
    if ("${TARGET}" STREQUAL "pool_op")
        set(pybind_flag 1)
        # It's enough to just adding one operator to pybind
        file(APPEND ${pybind_file} "USE_OP(pool2d);\n")
    endif()

C
chengduoZH 已提交
90 91
    # pool_cudnn_op contains several operators
    if ("${TARGET}" STREQUAL "pool_cudnn_op")
Y
Yu Yang 已提交
92
        set(pybind_flag 1)
C
chengduoZH 已提交
93 94
        # It's enough to just adding one operator to pybind
        file(APPEND ${pybind_file} "USE_OP(pool2d_cudnn);\n")
Y
Yu Yang 已提交
95 96
    endif()

97
    if ("${TARGET}" STREQUAL "logical_op")
C
chengduoZH 已提交
98
        set(pybind_flag 1)
99
        file(APPEND ${pybind_file} "USE_OP(logical_and);\n")
C
chengduoZH 已提交
100 101 102 103 104 105 106 107 108
    endif()

    # pool_with_index_op contains several operators
    if ("${TARGET}" STREQUAL "pool_with_index_op")
        set(pybind_flag 1)
        # It's enough to just adding one operator to pybind
        file(APPEND ${pybind_file} "USE_OP(max_pool2d_with_index);\n")
    endif()

C
chengduoZH 已提交
109 110
    # conv_transpose_op contains several operators
    if ("${TARGET}" STREQUAL "conv_transpose_op")
C
chengduoZH 已提交
111 112
        set(pybind_flag 1)
        # It's enough to just adding one operator to pybind
C
chengduoZH 已提交
113
        file(APPEND ${pybind_file} "USE_OP(conv2d_transpose);\n")
C
chengduoZH 已提交
114
    endif()
C
chengduoZH 已提交
115 116 117

    # conv_transpose_cudnn_op contains two operators
    if ("${TARGET}" STREQUAL "conv_transpose_cudnn_op")
C
chengduoZH 已提交
118 119
        set(pybind_flag 1)
        # It's enough to just adding one operator to pybind
C
chengduoZH 已提交
120
        file(APPEND ${pybind_file} "USE_OP(conv2d_transpose_cudnn);\n")
C
chengduoZH 已提交
121 122
    endif()

123 124 125 126 127 128 129
    # save_restore_op contains several operators
    if ("${TARGET}" STREQUAL "save_restore_op")
        set(pybind_flag 1)
        # It's enough to just adding one operator to pybind
        file(APPEND ${pybind_file} "USE_NO_KERNEL_OP(save);\n")
    endif()

Q
qijun 已提交
130 131 132 133 134 135
    # activation_op contains several operators
    if ("${TARGET}" STREQUAL "activation_op")
        set(pybind_flag 1)
        # It's enough to just adding one operator to pybind
        file(APPEND ${pybind_file} "USE_OP(sigmoid);\n")
    endif()
Y
Yu Yang 已提交
136

D
Dong Zhihong 已提交
137 138 139 140
    # nccl_op contains several operators
    if ("${TARGET}" STREQUAL "nccl_op")
        set(pybind_flag 1)
        # It's enough to just adding one operator to pybind
Q
QI JUN 已提交
141
        file(APPEND ${pybind_file} "USE_CUDA_ONLY_OP(ncclAllReduce);\n")
D
Dong Zhihong 已提交
142
    endif()
W
wanghaox 已提交
143

G
guosheng 已提交
144 145 146 147 148 149
    # reduce_op contains several operators
    if ("${TARGET}" STREQUAL "reduce_op")
        set(pybind_flag 1)
        # It's enough to just adding one operator to pybind
        file(APPEND ${pybind_file} "USE_OP(reduce_sum);\n")
    endif()
Q
qijun 已提交
150

Y
Yu Yang 已提交
151 152 153 154 155
    if ("${TARGET}" STREQUAL "tensor_array_read_write_op")
        set(pybind_flag 1)
        file(APPEND ${pybind_file} "USE_NO_KERNEL_OP(read_from_array);\nUSE_NO_KERNEL_OP(write_to_array);\n")
    endif()

L
Luo Tao 已提交
156
    # pybind USE_NO_KERNEL_OP
T
typhoonzero 已提交
157
    # HACK: if REGISTER_OP_CPU_KERNEL presents the operator must have kernel
L
Luo Tao 已提交
158
    file(READ ${TARGET}.cc TARGET_CONTENT)
T
typhoonzero 已提交
159
    string(REGEX MATCH "REGISTER_OP_CPU_KERNEL" regex_result "${TARGET_CONTENT}")
L
Luo Tao 已提交
160 161 162 163 164 165 166 167
    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)
168 169 170
    list(LENGTH cu_cc_srcs cu_cc_srcs_len)

    if (${pybind_flag} EQUAL 0 AND ${cu_srcs_len} EQUAL 0 AND ${cu_cc_srcs_len} EQUAL 0)
L
Luo Tao 已提交
171 172 173 174 175 176 177 178
        file(APPEND ${pybind_file} "USE_CPU_ONLY_OP(${TARGET});\n")
        set(pybind_flag 1)
    endif()

    # pybind USE_OP
    if (${pybind_flag} EQUAL 0)
        file(APPEND ${pybind_file} "USE_OP(${TARGET});\n")
    endif()
Y
Yu Yang 已提交
179 180
endfunction()

Q
qijun 已提交
181
add_subdirectory(math)
D
Dong Zhihong 已提交
182
add_subdirectory(nccl)
183

184
set(DEPS_OPS
185 186
    cond_op
    cross_entropy_op
Y
Yu Yang 已提交
187
    recurrent_op
Y
Yu Yang 已提交
188
    softmax_with_cross_entropy_op
189 190
    softmax_op
    sequence_softmax_op
C
chengduoZH 已提交
191 192
    sum_op
    pool_op
W
wanghaox 已提交
193
    maxout_op
S
sweetsky0901 已提交
194
    unpool_op
195
    pool_with_index_op
C
chengduoZH 已提交
196
    conv_op
197
    conv_transpose_op
D
Dong Zhihong 已提交
198
    nccl_op
C
chengduoZH 已提交
199
    sequence_conv_op
200
    sequence_pool_op
Y
Yu Yang 已提交
201
    lod_rank_table_op
202 203
    lod_tensor_to_array_op
    array_to_lod_tensor_op
F
fengjiayi 已提交
204
    max_sequence_len_op
G
guosheng 已提交
205
    lstm_op
Y
Yu Yang 已提交
206
    tensor_array_read_write_op
Q
QI JUN 已提交
207 208
    gru_op
    adagrad_op
武毅 已提交
209 210 211 212
    sgd_op
    save_op
    load_op
    send_op
S
sweetsky0901 已提交
213 214
    recv_op
    detection_output_op)
Q
QI JUN 已提交
215

T
typhoonzero 已提交
216
if(WITH_DISTRIBUTE)
武毅 已提交
217 218 219 220 221
add_subdirectory(detail)
op_library(send_op SRCS send_op.cc DEPS sendrecvop_grpc grpc++_unsecure grpc_unsecure gpr cares zlib_target protobuf)
set_source_files_properties(
    send_op.cc
    PROPERTIES
Y
Yancey 已提交
222
    COMPILE_FLAGS  "-Wno-non-virtual-dtor -Wno-error=non-virtual-dtor -Wno-error=delete-non-virtual-dtor")
武毅 已提交
223 224 225 226 227

op_library(recv_op SRCS recv_op.cc DEPS sendrecvop_grpc grpc++_unsecure grpc_unsecure gpr cares zlib_target protobuf)
set_source_files_properties(
    recv_op.cc
    PROPERTIES
Y
Yancey 已提交
228
    COMPILE_FLAGS  "-Wno-non-virtual-dtor -Wno-error=non-virtual-dtor -Wno-error=delete-non-virtual-dtor")
Y
Yu Yang 已提交
229

T
typhoonzero 已提交
230 231 232
cc_test(test_send_recv SRCS send_recv_op_test.cc DEPS send_op recv_op sum_op executor)
endif()

Z
cond op  
zchen0211 已提交
233
op_library(cond_op SRCS cond_op.cc DEPS framework_proto tensor operator net_op)
234 235
op_library(cross_entropy_op DEPS cross_entropy)
op_library(softmax_with_cross_entropy_op DEPS cross_entropy softmax)
236
op_library(softmax_op DEPS softmax)
S
sweetsky0901 已提交
237
op_library(detection_output_op DEPS softmax)
238
op_library(sequence_softmax_op DEPS softmax)
Q
QI JUN 已提交
239 240 241
op_library(sum_op DEPS selected_rows_functor)
op_library(sgd_op DEPS selected_rows_functor)
op_library(adagrad_op DEPS selected_rows_functor)
C
chengduoZH 已提交
242
op_library(conv_op DEPS vol2col)
C
chengduoZH 已提交
243
op_library(pool_op DEPS pooling)
W
wanghaox 已提交
244
op_library(maxout_op DEPS maxouting)
S
sweetsky0901 已提交
245
op_library(unpool_op DEPS unpooling)
C
chengduoZH 已提交
246
op_library(pool_with_index_op DEPS pooling)
Y
Yu Yang 已提交
247
op_library(lod_rank_table_op SRCS lod_rank_table_op.cc DEPS lod_rank_table)
248 249
op_library(lod_tensor_to_array_op SRCS lod_tensor_to_array_op.cc DEPS lod_rank_table_op)
op_library(array_to_lod_tensor_op SRCS array_to_lod_tensor_op.cc DEPS lod_rank_table_op)
F
fengjiayi 已提交
250
op_library(max_sequence_len_op SRCS max_sequence_len_op.cc DEPS lod_rank_table)
Y
Yu Yang 已提交
251
op_library(tensor_array_read_write_op SRCS tensor_array_read_write_op.cc)
D
Dong Zhihong 已提交
252 253 254
if(WITH_GPU)
op_library(nccl_op DEPS nccl_common)
endif()
C
chengduoZH 已提交
255
op_library(sequence_conv_op DEPS context_project)
256
op_library(sequence_pool_op DEPS sequence_pooling)
D
dangqingqing 已提交
257
op_library(lstm_op DEPS sequence2batch lstm_compute)
C
chengduoZH 已提交
258
op_library(conv_transpose_op DEPS vol2col)
G
guosheng 已提交
259
op_library(gru_op DEPS sequence2batch gru_compute)
Y
Yu Yang 已提交
260
op_library(recurrent_op SRCS recurrent_op.cc DEPS executor)
261

武毅 已提交
262 263 264 265
# FIXME(typhoonzero): save/load depends lodtensor serialization functions
op_library(save_op DEPS lod_tensor)
op_library(load_op DEPS lod_tensor)

266
list(REMOVE_ITEM GENERAL_OPS ${DEPS_OPS})
L
Luo Tao 已提交
267
foreach(src ${GENERAL_OPS})
268
    op_library(${src})
L
Luo Tao 已提交
269 270 271 272
endforeach()

set(GLOB_OP_LIB ${OP_LIBRARY} CACHE INTERNAL "Global OP library")

武毅 已提交
273 274


275 276 277
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 已提交
278
cc_test(beam_search_decode_op_test SRCS beam_search_decode_op_test.cc DEPS lod_tensor)
279
cc_test(strided_memcpy_test SRCS strided_memcpy_test.cc DEPS tensor paddle_memory)
D
Dong Zhihong 已提交
280
if(WITH_GPU)
281
  cc_test(nccl_op_test SRCS nccl_op_test.cu.cc DEPS nccl_op gpu_info device_context)
D
Dong Zhihong 已提交
282
endif()
Y
Yu Yang 已提交
283
cc_test(save_load_op_test SRCS save_load_op_test.cc DEPS save_op load_op)