* Moved various loose functions to either the mlir::tosa namespace or made static
* Fixed an unused variable warning in TosaMakeBroadcastable.cpp.
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.
}];
}
(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.
"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
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
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
(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
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
def Tosa_PadOpQuantInfoBuilder : OpBuilderDAG<
(ins "Type":$outputType, "Value":$input, "Value":$paddings),
[{
- ::buildPadOpWithQuantInfo($_builder, $_state, outputType,
- input, paddings);
+ buildPadOpWithQuantInfo($_builder, $_state, outputType,
+ input, paddings);
}]>;
//===----------------------------------------------------------------------===//
let builders = [Tosa_ConvOpQuantInfoBuilder];
- let verifier = [{ return ::verifyConvOp(*this); }];
+ let verifier = [{ return verifyConvOp(*this); }];
}
//===----------------------------------------------------------------------===//
let builders = [Tosa_ConvOpQuantInfoBuilder];
- let verifier = [{ return ::verifyConvOp(*this); }];
+ let verifier = [{ return verifyConvOp(*this); }];
}
//===----------------------------------------------------------------------===//
let builders = [Tosa_ConvOpQuantInfoBuilder];
- let verifier = [{ return ::verifyConvOp(*this); }];
+ let verifier = [{ return verifyConvOp(*this); }];
}
//===----------------------------------------------------------------------===//
let builders = [Tosa_FCOpQuantInfoBuilder];
- let verifier = [{ return ::verifyConvOp(*this); }];
+ let verifier = [{ return verifyConvOp(*this); }];
}
//===----------------------------------------------------------------------===//
#include "mlir/Pass/Pass.h"
namespace mlir {
-
namespace tosa {
std::unique_ptr<Pass> createTosaMakeBroadcastablePass();
#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.
IntegerAttr quantBits, int filterQuantDim,
bool isSigned, BoolAttr narrowRange);
+} // namespace tosa
+} // namespace mlir
+
#endif // DIALECT_TOSA_UTILS_QUANT_UTILS_H
// 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>();
/// 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);
}
/// 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);
/// 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);
/// 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);
/// 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);
/// 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)
/// 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)
}
ArrayRef<int64_t> outputRankShape = outputType.getShape();
- ArrayRef<int64_t> higherRankShape =
- higherTensorValue.getType().cast<RankedTensorType>().getShape();
ArrayRef<int64_t> lowerRankShape =
lowerTensorValue.getType().cast<RankedTensorType>().getShape();
/// 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));
/// 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));
}
/// 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:
/// 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>();
/// 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>();
/// 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>();
/// 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>();
/// 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>();
}
/// 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;
}
/// 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,