1//===- TosaTestPasses.cpp -------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Test passes to exercise TOSA helper functions.
10//
11//===----------------------------------------------------------------------===//
12
13#include "mlir/Dialect/Func/IR/FuncOps.h"
14#include "mlir/Dialect/Tensor/IR/Tensor.h"
15#include "mlir/Dialect/Tosa/IR/TosaOps.h"
16#include "mlir/Dialect/Tosa/Utils/QuantUtils.h"
17#include "mlir/IR/BuiltinTypes.h"
18#include "mlir/IR/Matchers.h"
19#include "mlir/Pass/Pass.h"
20#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
21
22#define PASS_NAME "tosa-test-quant-utils"
23
24using namespace mlir;
25using namespace mlir::tosa;
26
27// This transformation converts quantized uint8 to quantized int8. The
28// construction of the new type invokes buildQTypeFromMinMax. Extracted from
29// TOSA legalization infrastructure.
30struct ConvertTosaNegateOp : public RewritePattern {
31 explicit ConvertTosaNegateOp(MLIRContext *context)
32 : RewritePattern(tosa::NegateOp::getOperationName(), 1, context) {}
33 LogicalResult matchAndRewrite(Operation *op,
34 PatternRewriter &rewriter) const override;
35};
36
37LogicalResult
38ConvertTosaNegateOp::matchAndRewrite(Operation *op,
39 PatternRewriter &rewriter) const {
40
41 auto tosaNegateOp = cast<tosa::NegateOp>(Val: op);
42
43 auto inputType =
44 dyn_cast<mlir::RankedTensorType>(Val: tosaNegateOp.getInput1().getType());
45 // skip if input is not ranked tensor type
46 if (!inputType)
47 return failure();
48
49 // skip if it's not ranked tensor type.
50 auto outputType =
51 dyn_cast<mlir::RankedTensorType>(Val: tosaNegateOp.getResult().getType());
52 if (!outputType)
53 return failure();
54
55 // skip if output is not per-tensor quantized type.
56 auto outputElementType =
57 dyn_cast<mlir::quant::UniformQuantizedType>(Val: outputType.getElementType());
58 if (!outputElementType)
59 return failure();
60
61 // skip if output is not uint8.
62 if (outputElementType.isSigned() ||
63 outputElementType.getStorageTypeIntegralWidth() != 8)
64 return failure();
65
66 double typeRangeMin = double(outputElementType.getStorageTypeMin() -
67 outputElementType.getZeroPoint()) *
68 outputElementType.getScale();
69 double typeRangeMax = double(outputElementType.getStorageTypeMax() -
70 outputElementType.getZeroPoint()) *
71 outputElementType.getScale();
72 bool narrowRange = outputElementType.getStorageTypeMin() == 1;
73
74 auto dstQConstType = RankedTensorType::get(
75 shape: outputType.getShape(),
76 elementType: buildQTypeFromMinMax(builder: rewriter, inputDType: outputElementType.getExpressedType(),
77 minAttr: rewriter.getF64FloatAttr(value: typeRangeMin),
78 maxAttr: rewriter.getF64FloatAttr(value: typeRangeMax),
79 quantBits: rewriter.getI32IntegerAttr(
80 value: outputElementType.getStorageTypeIntegralWidth()),
81 filterQuantDim: 0, isSigned: true /* signed */,
82 narrowRange: rewriter.getBoolAttr(value: narrowRange)));
83
84 ElementsAttr inputElems;
85 if (!matchPattern(value: tosaNegateOp.getInput1(), pattern: m_Constant(bind_value: &inputElems)))
86 return failure();
87
88 auto newConstOp =
89 tosa::ConstOp::create(builder&: rewriter, location: op->getLoc(), output: dstQConstType, values: inputElems);
90 auto newNegateOp = tosa::NegateOp::create(
91 builder&: rewriter, location: op->getLoc(), outputType: dstQConstType, input: newConstOp.getResult());
92
93 rewriter.replaceOp(op, newValues: {newNegateOp.getResult()});
94 return success();
95}
96
97// This transformation modifies the quantized output of a test conv2d input and
98// appends a TOSA rescale after it. The rescale op requires the invocation of
99// computeMultiplierAndShift. From TOSA legalization infrastructure.
100struct ConvertTosaConv2DOp : public RewritePattern {
101 explicit ConvertTosaConv2DOp(MLIRContext *context)
102 : RewritePattern(tosa::Conv2DOp::getOperationName(), 1, context) {}
103 LogicalResult matchAndRewrite(Operation *op,
104 PatternRewriter &rewriter) const override;
105};
106
107LogicalResult
108ConvertTosaConv2DOp::matchAndRewrite(Operation *op,
109 PatternRewriter &rewriter) const {
110
111 auto tosaConv2DOp = cast<tosa::Conv2DOp>(Val: op);
112
113 auto inputType =
114 dyn_cast<mlir::RankedTensorType>(Val: tosaConv2DOp.getInput().getType());
115
116 // skip if input is not ranked tensor type
117 if (!inputType)
118 return failure();
119
120 auto weightType =
121 dyn_cast<mlir::RankedTensorType>(Val: tosaConv2DOp.getWeight().getType());
122
123 // skip if wt is not ranked tensor type
124 if (!weightType)
125 return failure();
126
127 // skip if it's not ranked tensor type.
128 auto outputType =
129 dyn_cast<mlir::RankedTensorType>(Val: tosaConv2DOp.getResult().getType());
130 if (!outputType)
131 return failure();
132
133 auto inputQType =
134 dyn_cast<mlir::quant::UniformQuantizedType>(Val: inputType.getElementType());
135 auto weightQType =
136 dyn_cast<mlir::quant::UniformQuantizedType>(Val: weightType.getElementType());
137 auto outputQType =
138 dyn_cast<mlir::quant::UniformQuantizedType>(Val: outputType.getElementType());
139
140 // Works on quantized type only.
141 if (!(inputQType && weightQType && outputQType))
142 return failure();
143
144 auto newTosaConv2DOpType =
145 RankedTensorType::get(shape: outputType.getShape(), elementType: rewriter.getIntegerType(width: 32));
146
147 auto newTosaConv2DOp = tosa::Conv2DOp::create(
148 builder&: rewriter, location: op->getLoc(), outputType: newTosaConv2DOpType, input: tosaConv2DOp.getInput(),
149 weight: tosaConv2DOp.getWeight(), bias: tosaConv2DOp.getBias(),
150 pad: tosaConv2DOp.getPadAttr(), stride: tosaConv2DOp.getStrideAttr(),
151 dilation: tosaConv2DOp.getDilationAttr(), acc_type: tosaConv2DOp.getAccTypeAttr());
152
153 // Create rescale to quantized type
154 double inputScale = inputQType.getScale();
155 double weightScale = weightQType.getScale();
156 double outputScale = outputQType.getScale();
157 int64_t outputZpVal = outputQType.getZeroPoint();
158
159 auto inputZp =
160 createZeroPointTensor(builder&: rewriter, loc: op->getLoc(), srcElemType: newTosaConv2DOpType, zp: 0);
161 auto outputZp = createZeroPointTensor(
162 builder&: rewriter, loc: op->getLoc(), srcElemType: tosaConv2DOp.getOutput().getType(), zp: outputZpVal);
163
164 if (!inputZp || !outputZp)
165 return failure();
166
167 double opTensorScale = (inputScale * weightScale) / outputScale;
168
169 int32_t multiplier;
170 int32_t shift;
171
172 // Obtain the quantized scale = multiplier and shift.
173 if (!computeMultiplierAndShift(scale: opTensorScale, multiplier, shift, scaleWidth: 32))
174 return failure();
175
176 bool inputUnsigned =
177 newTosaConv2DOp.getResult().getType().isUnsignedInteger();
178 bool outputUnsigned = outputType.isUnsignedInteger();
179
180 RoundingModeAttr doubleRoundAttr =
181 RoundingModeAttr::get(context: rewriter.getContext(), value: RoundingMode::DOUBLE_ROUND);
182 auto newTosaRescaleOp = tosa::RescaleOp::create(
183 builder&: rewriter, location: op->getLoc(), output: outputType, input: newTosaConv2DOp.getResult(),
184 multiplier: getConstTensorInt<int32_t>(builder&: rewriter, loc: op->getLoc(), vec: {multiplier}),
185 shift: getConstTensorInt<int8_t>(builder&: rewriter, loc: op->getLoc(),
186 vec: {static_cast<int8_t>(shift)}),
187 input_zp: inputZp.value(), output_zp: outputZp.value(),
188 /* scale32 = */ rewriter.getBoolAttr(value: true),
189 /* double_round = */ rounding_mode: doubleRoundAttr,
190 /* per_channel = */ rewriter.getBoolAttr(value: false),
191 input_unsigned: rewriter.getBoolAttr(value: inputUnsigned),
192 output_unsigned: rewriter.getBoolAttr(value: outputUnsigned));
193
194 rewriter.replaceOp(op, newValues: {newTosaRescaleOp.getResult()});
195 return success();
196}
197
198namespace {
199
200struct TosaTestQuantUtilAPI
201 : public PassWrapper<TosaTestQuantUtilAPI, OperationPass<func::FuncOp>> {
202 MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TosaTestQuantUtilAPI)
203
204 StringRef getArgument() const final { return PASS_NAME; }
205 StringRef getDescription() const final {
206 return "TOSA Test: Exercise the APIs in QuantUtils.cpp.";
207 }
208 void runOnOperation() override;
209};
210
211void TosaTestQuantUtilAPI::runOnOperation() {
212 auto *ctx = &getContext();
213 RewritePatternSet patterns(ctx);
214 auto func = getOperation();
215
216 patterns.add<ConvertTosaNegateOp>(arg&: ctx);
217 patterns.add<ConvertTosaConv2DOp>(arg&: ctx);
218 (void)applyPatternsGreedily(op: func, patterns: std::move(patterns));
219}
220
221} // namespace
222
223namespace mlir {
224void registerTosaTestQuantUtilAPIPass() {
225 PassRegistration<TosaTestQuantUtilAPI>();
226}
227} // namespace mlir
228