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 16 extern "C" { 17 #endif 18 19 MLIR_DECLARE_CAPI_DIALECT_REGISTRATION(quant, quant); 20 21 //===---------------------------------------------------------------------===// 22 // QuantizedType 23 //===---------------------------------------------------------------------===// 24 25 /// Returns `true` if the given type is a quantization dialect type. 26 MLIR_CAPI_EXPORTED bool mlirTypeIsAQuantizedType(MlirType type); 27 28 /// Returns the bit flag used to indicate signedness of a quantized type. 29 MLIR_CAPI_EXPORTED unsigned mlirQuantizedTypeGetSignedFlag(void); 30 31 /// Returns the minimum possible value stored by a quantized type. 32 MLIR_CAPI_EXPORTED int64_t mlirQuantizedTypeGetDefaultMinimumForInteger( 33 bool isSigned, unsigned integralWidth); 34 35 /// Returns the maximum possible value stored by a quantized type. 36 MLIR_CAPI_EXPORTED int64_t mlirQuantizedTypeGetDefaultMaximumForInteger( 37 bool isSigned, unsigned integralWidth); 38 39 /// Gets the original type approximated by the given quantized type. 40 MLIR_CAPI_EXPORTED MlirType mlirQuantizedTypeGetExpressedType(MlirType type); 41 42 /// Gets the flags associated with the given quantized type. 43 MLIR_CAPI_EXPORTED unsigned mlirQuantizedTypeGetFlags(MlirType type); 44 45 /// Returns `true` if the given type is signed, `false` otherwise. 46 MLIR_CAPI_EXPORTED bool mlirQuantizedTypeIsSigned(MlirType type); 47 48 /// Returns the underlying type used to store the values. 49 MLIR_CAPI_EXPORTED MlirType mlirQuantizedTypeGetStorageType(MlirType type); 50 51 /// Returns the minimum value that the storage type of the given quantized type 52 /// can take. 53 MLIR_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. 57 MLIR_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. 61 MLIR_CAPI_EXPORTED unsigned 62 mlirQuantizedTypeGetStorageTypeIntegralWidth(MlirType type); 63 64 /// Returns `true` if the `candidate` type is compatible with the given 65 /// quantized `type`. 66 MLIR_CAPI_EXPORTED bool 67 mlirQuantizedTypeIsCompatibleExpressedType(MlirType type, MlirType candidate); 68 69 /// Returns the element type of the given quantized type as another quantized 70 /// type. 71 MLIR_CAPI_EXPORTED MlirType 72 mlirQuantizedTypeGetQuantizedElementType(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. 77 MLIR_CAPI_EXPORTED MlirType 78 mlirQuantizedTypeCastFromStorageType(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. 82 MLIR_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. 87 MLIR_CAPI_EXPORTED MlirType 88 mlirQuantizedTypeCastFromExpressedType(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. 92 MLIR_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. 96 MLIR_CAPI_EXPORTED MlirType 97 mlirQuantizedTypeCastExpressedToStorageType(MlirType type, MlirType candidate); 98 99 //===---------------------------------------------------------------------===// 100 // AnyQuantizedType 101 //===---------------------------------------------------------------------===// 102 103 /// Returns `true` if the given type is an AnyQuantizedType. 104 MLIR_CAPI_EXPORTED bool mlirTypeIsAAnyQuantizedType(MlirType type); 105 106 /// Creates an instance of AnyQuantizedType with the given parameters in the 107 /// same context as `storageType` and returns it. The instance is owned by the 108 /// context. 109 MLIR_CAPI_EXPORTED MlirType mlirAnyQuantizedTypeGet(unsigned flags, 110 MlirType storageType, 111 MlirType expressedType, 112 int64_t storageTypeMin, 113 int64_t storageTypeMax); 114 115 //===---------------------------------------------------------------------===// 116 // UniformQuantizedType 117 //===---------------------------------------------------------------------===// 118 119 /// Returns `true` if the given type is a UniformQuantizedType. 120 MLIR_CAPI_EXPORTED bool mlirTypeIsAUniformQuantizedType(MlirType type); 121 122 /// Creates an instance of UniformQuantizedType with the given parameters in the 123 /// same context as `storageType` and returns it. The instance is owned by the 124 /// context. 125 MLIR_CAPI_EXPORTED MlirType mlirUniformQuantizedTypeGet( 126 unsigned flags, MlirType storageType, MlirType expressedType, double scale, 127 int64_t zeroPoint, int64_t storageTypeMin, int64_t storageTypeMax); 128 129 /// Returns the scale of the given uniform quantized type. 130 MLIR_CAPI_EXPORTED double mlirUniformQuantizedTypeGetScale(MlirType type); 131 132 /// Returns the zero point of the given uniform quantized type. 133 MLIR_CAPI_EXPORTED int64_t mlirUniformQuantizedTypeGetZeroPoint(MlirType type); 134 135 /// Returns `true` if the given uniform quantized type is fixed-point. 136 MLIR_CAPI_EXPORTED bool mlirUniformQuantizedTypeIsFixedPoint(MlirType type); 137 138 //===---------------------------------------------------------------------===// 139 // UniformQuantizedPerAxisType 140 //===---------------------------------------------------------------------===// 141 142 /// Returns `true` if the given type is a UniformQuantizedPerAxisType. 143 MLIR_CAPI_EXPORTED bool mlirTypeIsAUniformQuantizedPerAxisType(MlirType type); 144 145 /// Creates an instance of UniformQuantizedPerAxisType with the given parameters 146 /// in the same context as `storageType` and returns it. `scales` and 147 /// `zeroPoints` point to `nDims` number of elements. The instance is owned 148 /// by the context. 149 MLIR_CAPI_EXPORTED MlirType mlirUniformQuantizedPerAxisTypeGet( 150 unsigned flags, MlirType storageType, MlirType expressedType, 151 intptr_t nDims, double *scales, int64_t *zeroPoints, 152 int32_t quantizedDimension, int64_t storageTypeMin, int64_t storageTypeMax); 153 154 /// Returns the number of axes in the given quantized per-axis type. 155 MLIR_CAPI_EXPORTED intptr_t 156 mlirUniformQuantizedPerAxisTypeGetNumDims(MlirType type); 157 158 /// Returns `pos`-th scale of the given quantized per-axis type. 159 MLIR_CAPI_EXPORTED double mlirUniformQuantizedPerAxisTypeGetScale(MlirType type, 160 intptr_t pos); 161 162 /// Returns `pos`-th zero point of the given quantized per-axis type. 163 MLIR_CAPI_EXPORTED int64_t 164 mlirUniformQuantizedPerAxisTypeGetZeroPoint(MlirType type, intptr_t pos); 165 166 /// Returns the index of the quantized dimension in the given quantized per-axis 167 /// type. 168 MLIR_CAPI_EXPORTED int32_t 169 mlirUniformQuantizedPerAxisTypeGetQuantizedDimension(MlirType type); 170 171 /// Returns `true` if the given uniform quantized per-axis type is fixed-point. 172 MLIR_CAPI_EXPORTED bool 173 mlirUniformQuantizedPerAxisTypeIsFixedPoint(MlirType type); 174 175 //===---------------------------------------------------------------------===// 176 // CalibratedQuantizedType 177 //===---------------------------------------------------------------------===// 178 179 /// Returns `true` if the given type is a CalibratedQuantizedType. 180 MLIR_CAPI_EXPORTED bool mlirTypeIsACalibratedQuantizedType(MlirType type); 181 182 /// Creates an instance of CalibratedQuantizedType with the given parameters 183 /// in the same context as `expressedType` and returns it. The instance is owned 184 /// by the context. 185 MLIR_CAPI_EXPORTED MlirType 186 mlirCalibratedQuantizedTypeGet(MlirType expressedType, double min, double max); 187 188 /// Returns the min value of the given calibrated quantized type. 189 MLIR_CAPI_EXPORTED double mlirCalibratedQuantizedTypeGetMin(MlirType type); 190 191 /// Returns the max value of the given calibrated quantized type. 192 MLIR_CAPI_EXPORTED double mlirCalibratedQuantizedTypeGetMax(MlirType type); 193 194 #ifdef __cplusplus 195 } 196 #endif 197 198 #endif // MLIR_C_DIALECT_QUANT_H 199