Skip to content

Commit 231e2ee

Browse files
authored
Merge pull request #7148 from luotao1/op_make
refine pybind when *_op.cc contains several operators
2 parents 907e6d0 + 2d2b633 commit 231e2ee

File tree

2 files changed

+22
-98
lines changed

2 files changed

+22
-98
lines changed

paddle/framework/op_registry.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ class Registrar {
3737
public:
3838
// In our design, various kinds of classes, e.g., operators and kernels,
3939
// have their corresponding registry and registrar. The action of
40-
// registration is in the constructor of a global registrar variable, which,
41-
// however, are not used in the code that calls package framework, and would
40+
// registration is in the constructor of a global registrar variable, which
41+
// are not used in the code that calls package framework, and would
4242
// be removed from the generated binary file by the linker. To avoid such
4343
// removal, we add Touch to all registrar classes and make USE_OP macros to
4444
// call this method. So, as long as the callee code calls USE_OP, the global

paddle/operators/CMakeLists.txt

+20-96
Original file line numberDiff line numberDiff line change
@@ -61,106 +61,28 @@ function(op_library TARGET)
6161
${op_common_deps})
6262
endif()
6363

64-
# net_op doesn't need pybind
65-
if ("${TARGET}" STREQUAL "net_op")
66-
set(pybind_flag 1)
67-
endif()
68-
69-
if ("${TARGET}" STREQUAL "compare_op")
70-
set(pybind_flag 1)
71-
file(APPEND ${pybind_file} "USE_OP(less_than);\nUSE_OP(equal);\n")
72-
endif()
73-
74-
# conv_op contains several operators
75-
if ("${TARGET}" STREQUAL "conv_op")
76-
set(pybind_flag 1)
77-
# It's enough to just adding one operator to pybind
78-
file(APPEND ${pybind_file} "USE_OP(conv2d);\n")
79-
endif()
80-
81-
# conv_cudnn_op contains several operators
82-
if ("${TARGET}" STREQUAL "conv_cudnn_op")
83-
set(pybind_flag 1)
84-
# It's enough to just adding one operator to pybind
85-
file(APPEND ${pybind_file} "USE_OP(conv2d_cudnn);\n")
86-
endif()
87-
88-
# pool_op contains several operators
89-
if ("${TARGET}" STREQUAL "pool_op")
90-
set(pybind_flag 1)
91-
# It's enough to just adding one operator to pybind
92-
file(APPEND ${pybind_file} "USE_OP(pool2d);\n")
93-
endif()
94-
95-
# pool_cudnn_op contains several operators
96-
if ("${TARGET}" STREQUAL "pool_cudnn_op")
97-
set(pybind_flag 1)
98-
# It's enough to just adding one operator to pybind
99-
file(APPEND ${pybind_file} "USE_OP(pool2d_cudnn);\n")
100-
endif()
101-
102-
if ("${TARGET}" STREQUAL "logical_op")
103-
set(pybind_flag 1)
104-
file(APPEND ${pybind_file} "USE_OP(logical_and);\n")
105-
endif()
106-
107-
# pool_with_index_op contains several operators
108-
if ("${TARGET}" STREQUAL "pool_with_index_op")
109-
set(pybind_flag 1)
110-
# It's enough to just adding one operator to pybind
111-
file(APPEND ${pybind_file} "USE_OP(max_pool2d_with_index);\n")
112-
endif()
113-
114-
# conv_transpose_op contains several operators
115-
if ("${TARGET}" STREQUAL "conv_transpose_op")
116-
set(pybind_flag 1)
117-
# It's enough to just adding one operator to pybind
118-
file(APPEND ${pybind_file} "USE_OP(conv2d_transpose);\n")
119-
endif()
120-
121-
# conv_transpose_cudnn_op contains two operators
122-
if ("${TARGET}" STREQUAL "conv_transpose_cudnn_op")
123-
set(pybind_flag 1)
124-
# It's enough to just adding one operator to pybind
125-
file(APPEND ${pybind_file} "USE_OP(conv2d_transpose_cudnn);\n")
126-
endif()
127-
128-
# save_restore_op contains several operators
129-
if ("${TARGET}" STREQUAL "save_restore_op")
130-
set(pybind_flag 1)
131-
# It's enough to just adding one operator to pybind
132-
file(APPEND ${pybind_file} "USE_NO_KERNEL_OP(save);\n")
133-
endif()
134-
135-
# activation_op contains several operators
136-
if ("${TARGET}" STREQUAL "activation_op")
137-
set(pybind_flag 1)
138-
# It's enough to just adding one operator to pybind
139-
file(APPEND ${pybind_file} "USE_OP(sigmoid);\n")
140-
endif()
141-
142-
# nccl_op contains several operators
143-
if ("${TARGET}" STREQUAL "nccl_op")
144-
set(pybind_flag 1)
145-
# It's enough to just adding one operator to pybind
146-
file(APPEND ${pybind_file} "USE_CUDA_ONLY_OP(ncclAllReduce);\n")
147-
endif()
148-
149-
# reduce_op contains several operators
150-
if ("${TARGET}" STREQUAL "reduce_op")
151-
set(pybind_flag 1)
152-
# It's enough to just adding one operator to pybind
153-
file(APPEND ${pybind_file} "USE_OP(reduce_sum);\n")
154-
endif()
64+
# Define operators that don't need pybind here.
65+
foreach(manual_pybind_op "net_op" "compare_op" "logical_op" "nccl_op" "tensor_array_read_write_op")
66+
if ("${TARGET}" STREQUAL "${manual_pybind_op}")
67+
set(pybind_flag 1)
68+
endif()
69+
endforeach()
15570

156-
if ("${TARGET}" STREQUAL "tensor_array_read_write_op")
157-
set(pybind_flag 1)
158-
file(APPEND ${pybind_file} "USE_NO_KERNEL_OP(read_from_array);\nUSE_NO_KERNEL_OP(write_to_array);\n")
71+
# The registration of USE_OP, please refer to paddle/framework/op_registry.h.
72+
# Note that it's enough to just adding one operator to pybind in a *_op.cc file.
73+
# And for detail pybind information, please see generated paddle/pybind/pybind.h.
74+
file(READ ${TARGET}.cc TARGET_CONTENT)
75+
string(REGEX MATCH "REGISTER_OP\\(.*REGISTER_OP\\(" multi_register "${TARGET_CONTENT}")
76+
string(REGEX MATCH "REGISTER_OP\\([a-z0-9_]*," one_register "${multi_register}")
77+
if (one_register STREQUAL "")
78+
string(REPLACE "_op" "" TARGET "${TARGET}")
79+
else ()
80+
string(REPLACE "REGISTER_OP(" "" TARGET "${one_register}")
81+
string(REPLACE "," "" TARGET "${TARGET}")
15982
endif()
16083

16184
# pybind USE_NO_KERNEL_OP
16285
# HACK: if REGISTER_OP_CPU_KERNEL presents the operator must have kernel
163-
file(READ ${TARGET}.cc TARGET_CONTENT)
16486
string(REGEX MATCH "REGISTER_OP_CPU_KERNEL" regex_result "${TARGET_CONTENT}")
16587
string(REPLACE "_op" "" TARGET "${TARGET}")
16688
if (${pybind_flag} EQUAL 0 AND regex_result STREQUAL "")
@@ -171,7 +93,6 @@ function(op_library TARGET)
17193
# pybind USE_CPU_ONLY_OP
17294
list(LENGTH cu_srcs cu_srcs_len)
17395
list(LENGTH cu_cc_srcs cu_cc_srcs_len)
174-
17596
if (${pybind_flag} EQUAL 0 AND ${cu_srcs_len} EQUAL 0 AND ${cu_cc_srcs_len} EQUAL 0)
17697
file(APPEND ${pybind_file} "USE_CPU_ONLY_OP(${TARGET});\n")
17798
set(pybind_flag 1)
@@ -188,6 +109,7 @@ add_subdirectory(nccl)
188109

189110
if(WITH_GPU)
190111
op_library(nccl_op DEPS nccl_common)
112+
file(APPEND ${pybind_file} "USE_CUDA_ONLY_OP(ncclAllReduce);\n")
191113
else()
192114
set(DEPS_OPS ${DEPS_OPS} nccl_op)
193115
endif()
@@ -238,6 +160,8 @@ list(REMOVE_ITEM GENERAL_OPS ${DEPS_OPS})
238160
foreach(src ${GENERAL_OPS})
239161
op_library(${src})
240162
endforeach()
163+
file(APPEND ${pybind_file} "USE_OP(less_than);\nUSE_OP(logical_and);\nUSE_NO_KERNEL_OP(read_from_array);\n")
164+
241165

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

0 commit comments

Comments
 (0)