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
|
//===- TosaMakeBroadcastable.cpp ------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Insert reshape to binary op's input if needed to match rank
//
//===----------------------------------------------------------------------===//
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/Dialect/Tensor/IR/Tensor.h"
#include "mlir/Dialect/Tosa/IR/TosaOps.h"
#include "mlir/Dialect/Tosa/Transforms/Passes.h"
#include "mlir/Dialect/Tosa/Utils/QuantUtils.h"
#include "mlir/Pass/Pass.h"
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
namespace mlir {
namespace tosa {
#define GEN_PASS_DEF_TOSAMAKEBROADCASTABLE
#include "mlir/Dialect/Tosa/Transforms/Passes.h.inc"
} // namespace tosa
} // namespace mlir
using namespace mlir;
using namespace mlir::tosa;
/// There are two potential ways implementing broadcast:
/// a. https://www.tensorflow.org/xla/broadcasting#formal_definition
/// b. https://numpy.org/doc/stable/user/basics.broadcasting.html
/// This pass implements b (numpy style) now.
/// In this pass, we insert RESHAPE operators to increase the rank of the
/// lower rank operand as a first step in the broadcasting process. The TOSA
/// operators that support broadcast require that the rank of the operands
/// are equal.
// Examples:
// If lower=[c], higher=[a, b, c], [c] reshaped into [1, 1, c].
// If lower=[b, c], higher=[a, b, c], [b, c] reshaped into [1, b, c].
// If lower=[a], higher=[a, a], [a] reshaped into [1, a].
// If lower=[a], target=[a, b, a], [a] reshaped into [1, 1, a].
// If lower=[], target=[a, b, c], [] reshaped into [1, 1, 1].
static LogicalResult
computeReshapeOutput(ArrayRef<int64_t> higherRankShape,
ArrayRef<int64_t> lowerRankShape,
SmallVectorImpl<int64_t> &reshapeOutputShape) {
// Initialize new shapes with [1] * higherRank.
int64_t higherRank = higherRankShape.size();
int64_t lowerRank = lowerRankShape.size();
reshapeOutputShape.assign(higherRank, 1);
int64_t higherRankDim;
int64_t lowerRankDim;
for (int64_t i = higherRank - 1, j = lowerRank - 1; i >= 0 && j >= 0;
i--, j--) {
higherRankDim = higherRankShape[i];
lowerRankDim = lowerRankShape[j];
if (lowerRankDim == 1 && higherRankDim > 1)
reshapeOutputShape[i] = 1;
else if ((lowerRankDim > 1 && higherRankDim == 1) ||
(lowerRankDim == higherRankDim))
reshapeOutputShape[i] = lowerRankDim;
else if (higherRankDim != lowerRankDim)
return failure();
}
return success();
}
/// Common code to create the reshape op where necessary to make the rank of the
/// operations equal. input1 and input2 will be updated when the rank has
/// changed. The caller is expected to use these to rewrite the original
/// operator with the RESHAPE now in the graph.
static LogicalResult reshapeLowerToHigher(PatternRewriter &rewriter,
Location loc,
RankedTensorType outputType,
Value &input1, Value &input2) {
auto input1Ty = input1.getType().dyn_cast<RankedTensorType>();
auto input2Ty = input2.getType().dyn_cast<RankedTensorType>();
if (!input1Ty || !input2Ty) {
return rewriter.notifyMatchFailure(loc, "input not a ranked tensor");
}
int64_t input1Rank = input1Ty.getRank();
int64_t input2Rank = input2Ty.getRank();
if (input1Rank == input2Rank)
return rewriter.notifyMatchFailure(loc,
"cannot rewrite as its already correct");
Value higherTensorValue, lowerTensorValue;
if (input1Rank > input2Rank) {
higherTensorValue = input1;
lowerTensorValue = input2;
} else {
higherTensorValue = input2;
lowerTensorValue = input1;
}
ArrayRef<int64_t> higherRankShape =
higherTensorValue.getType().cast<RankedTensorType>().getShape();
ArrayRef<int64_t> lowerRankShape =
lowerTensorValue.getType().cast<RankedTensorType>().getShape();
SmallVector<int64_t, 4> reshapeOutputShape;
if (computeReshapeOutput(higherRankShape, lowerRankShape, reshapeOutputShape)
.failed())
return rewriter.notifyMatchFailure(loc, "fail to compute a reshape type");
auto reshapeInputType = lowerTensorValue.getType().cast<RankedTensorType>();
auto reshapeOutputType = RankedTensorType::get(
ArrayRef<int64_t>(reshapeOutputShape), reshapeInputType.getElementType());
// Verify the rank agrees with the output type if the output type is ranked.
if (outputType) {
if (outputType.getShape().size() != reshapeOutputShape.size() ||
outputType.getShape().size() != higherRankShape.size())
return rewriter.notifyMatchFailure(
loc, "the reshaped type doesn't agrees with the ranked output type");
}
auto reshapeLower = rewriter.create<tosa::ReshapeOp>(
loc, reshapeOutputType, lowerTensorValue,
rewriter.getDenseI64ArrayAttr(reshapeOutputShape));
if (input1Rank > input2Rank) {
input1 = higherTensorValue;
input2 = reshapeLower.getResult();
} else {
input1 = reshapeLower.getResult();
input2 = higherTensorValue;
}
return success();
}
namespace {
template <typename OpTy>
struct ConvertTosaOp : public OpRewritePattern<OpTy> {
using OpRewritePattern<OpTy>::OpRewritePattern;
LogicalResult matchAndRewrite(OpTy tosaBinaryOp,
PatternRewriter &rewriter) const override {
Value input1 = tosaBinaryOp.getInput1();
Value input2 = tosaBinaryOp.getInput2();
Value output = tosaBinaryOp.getResult();
auto outputType = output.getType().dyn_cast<RankedTensorType>();
if (!outputType)
return failure();
if (reshapeLowerToHigher(rewriter, tosaBinaryOp.getLoc(), outputType,
input1, input2)
.failed())
return failure();
rewriter.replaceOpWithNewOp<OpTy>(tosaBinaryOp, outputType, input1, input2);
return success();
}
};
// The MulOp has an extra parameter 'shift' not present in other elementwise
// binary ops, that necessitates special handling of its builder.
template <>
struct ConvertTosaOp<tosa::MulOp> : public OpRewritePattern<tosa::MulOp> {
using OpRewritePattern<tosa::MulOp>::OpRewritePattern;
LogicalResult matchAndRewrite(tosa::MulOp tosaBinaryOp,
PatternRewriter &rewriter) const override {
Value input1 = tosaBinaryOp.getInput1();
Value input2 = tosaBinaryOp.getInput2();
int32_t shift = tosaBinaryOp.getShift();
Value output = tosaBinaryOp.getResult();
auto outputType = output.getType().dyn_cast<RankedTensorType>();
if (!outputType)
return failure();
if (reshapeLowerToHigher(rewriter, tosaBinaryOp.getLoc(), outputType,
input1, input2)
.failed())
return failure();
rewriter.replaceOpWithNewOp<tosa::MulOp>(tosaBinaryOp, outputType, input1,
input2, shift);
return success();
}
};
// The ArithmeticRightShiftOp has an extra parameter 'round' not present in
// other elementwise binary ops, that necessitates special handling of its
// builder.
template <>
struct ConvertTosaOp<tosa::ArithmeticRightShiftOp>
: public OpRewritePattern<tosa::ArithmeticRightShiftOp> {
using OpRewritePattern<tosa::ArithmeticRightShiftOp>::OpRewritePattern;
LogicalResult matchAndRewrite(tosa::ArithmeticRightShiftOp tosaBinaryOp,
PatternRewriter &rewriter) const override {
Value input1 = tosaBinaryOp.getInput1();
Value input2 = tosaBinaryOp.getInput2();
int32_t round = tosaBinaryOp.getRound();
Value output = tosaBinaryOp.getResult();
auto outputType = output.getType().dyn_cast<RankedTensorType>();
if (!outputType)
return failure();
if (reshapeLowerToHigher(rewriter, tosaBinaryOp.getLoc(), outputType,
input1, input2)
.failed())
return failure();
rewriter.replaceOpWithNewOp<tosa::ArithmeticRightShiftOp>(
tosaBinaryOp, outputType, input1, input2, round);
return success();
}
};
template <>
struct ConvertTosaOp<tosa::SelectOp> : public OpRewritePattern<tosa::SelectOp> {
using OpRewritePattern<tosa::SelectOp>::OpRewritePattern;
LogicalResult matchAndRewrite(tosa::SelectOp tosaOp,
PatternRewriter &rewriter) const override {
Value input1 = tosaOp.getPred();
Value input2 = tosaOp.getOnTrue();
Value input3 = tosaOp.getOnFalse();
Value output = tosaOp.getResult();
auto outputType = output.getType().dyn_cast<RankedTensorType>();
if (!outputType)
return rewriter.notifyMatchFailure(tosaOp, "output not a ranked tensor");
// Apply broadcasting to each pair of inputs separately, and chain them as
// compound as below so that the broadcasting happens all at once.
bool reshaped1 = reshapeLowerToHigher(rewriter, tosaOp.getLoc(), outputType,
input1, input2)
.succeeded();
bool reshaped2 = reshapeLowerToHigher(rewriter, tosaOp.getLoc(), outputType,
input1, input3)
.succeeded();
bool reshaped3 = reshapeLowerToHigher(rewriter, tosaOp.getLoc(), outputType,
input2, input3)
.succeeded();
if (!reshaped1 && !reshaped2 && !reshaped3)
return rewriter.notifyMatchFailure(
tosaOp,
"cannot rewrite as the rank of all operands is already aligned");
int32_t result1Rank = input1.getType().cast<RankedTensorType>().getRank();
int32_t result2Rank = input2.getType().cast<RankedTensorType>().getRank();
int32_t result3Rank = input3.getType().cast<RankedTensorType>().getRank();
if ((result1Rank != result2Rank) || (result2Rank != result3Rank))
return rewriter.notifyMatchFailure(
tosaOp, "not all ranks are aligned with each other");
rewriter.replaceOpWithNewOp<tosa::SelectOp>(tosaOp, outputType, input1,
input2, input3);
return success();
}
};
} // namespace
namespace {
/// Pass that enables broadcast by making all input arrays have the same
/// number of dimensions. Insert RESHAPE operations to lower rank operand
struct TosaMakeBroadcastable
: public tosa::impl::TosaMakeBroadcastableBase<TosaMakeBroadcastable> {
public:
void runOnOperation() override {
auto func = getOperation();
RewritePatternSet patterns(func.getContext());
MLIRContext *ctx = func.getContext();
// Add the generated patterns to the list.
patterns.add<ConvertTosaOp<tosa::BitwiseAndOp>>(ctx);
patterns.add<ConvertTosaOp<tosa::BitwiseOrOp>>(ctx);
patterns.add<ConvertTosaOp<tosa::BitwiseXorOp>>(ctx);
patterns.add<ConvertTosaOp<tosa::AddOp>>(ctx);
patterns.add<ConvertTosaOp<tosa::SubOp>>(ctx);
patterns.add<ConvertTosaOp<tosa::MulOp>>(ctx);
patterns.add<ConvertTosaOp<tosa::DivOp>>(ctx);
patterns.add<ConvertTosaOp<tosa::MaximumOp>>(ctx);
patterns.add<ConvertTosaOp<tosa::MinimumOp>>(ctx);
patterns.add<ConvertTosaOp<tosa::EqualOp>>(ctx);
patterns.add<ConvertTosaOp<tosa::GreaterOp>>(ctx);
patterns.add<ConvertTosaOp<tosa::GreaterEqualOp>>(ctx);
patterns.add<ConvertTosaOp<tosa::LogicalLeftShiftOp>>(ctx);
patterns.add<ConvertTosaOp<tosa::ArithmeticRightShiftOp>>(ctx);
patterns.add<ConvertTosaOp<tosa::LogicalRightShiftOp>>(ctx);
patterns.add<ConvertTosaOp<tosa::LogicalAndOp>>(ctx);
patterns.add<ConvertTosaOp<tosa::LogicalOrOp>>(ctx);
patterns.add<ConvertTosaOp<tosa::LogicalXorOp>>(ctx);
patterns.add<ConvertTosaOp<tosa::SelectOp>>(ctx);
patterns.add<ConvertTosaOp<tosa::PowOp>>(ctx);
(void)applyPatternsAndFoldGreedily(func, std::move(patterns));
}
};
} // namespace
std::unique_ptr<Pass> mlir::tosa::createTosaMakeBroadcastablePass() {
return std::make_unique<TosaMakeBroadcastable>();
}
|