37 "default_minimum_for_integer",
38 [](
bool isSigned,
unsigned integralWidth) {
42 "Default minimum value for the integer with the specified signedness "
45 nb::arg(
"is_signed"), nb::arg(
"integral_width"));
47 "default_maximum_for_integer",
48 [](
bool isSigned,
unsigned integralWidth) {
52 "Default maximum value for the integer with the specified signedness "
55 nb::arg(
"is_signed"), nb::arg(
"integral_width"));
59 return PyType(type.getContext(),
63 "Type expressed by this quantized type.");
69 "Flags of this quantized type (named accessors should be preferred to "
76 "Signedness of this quantized type.");
80 return PyType(type.getContext(),
84 "Storage type backing this quantized type.");
90 "The minimum value held by the storage type of this quantized type.");
96 "The maximum value held by the storage type of this quantized type.");
98 "storage_type_integral_width",
102 "The bitwidth of the storage type of this quantized type.");
104 "is_compatible_expressed_type",
108 "Checks whether the candidate type can be expressed by this quantized "
110 nb::arg(
"candidate"));
112 "quantized_element_type",
114 return PyType(type.getContext(),
118 "Element type of this quantized type expressed as quantized type.");
120 "cast_from_storage_type",
122 MlirType castResult =
126 throw nb::type_error(
"Invalid cast.");
128 "Casts from a type based on the storage type of this quantized type to "
130 "corresponding type based on the quantized type. Raises TypeError if "
132 "cast is not valid.",
133 nb::arg(
"candidate"));
135 "cast_to_storage_type",
139 return PyType(type.getContext(), castResult).maybeDownCast();
140 throw nb::type_error(
"Invalid cast.");
142 "Casts from a type based on a quantized type to a corresponding type "
143 "based on the storage type of this quantized type. Raises TypeError if "
144 "the cast is not valid.",
147 "cast_from_expressed_type",
149 MlirType castResult =
152 return PyType(type.getContext(), castResult).maybeDownCast();
153 throw nb::type_error(
"Invalid cast.");
155 "Casts from a type based on the expressed type of this quantized type "
157 "a corresponding type based on the quantized type. Raises TypeError if "
158 "the cast is not valid.",
159 nb::arg(
"candidate"));
161 "cast_to_expressed_type",
165 return PyType(type.getContext(), castResult).maybeDownCast();
166 throw nb::type_error(
"Invalid cast.");
168 "Casts from a type based on a quantized type to a corresponding type "
169 "based on the expressed type of this quantized type. Raises TypeError "
171 "the cast is not valid.",
174 "cast_expressed_to_storage_type",
176 MlirType castResult =
179 return PyType(type.getContext(), castResult).maybeDownCast();
180 throw nb::type_error(
"Invalid cast.");
182 "Casts from a type based on the expressed type of this quantized type "
184 "a corresponding type based on the storage type. Raises TypeError if "
186 "cast is not valid.",
187 nb::arg(
"candidate"));
206 [](
unsigned flags,
const PyType &storageType,
212 storageTypeMin, storageTypeMax));
214 "Gets an instance of AnyQuantizedType in the same context as the "
215 "provided storage type.",
216 nb::arg(
"flags"), nb::arg(
"storage_type"), nb::arg(
"expressed_type"),
217 nb::arg(
"storage_type_min"), nb::arg(
"storage_type_max"),
218 nb::arg(
"context") = nb::none());
231 static constexpr const char *
pyClassName =
"UniformQuantizedType";
238 [](
unsigned flags,
const PyType &storageType,
239 const PyType &expressedType,
double scale,
int64_t zeroPoint,
245 scale, zeroPoint, storageTypeMin,
248 "Gets an instance of UniformQuantizedType in the same context as the "
249 "provided storage type.",
250 nb::arg(
"flags"), nb::arg(
"storage_type"), nb::arg(
"expressed_type"),
251 nb::arg(
"scale"), nb::arg(
"zero_point"), nb::arg(
"storage_type_min"),
252 nb::arg(
"storage_type_max"), nb::arg(
"context") = nb::none());
258 "The scale designates the difference between the real values "
259 "corresponding to consecutive quantized values differing by 1.");
265 "The storage value corresponding to the real value 0 in the affine "
272 "Fixed point values are real numbers divided by a scale.");
286 static constexpr const char *
pyClassName =
"UniformQuantizedPerAxisType";
294 [](
unsigned flags,
const PyType &storageType,
295 const PyType &expressedType, std::vector<double> scales,
296 std::vector<int64_t> zeroPoints, int32_t quantizedDimension,
299 if (scales.size() != zeroPoints.size())
300 throw nb::value_error(
301 "Mismatching number of scales and zero points.");
302 auto nDims =
static_cast<intptr_t>(scales.size());
306 flags, storageType, expressedType, nDims, scales.data(),
307 zeroPoints.data(), quantizedDimension, storageTypeMin,
310 "Gets an instance of UniformQuantizedPerAxisType in the same context "
312 "the provided storage type.",
313 nb::arg(
"flags"), nb::arg(
"storage_type"), nb::arg(
"expressed_type"),
314 nb::arg(
"scales"), nb::arg(
"zero_points"),
315 nb::arg(
"quantized_dimension"), nb::arg(
"storage_type_min"),
316 nb::arg(
"storage_type_max"), nb::arg(
"context") = nb::none());
321 std::vector<double> scales;
322 scales.reserve(nDim);
323 for (
intptr_t i = 0; i < nDim; ++i) {
325 scales.push_back(scale);
329 "The scales designate the difference between the real values "
330 "corresponding to consecutive quantized values differing by 1. The ith "
331 "scale corresponds to the ith slice in the quantized_dimension.");
336 std::vector<int64_t> zeroPoints;
337 zeroPoints.reserve(nDim);
338 for (
intptr_t i = 0; i < nDim; ++i) {
341 zeroPoints.push_back(zeroPoint);
345 "the storage values corresponding to the real value 0 in the affine "
346 "equation. The ith zero point corresponds to the ith slice in the "
347 "quantized_dimension.");
349 "quantized_dimension",
353 "Specifies the dimension of the shape that the scales and zero points "
360 "Fixed point values are real numbers divided by a scale.");
374 static constexpr const char *
pyClassName =
"UniformQuantizedSubChannelType";
382 [](
unsigned flags,
const PyType &storageType,
384 PyAttribute zeroPoints, std::vector<int32_t> quantizedDimensions,
385 std::vector<int64_t> blockSizes,
int64_t storageTypeMin,
390 flags, storageType, expressedType, scales, zeroPoints,
391 static_cast<intptr_t>(blockSizes.size()),
392 quantizedDimensions.data(), blockSizes.data(), storageTypeMin,
395 "Gets an instance of UniformQuantizedSubChannel in the same context as "
396 "the provided storage type.",
397 nb::arg(
"flags"), nb::arg(
"storage_type"), nb::arg(
"expressed_type"),
398 nb::arg(
"scales"), nb::arg(
"zero_points"),
399 nb::arg(
"quantized_dimensions"), nb::arg(
"block_sizes"),
400 nb::arg(
"storage_type_min"), nb::arg(
"storage_type_max"),
401 nb::arg(
"context") = nb::none());
403 "quantized_dimensions",
407 std::vector<int32_t> quantizedDimensions;
408 quantizedDimensions.reserve(nDim);
409 for (
intptr_t i = 0; i < nDim; ++i) {
410 quantizedDimensions.push_back(
414 return quantizedDimensions;
416 "Gets the quantized dimensions. Each element in the returned list "
417 "represents an axis of the quantized data tensor that has a specified "
418 "block size. The order of elements corresponds to the order of block "
419 "sizes returned by 'block_sizes' method. It means that the data tensor "
420 "is quantized along the i-th dimension in the returned list using the "
421 "i-th block size from block_sizes method.");
427 std::vector<int64_t> blockSizes;
428 blockSizes.reserve(nDim);
429 for (
intptr_t i = 0; i < nDim; ++i) {
430 blockSizes.push_back(
435 "Gets the block sizes for the quantized dimensions. The i-th element "
437 "the returned list corresponds to the block size for the i-th "
439 "in the list returned by quantized_dimensions method.");
447 "The scales of the quantized type.");
455 "The zero points of the quantized type.");
469 static constexpr const char *
pyClassName =
"CalibratedQuantizedType";
482 "Gets an instance of CalibratedQuantizedType in the same context as "
484 "provided expressed type.",
485 nb::arg(
"expressed_type"), nb::arg(
"min"), nb::arg(
"max"),
486 nb::arg(
"context") = nb::none());
487 c.def_prop_ro(
"min", [](
const PyType &type) {
490 c.def_prop_ro(
"max", [](
const PyType &type) {
500 auto quantizedTypeClass = m.attr(
"QuantizedType");
515 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.
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