| 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 | |
| 15 | using namespace mlir; |
| 16 | |
| 17 | MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(quant, quant, quant::QuantDialect) |
| 18 | |
| 19 | //===---------------------------------------------------------------------===// |
| 20 | // QuantizedType |
| 21 | //===---------------------------------------------------------------------===// |
| 22 | |
| 23 | bool mlirTypeIsAQuantizedType(MlirType type) { |
| 24 | return isa<quant::QuantizedType>(Val: unwrap(c: type)); |
| 25 | } |
| 26 | |
| 27 | unsigned mlirQuantizedTypeGetSignedFlag() { |
| 28 | return quant::QuantizationFlags::Signed; |
| 29 | } |
| 30 | |
| 31 | int64_t mlirQuantizedTypeGetDefaultMinimumForInteger(bool isSigned, |
| 32 | unsigned integralWidth) { |
| 33 | return quant::QuantizedType::getDefaultMinimumForInteger(isSigned, |
| 34 | integralWidth); |
| 35 | } |
| 36 | |
| 37 | int64_t mlirQuantizedTypeGetDefaultMaximumForInteger(bool isSigned, |
| 38 | unsigned integralWidth) { |
| 39 | return quant::QuantizedType::getDefaultMaximumForInteger(isSigned, |
| 40 | integralWidth); |
| 41 | } |
| 42 | |
| 43 | MlirType mlirQuantizedTypeGetExpressedType(MlirType type) { |
| 44 | return wrap(cpp: cast<quant::QuantizedType>(Val: unwrap(c: type)).getExpressedType()); |
| 45 | } |
| 46 | |
| 47 | unsigned mlirQuantizedTypeGetFlags(MlirType type) { |
| 48 | return cast<quant::QuantizedType>(Val: unwrap(c: type)).getFlags(); |
| 49 | } |
| 50 | |
| 51 | bool mlirQuantizedTypeIsSigned(MlirType type) { |
| 52 | return cast<quant::QuantizedType>(Val: unwrap(c: type)).isSigned(); |
| 53 | } |
| 54 | |
| 55 | MlirType mlirQuantizedTypeGetStorageType(MlirType type) { |
| 56 | return wrap(cpp: cast<quant::QuantizedType>(Val: unwrap(c: type)).getStorageType()); |
| 57 | } |
| 58 | |
| 59 | int64_t mlirQuantizedTypeGetStorageTypeMin(MlirType type) { |
| 60 | return cast<quant::QuantizedType>(Val: unwrap(c: type)).getStorageTypeMin(); |
| 61 | } |
| 62 | |
| 63 | int64_t mlirQuantizedTypeGetStorageTypeMax(MlirType type) { |
| 64 | return cast<quant::QuantizedType>(Val: unwrap(c: type)).getStorageTypeMax(); |
| 65 | } |
| 66 | |
| 67 | unsigned mlirQuantizedTypeGetStorageTypeIntegralWidth(MlirType type) { |
| 68 | return cast<quant::QuantizedType>(Val: unwrap(c: type)).getStorageTypeIntegralWidth(); |
| 69 | } |
| 70 | |
| 71 | bool mlirQuantizedTypeIsCompatibleExpressedType(MlirType type, |
| 72 | MlirType candidate) { |
| 73 | return cast<quant::QuantizedType>(Val: unwrap(c: type)) |
| 74 | .isCompatibleExpressedType(candidateExpressedType: unwrap(c: candidate)); |
| 75 | } |
| 76 | |
| 77 | MlirType mlirQuantizedTypeGetQuantizedElementType(MlirType type) { |
| 78 | return wrap(cpp: quant::QuantizedType::getQuantizedElementType(primitiveOrContainerType: unwrap(c: type))); |
| 79 | } |
| 80 | |
| 81 | MlirType 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 | |
| 87 | MlirType mlirQuantizedTypeCastToStorageType(MlirType type) { |
| 88 | return wrap(cpp: quant::QuantizedType::castToStorageType( |
| 89 | quantizedType: cast<quant::QuantizedType>(Val: unwrap(c: type)))); |
| 90 | } |
| 91 | |
| 92 | MlirType 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 | |
| 98 | MlirType mlirQuantizedTypeCastToExpressedType(MlirType type) { |
| 99 | return wrap(cpp: quant::QuantizedType::castToExpressedType(quantizedType: unwrap(c: type))); |
| 100 | } |
| 101 | |
| 102 | MlirType 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 | |
| 112 | bool mlirTypeIsAAnyQuantizedType(MlirType type) { |
| 113 | return isa<quant::AnyQuantizedType>(Val: unwrap(c: type)); |
| 114 | } |
| 115 | |
| 116 | MlirTypeID mlirAnyQuantizedTypeGetTypeID(void) { |
| 117 | return wrap(cpp: quant::AnyQuantizedType::getTypeID()); |
| 118 | } |
| 119 | |
| 120 | MlirType 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 | |
| 128 | MlirStringRef mlirAnyQuantizedTypeGetName(void) { |
| 129 | return wrap(ref: quant::AnyQuantizedType::name); |
| 130 | } |
| 131 | |
| 132 | //===---------------------------------------------------------------------===// |
| 133 | // UniformQuantizedType |
| 134 | //===---------------------------------------------------------------------===// |
| 135 | |
| 136 | bool mlirTypeIsAUniformQuantizedType(MlirType type) { |
| 137 | return isa<quant::UniformQuantizedType>(Val: unwrap(c: type)); |
| 138 | } |
| 139 | |
| 140 | MlirTypeID mlirUniformQuantizedTypeGetTypeID(void) { |
| 141 | return wrap(cpp: quant::UniformQuantizedType::getTypeID()); |
| 142 | } |
| 143 | |
| 144 | MlirType 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 | |
| 153 | MlirStringRef mlirUniformQuantizedTypeGetName(void) { |
| 154 | return wrap(ref: quant::UniformQuantizedType::name); |
| 155 | } |
| 156 | |
| 157 | double mlirUniformQuantizedTypeGetScale(MlirType type) { |
| 158 | return cast<quant::UniformQuantizedType>(Val: unwrap(c: type)).getScale(); |
| 159 | } |
| 160 | |
| 161 | int64_t mlirUniformQuantizedTypeGetZeroPoint(MlirType type) { |
| 162 | return cast<quant::UniformQuantizedType>(Val: unwrap(c: type)).getZeroPoint(); |
| 163 | } |
| 164 | |
| 165 | bool mlirUniformQuantizedTypeIsFixedPoint(MlirType type) { |
| 166 | return cast<quant::UniformQuantizedType>(Val: unwrap(c: type)).isFixedPoint(); |
| 167 | } |
| 168 | |
| 169 | //===---------------------------------------------------------------------===// |
| 170 | // UniformQuantizedPerAxisType |
| 171 | //===---------------------------------------------------------------------===// |
| 172 | |
| 173 | bool mlirTypeIsAUniformQuantizedPerAxisType(MlirType type) { |
| 174 | return isa<quant::UniformQuantizedPerAxisType>(Val: unwrap(c: type)); |
| 175 | } |
| 176 | |
| 177 | MlirTypeID mlirUniformQuantizedPerAxisTypeGetTypeID(void) { |
| 178 | return wrap(cpp: quant::UniformQuantizedPerAxisType::getTypeID()); |
| 179 | } |
| 180 | |
| 181 | MlirType 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 | |
| 192 | MlirStringRef mlirUniformQuantizedPerAxisTypeGetName(void) { |
| 193 | return wrap(ref: quant::UniformQuantizedPerAxisType::name); |
| 194 | } |
| 195 | |
| 196 | intptr_t mlirUniformQuantizedPerAxisTypeGetNumDims(MlirType type) { |
| 197 | return cast<quant::UniformQuantizedPerAxisType>(Val: unwrap(c: type)) |
| 198 | .getScales() |
| 199 | .size(); |
| 200 | } |
| 201 | |
| 202 | double mlirUniformQuantizedPerAxisTypeGetScale(MlirType type, intptr_t pos) { |
| 203 | return cast<quant::UniformQuantizedPerAxisType>(Val: unwrap(c: type)) |
| 204 | .getScales()[pos]; |
| 205 | } |
| 206 | |
| 207 | int64_t mlirUniformQuantizedPerAxisTypeGetZeroPoint(MlirType type, |
| 208 | intptr_t pos) { |
| 209 | return cast<quant::UniformQuantizedPerAxisType>(Val: unwrap(c: type)) |
| 210 | .getZeroPoints()[pos]; |
| 211 | } |
| 212 | |
| 213 | int32_t mlirUniformQuantizedPerAxisTypeGetQuantizedDimension(MlirType type) { |
| 214 | return cast<quant::UniformQuantizedPerAxisType>(Val: unwrap(c: type)) |
| 215 | .getQuantizedDimension(); |
| 216 | } |
| 217 | |
| 218 | bool mlirUniformQuantizedPerAxisTypeIsFixedPoint(MlirType type) { |
| 219 | return cast<quant::UniformQuantizedPerAxisType>(Val: unwrap(c: type)).isFixedPoint(); |
| 220 | } |
| 221 | |
| 222 | //===---------------------------------------------------------------------===// |
| 223 | // UniformQuantizedSubChannelType |
| 224 | //===---------------------------------------------------------------------===// |
| 225 | |
| 226 | bool mlirTypeIsAUniformQuantizedSubChannelType(MlirType type) { |
| 227 | return isa<quant::UniformQuantizedSubChannelType>(Val: unwrap(c: type)); |
| 228 | } |
| 229 | |
| 230 | MlirTypeID mlirUniformQuantizedSubChannelTypeGetTypeID(void) { |
| 231 | return wrap(cpp: quant::UniformQuantizedSubChannelType::getTypeID()); |
| 232 | } |
| 233 | |
| 234 | MlirType 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 | |
| 253 | MlirStringRef mlirUniformQuantizedSubChannelTypeGetName(void) { |
| 254 | return wrap(ref: quant::UniformQuantizedSubChannelType::name); |
| 255 | } |
| 256 | |
| 257 | intptr_t mlirUniformQuantizedSubChannelTypeGetNumBlockSizes(MlirType type) { |
| 258 | return cast<quant::UniformQuantizedSubChannelType>(Val: unwrap(c: type)) |
| 259 | .getBlockSizes() |
| 260 | .size(); |
| 261 | } |
| 262 | |
| 263 | int32_t mlirUniformQuantizedSubChannelTypeGetQuantizedDimension(MlirType type, |
| 264 | intptr_t pos) { |
| 265 | return cast<quant::UniformQuantizedSubChannelType>(Val: unwrap(c: type)) |
| 266 | .getQuantizedDimensions()[pos]; |
| 267 | } |
| 268 | |
| 269 | int64_t mlirUniformQuantizedSubChannelTypeGetBlockSize(MlirType type, |
| 270 | intptr_t pos) { |
| 271 | return cast<quant::UniformQuantizedSubChannelType>(Val: unwrap(c: type)) |
| 272 | .getBlockSizes()[pos]; |
| 273 | } |
| 274 | |
| 275 | MlirAttribute mlirUniformQuantizedSubChannelTypeGetScales(MlirType type) { |
| 276 | return wrap( |
| 277 | cpp: cast<quant::UniformQuantizedSubChannelType>(Val: unwrap(c: type)).getScales()); |
| 278 | } |
| 279 | |
| 280 | MlirAttribute mlirUniformQuantizedSubChannelTypeGetZeroPoints(MlirType type) { |
| 281 | return wrap(cpp: cast<quant::UniformQuantizedSubChannelType>(Val: unwrap(c: type)) |
| 282 | .getZeroPoints()); |
| 283 | } |
| 284 | |
| 285 | //===---------------------------------------------------------------------===// |
| 286 | // CalibratedQuantizedType |
| 287 | //===---------------------------------------------------------------------===// |
| 288 | |
| 289 | bool mlirTypeIsACalibratedQuantizedType(MlirType type) { |
| 290 | return isa<quant::CalibratedQuantizedType>(Val: unwrap(c: type)); |
| 291 | } |
| 292 | |
| 293 | MlirTypeID mlirCalibratedQuantizedTypeGetTypeID(void) { |
| 294 | return wrap(cpp: quant::CalibratedQuantizedType::getTypeID()); |
| 295 | } |
| 296 | |
| 297 | MlirType mlirCalibratedQuantizedTypeGet(MlirType expressedType, double min, |
| 298 | double max) { |
| 299 | return wrap( |
| 300 | cpp: quant::CalibratedQuantizedType::get(expressedType: unwrap(c: expressedType), min, max)); |
| 301 | } |
| 302 | |
| 303 | MlirStringRef mlirCalibratedQuantizedTypeGetName(void) { |
| 304 | return wrap(ref: quant::CalibratedQuantizedType::name); |
| 305 | } |
| 306 | |
| 307 | double mlirCalibratedQuantizedTypeGetMin(MlirType type) { |
| 308 | return cast<quant::CalibratedQuantizedType>(Val: unwrap(c: type)).getMin(); |
| 309 | } |
| 310 | |
| 311 | double mlirCalibratedQuantizedTypeGetMax(MlirType type) { |
| 312 | return cast<quant::CalibratedQuantizedType>(Val: unwrap(c: type)).getMax(); |
| 313 | } |
| 314 | |