38 "default_minimum_for_integer",
39 [](
bool isSigned,
unsigned integralWidth) {
43 "Default minimum value for the integer with the specified signedness "
46 nb::arg(
"is_signed"), nb::arg(
"integral_width"));
48 "default_maximum_for_integer",
49 [](
bool isSigned,
unsigned integralWidth) {
53 "Default maximum value for the integer with the specified signedness "
56 nb::arg(
"is_signed"), nb::arg(
"integral_width"));
60 return PyType(type.getContext(),
64 "Type expressed by this quantized type.");
70 "Flags of this quantized type (named accessors should be preferred to "
77 "Signedness of this quantized type.");
81 return PyType(type.getContext(),
85 "Storage type backing this quantized type.");
91 "The minimum value held by the storage type of this quantized type.");
97 "The maximum value held by the storage type of this quantized type.");
99 "storage_type_integral_width",
103 "The bitwidth of the storage type of this quantized type.");
105 "is_compatible_expressed_type",
109 "Checks whether the candidate type can be expressed by this quantized "
111 nb::arg(
"candidate"));
113 "quantized_element_type",
115 return PyType(type.getContext(),
119 "Element type of this quantized type expressed as quantized type.");
121 "cast_from_storage_type",
123 MlirType castResult =
127 throw nb::type_error(
"Invalid cast.");
129 "Casts from a type based on the storage type of this quantized type to "
131 "corresponding type based on the quantized type. Raises TypeError if "
133 "cast is not valid.",
134 nb::arg(
"candidate"));
136 "cast_to_storage_type",
140 return PyType(type.getContext(), castResult).maybeDownCast();
141 throw nb::type_error(
"Invalid cast.");
143 "Casts from a type based on a quantized type to a corresponding type "
144 "based on the storage type of this quantized type. Raises TypeError if "
145 "the cast is not valid.",
148 "cast_from_expressed_type",
150 MlirType castResult =
153 return PyType(type.getContext(), castResult).maybeDownCast();
154 throw nb::type_error(
"Invalid cast.");
156 "Casts from a type based on the expressed type of this quantized type "
158 "a corresponding type based on the quantized type. Raises TypeError if "
159 "the cast is not valid.",
160 nb::arg(
"candidate"));
162 "cast_to_expressed_type",
166 return PyType(type.getContext(), castResult).maybeDownCast();
167 throw nb::type_error(
"Invalid cast.");
169 "Casts from a type based on a quantized type to a corresponding type "
170 "based on the expressed type of this quantized type. Raises TypeError "
172 "the cast is not valid.",
175 "cast_expressed_to_storage_type",
177 MlirType castResult =
180 return PyType(type.getContext(), castResult).maybeDownCast();
181 throw nb::type_error(
"Invalid cast.");
183 "Casts from a type based on the expressed type of this quantized type "
185 "a corresponding type based on the storage type. Raises TypeError if "
187 "cast is not valid.",
188 nb::arg(
"candidate"));
207 [](
unsigned flags,
const PyType &storageType,
213 storageTypeMin, storageTypeMax));
215 "Gets an instance of AnyQuantizedType in the same context as the "
216 "provided storage type.",
217 nb::arg(
"flags"), nb::arg(
"storage_type"), nb::arg(
"expressed_type"),
218 nb::arg(
"storage_type_min"), nb::arg(
"storage_type_max"),
219 nb::arg(
"context") = nb::none());
232 static constexpr const char *
pyClassName =
"UniformQuantizedType";
239 [](
unsigned flags,
const PyType &storageType,
240 const PyType &expressedType,
double scale,
int64_t zeroPoint,
246 scale, zeroPoint, storageTypeMin,
249 "Gets an instance of UniformQuantizedType in the same context as the "
250 "provided storage type.",
251 nb::arg(
"flags"), nb::arg(
"storage_type"), nb::arg(
"expressed_type"),
252 nb::arg(
"scale"), nb::arg(
"zero_point"), nb::arg(
"storage_type_min"),
253 nb::arg(
"storage_type_max"), nb::arg(
"context") = nb::none());
259 "The scale designates the difference between the real values "
260 "corresponding to consecutive quantized values differing by 1.");
266 "The storage value corresponding to the real value 0 in the affine "
273 "Fixed point values are real numbers divided by a scale.");
287 static constexpr const char *
pyClassName =
"UniformQuantizedPerAxisType";
295 [](
unsigned flags,
const PyType &storageType,
296 const PyType &expressedType, std::vector<double> scales,
297 std::vector<int64_t> zeroPoints, int32_t quantizedDimension,
300 if (scales.size() != zeroPoints.size())
301 throw nb::value_error(
302 "Mismatching number of scales and zero points.");
303 auto nDims =
static_cast<intptr_t>(scales.size());
307 flags, storageType, expressedType, nDims, scales.data(),
308 zeroPoints.data(), quantizedDimension, storageTypeMin,
311 "Gets an instance of UniformQuantizedPerAxisType in the same context "
313 "the provided storage type.",
314 nb::arg(
"flags"), nb::arg(
"storage_type"), nb::arg(
"expressed_type"),
315 nb::arg(
"scales"), nb::arg(
"zero_points"),
316 nb::arg(
"quantized_dimension"), nb::arg(
"storage_type_min"),
317 nb::arg(
"storage_type_max"), nb::arg(
"context") = nb::none());
322 std::vector<double> scales;
323 scales.reserve(nDim);
324 for (
intptr_t i = 0; i < nDim; ++i) {
326 scales.push_back(scale);
330 "The scales designate the difference between the real values "
331 "corresponding to consecutive quantized values differing by 1. The ith "
332 "scale corresponds to the ith slice in the quantized_dimension.");
337 std::vector<int64_t> zeroPoints;
338 zeroPoints.reserve(nDim);
339 for (
intptr_t i = 0; i < nDim; ++i) {
342 zeroPoints.push_back(zeroPoint);
346 "the storage values corresponding to the real value 0 in the affine "
347 "equation. The ith zero point corresponds to the ith slice in the "
348 "quantized_dimension.");
350 "quantized_dimension",
354 "Specifies the dimension of the shape that the scales and zero points "
361 "Fixed point values are real numbers divided by a scale.");
375 static constexpr const char *
pyClassName =
"UniformQuantizedSubChannelType";
383 [](
unsigned flags,
const PyType &storageType,
385 PyAttribute zeroPoints, std::vector<int32_t> quantizedDimensions,
386 std::vector<int64_t> blockSizes,
int64_t storageTypeMin,
391 flags, storageType, expressedType, scales, zeroPoints,
392 static_cast<intptr_t>(blockSizes.size()),
393 quantizedDimensions.data(), blockSizes.data(), storageTypeMin,
396 "Gets an instance of UniformQuantizedSubChannel in the same context as "
397 "the provided storage type.",
398 nb::arg(
"flags"), nb::arg(
"storage_type"), nb::arg(
"expressed_type"),
399 nb::arg(
"scales"), nb::arg(
"zero_points"),
400 nb::arg(
"quantized_dimensions"), nb::arg(
"block_sizes"),
401 nb::arg(
"storage_type_min"), nb::arg(
"storage_type_max"),
402 nb::arg(
"context") = nb::none());
404 "quantized_dimensions",
408 std::vector<int32_t> quantizedDimensions;
409 quantizedDimensions.reserve(nDim);
410 for (
intptr_t i = 0; i < nDim; ++i) {
411 quantizedDimensions.push_back(
415 return quantizedDimensions;
417 "Gets the quantized dimensions. Each element in the returned list "
418 "represents an axis of the quantized data tensor that has a specified "
419 "block size. The order of elements corresponds to the order of block "
420 "sizes returned by 'block_sizes' method. It means that the data tensor "
421 "is quantized along the i-th dimension in the returned list using the "
422 "i-th block size from block_sizes method.");
428 std::vector<int64_t> blockSizes;
429 blockSizes.reserve(nDim);
430 for (
intptr_t i = 0; i < nDim; ++i) {
431 blockSizes.push_back(
436 "Gets the block sizes for the quantized dimensions. The i-th element "
438 "the returned list corresponds to the block size for the i-th "
440 "in the list returned by quantized_dimensions method.");
448 "The scales of the quantized type.");
456 "The zero points of the quantized type.");
470 static constexpr const char *
pyClassName =
"CalibratedQuantizedType";
483 "Gets an instance of CalibratedQuantizedType in the same context as "
485 "provided expressed type.",
486 nb::arg(
"expressed_type"), nb::arg(
"min"), nb::arg(
"max"),
487 nb::arg(
"context") = nb::none());
488 c.def_prop_ro(
"min", [](
const PyType &type) {
491 c.def_prop_ro(
"max", [](
const PyType &type) {
501 auto quantizedTypeClass = m.attr(
"QuantizedType");
516 m.doc() =
"MLIR Quantization dialect";
NB_MODULE(_mlirDialectsQuant, m)
static Value max(ImplicitLocOpBuilder &builder, Value value, Value bound)
static Value min(ImplicitLocOpBuilder &builder, Value value, Value bound)
Used in function arguments when None should resolve to the current context manager set instance.
Wrapper around the generic MlirAttribute.
nanobind::class_< QuantizedType, PyType > ClassTy
static void bind(nanobind::module_ &m)
MlirTypeID(*)() GetTypeIDFunctionTy
bool(*)(MlirType) IsAFunctionTy
Wrapper around the generic MlirType.
PyType(PyMlirContextRef contextRef, MlirType type)
MLIR_CAPI_EXPORTED intptr_t mlirUniformQuantizedPerAxisTypeGetNumDims(MlirType type)
Returns the number of axes in the given quantized per-axis type.
MLIR_CAPI_EXPORTED bool mlirTypeIsACalibratedQuantizedType(MlirType type)
Returns true if the given type is a CalibratedQuantizedType.
MLIR_CAPI_EXPORTED int64_t mlirQuantizedTypeGetDefaultMaximumForInteger(bool isSigned, unsigned integralWidth)
Returns the maximum possible value stored by a quantized type.
MLIR_CAPI_EXPORTED MlirTypeID mlirCalibratedQuantizedTypeGetTypeID(void)
MLIR_CAPI_EXPORTED bool mlirUniformQuantizedTypeIsFixedPoint(MlirType type)
Returns true if the given uniform quantized type is fixed-point.
MLIR_CAPI_EXPORTED MlirAttribute mlirUniformQuantizedSubChannelTypeGetZeroPoints(MlirType type)
Returns the zero-points of the quantized type.
MLIR_CAPI_EXPORTED MlirStringRef mlirUniformQuantizedPerAxisTypeGetName(void)
MLIR_CAPI_EXPORTED int64_t mlirQuantizedTypeGetStorageTypeMin(MlirType type)
Returns the minimum value that the storage type of the given quantized type can take.
MLIR_CAPI_EXPORTED bool mlirTypeIsAUniformQuantizedType(MlirType type)
Returns true if the given type is a UniformQuantizedType.
MLIR_CAPI_EXPORTED bool mlirTypeIsAQuantizedType(MlirType type)
Returns true if the given type is a quantization dialect type.
MLIR_CAPI_EXPORTED double mlirUniformQuantizedPerAxisTypeGetScale(MlirType type, intptr_t pos)
Returns pos-th scale of the given quantized per-axis type.
MLIR_CAPI_EXPORTED MlirAttribute mlirUniformQuantizedSubChannelTypeGetScales(MlirType type)
Returns the scales of the quantized type.
MLIR_CAPI_EXPORTED int64_t mlirUniformQuantizedTypeGetZeroPoint(MlirType type)
Returns the zero point of the given uniform quantized type.
MLIR_CAPI_EXPORTED bool mlirUniformQuantizedPerAxisTypeIsFixedPoint(MlirType type)
Returns true if the given uniform quantized per-axis type is fixed-point.
MLIR_CAPI_EXPORTED int64_t mlirUniformQuantizedSubChannelTypeGetBlockSize(MlirType type, intptr_t pos)
Returns the block size at the given position.
MLIR_CAPI_EXPORTED int64_t mlirQuantizedTypeGetStorageTypeMax(MlirType type)
Returns the maximum value that the storage type of the given quantized type can take.
MLIR_CAPI_EXPORTED double mlirCalibratedQuantizedTypeGetMax(MlirType type)
Returns the max value of the given calibrated quantized type.
MLIR_CAPI_EXPORTED MlirType mlirCalibratedQuantizedTypeGet(MlirType expressedType, double min, double max)
Creates an instance of CalibratedQuantizedType with the given parameters in the same context as expre...
MLIR_CAPI_EXPORTED bool mlirQuantizedTypeIsSigned(MlirType type)
Returns true if the given type is signed, false otherwise.
MLIR_CAPI_EXPORTED intptr_t mlirUniformQuantizedSubChannelTypeGetNumBlockSizes(MlirType type)
Returns the number of block sizes provided in type.
MLIR_CAPI_EXPORTED int64_t mlirUniformQuantizedPerAxisTypeGetZeroPoint(MlirType type, intptr_t pos)
Returns pos-th zero point of the given quantized per-axis type.
MLIR_CAPI_EXPORTED int32_t mlirUniformQuantizedPerAxisTypeGetQuantizedDimension(MlirType type)
Returns the index of the quantized dimension in the given quantized per-axis type.
MLIR_CAPI_EXPORTED MlirType mlirQuantizedTypeCastToExpressedType(MlirType type)
Casts from a type based on a quantized type to a corresponding typed based on the expressed type.
MLIR_CAPI_EXPORTED MlirType mlirQuantizedTypeGetExpressedType(MlirType type)
Gets the original type approximated by the given quantized type.
MLIR_CAPI_EXPORTED unsigned mlirQuantizedTypeGetStorageTypeIntegralWidth(MlirType type)
Returns the integral bitwidth that the storage type of the given quantized type can represent exactly...
MLIR_CAPI_EXPORTED MlirType mlirAnyQuantizedTypeGet(unsigned flags, MlirType storageType, MlirType expressedType, int64_t storageTypeMin, int64_t storageTypeMax)
Creates an instance of AnyQuantizedType with the given parameters in the same context as storageType ...
MLIR_CAPI_EXPORTED MlirType mlirQuantizedTypeCastToStorageType(MlirType type)
Casts from a type based on a quantized type to a corresponding typed based on the storage type.
MLIR_CAPI_EXPORTED MlirTypeID mlirAnyQuantizedTypeGetTypeID(void)
MLIR_CAPI_EXPORTED MlirTypeID mlirUniformQuantizedTypeGetTypeID(void)
MLIR_CAPI_EXPORTED MlirType mlirQuantizedTypeGetStorageType(MlirType type)
Returns the underlying type used to store the values.
MLIR_CAPI_EXPORTED MlirTypeID mlirUniformQuantizedPerAxisTypeGetTypeID(void)
MLIR_CAPI_EXPORTED int32_t mlirUniformQuantizedSubChannelTypeGetQuantizedDimension(MlirType type, intptr_t pos)
Returns the quantized dimension at the given position.
MLIR_CAPI_EXPORTED MlirType mlirQuantizedTypeCastFromExpressedType(MlirType type, MlirType candidate)
Casts from a type based on the expressed type of the given type to a corresponding type based on the ...
MLIR_CAPI_EXPORTED bool mlirTypeIsAUniformQuantizedPerAxisType(MlirType type)
Returns true if the given type is a UniformQuantizedPerAxisType.
MLIR_CAPI_EXPORTED MlirType mlirQuantizedTypeGetQuantizedElementType(MlirType type)
Returns the element type of the given quantized type as another quantized type.
MLIR_CAPI_EXPORTED bool mlirTypeIsAAnyQuantizedType(MlirType type)
Returns true if the given type is an AnyQuantizedType.
MLIR_CAPI_EXPORTED MlirStringRef mlirAnyQuantizedTypeGetName(void)
MLIR_CAPI_EXPORTED MlirType mlirQuantizedTypeCastExpressedToStorageType(MlirType type, MlirType candidate)
Casts from a type based on the expressed type of the given quantized type to equivalent type based on...
MLIR_CAPI_EXPORTED MlirType mlirQuantizedTypeCastFromStorageType(MlirType type, MlirType candidate)
Casts from a type based on the storage type of the given type to a corresponding type based on the gi...
MLIR_CAPI_EXPORTED unsigned mlirQuantizedTypeGetSignedFlag(void)
Returns the bit flag used to indicate signedness of a quantized type.
MLIR_CAPI_EXPORTED int64_t mlirQuantizedTypeGetDefaultMinimumForInteger(bool isSigned, unsigned integralWidth)
Returns the minimum possible value stored by a quantized type.
MLIR_CAPI_EXPORTED double mlirCalibratedQuantizedTypeGetMin(MlirType type)
Returns the min value of the given calibrated quantized type.
MLIR_CAPI_EXPORTED unsigned mlirQuantizedTypeGetFlags(MlirType type)
Gets the flags associated with the given quantized type.
MLIR_CAPI_EXPORTED MlirTypeID mlirUniformQuantizedSubChannelTypeGetTypeID(void)
MLIR_CAPI_EXPORTED MlirType mlirUniformQuantizedPerAxisTypeGet(unsigned flags, MlirType storageType, MlirType expressedType, intptr_t nDims, double *scales, int64_t *zeroPoints, int32_t quantizedDimension, int64_t storageTypeMin, int64_t storageTypeMax)
Creates an instance of UniformQuantizedPerAxisType with the given parameters in the same context as s...
MLIR_CAPI_EXPORTED MlirType mlirUniformQuantizedSubChannelTypeGet(unsigned flags, MlirType storageType, MlirType expressedType, MlirAttribute scalesAttr, MlirAttribute zeroPointsAttr, intptr_t blockSizeInfoLength, int32_t *quantizedDimensions, int64_t *blockSizes, int64_t storageTypeMin, int64_t storageTypeMax)
Creates a UniformQuantizedSubChannelType with the given parameters.
MLIR_CAPI_EXPORTED bool mlirQuantizedTypeIsCompatibleExpressedType(MlirType type, MlirType candidate)
Returns true if the candidate type is compatible with the given quantized type.
MLIR_CAPI_EXPORTED bool mlirTypeIsAUniformQuantizedSubChannelType(MlirType type)
Returns true if the given type is a UniformQuantizedSubChannel.
MLIR_CAPI_EXPORTED MlirStringRef mlirUniformQuantizedSubChannelTypeGetName(void)
MLIR_CAPI_EXPORTED MlirStringRef mlirCalibratedQuantizedTypeGetName(void)
MLIR_CAPI_EXPORTED double mlirUniformQuantizedTypeGetScale(MlirType type)
Returns the scale of the given uniform quantized type.
MLIR_CAPI_EXPORTED MlirStringRef mlirUniformQuantizedTypeGetName(void)
MLIR_CAPI_EXPORTED MlirType mlirUniformQuantizedTypeGet(unsigned flags, MlirType storageType, MlirType expressedType, double scale, int64_t zeroPoint, int64_t storageTypeMin, int64_t storageTypeMax)
Creates an instance of UniformQuantizedType with the given parameters in the same context as storageT...
static bool mlirTypeIsNull(MlirType type)
Checks whether a type is null.
The OpAsmOpInterface, see OpAsmInterface.td for more details.
static void populateDialectQuantSubmodule(nb::module_ &m)
Include the generated interface declarations.
A pointer to a sized fragment of a string, not necessarily null-terminated.
static constexpr IsAFunctionTy isaFunction
static constexpr const char * pyClassName
static void bindDerived(ClassTy &c)
static const MlirStringRef name
static constexpr GetTypeIDFunctionTy getTypeIdFunction
static constexpr const char * pyClassName
static constexpr GetTypeIDFunctionTy getTypeIdFunction
static void bindDerived(ClassTy &c)
static const MlirStringRef name
static constexpr IsAFunctionTy isaFunction
static void bindDerived(ClassTy &c)
static constexpr IsAFunctionTy isaFunction
static constexpr const char * pyClassName
static constexpr IsAFunctionTy isaFunction
static constexpr GetTypeIDFunctionTy getTypeIdFunction
static constexpr const char * pyClassName
static void bindDerived(ClassTy &c)
static const MlirStringRef name
static const MlirStringRef name
static constexpr GetTypeIDFunctionTy getTypeIdFunction
static void bindDerived(ClassTy &c)
static constexpr const char * pyClassName
static constexpr IsAFunctionTy isaFunction
static void bindDerived(ClassTy &c)
static constexpr GetTypeIDFunctionTy getTypeIdFunction
static constexpr IsAFunctionTy isaFunction
static constexpr const char * pyClassName
static const MlirStringRef name