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
116MlirType mlirAnyQuantizedTypeGet(unsigned flags, MlirType storageType,
117 MlirType expressedType, int64_t storageTypeMin,
118 int64_t storageTypeMax) {
119 return wrap(quant::AnyQuantizedType::get(flags, unwrap(storageType),
120 unwrap(expressedType),
121 storageTypeMin, storageTypeMax));
122}
123
124//===---------------------------------------------------------------------===//
125// UniformQuantizedType
126//===---------------------------------------------------------------------===//
127
129 return isa<quant::UniformQuantizedType>(unwrap(type));
130}
131
132MlirType mlirUniformQuantizedTypeGet(unsigned flags, MlirType storageType,
133 MlirType expressedType, double scale,
134 int64_t zeroPoint, int64_t storageTypeMin,
135 int64_t storageTypeMax) {
137 flags, unwrap(storageType), unwrap(expressedType), scale, zeroPoint,
138 storageTypeMin, storageTypeMax));
139}
140
141double mlirUniformQuantizedTypeGetScale(MlirType type) {
142 return cast<quant::UniformQuantizedType>(unwrap(type)).getScale();
143}
144
146 return cast<quant::UniformQuantizedType>(unwrap(type)).getZeroPoint();
147}
148
150 return cast<quant::UniformQuantizedType>(unwrap(type)).isFixedPoint();
151}
152
153//===---------------------------------------------------------------------===//
154// UniformQuantizedPerAxisType
155//===---------------------------------------------------------------------===//
156
158 return isa<quant::UniformQuantizedPerAxisType>(unwrap(type));
159}
160
162 unsigned flags, MlirType storageType, MlirType expressedType,
163 intptr_t nDims, double *scales, int64_t *zeroPoints,
164 int32_t quantizedDimension, int64_t storageTypeMin,
165 int64_t storageTypeMax) {
167 flags, unwrap(storageType), unwrap(expressedType),
168 llvm::ArrayRef(scales, nDims), llvm::ArrayRef(zeroPoints, nDims),
169 quantizedDimension, storageTypeMin, storageTypeMax));
170}
171
173 return cast<quant::UniformQuantizedPerAxisType>(unwrap(type))
174 .getScales()
175 .size();
176}
177
178double mlirUniformQuantizedPerAxisTypeGetScale(MlirType type, intptr_t pos) {
179 return cast<quant::UniformQuantizedPerAxisType>(unwrap(type))
180 .getScales()[pos];
181}
182
184 intptr_t pos) {
185 return cast<quant::UniformQuantizedPerAxisType>(unwrap(type))
186 .getZeroPoints()[pos];
187}
188
190 return cast<quant::UniformQuantizedPerAxisType>(unwrap(type))
191 .getQuantizedDimension();
192}
193
195 return cast<quant::UniformQuantizedPerAxisType>(unwrap(type)).isFixedPoint();
196}
197
198//===---------------------------------------------------------------------===//
199// UniformQuantizedSubChannelType
200//===---------------------------------------------------------------------===//
201
203 return isa<quant::UniformQuantizedSubChannelType>(unwrap(type));
204}
205
207 unsigned flags, MlirType storageType, MlirType expressedType,
208 MlirAttribute scalesAttr, MlirAttribute zeroPointsAttr, intptr_t nDims,
209 int32_t *quantizedDimensions, int64_t *blockSizes, int64_t storageTypeMin,
210 int64_t storageTypeMax) {
211 auto scales = dyn_cast<mlir::DenseElementsAttr>(unwrap(scalesAttr));
212 auto zeroPoints = dyn_cast<mlir::DenseElementsAttr>(unwrap(zeroPointsAttr));
213
214 if (!scales || !zeroPoints) {
215 return {};
216 }
217
219 flags, unwrap(storageType), unwrap(expressedType), scales, zeroPoints,
220 llvm::ArrayRef<int32_t>(quantizedDimensions, nDims),
221 llvm::ArrayRef<int64_t>(blockSizes, nDims), storageTypeMin,
222 storageTypeMax));
223}
224
226 return cast<quant::UniformQuantizedSubChannelType>(unwrap(type))
227 .getBlockSizes()
228 .size();
229}
230
232 intptr_t pos) {
233 return cast<quant::UniformQuantizedSubChannelType>(unwrap(type))
234 .getQuantizedDimensions()[pos];
235}
236
238 intptr_t pos) {
239 return cast<quant::UniformQuantizedSubChannelType>(unwrap(type))
240 .getBlockSizes()[pos];
241}
242
244 return wrap(
245 cast<quant::UniformQuantizedSubChannelType>(unwrap(type)).getScales());
246}
247
249 return wrap(cast<quant::UniformQuantizedSubChannelType>(unwrap(type))
250 .getZeroPoints());
251}
252
253//===---------------------------------------------------------------------===//
254// CalibratedQuantizedType
255//===---------------------------------------------------------------------===//
256
258 return isa<quant::CalibratedQuantizedType>(unwrap(type));
259}
260
261MlirType mlirCalibratedQuantizedTypeGet(MlirType expressedType, double min,
262 double max) {
263 return wrap(
265}
266
268 return cast<quant::CalibratedQuantizedType>(unwrap(type)).getMin();
269}
270
272 return cast<quant::CalibratedQuantizedType>(unwrap(type)).getMax();
273}
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:141
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:178
bool mlirTypeIsAUniformQuantizedSubChannelType(MlirType type)
Returns true if the given type is a UniformQuantizedSubChannel.
Definition Quant.cpp:202
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:261
double mlirCalibratedQuantizedTypeGetMax(MlirType type)
Returns the max value of the given calibrated quantized type.
Definition Quant.cpp:271
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:128
MlirAttribute mlirUniformQuantizedSubChannelTypeGetZeroPoints(MlirType type)
Returns the zero-points of the quantized type.
Definition Quant.cpp:248
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:116
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:145
int32_t mlirUniformQuantizedPerAxisTypeGetQuantizedDimension(MlirType type)
Returns the index of the quantized dimension in the given quantized per-axis type.
Definition Quant.cpp:189
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:231
MlirType mlirQuantizedTypeGetExpressedType(MlirType type)
Gets the original type approximated by the given quantized type.
Definition Quant.cpp:43
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:161
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:157
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:194
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:243
intptr_t mlirUniformQuantizedPerAxisTypeGetNumDims(MlirType type)
Returns the number of axes in the given quantized per-axis type.
Definition Quant.cpp:172
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:132
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:237
double mlirCalibratedQuantizedTypeGetMin(MlirType type)
Returns the min value of the given calibrated quantized type.
Definition Quant.cpp:267
unsigned mlirQuantizedTypeGetFlags(MlirType type)
Gets the flags associated with the given quantized type.
Definition Quant.cpp:47
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:206
int64_t mlirUniformQuantizedPerAxisTypeGetZeroPoint(MlirType type, intptr_t pos)
Returns pos-th zero point of the given quantized per-axis type.
Definition Quant.cpp:183
intptr_t mlirUniformQuantizedSubChannelTypeGetNumBlockSizes(MlirType type)
Returns the number of block sizes provided in type.
Definition Quant.cpp:225
bool mlirUniformQuantizedTypeIsFixedPoint(MlirType type)
Returns true if the given uniform quantized type is fixed-point.
Definition Quant.cpp:149
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:257
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 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 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 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 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.