| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved. |
| 4 | // |
| 5 | // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except |
| 6 | // in compliance with the License. You may obtain a copy of the License at |
| 7 | // |
| 8 | // https://opensource.org/licenses/BSD-3-Clause |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software distributed |
| 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 13 | // specific language governing permissions and limitations under the License. |
| 14 | |
| 15 | #ifndef NCNN_REWRITER_TD |
| 16 | #define NCNN_REWRITER_TD |
| 17 | |
| 18 | include "tf_ops.td" |
| 19 | include "ncnn_ops.td" |
| 20 | |
| 21 | def get_attr_f : NativeCodeCall<"$0.getValue<FloatAttr>(0)">; |
| 22 | |
| 23 | def OneElementAttrPred : CPred<"$_self.cast<ElementsAttr>().getType().getNumElements() == 1">; |
| 24 | |
| 25 | def OneElementAttr : ElementsAttrBase<And<[ElementsAttr.predicate, OneElementAttrPred]>, "Scalar ElementsAttr">; |
| 26 | |
| 27 | def EqualOperands : Constraint<CPred<"$0 == $1">>; |
| 28 | |
| 29 | def FuseBinaryOpPattern0 : Pat< |
| 30 | (TF_MulOp |
| 31 | $x, |
| 32 | (TF_ConstOp OneElementAttr:$b) |
| 33 | ), |
| 34 | |
| 35 | (NCNN_BinaryOpOp $x, ConstantAttr<I32Attr, "2">, ConstantAttr<I32Attr, "1">, (get_attr_f $b)) |
| 36 | >; |
| 37 | |
| 38 | def FuseBinaryOpPattern1 : Pat< |
| 39 | (TF_AddV2Op |
| 40 | $x, |
| 41 | (TF_ConstOp OneElementAttr:$b) |
| 42 | ), |
| 43 | |
| 44 | (NCNN_BinaryOpOp $x, ConstantAttr<I32Attr, "0">, ConstantAttr<I32Attr, "1">, (get_attr_f $b)) |
| 45 | >; |
| 46 | |
| 47 | def FuseKerasConv2DOpPattern : Pat< |
| 48 | (TF_BiasAddOp |
| 49 | (TF_Conv2DOp $x, $weight, $strides, $use_cudnn_on_gpu, $padding, $explicit_paddings, $data_format, $dilations), |
| 50 | $bias, |
| 51 | $data_format_ |
| 52 | ), |
| 53 | |
| 54 | (NCNN_KerasConv2DOp $x, $weight, $bias, $strides, $padding, $explicit_paddings, $dilations) |
| 55 | >; |
| 56 | |
| 57 | def FuseKerasConv2DOpPattern1 : Pat< |
| 58 | (TF_AddV2Op |
| 59 | (TF_Conv2DOp $x, $weight, $strides, $use_cudnn_on_gpu, $padding, $explicit_paddings, $data_format, $dilations), |
| 60 | $bias |
| 61 | ), |
| 62 | |
| 63 | (NCNN_KerasConv2DOp $x, $weight, $bias, $strides, $padding, $explicit_paddings, $dilations) |
| 64 | >; |
| 65 | |
| 66 | def FuseKerasDenseOpPattern : Pat< |
| 67 | (TF_BiasAddOp |
| 68 | (TF_MatMulOp $x, $weight, $transpose_a, $transpose_b), |
| 69 | $bias, |
| 70 | $data_format_ |
| 71 | ), |
| 72 | |
| 73 | (NCNN_KerasDenseOp $x, $weight, $bias) |
| 74 | >; |
| 75 | |
| 76 | def NonOneElementAttrPred : CPred<"$_self.cast<ElementsAttr>().getType().getNumElements() != 1">; |
| 77 | |
| 78 | def NonOneElementAttr : ElementsAttrBase<And<[ElementsAttr.predicate, NonOneElementAttrPred]>, "Non Scalar ElementsAttr">; |
| 79 | |
| 80 | def FuseKerasBatchNormOpPattern : Pat< |
| 81 | (TF_AddV2Op |
| 82 | (TF_MulOp |
| 83 | $x, |
| 84 | (TF_ConstOp:$gamma NonOneElementAttr) |
| 85 | ), |
| 86 | (TF_ConstOp:$bias NonOneElementAttr) |
| 87 | ), |
| 88 | |
| 89 | (NCNN_KerasBatchNormOp $x, $gamma, $bias) |
| 90 | >; |
| 91 | |
| 92 | def FuseInstanceNormPattern0 : Pat< |
| 93 | (TF_MulOp |
| 94 | (TF_RsqrtOp |
| 95 | (TF_AddV2Op |
| 96 | (TF_MeanOp |
| 97 | (TF_SquaredDifferenceOp |
| 98 | (TF_MeanOp:$mean |
| 99 | $x, |
| 100 | (TF_ConstOp:$reduce_axis ElementsAttr), |
| 101 | ConstBoolAttrTrue // keep_dims |
| 102 | ), |
| 103 | $x_ |
| 104 | ), |
| 105 | $reduce_axis_, |
| 106 | ConstBoolAttrTrue // keep_dims |
| 107 | ), |
| 108 | (TF_ConstOp ElementsAttr:$epsilon) |
| 109 | ) |
| 110 | ), |
| 111 | (TF_SubOp $x__, $mean_) |
| 112 | ), |
| 113 | |
| 114 | (NCNN_InstanceNormOp $x, (get_attr_f $epsilon)), |
| 115 | |
| 116 | [ |
| 117 | (EqualOperands $x, $x_), |
| 118 | (EqualOperands $x, $x__), |
| 119 | (EqualOperands $reduce_axis, $reduce_axis_), |
| 120 | (EqualOperands $mean, $mean_) |
| 121 | ] |
| 122 | >; |
| 123 | |
| 124 | def FuseInstanceNormPattern1 : Pat< |
| 125 | (TF_MulOp |
| 126 | (TF_RsqrtOp |
| 127 | (TF_AddV2Op |
| 128 | (TF_MeanOp |
| 129 | (TF_SquaredDifferenceOp |
| 130 | $x_, |
| 131 | (TF_MeanOp:$mean |
| 132 | $x, |
| 133 | (TF_ConstOp:$reduce_axis ElementsAttr), |
| 134 | ConstBoolAttrTrue // keep_dims |
| 135 | ) |
| 136 | ), |
| 137 | $reduce_axis_, |
| 138 | ConstBoolAttrTrue // keep_dims |
| 139 | ), |
| 140 | (TF_ConstOp ElementsAttr:$epsilon) |
| 141 | ) |
| 142 | ), |
| 143 | (TF_SubOp $x__, $mean_) |
| 144 | ), |
| 145 | |
| 146 | (NCNN_InstanceNormOp $x, (get_attr_f $epsilon)), |
| 147 | |
| 148 | [ |
| 149 | (EqualOperands $x, $x_), |
| 150 | (EqualOperands $x, $x__), |
| 151 | (EqualOperands $reduce_axis, $reduce_axis_), |
| 152 | (EqualOperands $mean, $mean_) |
| 153 | ] |
| 154 | >; |
| 155 | |
| 156 | def FuseInstanceNormAffinePattern : Pat< |
| 157 | (TF_ReshapeOp |
| 158 | (TF_AddV2Op |
| 159 | (TF_MulOp |
| 160 | $reshaped__, |
| 161 | (TF_MulOp:$rsqrt_var_eps_gamma |
| 162 | (TF_RsqrtOp |
| 163 | (TF_AddV2Op |
| 164 | (TF_MeanOp |
| 165 | (TF_SquaredDifferenceOp |
| 166 | $reshaped_, |
| 167 | (TF_MeanOp:$mean |
| 168 | (TF_ReshapeOp:$reshaped $x, (TF_ConstOp ElementsAttr)), |
| 169 | (TF_ConstOp:$reduce_axis ElementsAttr), |
| 170 | ConstBoolAttrTrue // keep_dims |
| 171 | ) |
| 172 | ), |
| 173 | $reduce_axis_, |
| 174 | ConstBoolAttrTrue // keep_dims |
| 175 | ), |
| 176 | (TF_ConstOp ElementsAttr:$epsilon) |
| 177 | ) |
| 178 | ), |
| 179 | $gamma |
| 180 | ) |
| 181 | ), |
| 182 | (TF_SubOp |
| 183 | $beta, |
| 184 | (TF_MulOp $rsqrt_var_eps_gamma_, $mean_) |
| 185 | ) |
| 186 | ), |
| 187 | (TF_ConstOp ElementsAttr) |
| 188 | ), |
| 189 | |
| 190 | (NCNN_InstanceNormAffineOp $x, $gamma, $beta, (get_attr_f $epsilon)), |
| 191 | |
| 192 | [ |
| 193 | (EqualOperands $reshaped, $reshaped_), |
| 194 | (EqualOperands $reshaped, $reshaped__), |
| 195 | (EqualOperands $reduce_axis, $reduce_axis_), |
| 196 | (EqualOperands $rsqrt_var_eps_gamma, $rsqrt_var_eps_gamma_), |
| 197 | (EqualOperands $mean, $mean_) |
| 198 | ] |
| 199 | >; |
| 200 | |
| 201 | def FuseSwishPattern : Pat< |
| 202 | (TF_MulOp |
| 203 | (TF_SigmoidOp $x), |
| 204 | $x |
| 205 | ), |
| 206 | |
| 207 | (NCNN_SwishOp $x) |
| 208 | >; |
| 209 | |
| 210 | #endif // NCNN_REWRITER_TD |