MLIR 22.0.0git
TypeParser.cpp File Reference
#include "mlir/Dialect/Quant/IR/Quant.h"
#include "mlir/Dialect/Quant/IR/QuantTypes.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/DialectImplementation.h"
#include "mlir/IR/Types.h"
#include "llvm/ADT/APFloat.h"

Go to the source code of this file.

Functions

static IntegerType parseStorageType (DialectAsmParser &parser, bool &isSigned)
static ParseResult parseStorageRange (DialectAsmParser &parser, IntegerType storageType, bool isSigned, int64_t &storageTypeMin, int64_t &storageTypeMax)
static FloatType parseExpressedTypeAndRange (DialectAsmParser &parser, double &min, double &max)
static Type parseAnyType (DialectAsmParser &parser)
 Parses an AnyQuantizedType.
static LogicalResult isScaleInExpressedTypeRange (function_ref< InFlightDiagnostic()> emitError, Type expressedType, double scale)
 Checks if the given scale value is within the valid range of the expressed type.
static ParseResult parseQuantParams (DialectAsmParser &parser, Type expressedType, double &scale, int64_t &zeroPoint)
 Parses a quantization parameter, which is either a scale value (float) or a scale-zero point pair (float:integer).
static ParseResult parseBlockSizeInfoUntilRBrace (DialectAsmParser &parser, SmallVectorImpl< int32_t > &quantizedDimensions, SmallVectorImpl< int64_t > &blockSizes)
 Parses block size information for sub-channel quantization, assuming the leading '{' has already been parsed.
static ParseResult parseQuantParamListUntilRBrace (DialectAsmParser &parser, Type expressedType, SmallVectorImpl< double > &scales, SmallVectorImpl< int64_t > &zeroPoints, SmallVectorImpl< int64_t > &dims)
 Parses a bracketed list of quantization parameters, returning the dimensions of the parsed sub-tensors in dims.
static Type parseUniformType (DialectAsmParser &parser)
 Parses a UniformQuantizedType.
static Type parseCalibratedType (DialectAsmParser &parser)
 Parses an CalibratedQuantizedType.
static void printStorageType (QuantizedType type, DialectAsmPrinter &out)
static void printQuantParams (double scale, int64_t zeroPoint, DialectAsmPrinter &out)
static void printBlockSizeInfo (ArrayRef< std::pair< int32_t, int64_t > > blockSizeInfo, DialectAsmPrinter &out)
static void printAnyQuantizedType (AnyQuantizedType type, DialectAsmPrinter &out)
 Helper that prints a AnyQuantizedType.
static void printUniformQuantizedType (UniformQuantizedType type, DialectAsmPrinter &out)
 Helper that prints a UniformQuantizedType.
static void printUniformQuantizedPerAxisType (UniformQuantizedPerAxisType type, DialectAsmPrinter &out)
 Helper that prints a UniformQuantizedPerAxisType.
static void printDenseQuantizationParameters (ArrayRef< APFloat > scales, ArrayRef< APInt > zeroPoints, ArrayRef< int64_t > shape, DialectAsmPrinter &out)
 Prints quantization parameters as a nested list of scale[:zero_point] elements.
static void printUniformQuantizedSubChannelType (UniformQuantizedSubChannelType type, DialectAsmPrinter &out)
 Helper that prints a UniformQuantizedSubChannelType.
static void printCalibratedQuantizedType (CalibratedQuantizedType type, DialectAsmPrinter &out)
 Helper that prints a CalibratedQuantizedType.

Function Documentation

◆ isScaleInExpressedTypeRange()

LogicalResult isScaleInExpressedTypeRange ( function_ref< InFlightDiagnostic()> emitError,
Type expressedType,
double scale )
static

Checks if the given scale value is within the valid range of the expressed type.

The expressedType argument is the floating-point type used for expressing the quantized values, and scale is the double value to check.

Definition at line 161 of file TypeParser.cpp.

References mlir::emitError(), and success().

Referenced by parseQuantParams().

◆ parseAnyType()

Type parseAnyType ( DialectAsmParser & parser)
static

Parses an AnyQuantizedType.

any ::= any< storage-spec (expressed-type-spec)?> storage-spec ::= storage-type (< storage-range >)? storage-range ::= integer-literal : integer-literal storage-type ::= (i | u) integer-literal expressed-type-spec ::= : f integer-literal

Definition at line 115 of file TypeParser.cpp.

References mlir::AsmParser::getChecked(), mlir::AsmParser::parseGreater(), mlir::AsmParser::parseLess(), mlir::AsmParser::parseOptionalColon(), parseStorageRange(), parseStorageType(), mlir::AsmParser::parseType(), and mlir::quant::QuantizationFlags::Signed.

◆ parseBlockSizeInfoUntilRBrace()

ParseResult parseBlockSizeInfoUntilRBrace ( DialectAsmParser & parser,
SmallVectorImpl< int32_t > & quantizedDimensions,
SmallVectorImpl< int64_t > & blockSizes )
static

Parses block size information for sub-channel quantization, assuming the leading '{' has already been parsed.

The block size information is provided as a comma-separated list of "Axis:BlockSize" pairs, terminated by a '}'.

The parsed axis indices are stored in quantizedDimensions, and the corresponding block sizes are stored in blockSizes.

Definition at line 207 of file TypeParser.cpp.

References mlir::AsmParser::parseColon(), mlir::AsmParser::parseCommaSeparatedList(), mlir::AsmParser::parseInteger(), mlir::AsmParser::parseOptionalRBrace(), mlir::AsmParser::parseRBrace(), and success().

Referenced by parseUniformType().

◆ parseCalibratedType()

Type parseCalibratedType ( DialectAsmParser & parser)
static

Parses an CalibratedQuantizedType.

calibrated ::= calibrated< expressed-spec > expressed-spec ::= expressed-type < calibrated-range > expressed-type ::= f integer-literal calibrated-range ::= float-literal : float-literal

Definition at line 447 of file TypeParser.cpp.

References mlir::AsmParser::getChecked(), max(), min(), parseExpressedTypeAndRange(), mlir::AsmParser::parseGreater(), and mlir::AsmParser::parseLess().

◆ parseExpressedTypeAndRange()

◆ parseQuantParamListUntilRBrace()

ParseResult parseQuantParamListUntilRBrace ( DialectAsmParser & parser,
Type expressedType,
SmallVectorImpl< double > & scales,
SmallVectorImpl< int64_t > & zeroPoints,
SmallVectorImpl< int64_t > & dims )
static

Parses a bracketed list of quantization parameters, returning the dimensions of the parsed sub-tensors in dims.

The dimension of the list is prepended to the dimensions of the sub-tensors. This function assumes that the initial left brace has already been parsed. For example:

parseQuantParamListUntilRBrace(1.0:1, 2.0:4, 3.0:4}) -> Success, dims = [3], scales = [1.0, 2.0, 3.0], zeroPoints = [1, 4, 4]

parseQuantParamListUntilRBrace({1.0, 2.0}, {3.0:1, 4.0:9}}) -> Success, dims = [2, 2], scales = [1.0, 2.0, 3.0, 4.0], zeroPoints = [0, 0, 1, 9]

This function expects all sub-tensors to have the same rank.

Definition at line 246 of file TypeParser.cpp.

References mlir::AsmParser::emitError(), mlir::AsmParser::getCurrentLocation(), mlir::AsmParser::parseCommaSeparatedList(), mlir::AsmParser::parseOptionalLBrace(), parseQuantParamListUntilRBrace(), parseQuantParams(), mlir::AsmParser::parseRBrace(), and success().

Referenced by parseQuantParamListUntilRBrace(), and parseUniformType().

◆ parseQuantParams()

ParseResult parseQuantParams ( DialectAsmParser & parser,
Type expressedType,
double & scale,
int64_t & zeroPoint )
static

Parses a quantization parameter, which is either a scale value (float) or a scale-zero point pair (float:integer).

expressedType, expressing the type of scale values, is used to validate the scale. The parsed scale and zero point (if any) are stored in scale and zeroPoint.

Definition at line 178 of file TypeParser.cpp.

References mlir::AsmParser::emitError(), mlir::AsmParser::getCurrentLocation(), isScaleInExpressedTypeRange(), mlir::AsmParser::parseFloat(), mlir::AsmParser::parseInteger(), mlir::AsmParser::parseOptionalColon(), and success().

Referenced by parseQuantParamListUntilRBrace(), and parseUniformType().

◆ parseStorageRange()

◆ parseStorageType()

◆ parseUniformType()

Type parseUniformType ( DialectAsmParser & parser)
static

Parses a UniformQuantizedType.

uniform_type ::= uniform_per_layer | uniform_per_axis | uniform_sub_channel uniform_per_layer ::= uniform< storage-spec expressed-type-spec , scale-zero > uniform_per_axis ::= uniform< storage-spec expressed-type-spec axis-spec , { scale-zero-list } > uniform_sub_channel ::= uniform< storage-spec expressed-type-spec block-size-info , scale-zero-tensor > storage-spec ::= storage-type (< storage-range >)? storage-range ::= integer-literal : integer-literal storage-type ::= (i | u) integer-literal expressed-type-spec ::= : f integer-literal axis-spec ::= : integer-literal scale-zero ::= scale (: zero-point)? scale ::= float-literal zero-point ::= integer-literal scale-zero-list ::= scale-zero (, scale-zero)* block-size-info ::= { } | { axis-block : (, axis-block)* } axis-block ::= axis-spec : block-size-spec block-size-spec ::= integer-literal scale-zero-tensor ::= scale-zero-dense-exp | scale-zero-list scale-zero-dense-exp ::= { scale-zero-tensor (, scale-zero-tensor)* }

Definition at line 324 of file TypeParser.cpp.

References mlir::DenseElementsAttr::get(), mlir::AsmParser::getChecked(), parseBlockSizeInfoUntilRBrace(), mlir::AsmParser::parseColon(), mlir::AsmParser::parseComma(), mlir::AsmParser::parseGreater(), mlir::AsmParser::parseInteger(), mlir::AsmParser::parseLBrace(), mlir::AsmParser::parseLess(), mlir::AsmParser::parseOptionalColon(), mlir::AsmParser::parseOptionalLBrace(), parseQuantParamListUntilRBrace(), parseQuantParams(), parseStorageRange(), parseStorageType(), mlir::AsmParser::parseType(), and mlir::quant::QuantizationFlags::Signed.

◆ printAnyQuantizedType()

void printAnyQuantizedType ( AnyQuantizedType type,
DialectAsmPrinter & out )
static

Helper that prints a AnyQuantizedType.

Definition at line 525 of file TypeParser.cpp.

References printStorageType().

◆ printBlockSizeInfo()

void printBlockSizeInfo ( ArrayRef< std::pair< int32_t, int64_t > > blockSizeInfo,
DialectAsmPrinter & out )
static

Definition at line 514 of file TypeParser.cpp.

Referenced by printUniformQuantizedSubChannelType().

◆ printCalibratedQuantizedType()

void printCalibratedQuantizedType ( CalibratedQuantizedType type,
DialectAsmPrinter & out )
static

◆ printDenseQuantizationParameters()

void printDenseQuantizationParameters ( ArrayRef< APFloat > scales,
ArrayRef< APInt > zeroPoints,
ArrayRef< int64_t > shape,
DialectAsmPrinter & out )
static

Prints quantization parameters as a nested list of scale[:zero_point] elements.

The nesting corresponds to the shape dimensions.

Elements are delimited by commas, and the inner dimensions are enclosed in braces. zero_point is only printed if it is non-zero. For example:

printDenseQuantizationParameters(scales=[1.0, 2.0, 3.0, 4.0], zeroPoints=[0, 0, 1, 9], shape=[2, 2])

would print:

{{1.0, 2.0}, {3.0:1, 4.0:9}}

Definition at line 582 of file TypeParser.cpp.

Referenced by printUniformQuantizedSubChannelType().

◆ printQuantParams()

void printQuantParams ( double scale,
int64_t zeroPoint,
DialectAsmPrinter & out )
static

Definition at line 505 of file TypeParser.cpp.

Referenced by printUniformQuantizedType().

◆ printStorageType()

◆ printUniformQuantizedPerAxisType()

◆ printUniformQuantizedSubChannelType()

◆ printUniformQuantizedType()