NFC: Fix some post-review nits for the Tosa dialect.

* Moved various loose functions to either the mlir::tosa namespace or made static
* Fixed an unused variable warning in TosaMakeBroadcastable.cpp.
This commit is contained in:
Stella Laurenzo 2020-11-07 08:54:31 -08:00
parent b28121133d
commit b5fcd06105
8 changed files with 84 additions and 75 deletions

View File

@ -17,8 +17,7 @@ include "mlir/IR/OpBase.td"
def TosaOpInterface : OpInterface<"TosaOp"> {
let description = [{
Implements interfaces implemented by ops that correspond to the Tosa
specification.
Implemented by ops that correspond to the Tosa specification.
}];
}

View File

@ -114,9 +114,9 @@ def Tosa_ConvOpQuantInfoBuilder : OpBuilderDAG<
(ins "Type":$outputType, "Value":$input, "Value":$weight, "Value":$bias,
"ArrayAttr":$pad, "ArrayAttr":$stride, "ArrayAttr":$dilation),
[{
::buildConvOpWithQuantInfo($_builder, $_state, outputType,
input, weight, bias,
pad, stride, dilation);
buildConvOpWithQuantInfo($_builder, $_state, outputType,
input, weight, bias,
pad, stride, dilation);
}]>;
// Handles tosa.transpose_conv2d which has an outpad and output shape attribute.
@ -125,10 +125,10 @@ def Tosa_TransConvOpQuantInfoBuilder : OpBuilderDAG<
"ArrayAttr":$outpad, "ArrayAttr":$stride, "ArrayAttr":$dilation,
"ArrayAttr":$outputShape),
[{
::buildTransConvOpWithQuantInfo($_builder, $_state, outputType,
input, weight, bias,
outpad, stride, dilation,
outputShape);
buildTransConvOpWithQuantInfo($_builder, $_state, outputType,
input, weight, bias,
outpad, stride, dilation,
outputShape);
}]>;
// The tosa.fully_connected op has its own builder as it does not have
@ -136,8 +136,8 @@ def Tosa_TransConvOpQuantInfoBuilder : OpBuilderDAG<
def Tosa_FCOpQuantInfoBuilder : OpBuilderDAG<
(ins "Type":$outputType, "Value":$input, "Value":$weight, "Value":$bias),
[{
::buildFCOpWithQuantInfo($_builder, $_state, outputType,
input, weight, bias);
buildFCOpWithQuantInfo($_builder, $_state, outputType,
input, weight, bias);
}]>;
// The tosa.matmul op is also intended to be generated where a fully_connected
@ -147,8 +147,8 @@ def Tosa_FCOpQuantInfoBuilder : OpBuilderDAG<
def Tosa_MatMulOpQuantInfoBuilder : OpBuilderDAG<
(ins "Type":$outputType, "Value":$a, "Value":$b),
[{
::buildMatMulOpWithQuantInfo($_builder, $_state, outputType,
a, b);
buildMatMulOpWithQuantInfo($_builder, $_state, outputType,
a, b);
}]>;
// Both the tosa.avg_pool2d and unary ops use the same
@ -158,8 +158,8 @@ def Tosa_AvgPool2dOpQuantInfoBuilder : OpBuilderDAG<
(ins "Type":$outputType, "Value":$input, "ArrayAttr":$kernel,
"ArrayAttr":$stride, "ArrayAttr":$pad),
[{
::buildAvgPool2dOpWithQuantInfo($_builder, $_state, outputType,
input, kernel, stride, pad);
buildAvgPool2dOpWithQuantInfo($_builder, $_state, outputType,
input, kernel, stride, pad);
}]>;
// This builder is called on single-parameter unary operators that have a scale
@ -168,7 +168,7 @@ def Tosa_AvgPool2dOpQuantInfoBuilder : OpBuilderDAG<
def Tosa_UnaryOpQuantInfoBuilder : OpBuilderDAG<
(ins "Type":$outputType, "Value":$input),
[{
::buildUnaryOpWithQuantInfo($_builder, $_state, outputType, input);
buildUnaryOpWithQuantInfo($_builder, $_state, outputType, input);
}]>;
// This builder is called on the TOSA pad operator that needs to create its own
@ -177,8 +177,8 @@ def Tosa_UnaryOpQuantInfoBuilder : OpBuilderDAG<
def Tosa_PadOpQuantInfoBuilder : OpBuilderDAG<
(ins "Type":$outputType, "Value":$input, "Value":$paddings),
[{
::buildPadOpWithQuantInfo($_builder, $_state, outputType,
input, paddings);
buildPadOpWithQuantInfo($_builder, $_state, outputType,
input, paddings);
}]>;
//===----------------------------------------------------------------------===//

View File

@ -104,7 +104,7 @@ def Tosa_Conv2DOp : Tosa_Op<"conv2d", [NoSideEffect]> {
let builders = [Tosa_ConvOpQuantInfoBuilder];
let verifier = [{ return ::verifyConvOp(*this); }];
let verifier = [{ return verifyConvOp(*this); }];
}
//===----------------------------------------------------------------------===//
@ -134,7 +134,7 @@ def Tosa_Conv3DOp : Tosa_Op<"conv3d", [NoSideEffect]> {
let builders = [Tosa_ConvOpQuantInfoBuilder];
let verifier = [{ return ::verifyConvOp(*this); }];
let verifier = [{ return verifyConvOp(*this); }];
}
//===----------------------------------------------------------------------===//
@ -165,7 +165,7 @@ def Tosa_DepthwiseConv2DOp : Tosa_Op<"depthwise_conv2d", [NoSideEffect]> {
let builders = [Tosa_ConvOpQuantInfoBuilder];
let verifier = [{ return ::verifyConvOp(*this); }];
let verifier = [{ return verifyConvOp(*this); }];
}
//===----------------------------------------------------------------------===//
@ -191,7 +191,7 @@ def Tosa_FullyConnectedOp : Tosa_Op<"fully_connected", [NoSideEffect]> {
let builders = [Tosa_FCOpQuantInfoBuilder];
let verifier = [{ return ::verifyConvOp(*this); }];
let verifier = [{ return verifyConvOp(*this); }];
}
//===----------------------------------------------------------------------===//

View File

@ -16,7 +16,6 @@
#include "mlir/Pass/Pass.h"
namespace mlir {
namespace tosa {
std::unique_ptr<Pass> createTosaMakeBroadcastablePass();

View File

@ -19,8 +19,8 @@
#include "mlir/Dialect/Quant/FakeQuantSupport.h"
#include "mlir/Dialect/Quant/UniformSupport.h"
using namespace mlir;
using namespace mlir::tosa;
namespace mlir {
namespace tosa {
//===----------------------------------------------------------------------===//
// Utililty functions to support quantization handling in Tosa.
@ -65,4 +65,7 @@ TypeAttr buildQTypeAttrFromMinMax(OpBuilder builder, Type inputDType,
IntegerAttr quantBits, int filterQuantDim,
bool isSigned, BoolAttr narrowRange);
} // namespace tosa
} // namespace mlir
#endif // DIALECT_TOSA_UTILS_QUANT_UTILS_H

View File

@ -93,7 +93,8 @@ void TosaDialect::initialize() {
// TOSA Operator Verifiers.
//===----------------------------------------------------------------------===//
template <typename T> static LogicalResult verifyConvOp(T op) {
template <typename T>
static LogicalResult verifyConvOp(T op) {
// All TOSA conv ops have an input() and weight().
auto inputType = op.input().getType().template dyn_cast<RankedTensorType>();
auto weightType = op.weight().getType().template dyn_cast<RankedTensorType>();
@ -127,10 +128,10 @@ template <typename T> static LogicalResult verifyConvOp(T op) {
/// This builder is called on all convolution operators except TransposeConv,
/// which has specialized output shape semantics. The builder also defines the
/// bitwidth of the output given the bit width of the input & weight content.
void buildConvOpWithQuantInfo(OpBuilder &builder, OperationState &result,
Type outputType, Value input, Value weight,
Value bias, ArrayAttr pad, ArrayAttr stride,
ArrayAttr dilation) {
static void buildConvOpWithQuantInfo(OpBuilder &builder, OperationState &result,
Type outputType, Value input, Value weight,
Value bias, ArrayAttr pad,
ArrayAttr stride, ArrayAttr dilation) {
result.addOperands({input, weight, bias});
result.addAttribute("pad", pad);
@ -148,11 +149,11 @@ void buildConvOpWithQuantInfo(OpBuilder &builder, OperationState &result,
}
/// Handles tosa.transpose_conv2d which has outpad and output shape attributes.
void buildTransConvOpWithQuantInfo(OpBuilder &builder, OperationState &result,
Type outputType, Value input, Value weight,
Value bias, ArrayAttr outpad,
ArrayAttr stride, ArrayAttr dilation,
ArrayAttr outputShape) {
static void
buildTransConvOpWithQuantInfo(OpBuilder &builder, OperationState &result,
Type outputType, Value input, Value weight,
Value bias, ArrayAttr outpad, ArrayAttr stride,
ArrayAttr dilation, ArrayAttr outputShape) {
result.addOperands({input, weight, bias});
result.addAttribute("out_pad", outpad);
result.addAttribute("stride", stride);
@ -171,9 +172,9 @@ void buildTransConvOpWithQuantInfo(OpBuilder &builder, OperationState &result,
/// The tosa.fully_connected op has its own builder as it does not have
/// strides/dilation/padding.
void buildFCOpWithQuantInfo(OpBuilder &builder, OperationState &result,
Type outputType, Value input, Value weight,
Value bias) {
static void buildFCOpWithQuantInfo(OpBuilder &builder, OperationState &result,
Type outputType, Value input, Value weight,
Value bias) {
result.addOperands({input, weight, bias});
auto quantAttr = ::buildConvOpQuantizationAttr(builder, input, weight);
@ -190,8 +191,9 @@ void buildFCOpWithQuantInfo(OpBuilder &builder, OperationState &result,
/// op must be constructed where the weight is not a constant. In this case,
/// the fully_connected op must be expressed using matmul.
/// TODO: Add link to the leglization document explaining this.
void buildMatMulOpWithQuantInfo(OpBuilder &builder, OperationState &result,
Type outputType, Value a, Value b) {
static void buildMatMulOpWithQuantInfo(OpBuilder &builder,
OperationState &result, Type outputType,
Value a, Value b) {
result.addOperands({a, b});
auto quantAttr = ::buildMatMulOpQuantizationAttr(builder, a, b);
@ -227,10 +229,11 @@ void buildMatMulOpWithQuantInfo(OpBuilder &builder, OperationState &result,
/// Both the tosa.avg_pool2d and unary ops use the same UnaruOpQuantizationAttr
/// but avg_pool operator has its own builder as it has additional parameters
/// not part of the unary ops.
void buildAvgPool2dOpWithQuantInfo(OpBuilder &builder, OperationState &result,
Type outputType, Value input,
ArrayAttr kernel, ArrayAttr stride,
ArrayAttr pad) {
static void buildAvgPool2dOpWithQuantInfo(OpBuilder &builder,
OperationState &result,
Type outputType, Value input,
ArrayAttr kernel, ArrayAttr stride,
ArrayAttr pad) {
result.addOperands(input);
result.addAttribute("kernel", kernel);
result.addAttribute("stride", stride);
@ -244,8 +247,9 @@ void buildAvgPool2dOpWithQuantInfo(OpBuilder &builder, OperationState &result,
/// This builder is called on single-parameter unary operators that have scale
/// relationship between their input and output, expressed by the
/// UnaryOpQuantizationAttr.
void buildUnaryOpWithQuantInfo(OpBuilder &builder, OperationState &result,
Type outputType, Value input) {
static void buildUnaryOpWithQuantInfo(OpBuilder &builder,
OperationState &result, Type outputType,
Value input) {
result.addOperands(input);
auto quantAttr = buildUnaryOpQuantizationAttr(builder, input, outputType);
if (quantAttr)
@ -256,8 +260,9 @@ void buildUnaryOpWithQuantInfo(OpBuilder &builder, OperationState &result,
/// This builder is called on TOSA pad operator that needs to create its own
/// OptionalAttr quantization_attr parameter to scale the padding values
/// correctly.
void buildPadOpWithQuantInfo(OpBuilder &builder, OperationState &result,
Type outputType, Value input, Value paddings) {
static void buildPadOpWithQuantInfo(OpBuilder &builder, OperationState &result,
Type outputType, Value input,
Value paddings) {
result.addOperands({input, paddings});
auto quantAttr = buildPadOpQuantizationAttr(builder, input);
if (quantAttr)

View File

@ -128,8 +128,6 @@ static int reshapeLowerToHigher(PatternRewriter &rewriter, Location loc,
}
ArrayRef<int64_t> outputRankShape = outputType.getShape();
ArrayRef<int64_t> higherRankShape =
higherTensorValue.getType().cast<RankedTensorType>().getShape();
ArrayRef<int64_t> lowerRankShape =
lowerTensorValue.getType().cast<RankedTensorType>().getShape();

View File

@ -19,8 +19,9 @@ using namespace mlir::tosa;
/// From a scale value, generates multiplier and shift values where
/// mantissa is in [-1.0,-0.5] or [0.5, 1.0] such that
/// multiplier = mantissa*2^shift for 16-bit scaling.
void computeMultiplierAndShiftTosaScale16(double scale, int32_t &multiplier,
int32_t &shift) {
static void computeMultiplierAndShiftTosaScale16(double scale,
int32_t &multiplier,
int32_t &shift) {
const double mantissa = std::frexp(scale, &shift);
auto shiftedM = std::round(mantissa * (int64_t(1) << 15));
@ -47,8 +48,9 @@ void computeMultiplierAndShiftTosaScale16(double scale, int32_t &multiplier,
/// From a scale value, generates multiplier and shift values where
/// mantissa is in [-1.0,-0.5] or [0.5, 1.0] such that
/// multiplier = mantissa*2^shift for 32-bit scaling.
void computeMultiplierAndShiftTosaScale32(double scale, int32_t &multiplier,
int32_t &shift) {
static void computeMultiplierAndShiftTosaScale32(double scale,
int32_t &multiplier,
int32_t &shift) {
const double mantissa = std::frexp(scale, &shift);
auto shiftedM = std::round(mantissa * (int64_t(1) << 31));
@ -72,8 +74,8 @@ void computeMultiplierAndShiftTosaScale32(double scale, int32_t &multiplier,
}
/// Generates a quantized multiplier/shift from double.
void computeMultiplierAndShift(double scale, int32_t &multiplier,
int32_t &shift, int32_t scaleWidth) {
void mlir::tosa::computeMultiplierAndShift(double scale, int32_t &multiplier,
int32_t &shift, int32_t scaleWidth) {
switch (scaleWidth) {
case 16:
@ -96,8 +98,9 @@ void computeMultiplierAndShift(double scale, int32_t &multiplier,
/// ConvOpQuantInfoBuilder/TransConvOpQuantInfoBuilder:
/// input_zp: input zeropoint
/// weight_zp: weight zeropoint.
ConvOpQuantizationAttr buildConvOpQuantizationAttr(OpBuilder &builder,
Value input, Value weight) {
ConvOpQuantizationAttr
mlir::tosa::buildConvOpQuantizationAttr(OpBuilder &builder, Value input,
Value weight) {
auto inputType = input.getType().dyn_cast<RankedTensorType>();
auto weightType = weight.getType().dyn_cast<RankedTensorType>();
@ -144,8 +147,9 @@ ConvOpQuantizationAttr buildConvOpQuantizationAttr(OpBuilder &builder,
/// MatMulOpQuantInfoBuilder:
/// aZp: input a zeropoint
/// bZp: input b zeropoint.
MatMulOpQuantizationAttr buildMatMulOpQuantizationAttr(OpBuilder &builder,
Value a, Value b) {
MatMulOpQuantizationAttr
mlir::tosa::buildMatMulOpQuantizationAttr(OpBuilder &builder, Value a,
Value b) {
auto aType = a.getType().dyn_cast<RankedTensorType>();
auto bType = b.getType().dyn_cast<RankedTensorType>();
@ -179,9 +183,9 @@ MatMulOpQuantizationAttr buildMatMulOpQuantizationAttr(OpBuilder &builder,
/// UnaryOpQuantInfoBuilder:
/// inputZp: input zeropoint
/// outputZp: output zeropoint.
UnaryOpQuantizationAttr buildUnaryOpQuantizationAttr(OpBuilder &builder,
Value input,
Type outputRawType) {
UnaryOpQuantizationAttr
mlir::tosa::buildUnaryOpQuantizationAttr(OpBuilder &builder, Value input,
Type outputRawType) {
auto inputType = input.getType().dyn_cast<RankedTensorType>();
auto outputType = outputRawType.dyn_cast<RankedTensorType>();
@ -213,8 +217,8 @@ UnaryOpQuantizationAttr buildUnaryOpQuantizationAttr(OpBuilder &builder,
/// Builds PadOpQuantizationAttr, called from PadOpQuantInfoBuilder:
/// inputZp: input zeropoint.
PadOpQuantizationAttr buildPadOpQuantizationAttr(OpBuilder &builder,
Value input) {
PadOpQuantizationAttr mlir::tosa::buildPadOpQuantizationAttr(OpBuilder &builder,
Value input) {
auto inputType = input.getType().dyn_cast<RankedTensorType>();
@ -238,8 +242,8 @@ PadOpQuantizationAttr buildPadOpQuantizationAttr(OpBuilder &builder,
/// Builds output type for a quantized ConvOp with the right bitwidth.
/// This is called by the builder when dealing with quantized content.
Type buildConvOpResultTypeInfo(OpBuilder &builder, Type outputType, Value input,
Value weight) {
Type mlir::tosa::buildConvOpResultTypeInfo(OpBuilder &builder, Type outputType,
Value input, Value weight) {
auto inputType = input.getType().dyn_cast<RankedTensorType>();
auto weightType = weight.getType().dyn_cast<RankedTensorType>();
@ -272,10 +276,10 @@ Type buildConvOpResultTypeInfo(OpBuilder &builder, Type outputType, Value input,
}
/// Builds Tosa quantization attributes from min/max values.
Type buildQTypeFromMinMax(OpBuilder builder, Type inputDType, Attribute minAttr,
Attribute maxAttr, IntegerAttr quantBits,
int filterQuantDim, bool isSigned,
BoolAttr narrowRange) {
Type mlir::tosa::buildQTypeFromMinMax(OpBuilder builder, Type inputDType,
Attribute minAttr, Attribute maxAttr,
IntegerAttr quantBits, int filterQuantDim,
bool isSigned, BoolAttr narrowRange) {
quant::QuantizedType retType;
@ -339,10 +343,11 @@ Type buildQTypeFromMinMax(OpBuilder builder, Type inputDType, Attribute minAttr,
}
/// Builds Tosa quantization attributes from min/max values.
TypeAttr buildQTypeAttrFromMinMax(OpBuilder builder, Type inputDtype,
Attribute minAttr, Attribute maxAttr,
IntegerAttr quantBits, int filterQuantDim,
bool isSigned, BoolAttr narrowRange) {
TypeAttr
mlir::tosa::buildQTypeAttrFromMinMax(OpBuilder builder, Type inputDtype,
Attribute minAttr, Attribute maxAttr,
IntegerAttr quantBits, int filterQuantDim,
bool isSigned, BoolAttr narrowRange) {
return TypeAttr::get(buildQTypeFromMinMax(builder, inputDtype, minAttr,
maxAttr, quantBits, filterQuantDim,