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-c/BuiltinAttributes.h"
11#include "mlir/CAPI/Registration.h"
12#include "mlir/Dialect/Quant/IR/Quant.h"
13#include "mlir/Dialect/Quant/IR/QuantTypes.h"
14
15using namespace mlir;
16
17MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(quant, quant, quant::QuantDialect)
18
19//===---------------------------------------------------------------------===//
20// QuantizedType
21//===---------------------------------------------------------------------===//
22
23bool mlirTypeIsAQuantizedType(MlirType type) {
24 return isa<quant::QuantizedType>(Val: unwrap(c: type));
25}
26
27unsigned mlirQuantizedTypeGetSignedFlag() {
28 return quant::QuantizationFlags::Signed;
29}
30
31int64_t mlirQuantizedTypeGetDefaultMinimumForInteger(bool isSigned,
32 unsigned integralWidth) {
33 return quant::QuantizedType::getDefaultMinimumForInteger(isSigned,
34 integralWidth);
35}
36
37int64_t mlirQuantizedTypeGetDefaultMaximumForInteger(bool isSigned,
38 unsigned integralWidth) {
39 return quant::QuantizedType::getDefaultMaximumForInteger(isSigned,
40 integralWidth);
41}
42
43MlirType mlirQuantizedTypeGetExpressedType(MlirType type) {
44 return wrap(cpp: cast<quant::QuantizedType>(Val: unwrap(c: type)).getExpressedType());
45}
46
47unsigned mlirQuantizedTypeGetFlags(MlirType type) {
48 return cast<quant::QuantizedType>(Val: unwrap(c: type)).getFlags();
49}
50
51bool mlirQuantizedTypeIsSigned(MlirType type) {
52 return cast<quant::QuantizedType>(Val: unwrap(c: type)).isSigned();
53}
54
55MlirType mlirQuantizedTypeGetStorageType(MlirType type) {
56 return wrap(cpp: cast<quant::QuantizedType>(Val: unwrap(c: type)).getStorageType());
57}
58
59int64_t mlirQuantizedTypeGetStorageTypeMin(MlirType type) {
60 return cast<quant::QuantizedType>(Val: unwrap(c: type)).getStorageTypeMin();
61}
62
63int64_t mlirQuantizedTypeGetStorageTypeMax(MlirType type) {
64 return cast<quant::QuantizedType>(Val: unwrap(c: type)).getStorageTypeMax();
65}
66
67unsigned mlirQuantizedTypeGetStorageTypeIntegralWidth(MlirType type) {
68 return cast<quant::QuantizedType>(Val: unwrap(c: type)).getStorageTypeIntegralWidth();
69}
70
71bool mlirQuantizedTypeIsCompatibleExpressedType(MlirType type,
72 MlirType candidate) {
73 return cast<quant::QuantizedType>(Val: unwrap(c: type))
74 .isCompatibleExpressedType(candidateExpressedType: unwrap(c: candidate));
75}
76
77MlirType mlirQuantizedTypeGetQuantizedElementType(MlirType type) {
78 return wrap(cpp: quant::QuantizedType::getQuantizedElementType(primitiveOrContainerType: unwrap(c: type)));
79}
80
81MlirType mlirQuantizedTypeCastFromStorageType(MlirType type,
82 MlirType candidate) {
83 return wrap(cpp: cast<quant::QuantizedType>(Val: unwrap(c: type))
84 .castFromStorageType(candidateType: unwrap(c: candidate)));
85}
86
87MlirType mlirQuantizedTypeCastToStorageType(MlirType type) {
88 return wrap(cpp: quant::QuantizedType::castToStorageType(
89 quantizedType: cast<quant::QuantizedType>(Val: unwrap(c: type))));
90}
91
92MlirType mlirQuantizedTypeCastFromExpressedType(MlirType type,
93 MlirType candidate) {
94 return wrap(cpp: cast<quant::QuantizedType>(Val: unwrap(c: type))
95 .castFromExpressedType(candidateType: unwrap(c: candidate)));
96}
97
98MlirType mlirQuantizedTypeCastToExpressedType(MlirType type) {
99 return wrap(cpp: quant::QuantizedType::castToExpressedType(quantizedType: unwrap(c: type)));
100}
101
102MlirType mlirQuantizedTypeCastExpressedToStorageType(MlirType type,
103 MlirType candidate) {
104 return wrap(cpp: cast<quant::QuantizedType>(Val: unwrap(c: type))
105 .castExpressedToStorageType(candidateType: unwrap(c: candidate)));
106}
107
108//===---------------------------------------------------------------------===//
109// AnyQuantizedType
110//===---------------------------------------------------------------------===//
111
112bool mlirTypeIsAAnyQuantizedType(MlirType type) {
113 return isa<quant::AnyQuantizedType>(Val: unwrap(c: type));
114}
115
116MlirTypeID mlirAnyQuantizedTypeGetTypeID(void) {
117 return wrap(cpp: quant::AnyQuantizedType::getTypeID());
118}
119
120MlirType mlirAnyQuantizedTypeGet(unsigned flags, MlirType storageType,
121 MlirType expressedType, int64_t storageTypeMin,
122 int64_t storageTypeMax) {
123 return wrap(cpp: quant::AnyQuantizedType::get(flags, storageType: unwrap(c: storageType),
124 expressedType: unwrap(c: expressedType),
125 storageTypeMin, storageTypeMax));
126}
127
128MlirStringRef mlirAnyQuantizedTypeGetName(void) {
129 return wrap(ref: quant::AnyQuantizedType::name);
130}
131
132//===---------------------------------------------------------------------===//
133// UniformQuantizedType
134//===---------------------------------------------------------------------===//
135
136bool mlirTypeIsAUniformQuantizedType(MlirType type) {
137 return isa<quant::UniformQuantizedType>(Val: unwrap(c: type));
138}
139
140MlirTypeID mlirUniformQuantizedTypeGetTypeID(void) {
141 return wrap(cpp: quant::UniformQuantizedType::getTypeID());
142}
143
144MlirType mlirUniformQuantizedTypeGet(unsigned flags, MlirType storageType,
145 MlirType expressedType, double scale,
146 int64_t zeroPoint, int64_t storageTypeMin,
147 int64_t storageTypeMax) {
148 return wrap(cpp: quant::UniformQuantizedType::get(
149 flags, storageType: unwrap(c: storageType), expressedType: unwrap(c: expressedType), scale, zeroPoint,
150 storageTypeMin, storageTypeMax));
151}
152
153MlirStringRef mlirUniformQuantizedTypeGetName(void) {
154 return wrap(ref: quant::UniformQuantizedType::name);
155}
156
157double mlirUniformQuantizedTypeGetScale(MlirType type) {
158 return cast<quant::UniformQuantizedType>(Val: unwrap(c: type)).getScale();
159}
160
161int64_t mlirUniformQuantizedTypeGetZeroPoint(MlirType type) {
162 return cast<quant::UniformQuantizedType>(Val: unwrap(c: type)).getZeroPoint();
163}
164
165bool mlirUniformQuantizedTypeIsFixedPoint(MlirType type) {
166 return cast<quant::UniformQuantizedType>(Val: unwrap(c: type)).isFixedPoint();
167}
168
169//===---------------------------------------------------------------------===//
170// UniformQuantizedPerAxisType
171//===---------------------------------------------------------------------===//
172
173bool mlirTypeIsAUniformQuantizedPerAxisType(MlirType type) {
174 return isa<quant::UniformQuantizedPerAxisType>(Val: unwrap(c: type));
175}
176
177MlirTypeID mlirUniformQuantizedPerAxisTypeGetTypeID(void) {
178 return wrap(cpp: quant::UniformQuantizedPerAxisType::getTypeID());
179}
180
181MlirType mlirUniformQuantizedPerAxisTypeGet(
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) {
186 return wrap(cpp: quant::UniformQuantizedPerAxisType::get(
187 flags, storageType: unwrap(c: storageType), expressedType: unwrap(c: expressedType),
188 scales: llvm::ArrayRef(scales, nDims), zeroPoints: llvm::ArrayRef(zeroPoints, nDims),
189 quantizedDimension, storageTypeMin, storageTypeMax));
190}
191
192MlirStringRef mlirUniformQuantizedPerAxisTypeGetName(void) {
193 return wrap(ref: quant::UniformQuantizedPerAxisType::name);
194}
195
196intptr_t mlirUniformQuantizedPerAxisTypeGetNumDims(MlirType type) {
197 return cast<quant::UniformQuantizedPerAxisType>(Val: unwrap(c: type))
198 .getScales()
199 .size();
200}
201
202double mlirUniformQuantizedPerAxisTypeGetScale(MlirType type, intptr_t pos) {
203 return cast<quant::UniformQuantizedPerAxisType>(Val: unwrap(c: type))
204 .getScales()[pos];
205}
206
207int64_t mlirUniformQuantizedPerAxisTypeGetZeroPoint(MlirType type,
208 intptr_t pos) {
209 return cast<quant::UniformQuantizedPerAxisType>(Val: unwrap(c: type))
210 .getZeroPoints()[pos];
211}
212
213int32_t mlirUniformQuantizedPerAxisTypeGetQuantizedDimension(MlirType type) {
214 return cast<quant::UniformQuantizedPerAxisType>(Val: unwrap(c: type))
215 .getQuantizedDimension();
216}
217
218bool mlirUniformQuantizedPerAxisTypeIsFixedPoint(MlirType type) {
219 return cast<quant::UniformQuantizedPerAxisType>(Val: unwrap(c: type)).isFixedPoint();
220}
221
222//===---------------------------------------------------------------------===//
223// UniformQuantizedSubChannelType
224//===---------------------------------------------------------------------===//
225
226bool mlirTypeIsAUniformQuantizedSubChannelType(MlirType type) {
227 return isa<quant::UniformQuantizedSubChannelType>(Val: unwrap(c: type));
228}
229
230MlirTypeID mlirUniformQuantizedSubChannelTypeGetTypeID(void) {
231 return wrap(cpp: quant::UniformQuantizedSubChannelType::getTypeID());
232}
233
234MlirType mlirUniformQuantizedSubChannelTypeGet(
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>(Val: unwrap(c: scalesAttr));
240 auto zeroPoints = dyn_cast<mlir::DenseElementsAttr>(Val: unwrap(c: zeroPointsAttr));
241
242 if (!scales || !zeroPoints) {
243 return {};
244 }
245
246 return wrap(cpp: quant::UniformQuantizedSubChannelType::get(
247 flags, storageType: unwrap(c: storageType), expressedType: unwrap(c: expressedType), scales, zeroPoints,
248 quantizedDimensions: llvm::ArrayRef<int32_t>(quantizedDimensions, nDims),
249 blockSizes: llvm::ArrayRef<int64_t>(blockSizes, nDims), storageTypeMin,
250 storageTypeMax));
251}
252
253MlirStringRef mlirUniformQuantizedSubChannelTypeGetName(void) {
254 return wrap(ref: quant::UniformQuantizedSubChannelType::name);
255}
256
257intptr_t mlirUniformQuantizedSubChannelTypeGetNumBlockSizes(MlirType type) {
258 return cast<quant::UniformQuantizedSubChannelType>(Val: unwrap(c: type))
259 .getBlockSizes()
260 .size();
261}
262
263int32_t mlirUniformQuantizedSubChannelTypeGetQuantizedDimension(MlirType type,
264 intptr_t pos) {
265 return cast<quant::UniformQuantizedSubChannelType>(Val: unwrap(c: type))
266 .getQuantizedDimensions()[pos];
267}
268
269int64_t mlirUniformQuantizedSubChannelTypeGetBlockSize(MlirType type,
270 intptr_t pos) {
271 return cast<quant::UniformQuantizedSubChannelType>(Val: unwrap(c: type))
272 .getBlockSizes()[pos];
273}
274
275MlirAttribute mlirUniformQuantizedSubChannelTypeGetScales(MlirType type) {
276 return wrap(
277 cpp: cast<quant::UniformQuantizedSubChannelType>(Val: unwrap(c: type)).getScales());
278}
279
280MlirAttribute mlirUniformQuantizedSubChannelTypeGetZeroPoints(MlirType type) {
281 return wrap(cpp: cast<quant::UniformQuantizedSubChannelType>(Val: unwrap(c: type))
282 .getZeroPoints());
283}
284
285//===---------------------------------------------------------------------===//
286// CalibratedQuantizedType
287//===---------------------------------------------------------------------===//
288
289bool mlirTypeIsACalibratedQuantizedType(MlirType type) {
290 return isa<quant::CalibratedQuantizedType>(Val: unwrap(c: type));
291}
292
293MlirTypeID mlirCalibratedQuantizedTypeGetTypeID(void) {
294 return wrap(cpp: quant::CalibratedQuantizedType::getTypeID());
295}
296
297MlirType mlirCalibratedQuantizedTypeGet(MlirType expressedType, double min,
298 double max) {
299 return wrap(
300 cpp: quant::CalibratedQuantizedType::get(expressedType: unwrap(c: expressedType), min, max));
301}
302
303MlirStringRef mlirCalibratedQuantizedTypeGetName(void) {
304 return wrap(ref: quant::CalibratedQuantizedType::name);
305}
306
307double mlirCalibratedQuantizedTypeGetMin(MlirType type) {
308 return cast<quant::CalibratedQuantizedType>(Val: unwrap(c: type)).getMin();
309}
310
311double mlirCalibratedQuantizedTypeGetMax(MlirType type) {
312 return cast<quant::CalibratedQuantizedType>(Val: unwrap(c: type)).getMax();
313}
314