MLIR  16.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 unwrap(type).isa<quant::QuantizedType>();
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(unwrap(type).cast<quant::QuantizedType>().getExpressedType());
44 }
45 
46 unsigned mlirQuantizedTypeGetFlags(MlirType type) {
47  return unwrap(type).cast<quant::QuantizedType>().getFlags();
48 }
49 
50 bool mlirQuantizedTypeIsSigned(MlirType type) {
51  return unwrap(type).cast<quant::QuantizedType>().isSigned();
52 }
53 
54 MlirType mlirQuantizedTypeGetStorageType(MlirType type) {
55  return wrap(unwrap(type).cast<quant::QuantizedType>().getStorageType());
56 }
57 
58 int64_t mlirQuantizedTypeGetStorageTypeMin(MlirType type) {
59  return unwrap(type).cast<quant::QuantizedType>().getStorageTypeMin();
60 }
61 
62 int64_t mlirQuantizedTypeGetStorageTypeMax(MlirType type) {
63  return unwrap(type).cast<quant::QuantizedType>().getStorageTypeMax();
64 }
65 
67  return unwrap(type)
68  .cast<quant::QuantizedType>()
69  .getStorageTypeIntegralWidth();
70 }
71 
73  MlirType candidate) {
74  return unwrap(type).cast<quant::QuantizedType>().isCompatibleExpressedType(
75  unwrap(candidate));
76 }
77 
80 }
81 
82 MlirType mlirQuantizedTypeCastFromStorageType(MlirType type,
83  MlirType candidate) {
84  return wrap(unwrap(type).cast<quant::QuantizedType>().castFromStorageType(
85  unwrap(candidate)));
86 }
87 
88 MlirType mlirQuantizedTypeCastToStorageType(MlirType type) {
90  unwrap(type).cast<quant::QuantizedType>()));
91 }
92 
94  MlirType candidate) {
95  return wrap(unwrap(type).cast<quant::QuantizedType>().castFromExpressedType(
96  unwrap(candidate)));
97 }
98 
99 MlirType mlirQuantizedTypeCastToExpressedType(MlirType type) {
101 }
102 
104  MlirType candidate) {
105  return wrap(
106  unwrap(type).cast<quant::QuantizedType>().castExpressedToStorageType(
107  unwrap(candidate)));
108 }
109 
110 //===---------------------------------------------------------------------===//
111 // AnyQuantizedType
112 //===---------------------------------------------------------------------===//
113 
114 bool mlirTypeIsAAnyQuantizedType(MlirType type) {
115  return unwrap(type).isa<quant::AnyQuantizedType>();
116 }
117 
118 MlirType mlirAnyQuantizedTypeGet(unsigned flags, MlirType storageType,
119  MlirType expressedType, int64_t storageTypeMin,
120  int64_t storageTypeMax) {
121  return wrap(quant::AnyQuantizedType::get(flags, unwrap(storageType),
122  unwrap(expressedType),
123  storageTypeMin, storageTypeMax));
124 }
125 
126 //===---------------------------------------------------------------------===//
127 // UniformQuantizedType
128 //===---------------------------------------------------------------------===//
129 
130 bool mlirTypeIsAUniformQuantizedType(MlirType type) {
131  return unwrap(type).isa<quant::UniformQuantizedType>();
132 }
133 
134 MlirType mlirUniformQuantizedTypeGet(unsigned flags, MlirType storageType,
135  MlirType expressedType, double scale,
136  int64_t zeroPoint, int64_t storageTypeMin,
137  int64_t storageTypeMax) {
139  flags, unwrap(storageType), unwrap(expressedType), scale, zeroPoint,
140  storageTypeMin, storageTypeMax));
141 }
142 
143 double mlirUniformQuantizedTypeGetScale(MlirType type) {
144  return unwrap(type).cast<quant::UniformQuantizedType>().getScale();
145 }
146 
147 int64_t mlirUniformQuantizedTypeGetZeroPoint(MlirType type) {
148  return unwrap(type).cast<quant::UniformQuantizedType>().getZeroPoint();
149 }
150 
152  return unwrap(type).cast<quant::UniformQuantizedType>().isFixedPoint();
153 }
154 
155 //===---------------------------------------------------------------------===//
156 // UniformQuantizedPerAxisType
157 //===---------------------------------------------------------------------===//
158 
160  return unwrap(type).isa<quant::UniformQuantizedPerAxisType>();
161 }
162 
164  unsigned flags, MlirType storageType, MlirType expressedType,
165  intptr_t nDims, double *scales, int64_t *zeroPoints,
166  int32_t quantizedDimension, int64_t storageTypeMin,
167  int64_t storageTypeMax) {
169  flags, unwrap(storageType), unwrap(expressedType),
170  llvm::makeArrayRef(scales, nDims), llvm::makeArrayRef(zeroPoints, nDims),
171  quantizedDimension, storageTypeMin, storageTypeMax));
172 }
173 
175  return unwrap(type)
177  .getScales()
178  .size();
179 }
180 
181 double mlirUniformQuantizedPerAxisTypeGetScale(MlirType type, intptr_t pos) {
182  return unwrap(type)
184  .getScales()[pos];
185 }
186 
188  intptr_t pos) {
189  return unwrap(type)
191  .getZeroPoints()[pos];
192 }
193 
195  return unwrap(type)
197  .getQuantizedDimension();
198 }
199 
201  return unwrap(type).cast<quant::UniformQuantizedPerAxisType>().isFixedPoint();
202 }
203 
204 //===---------------------------------------------------------------------===//
205 // CalibratedQuantizedType
206 //===---------------------------------------------------------------------===//
207 
209  return unwrap(type).isa<quant::CalibratedQuantizedType>();
210 }
211 
212 MlirType mlirCalibratedQuantizedTypeGet(MlirType expressedType, double min,
213  double max) {
214  return wrap(
216 }
217 
218 double mlirCalibratedQuantizedTypeGetMin(MlirType type) {
219  return unwrap(type).cast<quant::CalibratedQuantizedType>().getMin();
220 }
221 
222 double mlirCalibratedQuantizedTypeGetMax(MlirType type) {
223  return unwrap(type).cast<quant::CalibratedQuantizedType>().getMax();
224 }
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:143
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:99
double mlirUniformQuantizedPerAxisTypeGetScale(MlirType type, intptr_t pos)
Returns pos-th scale of the given quantized per-axis type.
Definition: Quant.cpp:181
bool mlirQuantizedTypeIsCompatibleExpressedType(MlirType type, MlirType candidate)
Returns true if the candidate type is compatible with the given quantized type.
Definition: Quant.cpp:72
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:212
double mlirCalibratedQuantizedTypeGetMax(MlirType type)
Returns the max value of the given calibrated quantized type.
Definition: Quant.cpp:222
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:82
bool mlirTypeIsAUniformQuantizedType(MlirType type)
Returns true if the given type is a UniformQuantizedType.
Definition: Quant.cpp:130
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:118
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:93
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:88
int64_t mlirUniformQuantizedTypeGetZeroPoint(MlirType type)
Returns the zero point of the given uniform quantized type.
Definition: Quant.cpp:147
int32_t mlirUniformQuantizedPerAxisTypeGetQuantizedDimension(MlirType type)
Returns the index of the quantized dimension in the given quantized per-axis type.
Definition: Quant.cpp:194
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:163
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:159
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:103
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:200
MlirType mlirQuantizedTypeGetQuantizedElementType(MlirType type)
Returns the element type of the given quantized type as another quantized type.
Definition: Quant.cpp:78
intptr_t mlirUniformQuantizedPerAxisTypeGetNumDims(MlirType type)
Returns the number of axes in the given quantized per-axis type.
Definition: Quant.cpp:174
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:134
bool mlirTypeIsAAnyQuantizedType(MlirType type)
Returns true if the given type is an AnyQuantizedType.
Definition: Quant.cpp:114
double mlirCalibratedQuantizedTypeGetMin(MlirType type)
Returns the min value of the given calibrated quantized type.
Definition: Quant.cpp:218
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:187
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:151
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:208
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
A quantized type that maps storage to/from expressed types in an unspecified way.
Definition: QuantTypes.h:199
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:216
A quantized type that infers its range from given min/max values.
Definition: QuantTypes.h:384
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:370
Base class for all quantized types known to this dialect.
Definition: QuantTypes.h:52
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:126
static Type castToExpressedType(Type quantizedType)
Casts from a type based on QuantizedType to a corresponding type based on the expressedType (returns ...
Definition: QuantTypes.cpp:181
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:80
static int64_t getDefaultMinimumForInteger(bool isSigned, unsigned integralWidth)
Gets the minimum possible stored by a storageType.
Definition: QuantTypes.h:70
Represents per-axis (also known as per-channel quantization).
Definition: QuantTypes.h:316
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:303
Represents a family of uniform, quantized types.
Definition: QuantTypes.h:258
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:252
mlir::Diagnostic & unwrap(MlirDiagnostic diagnostic)
Definition: Diagnostics.h:19
MlirDiagnostic wrap(mlir::Diagnostic &diagnostic)
Definition: Diagnostics.h:24
Include the generated interface declarations.