MLIR 22.0.0git
Quant.cpp
Go to the documentation of this file.
1//===- Quant.cpp - C Interface for Quant dialect --------------------------===//
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
14
15using namespace mlir;
16
18
19//===---------------------------------------------------------------------===//
20// QuantizedType
21//===---------------------------------------------------------------------===//
22
24 return isa<quant::QuantizedType>(unwrap(type));
25}
26
30
32 unsigned integralWidth) {
34 integralWidth);
35}
36
38 unsigned integralWidth) {
40 integralWidth);
41}
42
43MlirType mlirQuantizedTypeGetExpressedType(MlirType type) {
44 return wrap(cast<quant::QuantizedType>(unwrap(type)).getExpressedType());
45}
46
47unsigned mlirQuantizedTypeGetFlags(MlirType type) {
48 return cast<quant::QuantizedType>(unwrap(type)).getFlags();
49}
50
51bool mlirQuantizedTypeIsSigned(MlirType type) {
52 return cast<quant::QuantizedType>(unwrap(type)).isSigned();
53}
54
55MlirType mlirQuantizedTypeGetStorageType(MlirType type) {
56 return wrap(cast<quant::QuantizedType>(unwrap(type)).getStorageType());
57}
58
60 return cast<quant::QuantizedType>(unwrap(type)).getStorageTypeMin();
61}
62
64 return cast<quant::QuantizedType>(unwrap(type)).getStorageTypeMax();
65}
66
68 return cast<quant::QuantizedType>(unwrap(type)).getStorageTypeIntegralWidth();
69}
70
72 MlirType candidate) {
73 return cast<quant::QuantizedType>(unwrap(type))
74 .isCompatibleExpressedType(unwrap(candidate));
75}
76
80
82 MlirType candidate) {
83 return wrap(cast<quant::QuantizedType>(unwrap(type))
84 .castFromStorageType(unwrap(candidate)));
85}
86
87MlirType mlirQuantizedTypeCastToStorageType(MlirType type) {
89 cast<quant::QuantizedType>(unwrap(type))));
90}
91
93 MlirType candidate) {
94 return wrap(cast<quant::QuantizedType>(unwrap(type))
95 .castFromExpressedType(unwrap(candidate)));
96}
97
101
103 MlirType candidate) {
104 return wrap(cast<quant::QuantizedType>(unwrap(type))
105 .castExpressedToStorageType(unwrap(candidate)));
106}
107
108//===---------------------------------------------------------------------===//
109// AnyQuantizedType
110//===---------------------------------------------------------------------===//
111
112bool mlirTypeIsAAnyQuantizedType(MlirType type) {
113 return isa<quant::AnyQuantizedType>(unwrap(type));
114}
115
119
120MlirType mlirAnyQuantizedTypeGet(unsigned flags, MlirType storageType,
121 MlirType expressedType, int64_t storageTypeMin,
122 int64_t storageTypeMax) {
123 return wrap(quant::AnyQuantizedType::get(flags, unwrap(storageType),
124 unwrap(expressedType),
125 storageTypeMin, storageTypeMax));
126}
127
131
132//===---------------------------------------------------------------------===//
133// UniformQuantizedType
134//===---------------------------------------------------------------------===//
135
137 return isa<quant::UniformQuantizedType>(unwrap(type));
138}
139
143
144MlirType mlirUniformQuantizedTypeGet(unsigned flags, MlirType storageType,
145 MlirType expressedType, double scale,
146 int64_t zeroPoint, int64_t storageTypeMin,
147 int64_t storageTypeMax) {
149 flags, unwrap(storageType), unwrap(expressedType), scale, zeroPoint,
150 storageTypeMin, storageTypeMax));
151}
152
156
157double mlirUniformQuantizedTypeGetScale(MlirType type) {
158 return cast<quant::UniformQuantizedType>(unwrap(type)).getScale();
159}
160
162 return cast<quant::UniformQuantizedType>(unwrap(type)).getZeroPoint();
163}
164
166 return cast<quant::UniformQuantizedType>(unwrap(type)).isFixedPoint();
167}
168
169//===---------------------------------------------------------------------===//
170// UniformQuantizedPerAxisType
171//===---------------------------------------------------------------------===//
172
174 return isa<quant::UniformQuantizedPerAxisType>(unwrap(type));
175}
176
180
182 unsigned flags, MlirType storageType, MlirType expressedType,
183 intptr_t nDims, double *scales, int64_t *zeroPoints,
184 int32_t quantizedDimension, int64_t storageTypeMin,
185 int64_t storageTypeMax) {
187 flags, unwrap(storageType), unwrap(expressedType),
188 llvm::ArrayRef(scales, nDims), llvm::ArrayRef(zeroPoints, nDims),
189 quantizedDimension, storageTypeMin, storageTypeMax));
190}
191
195
197 return cast<quant::UniformQuantizedPerAxisType>(unwrap(type))
198 .getScales()
199 .size();
200}
201
203 return cast<quant::UniformQuantizedPerAxisType>(unwrap(type))
204 .getScales()[pos];
205}
206
208 intptr_t pos) {
209 return cast<quant::UniformQuantizedPerAxisType>(unwrap(type))
210 .getZeroPoints()[pos];
211}
212
214 return cast<quant::UniformQuantizedPerAxisType>(unwrap(type))
215 .getQuantizedDimension();
216}
217
219 return cast<quant::UniformQuantizedPerAxisType>(unwrap(type)).isFixedPoint();
220}
221
222//===---------------------------------------------------------------------===//
223// UniformQuantizedSubChannelType
224//===---------------------------------------------------------------------===//
225
227 return isa<quant::UniformQuantizedSubChannelType>(unwrap(type));
228}
229
233
235 unsigned flags, MlirType storageType, MlirType expressedType,
236 MlirAttribute scalesAttr, MlirAttribute zeroPointsAttr, intptr_t nDims,
237 int32_t *quantizedDimensions, int64_t *blockSizes, int64_t storageTypeMin,
238 int64_t storageTypeMax) {
239 auto scales = dyn_cast<mlir::DenseElementsAttr>(unwrap(scalesAttr));
240 auto zeroPoints = dyn_cast<mlir::DenseElementsAttr>(unwrap(zeroPointsAttr));
241
242 if (!scales || !zeroPoints) {
243 return {};
244 }
245
247 flags, unwrap(storageType), unwrap(expressedType), scales, zeroPoints,
248 llvm::ArrayRef<int32_t>(quantizedDimensions, nDims),
249 llvm::ArrayRef<int64_t>(blockSizes, nDims), storageTypeMin,
250 storageTypeMax));
251}
252
256
258 return cast<quant::UniformQuantizedSubChannelType>(unwrap(type))
259 .getBlockSizes()
260 .size();
261}
262
264 intptr_t pos) {
265 return cast<quant::UniformQuantizedSubChannelType>(unwrap(type))
266 .getQuantizedDimensions()[pos];
267}
268
270 intptr_t pos) {
271 return cast<quant::UniformQuantizedSubChannelType>(unwrap(type))
272 .getBlockSizes()[pos];
273}
274
276 return wrap(
277 cast<quant::UniformQuantizedSubChannelType>(unwrap(type)).getScales());
278}
279
281 return wrap(cast<quant::UniformQuantizedSubChannelType>(unwrap(type))
282 .getZeroPoints());
283}
284
285//===---------------------------------------------------------------------===//
286// CalibratedQuantizedType
287//===---------------------------------------------------------------------===//
288
290 return isa<quant::CalibratedQuantizedType>(unwrap(type));
291}
292
296
297MlirType mlirCalibratedQuantizedTypeGet(MlirType expressedType, double min,
298 double max) {
299 return wrap(
301}
302
306
308 return cast<quant::CalibratedQuantizedType>(unwrap(type)).getMin();
309}
310
312 return cast<quant::CalibratedQuantizedType>(unwrap(type)).getMax();
313}
static Value max(ImplicitLocOpBuilder &builder, Value value, Value bound)
static Value min(ImplicitLocOpBuilder &builder, Value value, Value bound)
double mlirUniformQuantizedTypeGetScale(MlirType type)
Returns the scale of the given uniform quantized type.
Definition Quant.cpp:157
MlirType mlirQuantizedTypeCastToExpressedType(MlirType type)
Casts from a type based on a quantized type to a corresponding typed based on the expressed type.
Definition Quant.cpp:98
double mlirUniformQuantizedPerAxisTypeGetScale(MlirType type, intptr_t pos)
Returns pos-th scale of the given quantized per-axis type.
Definition Quant.cpp:202
MlirStringRef mlirUniformQuantizedTypeGetName(void)
Definition Quant.cpp:153
MlirTypeID mlirUniformQuantizedTypeGetTypeID(void)
Definition Quant.cpp:140
bool mlirTypeIsAUniformQuantizedSubChannelType(MlirType type)
Returns true if the given type is a UniformQuantizedSubChannel.
Definition Quant.cpp:226
bool mlirQuantizedTypeIsCompatibleExpressedType(MlirType type, MlirType candidate)
Returns true if the candidate type is compatible with the given quantized type.
Definition Quant.cpp:71
MlirType mlirCalibratedQuantizedTypeGet(MlirType expressedType, double min, double max)
Creates an instance of CalibratedQuantizedType with the given parameters in the same context as expre...
Definition Quant.cpp:297
MlirTypeID mlirCalibratedQuantizedTypeGetTypeID(void)
Definition Quant.cpp:293
double mlirCalibratedQuantizedTypeGetMax(MlirType type)
Returns the max value of the given calibrated quantized type.
Definition Quant.cpp:311
MlirType mlirQuantizedTypeGetStorageType(MlirType type)
Returns the underlying type used to store the values.
Definition Quant.cpp:55
MlirType mlirQuantizedTypeCastFromStorageType(MlirType type, MlirType candidate)
Casts from a type based on the storage type of the given type to a corresponding type based on the gi...
Definition Quant.cpp:81
bool mlirTypeIsAUniformQuantizedType(MlirType type)
Returns true if the given type is a UniformQuantizedType.
Definition Quant.cpp:136
MlirAttribute mlirUniformQuantizedSubChannelTypeGetZeroPoints(MlirType type)
Returns the zero-points of the quantized type.
Definition Quant.cpp:280
MlirType mlirAnyQuantizedTypeGet(unsigned flags, MlirType storageType, MlirType expressedType, int64_t storageTypeMin, int64_t storageTypeMax)
Creates an instance of AnyQuantizedType with the given parameters in the same context as storageType ...
Definition Quant.cpp:120
bool mlirQuantizedTypeIsSigned(MlirType type)
Returns true if the given type is signed, false otherwise.
Definition Quant.cpp:51
MlirType mlirQuantizedTypeCastFromExpressedType(MlirType type, MlirType candidate)
Casts from a type based on the expressed type of the given type to a corresponding type based on the ...
Definition Quant.cpp:92
MlirType mlirQuantizedTypeCastToStorageType(MlirType type)
Casts from a type based on a quantized type to a corresponding typed based on the storage type.
Definition Quant.cpp:87
int64_t mlirUniformQuantizedTypeGetZeroPoint(MlirType type)
Returns the zero point of the given uniform quantized type.
Definition Quant.cpp:161
int32_t mlirUniformQuantizedPerAxisTypeGetQuantizedDimension(MlirType type)
Returns the index of the quantized dimension in the given quantized per-axis type.
Definition Quant.cpp:213
MlirStringRef mlirUniformQuantizedSubChannelTypeGetName(void)
Definition Quant.cpp:253
unsigned mlirQuantizedTypeGetSignedFlag()
Returns the bit flag used to indicate signedness of a quantized type.
Definition Quant.cpp:27
int32_t mlirUniformQuantizedSubChannelTypeGetQuantizedDimension(MlirType type, intptr_t pos)
Returns the quantized dimension at the given position.
Definition Quant.cpp:263
MlirType mlirQuantizedTypeGetExpressedType(MlirType type)
Gets the original type approximated by the given quantized type.
Definition Quant.cpp:43
MlirStringRef mlirCalibratedQuantizedTypeGetName(void)
Definition Quant.cpp:303
MlirTypeID mlirUniformQuantizedSubChannelTypeGetTypeID(void)
Definition Quant.cpp:230
MlirType mlirUniformQuantizedPerAxisTypeGet(unsigned flags, MlirType storageType, MlirType expressedType, intptr_t nDims, double *scales, int64_t *zeroPoints, int32_t quantizedDimension, int64_t storageTypeMin, int64_t storageTypeMax)
Creates an instance of UniformQuantizedPerAxisType with the given parameters in the same context as s...
Definition Quant.cpp:181
unsigned mlirQuantizedTypeGetStorageTypeIntegralWidth(MlirType type)
Returns the integral bitwidth that the storage type of the given quantized type can represent exactly...
Definition Quant.cpp:67
int64_t mlirQuantizedTypeGetStorageTypeMin(MlirType type)
Returns the minimum value that the storage type of the given quantized type can take.
Definition Quant.cpp:59
bool mlirTypeIsAUniformQuantizedPerAxisType(MlirType type)
Returns true if the given type is a UniformQuantizedPerAxisType.
Definition Quant.cpp:173
MlirType mlirQuantizedTypeCastExpressedToStorageType(MlirType type, MlirType candidate)
Casts from a type based on the expressed type of the given quantized type to equivalent type based on...
Definition Quant.cpp:102
int64_t mlirQuantizedTypeGetStorageTypeMax(MlirType type)
Returns the maximum value that the storage type of the given quantized type can take.
Definition Quant.cpp:63
bool mlirUniformQuantizedPerAxisTypeIsFixedPoint(MlirType type)
Returns true if the given uniform quantized per-axis type is fixed-point.
Definition Quant.cpp:218
MlirType mlirQuantizedTypeGetQuantizedElementType(MlirType type)
Returns the element type of the given quantized type as another quantized type.
Definition Quant.cpp:77
MlirAttribute mlirUniformQuantizedSubChannelTypeGetScales(MlirType type)
Returns the scales of the quantized type.
Definition Quant.cpp:275
intptr_t mlirUniformQuantizedPerAxisTypeGetNumDims(MlirType type)
Returns the number of axes in the given quantized per-axis type.
Definition Quant.cpp:196
MlirTypeID mlirAnyQuantizedTypeGetTypeID(void)
Definition Quant.cpp:116
MlirType mlirUniformQuantizedTypeGet(unsigned flags, MlirType storageType, MlirType expressedType, double scale, int64_t zeroPoint, int64_t storageTypeMin, int64_t storageTypeMax)
Creates an instance of UniformQuantizedType with the given parameters in the same context as storageT...
Definition Quant.cpp:144
bool mlirTypeIsAAnyQuantizedType(MlirType type)
Returns true if the given type is an AnyQuantizedType.
Definition Quant.cpp:112
int64_t mlirUniformQuantizedSubChannelTypeGetBlockSize(MlirType type, intptr_t pos)
Returns the block size at the given position.
Definition Quant.cpp:269
double mlirCalibratedQuantizedTypeGetMin(MlirType type)
Returns the min value of the given calibrated quantized type.
Definition Quant.cpp:307
unsigned mlirQuantizedTypeGetFlags(MlirType type)
Gets the flags associated with the given quantized type.
Definition Quant.cpp:47
MlirTypeID mlirUniformQuantizedPerAxisTypeGetTypeID(void)
Definition Quant.cpp:177
MlirType mlirUniformQuantizedSubChannelTypeGet(unsigned flags, MlirType storageType, MlirType expressedType, MlirAttribute scalesAttr, MlirAttribute zeroPointsAttr, intptr_t nDims, int32_t *quantizedDimensions, int64_t *blockSizes, int64_t storageTypeMin, int64_t storageTypeMax)
Creates a UniformQuantizedSubChannelType with the given parameters.
Definition Quant.cpp:234
int64_t mlirUniformQuantizedPerAxisTypeGetZeroPoint(MlirType type, intptr_t pos)
Returns pos-th zero point of the given quantized per-axis type.
Definition Quant.cpp:207
intptr_t mlirUniformQuantizedSubChannelTypeGetNumBlockSizes(MlirType type)
Returns the number of block sizes provided in type.
Definition Quant.cpp:257
bool mlirUniformQuantizedTypeIsFixedPoint(MlirType type)
Returns true if the given uniform quantized type is fixed-point.
Definition Quant.cpp:165
MlirStringRef mlirUniformQuantizedPerAxisTypeGetName(void)
Definition Quant.cpp:192
int64_t mlirQuantizedTypeGetDefaultMinimumForInteger(bool isSigned, unsigned integralWidth)
Returns the minimum possible value stored by a quantized type.
Definition Quant.cpp:31
bool mlirTypeIsACalibratedQuantizedType(MlirType type)
Returns true if the given type is a CalibratedQuantizedType.
Definition Quant.cpp:289
MlirStringRef mlirAnyQuantizedTypeGetName(void)
Definition Quant.cpp:128
int64_t mlirQuantizedTypeGetDefaultMaximumForInteger(bool isSigned, unsigned integralWidth)
Returns the maximum possible value stored by a quantized type.
Definition Quant.cpp:37
#define MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(Name, Namespace, ClassName)
static AnyQuantizedType get(unsigned flags, Type storageType, Type expressedType, int64_t storageTypeMin, int64_t storageTypeMax)
Gets an instance of the type with all parameters specified but not checked.
static constexpr StringLiteral name
Definition QuantTypes.h:208
static constexpr StringLiteral name
Definition QuantTypes.h:529
static CalibratedQuantizedType get(Type expressedType, double min, double max)
Gets an instance of the type with all parameters specified but not checked.
static Type castToStorageType(Type quantizedType)
Casts from a type based on a QuantizedType to a corresponding type based on the storageType (returns ...
static Type castToExpressedType(Type quantizedType)
Casts from a type based on QuantizedType to a corresponding type based on the expressedType (returns ...
static QuantizedType getQuantizedElementType(Type primitiveOrContainerType)
Returns the element type as a QuantizedType or nullptr if it is not a quantized type.
static int64_t getDefaultMaximumForInteger(bool isSigned, unsigned integralWidth)
Gets the maximum possible stored by a storageType.
Definition QuantTypes.h:78
static int64_t getDefaultMinimumForInteger(bool isSigned, unsigned integralWidth)
Gets the minimum possible stored by a storageType.
Definition QuantTypes.h:68
static constexpr StringLiteral name
Definition QuantTypes.h:329
static UniformQuantizedPerAxisType get(unsigned flags, Type storageType, Type expressedType, ArrayRef< double > scales, ArrayRef< int64_t > zeroPoints, int32_t quantizedDimension, int64_t storageTypeMin, int64_t storageTypeMax)
Gets an instance of the type with all parameters specified but not checked.
static constexpr StringLiteral name
Definition QuantTypes.h:414
static UniformQuantizedSubChannelType get(unsigned flags, Type storageType, Type expressedType, DenseElementsAttr scales, DenseElementsAttr zeroPoints, ArrayRef< int32_t > quantizedDimensions, ArrayRef< int64_t > blockSizes, int64_t storageTypeMin, int64_t storageTypeMax)
Gets an instance of the type with all parameters specified but not checked.
static constexpr StringLiteral name
Definition QuantTypes.h:269
static UniformQuantizedType get(unsigned flags, Type storageType, Type expressedType, double scale, int64_t zeroPoint, int64_t storageTypeMin, int64_t storageTypeMax)
Gets an instance of the type with all parameters specified but not checked.
MlirDiagnostic wrap(mlir::Diagnostic &diagnostic)
Definition Diagnostics.h:24
mlir::Diagnostic & unwrap(MlirDiagnostic diagnostic)
Definition Diagnostics.h:19
MLIR_CAPI_EXPORTED bool mlirTypeIsAQuantizedType(MlirType type)
Returns true if the given type is a quantization dialect type.
Definition Quant.cpp:23
Include the generated interface declarations.
A pointer to a sized fragment of a string, not necessarily null-terminated.
Definition Support.h:75