1//===-- mlir-c/Dialect/Quant.h - C API for LLVM -------------------*- C -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM
4// Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef MLIR_C_DIALECT_QUANT_H
11#define MLIR_C_DIALECT_QUANT_H
12
13#include "mlir-c/IR.h"
14
15#ifdef __cplusplus
16extern "C" {
17#endif
18
19MLIR_DECLARE_CAPI_DIALECT_REGISTRATION(quant, quant);
20
21//===---------------------------------------------------------------------===//
22// QuantizedType
23//===---------------------------------------------------------------------===//
24
25/// Returns `true` if the given type is a quantization dialect type.
26MLIR_CAPI_EXPORTED bool mlirTypeIsAQuantizedType(MlirType type);
27
28/// Returns the bit flag used to indicate signedness of a quantized type.
29MLIR_CAPI_EXPORTED unsigned mlirQuantizedTypeGetSignedFlag(void);
30
31/// Returns the minimum possible value stored by a quantized type.
32MLIR_CAPI_EXPORTED int64_t mlirQuantizedTypeGetDefaultMinimumForInteger(
33 bool isSigned, unsigned integralWidth);
34
35/// Returns the maximum possible value stored by a quantized type.
36MLIR_CAPI_EXPORTED int64_t mlirQuantizedTypeGetDefaultMaximumForInteger(
37 bool isSigned, unsigned integralWidth);
38
39/// Gets the original type approximated by the given quantized type.
40MLIR_CAPI_EXPORTED MlirType mlirQuantizedTypeGetExpressedType(MlirType type);
41
42/// Gets the flags associated with the given quantized type.
43MLIR_CAPI_EXPORTED unsigned mlirQuantizedTypeGetFlags(MlirType type);
44
45/// Returns `true` if the given type is signed, `false` otherwise.
46MLIR_CAPI_EXPORTED bool mlirQuantizedTypeIsSigned(MlirType type);
47
48/// Returns the underlying type used to store the values.
49MLIR_CAPI_EXPORTED MlirType mlirQuantizedTypeGetStorageType(MlirType type);
50
51/// Returns the minimum value that the storage type of the given quantized type
52/// can take.
53MLIR_CAPI_EXPORTED int64_t mlirQuantizedTypeGetStorageTypeMin(MlirType type);
54
55/// Returns the maximum value that the storage type of the given quantized type
56/// can take.
57MLIR_CAPI_EXPORTED int64_t mlirQuantizedTypeGetStorageTypeMax(MlirType type);
58
59/// Returns the integral bitwidth that the storage type of the given quantized
60/// type can represent exactly.
61MLIR_CAPI_EXPORTED unsigned
62mlirQuantizedTypeGetStorageTypeIntegralWidth(MlirType type);
63
64/// Returns `true` if the `candidate` type is compatible with the given
65/// quantized `type`.
66MLIR_CAPI_EXPORTED bool
67mlirQuantizedTypeIsCompatibleExpressedType(MlirType type, MlirType candidate);
68
69/// Returns the element type of the given quantized type as another quantized
70/// type.
71MLIR_CAPI_EXPORTED MlirType
72mlirQuantizedTypeGetQuantizedElementType(MlirType type);
73
74/// Casts from a type based on the storage type of the given type to a
75/// corresponding type based on the given type. Returns a null type if the cast
76/// is not valid.
77MLIR_CAPI_EXPORTED MlirType
78mlirQuantizedTypeCastFromStorageType(MlirType type, MlirType candidate);
79
80/// Casts from a type based on a quantized type to a corresponding typed based
81/// on the storage type. Returns a null type if the cast is not valid.
82MLIR_CAPI_EXPORTED MlirType mlirQuantizedTypeCastToStorageType(MlirType type);
83
84/// Casts from a type based on the expressed type of the given type to a
85/// corresponding type based on the given type. Returns a null type if the cast
86/// is not valid.
87MLIR_CAPI_EXPORTED MlirType
88mlirQuantizedTypeCastFromExpressedType(MlirType type, MlirType candidate);
89
90/// Casts from a type based on a quantized type to a corresponding typed based
91/// on the expressed type. Returns a null type if the cast is not valid.
92MLIR_CAPI_EXPORTED MlirType mlirQuantizedTypeCastToExpressedType(MlirType type);
93
94/// Casts from a type based on the expressed type of the given quantized type to
95/// equivalent type based on storage type of the same quantized type.
96MLIR_CAPI_EXPORTED MlirType
97mlirQuantizedTypeCastExpressedToStorageType(MlirType type, MlirType candidate);
98
99//===---------------------------------------------------------------------===//
100// AnyQuantizedType
101//===---------------------------------------------------------------------===//
102
103/// Returns `true` if the given type is an AnyQuantizedType.
104MLIR_CAPI_EXPORTED bool mlirTypeIsAAnyQuantizedType(MlirType type);
105
106MLIR_CAPI_EXPORTED MlirTypeID mlirAnyQuantizedTypeGetTypeID(void);
107
108/// Creates an instance of AnyQuantizedType with the given parameters in the
109/// same context as `storageType` and returns it. The instance is owned by the
110/// context.
111MLIR_CAPI_EXPORTED MlirType mlirAnyQuantizedTypeGet(unsigned flags,
112 MlirType storageType,
113 MlirType expressedType,
114 int64_t storageTypeMin,
115 int64_t storageTypeMax);
116
117MLIR_CAPI_EXPORTED MlirStringRef mlirAnyQuantizedTypeGetName(void);
118
119//===---------------------------------------------------------------------===//
120// UniformQuantizedType
121//===---------------------------------------------------------------------===//
122
123/// Returns `true` if the given type is a UniformQuantizedType.
124MLIR_CAPI_EXPORTED bool mlirTypeIsAUniformQuantizedType(MlirType type);
125
126MLIR_CAPI_EXPORTED MlirTypeID mlirUniformQuantizedTypeGetTypeID(void);
127
128/// Creates an instance of UniformQuantizedType with the given parameters in the
129/// same context as `storageType` and returns it. The instance is owned by the
130/// context.
131MLIR_CAPI_EXPORTED MlirType mlirUniformQuantizedTypeGet(
132 unsigned flags, MlirType storageType, MlirType expressedType, double scale,
133 int64_t zeroPoint, int64_t storageTypeMin, int64_t storageTypeMax);
134
135MLIR_CAPI_EXPORTED MlirStringRef mlirUniformQuantizedTypeGetName(void);
136
137/// Returns the scale of the given uniform quantized type.
138MLIR_CAPI_EXPORTED double mlirUniformQuantizedTypeGetScale(MlirType type);
139
140/// Returns the zero point of the given uniform quantized type.
141MLIR_CAPI_EXPORTED int64_t mlirUniformQuantizedTypeGetZeroPoint(MlirType type);
142
143/// Returns `true` if the given uniform quantized type is fixed-point.
144MLIR_CAPI_EXPORTED bool mlirUniformQuantizedTypeIsFixedPoint(MlirType type);
145
146//===---------------------------------------------------------------------===//
147// UniformQuantizedPerAxisType
148//===---------------------------------------------------------------------===//
149
150/// Returns `true` if the given type is a UniformQuantizedPerAxisType.
151MLIR_CAPI_EXPORTED bool mlirTypeIsAUniformQuantizedPerAxisType(MlirType type);
152
153MLIR_CAPI_EXPORTED MlirTypeID mlirUniformQuantizedPerAxisTypeGetTypeID(void);
154
155/// Creates an instance of UniformQuantizedPerAxisType with the given parameters
156/// in the same context as `storageType` and returns it. `scales` and
157/// `zeroPoints` point to `nDims` number of elements. The instance is owned
158/// by the context.
159MLIR_CAPI_EXPORTED MlirType mlirUniformQuantizedPerAxisTypeGet(
160 unsigned flags, MlirType storageType, MlirType expressedType,
161 intptr_t nDims, double *scales, int64_t *zeroPoints,
162 int32_t quantizedDimension, int64_t storageTypeMin, int64_t storageTypeMax);
163
164MLIR_CAPI_EXPORTED MlirStringRef mlirUniformQuantizedPerAxisTypeGetName(void);
165
166/// Returns the number of axes in the given quantized per-axis type.
167MLIR_CAPI_EXPORTED intptr_t
168mlirUniformQuantizedPerAxisTypeGetNumDims(MlirType type);
169
170/// Returns `pos`-th scale of the given quantized per-axis type.
171MLIR_CAPI_EXPORTED double mlirUniformQuantizedPerAxisTypeGetScale(MlirType type,
172 intptr_t pos);
173
174/// Returns `pos`-th zero point of the given quantized per-axis type.
175MLIR_CAPI_EXPORTED int64_t
176mlirUniformQuantizedPerAxisTypeGetZeroPoint(MlirType type, intptr_t pos);
177
178/// Returns the index of the quantized dimension in the given quantized per-axis
179/// type.
180MLIR_CAPI_EXPORTED int32_t
181mlirUniformQuantizedPerAxisTypeGetQuantizedDimension(MlirType type);
182
183/// Returns `true` if the given uniform quantized per-axis type is fixed-point.
184MLIR_CAPI_EXPORTED bool
185mlirUniformQuantizedPerAxisTypeIsFixedPoint(MlirType type);
186
187//===---------------------------------------------------------------------===//
188// UniformQuantizedSubChannelType
189//===---------------------------------------------------------------------===//
190
191/// Returns `true` if the given type is a UniformQuantizedSubChannel.
192MLIR_CAPI_EXPORTED bool
193mlirTypeIsAUniformQuantizedSubChannelType(MlirType type);
194
195MLIR_CAPI_EXPORTED MlirTypeID mlirUniformQuantizedSubChannelTypeGetTypeID(void);
196
197/// Creates a UniformQuantizedSubChannelType with the given parameters.
198///
199/// The type is owned by the context. `scalesAttr` and `zeroPointsAttr` must be
200/// DenseElementsAttrs. `quantizedDimensions` and `blockSizes`
201/// point to `blockSizeInfoLength` number of elements, describing respectively
202/// the quantization axis and corresponding block size.
203MLIR_CAPI_EXPORTED MlirType mlirUniformQuantizedSubChannelTypeGet(
204 unsigned flags, MlirType storageType, MlirType expressedType,
205 MlirAttribute scalesAttr, MlirAttribute zeroPointsAttr,
206 intptr_t blockSizeInfoLength, int32_t *quantizedDimensions,
207 int64_t *blockSizes, int64_t storageTypeMin, int64_t storageTypeMax);
208
209MLIR_CAPI_EXPORTED MlirStringRef
210mlirUniformQuantizedSubChannelTypeGetName(void);
211
212/// Returns the number of block sizes provided in type.
213MLIR_CAPI_EXPORTED intptr_t
214mlirUniformQuantizedSubChannelTypeGetNumBlockSizes(MlirType type);
215
216/// Returns the quantized dimension at the given position.
217MLIR_CAPI_EXPORTED int32_t
218mlirUniformQuantizedSubChannelTypeGetQuantizedDimension(MlirType type,
219 intptr_t pos);
220
221/// Returns the block size at the given position.
222MLIR_CAPI_EXPORTED int64_t
223mlirUniformQuantizedSubChannelTypeGetBlockSize(MlirType type, intptr_t pos);
224
225/// Returns the scales of the quantized type.
226MLIR_CAPI_EXPORTED MlirAttribute
227mlirUniformQuantizedSubChannelTypeGetScales(MlirType type);
228
229/// Returns the zero-points of the quantized type.
230MLIR_CAPI_EXPORTED MlirAttribute
231mlirUniformQuantizedSubChannelTypeGetZeroPoints(MlirType type);
232
233//===---------------------------------------------------------------------===//
234// CalibratedQuantizedType
235//===---------------------------------------------------------------------===//
236
237/// Returns `true` if the given type is a CalibratedQuantizedType.
238MLIR_CAPI_EXPORTED bool mlirTypeIsACalibratedQuantizedType(MlirType type);
239
240MLIR_CAPI_EXPORTED MlirTypeID mlirCalibratedQuantizedTypeGetTypeID(void);
241
242/// Creates an instance of CalibratedQuantizedType with the given parameters
243/// in the same context as `expressedType` and returns it. The instance is owned
244/// by the context.
245MLIR_CAPI_EXPORTED MlirType
246mlirCalibratedQuantizedTypeGet(MlirType expressedType, double min, double max);
247
248MLIR_CAPI_EXPORTED MlirStringRef mlirCalibratedQuantizedTypeGetName(void);
249
250/// Returns the min value of the given calibrated quantized type.
251MLIR_CAPI_EXPORTED double mlirCalibratedQuantizedTypeGetMin(MlirType type);
252
253/// Returns the max value of the given calibrated quantized type.
254MLIR_CAPI_EXPORTED double mlirCalibratedQuantizedTypeGetMax(MlirType type);
255
256#ifdef __cplusplus
257}
258#endif
259
260#endif // MLIR_C_DIALECT_QUANT_H
261