forked from onnx/onnx-mlir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDecompose.td
executable file
·347 lines (294 loc) · 12.5 KB
/
Decompose.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// SPDX-License-Identifier: Apache-2.0
//===- ONNXDecompose.td - Rewriting for decomposing ONNX Ops -*- tablegen -===//
//
// Copyright 2019-2020 The IBM Research Authors.
//
// =============================================================================
//
// Defines language-specific pattern match rewritings for ONNX using
// Declarative Rewrite Rules (DRR) specified using TableGen records.
//
//===----------------------------------------------------------------------===//
#ifndef ONNX_DECOMPOSE
#define ONNX_DECOMPOSE
#ifndef OP_BASE
include "src/Dialect/ONNX/ONNX.td"
#endif // OP_BASE
/// Note: The DRR definition used for defining patterns is shown below:
///
/// class Pattern<
/// dag sourcePattern, list<dag> resultPatterns,
/// list<dag> additionalConstraints = [],
/// dag benefitsAdded = (addBenefit 0)
/// >;
def KeepdimsTrue
: NativeCodeCall<"IntegerAttr::get($_builder.getIntegerType(64, "
"/*isSigned=*/true), APInt(64, 1, /*isSigned=*/true))">;
def KeepdimsIsTrue
: Constraint<CPred<"$_self.cast<IntegerAttr>().getSInt() == 1">,
"keepdims attribute is true">;
def AttributeIsNull : Constraint<CPred<"! ($_self)">, "Attribute is null">;
def HasFloatType : Constraint<CPred<"(($_self).getType().dyn_cast<ShapedType>()"
".getElementType().isF32())">>;
def IsNoneType : Constraint<CPred<"(($_self).getType().isa<NoneType>())">>;
def GetNullAttr : NativeCodeCall<"Attribute()">;
def GetNullFloatAttr :
NativeCodeCall<"FloatAttr()">;
def GetNullIntegerAttr :
NativeCodeCall<"IntegerAttr()">;
def GetNullStringAttr :
NativeCodeCall<"StringAttr()">;
// Create a unit constant that will be used as none input.
def CreateUnitConstant
: NativeCodeCall<"createUnitConstant($_builder, $_loc)">;
// Create a scalar DenseElementsAttr (rank 0) from a single attribute.
// E.g return type is tensor<f32> instead of tensor<0xf32> or tensor<1xf32>
def createScalarDenseAttrRank0
: NativeCodeCall<"createScalarDenseAttr($_builder, $0)">;
// Create a DenseElementsAttr from a single attribute.
def createDenseArrayAttrFromSingleAttr
: NativeCodeCall<"createDenseArrayAttr($_builder, $_builder.getArrayAttr($0))">;
// Create a DenseElementsAttr from an ArrayAttr.
def createDenseArrayAttr
: NativeCodeCall<"createDenseArrayAttr($_builder, $0)">;
def createDenseArrayAttrOrEmpty
: NativeCodeCall<"createDenseArrayAttrOrEmpty($_builder, $0)">;
def ScalerT : NativeCodeCall<"TypeAttr::get($_builder.getF32Type())">;
def noop_with_empty_axes
: NativeCodeCall<"IntegerAttr::get($_builder.getIntegerType(64, "
"/*isSigned=*/true), APInt(64, 0, /*isSigned=*/true))">;
// Check if value is a dense constant
def IsDenseONNXConstant:
Constraint<CPred<"isDenseONNXConstant($_self)">, "not a dense constant">;
// Create an ArrayAttr from a dense ConstantOp
def createArrayAttrFromConstantOp : NativeCodeCall<
"createArrayAttrFromConstantOp($_builder, $0)">;
def createSequenceConstructOp : NativeCodeCall<
"createSequenceConstructOp($_builder, $0, $1)">;
//===----------------------------------------------------------------------===//
// ONNXReduceL1Op %X = ONNXReduceSumOp (ONNXAbsOp %X)
//===----------------------------------------------------------------------===//
def ReduceL1OpPattern1
: Pat<(ONNXReduceL1Op $oprd, $axes, $keepdims),
(ONNXReduceSumOp(ONNXAbsOp $oprd),
(CreateUnitConstant), $keepdims, (noop_with_empty_axes)),
[(AttributeIsNull:$axes)], (addBenefit 1)>;
def ReduceL1OpPattern2
: Pat<(ONNXReduceL1Op $oprd, $axes, $keepdims),
(ONNXReduceSumOp(ONNXAbsOp $oprd),
(ONNXConstantOpFromDenseAttr(createDenseArrayAttr $axes)),
$keepdims, (noop_with_empty_axes)),
[], (addBenefit 0)>;
//===----------------------------------------------------------------------===//
// ONNXReduceL2Op %X = ONNXSqrtOp (ONNXReduceSumSquareOp (%X))
//===----------------------------------------------------------------------===//
def ReduceL2OpPattern
: Pat<(ONNXReduceL2Op $oprd, $axes, $keepdims),
(ONNXSqrtOp(ONNXReduceSumSquareOp $oprd, $axes, $keepdims))>;
//===----------------------------------------------------------------------===//
// ONNXReduceLogSumOp %X = ONNXLogOp (ONNXReduceSumOp (%X))
//===----------------------------------------------------------------------===//
def ReduceLogSumOpPattern1
: Pat<(ONNXReduceLogSumOp $oprd, $axes, $keepdims),
(ONNXLogOp(ONNXReduceSumOp $oprd,
(CreateUnitConstant), $keepdims, (noop_with_empty_axes))),
[(AttributeIsNull:$axes)], (addBenefit 1)>;
def ReduceLogSumOpPattern2
: Pat<(ONNXReduceLogSumOp $oprd, $axes, $keepdims),
(ONNXLogOp(ONNXReduceSumOp $oprd,
(ONNXConstantOpFromDenseAttr(createDenseArrayAttrOrEmpty $axes)),
$keepdims, (noop_with_empty_axes))),
[], (addBenefit 0)>;
//===----------------------------------------------------------------------===//
// ONNXReduceLogSumExpOp %X
//===----------------------------------------------------------------------===//
// keepdims is true
def ReduceLogSumExpOpPattern1
: Pat<(ONNXReduceLogSumExpOp $oprd, $axes, $keepdims),
(ONNXAddOp(ONNXReduceLogSumOp(ONNXExpOp(ONNXSubOp $oprd,
(ONNXReduceMaxOp
: $max $oprd, $axes, (KeepdimsTrue)))),
$axes, $keepdims),
$max),
[(KeepdimsIsTrue
: $keepdims)],
(addBenefit 1)>;
// keepdims is false
def ReduceLogSumExpOpPattern2
: Pat<(ONNXReduceLogSumExpOp $oprd, $axes, $keepdims),
(ONNXAddOp(ONNXReduceLogSumOp(ONNXExpOp(ONNXSubOp $oprd,
(ONNXReduceMaxOp
: $max $oprd, $axes, (KeepdimsTrue)))),
$axes, $keepdims),
(ONNXSqueezeV11Op $max, $axes)),
[], (addBenefit 0)>;
//===----------------------------------------------------------------------===//
// ONNXReduceSumSquareOp %X = ONNXReduceSumOp (ONNXMulOp %X, %X)
//===----------------------------------------------------------------------===//
def ReduceSumSquareOpPattern1
: Pat<(ONNXReduceSumSquareOp $oprd, $axes, $keepdims),
(ONNXReduceSumOp (ONNXMulOp $oprd, $oprd),
(CreateUnitConstant), $keepdims, (noop_with_empty_axes)),
[(AttributeIsNull:$axes)], (addBenefit 1)>;
def ReduceSumSquareOpPattern2
: Pat<(ONNXReduceSumSquareOp $oprd, $axes, $keepdims),
(ONNXReduceSumOp(ONNXMulOp $oprd, $oprd),
(ONNXConstantOpFromDenseAttr(createDenseArrayAttr $axes)),
$keepdims, (noop_with_empty_axes)),
[], (addBenefit 0)>;
//===----------------------------------------------------------------------===//
// ONNXScalerOp %X, %Offest, %Scale
// x input, a offset, b scale
//===----------------------------------------------------------------------===//
// No attribute
def ScalerNullPattern
: Pat<(ONNXScalerOp $x, $a, $b), (replaceWithValue $x),
[(HasFloatType
: $x),
(AttributeIsNull
: $a),
(AttributeIsNull
: $b)],
(addBenefit 5)>;
// No attribute, input x not float type
def ScalerNullPattern2
: Pat<(ONNXScalerOp $x, $a, $b), (ONNXCastOp $x, (ScalerT)),
[(AttributeIsNull
: $a),
(AttributeIsNull
: $b)],
(addBenefit 4)>;
// No scale
def ScalerNoScalePattern
: Pat<(ONNXScalerOp $x, $a, $b),
(ONNXSubOp $x,
(ONNXConstantOpFromDenseAttr(createDenseArrayAttr $a))),
[(HasFloatType
: $x),
(AttributeIsNull
: $b)],
(addBenefit 3)>;
// No scale, input x not float type
def ScalerNoScalePattern2
: Pat<(ONNXScalerOp $x, $a, $b),
(ONNXSubOp(ONNXCastOp $x, (ScalerT)),
(ONNXConstantOpFromDenseAttr(createDenseArrayAttr $a))),
[(AttributeIsNull
: $b)],
(addBenefit 2)>;
// No offset
def ScalerNoOffsetPattern
: Pat<(ONNXScalerOp $x, $a, $b),
(ONNXMulOp $x,
(ONNXConstantOpFromDenseAttr(createDenseArrayAttr $b))),
[(HasFloatType
: $x),
(AttributeIsNull
: $a)],
(addBenefit 3)>;
// No offset, input x not float type
def ScalerNoOffsetPattern2
: Pat<(ONNXScalerOp $x, $a, $b),
(ONNXMulOp(ONNXCastOp $x, (ScalerT)),
(ONNXConstantOpFromDenseAttr(createDenseArrayAttr $b))),
[(AttributeIsNull
: $a)],
(addBenefit 2)>;
// Normal ONNXScalerOp
def ScalerPattern
: Pat<(ONNXScalerOp $x, $a, $b),
(ONNXMulOp(ONNXSubOp $x,
(ONNXConstantOpFromDenseAttr(createDenseArrayAttr $a))),
(ONNXConstantOpFromDenseAttr(createDenseArrayAttr $b))),
[(HasFloatType
: $x)],
(addBenefit 1)>;
// Normal ONNXScalerOp, input x not float type
def ScalerPattern2
: Pat<(ONNXScalerOp $x, $a, $b),
(ONNXMulOp(ONNXSubOp(ONNXCastOp $x, (ScalerT)),
(ONNXConstantOpFromDenseAttr(createDenseArrayAttr $a))),
(ONNXConstantOpFromDenseAttr(createDenseArrayAttr $b))),
[], (addBenefit 0)>;
// Express LogSoftmax using Log and Softmax.
def LogSoftmaxPattern
: Pat<(ONNXLogSoftmaxOp $x, $axis), (ONNXLogOp(ONNXSoftmaxOp $x, $axis))>;
// Express Upsample using Resize.
def UpsamplePattern : Pat<
(ONNXUpsampleOp $x, $scales, $mode),
(ONNXResizeOp $x, (CreateUnitConstant), $scales, (CreateUnitConstant),
(GetNullStringAttr), (GetNullFloatAttr), (GetNullIntegerAttr),
(GetNullFloatAttr), $mode, (GetNullFloatAttr))
>;
def UpsampleV9Pattern : Pat<
(ONNXUpsampleV9Op $x, $scales, $mode),
(ONNXResizeOp $x, (CreateUnitConstant), $scales, (CreateUnitConstant),
(GetNullStringAttr), (GetNullFloatAttr), (GetNullIntegerAttr),
(GetNullFloatAttr), $mode, (GetNullFloatAttr))
>;
def UpsampleV7Pattern : Pat<
(ONNXUpsampleV7Op $x, $mode, $scales),
(ONNXResizeOp $x, (CreateUnitConstant),
(ONNXConstantOpFromDenseAttr(createDenseArrayAttr $scales)),
(CreateUnitConstant), (GetNullStringAttr), (GetNullFloatAttr),
(GetNullIntegerAttr), (GetNullFloatAttr), $mode, (GetNullFloatAttr))
>;
// Express PadV2 using the latest Pad version.
def PadV2Pattern : Pat<
(ONNXPadV2Op $x, $modeAttr, $padsAttr, $valueAttr),
(ONNXPadOp $x, (ONNXConstantOpFromDenseAttr(createDenseArrayAttr $padsAttr)),
(ONNXConstantOpFromDenseAttr(createDenseArrayAttrFromSingleAttr $valueAttr)),
$modeAttr)
>;
// Express PadV11 using the latest Pad version.
def PadV11Pattern : Pat<
(ONNXPadV11Op $x, $pads, $mode, $value),
(ONNXPadOp $x, $pads, $mode, $value)
>;
def ResizeV10Pattern: Pat<
(ONNXResizeV10Op:$res $x, $scales, $mode),
(ONNXResizeOp $x, (createNoneFloatConstant $res),
$scales, (createNoneIntegerConstant $res),
/*coordinate_transformation_mode*/(GetNullStringAttr),
/*cubic_coeff_a*/(GetNullFloatAttr),
/*exclude_outside*/(GetNullIntegerAttr),
/*exttrapolation_value*/(GetNullFloatAttr),
/*mode*/$mode,
/*nearest_mode*/(GetNullStringAttr))
>;
def ResizeV11Pattern: Pat<
(ONNXResizeV11Op:$res $x, $roi, $scales, $size,
$coordinate_transformation_node, $cubic_coeff_a,
$exclude_outside, $exttrapolation_value, $mode,
$nearest_mode),
(ONNXResizeOp $x, $roi, $scales, $size,
$coordinate_transformation_node, $cubic_coeff_a,
$exclude_outside, $exttrapolation_value, $mode,
$nearest_mode)
>;
// ToFix: Create type attribute for SequenceEmpty when type is not F32
// There is a mlirTypeToOnnxType in KrnlToLLVM conversion.
// Need to move it into ONNXOpHelper
def SequenceConstructPattern1: Pat<
(ONNXSequenceConstructOp:$res $x1),
(createSequenceConstructOp
(ONNXSequenceEmptyOp (GetNullIntegerAttr), (returnType $res)),
$x1)
>;
// Express Clip V6 using Clip V11.
def ClipV6Pattern : Pat<
(ONNXClipV6Op $x, $maxAttr, $minAttr),
(ONNXClipV11Op $x, (ONNXConstantOpFromDenseAttr(createScalarDenseAttrRank0 $minAttr)),
(ONNXConstantOpFromDenseAttr(createScalarDenseAttrRank0 $maxAttr)))
>;
// Express Clip V11 using Clip V12.
def ClipV11Pattern : Pat<
(ONNXClipV11Op $x, $min, $max),
(ONNXClipV12Op $x, $min, $max)
>;
// Express Clip V12 using Clip V13 (the lastest).
def ClipV12Pattern : Pat<
(ONNXClipV12Op $x, $min, $max),
(ONNXClipOp $x, $min, $max)
>;
#endif // ONNX_DECOMPOSE