MLIR 24.0.0git
QuantOps.cpp
Go to the documentation of this file.
1//===- QuantOps.cpp - Quantization Type and Ops Implementation --*- C++ -*-===//
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
10#include "TypeDetail.h"
11
18
19#include "mlir/Dialect/Quant/IR/QuantOpsDialect.cpp.inc"
20
21namespace mlir {
22namespace quant {
23
24namespace {
25
26// Verify the integrity of per-axis quantization information, if present.
27//
28// - uniformQuantizedPerAxisType
29// A quantized type with per-axis quantization.
30//
31// - containerType
32// Original input or result type of the operation using the provided quantized
33// type. Used to ensure that the quantized type appears within a tensor and
34// that the tensor is compatible with per-axis quantization information.
35//
36LogicalResult verifyPerAxisQuantization(
37 Operation *op, UniformQuantizedPerAxisType uniformQuantizedPerAxisType,
38 Type containerType) {
39 auto tensorType = dyn_cast<TensorType>(containerType);
40 if (!tensorType)
41 return op->emitError("scalar types may not use per-axis quantization");
42
43 if (!tensorType.hasRank())
44 return success();
45
46 int32_t quantizedDimension =
47 uniformQuantizedPerAxisType.getQuantizedDimension();
48 if ((int64_t)quantizedDimension >= tensorType.getRank())
49 return op->emitError("quantized dimension must be less than tensor rank");
50
51 int64_t quantizedDimensionSize = tensorType.getDimSize(quantizedDimension);
52 if (quantizedDimensionSize != ShapedType::kDynamic &&
53 quantizedDimensionSize !=
54 (int64_t)uniformQuantizedPerAxisType.getScales().size())
55 return op->emitError(
56 "quantized dimension size does not match number of scales");
57
58 return success();
59}
60
61// Verifies that the sub-channel quantization parameters are consistent with
62// the given container type. The function checks the following:
63//
64// - The container type must be a ranked tensor type.
65// - Each quantized dimension must be less than the rank of the tensor.
66// - The size of each dimension at the quantized dimension must be divisible
67// by the corresponding block size.
68// - The scale dimension size at each axis index should match the tensor
69// dimension at the index divided by the corresponding block size.
70//
71// The `uniformQuantizedSubChannelType` argument provides the sub-channel
72// quantization parameters, and the `containerType` argument specifies the
73// type of the container holding the quantized data.
74//
75LogicalResult verifySubChannelQuantization(
76 Operation *op,
77 UniformQuantizedSubChannelType uniformQuantizedSubChannelType,
78 Type containerType) {
79 auto tensorType = dyn_cast<TensorType>(containerType);
80 if (!tensorType)
81 return op->emitError("scalar types may not use sub-channel quantization");
82
83 if (!tensorType.hasRank())
84 return op->emitError(
85 "tensor containing the sub-channel quantized type must be ranked");
86
87 const SmallVector<std::pair<int32_t, int64_t>> &blockSizeInfo =
88 uniformQuantizedSubChannelType.getBlockSizeInfo();
89 auto shape = tensorType.getShape();
90
91 // The dimension size of scale for an axis which is not specified as quantized
92 // dimension should be 1.
93 SmallVector<int64_t> expectedScaleShape(tensorType.getShape().size(), 1);
94 for (auto [quantizedDimension, blockSize] : blockSizeInfo) {
95 if (quantizedDimension >= tensorType.getRank())
96 return op->emitError()
97 << "quantized dimension " << quantizedDimension
98 << " must be less than tensor rank " << tensorType.getRank();
99 if (!tensorType.isDynamicDim(quantizedDimension) &&
100 tensorType.getDimSize(quantizedDimension) % blockSize != 0)
101 return op->emitError()
102 << "tensor dimension size "
103 << tensorType.getDimSize(quantizedDimension) << " at axis "
104 << quantizedDimension
105 << " must be divisible by the corresponding block size "
106 << blockSize;
107 if (tensorType.isDynamicDim(quantizedDimension))
108 expectedScaleShape[quantizedDimension] = ShapedType::kDynamic;
109 else
110 expectedScaleShape[quantizedDimension] =
111 tensorType.getDimSize(quantizedDimension) / blockSize;
112 }
113
114 // Block sizes must be greater than 0 and divide the corresponding dimension
115 // size. While a block size b must be less than or equal to the corresponding
116 // dimension size d, this constraint is implicitly enforced by requiring that
117 // d % b == 0 when d != 0.
118 //
119 // However, a problem arises when d = 0. The divisibility constraint allows b
120 // to be any value, potentially violating the requirement that b <= d.
121 // Furthermore, if b is unspecified (implicitly equal to d), it violates the
122 // constraint that b > 0.
123 //
124 // Therefore, we explicitly disallow the case where d = 0 to maintain
125 // consistency and avoid these issues.
126 if (llvm::is_contained(tensorType.getShape(), 0)) {
127 return op->emitError() << "tensor dimension size of zero is not allowed "
128 "with sub-channel quantization";
129 }
130
131 auto scaleShape =
132 uniformQuantizedSubChannelType.getScales().getType().getShape();
133 if (scaleShape.size() != shape.size()) {
134 return op->emitError() << "Rank of scales " << scaleShape.size()
135 << " must match "
136 << "the rank of the tensor " << shape.size();
137 }
138
139 for (auto [index, scaleDim] : llvm::enumerate(expectedScaleShape)) {
140 if (expectedScaleShape[index] != ShapedType::kDynamic &&
141 expectedScaleShape[index] != scaleShape[index])
142 return op->emitError() << "dimension size " << scaleDim
143 << " of scales tensor at axis " << index
144 << " should match (tensor dimension at axis / "
145 "block sizes at axis) = "
146 << expectedScaleShape[index];
147 }
148
149 return success();
150}
151
152// Common verification logic for 'quant.dcast' and 'quant.qcast' ops.
153//
154// - quantizedType
155// Quantized type used in the input ('quant.dcast') or result ('quant.qcast'),
156// whether as a primitive type or in a tensor.
157//
158// - floatType
159// Float type used in the input ('quant.qcast') or result ('quant.dcast'),
160// whether as a primitive type or in a tensor.
161//
162// - containerType
163// Type of original input or result.
164//
165LogicalResult verifyQuantizationOp(Operation *op, QuantizedType quantizedType,
166 FloatType floatType, Type containerType) {
167 if (quantizedType.getExpressedType() != floatType)
168 return op->emitError(
169 "expressed type in quantized type expected to match float type");
170
171 // Verify integrity of per-axis quantization information, if present.
172 if (auto quantizedPerAxisType =
173 dyn_cast<UniformQuantizedPerAxisType>(quantizedType)) {
174 return verifyPerAxisQuantization(op, quantizedPerAxisType, containerType);
175 }
176
177 if (auto quantizedSubChannelType =
178 dyn_cast<UniformQuantizedSubChannelType>(quantizedType)) {
179 return verifySubChannelQuantization(op, quantizedSubChannelType,
180 containerType);
181 }
182
183 // At this point the type is UniformQuantizedType
184 return success();
185}
186
187struct QuantInlinerInterface : public DialectInlinerInterface {
188 using DialectInlinerInterface::DialectInlinerInterface;
189 /// All quant dialect ops can be inlined.
190 bool isLegalToInline(Operation *, Region *, bool, IRMapping &) const final {
191 return true;
192 }
193};
194
195} // namespace
196
197//===----------------------------------------------------------------------===//
198// Dialect
199//===----------------------------------------------------------------------===//
200
201void QuantDialect::initialize() {
204 QuantileType>();
206 addInterfaces<QuantInlinerInterface>();
207}
208
209//===----------------------------------------------------------------------===//
210// DequantizeCastOp
211//===----------------------------------------------------------------------===//
212
213LogicalResult DequantizeCastOp::verify() {
214 return verifyQuantizationOp(*this, getQuantizedType(), getFloatType(),
215 getInput().getType());
216}
217
218OpFoldResult DequantizeCastOp::fold(FoldAdaptor adaptor) {
219 // Matches x -> quant.qcast -> quant.dcast -> y, replacing the quant.dcast op
220 // with the value of x. Values x and y are guaranteed to be of the same type
221 // in this pattern.
222 auto srcQcastOp = getInput().getDefiningOp<QuantizeCastOp>();
223 if (!srcQcastOp)
224 return {};
225 assert(srcQcastOp.getInput().getType() == getType());
226 return srcQcastOp.getInput();
227}
228
229FloatType DequantizeCastOp::getFloatType() {
230 return cast<FloatType>(getElementTypeOrSelf(getResult().getType()));
231}
232
233QuantizedType DequantizeCastOp::getQuantizedType() {
234 return cast<QuantizedType>(getElementTypeOrSelf(getInput().getType()));
235}
236
237//===----------------------------------------------------------------------===//
238// QuantizeCastOp
239//===----------------------------------------------------------------------===//
240
241LogicalResult QuantizeCastOp::verify() {
242 return verifyQuantizationOp(*this, getQuantizedType(), getFloatType(),
243 getInput().getType());
244}
245
246OpFoldResult QuantizeCastOp::fold(FoldAdaptor adaptor) {
247 // Matches x -> quant.dcast -> quant.qcast -> y, replacing the quant.qcast op
248 // with the value of x if the casts invert each other. Contrary to the folding
249 // pattern in quant.dcast (i.e., x -> quant.qcast -> quant.dcast -> y), values
250 // x and y are not guaranteed to be of the same type here, as they may use
251 // different quantization parameters.
252 auto srcDcastOp = getInput().getDefiningOp<DequantizeCastOp>();
253 if (!srcDcastOp || srcDcastOp.getInput().getType() != getType())
254 return {};
255 return srcDcastOp.getInput();
256}
257
258FloatType QuantizeCastOp::getFloatType() {
259 return cast<FloatType>(getElementTypeOrSelf(getInput().getType()));
260}
261
262QuantizedType QuantizeCastOp::getQuantizedType() {
263 return cast<QuantizedType>(getElementTypeOrSelf(getResult().getType()));
264}
265
266//===----------------------------------------------------------------------===//
267// StorageCastOp
268//===----------------------------------------------------------------------===//
269
270LogicalResult StorageCastOp::verify() {
271 auto quantizedType = getQuantizedType();
272 auto integerType = getIntegerType();
273 if (quantizedType.getStorageType() != integerType)
274 return emitError(
275 "storage type in quantized type expected to match integer type");
276
277 // Verify integrity of per-axis quantization information, if available. While
278 // the quantization type may appear in the input or the result, their tensor
279 // shapes are guaranteed to be identical at this point.
280 if (auto quantizedPerAxisType =
281 dyn_cast<UniformQuantizedPerAxisType>(quantizedType)) {
282 return verifyPerAxisQuantization(*this, quantizedPerAxisType,
283 getInput().getType());
284 }
285
286 if (auto quantizedSunChannelType =
287 dyn_cast<UniformQuantizedSubChannelType>(quantizedType)) {
288 return verifySubChannelQuantization(*this, quantizedSunChannelType,
289 getInput().getType());
290 }
291
292 // At this point the type is UniformQuantizedType
293 return success();
294}
295
296OpFoldResult StorageCastOp::fold(FoldAdaptor adaptor) {
297 // Matches x -> quant.scast -> quant.scast -> y, replacing the second
298 // quant.scast with the value of x if the casts invert each other.
299 auto srcScastOp = getInput().getDefiningOp<StorageCastOp>();
300 if (!srcScastOp || srcScastOp.getInput().getType() != getType())
301 return {};
302 return srcScastOp.getInput();
303}
304
305IntegerType StorageCastOp::getIntegerType() {
306 auto inputScalarType = getElementTypeOrSelf(getInput().getType());
307 if (auto integerType = dyn_cast<IntegerType>(inputScalarType))
308 return integerType;
309
310 auto resultScalarType = getElementTypeOrSelf(getResult().getType());
311 return cast<IntegerType>(resultScalarType);
312}
313
314QuantizedType StorageCastOp::getQuantizedType() {
315 auto inputScalarType = getElementTypeOrSelf(getInput().getType());
316 if (auto quantizedType = dyn_cast<QuantizedType>(inputScalarType))
317 return quantizedType;
318
319 auto resultScalarType = getElementTypeOrSelf(getResult().getType());
320 return cast<QuantizedType>(resultScalarType);
321}
322
323} // namespace quant
324} // namespace mlir
325
326#define GET_OP_CLASSES
327#include "mlir/Dialect/Quant/IR/QuantOps.cpp.inc"
return success()
static bool isLegalToInline(InlinerInterface &interface, Region *src, Region *insertRegion, bool shouldCloneInlinedRegion, IRMapping &valueMapping)
Utility to check that all of the operations within 'src' can be inlined.
A quantized type that maps storage to/from expressed types in an unspecified way.
Definition QuantTypes.h:204
A quantized type that infers its range from given min/max values.
Definition QuantTypes.h:525
Base class for all quantized types known to this dialect.
Definition QuantTypes.h:51
Represents per-axis (also known as per-channel quantization).
Definition QuantTypes.h:325
Represents sub-channel (also known as blockwise quantization).
Definition QuantTypes.h:410
Represents a family of uniform, quantized types.
Definition QuantTypes.h:265
FloatType getFloatType(MLIRContext *context, unsigned width)
Returns a supported MLIR floating point type of the given bit width or null if the bit width is not s...
void addBytecodeInterface(QuantDialect *dialect)
Add the interfaces necessary for encoding the quantization dialect components in bytecode.
Include the generated interface declarations.
Type getType(OpFoldResult ofr)
Returns the int type of the integer in ofr.
Definition Utils.cpp:311
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
Type getElementTypeOrSelf(Type type)
Return the element type or return the type itself.