get_phi_kernel_function.sh 7.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#!/usr/bin/env bash

# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#=================================================
#                   Utils
#=================================================

set -e

#step 1:get kernel registered info
W
Wilber 已提交
24
# The shell script has some problem when register with macro, such as in `activation_kernel.c*`
25
kernel_register_info_file=`mktemp`
W
Wilber 已提交
26
PADDLE_ROOT="$( cd "$( dirname "$0" )/../../" && pwd )"
W
Wilber 已提交
27
unset GREP_OPTIONS && find ${PADDLE_ROOT}/paddle/phi/kernels -name "*.c*" | grep -v "activation_kernel.c*" \
28
  | xargs sed -e '/PD_REGISTER_\(GENERAL_\)\?KERNEL(/,/)/!d' \
W
Wilber 已提交
29
  | awk 'BEGIN { RS="{" }{ gsub(/\n /,""); print $0 }' \
30
  | grep PD_REGISTER \
W
Wilber 已提交
31
  | awk -F ",|\(|\)" '{gsub(/ /,"");$1="";print}' \
32
  | sort -u  | awk '{gsub(/phi::/,"");gsub(/paddle::platform::/,"");gsub(/dtype::/,"");gsub(/paddle::/,"");print $0}' \
W
Wilber 已提交
33
  | grep -v "_grad" > $kernel_register_info_file
34

W
Wilber 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
# handle `activation_kernel.cc` case by case.
find ${PADDLE_ROOT}/paddle/phi/kernels -name "activation_kernel.cc" | xargs sed -e '/PD_REGISTER_KERNEL(relu/,/)/!d' \
  | awk 'BEGIN { RS="{" }{ gsub(/\n /,""); print $0 }' |   grep PD_REGISTER_KERNEL \
  | awk -F ",|\(|\)" '{gsub(/ /,"");$1="";print}' \
  | sort -u  | awk '{gsub(/phi::/,"");gsub(/paddle::platform::/,"");gsub(/dtype::/,"");gsub(/paddle::/,"");print $0}' \
  | grep -v "_grad" >> $kernel_register_info_file
act_temp=$(find ${PADDLE_ROOT}/paddle/phi/kernels -name "activation_kernel.cc" | xargs sed -e '/PD_REGISTER_KERNEL(name/,/)/!d' \
  | awk 'BEGIN { RS="{" }{ gsub(/\n /,""); print $0 }' | grep -E "PD_REGISTER_(GENERAL_)?KERNEL" \
  | awk -F ",|\(|\)" '{gsub(/ /,"");gsub(/\\/,"");$1="";print}' | sort -u \
  | awk '{gsub(/phi::/,"");gsub(/paddle::platform::/,"");gsub(/dtype::/,"");gsub(/paddle::/,"");print $0}' \
  | grep -v "_grad")
all_act_arg=$(find ${PADDLE_ROOT}/paddle/phi/kernels -name "activation_kernel.cc" | xargs sed -e '/PD_REGISTER_ACTIVATION_KERNEL(/,/)/!d' | grep -v '#define' | grep PD_REGISTER_ACTIVATION_KERNEL |   awk -F "\(|\)" '{gsub(/ /,"");$1="";print}' | sed -e 's/[ \t]*$//g')
for act in $all_act_arg
do
  name=${act%,*}
  kernel=$(echo ${act#*,} | sed -e 's/\r//g')
  tmp=${act_temp/name/${name}}
  echo "${tmp/func/${kernel}}" >> $kernel_register_info_file
done

# TODO(wilber): We just support cuda, not support rocm.
# handle `activation_kernel.cu` which register with macro.
# - process relu kernel.
find ${PADDLE_ROOT}/paddle/phi/kernels -name "activation_kernel.cu" | xargs sed -e '/PD_REGISTER_KERNEL(relu/,/)/!d' \
  | awk 'BEGIN { RS="{" }{ gsub(/\n /,""); print $0 }' | awk 'NR>2' |   grep PD_REGISTER \
  | awk -F ",|\(|\)" '{gsub(/ /,"");$1="";print}' \
  | sort -u  | awk '{gsub(/phi::/,"");gsub(/paddle::platform::/,"");gsub(/dtype::/,"");gsub(/paddle::/,"");print $0}' \
  | grep -v "_grad" >> $kernel_register_info_file
# - process PD_REGISTER_ACTIVATION_KERNEL kernels.
act_temp=$(find ${PADDLE_ROOT}/paddle/phi/kernels -name "activation_kernel.cu" | xargs sed -e '/PD_REGISTER_KERNEL(name/,/)/!d' \
  | awk 'BEGIN { RS="{" }{ gsub(/\n /,""); print $0 }' | grep PD_REGISTER \
  | awk -F ",|\(|\)" '{gsub(/ /,"");gsub(/\\/,"");$1="";print}' | sort -u \
  | awk '{gsub(/phi::/,"");gsub(/paddle::platform::/,"");gsub(/dtype::/,"");gsub(/paddle::/,"");print $0}' \
  | grep -v "_grad")
all_act_arg=$(find ${PADDLE_ROOT}/paddle/phi/kernels -name "activation_kernel.cu" | xargs sed -e '/PD_REGISTER_ACTIVATION_KERNEL(/,/)/!d' | grep -v '#define' | grep PD_REGISTER_ACTIVATION_KERNEL |   awk -F "\(|\)" '{gsub(/ /,"");$1="";print}' | sed -e 's/[ \t]*$//g')
for act in $all_act_arg
do
  name=${act%,*}
  kernel=$(echo ${act#*,} | sed -e 's/\r//g')
  tmp=${act_temp/name/${name}}
  echo "${tmp/func/${kernel}}" >> $kernel_register_info_file
done

78 79
#step 2:get simple general inferMeta function wrap info
temp_path=`mktemp -d`
80 81
python3 ${PADDLE_ROOT}/paddle/phi/api/yaml/generator/wrapped_infermeta_gen.py \
  --api_yaml_path ${PADDLE_ROOT}/paddle/phi/api/yaml/api.yaml ${PADDLE_ROOT}/paddle/phi/api/yaml/legacy_api.yaml \
82 83 84
  --wrapped_infermeta_header_path ${temp_path}/generate.h \
  --wrapped_infermeta_source_path ${temp_path}/generate.cc

85
find  ${PADDLE_ROOT}/paddle/phi/ -name "*.cc" | xargs grep PD_REGISTER_INFER_META_FN ${temp_path}/generate.cc \
86 87
  | awk -F "\(|,|::|\)" '{print $2, $4}' > ${temp_path}/wrap_info.txt

W
Wilber 已提交
88 89 90
#step 3:get ir's attr_name.
ir_attr_name_info_file=`mktemp`
# phi_cpu attr
91
all_ir_name=`grep -Eo "PDTCPU_Kernel<.*\"" ${PADDLE_ROOT}/paddle/infrt/dialect/phi/ir/phi_cpu_kernels.td | awk -v FS="<" '{gsub(/\"/,"");print $2}'`
W
Wilber 已提交
92 93
for ir in $all_ir_name
do
94
  attr_name=`grep "<\"$ir" -A 3 ${PADDLE_ROOT}/paddle/infrt/dialect/phi/ir/phi_cpu_kernels.td  | grep -Eo "Attr:.*)" \
95
  | awk '{gsub(/F32Attr/,"");gsub(/F64Attr/,"");gsub(/StrAttr/,"");gsub(/BoolAttr/,""); \
W
Wilber 已提交
96 97 98
  gsub(/SI1Attr/,"");gsub(/SI8Attr/,"");gsub(/SI16Attr/,"");gsub(/SI32Attr/,"");gsub(/SI64Attr/,""); \
  gsub(/UI1Attr/,"");gsub(/UI8Attr/,"");gsub(/I16Attr/,"");gsub(/I32Attr/,"");gsub(/I64Attr/,""); \
  gsub(/I1Attr/,"");gsub(/I8Attr/,"");gsub(/UI16Attr/,"");gsub(/UI32Attr/,"");gsub(/UI64Attr/,""); \
99
  gsub(/I32ArrayAttr/,"");gsub(/SI32ArrayAttr/,""); \
W
Wilber 已提交
100 101 102 103 104
  gsub(/Attr/,"");gsub(/\)/,""); \
  gsub(/[,:]/,"");print $a}'`
  echo phi_cpu.$ir $attr_name >> $ir_attr_name_info_file
done
# phi_gpu attr
105
all_ir_name=`grep -Eo "PDTGPU_Kernel<.*\"" ${PADDLE_ROOT}/paddle/infrt/dialect/phi/ir/phi_gpu_kernels.td | awk -v FS="<" '{gsub(/\"/,"");print $2}'`
W
Wilber 已提交
106 107
for ir in $all_ir_name
do
108
  attr_name=`grep "<\"$ir" -A 3 ${PADDLE_ROOT}/paddle/infrt/dialect/phi/ir/phi_gpu_kernels.td  | grep -Eo "Attr:.*)" \
109
  | awk '{gsub(/F32Attr/,"");gsub(/F64Attr/,"");gsub(/StrAttr/,"");gsub(/BoolAttr/,""); \
W
Wilber 已提交
110 111 112
  gsub(/SI1Attr/,"");gsub(/SI8Attr/,"");gsub(/SI16Attr/,"");gsub(/SI32Attr/,"");gsub(/SI64Attr/,""); \
  gsub(/UI1Attr/,"");gsub(/UI8Attr/,"");gsub(/I16Attr/,"");gsub(/I32Attr/,"");gsub(/I64Attr/,""); \
  gsub(/I1Attr/,"");gsub(/I8Attr/,"");gsub(/UI16Attr/,"");gsub(/UI32Attr/,"");gsub(/UI64Attr/,""); \
113 114
  gsub(/I32ArrayAttr/,"");gsub(/SI32ArrayAttr/,""); \
  gsub(/Attr/,"");gsub(/\)/,"") \
W
Wilber 已提交
115 116 117 118 119
  gsub(/[,:]/,"");print $a}'`
  echo phi_gpu.$ir $attr_name >> $ir_attr_name_info_file
done

#step 4: merge all infos
120
#  @input1 => phi kernel infomation : kernel_name kernel_key(GPU/CPU, precision, layout)
121 122 123
#  @input2 => information from api.yaml : kernel_name kernel_function_name inferMeta_function_name 
#  @input3 => information from wrapped_infermeta_gen : ensure the inferMeta function has
#             same signature with kernel function
124
python3 ${PADDLE_ROOT}/tools/infrt/get_phi_kernel_info.py \
125 126
  --paddle_root_path ${PADDLE_ROOT} \
  --kernel_info_file $kernel_register_info_file \
W
Wilber 已提交
127
  --infermeta_wrap_file ${temp_path}/wrap_info.txt \
W
Wilber 已提交
128
  --attr_info_file $ir_attr_name_info_file \
129
  --generate_file ${PADDLE_ROOT}/paddle/infrt/kernel/phi/infershaped/infershaped_kernel_launchers.cc