xref: /llvm-project/mlir/include/mlir-c/Dialect/Quant.h (revision 5d91f79fced13604ff401e5f5a6d5c3a9062ab20)
1a8a2ee63SDenys Shabalin //===-- mlir-c/Dialect/Quant.h - C API for LLVM -------------------*- C -*-===//
29bcf13bfSAlex Zinenko //
39bcf13bfSAlex Zinenko // Part of the LLVM Project, under the Apache License v2.0 with LLVM
49bcf13bfSAlex Zinenko // Exceptions.
59bcf13bfSAlex Zinenko // See https://llvm.org/LICENSE.txt for license information.
69bcf13bfSAlex Zinenko // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
79bcf13bfSAlex Zinenko //
89bcf13bfSAlex Zinenko //===----------------------------------------------------------------------===//
99bcf13bfSAlex Zinenko 
109bcf13bfSAlex Zinenko #ifndef MLIR_C_DIALECT_QUANT_H
119bcf13bfSAlex Zinenko #define MLIR_C_DIALECT_QUANT_H
129bcf13bfSAlex Zinenko 
139bcf13bfSAlex Zinenko #include "mlir-c/IR.h"
149bcf13bfSAlex Zinenko 
159bcf13bfSAlex Zinenko #ifdef __cplusplus
169bcf13bfSAlex Zinenko extern "C" {
179bcf13bfSAlex Zinenko #endif
189bcf13bfSAlex Zinenko 
199bcf13bfSAlex Zinenko MLIR_DECLARE_CAPI_DIALECT_REGISTRATION(quant, quant);
209bcf13bfSAlex Zinenko 
219bcf13bfSAlex Zinenko //===---------------------------------------------------------------------===//
229bcf13bfSAlex Zinenko // QuantizedType
239bcf13bfSAlex Zinenko //===---------------------------------------------------------------------===//
249bcf13bfSAlex Zinenko 
259bcf13bfSAlex Zinenko /// Returns `true` if the given type is a quantization dialect type.
269bcf13bfSAlex Zinenko MLIR_CAPI_EXPORTED bool mlirTypeIsAQuantizedType(MlirType type);
279bcf13bfSAlex Zinenko 
289bcf13bfSAlex Zinenko /// Returns the bit flag used to indicate signedness of a quantized type.
29*5d91f79fSTom Eccles MLIR_CAPI_EXPORTED unsigned mlirQuantizedTypeGetSignedFlag(void);
309bcf13bfSAlex Zinenko 
319bcf13bfSAlex Zinenko /// Returns the minimum possible value stored by a quantized type.
329bcf13bfSAlex Zinenko MLIR_CAPI_EXPORTED int64_t mlirQuantizedTypeGetDefaultMinimumForInteger(
339bcf13bfSAlex Zinenko     bool isSigned, unsigned integralWidth);
349bcf13bfSAlex Zinenko 
359bcf13bfSAlex Zinenko /// Returns the maximum possible value stored by a quantized type.
369bcf13bfSAlex Zinenko MLIR_CAPI_EXPORTED int64_t mlirQuantizedTypeGetDefaultMaximumForInteger(
379bcf13bfSAlex Zinenko     bool isSigned, unsigned integralWidth);
389bcf13bfSAlex Zinenko 
399bcf13bfSAlex Zinenko /// Gets the original type approximated by the given quantized type.
409bcf13bfSAlex Zinenko MLIR_CAPI_EXPORTED MlirType mlirQuantizedTypeGetExpressedType(MlirType type);
419bcf13bfSAlex Zinenko 
429bcf13bfSAlex Zinenko /// Gets the flags associated with the given quantized type.
439bcf13bfSAlex Zinenko MLIR_CAPI_EXPORTED unsigned mlirQuantizedTypeGetFlags(MlirType type);
449bcf13bfSAlex Zinenko 
459bcf13bfSAlex Zinenko /// Returns `true` if the given type is signed, `false` otherwise.
469bcf13bfSAlex Zinenko MLIR_CAPI_EXPORTED bool mlirQuantizedTypeIsSigned(MlirType type);
479bcf13bfSAlex Zinenko 
489bcf13bfSAlex Zinenko /// Returns the underlying type used to store the values.
499bcf13bfSAlex Zinenko MLIR_CAPI_EXPORTED MlirType mlirQuantizedTypeGetStorageType(MlirType type);
509bcf13bfSAlex Zinenko 
519bcf13bfSAlex Zinenko /// Returns the minimum value that the storage type of the given quantized type
529bcf13bfSAlex Zinenko /// can take.
539bcf13bfSAlex Zinenko MLIR_CAPI_EXPORTED int64_t mlirQuantizedTypeGetStorageTypeMin(MlirType type);
549bcf13bfSAlex Zinenko 
559bcf13bfSAlex Zinenko /// Returns the maximum value that the storage type of the given quantized type
569bcf13bfSAlex Zinenko /// can take.
579bcf13bfSAlex Zinenko MLIR_CAPI_EXPORTED int64_t mlirQuantizedTypeGetStorageTypeMax(MlirType type);
589bcf13bfSAlex Zinenko 
599bcf13bfSAlex Zinenko /// Returns the integral bitwidth that the storage type of the given quantized
609bcf13bfSAlex Zinenko /// type can represent exactly.
619bcf13bfSAlex Zinenko MLIR_CAPI_EXPORTED unsigned
629bcf13bfSAlex Zinenko mlirQuantizedTypeGetStorageTypeIntegralWidth(MlirType type);
639bcf13bfSAlex Zinenko 
649bcf13bfSAlex Zinenko /// Returns `true` if the `candidate` type is compatible with the given
659bcf13bfSAlex Zinenko /// quantized `type`.
669bcf13bfSAlex Zinenko MLIR_CAPI_EXPORTED bool
679bcf13bfSAlex Zinenko mlirQuantizedTypeIsCompatibleExpressedType(MlirType type, MlirType candidate);
689bcf13bfSAlex Zinenko 
699bcf13bfSAlex Zinenko /// Returns the element type of the given quantized type as another quantized
709bcf13bfSAlex Zinenko /// type.
719bcf13bfSAlex Zinenko MLIR_CAPI_EXPORTED MlirType
729bcf13bfSAlex Zinenko mlirQuantizedTypeGetQuantizedElementType(MlirType type);
739bcf13bfSAlex Zinenko 
749bcf13bfSAlex Zinenko /// Casts from a type based on the storage type of the given type to a
759bcf13bfSAlex Zinenko /// corresponding type based on the given type. Returns a null type if the cast
769bcf13bfSAlex Zinenko /// is not valid.
779bcf13bfSAlex Zinenko MLIR_CAPI_EXPORTED MlirType
789bcf13bfSAlex Zinenko mlirQuantizedTypeCastFromStorageType(MlirType type, MlirType candidate);
799bcf13bfSAlex Zinenko 
809bcf13bfSAlex Zinenko /// Casts from a type based on a quantized type to a corresponding typed based
819bcf13bfSAlex Zinenko /// on the storage type. Returns a null type if the cast is not valid.
829bcf13bfSAlex Zinenko MLIR_CAPI_EXPORTED MlirType mlirQuantizedTypeCastToStorageType(MlirType type);
839bcf13bfSAlex Zinenko 
849bcf13bfSAlex Zinenko /// Casts from a type based on the expressed type of the given type to a
859bcf13bfSAlex Zinenko /// corresponding type based on the given type. Returns a null type if the cast
869bcf13bfSAlex Zinenko /// is not valid.
879bcf13bfSAlex Zinenko MLIR_CAPI_EXPORTED MlirType
889bcf13bfSAlex Zinenko mlirQuantizedTypeCastFromExpressedType(MlirType type, MlirType candidate);
899bcf13bfSAlex Zinenko 
909bcf13bfSAlex Zinenko /// Casts from a type based on a quantized type to a corresponding typed based
919bcf13bfSAlex Zinenko /// on the expressed type. Returns a null type if the cast is not valid.
929bcf13bfSAlex Zinenko MLIR_CAPI_EXPORTED MlirType mlirQuantizedTypeCastToExpressedType(MlirType type);
939bcf13bfSAlex Zinenko 
949bcf13bfSAlex Zinenko /// Casts from a type based on the expressed type of the given quantized type to
959bcf13bfSAlex Zinenko /// equivalent type based on storage type of the same quantized type.
969bcf13bfSAlex Zinenko MLIR_CAPI_EXPORTED MlirType
979bcf13bfSAlex Zinenko mlirQuantizedTypeCastExpressedToStorageType(MlirType type, MlirType candidate);
989bcf13bfSAlex Zinenko 
999bcf13bfSAlex Zinenko //===---------------------------------------------------------------------===//
1009bcf13bfSAlex Zinenko // AnyQuantizedType
1019bcf13bfSAlex Zinenko //===---------------------------------------------------------------------===//
1029bcf13bfSAlex Zinenko 
1039bcf13bfSAlex Zinenko /// Returns `true` if the given type is an AnyQuantizedType.
1049bcf13bfSAlex Zinenko MLIR_CAPI_EXPORTED bool mlirTypeIsAAnyQuantizedType(MlirType type);
1059bcf13bfSAlex Zinenko 
1069bcf13bfSAlex Zinenko /// Creates an instance of AnyQuantizedType with the given parameters in the
1079bcf13bfSAlex Zinenko /// same context as `storageType` and returns it. The instance is owned by the
1089bcf13bfSAlex Zinenko /// context.
1099bcf13bfSAlex Zinenko MLIR_CAPI_EXPORTED MlirType mlirAnyQuantizedTypeGet(unsigned flags,
1109bcf13bfSAlex Zinenko                                                     MlirType storageType,
1119bcf13bfSAlex Zinenko                                                     MlirType expressedType,
1129bcf13bfSAlex Zinenko                                                     int64_t storageTypeMin,
1139bcf13bfSAlex Zinenko                                                     int64_t storageTypeMax);
1149bcf13bfSAlex Zinenko 
1159bcf13bfSAlex Zinenko //===---------------------------------------------------------------------===//
1169bcf13bfSAlex Zinenko // UniformQuantizedType
1179bcf13bfSAlex Zinenko //===---------------------------------------------------------------------===//
1189bcf13bfSAlex Zinenko 
1199bcf13bfSAlex Zinenko /// Returns `true` if the given type is a UniformQuantizedType.
1209bcf13bfSAlex Zinenko MLIR_CAPI_EXPORTED bool mlirTypeIsAUniformQuantizedType(MlirType type);
1219bcf13bfSAlex Zinenko 
1229bcf13bfSAlex Zinenko /// Creates an instance of UniformQuantizedType with the given parameters in the
1239bcf13bfSAlex Zinenko /// same context as `storageType` and returns it. The instance is owned by the
1249bcf13bfSAlex Zinenko /// context.
1259bcf13bfSAlex Zinenko MLIR_CAPI_EXPORTED MlirType mlirUniformQuantizedTypeGet(
1269bcf13bfSAlex Zinenko     unsigned flags, MlirType storageType, MlirType expressedType, double scale,
1279bcf13bfSAlex Zinenko     int64_t zeroPoint, int64_t storageTypeMin, int64_t storageTypeMax);
1289bcf13bfSAlex Zinenko 
1299bcf13bfSAlex Zinenko /// Returns the scale of the given uniform quantized type.
1309bcf13bfSAlex Zinenko MLIR_CAPI_EXPORTED double mlirUniformQuantizedTypeGetScale(MlirType type);
1319bcf13bfSAlex Zinenko 
1329bcf13bfSAlex Zinenko /// Returns the zero point of the given uniform quantized type.
1339bcf13bfSAlex Zinenko MLIR_CAPI_EXPORTED int64_t mlirUniformQuantizedTypeGetZeroPoint(MlirType type);
1349bcf13bfSAlex Zinenko 
1359bcf13bfSAlex Zinenko /// Returns `true` if the given uniform quantized type is fixed-point.
1369bcf13bfSAlex Zinenko MLIR_CAPI_EXPORTED bool mlirUniformQuantizedTypeIsFixedPoint(MlirType type);
1379bcf13bfSAlex Zinenko 
1389bcf13bfSAlex Zinenko //===---------------------------------------------------------------------===//
1399bcf13bfSAlex Zinenko // UniformQuantizedPerAxisType
1409bcf13bfSAlex Zinenko //===---------------------------------------------------------------------===//
1419bcf13bfSAlex Zinenko 
1429bcf13bfSAlex Zinenko /// Returns `true` if the given type is a UniformQuantizedPerAxisType.
1439bcf13bfSAlex Zinenko MLIR_CAPI_EXPORTED bool mlirTypeIsAUniformQuantizedPerAxisType(MlirType type);
1449bcf13bfSAlex Zinenko 
1459bcf13bfSAlex Zinenko /// Creates an instance of UniformQuantizedPerAxisType with the given parameters
1469bcf13bfSAlex Zinenko /// in the same context as `storageType` and returns it. `scales` and
1479bcf13bfSAlex Zinenko /// `zeroPoints` point to `nDims` number of elements. The instance is owned
1489bcf13bfSAlex Zinenko /// by the context.
1499bcf13bfSAlex Zinenko MLIR_CAPI_EXPORTED MlirType mlirUniformQuantizedPerAxisTypeGet(
1509bcf13bfSAlex Zinenko     unsigned flags, MlirType storageType, MlirType expressedType,
1519bcf13bfSAlex Zinenko     intptr_t nDims, double *scales, int64_t *zeroPoints,
1529bcf13bfSAlex Zinenko     int32_t quantizedDimension, int64_t storageTypeMin, int64_t storageTypeMax);
1539bcf13bfSAlex Zinenko 
1549bcf13bfSAlex Zinenko /// Returns the number of axes in the given quantized per-axis type.
1559bcf13bfSAlex Zinenko MLIR_CAPI_EXPORTED intptr_t
1569bcf13bfSAlex Zinenko mlirUniformQuantizedPerAxisTypeGetNumDims(MlirType type);
1579bcf13bfSAlex Zinenko 
1589bcf13bfSAlex Zinenko /// Returns `pos`-th scale of the given quantized per-axis type.
1599bcf13bfSAlex Zinenko MLIR_CAPI_EXPORTED double mlirUniformQuantizedPerAxisTypeGetScale(MlirType type,
1609bcf13bfSAlex Zinenko                                                                   intptr_t pos);
1619bcf13bfSAlex Zinenko 
1629bcf13bfSAlex Zinenko /// Returns `pos`-th zero point of the given quantized per-axis type.
1639bcf13bfSAlex Zinenko MLIR_CAPI_EXPORTED int64_t
1649bcf13bfSAlex Zinenko mlirUniformQuantizedPerAxisTypeGetZeroPoint(MlirType type, intptr_t pos);
1659bcf13bfSAlex Zinenko 
1669bcf13bfSAlex Zinenko /// Returns the index of the quantized dimension in the given quantized per-axis
1679bcf13bfSAlex Zinenko /// type.
1689bcf13bfSAlex Zinenko MLIR_CAPI_EXPORTED int32_t
1699bcf13bfSAlex Zinenko mlirUniformQuantizedPerAxisTypeGetQuantizedDimension(MlirType type);
1709bcf13bfSAlex Zinenko 
1719bcf13bfSAlex Zinenko /// Returns `true` if the given uniform quantized per-axis type is fixed-point.
1729bcf13bfSAlex Zinenko MLIR_CAPI_EXPORTED bool
1739bcf13bfSAlex Zinenko mlirUniformQuantizedPerAxisTypeIsFixedPoint(MlirType type);
1749bcf13bfSAlex Zinenko 
1759bcf13bfSAlex Zinenko //===---------------------------------------------------------------------===//
1769bcf13bfSAlex Zinenko // CalibratedQuantizedType
1779bcf13bfSAlex Zinenko //===---------------------------------------------------------------------===//
1789bcf13bfSAlex Zinenko 
1799bcf13bfSAlex Zinenko /// Returns `true` if the given type is a CalibratedQuantizedType.
1809bcf13bfSAlex Zinenko MLIR_CAPI_EXPORTED bool mlirTypeIsACalibratedQuantizedType(MlirType type);
1819bcf13bfSAlex Zinenko 
1829bcf13bfSAlex Zinenko /// Creates an instance of CalibratedQuantizedType with the given parameters
1839bcf13bfSAlex Zinenko /// in the same context as `expressedType` and returns it. The instance is owned
1849bcf13bfSAlex Zinenko /// by the context.
1859bcf13bfSAlex Zinenko MLIR_CAPI_EXPORTED MlirType
1869bcf13bfSAlex Zinenko mlirCalibratedQuantizedTypeGet(MlirType expressedType, double min, double max);
1879bcf13bfSAlex Zinenko 
1889bcf13bfSAlex Zinenko /// Returns the min value of the given calibrated quantized type.
1899bcf13bfSAlex Zinenko MLIR_CAPI_EXPORTED double mlirCalibratedQuantizedTypeGetMin(MlirType type);
1909bcf13bfSAlex Zinenko 
1919bcf13bfSAlex Zinenko /// Returns the max value of the given calibrated quantized type.
1929bcf13bfSAlex Zinenko MLIR_CAPI_EXPORTED double mlirCalibratedQuantizedTypeGetMax(MlirType type);
1939bcf13bfSAlex Zinenko 
1949bcf13bfSAlex Zinenko #ifdef __cplusplus
1959bcf13bfSAlex Zinenko }
1969bcf13bfSAlex Zinenko #endif
1979bcf13bfSAlex Zinenko 
1989bcf13bfSAlex Zinenko #endif // MLIR_C_DIALECT_QUANT_H
199