| 1 | //===- TypeParser.h - Quantization Type Parser ------------------*- C++ -*-===// |
| 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-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "mlir/Dialect/Quant/IR/Quant.h" |
| 10 | #include "mlir/Dialect/Quant/IR/QuantTypes.h" |
| 11 | #include "mlir/IR/BuiltinTypes.h" |
| 12 | #include "mlir/IR/DialectImplementation.h" |
| 13 | #include "mlir/IR/QuantStorageTypeInterface.h" |
| 14 | #include "mlir/IR/Types.h" |
| 15 | #include "llvm/ADT/APFloat.h" |
| 16 | #include "llvm/ADT/SmallVectorExtras.h" |
| 17 | |
| 18 | using namespace mlir; |
| 19 | using namespace quant; |
| 20 | |
| 21 | static Type parseStorageType(DialectAsmParser &parser, bool &isSigned) { |
| 22 | auto typeLoc = parser.getCurrentLocation(); |
| 23 | Type type; |
| 24 | |
| 25 | // Parse storage type (alpha_ident, integer_literal). |
| 26 | StringRef identifier; |
| 27 | unsigned storageTypeWidth = 0; |
| 28 | OptionalParseResult result = parser.parseOptionalType(result&: type); |
| 29 | if (result.has_value()) { |
| 30 | if (!succeeded(Result: *result)) |
| 31 | return nullptr; |
| 32 | |
| 33 | if (auto quantStorageTypeInterface = |
| 34 | llvm::dyn_cast<QuantStorageTypeInterface>(Val&: type)) { |
| 35 | // Returns true if the type defaults to signed (e.g., si8, i8 or float |
| 36 | // types), false if it defaults to unsigned. |
| 37 | isSigned = quantStorageTypeInterface.shouldDefaultToSigned(); |
| 38 | storageTypeWidth = quantStorageTypeInterface.getStorageWidth(); |
| 39 | } else { |
| 40 | parser.emitError(loc: typeLoc, message: "illegal storage type prefix" ); |
| 41 | return nullptr; |
| 42 | } |
| 43 | } else if (succeeded(Result: parser.parseKeyword(keyword: &identifier))) { |
| 44 | // Otherwise, this must be an unsigned integer (`u` integer-literal) |
| 45 | if (identifier.consume_front(Prefix: "u" )) { |
| 46 | if (identifier.getAsInteger(Radix: 10, Result&: storageTypeWidth)) { |
| 47 | parser.emitError(loc: typeLoc, message: "expected storage type width" ); |
| 48 | return nullptr; |
| 49 | } |
| 50 | isSigned = false; |
| 51 | type = parser.getBuilder().getIntegerType(width: storageTypeWidth); |
| 52 | } else { |
| 53 | parser.emitError(loc: typeLoc, message: "illegal storage type prefix" ); |
| 54 | return nullptr; |
| 55 | } |
| 56 | } else { |
| 57 | return nullptr; |
| 58 | } |
| 59 | |
| 60 | if (storageTypeWidth == 0 || |
| 61 | storageTypeWidth > QuantizedType::MaxStorageBits) { |
| 62 | parser.emitError(loc: typeLoc, message: "illegal storage type size: " ) |
| 63 | << storageTypeWidth; |
| 64 | return nullptr; |
| 65 | } |
| 66 | |
| 67 | return type; |
| 68 | } |
| 69 | |
| 70 | static ParseResult parseStorageRange(DialectAsmParser &parser, Type storageType, |
| 71 | bool isSigned, int64_t &storageTypeMin, |
| 72 | int64_t &storageTypeMax) { |
| 73 | auto quantStorageTypeInterface = |
| 74 | llvm::dyn_cast<QuantStorageTypeInterface>(Val&: storageType); |
| 75 | |
| 76 | int64_t defaultMin = quantStorageTypeInterface.getDefaultMinimum(isSigned); |
| 77 | int64_t defaultMax = quantStorageTypeInterface.getDefaultMaximum(isSigned); |
| 78 | |
| 79 | if (failed(Result: parser.parseOptionalLess())) { |
| 80 | storageTypeMin = defaultMin; |
| 81 | storageTypeMax = defaultMax; |
| 82 | return success(); |
| 83 | } |
| 84 | |
| 85 | // Explicit storage min and storage max. |
| 86 | SMLoc minLoc = parser.getCurrentLocation(), maxLoc; |
| 87 | if (parser.parseInteger(result&: storageTypeMin) || parser.parseColon() || |
| 88 | parser.getCurrentLocation(loc: &maxLoc) || |
| 89 | parser.parseInteger(result&: storageTypeMax) || parser.parseGreater()) |
| 90 | return failure(); |
| 91 | if (storageTypeMin < defaultMin) { |
| 92 | return parser.emitError(loc: minLoc, message: "illegal storage type minimum: " ) |
| 93 | << storageTypeMin; |
| 94 | } |
| 95 | if (storageTypeMax > defaultMax) { |
| 96 | return parser.emitError(loc: maxLoc, message: "illegal storage type maximum: " ) |
| 97 | << storageTypeMax; |
| 98 | } |
| 99 | return success(); |
| 100 | } |
| 101 | |
| 102 | static FloatType parseExpressedTypeAndRange(DialectAsmParser &parser, |
| 103 | double &min, double &max) { |
| 104 | auto typeLoc = parser.getCurrentLocation(); |
| 105 | FloatType type; |
| 106 | |
| 107 | if (failed(Result: parser.parseType(result&: type))) { |
| 108 | parser.emitError(loc: typeLoc, message: "expecting float expressed type" ); |
| 109 | return nullptr; |
| 110 | } |
| 111 | |
| 112 | // Calibrated min and max values. |
| 113 | if (parser.parseLess() || parser.parseFloat(result&: min) || parser.parseColon() || |
| 114 | parser.parseFloat(result&: max) || parser.parseGreater()) { |
| 115 | parser.emitError(loc: typeLoc, message: "calibrated values must be present" ); |
| 116 | return nullptr; |
| 117 | } |
| 118 | return type; |
| 119 | } |
| 120 | |
| 121 | /// Parses an AnyQuantizedType. |
| 122 | /// |
| 123 | /// any ::= `any<` storage-spec (expressed-type-spec)?`>` |
| 124 | /// storage-spec ::= storage-type (`<` storage-range `>`)? |
| 125 | /// storage-range ::= integer-literal `:` integer-literal |
| 126 | /// storage-type ::= (`i` | `u`) integer-literal |
| 127 | /// expressed-type-spec ::= `:` `f` integer-literal |
| 128 | static Type parseAnyType(DialectAsmParser &parser) { |
| 129 | Type storageType; |
| 130 | FloatType expressedType; |
| 131 | unsigned typeFlags = 0; |
| 132 | int64_t storageTypeMin; |
| 133 | int64_t storageTypeMax; |
| 134 | |
| 135 | // Type specification. |
| 136 | if (parser.parseLess()) |
| 137 | return nullptr; |
| 138 | |
| 139 | // Storage type. |
| 140 | bool isSigned = false; |
| 141 | storageType = parseStorageType(parser, isSigned); |
| 142 | if (!storageType) { |
| 143 | return nullptr; |
| 144 | } |
| 145 | if (isSigned) { |
| 146 | typeFlags |= QuantizationFlags::Signed; |
| 147 | } |
| 148 | |
| 149 | // Storage type range. |
| 150 | if (parseStorageRange(parser, storageType, isSigned, storageTypeMin, |
| 151 | storageTypeMax)) { |
| 152 | return nullptr; |
| 153 | } |
| 154 | |
| 155 | // Optional expressed type. |
| 156 | if (succeeded(Result: parser.parseOptionalColon())) { |
| 157 | if (parser.parseType(result&: expressedType)) { |
| 158 | return nullptr; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | if (parser.parseGreater()) { |
| 163 | return nullptr; |
| 164 | } |
| 165 | |
| 166 | return parser.getChecked<AnyQuantizedType>( |
| 167 | params&: typeFlags, params&: storageType, params&: expressedType, params&: storageTypeMin, params&: storageTypeMax); |
| 168 | } |
| 169 | |
| 170 | /// Checks if the given scale value is within the valid range of the expressed |
| 171 | /// type. The `expressedType` argument is the floating-point type used for |
| 172 | /// expressing the quantized values, and `scale` is the double value to check. |
| 173 | static LogicalResult |
| 174 | isScaleInExpressedTypeRange(function_ref<InFlightDiagnostic()> emitError, |
| 175 | Type expressedType, double scale) { |
| 176 | auto floatType = cast<FloatType>(Val&: expressedType); |
| 177 | double minScale = |
| 178 | APFloat::getSmallest(Sem: floatType.getFloatSemantics()).convertToDouble(); |
| 179 | double maxScale = |
| 180 | APFloat::getLargest(Sem: floatType.getFloatSemantics()).convertToDouble(); |
| 181 | if (scale < minScale || scale > maxScale) |
| 182 | return emitError() << "scale " << scale << " out of expressed type range [" |
| 183 | << minScale << ", " << maxScale << "]" ; |
| 184 | return success(); |
| 185 | } |
| 186 | |
| 187 | /// Parses a quantization parameter, which is either a scale value (float) or a |
| 188 | /// scale-zero point pair (float:integer). `expressedType`, expressing the type |
| 189 | /// of scale values, is used to validate the scale. The parsed scale and zero |
| 190 | /// point (if any) are stored in `scale` and `zeroPoint`. |
| 191 | static ParseResult parseQuantParams(DialectAsmParser &parser, |
| 192 | Type expressedType, double &scale, |
| 193 | int64_t &zeroPoint) { |
| 194 | |
| 195 | if (parser.parseFloat(result&: scale)) { |
| 196 | return failure(); |
| 197 | } |
| 198 | |
| 199 | if (failed(Result: isScaleInExpressedTypeRange( |
| 200 | emitError: [&]() { return parser.emitError(loc: parser.getCurrentLocation()); }, |
| 201 | expressedType, scale))) { |
| 202 | return failure(); |
| 203 | } |
| 204 | |
| 205 | zeroPoint = 0; |
| 206 | if (failed(Result: parser.parseOptionalColon())) { |
| 207 | return success(); |
| 208 | } |
| 209 | |
| 210 | return parser.parseInteger(result&: zeroPoint); |
| 211 | } |
| 212 | |
| 213 | /// Parses block size information for sub-channel quantization, assuming the |
| 214 | /// leading '{' has already been parsed. The block size information is provided |
| 215 | /// as a comma-separated list of "Axis:BlockSize" pairs, terminated by a '}'. |
| 216 | /// |
| 217 | /// The parsed axis indices are stored in `quantizedDimensions`, and the |
| 218 | /// corresponding block sizes are stored in `blockSizes`. |
| 219 | static ParseResult |
| 220 | parseBlockSizeInfoUntilRBrace(DialectAsmParser &parser, |
| 221 | SmallVectorImpl<int32_t> &quantizedDimensions, |
| 222 | SmallVectorImpl<int64_t> &blockSizes) { |
| 223 | // Empty block-sizes info. |
| 224 | if (succeeded(Result: parser.parseOptionalRBrace())) { |
| 225 | return success(); |
| 226 | } |
| 227 | |
| 228 | auto parseBlockSizeElements = [&]() -> ParseResult { |
| 229 | quantizedDimensions.resize(N: quantizedDimensions.size() + 1); |
| 230 | blockSizes.resize(N: blockSizes.size() + 1); |
| 231 | if (parser.parseInteger(result&: quantizedDimensions.back()) || |
| 232 | parser.parseColon() || parser.parseInteger(result&: blockSizes.back())) |
| 233 | return failure(); |
| 234 | return success(); |
| 235 | }; |
| 236 | |
| 237 | if (parser.parseCommaSeparatedList(parseElementFn: parseBlockSizeElements) || |
| 238 | parser.parseRBrace()) { |
| 239 | return failure(); |
| 240 | } |
| 241 | |
| 242 | return success(); |
| 243 | } |
| 244 | |
| 245 | /// Parses a bracketed list of quantization parameters, returning the dimensions |
| 246 | /// of the parsed sub-tensors in `dims`. The dimension of the list is prepended |
| 247 | /// to the dimensions of the sub-tensors. This function assumes that the initial |
| 248 | /// left brace has already been parsed. For example: |
| 249 | /// |
| 250 | /// parseQuantParamListUntilRBrace(1.0:1, 2.0:4, 3.0:4}) -> Success, |
| 251 | /// dims = [3], scales = [1.0, 2.0, 3.0], zeroPoints = [1, 4, 4] |
| 252 | /// |
| 253 | /// parseQuantParamListUntilRBrace({1.0, 2.0}, {3.0:1, 4.0:9}}) -> Success, |
| 254 | /// dims = [2, 2], scales = [1.0, 2.0, 3.0, 4.0], zeroPoints = [0, 0, 1, |
| 255 | /// 9] |
| 256 | /// |
| 257 | /// This function expects all sub-tensors to have the same rank. |
| 258 | static ParseResult |
| 259 | parseQuantParamListUntilRBrace(DialectAsmParser &parser, Type expressedType, |
| 260 | SmallVectorImpl<double> &scales, |
| 261 | SmallVectorImpl<int64_t> &zeroPoints, |
| 262 | SmallVectorImpl<int64_t> &dims) { |
| 263 | auto checkDims = [&](const SmallVectorImpl<int64_t> &prevDims, |
| 264 | const SmallVectorImpl<int64_t> &newDims) -> ParseResult { |
| 265 | if (prevDims == newDims) |
| 266 | return success(); |
| 267 | return parser.emitError(loc: parser.getCurrentLocation()) |
| 268 | << "tensor literal is invalid; ranks are not consistent " |
| 269 | "between elements" ; |
| 270 | }; |
| 271 | |
| 272 | bool first = true; |
| 273 | SmallVector<int64_t, 4> newDims; |
| 274 | unsigned size = 0; |
| 275 | |
| 276 | auto parseOneElement = [&]() -> ParseResult { |
| 277 | SmallVector<int64_t, 4> thisDims; |
| 278 | if (succeeded(Result: parser.parseOptionalLBrace())) { |
| 279 | if (parseQuantParamListUntilRBrace(parser, expressedType, scales, |
| 280 | zeroPoints, dims&: thisDims)) |
| 281 | return failure(); |
| 282 | } else { |
| 283 | zeroPoints.resize(N: zeroPoints.size() + 1); |
| 284 | scales.resize(N: scales.size() + 1); |
| 285 | if (parseQuantParams(parser, expressedType, scale&: scales.back(), |
| 286 | zeroPoint&: zeroPoints.back())) { |
| 287 | return failure(); |
| 288 | } |
| 289 | } |
| 290 | ++size; |
| 291 | if (!first) |
| 292 | return checkDims(newDims, thisDims); |
| 293 | newDims = thisDims; |
| 294 | first = false; |
| 295 | return success(); |
| 296 | }; |
| 297 | |
| 298 | if (parser.parseCommaSeparatedList(parseElementFn: parseOneElement) || parser.parseRBrace()) { |
| 299 | return failure(); |
| 300 | } |
| 301 | |
| 302 | // Return the sublists' dimensions with 'size' prepended. |
| 303 | dims.clear(); |
| 304 | dims.push_back(Elt: size); |
| 305 | dims.append(in_start: newDims.begin(), in_end: newDims.end()); |
| 306 | |
| 307 | return success(); |
| 308 | } |
| 309 | |
| 310 | /// Parses a UniformQuantizedType. |
| 311 | /// |
| 312 | /// uniform_type ::= uniform_per_layer |
| 313 | /// | uniform_per_axis |
| 314 | /// | uniform_sub_channel |
| 315 | /// uniform_per_layer ::= `uniform<` storage-spec expressed-type-spec |
| 316 | /// `,` scale-zero `>` |
| 317 | /// uniform_per_axis ::= `uniform<` storage-spec expressed-type-spec |
| 318 | /// axis-spec `,` `{` scale-zero-list `}` `>` |
| 319 | /// uniform_sub_channel ::= `uniform<` storage-spec expressed-type-spec |
| 320 | /// block-size-info `,` scale-zero-tensor `>` |
| 321 | /// storage-spec ::= storage-type (`<` storage-range `>`)? |
| 322 | /// storage-range ::= integer-literal `:` integer-literal |
| 323 | /// storage-type ::= (`i` | `u`) integer-literal | `f8E5M2` | `f8E4M3FN` |
| 324 | // | `f4E2M1FN` | 'quantile' |
| 325 | /// expressed-type-spec ::= `:` `f` integer-literal |
| 326 | /// axis-spec ::= `:` integer-literal |
| 327 | /// scale-zero ::= scale (`:` zero-point)? |
| 328 | /// scale ::= float-literal |
| 329 | /// zero-point ::= integer-literal |
| 330 | /// scale-zero-list ::= scale-zero (`,` scale-zero)* |
| 331 | /// block-size-info ::= `{` `}` | `{` axis-block `:` (`,` axis-block)* `}` |
| 332 | /// axis-block ::= axis-spec `:` block-size-spec |
| 333 | /// block-size-spec ::= integer-literal |
| 334 | /// scale-zero-tensor ::= scale-zero-dense-exp | scale-zero-list |
| 335 | /// scale-zero-dense-exp ::= `{` |
| 336 | /// scale-zero-tensor (`,` scale-zero-tensor)* |
| 337 | /// `}` |
| 338 | static Type parseUniformType(DialectAsmParser &parser) { |
| 339 | Type storageType; |
| 340 | FloatType expressedType; |
| 341 | unsigned typeFlags = 0; |
| 342 | int64_t storageTypeMin; |
| 343 | int64_t storageTypeMax; |
| 344 | bool isPerAxis = false; |
| 345 | bool isSubChannel = false; |
| 346 | SmallVector<int32_t, 1> quantizedDimensions; |
| 347 | SmallVector<int64_t, 1> blockSizes; |
| 348 | SmallVector<double, 1> scales; |
| 349 | SmallVector<int64_t, 1> zeroPoints; |
| 350 | |
| 351 | // Type specification. |
| 352 | if (parser.parseLess()) { |
| 353 | return nullptr; |
| 354 | } |
| 355 | |
| 356 | // Storage type. |
| 357 | bool isSigned = false; |
| 358 | storageType = parseStorageType(parser, isSigned); |
| 359 | if (!storageType) { |
| 360 | return nullptr; |
| 361 | } |
| 362 | if (isSigned) { |
| 363 | typeFlags |= QuantizationFlags::Signed; |
| 364 | } |
| 365 | |
| 366 | // Storage type range. |
| 367 | if (parseStorageRange(parser, storageType, isSigned, storageTypeMin, |
| 368 | storageTypeMax)) { |
| 369 | return nullptr; |
| 370 | } |
| 371 | |
| 372 | // Expressed type. |
| 373 | if (parser.parseColon() || parser.parseType(result&: expressedType)) { |
| 374 | return nullptr; |
| 375 | } |
| 376 | |
| 377 | // Optionally parse quantized dimension for per-axis or sub-channel |
| 378 | // quantization. |
| 379 | if (succeeded(Result: parser.parseOptionalColon())) { |
| 380 | if (succeeded(Result: parser.parseOptionalLBrace())) { |
| 381 | isSubChannel = true; |
| 382 | if (parseBlockSizeInfoUntilRBrace(parser, quantizedDimensions, |
| 383 | blockSizes)) { |
| 384 | return nullptr; |
| 385 | } |
| 386 | } else { |
| 387 | isPerAxis = true; |
| 388 | quantizedDimensions.resize(N: 1); |
| 389 | if (parser.parseInteger(result&: quantizedDimensions.back())) { |
| 390 | return nullptr; |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | // Comma leading into range_spec. |
| 396 | if (parser.parseComma()) { |
| 397 | return nullptr; |
| 398 | } |
| 399 | |
| 400 | // Quantization parameter (scales/zeroPoints) specification. |
| 401 | bool isPerTensor = !isPerAxis && !isSubChannel; |
| 402 | SmallVector<int64_t> dims; |
| 403 | if (isPerTensor) { |
| 404 | zeroPoints.resize(N: zeroPoints.size() + 1); |
| 405 | scales.resize(N: scales.size() + 1); |
| 406 | if (parseQuantParams(parser, expressedType, scale&: scales.back(), |
| 407 | zeroPoint&: zeroPoints.back())) { |
| 408 | return nullptr; |
| 409 | } |
| 410 | |
| 411 | } else { |
| 412 | if (parser.parseLBrace() || |
| 413 | parseQuantParamListUntilRBrace(parser, expressedType, scales, |
| 414 | zeroPoints, dims)) { |
| 415 | return nullptr; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | if (parser.parseGreater()) { |
| 420 | return nullptr; |
| 421 | } |
| 422 | |
| 423 | if (isPerAxis) { |
| 424 | return parser.getChecked<UniformQuantizedPerAxisType>( |
| 425 | params&: typeFlags, params&: storageType, params&: expressedType, params&: scales, params&: zeroPoints, |
| 426 | params&: quantizedDimensions[0], params&: storageTypeMin, params&: storageTypeMax); |
| 427 | } |
| 428 | if (isSubChannel) { |
| 429 | SmallVector<APFloat> apFloatScales = |
| 430 | llvm::map_to_vector(C&: scales, F: [&](double scale) -> APFloat { |
| 431 | APFloat apFloatScale(scale); |
| 432 | bool unused; |
| 433 | apFloatScale.convert(ToSemantics: expressedType.getFloatSemantics(), |
| 434 | RM: APFloat::rmNearestTiesToEven, losesInfo: &unused); |
| 435 | return apFloatScale; |
| 436 | }); |
| 437 | SmallVector<APInt> apIntZeroPoints = |
| 438 | llvm::map_to_vector(C&: zeroPoints, F: [&](int64_t zeroPoint) -> APInt { |
| 439 | return APInt(storageType.getIntOrFloatBitWidth(), zeroPoint); |
| 440 | }); |
| 441 | auto scalesRef = mlir::DenseElementsAttr::get( |
| 442 | type: RankedTensorType::get(shape: dims, elementType: expressedType), values: apFloatScales); |
| 443 | auto zeroPointsRef = mlir::DenseElementsAttr::get( |
| 444 | type: RankedTensorType::get(shape: dims, elementType: storageType), values: apIntZeroPoints); |
| 445 | return parser.getChecked<UniformQuantizedSubChannelType>( |
| 446 | params&: typeFlags, params&: storageType, params&: expressedType, params&: scalesRef, params&: zeroPointsRef, |
| 447 | params&: quantizedDimensions, params&: blockSizes, params&: storageTypeMin, params&: storageTypeMax); |
| 448 | } |
| 449 | |
| 450 | return parser.getChecked<UniformQuantizedType>( |
| 451 | params&: typeFlags, params&: storageType, params&: expressedType, params&: scales.front(), params&: zeroPoints.front(), |
| 452 | params&: storageTypeMin, params&: storageTypeMax); |
| 453 | } |
| 454 | |
| 455 | /// Parses an CalibratedQuantizedType. |
| 456 | /// |
| 457 | /// calibrated ::= `calibrated<` expressed-spec `>` |
| 458 | /// expressed-spec ::= expressed-type `<` calibrated-range `>` |
| 459 | /// expressed-type ::= `f` integer-literal |
| 460 | /// calibrated-range ::= float-literal `:` float-literal |
| 461 | static Type parseCalibratedType(DialectAsmParser &parser) { |
| 462 | FloatType expressedType; |
| 463 | double min; |
| 464 | double max; |
| 465 | |
| 466 | // Type specification. |
| 467 | if (parser.parseLess()) |
| 468 | return nullptr; |
| 469 | |
| 470 | // Expressed type. |
| 471 | expressedType = parseExpressedTypeAndRange(parser, min, max); |
| 472 | if (!expressedType) { |
| 473 | return nullptr; |
| 474 | } |
| 475 | |
| 476 | if (parser.parseGreater()) { |
| 477 | return nullptr; |
| 478 | } |
| 479 | |
| 480 | return parser.getChecked<CalibratedQuantizedType>(params&: expressedType, params&: min, params&: max); |
| 481 | } |
| 482 | |
| 483 | static Type parseQuantileType(DialectAsmParser &parser) { |
| 484 | Type storageType; |
| 485 | Type quantileType; |
| 486 | SmallVector<double, 1> quantiles; |
| 487 | |
| 488 | if (parser.parseLess()) |
| 489 | return nullptr; |
| 490 | if (parser.parseType(result&: storageType)) |
| 491 | return nullptr; |
| 492 | if (parser.parseColon()) |
| 493 | return nullptr; |
| 494 | if (parser.parseType(result&: quantileType)) |
| 495 | return nullptr; |
| 496 | if (parser.parseComma()) |
| 497 | return nullptr; |
| 498 | if (parser.parseLBrace()) |
| 499 | return nullptr; |
| 500 | |
| 501 | // Allow empty braces `{}` — verify() will catch the empty quantile error. |
| 502 | if (failed(Result: parser.parseOptionalRBrace())) { |
| 503 | do { |
| 504 | quantiles.emplace_back(); |
| 505 | if (parser.parseFloat(result&: quantiles.back())) |
| 506 | return nullptr; |
| 507 | } while (succeeded(Result: parser.parseOptionalComma())); |
| 508 | |
| 509 | if (parser.parseRBrace()) |
| 510 | return nullptr; |
| 511 | } |
| 512 | |
| 513 | // Optionally parse explicit storage range: `, min:max` (inside the outer |
| 514 | // `<>`). |
| 515 | std::optional<int64_t> storageMin, storageMax; |
| 516 | if (succeeded(Result: parser.parseOptionalComma())) { |
| 517 | if (parser.parseLess()) |
| 518 | return nullptr; |
| 519 | int64_t minVal, maxVal; |
| 520 | if (parser.parseInteger(result&: minVal) || parser.parseColon() || |
| 521 | parser.parseInteger(result&: maxVal)) |
| 522 | return nullptr; |
| 523 | storageMin = minVal; |
| 524 | storageMax = maxVal; |
| 525 | if (parser.parseGreater()) |
| 526 | return nullptr; |
| 527 | } |
| 528 | |
| 529 | if (parser.parseGreater()) |
| 530 | return nullptr; |
| 531 | |
| 532 | mlir::MLIRContext *ctx = parser.getContext(); |
| 533 | return parser.getChecked<QuantileType>(params&: ctx, params&: storageType, params&: quantileType, |
| 534 | params&: quantiles, params&: storageMin, params&: storageMax); |
| 535 | } |
| 536 | |
| 537 | /// Parse a type registered to this dialect. |
| 538 | Type QuantDialect::parseType(DialectAsmParser &parser) const { |
| 539 | // All types start with an identifier that we switch on. |
| 540 | StringRef typeNameSpelling; |
| 541 | if (failed(Result: parser.parseKeyword(keyword: &typeNameSpelling))) |
| 542 | return nullptr; |
| 543 | |
| 544 | if (typeNameSpelling == "uniform" ) |
| 545 | return parseUniformType(parser); |
| 546 | if (typeNameSpelling == "any" ) |
| 547 | return parseAnyType(parser); |
| 548 | if (typeNameSpelling == "calibrated" ) |
| 549 | return parseCalibratedType(parser); |
| 550 | if (typeNameSpelling == "quantile" ) |
| 551 | return parseQuantileType(parser); |
| 552 | |
| 553 | parser.emitError(loc: parser.getNameLoc(), |
| 554 | message: "unknown quantized type " + typeNameSpelling); |
| 555 | return nullptr; |
| 556 | } |
| 557 | |
| 558 | static void printStorageType(QuantizedType type, DialectAsmPrinter &out) { |
| 559 | // storage type |
| 560 | auto quantStorageTypeInterface = |
| 561 | llvm::dyn_cast<QuantStorageTypeInterface>(Val: type.getStorageType()); |
| 562 | |
| 563 | out << quantStorageTypeInterface.getStorageTypeName(isSigned: type.isSigned()); |
| 564 | |
| 565 | // storageTypeMin and storageTypeMax if not default. |
| 566 | if (type.hasStorageTypeBounds()) { |
| 567 | out << "<" << type.getStorageTypeMin() << ":" << type.getStorageTypeMax() |
| 568 | << ">" ; |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | static void printQuantParams(double scale, int64_t zeroPoint, |
| 573 | DialectAsmPrinter &out) { |
| 574 | out << scale; |
| 575 | if (zeroPoint != 0) { |
| 576 | out << ":" << zeroPoint; |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | static void |
| 581 | printBlockSizeInfo(ArrayRef<std::pair<int32_t, int64_t>> blockSizeInfo, |
| 582 | DialectAsmPrinter &out) { |
| 583 | out << "{" ; |
| 584 | llvm::interleaveComma( |
| 585 | c: llvm::seq<size_t>(Begin: 0, End: blockSizeInfo.size()), os&: out, each_fn: [&](size_t index) { |
| 586 | out << blockSizeInfo[index].first << ":" << blockSizeInfo[index].second; |
| 587 | }); |
| 588 | out << "}" ; |
| 589 | } |
| 590 | |
| 591 | /// Helper that prints a AnyQuantizedType. |
| 592 | static void printAnyQuantizedType(AnyQuantizedType type, |
| 593 | DialectAsmPrinter &out) { |
| 594 | out << "any<" ; |
| 595 | printStorageType(type, out); |
| 596 | if (Type expressedType = type.getExpressedType()) { |
| 597 | out << ":" << expressedType; |
| 598 | } |
| 599 | out << ">" ; |
| 600 | } |
| 601 | |
| 602 | /// Helper that prints a UniformQuantizedType. |
| 603 | static void printUniformQuantizedType(UniformQuantizedType type, |
| 604 | DialectAsmPrinter &out) { |
| 605 | out << "uniform<" ; |
| 606 | printStorageType(type, out); |
| 607 | out << ":" << type.getExpressedType() << ", " ; |
| 608 | |
| 609 | // scheme specific parameters |
| 610 | printQuantParams(scale: type.getScale(), zeroPoint: type.getZeroPoint(), out); |
| 611 | out << ">" ; |
| 612 | } |
| 613 | |
| 614 | /// Helper that prints a UniformQuantizedPerAxisType. |
| 615 | static void printUniformQuantizedPerAxisType(UniformQuantizedPerAxisType type, |
| 616 | DialectAsmPrinter &out) { |
| 617 | out << "uniform<" ; |
| 618 | printStorageType(type, out); |
| 619 | out << ":" << type.getExpressedType() << ":" ; |
| 620 | out << type.getQuantizedDimension(); |
| 621 | out << ", " ; |
| 622 | |
| 623 | // scheme specific parameters |
| 624 | ArrayRef<double> scales = type.getScales(); |
| 625 | ArrayRef<int64_t> zeroPoints = type.getZeroPoints(); |
| 626 | out << "{" ; |
| 627 | llvm::interleave( |
| 628 | c: llvm::seq<size_t>(Begin: 0, End: scales.size()), os&: out, |
| 629 | each_fn: [&](size_t index) { |
| 630 | printQuantParams(scale: scales[index], zeroPoint: zeroPoints[index], out); |
| 631 | }, |
| 632 | separator: "," ); |
| 633 | out << "}>" ; |
| 634 | } |
| 635 | |
| 636 | /// Prints quantization parameters as a nested list of `scale`[:`zero_point`] |
| 637 | /// elements. The nesting corresponds to the `shape` dimensions. |
| 638 | /// |
| 639 | /// Elements are delimited by commas, and the inner dimensions are enclosed in |
| 640 | /// braces. `zero_point` is only printed if it is non-zero. For example: |
| 641 | /// |
| 642 | /// printDenseQuantizationParameters(scales=[1.0, 2.0, 3.0, 4.0], |
| 643 | /// zeroPoints=[0, 0, 1, 9], |
| 644 | /// shape=[2, 2]) |
| 645 | /// |
| 646 | /// would print: |
| 647 | /// |
| 648 | /// {{1.0, 2.0}, {3.0:1, 4.0:9}} |
| 649 | static void printDenseQuantizationParameters(ArrayRef<APFloat> scales, |
| 650 | ArrayRef<APInt> zeroPoints, |
| 651 | ArrayRef<int64_t> shape, |
| 652 | DialectAsmPrinter &out) { |
| 653 | int64_t rank = shape.size(); |
| 654 | SmallVector<unsigned, 4> counter(rank, 0); |
| 655 | unsigned openBrackets = 0; |
| 656 | |
| 657 | auto incrementCounterAndDelimit = [&]() { |
| 658 | ++counter[rank - 1]; |
| 659 | for (unsigned i = rank - 1; i > 0; --i) { |
| 660 | if (counter[i] >= shape[i]) { |
| 661 | counter[i] = 0; |
| 662 | ++counter[i - 1]; |
| 663 | --openBrackets; |
| 664 | out << '}'; |
| 665 | } |
| 666 | } |
| 667 | }; |
| 668 | |
| 669 | for (unsigned idx = 0, e = scales.size(); idx < e; ++idx) { |
| 670 | if (idx != 0) |
| 671 | out << ", " ; |
| 672 | while (openBrackets++ < rank) |
| 673 | out << '{'; |
| 674 | openBrackets = rank; |
| 675 | out << scales[idx]; |
| 676 | if (zeroPoints[idx] != 0) { |
| 677 | out << ":" << zeroPoints[idx]; |
| 678 | } |
| 679 | incrementCounterAndDelimit(); |
| 680 | } |
| 681 | while (openBrackets-- > 0) |
| 682 | out << '}'; |
| 683 | } |
| 684 | |
| 685 | /// Helper that prints a UniformQuantizedSubChannelType. |
| 686 | static void |
| 687 | printUniformQuantizedSubChannelType(UniformQuantizedSubChannelType type, |
| 688 | DialectAsmPrinter &out) { |
| 689 | out << "uniform<" ; |
| 690 | printStorageType(type, out); |
| 691 | out << ":" << type.getExpressedType() << ":" ; |
| 692 | printBlockSizeInfo(blockSizeInfo: type.getBlockSizeInfo(), out); |
| 693 | out << ", " ; |
| 694 | |
| 695 | auto scalesItr = type.getScales().getValues<APFloat>(); |
| 696 | auto zeroPointsItr = type.getZeroPoints().getValues<APInt>(); |
| 697 | SmallVector<APFloat> scales(scalesItr.begin(), scalesItr.end()); |
| 698 | SmallVector<APInt> zeroPoints(zeroPointsItr.begin(), zeroPointsItr.end()); |
| 699 | printDenseQuantizationParameters(scales, zeroPoints, |
| 700 | shape: type.getScales().getType().getShape(), out); |
| 701 | out << ">" ; |
| 702 | } |
| 703 | |
| 704 | /// Helper that prints a CalibratedQuantizedType. |
| 705 | static void printCalibratedQuantizedType(CalibratedQuantizedType type, |
| 706 | DialectAsmPrinter &out) { |
| 707 | out << "calibrated<" << type.getExpressedType(); |
| 708 | out << "<" << type.getMin() << ":" << type.getMax() << ">" ; |
| 709 | out << ">" ; |
| 710 | } |
| 711 | |
| 712 | static void printQuantileType(QuantileType type, DialectAsmPrinter &out) { |
| 713 | out << "quantile<" ; |
| 714 | out << type.getStorageType(); |
| 715 | out << ":" ; |
| 716 | out << type.getQuantileType(); |
| 717 | out << ", {" ; |
| 718 | ArrayRef<double> quantiles = type.getQuantiles(); |
| 719 | llvm::interleave( |
| 720 | c: llvm::seq<size_t>(Begin: 0, End: quantiles.size()), os&: out, |
| 721 | each_fn: [&](size_t index) { out << quantiles[index]; }, separator: "," ); |
| 722 | out << "}" ; |
| 723 | if (auto minVal = type.getStorageMin()) |
| 724 | if (auto maxVal = type.getStorageMax()) |
| 725 | out << ", <" << *minVal << ":" << *maxVal << ">" ; |
| 726 | out << ">" ; |
| 727 | } |
| 728 | |
| 729 | /// Print a type registered to this dialect. |
| 730 | void QuantDialect::printType(Type type, DialectAsmPrinter &os) const { |
| 731 | if (auto anyType = llvm::dyn_cast<AnyQuantizedType>(Val&: type)) |
| 732 | printAnyQuantizedType(type: anyType, out&: os); |
| 733 | else if (auto uniformType = llvm::dyn_cast<UniformQuantizedType>(Val&: type)) |
| 734 | printUniformQuantizedType(type: uniformType, out&: os); |
| 735 | else if (auto perAxisType = llvm::dyn_cast<UniformQuantizedPerAxisType>(Val&: type)) |
| 736 | printUniformQuantizedPerAxisType(type: perAxisType, out&: os); |
| 737 | else if (auto perAxisType = |
| 738 | llvm::dyn_cast<UniformQuantizedSubChannelType>(Val&: type)) |
| 739 | printUniformQuantizedSubChannelType(type: perAxisType, out&: os); |
| 740 | else if (auto calibratedType = llvm::dyn_cast<CalibratedQuantizedType>(Val&: type)) |
| 741 | printCalibratedQuantizedType(type: calibratedType, out&: os); |
| 742 | else if (auto quantileType = llvm::dyn_cast<QuantileType>(Val&: type)) |
| 743 | printQuantileType(type: quantileType, out&: os); |
| 744 | else |
| 745 | llvm_unreachable("Unhandled quantized type" ); |
| 746 | } |
| 747 | |