forked from onnx/onnx-mlir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ONNXToZHigh.cpp
399 lines (349 loc) · 16.1 KB
/
ONNXToZHigh.cpp
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
* SPDX-License-Identifier: Apache-2.0
*/
//====------ ONNXToZHigh.cpp - ONNX dialect to ZHigh lowering -------------===//
//
// Copyright 2019-2022 The IBM Research Authors.
//
// =============================================================================
//
// This file implements the lowering of ONNX operations to a combination of
// ONNX and ZHigh operations.
//
//===----------------------------------------------------------------------===//
#include "src/Accelerators/NNPA/Conversion/ONNXToZHigh/ONNXToZHigh.hpp"
#include "src/Accelerators/NNPA/Conversion/ONNXToZHigh/ONNXToZHighCommon.hpp"
#include "src/Accelerators/NNPA/Dialect/ZHigh/ZHighOps.hpp"
#include "src/Accelerators/NNPA/Pass/NNPAPasses.hpp"
#include "src/Conversion/ONNXToKrnl/RNN/RNNBase.hpp"
#include "src/Dialect/ONNX/DialectBuilder.hpp"
#include "src/Dialect/ONNX/ONNXDimAnalysis.hpp"
#include "src/Dialect/ONNX/ONNXOps.hpp"
#include "src/Dialect/ONNX/ONNXOps/ShapeHelper.hpp"
#include "src/Dialect/ONNX/Transforms/ShapeInference.hpp"
using namespace mlir;
//
// LSTM/GRU specific functions
//
namespace onnx_mlir {
ArrayAttr getLSTMGRUBiasSplitShape(
Location loc, PatternRewriter &rewriter, ArrayRef<int64_t> shapeR) {
int64_t hiddenSize = shapeR[2];
int splitNum = (shapeR[1] / shapeR[2]) * 2;
std::vector<int64_t> splitShape;
for (int i = 0; i < splitNum; i++) {
splitShape.emplace_back(hiddenSize);
}
return rewriter.getI64ArrayAttr(splitShape);
}
Value getLSTMGRUZDNNWeightFromONNXWeight(
Location loc, PatternRewriter &rewriter, Value weight, int isLSTM) {
int64_t splitNum = isLSTM ? 4 : 3;
RankedTensorType weightType = weight.getType().cast<RankedTensorType>();
Type elementType = weightType.getElementType();
ArrayRef<int64_t> weightShape = weightType.getShape();
int64_t direction = weightShape[0];
int64_t hiddenSize = weightShape[1] / splitNum;
int64_t weightHiddenSize = weightShape[1];
int64_t feature = weightShape[2];
SmallVector<int64_t, 3> transposeShape;
transposeShape.emplace_back(direction);
transposeShape.emplace_back(feature);
transposeShape.emplace_back(weightHiddenSize);
RankedTensorType transposeType =
RankedTensorType::get(transposeShape, elementType);
SmallVector<int64_t, 3> perms({0, 2, 1});
ArrayRef<int64_t> permArrayW(perms);
ArrayAttr permAttrW = rewriter.getI64ArrayAttr(permArrayW);
Value transposeOp =
rewriter.create<ONNXTransposeOp>(loc, transposeType, weight, permAttrW);
SmallVector<int64_t, 3> splitShape;
splitShape.emplace_back(direction);
splitShape.emplace_back(feature);
splitShape.emplace_back(hiddenSize);
Type splitType = RankedTensorType::get(splitShape, elementType);
int64_t axis = 2;
Value stickForOp;
if (isLSTM) {
SmallVector<Type, 4> splitTypes(splitNum, splitType);
ONNXSplitV11Op splitOp = rewriter.create<ONNXSplitV11Op>(
loc, splitTypes, transposeOp, axis, nullptr);
Value i_gate = splitOp.getResults()[0];
Value o_gate = splitOp.getResults()[1];
Value f_gate = splitOp.getResults()[2];
Value c_gate = splitOp.getResults()[3];
stickForOp = rewriter.create<zhigh::ZHighStickForLSTMOp>(
loc, f_gate, i_gate, c_gate, o_gate);
} else { // GRU
SmallVector<Type, 3> splitTypes(splitNum, splitType);
ONNXSplitV11Op splitOp = rewriter.create<ONNXSplitV11Op>(
loc, splitTypes, transposeOp, axis, nullptr);
Value z_gate = splitOp.getResults()[0];
Value r_gate = splitOp.getResults()[1];
Value h_gate = splitOp.getResults()[2];
stickForOp =
rewriter.create<zhigh::ZHighStickForGRUOp>(loc, z_gate, r_gate, h_gate);
}
return stickForOp;
}
Value getLSTMGRUGetY(
Location loc, PatternRewriter &rewriter, Value val, Value resY) {
Value noneValue;
if (isNoneValue(resY)) {
return noneValue;
}
return val;
}
Value getLSTMGRUGetYWithSequenceLens(Location loc, PatternRewriter &rewriter,
Value val, Value resY, Value sequenceLens, Value initialH) {
Value noneValue;
if (isNoneValue(resY)) {
return noneValue;
}
if (isNoneValue(sequenceLens))
return getLSTMGRUGetY(loc, rewriter, val, resY);
std::vector<Value> inputs = {val, sequenceLens, initialH};
return rewriter.create<zhigh::ZHighFixGRUYOp>(loc, resY.getType(), inputs);
}
Value getLSTMGRUGetYh(Location loc, PatternRewriter &rewriter, Value val,
Value resY, Value resYh, Value X, StringAttr direction) {
Value noneValue;
if (isNoneValue(resYh) || isNoneValue(val))
return noneValue;
ArrayRef<int64_t> shapeX = X.getType().cast<ShapedType>().getShape();
MultiDialectBuilder<OnnxBuilder> create(rewriter, loc);
// Generate Y_h for onnx.LSTM from hn_output for all timestep
Value minusOne = create.onnx.constantInt64({-1});
Value zero = create.onnx.constantInt64({0});
Value one = create.onnx.constantInt64({1});
// Use INT_MAX to get timestep dimension because timestep is the end of a
// dimension. INT_MAX is recommended in ONNX.Slice to slice to the end of a
// dimension with unknown size.
Value intMax = create.onnx.constantInt64({INT_MAX});
StringRef directionStr = direction.getValue();
ArrayRef<int64_t> resYhShape =
resYh.getType().cast<RankedTensorType>().getShape();
int64_t T = isNoneValue(resY) ? 1 : shapeX[0];
int64_t D = resYhShape[0];
int64_t B = resYhShape[1];
int64_t H = resYhShape[2];
Type elementType = resYh.getType().cast<ShapedType>().getElementType();
Value axis = zero;
Value step = one;
Value ret;
if (directionStr.equals_insensitive("forward") ||
directionStr.equals_insensitive("reverse")) {
Value start = directionStr.equals_insensitive("forward") ? minusOne : zero;
Value end = directionStr.equals_insensitive("forward") ? intMax : one;
Type sliceType = RankedTensorType::get({1, D, B, H}, elementType);
ONNXSliceOp sliceOp = rewriter.create<ONNXSliceOp>(
loc, sliceType, val, start, end, axis, step);
return rewriter.create<ONNXSqueezeV11Op>(
loc, resYh.getType(), sliceOp.getResult(), rewriter.getI64ArrayAttr(0));
} else if (directionStr.equals_insensitive("bidirectional")) {
Type splitType = RankedTensorType::get({T, 1, B, H}, elementType);
SmallVector<Type> splitTypes = {splitType, splitType};
ONNXSplitV11Op splitOp = rewriter.create<ONNXSplitV11Op>(
loc, splitTypes, val, /*splitAxis=*/1, nullptr);
Type sliceType = RankedTensorType::get({1, 1, B, H}, elementType);
Value fwdLastSlice = rewriter.create<ONNXSliceOp>(
loc, sliceType, splitOp.getResults()[0], minusOne, intMax, axis, step);
Value bkwFirstSlice = rewriter.create<ONNXSliceOp>(
loc, sliceType, splitOp.getResults()[1], zero, one, axis, step);
Type concatType = RankedTensorType::get({1, D, B, H}, elementType);
Value concatOp = rewriter.create<ONNXConcatOp>(loc, concatType,
ValueRange({fwdLastSlice, bkwFirstSlice}), /*concatAxis=*/1);
Type squeezeType = RankedTensorType::get({D, B, H}, elementType);
return rewriter.create<ONNXSqueezeV11Op>(
loc, squeezeType, concatOp, rewriter.getI64ArrayAttr(0));
} else {
llvm_unreachable("Invalid direction.");
}
return ret;
}
Value getLSTMGRUGetYhWithSequenceLens(Location loc, PatternRewriter &rewriter,
Value val, Value resY, Value resYh, Value X, StringAttr direction,
Value sequenceLens) {
Value noneValue;
if (isNoneValue(resYh) || isNoneValue(val))
return noneValue;
if (isNoneValue(sequenceLens))
return getLSTMGRUGetYh(loc, rewriter, val, resY, resYh, X, direction);
std::vector<Value> inputs = {val, sequenceLens};
return rewriter.create<zhigh::ZHighFixGRUYhOp>(loc, resYh.getType(), inputs);
}
Value getLSTMGRUGetYc(
Location loc, PatternRewriter &rewriter, Value val, Value resYc) {
Value noneValue;
if (isNoneValue(resYc))
return noneValue;
zhigh::ZHighUnstickOp unstickOp =
rewriter.create<zhigh::ZHighUnstickOp>(loc, val);
return rewriter.create<ONNXSqueezeV11Op>(
loc, resYc.getType(), unstickOp.getResult(), rewriter.getI64ArrayAttr(0));
}
SmallVector<Value, 4> emitONNXSplitOp(Location loc, PatternRewriter &rewriter,
Value input, IntegerAttr axis, ArrayAttr split) {
Type elementType = input.getType().cast<ShapedType>().getElementType();
SmallVector<mlir::Type> outputTypes;
int64_t splitNum = split.size();
ArrayRef<int64_t> inputShape =
input.getType().cast<RankedTensorType>().getShape();
int64_t splitAxis = axis.cast<IntegerAttr>().getSInt();
assert(splitAxis >= 0 && "Negative axis");
for (int i = 0; i < splitNum; i++) {
SmallVector<int64_t> outputShape;
for (size_t dim = 0; dim < inputShape.size(); dim++) {
outputShape.emplace_back((dim == (unsigned int)splitAxis)
? split[dim].cast<IntegerAttr>().getInt()
: inputShape[dim]);
}
outputTypes.emplace_back(RankedTensorType::get(outputShape, elementType));
}
ONNXSplitV11Op splitOp =
rewriter.create<ONNXSplitV11Op>(loc, outputTypes, input, axis, split);
return splitOp.getResults();
}
/// Get kernelShapes using shape helper
template <typename OP, typename OPAdaptor, typename OPShapeHelper>
SmallVector<int64_t, 2> getArrayKernelShape(OP op) {
OPShapeHelper shapeHelper(op.getOperation(), {});
shapeHelper.computeShapeAndAssertOnFailure();
// Check if kernelShape is literal. Only static value is supported.
assert((llvm::any_of(shapeHelper.kernelShape, [](IndexExpr val) {
return val.isLiteral();
})) && "Only support static kernel_shape ");
SmallVector<int64_t, 2> kernelShapesRet;
kernelShapesRet.emplace_back(shapeHelper.kernelShape[0].getLiteral());
kernelShapesRet.emplace_back(shapeHelper.kernelShape[1].getLiteral());
return kernelShapesRet;
}
/// Get strides using shape helper
template <typename OP, typename OPAdaptor, typename OPShapeHelper>
SmallVector<int64_t, 2> getArrayStrides(OP op) {
OPShapeHelper shapeHelper(op.getOperation(), {});
shapeHelper.computeShapeAndAssertOnFailure();
return shapeHelper.strides;
}
//===----------------------------------------------------------------------===//
// ONNX to ZHigh Lowering Pass
//===----------------------------------------------------------------------===//
namespace {
/// Include the patterns defined in the Declarative Rewrite framework.
#include "src/Accelerators/NNPA/Conversion/ONNXToZHigh/ONNXONNXToZHigh.inc"
// Enhance 'replaceONNXSumOpPatternRecursion' to allow operating recursively.
struct ONNXSumOpPatternEnhancedRecursion
: public replaceONNXSumOpPatternRecursion {
ONNXSumOpPatternEnhancedRecursion(MLIRContext *context)
: replaceONNXSumOpPatternRecursion(context) {}
void initialize() {
// This pattern recursively unpacks one variadic operand at a time. The
// recursion bounded as the number of variadic operands is strictly
// decreasing.
setHasBoundedRewriteRecursion(true);
}
};
struct ONNXToZHighLoweringPass
: public PassWrapper<ONNXToZHighLoweringPass, OperationPass<ModuleOp>> {
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(ONNXToZHighLoweringPass)
StringRef getArgument() const override { return "convert-onnx-to-zhigh"; }
StringRef getDescription() const override {
return "Lower ONNX ops to ZHigh ops.";
}
// Make sure that we have a valid default constructor and copy
// constructor to make sure that the options are initialized properly.
ONNXToZHighLoweringPass() = default;
ONNXToZHighLoweringPass(const ONNXToZHighLoweringPass &pass)
: PassWrapper<ONNXToZHighLoweringPass, OperationPass<ModuleOp>>() {}
void runOnOperation() final;
};
} // end anonymous namespace.
void getONNXToZHighOneOpPatterns(RewritePatternSet &patterns) {
MLIRContext *context = patterns.getContext();
populateWithGenerated(patterns);
patterns.insert<ONNXSumOpPatternEnhancedRecursion>(context);
}
void getONNXToZHighOneOpDynamicallyLegal(
ConversionTarget *target, const DimAnalysis *dimAnalysis) {
addDynamicallyLegalOpFor<ONNXAddOp>(target, dimAnalysis);
addDynamicallyLegalOpFor<ONNXSubOp>(target, dimAnalysis);
addDynamicallyLegalOpFor<ONNXMulOp>(target, dimAnalysis);
addDynamicallyLegalOpFor<ONNXDivOp>(target, dimAnalysis);
addDynamicallyLegalOpFor<ONNXSumOp>(target, dimAnalysis);
addDynamicallyLegalOpFor<ONNXMinOp>(target, dimAnalysis);
addDynamicallyLegalOpFor<ONNXMaxOp>(target, dimAnalysis);
addDynamicallyLegalOpFor<ONNXReluOp>(target, dimAnalysis);
addDynamicallyLegalOpFor<ONNXTanhOp>(target, dimAnalysis);
addDynamicallyLegalOpFor<ONNXSigmoidOp>(target, dimAnalysis);
addDynamicallyLegalOpFor<ONNXLogOp>(target, dimAnalysis);
addDynamicallyLegalOpFor<ONNXExpOp>(target, dimAnalysis);
addDynamicallyLegalOpFor<ONNXSoftmaxOp>(target, dimAnalysis);
addDynamicallyLegalOpFor<ONNXMaxPoolSingleOutOp>(target, dimAnalysis);
addDynamicallyLegalOpFor<ONNXAveragePoolOp>(target, dimAnalysis);
addDynamicallyLegalOpFor<ONNXMatMulOp>(target, dimAnalysis);
addDynamicallyLegalOpFor<ONNXGemmOp>(target, dimAnalysis);
addDynamicallyLegalOpFor<ONNXReduceMeanV13Op>(target, dimAnalysis);
addDynamicallyLegalOpFor<ONNXLSTMOp>(target, dimAnalysis);
addDynamicallyLegalOpFor<ONNXGRUOp>(target, dimAnalysis);
addDynamicallyLegalOpFor<ONNXConvOp>(target, dimAnalysis);
}
void getONNXToZHighMultipleOpPatterns(RewritePatternSet &patterns) {
MLIRContext *context = patterns.getContext();
patterns.insert<replaceONNXMatMulAddPattern1>(context);
patterns.insert<replaceONNXMatMulAddPattern2>(context);
patterns.insert<replaceONNXReluConvPattern>(context);
patterns.insert<replaceONNXLogSoftmaxPattern>(context);
// Shape inference for newly-added operations.
getShapeInferencePatterns(patterns);
}
void ONNXToZHighLoweringPass::runOnOperation() {
ModuleOp module = getOperation();
// The first thing to define is the conversion target. This will define the
// final target for this lowering.
ConversionTarget target(getContext());
// Enable reporting on NNPA unsupported ops when specifying
// `--opt-report=NNPAUnsupportedOps`.
OnnxToZHighLoweringConfiguration::reportOnNNPAUnsupportedOps =
OnnxToZHighLoweringConfiguration::optReportNNPAUnsupportedOps;
// We define the specific operations, or dialects, that are legal targets for
// this lowering.
target.addLegalDialect<ONNXDialect, zhigh::ZHighDialect, KrnlDialect,
func::FuncDialect, arith::ArithDialect>();
// NOTE: if we change the order of calling combinedPatterns and single op
// patterns, make sure to change the order in DevicePlacement.cpp also to make
// them synced.
// Combined ONNX ops to ZHigh lowering.
// There are some combinations of ONNX ops that can be lowering into a single
// ZHigh op, e.g. ONNXMatMul and ONNXAdd can be lowered to ZHighMatmul.
// The lowering of such combinations should be done before the lowering of
// a single ONNX Op, because the single op lowering might have conditions that
// prohibit the combined ops lowering happened.
RewritePatternSet combinedPatterns(&getContext());
onnx_mlir::getONNXToZHighMultipleOpPatterns(combinedPatterns);
// It's ok to fail.
(void)applyPatternsAndFoldGreedily(module, std::move(combinedPatterns));
// Run the unknown dimension analysis to help check equality of unknown
// dimensions at compile time.
onnx_mlir::DimAnalysis dimAnalysis(module);
dimAnalysis.analyze();
// Single ONNX to ZHigh operation lowering.
RewritePatternSet patterns(&getContext());
onnx_mlir::getONNXToZHighOneOpPatterns(patterns);
// This is to make sure we don't want to alloc any MemRef at this high-level
// representation.
target.addIllegalOp<mlir::memref::AllocOp>();
target.addIllegalOp<mlir::memref::DeallocOp>();
// ONNX ops to ZHigh dialect under specific conditions.
// When adding a new op, need to implement a method, i.e. isSuitableForZDNN,
// for the op in ONNXLegalityCheck.cpp.
getONNXToZHighOneOpDynamicallyLegal(&target, &dimAnalysis);
// With the target and rewrite patterns defined, we can now attempt the
// conversion. The conversion will signal failure if any of our `illegal`
// operations were not converted successfully.
if (failed(applyPartialConversion(module, target, std::move(patterns))))
signalPassFailure();
}
std::unique_ptr<Pass> createONNXToZHighPass() {
return std::make_unique<ONNXToZHighLoweringPass>();
}
} // namespace onnx_mlir