MLIR  21.0.0git
Functions
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/Location.h"
#include "mlir/IR/Types.h"
#include "llvm/ADT/APFloat.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/raw_ostream.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. More...
 
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. More...
 
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). More...
 
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. More...
 
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. More...
 
static Type parseUniformType (DialectAsmParser &parser)
 Parses a UniformQuantizedType. More...
 
static Type parseCalibratedType (DialectAsmParser &parser)
 Parses an CalibratedQuantizedType. More...
 
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. More...
 
static void printUniformQuantizedType (UniformQuantizedType type, DialectAsmPrinter &out)
 Helper that prints a UniformQuantizedType. More...
 
static void printUniformQuantizedPerAxisType (UniformQuantizedPerAxisType type, DialectAsmPrinter &out)
 Helper that prints a UniformQuantizedPerAxisType. More...
 
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. More...
 
static void printUniformQuantizedSubChannelType (UniformQuantizedSubChannelType type, DialectAsmPrinter &out)
 Helper that prints a UniformQuantizedSubChannelType. More...
 
static void printCalibratedQuantizedType (CalibratedQuantizedType type, DialectAsmPrinter &out)
 Helper that prints a CalibratedQuantizedType. More...
 

Function Documentation

◆ isScaleInExpressedTypeRange()

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.

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 166 of file TypeParser.cpp.

References mlir::emitError().

Referenced by parseQuantParams().

◆ parseAnyType()

static 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 120 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()

static 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 212 of file TypeParser.cpp.

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

Referenced by parseUniformType().

◆ parseCalibratedType()

static 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 451 of file TypeParser.cpp.

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

◆ parseExpressedTypeAndRange()

static FloatType parseExpressedTypeAndRange ( DialectAsmParser parser,
double &  min,
double &  max 
)
static

◆ parseQuantParamListUntilRBrace()

static 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 251 of file TypeParser.cpp.

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

Referenced by parseUniformType().

◆ parseQuantParams()

static 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 183 of file TypeParser.cpp.

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

Referenced by parseQuantParamListUntilRBrace(), and parseUniformType().

◆ parseStorageRange()

static ParseResult parseStorageRange ( DialectAsmParser parser,
IntegerType  storageType,
bool  isSigned,
int64_t &  storageTypeMin,
int64_t &  storageTypeMax 
)
static

◆ parseStorageType()

static IntegerType parseStorageType ( DialectAsmParser parser,
bool &  isSigned 
)
static

◆ parseUniformType()

static 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 329 of file TypeParser.cpp.

References mlir::get(), 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()

static void printAnyQuantizedType ( AnyQuantizedType  type,
DialectAsmPrinter out 
)
static

Helper that prints a AnyQuantizedType.

Definition at line 529 of file TypeParser.cpp.

References printStorageType().

◆ printBlockSizeInfo()

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

Definition at line 518 of file TypeParser.cpp.

Referenced by printUniformQuantizedSubChannelType().

◆ printCalibratedQuantizedType()

static void printCalibratedQuantizedType ( CalibratedQuantizedType  type,
DialectAsmPrinter out 
)
static

Helper that prints a CalibratedQuantizedType.

Definition at line 642 of file TypeParser.cpp.

References mlir::quant::CalibratedQuantizedType::getMax(), and mlir::quant::CalibratedQuantizedType::getMin().

◆ printDenseQuantizationParameters()

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.

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 586 of file TypeParser.cpp.

Referenced by printUniformQuantizedSubChannelType().

◆ printQuantParams()

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

Definition at line 509 of file TypeParser.cpp.

Referenced by printUniformQuantizedType().

◆ printStorageType()

static void printStorageType ( QuantizedType  type,
DialectAsmPrinter out 
)
static

◆ printUniformQuantizedPerAxisType()

static void printUniformQuantizedPerAxisType ( UniformQuantizedPerAxisType  type,
DialectAsmPrinter out 
)
static

◆ printUniformQuantizedSubChannelType()

static void printUniformQuantizedSubChannelType ( UniformQuantizedSubChannelType  type,
DialectAsmPrinter out 
)
static

◆ printUniformQuantizedType()

static void printUniformQuantizedType ( UniformQuantizedType  type,
DialectAsmPrinter out 
)
static