274 lines · cpp
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-exception6//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// QuantizedType21//===---------------------------------------------------------------------===//22 23bool mlirTypeIsAQuantizedType(MlirType type) {24 return isa<quant::QuantizedType>(unwrap(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(cast<quant::QuantizedType>(unwrap(type)).getExpressedType());45}46 47unsigned mlirQuantizedTypeGetFlags(MlirType type) {48 return cast<quant::QuantizedType>(unwrap(type)).getFlags();49}50 51bool mlirQuantizedTypeIsSigned(MlirType type) {52 return cast<quant::QuantizedType>(unwrap(type)).isSigned();53}54 55MlirType mlirQuantizedTypeGetStorageType(MlirType type) {56 return wrap(cast<quant::QuantizedType>(unwrap(type)).getStorageType());57}58 59int64_t mlirQuantizedTypeGetStorageTypeMin(MlirType type) {60 return cast<quant::QuantizedType>(unwrap(type)).getStorageTypeMin();61}62 63int64_t mlirQuantizedTypeGetStorageTypeMax(MlirType type) {64 return cast<quant::QuantizedType>(unwrap(type)).getStorageTypeMax();65}66 67unsigned mlirQuantizedTypeGetStorageTypeIntegralWidth(MlirType type) {68 return cast<quant::QuantizedType>(unwrap(type)).getStorageTypeIntegralWidth();69}70 71bool mlirQuantizedTypeIsCompatibleExpressedType(MlirType type,72 MlirType candidate) {73 return cast<quant::QuantizedType>(unwrap(type))74 .isCompatibleExpressedType(unwrap(candidate));75}76 77MlirType mlirQuantizedTypeGetQuantizedElementType(MlirType type) {78 return wrap(quant::QuantizedType::getQuantizedElementType(unwrap(type)));79}80 81MlirType mlirQuantizedTypeCastFromStorageType(MlirType type,82 MlirType candidate) {83 return wrap(cast<quant::QuantizedType>(unwrap(type))84 .castFromStorageType(unwrap(candidate)));85}86 87MlirType mlirQuantizedTypeCastToStorageType(MlirType type) {88 return wrap(quant::QuantizedType::castToStorageType(89 cast<quant::QuantizedType>(unwrap(type))));90}91 92MlirType mlirQuantizedTypeCastFromExpressedType(MlirType type,93 MlirType candidate) {94 return wrap(cast<quant::QuantizedType>(unwrap(type))95 .castFromExpressedType(unwrap(candidate)));96}97 98MlirType mlirQuantizedTypeCastToExpressedType(MlirType type) {99 return wrap(quant::QuantizedType::castToExpressedType(unwrap(type)));100}101 102MlirType mlirQuantizedTypeCastExpressedToStorageType(MlirType type,103 MlirType candidate) {104 return wrap(cast<quant::QuantizedType>(unwrap(type))105 .castExpressedToStorageType(unwrap(candidate)));106}107 108//===---------------------------------------------------------------------===//109// AnyQuantizedType110//===---------------------------------------------------------------------===//111 112bool mlirTypeIsAAnyQuantizedType(MlirType type) {113 return isa<quant::AnyQuantizedType>(unwrap(type));114}115 116MlirType mlirAnyQuantizedTypeGet(unsigned flags, MlirType storageType,117 MlirType expressedType, int64_t storageTypeMin,118 int64_t storageTypeMax) {119 return wrap(quant::AnyQuantizedType::get(flags, unwrap(storageType),120 unwrap(expressedType),121 storageTypeMin, storageTypeMax));122}123 124//===---------------------------------------------------------------------===//125// UniformQuantizedType126//===---------------------------------------------------------------------===//127 128bool mlirTypeIsAUniformQuantizedType(MlirType type) {129 return isa<quant::UniformQuantizedType>(unwrap(type));130}131 132MlirType mlirUniformQuantizedTypeGet(unsigned flags, MlirType storageType,133 MlirType expressedType, double scale,134 int64_t zeroPoint, int64_t storageTypeMin,135 int64_t storageTypeMax) {136 return wrap(quant::UniformQuantizedType::get(137 flags, unwrap(storageType), unwrap(expressedType), scale, zeroPoint,138 storageTypeMin, storageTypeMax));139}140 141double mlirUniformQuantizedTypeGetScale(MlirType type) {142 return cast<quant::UniformQuantizedType>(unwrap(type)).getScale();143}144 145int64_t mlirUniformQuantizedTypeGetZeroPoint(MlirType type) {146 return cast<quant::UniformQuantizedType>(unwrap(type)).getZeroPoint();147}148 149bool mlirUniformQuantizedTypeIsFixedPoint(MlirType type) {150 return cast<quant::UniformQuantizedType>(unwrap(type)).isFixedPoint();151}152 153//===---------------------------------------------------------------------===//154// UniformQuantizedPerAxisType155//===---------------------------------------------------------------------===//156 157bool mlirTypeIsAUniformQuantizedPerAxisType(MlirType type) {158 return isa<quant::UniformQuantizedPerAxisType>(unwrap(type));159}160 161MlirType mlirUniformQuantizedPerAxisTypeGet(162 unsigned flags, MlirType storageType, MlirType expressedType,163 intptr_t nDims, double *scales, int64_t *zeroPoints,164 int32_t quantizedDimension, int64_t storageTypeMin,165 int64_t storageTypeMax) {166 return wrap(quant::UniformQuantizedPerAxisType::get(167 flags, unwrap(storageType), unwrap(expressedType),168 llvm::ArrayRef(scales, nDims), llvm::ArrayRef(zeroPoints, nDims),169 quantizedDimension, storageTypeMin, storageTypeMax));170}171 172intptr_t mlirUniformQuantizedPerAxisTypeGetNumDims(MlirType type) {173 return cast<quant::UniformQuantizedPerAxisType>(unwrap(type))174 .getScales()175 .size();176}177 178double mlirUniformQuantizedPerAxisTypeGetScale(MlirType type, intptr_t pos) {179 return cast<quant::UniformQuantizedPerAxisType>(unwrap(type))180 .getScales()[pos];181}182 183int64_t mlirUniformQuantizedPerAxisTypeGetZeroPoint(MlirType type,184 intptr_t pos) {185 return cast<quant::UniformQuantizedPerAxisType>(unwrap(type))186 .getZeroPoints()[pos];187}188 189int32_t mlirUniformQuantizedPerAxisTypeGetQuantizedDimension(MlirType type) {190 return cast<quant::UniformQuantizedPerAxisType>(unwrap(type))191 .getQuantizedDimension();192}193 194bool mlirUniformQuantizedPerAxisTypeIsFixedPoint(MlirType type) {195 return cast<quant::UniformQuantizedPerAxisType>(unwrap(type)).isFixedPoint();196}197 198//===---------------------------------------------------------------------===//199// UniformQuantizedSubChannelType200//===---------------------------------------------------------------------===//201 202bool mlirTypeIsAUniformQuantizedSubChannelType(MlirType type) {203 return isa<quant::UniformQuantizedSubChannelType>(unwrap(type));204}205 206MlirType mlirUniformQuantizedSubChannelTypeGet(207 unsigned flags, MlirType storageType, MlirType expressedType,208 MlirAttribute scalesAttr, MlirAttribute zeroPointsAttr, intptr_t nDims,209 int32_t *quantizedDimensions, int64_t *blockSizes, int64_t storageTypeMin,210 int64_t storageTypeMax) {211 auto scales = dyn_cast<mlir::DenseElementsAttr>(unwrap(scalesAttr));212 auto zeroPoints = dyn_cast<mlir::DenseElementsAttr>(unwrap(zeroPointsAttr));213 214 if (!scales || !zeroPoints) {215 return {};216 }217 218 return wrap(quant::UniformQuantizedSubChannelType::get(219 flags, unwrap(storageType), unwrap(expressedType), scales, zeroPoints,220 llvm::ArrayRef<int32_t>(quantizedDimensions, nDims),221 llvm::ArrayRef<int64_t>(blockSizes, nDims), storageTypeMin,222 storageTypeMax));223}224 225intptr_t mlirUniformQuantizedSubChannelTypeGetNumBlockSizes(MlirType type) {226 return cast<quant::UniformQuantizedSubChannelType>(unwrap(type))227 .getBlockSizes()228 .size();229}230 231int32_t mlirUniformQuantizedSubChannelTypeGetQuantizedDimension(MlirType type,232 intptr_t pos) {233 return cast<quant::UniformQuantizedSubChannelType>(unwrap(type))234 .getQuantizedDimensions()[pos];235}236 237int64_t mlirUniformQuantizedSubChannelTypeGetBlockSize(MlirType type,238 intptr_t pos) {239 return cast<quant::UniformQuantizedSubChannelType>(unwrap(type))240 .getBlockSizes()[pos];241}242 243MlirAttribute mlirUniformQuantizedSubChannelTypeGetScales(MlirType type) {244 return wrap(245 cast<quant::UniformQuantizedSubChannelType>(unwrap(type)).getScales());246}247 248MlirAttribute mlirUniformQuantizedSubChannelTypeGetZeroPoints(MlirType type) {249 return wrap(cast<quant::UniformQuantizedSubChannelType>(unwrap(type))250 .getZeroPoints());251}252 253//===---------------------------------------------------------------------===//254// CalibratedQuantizedType255//===---------------------------------------------------------------------===//256 257bool mlirTypeIsACalibratedQuantizedType(MlirType type) {258 return isa<quant::CalibratedQuantizedType>(unwrap(type));259}260 261MlirType mlirCalibratedQuantizedTypeGet(MlirType expressedType, double min,262 double max) {263 return wrap(264 quant::CalibratedQuantizedType::get(unwrap(expressedType), min, max));265}266 267double mlirCalibratedQuantizedTypeGetMin(MlirType type) {268 return cast<quant::CalibratedQuantizedType>(unwrap(type)).getMin();269}270 271double mlirCalibratedQuantizedTypeGetMax(MlirType type) {272 return cast<quant::CalibratedQuantizedType>(unwrap(type)).getMax();273}274