CMakeLists.txt 7.3 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}")
3
set(DEPS_OPS "")
L
Luo Tao 已提交
4 5
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 已提交
6 7 8 9
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 已提交
10
    set(OP_LIBRARY ${TARGET} ${OP_LIBRARY} PARENT_SCOPE)
Y
Yu Yang 已提交
11 12
    set(cc_srcs)
    set(cu_srcs)
13
    set(cu_cc_srcs)
Y
update  
Yancey1989 已提交
14
    set(op_common_deps operator op_registry math_function)
Y
Yu Yang 已提交
15 16 17
    set(options "")
    set(oneValueArgs "")
    set(multiValueArgs SRCS DEPS)
L
Luo Tao 已提交
18
    set(pybind_flag 0)
Y
Yu Yang 已提交
19 20 21
    cmake_parse_arguments(op_library "${options}" "${oneValueArgs}"
            "${multiValueArgs}" ${ARGN})

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

52 53 54 55
    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 已提交
56
    if (WITH_GPU)
57
        nv_library(${TARGET} SRCS ${cc_srcs} ${cu_cc_srcs} ${cu_srcs} DEPS ${op_library_DEPS}
Y
Yu Yang 已提交
58 59 60 61 62
                ${op_common_deps})
    else()
        cc_library(${TARGET} SRCS ${cc_srcs} DEPS ${op_library_DEPS}
                ${op_common_deps})
    endif()
L
Luo Tao 已提交
63

64
    # Define operators that don't need pybind here.
65 66 67 68 69
    foreach(manual_pybind_op "net_op" "compare_op" "logical_op" "nccl_op" "tensor_array_read_write_op")
        if ("${TARGET}" STREQUAL "${manual_pybind_op}")
            set(pybind_flag 1)
        endif()
    endforeach()
Q
qijun 已提交
70

71 72 73
    # The registration of USE_OP, please refer to paddle/framework/op_registry.h.
    # 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.
74 75 76 77 78 79 80 81
    file(READ ${TARGET}.cc TARGET_CONTENT)
    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 已提交
82 83
    endif()

L
Luo Tao 已提交
84
    # pybind USE_NO_KERNEL_OP
T
typhoonzero 已提交
85
    # HACK: if REGISTER_OP_CPU_KERNEL presents the operator must have kernel
T
typhoonzero 已提交
86
    string(REGEX MATCH "REGISTER_OP_CPU_KERNEL" regex_result "${TARGET_CONTENT}")
L
Luo Tao 已提交
87 88 89 90 91 92 93 94
    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)
95 96
    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 已提交
97 98 99 100 101 102 103 104
        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 已提交
105 106
endfunction()

Q
qijun 已提交
107
add_subdirectory(math)
D
Dong Zhihong 已提交
108
add_subdirectory(nccl)
109

L
Luo Tao 已提交
110 111
if(WITH_GPU)
    op_library(nccl_op DEPS nccl_common)
112
    file(APPEND ${pybind_file} "USE_CUDA_ONLY_OP(ncclAllReduce);\n")
L
Luo Tao 已提交
113 114 115
else()
    set(DEPS_OPS ${DEPS_OPS} nccl_op)
endif()
Q
QI JUN 已提交
116

T
typhoonzero 已提交
117
if(WITH_DISTRIBUTE)
118 119 120 121 122 123 124 125 126 127
    add_subdirectory(detail)
    set(DISTRIBUTE_DEPS sendrecvop_grpc grpc++_unsecure grpc_unsecure gpr cares zlib_target protobuf)
    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})
    op_library(recv_op DEPS ${DISTRIBUTE_DEPS})
    set_source_files_properties(recv_op.cc PROPERTIES COMPILE_FLAGS ${DISTRIBUTE_COMPILE_FLAGS})
    cc_test(test_send_recv SRCS send_recv_op_test.cc DEPS send_op recv_op sum_op executor)
else()
    set(DEPS_OPS ${DEPS_OPS} send_op recv_op)
T
typhoonzero 已提交
128 129
endif()

130
op_library(cond_op DEPS framework_proto tensor net_op)
131 132
op_library(cross_entropy_op DEPS cross_entropy)
op_library(softmax_with_cross_entropy_op DEPS cross_entropy softmax)
133
op_library(softmax_op DEPS softmax)
S
sweetsky0901 已提交
134
op_library(detection_output_op DEPS softmax)
135
op_library(sequence_softmax_op DEPS softmax)
Q
QI JUN 已提交
136 137
op_library(sum_op DEPS selected_rows_functor)
op_library(sgd_op DEPS selected_rows_functor)
Y
Yan Chunwei 已提交
138
op_library(print_op DEPS lod_tensor)
Q
QI JUN 已提交
139
op_library(adagrad_op DEPS selected_rows_functor)
C
chengduoZH 已提交
140
op_library(conv_op DEPS vol2col)
C
chengduoZH 已提交
141
op_library(pool_op DEPS pooling)
W
wanghaox 已提交
142
op_library(maxout_op DEPS maxouting)
S
sweetsky0901 已提交
143
op_library(unpool_op DEPS unpooling)
C
chengduoZH 已提交
144
op_library(pool_with_index_op DEPS pooling)
145 146 147 148
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 已提交
149
op_library(sequence_conv_op DEPS context_project)
150
op_library(sequence_pool_op DEPS sequence_pooling)
D
dangqingqing 已提交
151
op_library(lstm_op DEPS sequence2batch lstm_compute)
C
chengduoZH 已提交
152
op_library(conv_transpose_op DEPS vol2col)
G
guosheng 已提交
153
op_library(gru_op DEPS sequence2batch gru_compute)
154
op_library(recurrent_op DEPS executor)
W
wanghaoshuang 已提交
155
op_library(warpctc_op DEPS dynload_warpctc sequence_padding sequence_scale math_function)
C
chengduoZH 已提交
156
op_library(cos_sim_op DEPS cos_sim_functor)
Y
Yang Yang 已提交
157
op_library(parallel_do_op DEPS executor)
武毅 已提交
158 159 160 161
# FIXME(typhoonzero): save/load depends lodtensor serialization functions
op_library(save_op DEPS lod_tensor)
op_library(load_op DEPS lod_tensor)

162
list(REMOVE_ITEM GENERAL_OPS ${DEPS_OPS})
L
Luo Tao 已提交
163
foreach(src ${GENERAL_OPS})
164
    op_library(${src})
L
Luo Tao 已提交
165
endforeach()
166 167
file(APPEND ${pybind_file} "USE_OP(less_than);\nUSE_OP(logical_and);\nUSE_NO_KERNEL_OP(read_from_array);\n")

L
Luo Tao 已提交
168 169 170

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

武毅 已提交
171

172 173 174
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 已提交
175
cc_test(beam_search_decode_op_test SRCS beam_search_decode_op_test.cc DEPS lod_tensor)
176
cc_test(strided_memcpy_test SRCS strided_memcpy_test.cc DEPS tensor paddle_memory)
D
Dong Zhihong 已提交
177
if(WITH_GPU)
178
    cc_test(nccl_op_test SRCS nccl_op_test.cu.cc DEPS nccl_op gpu_info device_context)
D
Dong Zhihong 已提交
179
endif()
Y
Yu Yang 已提交
180
cc_test(save_load_op_test SRCS save_load_op_test.cc DEPS save_op load_op)