2021-10-08 10:07:03 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
// Also available under a BSD-style license. See LICENSE.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "torch-mlir/Conversion/TorchToTosa/TorchToTosa.h"
|
2021-12-03 08:52:01 +08:00
|
|
|
#include "torch-mlir/Conversion/TorchToTosa/TosaLegalizeCommon.h"
|
|
|
|
#include "torch-mlir/Conversion/TorchToTosa/TosaLegalizeUtils.h"
|
2021-10-08 10:07:03 +08:00
|
|
|
|
|
|
|
#include "../PassDetail.h"
|
2021-12-16 03:01:01 +08:00
|
|
|
#include "mlir/Dialect/Arithmetic/IR/Arithmetic.h"
|
|
|
|
#include "mlir/Dialect/Tensor/IR/Tensor.h"
|
2021-10-08 10:07:03 +08:00
|
|
|
#include "mlir/Dialect/Tosa/IR/TosaOps.h"
|
|
|
|
#include "mlir/Dialect/Traits.h"
|
|
|
|
#include "mlir/IR/Matchers.h"
|
|
|
|
#include "mlir/Transforms/DialectConversion.h"
|
|
|
|
#include "torch-mlir/Dialect/Torch/IR/TorchOps.h"
|
|
|
|
#include "torch-mlir/Dialect/TorchConversion/IR/TorchConversionDialect.h"
|
|
|
|
#include "torch-mlir/Dialect/TorchConversion/Transforms/BackendTypeConversion.h"
|
|
|
|
|
|
|
|
using namespace mlir;
|
|
|
|
using namespace mlir::torch;
|
|
|
|
using namespace mlir::torch::Torch;
|
|
|
|
|
|
|
|
namespace {
|
2021-11-12 08:15:58 +08:00
|
|
|
|
|
|
|
// These legalizations are for unary ops with only for floating point datatypes.
|
2021-11-11 11:03:36 +08:00
|
|
|
// There is no supported quantized integer mode for these.
|
2021-11-12 08:15:58 +08:00
|
|
|
template <typename AtenOpT, typename TosaOpT>
|
|
|
|
class ConvertAtenUnaryFPOnlyOp : public OpConversionPattern<AtenOpT> {
|
|
|
|
public:
|
|
|
|
using OpConversionPattern<AtenOpT>::OpConversionPattern;
|
2021-11-16 07:00:53 +08:00
|
|
|
using OpAdaptor = typename AtenOpT::Adaptor;
|
2021-11-12 08:15:58 +08:00
|
|
|
LogicalResult
|
2021-11-16 07:00:53 +08:00
|
|
|
matchAndRewrite(AtenOpT op, OpAdaptor adaptor,
|
2021-11-12 08:15:58 +08:00
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
|
|
|
Value self = adaptor.self();
|
|
|
|
auto selfTy = self.getType().cast<TensorType>();
|
|
|
|
|
|
|
|
if (!selfTy)
|
|
|
|
return op.emitError("Only Tensor types supported in TOSA");
|
|
|
|
|
|
|
|
if (selfTy.getElementType().isa<mlir::FloatType>()) {
|
|
|
|
rewriter.replaceOpWithNewOp<TosaOpT>(
|
|
|
|
op,
|
|
|
|
OpConversionPattern<AtenOpT>::getTypeConverter()->convertType(
|
|
|
|
op.getType()),
|
|
|
|
self);
|
|
|
|
return success();
|
|
|
|
} else {
|
|
|
|
return op.emitError(
|
|
|
|
"Only floating-point datatype legalization supported");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// These unary op legalizations are identical for floating-point
|
|
|
|
// or quantized types
|
|
|
|
template <typename AtenOpT, typename TosaOpT>
|
|
|
|
class ConvertAtenUnaryOp : public OpConversionPattern<AtenOpT> {
|
|
|
|
public:
|
|
|
|
using OpConversionPattern<AtenOpT>::OpConversionPattern;
|
2021-11-16 07:00:53 +08:00
|
|
|
using OpAdaptor = typename AtenOpT::Adaptor;
|
2021-11-12 08:15:58 +08:00
|
|
|
LogicalResult
|
2021-11-16 07:00:53 +08:00
|
|
|
matchAndRewrite(AtenOpT op, OpAdaptor adaptor,
|
2021-11-12 08:15:58 +08:00
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
|
|
|
rewriter.replaceOpWithNewOp<TosaOpT>(
|
|
|
|
op,
|
|
|
|
OpConversionPattern<AtenOpT>::getTypeConverter()->convertType(
|
|
|
|
op.getType()),
|
|
|
|
adaptor.self());
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-12-16 03:19:25 +08:00
|
|
|
// These binary op legalizations are identical for floating-point
|
|
|
|
// or quantized types
|
|
|
|
template <typename AtenOpT, typename TosaOpT>
|
|
|
|
class ConvertAtenBinaryOp : public OpConversionPattern<AtenOpT> {
|
|
|
|
public:
|
|
|
|
using OpConversionPattern<AtenOpT>::OpConversionPattern;
|
|
|
|
using OpAdaptor = typename AtenOpT::Adaptor;
|
|
|
|
LogicalResult
|
|
|
|
matchAndRewrite(AtenOpT op, OpAdaptor adaptor,
|
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
|
|
|
Value lhs = adaptor.self();
|
|
|
|
auto lhsTy = lhs.getType().cast<TensorType>();
|
|
|
|
Value rhs = adaptor.other();
|
|
|
|
auto rhsTy = rhs.getType().cast<TensorType>();
|
|
|
|
|
|
|
|
if (!lhsTy || !rhsTy)
|
|
|
|
return op.emitError("Only Tensor types supported in TOSA");
|
|
|
|
|
|
|
|
auto lhsElemTy = lhsTy.getElementType();
|
|
|
|
auto rhsElemTy = rhsTy.getElementType();
|
|
|
|
|
|
|
|
if (lhsElemTy != rhsElemTy)
|
|
|
|
return op.emitError("Add: input datatypes mismatched");
|
|
|
|
|
|
|
|
rewriter.replaceOpWithNewOp<TosaOpT>(
|
|
|
|
op,
|
|
|
|
OpConversionPattern<AtenOpT>::getTypeConverter()->convertType(
|
|
|
|
op.getType()),
|
|
|
|
lhs, rhs);
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-11-12 08:15:58 +08:00
|
|
|
// These binary op legalizations are specific to add/sub which have an
|
|
|
|
// alpha multiplier.
|
|
|
|
template <typename AtenOpT, typename TosaOpT>
|
|
|
|
class ConvertAtenAddSubOp : public OpConversionPattern<AtenOpT> {
|
|
|
|
public:
|
|
|
|
using OpConversionPattern<AtenOpT>::OpConversionPattern;
|
2021-11-16 07:00:53 +08:00
|
|
|
using OpAdaptor = typename AtenOpT::Adaptor;
|
|
|
|
LogicalResult matchAndRewrite(AtenOpT op, OpAdaptor adaptor,
|
2021-11-12 08:15:58 +08:00
|
|
|
ConversionPatternRewriter &rewriter) const {
|
|
|
|
Value lhs = adaptor.self();
|
|
|
|
auto lhsTy = lhs.getType().cast<TensorType>();
|
|
|
|
Value rhs = adaptor.other();
|
|
|
|
auto rhsTy = rhs.getType().cast<TensorType>();
|
|
|
|
|
|
|
|
if (!lhsTy || !rhsTy)
|
|
|
|
return op.emitError("Only Tensor types supported in TOSA");
|
|
|
|
|
|
|
|
auto lhsElemTy = lhsTy.getElementType();
|
|
|
|
auto rhsElemTy = rhsTy.getElementType();
|
|
|
|
|
|
|
|
if (lhsElemTy != rhsElemTy)
|
|
|
|
return op.emitError("Add: input datatypes mismatched");
|
|
|
|
|
|
|
|
// FIXME: Handle alpha.
|
|
|
|
// Needs extraction of floating point constant.
|
|
|
|
|
|
|
|
if (lhsElemTy.isa<mlir::FloatType>()) {
|
|
|
|
rewriter.replaceOpWithNewOp<TosaOpT>(
|
|
|
|
op,
|
|
|
|
OpConversionPattern<AtenOpT>::getTypeConverter()->convertType(
|
|
|
|
op.getType()),
|
|
|
|
lhs, rhs);
|
|
|
|
return success();
|
|
|
|
} else {
|
|
|
|
return op.emitError(
|
|
|
|
"Only floating-point datatype legalization supported");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// This defines a template to construct ops whose legalizations are
|
|
|
|
// specialized.
|
|
|
|
template <typename AtenOpT>
|
|
|
|
class ConvertAtenOp : public OpConversionPattern<AtenOpT> {
|
|
|
|
public:
|
|
|
|
using OpConversionPattern<AtenOpT>::OpConversionPattern;
|
2021-11-16 07:00:53 +08:00
|
|
|
using OpAdaptor = typename AtenOpT::Adaptor;
|
2021-11-12 08:15:58 +08:00
|
|
|
LogicalResult
|
2021-11-16 07:00:53 +08:00
|
|
|
matchAndRewrite(AtenOpT op, OpAdaptor adaptor,
|
2021-11-12 08:15:58 +08:00
|
|
|
ConversionPatternRewriter &rewriter) const override;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
LogicalResult ConvertAtenOp<AtenTanhOp>::matchAndRewrite(
|
2021-11-16 07:00:53 +08:00
|
|
|
AtenTanhOp op, OpAdaptor adaptor,
|
2021-11-12 08:15:58 +08:00
|
|
|
ConversionPatternRewriter &rewriter) const {
|
2021-11-11 11:03:36 +08:00
|
|
|
Value self = adaptor.self();
|
|
|
|
auto selfTy = self.getType().cast<TensorType>();
|
|
|
|
if (selfTy && selfTy.getElementType().isa<mlir::FloatType>()) {
|
|
|
|
rewriter.replaceOpWithNewOp<tosa::TanhOp>(
|
|
|
|
op, getTypeConverter()->convertType(op.getType()), self);
|
|
|
|
return success();
|
|
|
|
} else {
|
|
|
|
// Sigmoid legalization in TOSA for quantized element-type uses
|
|
|
|
// specialized tosa.table construct.
|
2021-11-12 08:15:58 +08:00
|
|
|
return op.emitError(
|
|
|
|
"Only floating-point datatype legalization currently supported");
|
2021-11-11 11:03:36 +08:00
|
|
|
}
|
2021-10-29 01:09:12 +08:00
|
|
|
}
|
|
|
|
|
2021-11-12 08:15:58 +08:00
|
|
|
template <>
|
|
|
|
LogicalResult ConvertAtenOp<AtenSigmoidOp>::matchAndRewrite(
|
2021-11-16 07:00:53 +08:00
|
|
|
AtenSigmoidOp op, OpAdaptor adaptor,
|
2021-11-11 11:03:36 +08:00
|
|
|
ConversionPatternRewriter &rewriter) const {
|
|
|
|
Value self = adaptor.self();
|
|
|
|
auto selfTy = self.getType().cast<TensorType>();
|
|
|
|
if (selfTy && selfTy.getElementType().isa<mlir::FloatType>()) {
|
|
|
|
rewriter.replaceOpWithNewOp<tosa::SigmoidOp>(
|
|
|
|
op, getTypeConverter()->convertType(op.getType()), self);
|
|
|
|
return success();
|
|
|
|
} else {
|
|
|
|
// Sigmoid legalization in TOSA for quantized element-type uses
|
|
|
|
// specialized tosa.table construct.
|
2021-11-12 08:15:58 +08:00
|
|
|
return op.emitError(
|
|
|
|
"Only floating-point datatype legalization currently supported");
|
2021-11-11 11:03:36 +08:00
|
|
|
}
|
2021-11-12 08:15:58 +08:00
|
|
|
}
|
2021-11-11 11:03:36 +08:00
|
|
|
|
2021-11-12 08:15:58 +08:00
|
|
|
template <>
|
|
|
|
LogicalResult ConvertAtenOp<AtenReluOp>::matchAndRewrite(
|
2021-11-16 07:00:53 +08:00
|
|
|
AtenReluOp op, OpAdaptor adaptor,
|
2021-11-12 08:15:58 +08:00
|
|
|
ConversionPatternRewriter &rewriter) const {
|
2021-11-11 11:03:36 +08:00
|
|
|
Value self = adaptor.self();
|
|
|
|
auto selfTy = self.getType().cast<TensorType>();
|
|
|
|
|
|
|
|
// Maps to tosa.clamp which has both int and fp limits.
|
|
|
|
int64_t clampMin = 0;
|
|
|
|
Value clampIn = self;
|
|
|
|
if (selfTy) {
|
|
|
|
// Rescale the clampIn for quantized types. TBD
|
|
|
|
if (!selfTy.getElementType().isa<mlir::FloatType>()) {
|
2021-11-12 08:15:58 +08:00
|
|
|
return op.emitError(
|
|
|
|
"Only floating-point datatype legalization currently supported");
|
2021-11-11 11:03:36 +08:00
|
|
|
}
|
|
|
|
rewriter.replaceOpWithNewOp<tosa::ClampOp>(
|
|
|
|
op, getTypeConverter()->convertType(op.getType()), clampIn,
|
|
|
|
rewriter.getI64IntegerAttr(clampMin),
|
|
|
|
rewriter.getI64IntegerAttr(std::numeric_limits<int32_t>::max()),
|
|
|
|
rewriter.getF32FloatAttr(0.0f),
|
|
|
|
rewriter.getF32FloatAttr(std::numeric_limits<float>::max()));
|
|
|
|
return success();
|
|
|
|
} else {
|
|
|
|
return op.emitError("Only Tensor types supported in TOSA");
|
|
|
|
}
|
2021-10-29 01:09:12 +08:00
|
|
|
}
|
|
|
|
|
2021-11-12 08:15:58 +08:00
|
|
|
template <>
|
|
|
|
LogicalResult ConvertAtenOp<AtenMulTensorOp>::matchAndRewrite(
|
2021-11-16 07:00:53 +08:00
|
|
|
AtenMulTensorOp op, OpAdaptor adaptor,
|
2021-11-12 08:15:58 +08:00
|
|
|
ConversionPatternRewriter &rewriter) const {
|
|
|
|
|
|
|
|
Value lhs = adaptor.self();
|
|
|
|
auto lhsTy = lhs.getType().cast<TensorType>();
|
|
|
|
Value rhs = adaptor.other();
|
|
|
|
auto rhsTy = rhs.getType().cast<TensorType>();
|
|
|
|
|
|
|
|
if (!lhsTy || !rhsTy)
|
|
|
|
return op.emitError("Only Tensor types supported in TOSA");
|
|
|
|
|
|
|
|
auto lhsElemTy = lhsTy.getElementType();
|
|
|
|
auto rhsElemTy = rhsTy.getElementType();
|
|
|
|
|
|
|
|
if (lhsElemTy != rhsElemTy)
|
|
|
|
return op.emitError("Add: input datatypes mismatched");
|
|
|
|
|
|
|
|
if (lhsElemTy.isa<mlir::FloatType>()) {
|
|
|
|
rewriter.replaceOpWithNewOp<tosa::MulOp>(
|
|
|
|
op, getTypeConverter()->convertType(op.getType()), lhs, rhs,
|
|
|
|
/*shift=*/0);
|
|
|
|
return success();
|
|
|
|
} else {
|
|
|
|
// Quantized multiplication may need to rescale inputs.
|
|
|
|
return op.emitError(
|
|
|
|
"Only floating-point datatype legalization currently supported");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
LogicalResult ConvertAtenOp<AtenDivTensorOp>::matchAndRewrite(
|
2021-11-16 07:00:53 +08:00
|
|
|
AtenDivTensorOp op, OpAdaptor adaptor,
|
2021-11-12 08:15:58 +08:00
|
|
|
ConversionPatternRewriter &rewriter) const {
|
|
|
|
|
|
|
|
Value lhs = adaptor.self();
|
|
|
|
auto lhsTy = lhs.getType().cast<TensorType>();
|
|
|
|
Value rhs = adaptor.other();
|
|
|
|
auto rhsTy = rhs.getType().cast<TensorType>();
|
|
|
|
|
|
|
|
if (!lhsTy || !rhsTy)
|
|
|
|
return op.emitError("Only Tensor types supported in TOSA");
|
|
|
|
|
|
|
|
auto lhsElemTy = lhsTy.getElementType();
|
|
|
|
auto rhsElemTy = rhsTy.getElementType();
|
|
|
|
|
|
|
|
if (lhsElemTy != rhsElemTy)
|
|
|
|
return op.emitError("Add: input datatypes mismatched");
|
|
|
|
|
|
|
|
if (lhsElemTy.isa<mlir::FloatType>()) {
|
|
|
|
auto rcpOp = rewriter.create<tosa::ReciprocalOp>(
|
|
|
|
op->getLoc(), getTypeConverter()->convertType(op.getType()), rhs);
|
|
|
|
rewriter.replaceOpWithNewOp<tosa::MulOp>(
|
|
|
|
op, getTypeConverter()->convertType(op.getType()), lhs,
|
|
|
|
rcpOp.getResult(), /*shift=*/0);
|
|
|
|
} else {
|
|
|
|
rewriter.replaceOpWithNewOp<tosa::DivOp>(
|
|
|
|
op, getTypeConverter()->convertType(op.getType()), lhs, rhs);
|
|
|
|
}
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2021-12-03 08:52:01 +08:00
|
|
|
using ReductionConvFunc = llvm::Optional<Value> (*)(PatternRewriter &,
|
|
|
|
Operation *,
|
|
|
|
RankedTensorType, Value,
|
|
|
|
ElementsAttr, bool);
|
|
|
|
|
|
|
|
// They all constitute a common form invoking the appropriate
|
|
|
|
// converion function in TosaLegalizeCommon.cpp
|
|
|
|
template <typename AtenOpT, ReductionConvFunc ConversionFuncT>
|
|
|
|
class ConvertAtenReductionOp : public OpConversionPattern<AtenOpT> {
|
|
|
|
public:
|
|
|
|
using OpConversionPattern<AtenOpT>::OpConversionPattern;
|
|
|
|
using OpAdaptor = typename AtenOpT::Adaptor;
|
|
|
|
|
|
|
|
// Each variant must implement corresponding parameter parsing options
|
|
|
|
virtual LogicalResult readReduceDimsAndKeepDims(
|
|
|
|
AtenOpT op, OpAdaptor adaptor, ConversionPatternRewriter &rewriter,
|
|
|
|
ElementsAttr &reduceDimsAttr, bool &keepDims) const {
|
|
|
|
return rewriter.notifyMatchFailure(
|
|
|
|
op, "Unimplemented reduce_dims and keep_dims parsing function");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Common rewriter for all reduction ops, calls the specific implementation of
|
|
|
|
// readReduceDimsAndKeepDims() needed for the op variant.
|
|
|
|
LogicalResult matchAndRewrite(AtenOpT op, OpAdaptor adaptor,
|
|
|
|
ConversionPatternRewriter &rewriter) const {
|
|
|
|
Value self = adaptor.self();
|
|
|
|
auto selfTy = self.getType().cast<TensorType>();
|
|
|
|
|
|
|
|
if (!selfTy)
|
|
|
|
return op.emitError("Only Tensor types supported in TOSA");
|
|
|
|
|
|
|
|
auto outputTy = OpConversionPattern<AtenOpT>::getTypeConverter()
|
|
|
|
->convertType(op.getType())
|
|
|
|
.template cast<RankedTensorType>();
|
|
|
|
if (!outputTy)
|
|
|
|
return op.emitError(
|
|
|
|
"Only ranked tensor type outputs permitted for reduce_mean");
|
|
|
|
|
|
|
|
ElementsAttr reduceDimsAttr;
|
|
|
|
bool keepDims;
|
|
|
|
|
|
|
|
if (failed(readReduceDimsAndKeepDims(op, adaptor, rewriter, reduceDimsAttr,
|
|
|
|
keepDims)))
|
|
|
|
return failure();
|
|
|
|
|
|
|
|
llvm::Optional<Value> result =
|
|
|
|
ConversionFuncT(rewriter, op, outputTy, self, reduceDimsAttr, keepDims);
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
return failure();
|
|
|
|
|
|
|
|
// TBD - support dtype casting.
|
|
|
|
|
|
|
|
rewriter.replaceOp(op, {result.getValue()});
|
|
|
|
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// This reduction op legalization template handles op variants that have
|
|
|
|
// explicit reduce_dims dimensions (provided as a list) and keep_dims
|
|
|
|
// parameters.
|
|
|
|
template <typename AtenOpT, ReductionConvFunc ConversionFuncT>
|
|
|
|
class ConvertAtenMultipleDimsReductionOp
|
|
|
|
: public ConvertAtenReductionOp<AtenOpT, ConversionFuncT> {
|
|
|
|
using ConvertAtenReductionOp<AtenOpT,
|
|
|
|
ConversionFuncT>::ConvertAtenReductionOp;
|
|
|
|
using OpAdaptor = typename AtenOpT::Adaptor;
|
|
|
|
LogicalResult readReduceDimsAndKeepDims(AtenOpT op, OpAdaptor adaptor,
|
|
|
|
ConversionPatternRewriter &rewriter,
|
|
|
|
ElementsAttr &reduceDimsAttr,
|
|
|
|
bool &keepDims) const {
|
|
|
|
SmallVector<int64_t, 4> reduceDims;
|
|
|
|
if (!matchPattern(op.dim(), m_TorchConstantIntList(reduceDims)))
|
|
|
|
return rewriter.notifyMatchFailure(op,
|
|
|
|
"non-const dim parameter unsupported");
|
|
|
|
int64_t N = reduceDims.size();
|
|
|
|
auto reduceDimsType = RankedTensorType::get({N}, rewriter.getI64Type());
|
|
|
|
reduceDimsAttr = DenseIntElementsAttr::get(reduceDimsType,
|
|
|
|
llvm::makeArrayRef(reduceDims));
|
|
|
|
|
|
|
|
keepDims = false;
|
|
|
|
if (!matchPattern(op.keepdim(), m_TorchConstantBool(&keepDims)))
|
|
|
|
return rewriter.notifyMatchFailure(
|
|
|
|
op, "non-const keepdim parameter unsupported");
|
|
|
|
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// This reduction op legalization template handles op variants that reduce in
|
|
|
|
// only one explicit dim which is provided as a number (rather than a list), and
|
|
|
|
// a keep_dims parameter.
|
|
|
|
template <typename AtenOpT, ReductionConvFunc ConversionFuncT>
|
|
|
|
class ConvertAtenOneDimReductionOp
|
|
|
|
: public ConvertAtenReductionOp<AtenOpT, ConversionFuncT> {
|
|
|
|
using ConvertAtenReductionOp<AtenOpT,
|
|
|
|
ConversionFuncT>::ConvertAtenReductionOp;
|
|
|
|
using OpAdaptor = typename AtenOpT::Adaptor;
|
|
|
|
LogicalResult readReduceDimsAndKeepDims(AtenOpT op, OpAdaptor adaptor,
|
|
|
|
ConversionPatternRewriter &rewriter,
|
|
|
|
ElementsAttr &reduceDimsAttr,
|
|
|
|
bool &keepDims) const {
|
|
|
|
int64_t reduceDim;
|
|
|
|
if (!matchPattern(op.dim(), m_TorchConstantInt(&reduceDim)))
|
|
|
|
return rewriter.notifyMatchFailure(op,
|
|
|
|
"non-const dim parameter unsupported");
|
|
|
|
auto reduceDimsType = RankedTensorType::get({1}, rewriter.getI64Type());
|
|
|
|
reduceDimsAttr = DenseIntElementsAttr::get(reduceDimsType,
|
|
|
|
llvm::makeArrayRef({reduceDim}));
|
|
|
|
|
|
|
|
keepDims = false;
|
|
|
|
if (!matchPattern(op.keepdim(), m_TorchConstantBool(&keepDims)))
|
|
|
|
return rewriter.notifyMatchFailure(
|
|
|
|
op, "non-const keepdim parameter unsupported");
|
|
|
|
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// This reduction op legalization template handles op variants that reduce all
|
|
|
|
// dims does not keep dims.
|
|
|
|
template <typename AtenOpT, ReductionConvFunc ConversionFuncT>
|
|
|
|
class ConvertAtenAllDimsReductionOp
|
|
|
|
: public ConvertAtenReductionOp<AtenOpT, ConversionFuncT> {
|
|
|
|
public:
|
|
|
|
using ConvertAtenReductionOp<AtenOpT,
|
|
|
|
ConversionFuncT>::ConvertAtenReductionOp;
|
|
|
|
using OpAdaptor = typename AtenOpT::Adaptor;
|
|
|
|
LogicalResult readReduceDimsAndKeepDims(AtenOpT op, OpAdaptor adaptor,
|
|
|
|
ConversionPatternRewriter &rewriter,
|
|
|
|
ElementsAttr &reduceDimsAttr,
|
|
|
|
bool &keepDims) const {
|
|
|
|
auto self = adaptor.self();
|
|
|
|
auto selfTy = self.getType().template cast<RankedTensorType>();
|
|
|
|
|
|
|
|
// Select all dims to reduce
|
|
|
|
SmallVector<int64_t, 4> reduceDims;
|
|
|
|
for (int64_t i = 0; i < selfTy.getRank(); i++)
|
|
|
|
reduceDims.push_back(i);
|
|
|
|
int64_t N = selfTy.getRank();
|
|
|
|
auto reduceDimsType = RankedTensorType::get({N}, rewriter.getI64Type());
|
|
|
|
reduceDimsAttr = DenseIntElementsAttr::get(reduceDimsType,
|
|
|
|
llvm::makeArrayRef(reduceDims));
|
|
|
|
keepDims = false;
|
|
|
|
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-12-16 03:01:01 +08:00
|
|
|
template <>
|
|
|
|
LogicalResult ConvertAtenOp<AtenArgmaxOp>::matchAndRewrite(
|
|
|
|
AtenArgmaxOp op, OpAdaptor adaptor,
|
|
|
|
ConversionPatternRewriter &rewriter) const {
|
|
|
|
|
|
|
|
Value self = adaptor.self();
|
|
|
|
auto selfTy = self.getType().template cast<RankedTensorType>();
|
|
|
|
|
|
|
|
if (!selfTy)
|
|
|
|
return op.emitError("Only ranked tensor types supported in TOSA argmax");
|
|
|
|
|
|
|
|
int64_t reduceDim;
|
|
|
|
if (!matchPattern(op.dim(), m_TorchConstantInt(&reduceDim))) {
|
|
|
|
// NoneType indicates reduce on all dims
|
|
|
|
reduceDim = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool keepDim = false;
|
|
|
|
if (!matchPattern(op.keepdim(), m_TorchConstantBool(&keepDim)))
|
|
|
|
return rewriter.notifyMatchFailure(
|
|
|
|
op, "non-const keepdim parameter unsupported");
|
|
|
|
|
|
|
|
auto resultTy = getTypeConverter()
|
|
|
|
->convertType(op.getResult().getType())
|
|
|
|
.cast<RankedTensorType>();
|
|
|
|
auto outputETy = resultTy.getElementType();
|
|
|
|
|
|
|
|
// Create a single instance of tosa.argmax.
|
|
|
|
// Multiple dims require chained construct.
|
|
|
|
auto buildArgmax = [&](int64_t reduceDim, Value input) -> Value {
|
|
|
|
auto inputTy = input.getType().cast<RankedTensorType>();
|
|
|
|
auto inputShape = inputTy.getShape();
|
|
|
|
SmallVector<int64_t> outputShapeArr = {};
|
|
|
|
int32_t i = 0;
|
|
|
|
|
|
|
|
for (auto &dim : inputShape) {
|
|
|
|
if (i++ != reduceDim) {
|
|
|
|
outputShapeArr.push_back(dim);
|
|
|
|
} else {
|
|
|
|
if (keepDim)
|
|
|
|
outputShapeArr.push_back(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tosa argmax output is i32, while Torch backend mandates i64.
|
|
|
|
auto outputReduceTy = RankedTensorType::get(
|
|
|
|
ArrayRef<int64_t>(outputShapeArr), rewriter.getI32Type());
|
|
|
|
auto reduceDimAttr =
|
|
|
|
rewriter.getIntegerAttr(rewriter.getI64Type(), reduceDim);
|
|
|
|
return rewriter
|
|
|
|
.create<tosa::ArgMaxOp>(op->getLoc(),
|
|
|
|
getTypeConverter()->convertType(outputReduceTy),
|
|
|
|
input, reduceDimAttr)
|
|
|
|
.getResult();
|
|
|
|
};
|
|
|
|
|
|
|
|
// Convert the final index to i64 for backend finalization, However, i64
|
|
|
|
// is not a defined type for tosa.cast, so using arith.extsi instead.
|
|
|
|
auto castToInt64 = [&](Value result) -> LogicalResult {
|
|
|
|
auto resTy = result.getType().cast<ShapedType>();
|
|
|
|
if (!resTy)
|
|
|
|
return op.emitError("Argmax: Result is not a shaped type");
|
|
|
|
|
|
|
|
auto resShape = resTy.getShape();
|
|
|
|
auto outTy =
|
|
|
|
RankedTensorType::get(resShape, outputETy); // rewriter.getI64Type());
|
|
|
|
|
|
|
|
rewriter.replaceOpWithNewOp<arith::ExtSIOp>(
|
|
|
|
op, getTypeConverter()->convertType(outTy), result);
|
|
|
|
|
|
|
|
return success();
|
|
|
|
};
|
|
|
|
|
|
|
|
if (reduceDim == -1) { // reducing on all dims
|
|
|
|
Value input = self;
|
|
|
|
for (int dim = 0; dim < selfTy.getRank(); dim++) {
|
|
|
|
// progressively reduce each 0-th dim
|
|
|
|
input = buildArgmax(0, input);
|
|
|
|
}
|
|
|
|
return castToInt64(input);
|
|
|
|
} else {
|
|
|
|
return castToInt64(buildArgmax(reduceDim, self));
|
|
|
|
}
|
|
|
|
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2021-10-08 10:07:03 +08:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
2021-10-29 01:09:12 +08:00
|
|
|
// TorchToTosa Pass
|
2021-10-08 10:07:03 +08:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
namespace {
|
2021-11-11 11:03:36 +08:00
|
|
|
class ConvertTorchToTosa : public ConvertTorchToTosaBase<ConvertTorchToTosa> {
|
2021-10-08 10:07:03 +08:00
|
|
|
public:
|
|
|
|
void getDependentDialects(DialectRegistry ®istry) const override {
|
|
|
|
registry.insert<tosa::TosaDialect>();
|
2021-12-16 03:01:01 +08:00
|
|
|
registry.insert<tensor::TensorDialect>();
|
|
|
|
registry.insert<arith::ArithmeticDialect>();
|
2021-10-08 10:07:03 +08:00
|
|
|
TorchConversion::getBackendTypeConversionDependentDialects(registry);
|
|
|
|
}
|
|
|
|
|
|
|
|
void runOnOperation() override {
|
|
|
|
MLIRContext *context = &getContext();
|
|
|
|
ConversionTarget target(*context);
|
2021-12-16 03:01:01 +08:00
|
|
|
target.addLegalDialect<tosa::TosaDialect, tensor::TensorDialect,
|
|
|
|
arith::ArithmeticDialect>();
|
2021-10-08 10:07:03 +08:00
|
|
|
|
|
|
|
TypeConverter typeConverter;
|
|
|
|
typeConverter.addConversion([](Type type) { return type; });
|
|
|
|
TorchConversion::setupBackendTypeConversion(target, typeConverter);
|
|
|
|
|
|
|
|
RewritePatternSet patterns(context);
|
2021-10-29 01:09:12 +08:00
|
|
|
|
2021-11-12 08:15:58 +08:00
|
|
|
#define INSERT_UNARY_FPONLY_PATTERN(AtenOp, TosaOp) \
|
|
|
|
target.addIllegalOp<AtenOp>(); \
|
|
|
|
patterns.add<ConvertAtenUnaryFPOnlyOp<AtenOp, TosaOp>>(typeConverter, \
|
|
|
|
context);
|
|
|
|
INSERT_UNARY_FPONLY_PATTERN(AtenLogOp, tosa::LogOp)
|
|
|
|
INSERT_UNARY_FPONLY_PATTERN(AtenExpOp, tosa::ExpOp)
|
|
|
|
#undef INSERT_UNARY_FPONLY_PATTERN
|
|
|
|
|
|
|
|
#define INSERT_UNARY_PATTERN(AtenOp, TosaOp) \
|
|
|
|
target.addIllegalOp<AtenOp>(); \
|
|
|
|
patterns.add<ConvertAtenUnaryOp<AtenOp, TosaOp>>(typeConverter, context);
|
|
|
|
INSERT_UNARY_PATTERN(AtenNegOp, tosa::NegateOp)
|
|
|
|
INSERT_UNARY_PATTERN(AtenFloorOp, tosa::FloorOp)
|
2021-12-15 02:03:58 +08:00
|
|
|
INSERT_UNARY_PATTERN(AtenRsqrtOp, tosa::RsqrtOp)
|
2021-11-12 08:15:58 +08:00
|
|
|
INSERT_UNARY_PATTERN(AtenBitwiseNotOp, tosa::BitwiseNotOp)
|
|
|
|
#undef INSERT_UNARY_PATTERN
|
|
|
|
|
2021-12-16 03:19:25 +08:00
|
|
|
#define INSERT_BINARY_PATTERN(AtenOp, TosaOp) \
|
|
|
|
target.addIllegalOp<AtenOp>(); \
|
|
|
|
patterns.add<ConvertAtenBinaryOp<AtenOp, TosaOp>>(typeConverter, context);
|
|
|
|
INSERT_BINARY_PATTERN(AtenMaximumOp, tosa::MaximumOp)
|
|
|
|
INSERT_BINARY_PATTERN(AtenMinimumOp, tosa::MinimumOp)
|
|
|
|
#undef INSERT_BINARY_PATTERN
|
|
|
|
|
2021-11-12 08:15:58 +08:00
|
|
|
#define INSERT_BINARY_ADDSUB_PATTERN(AtenOp, TosaOp) \
|
|
|
|
target.addIllegalOp<AtenOp>(); \
|
|
|
|
patterns.add<ConvertAtenAddSubOp<AtenOp, TosaOp>>(typeConverter, context);
|
|
|
|
INSERT_BINARY_ADDSUB_PATTERN(AtenAddTensorOp, tosa::AddOp)
|
|
|
|
INSERT_BINARY_ADDSUB_PATTERN(AtenSubTensorOp, tosa::SubOp)
|
|
|
|
#undef INSERT_BINARY_ADDSUB_PATTERN
|
|
|
|
|
2021-12-03 08:52:01 +08:00
|
|
|
#define INSERT_NDIMS_REDUCTION_OP_PATTERN(AtenOp, ConversionFunc) \
|
|
|
|
target.addIllegalOp<AtenOp>(); \
|
|
|
|
patterns.add<ConvertAtenMultipleDimsReductionOp<AtenOp, ConversionFunc>>( \
|
|
|
|
typeConverter, context);
|
|
|
|
INSERT_NDIMS_REDUCTION_OP_PATTERN(AtenMeanDimOp,
|
|
|
|
mlir::tosa::convertReduceMeanOp)
|
|
|
|
INSERT_NDIMS_REDUCTION_OP_PATTERN(AtenSumDimIntListOp,
|
|
|
|
mlir::tosa::convertReduceSumOp)
|
|
|
|
#undef INSERT_NDIMS_REDUCTION_OP_PATTERN
|
|
|
|
|
|
|
|
#define INSERT_ONEDIM_REDUCTION_OP_PATTERN(AtenOp, ConversionFunc) \
|
|
|
|
target.addIllegalOp<AtenOp>(); \
|
|
|
|
patterns.add<ConvertAtenOneDimReductionOp<AtenOp, ConversionFunc>>( \
|
|
|
|
typeConverter, context);
|
|
|
|
INSERT_ONEDIM_REDUCTION_OP_PATTERN(AtenAnyDimOp,
|
|
|
|
mlir::tosa::convertReduceAnyOp)
|
|
|
|
#undef INSERT_ONEDIM_REDUCTION_OP_PATTERN
|
|
|
|
|
|
|
|
#define INSERT_ALLDIMS_REDUCTION_OP_PATTERN(AtenOp, ConversionFunc) \
|
|
|
|
target.addIllegalOp<AtenOp>(); \
|
|
|
|
patterns.add<ConvertAtenAllDimsReductionOp<AtenOp, ConversionFunc>>( \
|
|
|
|
typeConverter, context);
|
|
|
|
INSERT_ALLDIMS_REDUCTION_OP_PATTERN(AtenAllOp,
|
|
|
|
mlir::tosa::convertReduceAllOp)
|
|
|
|
INSERT_ALLDIMS_REDUCTION_OP_PATTERN(AtenAnyOp,
|
|
|
|
mlir::tosa::convertReduceAnyOp)
|
|
|
|
INSERT_ALLDIMS_REDUCTION_OP_PATTERN(AtenSumOp,
|
|
|
|
mlir::tosa::convertReduceSumOp)
|
|
|
|
#undef INSERT_ALLDIMS_REDUCTION_OP_PATTERN
|
|
|
|
|
2021-11-12 08:15:58 +08:00
|
|
|
#define INSERT_ATENOP_PATTERN(AtenOp) \
|
|
|
|
target.addIllegalOp<AtenOp>(); \
|
|
|
|
patterns.add<ConvertAtenOp<AtenOp>>(typeConverter, context);
|
|
|
|
INSERT_ATENOP_PATTERN(AtenTanhOp);
|
|
|
|
INSERT_ATENOP_PATTERN(AtenSigmoidOp);
|
|
|
|
INSERT_ATENOP_PATTERN(AtenReluOp);
|
|
|
|
INSERT_ATENOP_PATTERN(AtenMulTensorOp);
|
|
|
|
INSERT_ATENOP_PATTERN(AtenDivTensorOp);
|
2021-12-16 03:01:01 +08:00
|
|
|
INSERT_ATENOP_PATTERN(AtenArgmaxOp);
|
2021-11-12 08:15:58 +08:00
|
|
|
#undef INSERT_ATENOP_PATTERN
|
2021-10-08 10:07:03 +08:00
|
|
|
|
|
|
|
if (failed(applyPartialConversion(getOperation(), target,
|
|
|
|
std::move(patterns))))
|
|
|
|
return signalPassFailure();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
std::unique_ptr<OperationPass<FuncOp>>
|
|
|
|
mlir::torch::createConvertTorchToTosaPass() {
|
|
|
|
return std::make_unique<ConvertTorchToTosa>();
|
|
|
|
}
|