CMakeLists.txt 7.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)
Y
update  
Yancey1989 已提交
12
    set(op_common_deps operator op_registry math_function)
Y
Yu Yang 已提交
13 14 15
    set(options "")
    set(oneValueArgs "")
    set(multiValueArgs SRCS DEPS)
L
Luo Tao 已提交
16
    set(pybind_flag 0)
Y
Yu Yang 已提交
17 18 19
    cmake_parse_arguments(op_library "${options}" "${oneValueArgs}"
            "${multiValueArgs}" ${ARGN})

20 21 22 23
    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 已提交
24
        endif()
25
        if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${TARGET}.cu)
26
            list(APPEND cu_srcs ${TARGET}.cu)
27 28 29 30 31 32 33 34 35 36 37 38
        endif()
    else()
        foreach(src ${op_library_SRCS})
            if (${src} MATCHES ".*\\.cu$")
                list(APPEND cu_srcs ${src})
            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 已提交
39 40 41 42 43 44 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)
        nv_library(${TARGET} SRCS ${cc_srcs} ${cu_srcs} DEPS ${op_library_DEPS}
                ${op_common_deps})
    else()
        cc_library(${TARGET} SRCS ${cc_srcs} DEPS ${op_library_DEPS}
                ${op_common_deps})
    endif()
L
Luo Tao 已提交
52 53 54

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

C
chengduoZH 已提交
58
    # pool_op contains several operators
59 60 61 62 63 64
    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()

Y
Yu Yang 已提交
65 66 67 68 69
    if ("${TARGET}" STREQUAL "compare_op")
        set(pybind_flag 1)
        file(APPEND ${pybind_file} "USE_OP(less_than);\nUSE_OP(equal);\n")
    endif()

C
chengduoZH 已提交
70 71 72 73 74 75 76
    # 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 已提交
77 78 79 80 81 82
    # 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()
83

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

98 99 100 101 102 103 104
    # 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 已提交
105 106 107 108 109 110
    # 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 已提交
111

D
Dong Zhihong 已提交
112 113 114 115
    # nccl_op contains several operators
    if ("${TARGET}" STREQUAL "nccl_op")
        set(pybind_flag 1)
        # It's enough to just adding one operator to pybind
D
Dong Zhihong 已提交
116
        file(APPEND ${pybind_file} "USE_GPU_ONLY_OP(ncclAllReduce);\n")
D
Dong Zhihong 已提交
117
    endif()
Y
Yu Yang 已提交
118

G
guosheng 已提交
119 120 121 122 123 124
    # 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 已提交
125

Y
Yu Yang 已提交
126 127 128 129 130
    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 已提交
131
    # pybind USE_NO_KERNEL_OP
T
typhoonzero 已提交
132
    # HACK: if REGISTER_OP_CPU_KERNEL presents the operator must have kernel
L
Luo Tao 已提交
133
    file(READ ${TARGET}.cc TARGET_CONTENT)
T
typhoonzero 已提交
134
    string(REGEX MATCH "REGISTER_OP_CPU_KERNEL" regex_result "${TARGET_CONTENT}")
L
Luo Tao 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
    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)
    if (${pybind_flag} EQUAL 0 AND ${cu_srcs_len} EQUAL 0)
        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 已提交
152 153
endfunction()

Q
qijun 已提交
154
add_subdirectory(math)
D
Dong Zhihong 已提交
155
add_subdirectory(nccl)
156

157
set(DEPS_OPS
158 159
    cond_op
    cross_entropy_op
Y
Yu Yang 已提交
160 161
    recurrent_op
    dynamic_recurrent_op
Y
Yu Yang 已提交
162
    softmax_with_cross_entropy_op
C
chengduoZH 已提交
163
    sum_op
C
chengduoZH 已提交
164
    pool_op
C
chengduoZH 已提交
165
    pool_with_index_op
C
chengduoZH 已提交
166
    conv_op
C
chengduoZH 已提交
167
    lstm_op
168
    conv_transpose_op
D
Dong Zhihong 已提交
169
    nccl_op
C
chengduoZH 已提交
170
    sequence_conv_op
171
    sequence_pool_op
Y
Yu Yang 已提交
172
    lod_rank_table_op
173 174
    lod_tensor_to_array_op
    array_to_lod_tensor_op
G
guosheng 已提交
175
    lstm_op
Y
Yu Yang 已提交
176
    tensor_array_read_write_op
G
guosheng 已提交
177
    gru_op)
Y
Yu Yang 已提交
178

Z
cond op  
zchen0211 已提交
179
op_library(cond_op SRCS cond_op.cc DEPS framework_proto tensor operator net_op)
180 181
op_library(cross_entropy_op DEPS cross_entropy)
op_library(softmax_with_cross_entropy_op DEPS cross_entropy softmax)
C
chengduoZH 已提交
182
op_library(conv_op DEPS vol2col)
Q
QI JUN 已提交
183
op_library(sum_op DEPS net_op selected_rows_functor)
C
chengduoZH 已提交
184 185
op_library(pool_op DEPS pooling)
op_library(pool_with_index_op DEPS pooling)
Y
Yu Yang 已提交
186
op_library(lod_rank_table_op SRCS lod_rank_table_op.cc DEPS lod_rank_table)
187 188
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)
Y
Yu Yang 已提交
189
op_library(tensor_array_read_write_op SRCS tensor_array_read_write_op.cc)
D
Dong Zhihong 已提交
190 191 192
if(WITH_GPU)
op_library(nccl_op DEPS nccl_common)
endif()
C
chengduoZH 已提交
193
op_library(sequence_conv_op DEPS context_project)
194
op_library(sequence_pool_op DEPS sequence_pooling)
D
dangqingqing 已提交
195
op_library(lstm_op DEPS sequence2batch lstm_compute)
C
chengduoZH 已提交
196
op_library(conv_transpose_op DEPS vol2col)
G
guosheng 已提交
197
op_library(gru_op DEPS sequence2batch gru_compute)
Y
Yu Yang 已提交
198 199 200 201 202 203 204
if(WITH_TESTING)
    op_library(dynamic_recurrent_op SRCS dynamic_recurrent_op.cc rnn/recurrent_op_utils.cc
        DEPS net_op tensor_array gtest)
else()
    op_library(dynamic_recurrent_op SRCS dynamic_recurrent_op.cc rnn/recurrent_op_utils.cc
            DEPS net_op tensor_array)
endif()
Y
Yu Yang 已提交
205
op_library(recurrent_op SRCS recurrent_op.cc DEPS executor)
206

207
list(REMOVE_ITEM GENERAL_OPS ${DEPS_OPS})
L
Luo Tao 已提交
208
foreach(src ${GENERAL_OPS})
209
    op_library(${src})
L
Luo Tao 已提交
210 211 212 213
endforeach()

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

214 215 216
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)
217
cc_test(strided_memcpy_test SRCS strided_memcpy_test.cc DEPS tensor paddle_memory)
Y
Yu Yang 已提交
218 219 220
cc_test(dynamic_recurrent_op_test SRCS dynamic_recurrent_op_test.cc
        rnn/recurrent_op_utils.cc
        DEPS dynamic_recurrent_op)
D
Dong Zhihong 已提交
221 222 223
if(WITH_GPU)
  nv_test(nccl_op_test SRCS nccl_op_test.cu DEPS nccl_op gpu_info device_context)
endif()
Y
Yu Yang 已提交
224
cc_test(save_load_op_test SRCS save_load_op_test.cc DEPS save_op load_op)