@@ -112,354 +112,6 @@ function(kernel_declare TARGET_LIST)
112112 endforeach ()
113113endfunction ()
114114
115- function (kernel_library TARGET )
116- return ()
117- set (common_srcs)
118- set (cpu_srcs)
119- set (gpu_srcs)
120- set (xpu_srcs)
121- set (gpudnn_srcs)
122- set (kps_srcs)
123- # parse and save the deps kerenl targets
124- set (all_srcs)
125- set (kernel_deps)
126-
127- set (oneValueArgs SUB_DIR)
128- set (multiValueArgs SRCS DEPS)
129- set (target_build_flag 1)
130-
131- cmake_parse_arguments (kernel_library "${options} " "${oneValueArgs} "
132- "${multiValueArgs} " ${ARGN} )
133-
134- # used for cc_library selected_rows dir target
135- set (target_suffix "" )
136- if ("${kernel_library_SUB_DIR} " STREQUAL "selected_rows" )
137- set (target_suffix "_sr" )
138- endif ()
139- if ("${kernel_library_SUB_DIR} " STREQUAL "sparse" )
140- set (target_suffix "_sp" )
141- endif ()
142-
143- list (LENGTH kernel_library_SRCS kernel_library_SRCS_len)
144- # one kernel only match one impl file in each backend
145- if (${kernel_library_SRCS_len} EQUAL 0)
146- if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR} /${TARGET} .cc)
147- list (APPEND common_srcs ${CMAKE_CURRENT_SOURCE_DIR} /${TARGET} .cc)
148- endif ()
149- if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR} /cpu/${TARGET} .cc)
150- list (APPEND cpu_srcs ${CMAKE_CURRENT_SOURCE_DIR} /cpu/${TARGET} .cc)
151- endif ()
152- if (WITH_GPU OR WITH_ROCM)
153- if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR} /gpu/${TARGET} .cu)
154- list (APPEND gpu_srcs ${CMAKE_CURRENT_SOURCE_DIR} /gpu/${TARGET} .cu)
155- endif ()
156- if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR} /gpu/${TARGET} .cu.cc)
157- list (APPEND gpu_srcs ${CMAKE_CURRENT_SOURCE_DIR} /gpu/${TARGET} .cu.cc)
158- endif ()
159- if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR} /kps/${TARGET} .cu)
160- list (APPEND gpu_srcs ${CMAKE_CURRENT_SOURCE_DIR} /kps/${TARGET} .cu)
161- endif ()
162- if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR} /gpudnn/${TARGET} .cu)
163- list (APPEND gpudnn_srcs ${CMAKE_CURRENT_SOURCE_DIR} /gpudnn/${TARGET} .cu)
164- endif ()
165- endif ()
166- if (WITH_XPU)
167- if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR} /xpu/${TARGET} .cc)
168- list (APPEND xpu_srcs ${CMAKE_CURRENT_SOURCE_DIR} /xpu/${TARGET} .cc)
169- endif ()
170- endif ()
171- if (WITH_XPU_KP)
172- if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR} /kps/${TARGET} .cu)
173- # Change XPU2 file suffix
174- # NOTE(chenweihang): If we can be sure that the *.kps suffix is no longer used, it can be copied directly to *.xpu
175- file (COPY ${CMAKE_CURRENT_SOURCE_DIR} /kps/${TARGET} .cu
176- DESTINATION ${CMAKE_CURRENT_BINARY_DIR} /kps)
177- file (RENAME ${CMAKE_CURRENT_BINARY_DIR} /kps/${TARGET} .cu
178- ${CMAKE_CURRENT_BINARY_DIR} /kps/${TARGET} .kps)
179- list (APPEND kps_srcs ${CMAKE_CURRENT_BINARY_DIR} /kps/${TARGET} .kps)
180- endif ()
181- endif ()
182- else ()
183- # TODO(chenweihang): impl compile by source later
184- endif ()
185-
186- list (APPEND all_srcs ${CMAKE_CURRENT_SOURCE_DIR} /${TARGET} .h)
187- if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR} /impl/${TARGET} _impl.h)
188- list (APPEND all_srcs ${CMAKE_CURRENT_SOURCE_DIR} /impl/${TARGET} _impl.h)
189- endif ()
190- list (APPEND all_srcs ${common_srcs} )
191- list (APPEND all_srcs ${cpu_srcs} )
192- list (APPEND all_srcs ${gpu_srcs} )
193- list (APPEND all_srcs ${xpu_srcs} )
194- list (APPEND all_srcs ${gpudnn_srcs} )
195- list (APPEND all_srcs ${kps_srcs} )
196-
197- set (all_include_kernels)
198- set (all_kernel_name)
199-
200- foreach (src ${all_srcs} )
201- file (READ ${src} target_content)
202- # "kernels/xxx"(DenseTensor Kernel) can only include each other, but can't include "SUB_DIR/xxx" (such as selected_rows Kernel)
203- string (REGEX MATCHALL
204- "#include \" paddle\/ phi\/ kernels\/ [a-z0-9_]+_kernel.h\" "
205- include_kernels ${target_content} )
206- list (APPEND all_include_kernels ${include_kernels} )
207-
208- # "SUB_DIR/xxx" can include "kernels/xx" and "SUB_DIR/xxx"
209- if (NOT "${kernel_library_SUB_DIR} " STREQUAL "" )
210- string (
211- REGEX
212- MATCHALL
213- "#include \" paddle\/ phi\/ kernels\/ ${kernel_library_SUB_DIR} \/ [a-z0-9_]+_kernel.h\" "
214- include_kernels
215- ${target_content} )
216- list (APPEND all_include_kernels ${include_kernels} )
217- endif ()
218-
219- foreach (include_kernel ${all_include_kernels} )
220- if ("${kernel_library_SUB_DIR} " STREQUAL "" )
221- string (REGEX REPLACE "#include \" paddle\/ phi\/ kernels\/ " "" kernel_name
222- ${include_kernel} )
223- string (REGEX REPLACE ".h\" " "" kernel_name ${kernel_name} )
224- list (APPEND all_kernel_name ${kernel_name} )
225- else ()
226- # NOTE(dev): we should firstly match kernel_library_SUB_DIR.
227- if (${include_kernel} MATCHES
228- "#include \" paddle\/ phi\/ kernels\/ ${kernel_library_SUB_DIR} \/ " )
229- string (
230- REGEX
231- REPLACE
232- "#include \" paddle\/ phi\/ kernels\/ ${kernel_library_SUB_DIR} \/ " ""
233- kernel_name ${include_kernel} )
234- # for selected_rows directory, add ${target_suffix}.
235- string (REGEX REPLACE ".h\" " "${target_suffix} " kernel_name
236- ${kernel_name} )
237- list (APPEND all_kernel_name ${kernel_name} )
238- else ()
239- string (REGEX REPLACE "#include \" paddle\/ phi\/ kernels\/ " ""
240- kernel_name ${include_kernel} )
241- string (REGEX REPLACE ".h\" " "" kernel_name ${kernel_name} )
242- list (APPEND all_kernel_name ${kernel_name} )
243- endif ()
244- endif ()
245- list (APPEND kernel_deps ${all_kernel_name} )
246- endforeach ()
247- endforeach ()
248- list (REMOVE_DUPLICATES kernel_deps)
249- list (REMOVE_ITEM kernel_deps ${TARGET}${target_suffix} )
250-
251- list (LENGTH common_srcs common_srcs_len)
252- list (LENGTH cpu_srcs cpu_srcs_len)
253- list (LENGTH gpu_srcs gpu_srcs_len)
254- list (LENGTH xpu_srcs xpu_srcs_len)
255- list (LENGTH gpudnn_srcs gpudnn_srcs_len)
256- list (LENGTH kps_srcs kps_srcs_len)
257-
258- # kernel source file level
259- # level 1: base device kernel (if any device or dnn kernel exists, the cpu_kernel must be exists!!!)
260- # - cpu_srcs / gpu_srcs / xpu_srcs / kps_srcs
261- # = dnn srcs: gpudnn_srcs
262- # level 2: device-independent kernel
263- # - common_srcs
264-
265- set (partial_build_flag 0)
266- set (base_build_flag 0)
267- if (${common_srcs_len} GREATER 0)
268- set (partial_build_flag 1)
269- endif ()
270- if (${cpu_srcs_len} GREATER 0
271- OR ${gpu_srcs_len} GREATER 0
272- OR ${xpu_srcs_len} GREATER 0
273- OR ${kps_srcs_len} GREATER 0)
274- set (base_build_flag 1)
275- endif ()
276-
277- # gpudnn or mkldnn needs to be compiled separately
278- set (dnn_kernels)
279- if (${gpudnn_srcs_len} GREATER 0)
280- if (WITH_GPU)
281- nv_library(
282- ${TARGET} _gpudnn${target_suffix}
283- SRCS ${gpudnn_srcs}
284- DEPS ${kernel_library_DEPS} ${kernel_deps} )
285- elseif (WITH_ROCM)
286- hip_library(
287- ${TARGET} _gpudnn${target_suffix}
288- SRCS ${gpudnn_srcs}
289- DEPS ${kernel_library_DEPS} ${kernel_deps} )
290- endif ()
291- list (APPEND dnn_kernels ${TARGET} _gpudnn${target_suffix} )
292- endif ()
293- list (LENGTH dnn_kernels dnn_kernels_len)
294-
295- if (${partial_build_flag} EQUAL 0 AND ${base_build_flag} EQUAL 1)
296- if (WITH_GPU)
297- if (${dnn_kernels_len} GREATER 0)
298- nv_library(
299- ${TARGET} _base${target_suffix}
300- SRCS ${cpu_srcs} ${gpu_srcs}
301- DEPS ${kernel_library_DEPS} ${kernel_deps} )
302- nv_library(${TARGET}${target_suffix} DEPS ${TARGET} _base${target_suffix}
303- ${dnn_kernels} )
304- else ()
305- nv_library(
306- ${TARGET}${target_suffix}
307- SRCS ${cpu_srcs} ${gpu_srcs}
308- DEPS ${kernel_library_DEPS} ${kernel_deps} )
309- endif ()
310- elseif (WITH_ROCM)
311- if (${dnn_kernels_len} GREATER 0)
312- hip_library(
313- ${TARGET} _base${target_suffix}
314- SRCS ${cpu_srcs} ${gpu_srcs}
315- DEPS ${kernel_library_DEPS} ${kernel_deps} )
316- hip_library(${TARGET}${target_suffix}
317- DEPS ${TARGET} _base${target_suffix} ${dnn_kernels} )
318- else ()
319- hip_library(
320- ${TARGET}${target_suffix}
321- SRCS ${cpu_srcs} ${gpu_srcs}
322- DEPS ${kernel_library_DEPS} ${kernel_deps} )
323- endif ()
324- elseif (WITH_XPU_KP)
325- xpu_library(
326- ${TARGET}${target_suffix}
327- SRCS ${cpu_srcs} ${kps_srcs} ${xpu_srcs}
328- DEPS ${kernel_library_DEPS} ${kernel_deps} )
329- else ()
330- cc_library(
331- ${TARGET}${target_suffix}
332- SRCS ${cpu_srcs} ${xpu_srcs}
333- DEPS ${kernel_library_DEPS} ${kernel_deps} )
334- endif ()
335- elseif (${partial_build_flag} EQUAL 1 AND ${base_build_flag} EQUAL 1)
336- if (WITH_GPU)
337- nv_library(
338- ${TARGET} _base${target_suffix}
339- SRCS ${cpu_srcs} ${gpu_srcs}
340- DEPS ${kernel_library_DEPS} ${kernel_deps} )
341- nv_library(
342- ${TARGET}${target_suffix}
343- SRCS ${common_srcs}
344- DEPS ${TARGET} _base${target_suffix} ${dnn_kernels} )
345- elseif (WITH_ROCM)
346- hip_library(
347- ${TARGET} _base${target_suffix}
348- SRCS ${cpu_srcs} ${gpu_srcs}
349- DEPS ${kernel_library_DEPS} ${kernel_deps} )
350- hip_library(
351- ${TARGET}${target_suffix}
352- SRCS ${common_srcs}
353- DEPS ${TARGET} _base${target_suffix} ${dnn_kernels} )
354- elseif (WITH_XPU_KP)
355- xpu_library(
356- ${TARGET} _base${target_suffix}
357- SRCS ${cpu_srcs} ${kps_srcs} ${xpu_srcs}
358- DEPS ${kernel_library_DEPS} ${kernel_deps} )
359- xpu_library(
360- ${TARGET}${target_suffix}
361- SRCS ${common_srcs}
362- DEPS ${TARGET} _base${target_suffix} )
363- else ()
364- cc_library(
365- ${TARGET} _base${target_suffix}
366- SRCS ${cpu_srcs} ${xpu_srcs}
367- DEPS ${kernel_library_DEPS} ${kernel_deps} )
368- cc_library(
369- ${TARGET}${target_suffix}
370- SRCS ${common_srcs}
371- DEPS ${TARGET} _base${target_suffix} )
372- endif ()
373- elseif (${partial_build_flag} EQUAL 1 AND ${base_build_flag} EQUAL 0)
374- if (WITH_GPU)
375- nv_library(
376- ${TARGET}${target_suffix}
377- SRCS ${common_srcs}
378- DEPS ${kernel_library_DEPS} ${kernel_deps} )
379- elseif (WITH_ROCM)
380- hip_library(
381- ${TARGET}${target_suffix}
382- SRCS ${common_srcs}
383- DEPS ${kernel_library_DEPS} ${kernel_deps} )
384- elseif (WITH_XPU_KP)
385- xpu_library(
386- ${TARGET}${target_suffix}
387- SRCS ${common_srcs}
388- DEPS ${kernel_library_DEPS} ${kernel_deps} )
389- else ()
390- cc_library(
391- ${TARGET}${target_suffix}
392- SRCS ${common_srcs}
393- DEPS ${kernel_library_DEPS} ${kernel_deps} )
394- endif ()
395- else ()
396- set (target_build_flag 0)
397- endif ()
398-
399- if (${target_build_flag} EQUAL 1)
400- if (${common_srcs_len} GREATER 0
401- OR ${cpu_srcs_len} GREATER 0
402- OR ${gpu_srcs_len} GREATER 0
403- OR ${xpu_srcs_len} GREATER 0
404- OR ${kps_srcs_len} GREATER 0
405- OR ${gpudnn_srcs_len} GREATER 0)
406- # append target into PHI_KERNELS property
407- get_property (phi_kernels GLOBAL PROPERTY PHI_KERNELS)
408- set (phi_kernels ${phi_kernels} ${TARGET}${target_suffix} )
409- set_property (GLOBAL PROPERTY PHI_KERNELS ${phi_kernels} )
410- endif ()
411-
412- # parse kernel name and auto generate kernel declaration
413- # here, we don't need to check WITH_XXX, because if not WITH_XXX, the
414- # xxx_srcs_len will be equal to 0
415- if (${common_srcs_len} GREATER 0)
416- kernel_declare(${common_srcs} )
417- endif ()
418- if (${cpu_srcs_len} GREATER 0)
419- kernel_declare(${cpu_srcs} )
420- endif ()
421- if (${gpu_srcs_len} GREATER 0)
422- kernel_declare(${gpu_srcs} )
423- endif ()
424- if (${xpu_srcs_len} GREATER 0)
425- kernel_declare(${xpu_srcs} )
426- endif ()
427- if (${gpudnn_srcs_len} GREATER 0)
428- kernel_declare(${gpudnn_srcs} )
429- endif ()
430- if (${kps_srcs_len} GREATER 0)
431- kernel_declare(${kps_srcs} )
432- endif ()
433- endif ()
434- endfunction ()
435-
436- function (register_kernels)
437- set (options "" )
438- set (oneValueArgs SUB_DIR)
439- set (multiValueArgs EXCLUDES DEPS)
440- cmake_parse_arguments (register_kernels "${options} " "${oneValueArgs} "
441- "${multiValueArgs} " ${ARGN} )
442-
443- file (
444- GLOB KERNELS
445- RELATIVE "${CMAKE_CURRENT_SOURCE_DIR} "
446- "*_kernel.h" )
447- string (REPLACE ".h" "" KERNELS "${KERNELS} " )
448- list (LENGTH register_kernels_DEPS register_kernels_DEPS_len)
449-
450- foreach (target ${KERNELS} )
451- list (FIND register_kernels_EXCLUDES ${target} _index)
452- if (${_index} EQUAL -1)
453- if (${register_kernels_DEPS_len} GREATER 0)
454- kernel_library(${target} DEPS ${register_kernels_DEPS} SUB_DIR
455- ${register_kernels_SUB_DIR} )
456- else ()
457- kernel_library(${target} SUB_DIR ${register_kernels_SUB_DIR} )
458- endif ()
459- endif ()
460- endforeach ()
461- endfunction ()
462-
463115function (append_op_util_declare TARGET )
464116 file (READ ${CMAKE_CURRENT_SOURCE_DIR} /${TARGET} target_content)
465117 string (
0 commit comments