MLIR  19.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 
9 #include "mlir-c/Dialect/Quant.h"
10 #include "mlir/CAPI/Registration.h"
13 
14 using namespace mlir;
15 
16 MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(quant, quant, quant::QuantizationDialect)
17 
18 //===---------------------------------------------------------------------===//
19 // QuantizedType
20 //===---------------------------------------------------------------------===//
21 
22 bool mlirTypeIsAQuantizedType(MlirType type) {
23  return isa<quant::QuantizedType>(unwrap(type));
24 }
25 
28 }
29 
31  unsigned integralWidth) {
33  integralWidth);
34 }
35 
37  unsigned integralWidth) {
39  integralWidth);
40 }
41 
42 MlirType mlirQuantizedTypeGetExpressedType(MlirType type) {
43  return wrap(cast<quant::QuantizedType>(unwrap(type)).getExpressedType());
44 }
45 
46 unsigned mlirQuantizedTypeGetFlags(MlirType type) {
47  return cast<quant::QuantizedType>(unwrap(type)).getFlags();
48 }
49 
50 bool mlirQuantizedTypeIsSigned(MlirType type) {
51  return cast<quant::QuantizedType>(unwrap(type)).isSigned();
52 }
53 
54 MlirType mlirQuantizedTypeGetStorageType(MlirType type) {
55  return wrap(cast<quant::QuantizedType>(unwrap(type)).getStorageType());
56 }
57 
58 int64_t mlirQuantizedTypeGetStorageTypeMin(MlirType type) {
59  return cast<quant::QuantizedType>(unwrap(type)).getStorageTypeMin();
60 }
61 
62 int64_t mlirQuantizedTypeGetStorageTypeMax(MlirType type) {
63  return cast<quant::QuantizedType>(unwrap(type)).getStorageTypeMax();
64 }
65 
67  return cast<quant::QuantizedType>(unwrap(type)).getStorageTypeIntegralWidth();
68 }
69 
71  MlirType candidate) {
72  return cast<quant::QuantizedType>(unwrap(type))
73  .isCompatibleExpressedType(unwrap(candidate));
74 }
75 
78 }
79 
80 MlirType mlirQuantizedTypeCastFromStorageType(MlirType type,
81  MlirType candidate) {
82  return wrap(cast<quant::QuantizedType>(unwrap(type))
83  .castFromStorageType(unwrap(candidate)));
84 }
85 
86 MlirType mlirQuantizedTypeCastToStorageType(MlirType type) {
88  cast<quant::QuantizedType>(unwrap(type))));
89 }
90 
92  MlirType candidate) {
93  return wrap(cast<quant::QuantizedType>(unwrap(type))
94  .castFromExpressedType(unwrap(candidate)));
95 }
96 
97 MlirType mlirQuantizedTypeCastToExpressedType(MlirType type) {
99 }
100 
102  MlirType candidate) {
103  return wrap(cast<quant::QuantizedType>(unwrap(type))
104  .castExpressedToStorageType(unwrap(candidate)));
105 }
106 
107 //===---------------------------------------------------------------------===//
108 // AnyQuantizedType
109 //===---------------------------------------------------------------------===//
110 
111 bool mlirTypeIsAAnyQuantizedType(MlirType type) {
112  return isa<quant::AnyQuantizedType>(unwrap(type));
113 }
114 
115 MlirType mlirAnyQuantizedTypeGet(unsigned flags, MlirType storageType,
116  MlirType expressedType, int64_t storageTypeMin,
117  int64_t storageTypeMax) {
118  return wrap(quant::AnyQuantizedType::get(flags, unwrap(storageType),
119  unwrap(expressedType),
120  storageTypeMin, storageTypeMax));
121 }
122 
123 //===---------------------------------------------------------------------===//
124 // UniformQuantizedType
125 //===---------------------------------------------------------------------===//
126 
127 bool mlirTypeIsAUniformQuantizedType(MlirType type) {
128  return isa<quant::UniformQuantizedType>(unwrap(type));
129 }
130 
131 MlirType mlirUniformQuantizedTypeGet(unsigned flags, MlirType storageType,
132  MlirType expressedType, double scale,
133  int64_t zeroPoint, int64_t storageTypeMin,
134  int64_t storageTypeMax) {
136  flags, unwrap(storageType), unwrap(expressedType), scale, zeroPoint,
137  storageTypeMin, storageTypeMax));
138 }
139 
140 double mlirUniformQuantizedTypeGetScale(MlirType type) {
141  return cast<quant::UniformQuantizedType>(unwrap(type)).getScale();
142 }
143 
144 int64_t mlirUniformQuantizedTypeGetZeroPoint(MlirType type) {
145  return cast<quant::UniformQuantizedType>(unwrap(type)).getZeroPoint();
146 }
147 
149  return cast<quant::UniformQuantizedType>(unwrap(type)).isFixedPoint();
150 }
151 
152 //===---------------------------------------------------------------------===//
153 // UniformQuantizedPerAxisType
154 //===---------------------------------------------------------------------===//
155 
157  return isa<quant::UniformQuantizedPerAxisType>(unwrap(type));
158 }
159 
161  unsigned flags, MlirType storageType, MlirType expressedType,
162  intptr_t nDims, double *scales, int64_t *zeroPoints,
163  int32_t quantizedDimension, int64_t storageTypeMin,
164  int64_t storageTypeMax) {
166  flags, unwrap(storageType), unwrap(expressedType),
167  llvm::ArrayRef(scales, nDims), llvm::ArrayRef(zeroPoints, nDims),
168  quantizedDimension, storageTypeMin, storageTypeMax));
169 }
170 
172  return cast<quant::UniformQuantizedPerAxisType>(unwrap(type))
173  .getScales()
174  .size();
175 }
176 
177 double mlirUniformQuantizedPerAxisTypeGetScale(MlirType type, intptr_t pos) {
178  return cast<quant::UniformQuantizedPerAxisType>(unwrap(type))
179  .getScales()[pos];
180 }
181 
183  intptr_t pos) {
184  return cast<quant::UniformQuantizedPerAxisType>(unwrap(type))
185  .getZeroPoints()[pos];
186 }
187 
189  return cast<quant::UniformQuantizedPerAxisType>(unwrap(type))
190  .getQuantizedDimension();
191 }
192 
194  return cast<quant::UniformQuantizedPerAxisType>(unwrap(type)).isFixedPoint();
195 }
196 
197 //===---------------------------------------------------------------------===//
198 // CalibratedQuantizedType
199 //===---------------------------------------------------------------------===//
200 
202  return isa<quant::CalibratedQuantizedType>(unwrap(type));
203 }
204 
205 MlirType mlirCalibratedQuantizedTypeGet(MlirType expressedType, double min,
206  double max) {
207  return wrap(
209 }
210 
211 double mlirCalibratedQuantizedTypeGetMin(MlirType type) {
212  return cast<quant::CalibratedQuantizedType>(unwrap(type)).getMin();
213 }
214 
215 double mlirCalibratedQuantizedTypeGetMax(MlirType type) {
216  return cast<quant::CalibratedQuantizedType>(unwrap(type)).getMax();
217 }
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:140
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:97
double mlirUniformQuantizedPerAxisTypeGetScale(MlirType type, intptr_t pos)
Returns pos-th scale of the given quantized per-axis type.
Definition: Quant.cpp:177
bool mlirQuantizedTypeIsCompatibleExpressedType(MlirType type, MlirType candidate)
Returns true if the candidate type is compatible with the given quantized type.
Definition: Quant.cpp:70
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:205
double mlirCalibratedQuantizedTypeGetMax(MlirType type)
Returns the max value of the given calibrated quantized type.
Definition: Quant.cpp:215
MlirType mlirQuantizedTypeGetStorageType(MlirType type)
Returns the underlying type used to store the values.
Definition: Quant.cpp:54
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:80
bool mlirTypeIsAUniformQuantizedType(MlirType type)
Returns true if the given type is a UniformQuantizedType.
Definition: Quant.cpp:127
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:115
bool mlirQuantizedTypeIsSigned(MlirType type)
Returns true if the given type is signed, false otherwise.
Definition: Quant.cpp:50
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:91
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:86
int64_t mlirUniformQuantizedTypeGetZeroPoint(MlirType type)
Returns the zero point of the given uniform quantized type.
Definition: Quant.cpp:144
int32_t mlirUniformQuantizedPerAxisTypeGetQuantizedDimension(MlirType type)
Returns the index of the quantized dimension in the given quantized per-axis type.
Definition: Quant.cpp:188
unsigned mlirQuantizedTypeGetSignedFlag()
Returns the bit flag used to indicate signedness of a quantized type.
Definition: Quant.cpp:26
MlirType mlirQuantizedTypeGetExpressedType(MlirType type)
Gets the original type approximated by the given quantized type.
Definition: Quant.cpp:42
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:160
unsigned mlirQuantizedTypeGetStorageTypeIntegralWidth(MlirType type)
Returns the integral bitwidth that the storage type of the given quantized type can represent exactly...
Definition: Quant.cpp:66
int64_t mlirQuantizedTypeGetStorageTypeMin(MlirType type)
Returns the minimum value that the storage type of the given quantized type can take.
Definition: Quant.cpp:58
bool mlirTypeIsAUniformQuantizedPerAxisType(MlirType type)
Returns true if the given type is a UniformQuantizedPerAxisType.
Definition: Quant.cpp:156
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:101
int64_t mlirQuantizedTypeGetStorageTypeMax(MlirType type)
Returns the maximum value that the storage type of the given quantized type can take.
Definition: Quant.cpp:62
bool mlirUniformQuantizedPerAxisTypeIsFixedPoint(MlirType type)
Returns true if the given uniform quantized per-axis type is fixed-point.
Definition: Quant.cpp:193
MlirType mlirQuantizedTypeGetQuantizedElementType(MlirType type)
Returns the element type of the given quantized type as another quantized type.
Definition: Quant.cpp:76
intptr_t mlirUniformQuantizedPerAxisTypeGetNumDims(MlirType type)
Returns the number of axes in the given quantized per-axis type.
Definition: Quant.cpp:171
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:131
bool mlirTypeIsAAnyQuantizedType(MlirType type)
Returns true if the given type is an AnyQuantizedType.
Definition: Quant.cpp:111
double mlirCalibratedQuantizedTypeGetMin(MlirType type)
Returns the min value of the given calibrated quantized type.
Definition: Quant.cpp:211
unsigned mlirQuantizedTypeGetFlags(MlirType type)
Gets the flags associated with the given quantized type.
Definition: Quant.cpp:46
int64_t mlirUniformQuantizedPerAxisTypeGetZeroPoint(MlirType type, intptr_t pos)
Returns pos-th zero point of the given quantized per-axis type.
Definition: Quant.cpp:182
bool mlirTypeIsAQuantizedType(MlirType type)
Returns true if the given type is a quantization dialect type.
Definition: Quant.cpp:22
bool mlirUniformQuantizedTypeIsFixedPoint(MlirType type)
Returns true if the given uniform quantized type is fixed-point.
Definition: Quant.cpp:148
int64_t mlirQuantizedTypeGetDefaultMinimumForInteger(bool isSigned, unsigned integralWidth)
Returns the minimum possible value stored by a quantized type.
Definition: Quant.cpp:30
bool mlirTypeIsACalibratedQuantizedType(MlirType type)
Returns true if the given type is a CalibratedQuantizedType.
Definition: Quant.cpp:201
int64_t mlirQuantizedTypeGetDefaultMaximumForInteger(bool isSigned, unsigned integralWidth)
Returns the maximum possible value stored by a quantized type.
Definition: Quant.cpp:36
#define MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(Name, Namespace, ClassName)
Definition: Registration.h:36
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.
Definition: QuantTypes.cpp:217
static CalibratedQuantizedType get(Type expressedType, double min, double max)
Gets an instance of the type with all parameters specified but not checked.
Definition: QuantTypes.cpp:371
static Type castToStorageType(Type quantizedType)
Casts from a type based on a QuantizedType to a corresponding type based on the storageType (returns ...
Definition: QuantTypes.cpp:127
static Type castToExpressedType(Type quantizedType)
Casts from a type based on QuantizedType to a corresponding type based on the expressedType (returns ...
Definition: QuantTypes.cpp:182
static QuantizedType getQuantizedElementType(Type primitiveOrContainerType)
Returns the element type as a QuantizedType or nullptr if it is not a quantized type.
Definition: QuantTypes.cpp:94
static int64_t getDefaultMaximumForInteger(bool isSigned, unsigned integralWidth)
Gets the maximum possible stored by a storageType.
Definition: QuantTypes.h:77
static int64_t getDefaultMinimumForInteger(bool isSigned, unsigned integralWidth)
Gets the minimum possible stored by a storageType.
Definition: QuantTypes.h:67
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.
Definition: QuantTypes.cpp:304
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.
Definition: QuantTypes.cpp:253
mlir::Diagnostic & unwrap(MlirDiagnostic diagnostic)
Definition: Diagnostics.h:19
MlirDiagnostic wrap(mlir::Diagnostic &diagnostic)
Definition: Diagnostics.h:24
Include the generated interface declarations.