-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathoptimize_patterns_common.td
182 lines (171 loc) · 6.78 KB
/
optimize_patterns_common.td
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
include "mlir/Dialect/StandardOps/IR/Ops.td"
include "tensorflow/compiler/mlir/lite/ir/tfl_ops.td"
include "larq_compute_engine/mlir/ir/lce_ops.td"
def F32ElementsAttr : ElementsAttrBase<
CPred<"$_self.cast<ElementsAttr>().getType().getElementType().isF32()">, "float constant tensor">;
// Checks if the value has only one user.
def HasOneUse : Constraint<CPred<"$0.hasOneUse()">>;
class ConstantValue<string val> : AttrConstraint<CPred<"IsConstantValue($_self, " # val # ")">>;
def : Pat<(LQ_QuantizeOp
(TFL_GreaterEqualOp:$ge_op
$input,
(ConstantOp ConstantValue<"0.0f">))),
(LQ_QuantizeOp $input),
[(HasOneUse $ge_op)],
(addBenefit 150)>;
def : Pat<(LQ_QuantizeOp
(TFL_GreaterEqualOp:$ge_op
$input,
$threshold)),
(LQ_QuantizeOp
(TFL_SubOp $input, $threshold, TFL_AF_None)),
[(HasOneUse $ge_op)],
(addBenefit 100)>;
def : Pat<(LQ_QuantizeOp
(TFL_LessEqualOp:$ge_op $lhs, $rhs)),
(LQ_QuantizeOp
(TFL_GreaterEqualOp $rhs, $lhs)),
[(HasOneUse $ge_op)],
(addBenefit 100)>;
// TODO: Check shapes before fusing
multiclass FuseAddOrSubWithBConv2D<Op binaryOp> {
def : Pat<(binaryOp
(LQ_Bconv2dOp:$output
$input,
$filter,
$post_activation_multiplier,
(ConstantOp F32ElementsAttr:$post_activation_bias),
$output_threshold,
$channels_in,
$dilation_height_factor,
$dilation_width_factor,
$fused_activation_function,
$pad_values,
$padding,
$stride_height,
$stride_width),
(ConstantOp F32ElementsAttr:$value), TFL_AF_None),
(LQ_Bconv2dOp
$input,
$filter,
$post_activation_multiplier,
(binaryOp (ConstantOp $post_activation_bias),
(ConstantOp $value), TFL_AF_None),
$output_threshold,
$channels_in,
$dilation_height_factor,
$dilation_width_factor,
$fused_activation_function,
$pad_values,
$padding,
$stride_height,
$stride_width),
[(HasOneUse $output)], (addBenefit 100)>;
}
foreach binaryOp = [TFL_AddOp, TFL_SubOp] in
defm : FuseAddOrSubWithBConv2D<binaryOp>;
// TODO: Check shapes before fusing
multiclass FuseMulOrDivWithBConv2D<Op binaryOp> {
def : Pat<(binaryOp
(LQ_Bconv2dOp:$conv_output
$input,
$filter,
(ConstantOp F32ElementsAttr:$post_activation_multiplier),
(ConstantOp F32ElementsAttr:$post_activation_bias),
$output_threshold,
$channels_in,
$dilation_height_factor,
$dilation_width_factor,
$fused_activation_function,
$pad_values,
$padding,
$stride_height,
$stride_width),
(ConstantOp F32ElementsAttr:$value), TFL_AF_None),
(LQ_Bconv2dOp
$input,
$filter,
(binaryOp (ConstantOp $post_activation_multiplier),
(ConstantOp $value), TFL_AF_None),
(binaryOp (ConstantOp $post_activation_bias),
(ConstantOp $value), TFL_AF_None),
$output_threshold,
$channels_in,
$dilation_height_factor,
$dilation_width_factor,
$fused_activation_function,
$pad_values,
$padding,
$stride_height,
$stride_width),
[(HasOneUse $conv_output)], (addBenefit 100)>;
}
foreach binaryOp = [TFL_DivOp, TFL_MulOp] in
defm : FuseMulOrDivWithBConv2D<binaryOp>;
// Fuse an activation function into the BConv2D.
multiclass FuseActFnIntoConvOpPat<Op ActFnOp, Attr ActFnAttr> {
def : Pat<(ActFnOp
(LQ_Bconv2dOp:$conv_output
$input,
$filter,
(ConstantOp ConstantValue<"1.0f">:$post_activation_multiplier),
(ConstantOp ConstantValue<"0.0f">:$post_activation_bias),
$output_threshold,
$channels_in,
$dilation_height_factor,
$dilation_width_factor,
TFL_AF_None,
$pad_values,
ConstantAttr<StrAttr, "VALID">:$padding,
$stride_height,
$stride_width)),
(LQ_Bconv2dOp
$input,
$filter,
(ConstantOp $post_activation_multiplier),
(ConstantOp $post_activation_bias),
$output_threshold,
$channels_in,
$dilation_height_factor,
$dilation_width_factor,
ActFnAttr,
$pad_values,
$padding,
$stride_height,
$stride_width),
[(HasOneUse $conv_output)], (addBenefit 100)>;
def : Pat<(ActFnOp
(LQ_Bconv2dOp:$conv_output
$input,
$filter,
(ConstantOp ConstantValue<"1.0f">:$post_activation_multiplier),
(ConstantOp ConstantValue<"0.0f">:$post_activation_bias),
$output_threshold,
$channels_in,
$dilation_height_factor,
$dilation_width_factor,
TFL_AF_None,
ConstantAttr<I32Attr, "1">:$pad_values,
ConstantAttr<StrAttr, "SAME">:$padding,
$stride_height,
$stride_width)),
(LQ_Bconv2dOp
$input,
$filter,
(ConstantOp $post_activation_multiplier),
(ConstantOp $post_activation_bias),
$output_threshold,
$channels_in,
$dilation_height_factor,
$dilation_width_factor,
ActFnAttr,
$pad_values,
$padding,
$stride_height,
$stride_width),
[(HasOneUse $conv_output)], (addBenefit 100)>;
}
foreach actFnPair = [[TFL_ReluOp, TFL_AF_Relu],
[TFL_Relu1Op, TFL_AF_Relu1],
[TFL_Relu6Op, TFL_AF_Relu6]] in
defm : FuseActFnIntoConvOpPat<!cast<Op>(actFnPair[0]), !cast<Attr>(actFnPair[1])>;