2022-07-21 07:18:16 +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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2023-02-02 21:29:47 +08:00
|
|
|
#include "torch-mlir/Conversion/TorchToStablehlo/TorchToStablehlo.h"
|
2022-07-21 07:18:16 +08:00
|
|
|
|
|
|
|
#include "../PassDetail.h"
|
2023-02-02 21:29:47 +08:00
|
|
|
#include "PopulatePatterns.h"
|
|
|
|
|
2022-10-05 21:28:06 +08:00
|
|
|
#include "mlir/Dialect/Arith/IR/Arith.h"
|
2022-08-02 12:53:24 +08:00
|
|
|
#include "mlir/Dialect/Tensor/IR/Tensor.h"
|
2022-08-31 03:44:00 +08:00
|
|
|
#include "stablehlo/dialect/ChloOps.h"
|
2023-02-02 21:29:47 +08:00
|
|
|
#include "stablehlo/dialect/StablehloOps.h"
|
2022-07-21 07:18:16 +08:00
|
|
|
#include "torch-mlir/Conversion/Utils/Utils.h"
|
|
|
|
#include "torch-mlir/Dialect/Torch/IR/TorchDialect.h"
|
|
|
|
#include "torch-mlir/Dialect/Torch/IR/TorchOps.h"
|
|
|
|
#include "torch-mlir/Dialect/Torch/Utils/TorchUpstream.h"
|
|
|
|
#include "torch-mlir/Dialect/Torch/Utils/Utils.h"
|
|
|
|
#include "torch-mlir/Dialect/TorchConversion/IR/TorchConversionOps.h"
|
2023-03-28 12:16:21 +08:00
|
|
|
#include "torch-mlir/Conversion/TorchToStablehlo/StablehloLegalizeUtils.h"
|
2022-11-17 06:40:36 +08:00
|
|
|
#include "utils/hlo_utils.h"
|
2022-07-21 07:18:16 +08:00
|
|
|
#include <iostream>
|
|
|
|
#include <numeric>
|
|
|
|
|
|
|
|
using namespace mlir;
|
|
|
|
using namespace mlir::torch;
|
|
|
|
using namespace mlir::torch::Torch;
|
2023-02-02 21:29:47 +08:00
|
|
|
using namespace mlir::torch::torch_to_stablehlo;
|
2022-07-21 07:18:16 +08:00
|
|
|
|
2022-11-24 14:28:34 +08:00
|
|
|
LogicalResult broadcastRanks(PatternRewriter &rewriter, Operation *op,
|
|
|
|
mlir::Value &self, mlir::Value &other,
|
|
|
|
size_t dimSizeIndexBits) {
|
|
|
|
auto selfTy = self.getType().template dyn_cast<RankedTensorType>();
|
|
|
|
auto otherTy = other.getType().template dyn_cast<RankedTensorType>();
|
|
|
|
auto selfRank = selfTy.getRank();
|
|
|
|
auto otherRank = otherTy.getRank();
|
|
|
|
if (selfRank == 0 || otherRank == 0)
|
|
|
|
return success();
|
|
|
|
if (selfRank > otherRank) {
|
|
|
|
auto unsqueezeDims =
|
|
|
|
llvm::to_vector<4>(llvm::seq<int64_t>(0, selfRank - otherRank));
|
2023-02-02 21:29:47 +08:00
|
|
|
auto unsqueezeInfo = hlo::unsqueezeTensor(rewriter, op, other,
|
|
|
|
unsqueezeDims, dimSizeIndexBits);
|
2022-11-24 14:28:34 +08:00
|
|
|
if (failed(unsqueezeInfo))
|
|
|
|
return failure();
|
|
|
|
other = *unsqueezeInfo;
|
|
|
|
} else if (otherRank > selfRank) {
|
|
|
|
auto unsqueezeDims =
|
|
|
|
llvm::to_vector<4>(llvm::seq<int64_t>(0, otherRank - selfRank));
|
2023-02-02 21:29:47 +08:00
|
|
|
auto unsqueezeInfo = hlo::unsqueezeTensor(rewriter, op, self, unsqueezeDims,
|
|
|
|
dimSizeIndexBits);
|
2022-11-24 14:28:34 +08:00
|
|
|
if (failed(unsqueezeInfo))
|
|
|
|
return failure();
|
|
|
|
self = *unsqueezeInfo;
|
|
|
|
}
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2022-07-27 13:07:51 +08:00
|
|
|
bool skipMultiplyAlpha(Value alphaValue) {
|
|
|
|
double doubleValue;
|
|
|
|
auto isFloat = matchPattern(alphaValue, m_TorchConstantFloat(&doubleValue));
|
|
|
|
|
|
|
|
int64_t intValue;
|
|
|
|
auto isInt = matchPattern(alphaValue, m_TorchConstantInt(&intValue));
|
|
|
|
|
|
|
|
return ((isFloat && doubleValue == 1.0) || (isInt && intValue == 1.0));
|
|
|
|
}
|
|
|
|
|
2022-09-16 15:09:21 +08:00
|
|
|
static FailureOr<Value> getMaxValueOfDtype(Operation *op, Type elementType,
|
|
|
|
PatternRewriter &rewriter) {
|
|
|
|
auto constType = RankedTensorType::get({}, elementType);
|
|
|
|
if (elementType.isa<mlir::FloatType>()) {
|
|
|
|
auto constAttr = SplatElementsAttr::get(
|
|
|
|
constType,
|
|
|
|
APFloat::getInf(elementType.cast<mlir::FloatType>().getFloatSemantics(),
|
|
|
|
/*negative=*/false));
|
2023-02-02 21:29:47 +08:00
|
|
|
return rewriter
|
|
|
|
.create<stablehlo::ConstantOp>(op->getLoc(), constType, constAttr)
|
2022-09-16 15:09:21 +08:00
|
|
|
.getResult();
|
|
|
|
}
|
|
|
|
if (elementType.isa<mlir::IntegerType>()) {
|
|
|
|
auto integerType = elementType.cast<mlir::IntegerType>();
|
|
|
|
DenseElementsAttr constAttr;
|
|
|
|
if (integerType.isUnsigned()) {
|
|
|
|
constAttr = SplatElementsAttr::get(
|
|
|
|
constType, APInt::getMaxValue(integerType.getWidth()));
|
|
|
|
} else {
|
|
|
|
constAttr = SplatElementsAttr::get(
|
|
|
|
constType, APInt::getSignedMaxValue(integerType.getWidth()));
|
|
|
|
}
|
2023-02-02 21:29:47 +08:00
|
|
|
return rewriter
|
|
|
|
.create<stablehlo::ConstantOp>(op->getLoc(), constType, constAttr)
|
2022-09-16 15:09:21 +08:00
|
|
|
.getResult();
|
|
|
|
}
|
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
|
|
|
|
static FailureOr<Value> getMinValueOfDtype(Operation *op, Type elementType,
|
|
|
|
PatternRewriter &rewriter) {
|
|
|
|
auto constType = RankedTensorType::get({}, elementType);
|
|
|
|
if (elementType.isa<mlir::FloatType>()) {
|
|
|
|
auto constAttr = SplatElementsAttr::get(
|
|
|
|
constType,
|
|
|
|
APFloat::getInf(elementType.cast<mlir::FloatType>().getFloatSemantics(),
|
|
|
|
/*negative=*/true));
|
2023-02-02 21:29:47 +08:00
|
|
|
return rewriter
|
|
|
|
.create<stablehlo::ConstantOp>(op->getLoc(), constType, constAttr)
|
2022-09-16 15:09:21 +08:00
|
|
|
.getResult();
|
|
|
|
}
|
|
|
|
if (elementType.isa<mlir::IntegerType>()) {
|
|
|
|
auto integerType = elementType.cast<mlir::IntegerType>();
|
|
|
|
DenseElementsAttr constAttr;
|
|
|
|
if (integerType.isUnsigned()) {
|
|
|
|
constAttr = SplatElementsAttr::get(
|
|
|
|
constType, APInt::getMinValue(integerType.getWidth()));
|
|
|
|
} else {
|
|
|
|
constAttr = SplatElementsAttr::get(
|
|
|
|
constType, APInt::getSignedMinValue(integerType.getWidth()));
|
|
|
|
}
|
2023-02-02 21:29:47 +08:00
|
|
|
return rewriter
|
|
|
|
.create<stablehlo::ConstantOp>(op->getLoc(), constType, constAttr)
|
2022-09-16 15:09:21 +08:00
|
|
|
.getResult();
|
|
|
|
}
|
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
|
2023-01-12 06:40:03 +08:00
|
|
|
// These legalizations are for unary ops.
|
|
|
|
namespace {
|
2023-02-02 21:29:47 +08:00
|
|
|
template <typename AtenOpT, typename StablehloOpT>
|
2023-01-12 06:40:03 +08:00
|
|
|
class ConvertAtenUnaryOp : public OpConversionPattern<AtenOpT> {
|
|
|
|
public:
|
|
|
|
using OpConversionPattern<AtenOpT>::OpConversionPattern;
|
|
|
|
using OpAdaptor = typename AtenOpT::Adaptor;
|
|
|
|
LogicalResult
|
|
|
|
matchAndRewrite(AtenOpT op, OpAdaptor adaptor,
|
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
|
|
|
Value self = adaptor.getSelf();
|
|
|
|
auto selfType = self.getType().cast<TensorType>();
|
|
|
|
if (!selfType) {
|
2023-02-02 21:29:47 +08:00
|
|
|
return op.emitError("only Tensor types supported in StableHLO");
|
2023-01-12 06:40:03 +08:00
|
|
|
}
|
|
|
|
auto outType = OpConversionPattern<AtenOpT>::getTypeConverter()
|
|
|
|
->convertType(op.getType())
|
|
|
|
.template cast<TensorType>();
|
2023-02-02 21:29:47 +08:00
|
|
|
self = hlo::promoteType(rewriter, self, outType);
|
|
|
|
rewriter.replaceOpWithNewOp<StablehloOpT>(op, outType, self);
|
2023-01-12 06:40:03 +08:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
2022-07-27 13:07:51 +08:00
|
|
|
// These legalizations are for unary ops with only for floating point datatypes.
|
|
|
|
// There is no supported quantized integer mode for these.
|
|
|
|
namespace {
|
2023-02-02 21:29:47 +08:00
|
|
|
template <typename AtenOpT, typename StablehloOpT>
|
2022-07-27 13:07:51 +08:00
|
|
|
class ConvertAtenUnaryFPOnlyOp : public OpConversionPattern<AtenOpT> {
|
|
|
|
public:
|
|
|
|
using OpConversionPattern<AtenOpT>::OpConversionPattern;
|
|
|
|
using OpAdaptor = typename AtenOpT::Adaptor;
|
|
|
|
LogicalResult
|
|
|
|
matchAndRewrite(AtenOpT op, OpAdaptor adaptor,
|
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
2022-12-08 04:20:41 +08:00
|
|
|
Value self = adaptor.getSelf();
|
2022-07-27 13:07:51 +08:00
|
|
|
auto selfTy = self.getType().cast<TensorType>();
|
|
|
|
|
|
|
|
if (!selfTy)
|
2023-02-02 21:29:47 +08:00
|
|
|
return op.emitError("only Tensor types supported in StableHLO");
|
2022-07-27 13:07:51 +08:00
|
|
|
|
|
|
|
if (selfTy.getElementType().isa<mlir::FloatType>()) {
|
2023-02-02 21:29:47 +08:00
|
|
|
rewriter.replaceOpWithNewOp<StablehloOpT>(
|
2022-07-27 13:07:51 +08:00
|
|
|
op,
|
|
|
|
OpConversionPattern<AtenOpT>::getTypeConverter()->convertType(
|
|
|
|
op.getType()),
|
|
|
|
self);
|
|
|
|
return success();
|
|
|
|
} else {
|
|
|
|
return op.emitError(
|
2022-08-02 12:53:24 +08:00
|
|
|
"only floating-point datatype legalization supported");
|
2022-07-27 13:07:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// aten.ones & aten.zeros
|
|
|
|
// Ref: Error checking based on the Torch to TOSA lowering
|
|
|
|
namespace {
|
|
|
|
template <typename AtenOpT, int fillVal>
|
|
|
|
class ConvertAtenConstPatternOp : public OpConversionPattern<AtenOpT> {
|
|
|
|
public:
|
|
|
|
using OpConversionPattern<AtenOpT>::OpConversionPattern;
|
|
|
|
using OpAdaptor = typename AtenOpT::Adaptor;
|
|
|
|
LogicalResult
|
|
|
|
matchAndRewrite(AtenOpT op, OpAdaptor adaptor,
|
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
|
|
|
|
|
|
|
auto outType = OpConversionPattern<AtenOpT>::getTypeConverter()
|
|
|
|
->convertType(op.getType())
|
|
|
|
.template dyn_cast<TensorType>();
|
|
|
|
|
|
|
|
if (!outType)
|
2023-02-02 21:29:47 +08:00
|
|
|
return op.emitError("only Tensor types supported in StableHLO");
|
2022-07-27 13:07:51 +08:00
|
|
|
|
|
|
|
Type outElemTy = outType.getElementType();
|
|
|
|
if (!outElemTy.isIntOrFloat())
|
|
|
|
return op.emitError(
|
2022-08-02 12:53:24 +08:00
|
|
|
"only floating-point or integer datatype legalization supported");
|
2022-07-27 13:07:51 +08:00
|
|
|
|
|
|
|
SmallVector<int64_t> shape;
|
2022-12-08 04:20:41 +08:00
|
|
|
if (!matchPattern(op.getSize(), m_TorchListOfConstantInts(shape))) {
|
2022-08-02 12:53:24 +08:00
|
|
|
return op.emitError("shape must be a list of Scalar constants");
|
2022-07-27 13:07:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int64_t size = 1;
|
|
|
|
for (auto s : shape)
|
|
|
|
size *= s;
|
|
|
|
|
|
|
|
SmallVector<int32_t> values(size, fillVal);
|
|
|
|
auto constOp =
|
2023-02-02 21:29:47 +08:00
|
|
|
hlo::getConstTensor<int32_t>(rewriter, op, values, shape).value();
|
2022-07-27 13:07:51 +08:00
|
|
|
|
2023-02-02 21:29:47 +08:00
|
|
|
rewriter.replaceOpWithNewOp<stablehlo::ConvertOp>(op, outType, constOp);
|
2022-07-27 13:07:51 +08:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2022-09-23 20:39:15 +08:00
|
|
|
// The binary broadcast patterns
|
|
|
|
namespace {
|
|
|
|
template <typename AtenOpT, typename ChloOpT>
|
|
|
|
class ConvertAtenBinaryBroadcastOp : public OpConversionPattern<AtenOpT> {
|
|
|
|
public:
|
|
|
|
using OpConversionPattern<AtenOpT>::OpConversionPattern;
|
|
|
|
using OpAdaptor = typename AtenOpT::Adaptor;
|
|
|
|
LogicalResult
|
|
|
|
matchAndRewrite(AtenOpT op, OpAdaptor adaptor,
|
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
2022-12-08 04:20:41 +08:00
|
|
|
Value lhs = adaptor.getSelf();
|
2022-09-23 20:39:15 +08:00
|
|
|
auto lhsTy = lhs.getType().cast<TensorType>();
|
2022-12-08 04:20:41 +08:00
|
|
|
Value rhs = adaptor.getOther();
|
2022-09-23 20:39:15 +08:00
|
|
|
auto rhsTy = rhs.getType().cast<TensorType>();
|
|
|
|
|
|
|
|
if (!lhsTy || !rhsTy)
|
|
|
|
return op.emitError("only Tensor types supported");
|
|
|
|
|
2023-01-12 06:40:03 +08:00
|
|
|
auto outTy = OpConversionPattern<AtenOpT>::getTypeConverter()
|
|
|
|
->convertType(op.getType())
|
|
|
|
.template cast<TensorType>();
|
2022-09-23 20:39:15 +08:00
|
|
|
|
2023-02-02 21:29:47 +08:00
|
|
|
lhs = hlo::promoteType(rewriter, lhs, outTy);
|
|
|
|
rhs = hlo::promoteType(rewriter, rhs, outTy);
|
2022-09-23 20:39:15 +08:00
|
|
|
|
2023-01-12 06:40:03 +08:00
|
|
|
rewriter.replaceOpWithNewOp<ChloOpT>(op, outTy, lhs, rhs,
|
|
|
|
/*broadcast_attr*/ nullptr);
|
2022-09-23 20:39:15 +08:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
2022-07-27 13:07:51 +08:00
|
|
|
// These binary op legalizations are specific to add/sub which have an
|
|
|
|
// alpha multiplier.
|
|
|
|
namespace {
|
2022-08-02 12:53:24 +08:00
|
|
|
template <typename AtenOpT, typename ChloOpT>
|
2022-07-27 13:07:51 +08:00
|
|
|
class ConvertAtenAddSubOp : public OpConversionPattern<AtenOpT> {
|
|
|
|
public:
|
|
|
|
using OpConversionPattern<AtenOpT>::OpConversionPattern;
|
|
|
|
using OpAdaptor = typename AtenOpT::Adaptor;
|
|
|
|
LogicalResult
|
|
|
|
matchAndRewrite(AtenOpT op, OpAdaptor adaptor,
|
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
2022-12-08 04:20:41 +08:00
|
|
|
Value lhs = adaptor.getSelf();
|
2022-08-02 12:53:24 +08:00
|
|
|
RankedTensorType lhsType = lhs.getType().dyn_cast<RankedTensorType>();
|
2022-12-08 04:20:41 +08:00
|
|
|
Value rhs = adaptor.getOther();
|
2022-08-02 12:53:24 +08:00
|
|
|
RankedTensorType rhsType = rhs.getType().dyn_cast<RankedTensorType>();
|
2022-07-27 13:07:51 +08:00
|
|
|
|
|
|
|
if (!lhsType)
|
2023-02-02 21:29:47 +08:00
|
|
|
return op.emitError("only Tensor types supported in StableHLO");
|
2022-07-27 13:07:51 +08:00
|
|
|
|
|
|
|
TensorType outType = OpConversionPattern<AtenOpT>::getTypeConverter()
|
|
|
|
->convertType(op.getType())
|
|
|
|
.template cast<TensorType>();
|
|
|
|
|
|
|
|
Type outElemTy = outType.getElementType();
|
|
|
|
if (!outElemTy.isIntOrFloat()) {
|
|
|
|
return op.emitError(
|
2022-08-02 12:53:24 +08:00
|
|
|
"only floating-point or integer datatype legalization supported");
|
2022-07-27 13:07:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!rhsType) {
|
2023-02-02 21:29:47 +08:00
|
|
|
rhs = hlo::scalarToStablehloTensor(rewriter, op, adaptor.getOther(),
|
|
|
|
outElemTy);
|
2022-08-17 09:07:36 +08:00
|
|
|
if (isa<AtenRsubScalarOp>(op)) {
|
|
|
|
std::swap(lhs, rhs);
|
|
|
|
}
|
2022-07-27 13:07:51 +08:00
|
|
|
}
|
2022-08-02 12:53:24 +08:00
|
|
|
|
2023-02-02 21:29:47 +08:00
|
|
|
lhs = hlo::promoteType(rewriter, lhs, outType);
|
|
|
|
rhs = hlo::promoteType(rewriter, rhs, outType);
|
2022-08-02 12:53:24 +08:00
|
|
|
|
2022-12-08 04:20:41 +08:00
|
|
|
if (!skipMultiplyAlpha(op.getAlpha())) {
|
2023-02-02 21:29:47 +08:00
|
|
|
Value alpha = hlo::scalarToStablehloTensor(rewriter, op,
|
|
|
|
adaptor.getAlpha(), outElemTy);
|
2022-08-02 12:53:24 +08:00
|
|
|
DenseIntElementsAttr bcastDimensions;
|
|
|
|
rhs = rewriter.create<chlo::BroadcastMulOp>(op->getLoc(), rhs, alpha,
|
|
|
|
bcastDimensions);
|
2022-07-27 13:07:51 +08:00
|
|
|
}
|
|
|
|
|
2022-08-02 12:53:24 +08:00
|
|
|
DenseIntElementsAttr bcastDimensions;
|
|
|
|
rewriter.replaceOpWithNewOp<ChloOpT>(op, outType, lhs, rhs,
|
|
|
|
bcastDimensions);
|
2022-07-27 13:07:51 +08:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
2022-08-02 12:53:24 +08:00
|
|
|
// Binary op legalizations for Mul/Div variants.
|
2022-07-27 13:07:51 +08:00
|
|
|
namespace {
|
2022-08-02 12:53:24 +08:00
|
|
|
template <typename AtenOpT, typename ChloOpT>
|
|
|
|
class ConvertAtenMulDivOp : public OpConversionPattern<AtenOpT> {
|
2022-07-27 13:07:51 +08:00
|
|
|
public:
|
|
|
|
using OpConversionPattern<AtenOpT>::OpConversionPattern;
|
|
|
|
using OpAdaptor = typename AtenOpT::Adaptor;
|
|
|
|
LogicalResult
|
|
|
|
matchAndRewrite(AtenOpT op, OpAdaptor adaptor,
|
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
2022-12-08 04:20:41 +08:00
|
|
|
Value lhs = adaptor.getSelf();
|
2022-07-27 13:07:51 +08:00
|
|
|
auto lhsType = lhs.getType().dyn_cast<TensorType>();
|
2022-12-08 04:20:41 +08:00
|
|
|
Value rhs = adaptor.getOther();
|
2022-07-27 13:07:51 +08:00
|
|
|
TensorType rhsType = rhs.getType().dyn_cast<TensorType>();
|
|
|
|
|
|
|
|
if (!lhsType)
|
2023-02-02 21:29:47 +08:00
|
|
|
return op.emitError("only Tensor types supported in StableHLO");
|
2022-07-27 13:07:51 +08:00
|
|
|
|
|
|
|
auto outType = OpConversionPattern<AtenOpT>::getTypeConverter()
|
|
|
|
->convertType(op.getType())
|
|
|
|
.template cast<TensorType>();
|
|
|
|
|
|
|
|
Type outElemTy = outType.getElementType();
|
|
|
|
if (!outElemTy.isIntOrFloat()) {
|
|
|
|
return op.emitError(
|
2022-08-02 12:53:24 +08:00
|
|
|
"only floating-point or integer datatype legalization supported");
|
2022-07-27 13:07:51 +08:00
|
|
|
}
|
|
|
|
|
2022-08-03 08:16:31 +08:00
|
|
|
if (std::is_same<AtenOpT, AtenSquareOp>()) {
|
|
|
|
rhs = lhs;
|
|
|
|
} else if (!rhsType) {
|
2023-02-02 21:29:47 +08:00
|
|
|
rhs = hlo::scalarToStablehloTensor(rewriter, op, adaptor.getOther(),
|
|
|
|
outElemTy);
|
2022-08-03 08:16:31 +08:00
|
|
|
}
|
2022-08-02 12:53:24 +08:00
|
|
|
DenseIntElementsAttr bcastDimensions;
|
2023-02-02 21:29:47 +08:00
|
|
|
lhs = hlo::promoteType(rewriter, lhs, outType);
|
|
|
|
rhs = hlo::promoteType(rewriter, rhs, outType);
|
2022-08-06 23:38:06 +08:00
|
|
|
auto loc = op.getLoc();
|
|
|
|
Value result =
|
|
|
|
rewriter.create<ChloOpT>(loc, outType, lhs, rhs, bcastDimensions);
|
|
|
|
|
|
|
|
if (!isa<AtenDivTensorModeOp>(op)) {
|
|
|
|
rewriter.replaceOp(op, result);
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
|
|
|
AtenDivTensorModeOp divTensorModeOp =
|
|
|
|
llvm::dyn_cast<AtenDivTensorModeOp>(op.getOperation());
|
|
|
|
std::string roundingMode;
|
2022-12-08 04:20:41 +08:00
|
|
|
if (!matchPattern(divTensorModeOp.getRoundingMode(),
|
2022-08-06 23:38:06 +08:00
|
|
|
m_TorchConstantStr(roundingMode)))
|
|
|
|
return rewriter.notifyMatchFailure(
|
|
|
|
op, "only support constant str rounding mode");
|
|
|
|
|
|
|
|
if (roundingMode == "trunc") {
|
|
|
|
// "trunc" - rounds the results of the division towards zero. Equivalent
|
|
|
|
// to C-style integer division.
|
2023-02-02 21:29:47 +08:00
|
|
|
auto sign = rewriter.create<stablehlo::SignOp>(loc, result);
|
|
|
|
auto abs = rewriter.create<stablehlo::AbsOp>(loc, result);
|
|
|
|
auto floor = rewriter.create<stablehlo::FloorOp>(loc, abs);
|
|
|
|
result = rewriter.create<stablehlo::MulOp>(loc, sign, floor).getResult();
|
2022-08-06 23:38:06 +08:00
|
|
|
}
|
|
|
|
if (roundingMode == "floor") {
|
|
|
|
// "floor" - rounds the results of the division down. Equivalent to
|
|
|
|
// floor division in Python (the // operator)
|
2023-02-02 21:29:47 +08:00
|
|
|
result = rewriter.create<stablehlo::FloorOp>(loc, result).getResult();
|
2022-08-06 23:38:06 +08:00
|
|
|
}
|
|
|
|
rewriter.replaceOp(op, result);
|
2022-07-27 13:07:51 +08:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// Binary op legalizations for comparator ops.
|
|
|
|
namespace {
|
|
|
|
template <typename AtenOpT>
|
|
|
|
class ConvertAtenCompareOp : public OpConversionPattern<AtenOpT> {
|
|
|
|
public:
|
|
|
|
using OpConversionPattern<AtenOpT>::OpConversionPattern;
|
|
|
|
using OpAdaptor = typename AtenOpT::Adaptor;
|
|
|
|
|
|
|
|
LogicalResult
|
|
|
|
matchAndRewrite(AtenOpT op, OpAdaptor adaptor,
|
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
2022-12-08 04:20:41 +08:00
|
|
|
Value lhs = adaptor.getSelf();
|
|
|
|
Value rhs = adaptor.getOther();
|
2022-07-27 13:07:51 +08:00
|
|
|
RankedTensorType lhsTy = lhs.getType().dyn_cast<RankedTensorType>();
|
|
|
|
RankedTensorType rhsTy = rhs.getType().dyn_cast<RankedTensorType>();
|
|
|
|
|
|
|
|
if (!lhsTy)
|
2023-02-02 21:29:47 +08:00
|
|
|
return op.emitError("only Tensor types supported in StableHLO");
|
2022-07-27 13:07:51 +08:00
|
|
|
|
|
|
|
RankedTensorType outType = OpConversionPattern<AtenOpT>::getTypeConverter()
|
|
|
|
->convertType(op.getType())
|
|
|
|
.template cast<RankedTensorType>();
|
|
|
|
|
|
|
|
Type lhsElemTy = lhsTy.getElementType();
|
|
|
|
if (!lhsElemTy.isIntOrFloat()) {
|
|
|
|
return op.emitError(
|
2022-08-02 12:53:24 +08:00
|
|
|
"only floating-point or integer datatype legalization supported");
|
2022-07-27 13:07:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!rhsTy) {
|
2023-02-02 21:29:47 +08:00
|
|
|
rhs = hlo::scalarToStablehloTensor(rewriter, op, adaptor.getOther(),
|
|
|
|
lhsElemTy);
|
2022-07-27 13:07:51 +08:00
|
|
|
}
|
|
|
|
|
2022-08-02 12:53:24 +08:00
|
|
|
// TODO: what is the PyTorch default type promotion?
|
2023-02-02 21:29:47 +08:00
|
|
|
rhs = hlo::promoteType(rewriter, rhs, lhsTy);
|
2022-07-27 13:07:51 +08:00
|
|
|
|
2022-08-31 03:44:00 +08:00
|
|
|
chlo::ComparisonTypeAttr compareTypeAttr;
|
|
|
|
chlo::ComparisonDirectionAttr compareDirectionAttr;
|
2022-07-27 13:07:51 +08:00
|
|
|
|
|
|
|
if (lhsElemTy.isa<mlir::FloatType>()) {
|
2022-08-31 03:44:00 +08:00
|
|
|
compareTypeAttr = chlo::ComparisonTypeAttr::get(
|
|
|
|
op->getContext(), chlo::ComparisonType::FLOAT);
|
2022-07-27 13:07:51 +08:00
|
|
|
} else if (lhsElemTy.isa<mlir::IntegerType>()) {
|
2022-08-31 03:44:00 +08:00
|
|
|
compareTypeAttr = chlo::ComparisonTypeAttr::get(
|
|
|
|
op->getContext(), chlo::ComparisonType::SIGNED);
|
2022-07-27 13:07:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (std::is_same<AtenOpT, AtenLtTensorOp>() ||
|
|
|
|
std::is_same<AtenOpT, AtenLtScalarOp>()) {
|
2022-08-31 03:44:00 +08:00
|
|
|
compareDirectionAttr = chlo::ComparisonDirectionAttr::get(
|
|
|
|
op->getContext(), chlo::ComparisonDirection::LT);
|
2022-07-27 13:07:51 +08:00
|
|
|
} else if (std::is_same<AtenOpT, AtenGtTensorOp>() ||
|
|
|
|
std::is_same<AtenOpT, AtenGtScalarOp>()) {
|
2022-08-31 03:44:00 +08:00
|
|
|
compareDirectionAttr = chlo::ComparisonDirectionAttr::get(
|
|
|
|
op->getContext(), chlo::ComparisonDirection::GT);
|
2022-11-24 14:28:34 +08:00
|
|
|
} else if (std::is_same<AtenOpT, AtenGeTensorOp>() ||
|
|
|
|
std::is_same<AtenOpT, AtenGeScalarOp>()) {
|
|
|
|
compareDirectionAttr = chlo::ComparisonDirectionAttr::get(
|
|
|
|
op->getContext(), chlo::ComparisonDirection::GE);
|
2022-07-27 13:07:51 +08:00
|
|
|
} else if (std::is_same<AtenOpT, AtenEqTensorOp>() ||
|
|
|
|
std::is_same<AtenOpT, AtenEqScalarOp>()) {
|
2022-08-31 03:44:00 +08:00
|
|
|
compareDirectionAttr = chlo::ComparisonDirectionAttr::get(
|
|
|
|
op->getContext(), chlo::ComparisonDirection::EQ);
|
2022-07-27 13:07:51 +08:00
|
|
|
} else if (std::is_same<AtenOpT, AtenNeTensorOp>() ||
|
|
|
|
std::is_same<AtenOpT, AtenNeScalarOp>()) {
|
2022-08-31 03:44:00 +08:00
|
|
|
compareDirectionAttr = chlo::ComparisonDirectionAttr::get(
|
|
|
|
op->getContext(), chlo::ComparisonDirection::NE);
|
2022-11-24 14:28:34 +08:00
|
|
|
} else if (std::is_same<AtenOpT, AtenLtTensorOp>() ||
|
|
|
|
std::is_same<AtenOpT, AtenLtScalarOp>()) {
|
|
|
|
compareDirectionAttr = chlo::ComparisonDirectionAttr::get(
|
|
|
|
op->getContext(), chlo::ComparisonDirection::LT);
|
|
|
|
} else if (std::is_same<AtenOpT, AtenLeTensorOp>() ||
|
|
|
|
std::is_same<AtenOpT, AtenLeScalarOp>()) {
|
|
|
|
compareDirectionAttr = chlo::ComparisonDirectionAttr::get(
|
|
|
|
op->getContext(), chlo::ComparisonDirection::LE);
|
|
|
|
} else {
|
|
|
|
return op.emitError("operator haven't been supported");
|
2022-07-27 13:07:51 +08:00
|
|
|
}
|
2022-08-02 12:53:24 +08:00
|
|
|
DenseIntElementsAttr bcastDimensions;
|
|
|
|
rewriter.replaceOpWithNewOp<chlo::BroadcastCompareOp>(
|
|
|
|
op, outType, lhs, rhs, bcastDimensions, compareDirectionAttr,
|
2022-07-27 13:07:51 +08:00
|
|
|
compareTypeAttr);
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
2023-01-04 10:11:25 +08:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// Binary op legalizations for Logical And/Or/Xor.
|
|
|
|
namespace {
|
|
|
|
template <typename AtenOpT, typename ChloOpT>
|
|
|
|
class ConvertAtenLogicalBinaryOp : public OpConversionPattern<AtenOpT> {
|
|
|
|
public:
|
|
|
|
using OpConversionPattern<AtenOpT>::OpConversionPattern;
|
|
|
|
using OpAdaptor = typename AtenOpT::Adaptor;
|
2022-07-27 13:07:51 +08:00
|
|
|
|
2023-01-04 10:11:25 +08:00
|
|
|
LogicalResult
|
|
|
|
matchAndRewrite(AtenOpT op, OpAdaptor adaptor,
|
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
|
|
|
TensorType outType = OpConversionPattern<AtenOpT>::getTypeConverter()
|
|
|
|
->convertType(op.getType())
|
|
|
|
.template cast<TensorType>();
|
2023-02-02 21:29:47 +08:00
|
|
|
Value lhs = hlo::promoteType(rewriter, adaptor.getSelf(), outType);
|
|
|
|
Value rhs = hlo::promoteType(rewriter, adaptor.getOther(), outType);
|
2023-01-04 10:11:25 +08:00
|
|
|
|
|
|
|
DenseIntElementsAttr bcastDimensions;
|
|
|
|
rewriter.replaceOpWithNewOp<ChloOpT>(op, outType, lhs, rhs,
|
|
|
|
bcastDimensions);
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
2022-07-27 13:07:51 +08:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// AtenTransposeIntOp
|
|
|
|
namespace {
|
|
|
|
class ConvertAtenTransposeIntOp
|
|
|
|
: public OpConversionPattern<AtenTransposeIntOp> {
|
|
|
|
public:
|
|
|
|
using OpConversionPattern::OpConversionPattern;
|
|
|
|
LogicalResult
|
|
|
|
matchAndRewrite(AtenTransposeIntOp op, OpAdaptor adaptor,
|
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
2022-12-08 04:20:41 +08:00
|
|
|
Value self = adaptor.getSelf();
|
2022-07-27 13:07:51 +08:00
|
|
|
int64_t dim0;
|
2022-12-08 04:20:41 +08:00
|
|
|
if (!matchPattern(op.getDim0(), m_TorchConstantInt(&dim0))) {
|
2022-07-27 13:07:51 +08:00
|
|
|
return rewriter.notifyMatchFailure(op, "dim0 must be constant");
|
|
|
|
}
|
|
|
|
int64_t dim1;
|
2022-12-08 04:20:41 +08:00
|
|
|
if (!matchPattern(op.getDim1(), m_TorchConstantInt(&dim1))) {
|
2022-07-27 13:07:51 +08:00
|
|
|
return rewriter.notifyMatchFailure(op, "dim1 must be constant");
|
|
|
|
}
|
|
|
|
|
|
|
|
auto inType = self.getType().cast<RankedTensorType>();
|
|
|
|
auto inputRank = inType.getRank();
|
|
|
|
auto outType = getTypeConverter()
|
|
|
|
->convertType(op->getResult(0).getType())
|
|
|
|
.cast<RankedTensorType>();
|
|
|
|
|
|
|
|
dim0 = toPositiveDim(dim0, inputRank);
|
|
|
|
if (!isValidDim(dim0, inputRank)) {
|
|
|
|
return rewriter.notifyMatchFailure(op, "dim0 out of range");
|
|
|
|
}
|
|
|
|
dim1 = toPositiveDim(dim1, inputRank);
|
|
|
|
if (!isValidDim(dim1, inputRank)) {
|
|
|
|
return rewriter.notifyMatchFailure(op, "dim1 out of range");
|
|
|
|
}
|
|
|
|
|
|
|
|
SmallVector<int64_t> permValues(inputRank);
|
|
|
|
std::iota(std::begin(permValues), std::end(permValues), 0);
|
|
|
|
std::swap(permValues[dim0], permValues[dim1]);
|
|
|
|
DenseIntElementsAttr permutation = DenseIntElementsAttr::get(
|
|
|
|
RankedTensorType::get({static_cast<long int>(permValues.size())},
|
|
|
|
rewriter.getI64Type()),
|
|
|
|
permValues);
|
2023-02-02 21:29:47 +08:00
|
|
|
rewriter.replaceOpWithNewOp<stablehlo::TransposeOp>(op, outType, self,
|
|
|
|
permutation);
|
2022-07-27 13:07:51 +08:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
2022-09-23 10:24:36 +08:00
|
|
|
// AtenToDtypeOp
|
|
|
|
template <>
|
|
|
|
LogicalResult ConvertAtenOp<AtenToDtypeOp>::matchAndRewrite(
|
|
|
|
AtenToDtypeOp op, OpAdaptor adaptor,
|
|
|
|
ConversionPatternRewriter &rewriter) const {
|
2022-12-08 04:20:41 +08:00
|
|
|
Value self = adaptor.getSelf();
|
2022-09-23 10:24:36 +08:00
|
|
|
auto outType =
|
|
|
|
getTypeConverter()->convertType(op.getType()).cast<RankedTensorType>();
|
2023-02-02 21:29:47 +08:00
|
|
|
rewriter.replaceOpWithNewOp<stablehlo::ConvertOp>(op, outType, self);
|
2022-09-23 10:24:36 +08:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
LogicalResult ConvertAtenOp<AtenSizeIntOp>::matchAndRewrite(
|
|
|
|
AtenSizeIntOp op, OpAdaptor adaptor,
|
|
|
|
ConversionPatternRewriter &rewriter) const {
|
|
|
|
// Not a tensor type.
|
2022-12-08 04:20:41 +08:00
|
|
|
auto selfType = adaptor.getSelf().getType().dyn_cast<TensorType>();
|
2022-09-23 10:24:36 +08:00
|
|
|
if (!selfType)
|
|
|
|
return op.emitError("only tensor types are currently supported");
|
2022-11-21 21:50:35 +08:00
|
|
|
|
|
|
|
Value dim;
|
|
|
|
int64_t dimInt;
|
2022-12-08 04:20:41 +08:00
|
|
|
if (matchPattern(op.getDim(), m_TorchConstantInt(&dimInt))) {
|
2022-11-21 21:50:35 +08:00
|
|
|
dimInt = toPositiveDim(dimInt, selfType.getRank());
|
2023-04-07 19:49:35 +08:00
|
|
|
if (!isValidDim(dimInt, selfType.getRank()))
|
|
|
|
return rewriter.notifyMatchFailure(op, "dim is statically invalid");
|
2022-11-21 21:50:35 +08:00
|
|
|
dim = rewriter.create<arith::ConstantIndexOp>(op.getLoc(), dimInt);
|
|
|
|
} else {
|
|
|
|
Value inputRank = rewriter.create<arith::ConstantOp>(
|
|
|
|
op.getLoc(), rewriter.getI64IntegerAttr(selfType.getRank()));
|
2023-02-02 21:29:47 +08:00
|
|
|
dim = toPositiveDimDynamic(rewriter, op.getLoc(), adaptor.getDim(),
|
|
|
|
inputRank);
|
2022-11-21 21:50:35 +08:00
|
|
|
dim = rewriter.create<arith::IndexCastOp>(op.getLoc(),
|
|
|
|
rewriter.getIndexType(), dim);
|
|
|
|
}
|
|
|
|
|
2022-09-23 10:24:36 +08:00
|
|
|
auto dimSize = rewriter.create<tensor::DimOp>(
|
2022-12-08 04:20:41 +08:00
|
|
|
op.getLoc(), rewriter.getIndexType(), adaptor.getSelf(), dim);
|
2022-09-23 10:24:36 +08:00
|
|
|
|
|
|
|
rewriter.replaceOpWithNewOp<arith::IndexCastOp>(
|
|
|
|
op, getTypeConverter()->convertType(op.getType()), dimSize);
|
2022-11-21 21:50:35 +08:00
|
|
|
|
2022-09-23 10:24:36 +08:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2022-11-24 14:28:34 +08:00
|
|
|
template <>
|
|
|
|
LogicalResult ConvertAtenOp<AtenWhereSelfOp>::matchAndRewrite(
|
2023-02-02 21:29:47 +08:00
|
|
|
AtenWhereSelfOp op, OpAdaptor adaptor,
|
|
|
|
ConversionPatternRewriter &rewriter) const {
|
2022-12-08 04:20:41 +08:00
|
|
|
Value self = adaptor.getSelf();
|
|
|
|
Value cond = adaptor.getCondition();
|
|
|
|
Value other = adaptor.getOther();
|
2022-11-24 14:28:34 +08:00
|
|
|
|
2023-05-06 06:21:55 +08:00
|
|
|
auto outType =
|
|
|
|
getTypeConverter()->convertType(op.getType()).cast<RankedTensorType>();
|
|
|
|
// promote self and other types
|
|
|
|
self = hlo::promoteType(rewriter, self, outType);
|
|
|
|
other = hlo::promoteType(rewriter, other, outType);
|
|
|
|
|
2022-11-24 14:28:34 +08:00
|
|
|
if (failed(
|
|
|
|
broadcastRanks(rewriter, op, self, cond, options.dimSizeIndexBits)))
|
|
|
|
return op.emitError("failed broadcast self and condition ranks");
|
|
|
|
|
|
|
|
if (failed(
|
|
|
|
broadcastRanks(rewriter, op, other, cond, options.dimSizeIndexBits)))
|
|
|
|
return op.emitError("failed broadcast other and condition ranks");
|
|
|
|
|
|
|
|
rewriter.replaceOpWithNewOp<chlo::BroadcastSelectOp>(
|
2023-02-02 21:29:47 +08:00
|
|
|
op, getTypeConverter()->convertType(op.getType()),
|
2022-11-24 14:28:34 +08:00
|
|
|
ArrayRef<Value>{cond, self, other});
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2022-07-27 13:07:51 +08:00
|
|
|
// AtenBroadcastToOp
|
2022-09-01 10:36:02 +08:00
|
|
|
template <>
|
|
|
|
LogicalResult ConvertAtenOp<AtenBroadcastToOp>::matchAndRewrite(
|
|
|
|
AtenBroadcastToOp op, OpAdaptor adaptor,
|
|
|
|
ConversionPatternRewriter &rewriter) const {
|
2022-12-08 04:20:41 +08:00
|
|
|
Value self = adaptor.getSelf();
|
2022-09-01 10:36:02 +08:00
|
|
|
auto selfTy = self.getType().cast<RankedTensorType>();
|
|
|
|
auto outType = getTypeConverter()
|
|
|
|
->convertType(op->getResult(0).getType())
|
|
|
|
.cast<RankedTensorType>();
|
|
|
|
|
|
|
|
if (options.enableStaticShape && selfTy.hasStaticShape()) {
|
2023-02-02 21:29:47 +08:00
|
|
|
Value bcastOp = hlo::promoteAndBroadcast(rewriter, self, outType);
|
2022-09-01 10:36:02 +08:00
|
|
|
rewriter.replaceOp(op, bcastOp);
|
|
|
|
return success();
|
|
|
|
}
|
2022-08-02 12:53:24 +08:00
|
|
|
|
2022-09-01 10:36:02 +08:00
|
|
|
SmallVector<Value> shape;
|
2022-12-08 04:20:41 +08:00
|
|
|
if (!(getListConstructElements(adaptor.getSize(), shape))) {
|
2022-09-01 10:36:02 +08:00
|
|
|
return op->emitError("desired shape must be a list of scalar");
|
|
|
|
}
|
|
|
|
SmallVector<Value> bcastShapeVec;
|
|
|
|
int64_t totalRank = shape.size();
|
|
|
|
int64_t selfRank = selfTy.getRank();
|
|
|
|
int64_t leadingRank = totalRank - selfRank;
|
|
|
|
|
|
|
|
for (int64_t i = 0; i < totalRank; ++i) {
|
|
|
|
Value dValue = shape[i];
|
|
|
|
Value newD;
|
|
|
|
int64_t dInt;
|
2022-09-23 10:24:36 +08:00
|
|
|
if (i >= leadingRank && matchPattern(dValue, m_TorchConstantInt(&dInt)) &&
|
|
|
|
dInt == -1) {
|
2022-09-01 10:36:02 +08:00
|
|
|
newD = rewriter.create<mlir::tensor::DimOp>(op->getLoc(), self,
|
|
|
|
i - leadingRank);
|
|
|
|
} else {
|
|
|
|
dValue = rewriter.create<torch::TorchConversion::ToI64Op>(op->getLoc(),
|
|
|
|
dValue);
|
|
|
|
newD = rewriter.create<mlir::arith::IndexCastOp>(
|
|
|
|
op->getLoc(), rewriter.getIndexType(), dValue);
|
2022-08-02 12:53:24 +08:00
|
|
|
}
|
2022-09-01 10:36:02 +08:00
|
|
|
bcastShapeVec.push_back(newD);
|
|
|
|
}
|
2022-08-02 12:53:24 +08:00
|
|
|
|
2022-09-01 10:36:02 +08:00
|
|
|
if (options.dimSizeIndexBits == 32) {
|
2022-08-02 12:53:24 +08:00
|
|
|
for (auto &dsize : bcastShapeVec) {
|
|
|
|
auto dsizeI64 = rewriter.create<mlir::arith::IndexCastOp>(
|
|
|
|
op->getLoc(), rewriter.getI64Type(), dsize);
|
|
|
|
dsize = rewriter.create<arith::TruncIOp>(op->getLoc(),
|
|
|
|
rewriter.getI32Type(), dsizeI64);
|
|
|
|
}
|
2022-09-01 10:36:02 +08:00
|
|
|
}
|
2022-08-02 12:53:24 +08:00
|
|
|
|
2022-09-23 10:24:36 +08:00
|
|
|
if (bcastShapeVec.size() == 0) {
|
|
|
|
rewriter.replaceOpWithNewOp<tensor::CastOp>(op, outType, self);
|
|
|
|
} else {
|
2022-08-02 12:53:24 +08:00
|
|
|
Value bcastShapeTensor = rewriter.create<mlir::tensor::FromElementsOp>(
|
|
|
|
op->getLoc(), ValueRange{bcastShapeVec});
|
|
|
|
auto dimensionNumbers =
|
|
|
|
llvm::to_vector<4>(llvm::seq<int64_t>(leadingRank, totalRank));
|
2023-02-02 21:29:47 +08:00
|
|
|
rewriter.replaceOpWithNewOp<stablehlo::DynamicBroadcastInDimOp>(
|
2022-08-02 12:53:24 +08:00
|
|
|
op, outType, self, bcastShapeTensor,
|
|
|
|
rewriter.getI64TensorAttr(dimensionNumbers));
|
2022-09-23 10:24:36 +08:00
|
|
|
}
|
|
|
|
return success();
|
2022-09-01 10:36:02 +08:00
|
|
|
}
|
2022-07-27 13:07:51 +08:00
|
|
|
|
|
|
|
// AtenPermuteOp
|
2022-09-01 10:36:02 +08:00
|
|
|
template <>
|
|
|
|
LogicalResult ConvertAtenOp<AtenPermuteOp>::matchAndRewrite(
|
|
|
|
AtenPermuteOp op, OpAdaptor adaptor,
|
|
|
|
ConversionPatternRewriter &rewriter) const {
|
2022-12-08 04:20:41 +08:00
|
|
|
Value self = adaptor.getSelf();
|
2022-09-01 10:36:02 +08:00
|
|
|
// Not a ranked tensor type
|
|
|
|
auto inType = self.getType().dyn_cast<RankedTensorType>();
|
|
|
|
auto outType = getTypeConverter()
|
|
|
|
->convertType(op->getResult(0).getType())
|
|
|
|
.cast<RankedTensorType>();
|
|
|
|
if (!inType)
|
|
|
|
return op.emitError("only ranked tensor types with static shapes are "
|
|
|
|
"currently supported");
|
|
|
|
|
|
|
|
SmallVector<int64_t> permValues;
|
2022-12-08 04:20:41 +08:00
|
|
|
if (!matchPattern(adaptor.getDims(), m_TorchListOfConstantInts(permValues)))
|
2022-09-01 10:36:02 +08:00
|
|
|
return rewriter.notifyMatchFailure(
|
|
|
|
op, "only constant dimensions are currently supported");
|
2022-07-27 13:07:51 +08:00
|
|
|
|
2022-09-01 10:36:02 +08:00
|
|
|
int64_t inRank = inType.getRank();
|
|
|
|
for (auto &d : permValues) {
|
|
|
|
d = toPositiveDim(d, inRank);
|
|
|
|
if (!isValidDim(d, inRank))
|
|
|
|
return op.emitError("not all dims are valid");
|
2022-07-27 13:07:51 +08:00
|
|
|
}
|
2022-07-21 07:18:16 +08:00
|
|
|
|
2022-09-01 10:36:02 +08:00
|
|
|
DenseIntElementsAttr permutation = DenseIntElementsAttr::get(
|
|
|
|
RankedTensorType::get({static_cast<long int>(permValues.size())},
|
|
|
|
rewriter.getI64Type()),
|
|
|
|
permValues);
|
2023-02-02 21:29:47 +08:00
|
|
|
rewriter.replaceOpWithNewOp<stablehlo::TransposeOp>(op, outType, self,
|
|
|
|
permutation);
|
2022-09-01 10:36:02 +08:00
|
|
|
return success();
|
|
|
|
}
|
2022-07-21 07:18:16 +08:00
|
|
|
|
2022-07-27 13:07:51 +08:00
|
|
|
// ValueTensorLiteralOp
|
|
|
|
template <>
|
|
|
|
LogicalResult ConvertAtenOp<ValueTensorLiteralOp>::matchAndRewrite(
|
|
|
|
ValueTensorLiteralOp op, OpAdaptor adaptor,
|
|
|
|
ConversionPatternRewriter &rewriter) const {
|
|
|
|
RankedTensorType resultType = getTypeConverter()
|
|
|
|
->convertType(op->getResult(0).getType())
|
|
|
|
.cast<RankedTensorType>();
|
|
|
|
|
|
|
|
// Tensors with integer types need to be converted to signless integer
|
|
|
|
// element type. All tensors with element types other than integer can reuse
|
|
|
|
// existing elements attribute.
|
2022-08-02 12:53:24 +08:00
|
|
|
// TODO: what about unsigned integer?
|
2022-12-08 04:20:41 +08:00
|
|
|
if (auto elements = op.getValueAttr().dyn_cast<DenseIntElementsAttr>()) {
|
2022-07-27 13:07:51 +08:00
|
|
|
Type builtinTensorElemTy = resultType.getElementType();
|
|
|
|
unsigned bitWidth = builtinTensorElemTy.getIntOrFloatBitWidth();
|
|
|
|
|
|
|
|
DenseElementsAttr valueAttr =
|
|
|
|
elements.mapValues(builtinTensorElemTy, [&](const APInt &v) {
|
|
|
|
return APInt(bitWidth, v.getSExtValue());
|
|
|
|
});
|
2023-02-02 21:29:47 +08:00
|
|
|
rewriter.replaceOpWithNewOp<stablehlo::ConstantOp>(op, resultType,
|
|
|
|
valueAttr);
|
2022-07-27 13:07:51 +08:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2023-02-02 21:29:47 +08:00
|
|
|
rewriter.replaceOpWithNewOp<stablehlo::ConstantOp>(op, resultType,
|
|
|
|
adaptor.getValue());
|
2022-07-27 13:07:51 +08:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
|
|
|
// AtenReciprocalOp
|
|
|
|
// Reciprocal(x) = Div(1, x)
|
|
|
|
template <>
|
|
|
|
LogicalResult ConvertAtenOp<AtenReciprocalOp>::matchAndRewrite(
|
|
|
|
AtenReciprocalOp op, OpAdaptor adaptor,
|
|
|
|
ConversionPatternRewriter &rewriter) const {
|
2022-12-08 04:20:41 +08:00
|
|
|
Value input = adaptor.getSelf();
|
2022-07-27 13:07:51 +08:00
|
|
|
auto inputTy = input.getType().cast<RankedTensorType>();
|
|
|
|
auto outTy =
|
|
|
|
getTypeConverter()->convertType(op.getType()).cast<RankedTensorType>();
|
|
|
|
if (!inputTy.getElementType().isa<mlir::FloatType>()) {
|
2022-08-02 12:53:24 +08:00
|
|
|
return op.emitError("only floating-point datatype legalization supported "
|
2022-07-27 13:07:51 +08:00
|
|
|
"for AtenReciprocalOp");
|
|
|
|
}
|
2022-08-02 12:53:24 +08:00
|
|
|
|
|
|
|
Value oneTensor = chlo::getConstantLike(rewriter, op->getLoc(), 1, input);
|
2023-02-02 21:29:47 +08:00
|
|
|
rewriter.replaceOpWithNewOp<stablehlo::DivOp>(op, outTy, oneTensor, input);
|
2022-07-27 13:07:51 +08:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2023-02-17 12:26:46 +08:00
|
|
|
// AtenPowTensorScalarOp
|
|
|
|
template <>
|
|
|
|
LogicalResult ConvertAtenOp<AtenPowTensorScalarOp>::matchAndRewrite(
|
|
|
|
AtenPowTensorScalarOp op, OpAdaptor adaptor,
|
|
|
|
ConversionPatternRewriter &rewriter) const {
|
|
|
|
Value lhs = adaptor.getSelf();
|
|
|
|
auto lhsType = lhs.getType().dyn_cast<TensorType>();
|
|
|
|
Value rhs = adaptor.getExponent();
|
|
|
|
TensorType rhsType = rhs.getType().dyn_cast<TensorType>();
|
|
|
|
|
|
|
|
if (!lhsType)
|
|
|
|
return op.emitError("only Tensor types supported in StableHLO");
|
|
|
|
|
|
|
|
auto outType = OpConversionPattern<AtenPowTensorScalarOp>::getTypeConverter()
|
|
|
|
->convertType(op.getType())
|
|
|
|
.template cast<TensorType>();
|
|
|
|
|
|
|
|
Type outElemTy = outType.getElementType();
|
|
|
|
if (!outElemTy.isIntOrFloat()) {
|
|
|
|
return op.emitError(
|
|
|
|
"only floating-point or integer datatype legalization supported");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!rhsType) {
|
|
|
|
rhs = hlo::scalarToStablehloTensor(rewriter, op, rhs,
|
|
|
|
outElemTy);
|
|
|
|
}
|
|
|
|
DenseIntElementsAttr bcastDimensions;
|
|
|
|
lhs = hlo::promoteType(rewriter, lhs, outType);
|
|
|
|
rhs = hlo::promoteType(rewriter, rhs, outType);
|
|
|
|
auto loc = op.getLoc();
|
|
|
|
Value result =
|
|
|
|
rewriter.create<chlo::BroadcastPowOp>(loc, outType, lhs, rhs, bcastDimensions);
|
|
|
|
|
|
|
|
rewriter.replaceOp(op, result);
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2022-07-27 13:07:51 +08:00
|
|
|
// PrimNumToTensorScalarOp
|
|
|
|
template <>
|
|
|
|
LogicalResult ConvertAtenOp<PrimNumToTensorScalarOp>::matchAndRewrite(
|
|
|
|
PrimNumToTensorScalarOp op, OpAdaptor adaptor,
|
|
|
|
ConversionPatternRewriter &rewriter) const {
|
|
|
|
RankedTensorType outputType = getTypeConverter()
|
|
|
|
->convertType(op->getResult(0).getType())
|
|
|
|
.cast<RankedTensorType>();
|
|
|
|
auto outputElemType = outputType.getElementType();
|
2023-02-02 21:29:47 +08:00
|
|
|
Value stablehloTensor = hlo::scalarToStablehloTensor(
|
|
|
|
rewriter, op, adaptor.getA(), outputElemType);
|
|
|
|
rewriter.replaceOp(op, stablehloTensor);
|
2022-07-27 13:07:51 +08:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
|
|
|
// AtenContiguousOp
|
|
|
|
// Ref: TosaToTosa.cpp for implementation details
|
|
|
|
template <>
|
|
|
|
LogicalResult ConvertAtenOp<AtenContiguousOp>::matchAndRewrite(
|
|
|
|
AtenContiguousOp op, OpAdaptor adaptor,
|
|
|
|
ConversionPatternRewriter &rewriter) const {
|
|
|
|
|
|
|
|
// Not a tensor type.
|
2022-12-08 04:20:41 +08:00
|
|
|
auto selfType = adaptor.getSelf().getType().dyn_cast<TensorType>();
|
2022-07-27 13:07:51 +08:00
|
|
|
if (!selfType)
|
2022-08-02 12:53:24 +08:00
|
|
|
return op.emitError("only tensor types are currently supported");
|
2022-07-27 13:07:51 +08:00
|
|
|
|
|
|
|
// FIXME: memory_format is not handled.
|
|
|
|
|
2022-12-08 04:20:41 +08:00
|
|
|
rewriter.replaceOp(op, adaptor.getSelf());
|
2022-07-27 13:07:51 +08:00
|
|
|
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
|
|
|
// AtenReluOp
|
|
|
|
// Relu(x) = Max(0, x)
|
|
|
|
template <>
|
|
|
|
LogicalResult ConvertAtenOp<AtenReluOp>::matchAndRewrite(
|
|
|
|
AtenReluOp op, OpAdaptor adaptor,
|
|
|
|
ConversionPatternRewriter &rewriter) const {
|
2022-12-08 04:20:41 +08:00
|
|
|
Value lhs = adaptor.getSelf();
|
2022-07-27 13:07:51 +08:00
|
|
|
auto lhsTy = lhs.getType().cast<RankedTensorType>();
|
|
|
|
auto lhsElemTy = lhsTy.getElementType();
|
|
|
|
|
2022-08-02 12:53:24 +08:00
|
|
|
if (!lhsElemTy.isa<mlir::FloatType>()) {
|
|
|
|
return op->emitError("only float tensor in relu op is supported");
|
2022-07-27 13:07:51 +08:00
|
|
|
}
|
|
|
|
|
2022-08-02 12:53:24 +08:00
|
|
|
Value zeroTensor;
|
|
|
|
zeroTensor = chlo::getConstantLike(
|
|
|
|
rewriter, op->getLoc(),
|
|
|
|
APFloat::getZero(lhsElemTy.cast<mlir::FloatType>().getFloatSemantics(),
|
|
|
|
false),
|
|
|
|
lhs);
|
2023-02-02 21:29:47 +08:00
|
|
|
rewriter.replaceOpWithNewOp<stablehlo::MaxOp>(op, lhs, zeroTensor);
|
2022-07-27 13:07:51 +08:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2022-08-02 15:01:30 +08:00
|
|
|
// Convert a Aten::GELU to HLO
|
|
|
|
// Gelu(x) = x * 1/2 * [1 + erf(x/(sqrt(2)))]
|
|
|
|
template <>
|
|
|
|
LogicalResult ConvertAtenOp<AtenGeluOp>::matchAndRewrite(
|
2022-08-04 12:34:22 +08:00
|
|
|
AtenGeluOp op, OpAdaptor adaptor,
|
|
|
|
ConversionPatternRewriter &rewriter) const {
|
2022-08-02 15:01:30 +08:00
|
|
|
Location loc = op.getLoc();
|
2022-12-08 04:20:41 +08:00
|
|
|
Value input = adaptor.getSelf();
|
2022-08-02 15:01:30 +08:00
|
|
|
auto inputTy = input.getType().template dyn_cast<RankedTensorType>();
|
|
|
|
if (!inputTy) {
|
|
|
|
return op.emitError("only ranked tensor type is supported.");
|
|
|
|
}
|
|
|
|
|
|
|
|
Value one = chlo::getConstantLike(rewriter, loc, 1.0, input);
|
|
|
|
Value two = chlo::getConstantLike(rewriter, loc, 2.0, input);
|
|
|
|
Value half = chlo::getConstantLike(rewriter, loc, 0.5, input);
|
2023-02-02 21:29:47 +08:00
|
|
|
auto rsqrtTwo = rewriter.create<mlir::stablehlo::RsqrtOp>(loc, two);
|
|
|
|
auto erfElement = rewriter.create<stablehlo::MulOp>(loc, input, rsqrtTwo);
|
2022-08-02 15:01:30 +08:00
|
|
|
auto erf = rewriter.create<mlir::chlo::ErfOp>(loc, erfElement);
|
2023-02-02 21:29:47 +08:00
|
|
|
auto erfAdd = rewriter.create<stablehlo::AddOp>(loc, erf, one);
|
|
|
|
auto halfMul = rewriter.create<stablehlo::MulOp>(loc, erfAdd, half);
|
|
|
|
rewriter.replaceOpWithNewOp<stablehlo::MulOp>(op, input, halfMul);
|
2022-08-02 15:01:30 +08:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2022-07-27 13:07:51 +08:00
|
|
|
// AtenErfOp
|
|
|
|
template <>
|
|
|
|
LogicalResult ConvertAtenOp<AtenErfOp>::matchAndRewrite(
|
|
|
|
AtenErfOp op, OpAdaptor adaptor,
|
|
|
|
ConversionPatternRewriter &rewriter) const {
|
2022-12-08 04:20:41 +08:00
|
|
|
Value input = adaptor.getSelf();
|
2022-08-02 12:53:24 +08:00
|
|
|
auto inputType = input.getType().cast<TensorType>();
|
2022-07-27 13:07:51 +08:00
|
|
|
if (!inputType.getElementType().isa<mlir::FloatType>()) {
|
2022-08-02 12:53:24 +08:00
|
|
|
return rewriter.notifyMatchFailure(op, "only float tensor is supported");
|
2022-07-27 13:07:51 +08:00
|
|
|
}
|
2022-08-02 12:53:24 +08:00
|
|
|
rewriter.replaceOpWithNewOp<chlo::ErfOp>(
|
|
|
|
op, getTypeConverter()->convertType(op.getType()), input);
|
2022-07-27 13:07:51 +08:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
|
|
|
// AtenBatchNormOp
|
|
|
|
template <>
|
|
|
|
LogicalResult ConvertAtenOp<AtenBatchNormOp>::matchAndRewrite(
|
|
|
|
AtenBatchNormOp op, OpAdaptor adaptor,
|
|
|
|
ConversionPatternRewriter &rewriter) const {
|
2022-12-08 04:20:41 +08:00
|
|
|
Value input = adaptor.getInput();
|
2022-07-27 13:07:51 +08:00
|
|
|
// shape = [N, C, H, W]
|
|
|
|
auto inputTy = input.getType().cast<RankedTensorType>();
|
2022-12-08 04:20:41 +08:00
|
|
|
Value weight = adaptor.getWeight();
|
|
|
|
Value bias = adaptor.getBias();
|
|
|
|
Value runningMean = adaptor.getRunningMean();
|
|
|
|
Value runningVar = adaptor.getRunningVar();
|
2022-07-27 13:07:51 +08:00
|
|
|
// momentum is ignored
|
2022-12-08 04:20:41 +08:00
|
|
|
Value momentum = adaptor.getMomentum();
|
2022-07-27 13:07:51 +08:00
|
|
|
(void)momentum;
|
|
|
|
|
2023-05-18 00:04:40 +08:00
|
|
|
// handle feature index, see torch's BatchNorm1d, BatchNorm2d, BatchNorm3d,
|
|
|
|
// all of NC, NCL, NCHW, NCDHW's feature index is 1.
|
|
|
|
int64_t feature_index = 1;
|
|
|
|
|
2022-08-02 12:53:24 +08:00
|
|
|
if (!inputTy.getElementType().template isa<mlir::FloatType>()) {
|
|
|
|
return op.emitError("only input tensor of float type is supported");
|
|
|
|
}
|
|
|
|
auto inputElemTy = inputTy.getElementType().cast<mlir::FloatType>();
|
|
|
|
|
|
|
|
Value channelDim = rewriter.create<tensor::DimOp>(op->getLoc(), input, 1);
|
|
|
|
|
2022-09-01 10:36:02 +08:00
|
|
|
if (options.dimSizeIndexBits == 32) {
|
|
|
|
auto channelDimI64 = rewriter.create<mlir::arith::IndexCastOp>(
|
|
|
|
op->getLoc(), rewriter.getI64Type(), channelDim);
|
|
|
|
channelDim = rewriter.create<arith::TruncIOp>(
|
|
|
|
op->getLoc(), rewriter.getI32Type(), channelDimI64);
|
|
|
|
}
|
2022-08-02 12:53:24 +08:00
|
|
|
|
|
|
|
Value channelShape = rewriter.create<tensor::FromElementsOp>(
|
|
|
|
op->getLoc(), ValueRange{channelDim});
|
2022-07-27 13:07:51 +08:00
|
|
|
if (failed(checkNotNone(rewriter, op, weight))) {
|
2023-02-02 21:29:47 +08:00
|
|
|
weight = hlo::getConstantOfShape(
|
2022-08-02 12:53:24 +08:00
|
|
|
rewriter, op->getLoc(), APFloat(inputElemTy.getFloatSemantics(), 1),
|
|
|
|
channelShape,
|
|
|
|
RankedTensorType::get({inputTy.getShape()[1]},
|
|
|
|
inputTy.getElementType()));
|
2022-07-27 13:07:51 +08:00
|
|
|
}
|
|
|
|
if (failed(checkNotNone(rewriter, op, bias))) {
|
2023-02-02 21:29:47 +08:00
|
|
|
bias = hlo::getConstantOfShape(
|
2022-08-02 12:53:24 +08:00
|
|
|
rewriter, op->getLoc(), APFloat(inputElemTy.getFloatSemantics(), 0),
|
|
|
|
channelShape,
|
|
|
|
RankedTensorType::get({inputTy.getShape()[1]},
|
|
|
|
inputTy.getElementType()));
|
2022-07-27 13:07:51 +08:00
|
|
|
}
|
|
|
|
if (failed(checkNotNone(rewriter, op, runningVar))) {
|
2023-02-02 21:29:47 +08:00
|
|
|
runningVar = hlo::getConstantOfShape(
|
2022-08-02 12:53:24 +08:00
|
|
|
rewriter, op->getLoc(), APFloat(inputElemTy.getFloatSemantics(), 1),
|
|
|
|
channelShape,
|
|
|
|
RankedTensorType::get({inputTy.getShape()[1]},
|
|
|
|
inputTy.getElementType()));
|
2022-07-27 13:07:51 +08:00
|
|
|
}
|
|
|
|
if (failed(checkNotNone(rewriter, op, runningMean))) {
|
2023-02-02 21:29:47 +08:00
|
|
|
runningMean = hlo::getConstantOfShape(
|
2022-08-02 12:53:24 +08:00
|
|
|
rewriter, op->getLoc(), APFloat(inputElemTy.getFloatSemantics(), 0),
|
|
|
|
channelShape,
|
|
|
|
RankedTensorType::get({inputTy.getShape()[1]},
|
|
|
|
inputTy.getElementType()));
|
2022-07-27 13:07:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
auto weightTy = weight.getType().cast<RankedTensorType>();
|
|
|
|
auto biasTy = bias.getType().cast<RankedTensorType>();
|
|
|
|
auto runningMeanTy = runningMean.getType().cast<RankedTensorType>();
|
|
|
|
auto runningVarTy = runningVar.getType().cast<RankedTensorType>();
|
2022-08-02 12:53:24 +08:00
|
|
|
|
2022-07-27 13:07:51 +08:00
|
|
|
if (weightTy.getRank() != 1 || biasTy.getRank() != 1 ||
|
|
|
|
runningMeanTy.getRank() != 1 || runningVarTy.getRank() != 1) {
|
|
|
|
return rewriter.notifyMatchFailure(
|
|
|
|
op, "expect weight, bias, running_mean and running_var to be rank 1");
|
|
|
|
}
|
2022-08-02 12:53:24 +08:00
|
|
|
if (!weightTy.getElementType().template isa<mlir::FloatType>() ||
|
2022-07-27 13:07:51 +08:00
|
|
|
!biasTy.getElementType().template isa<mlir::FloatType>() ||
|
|
|
|
!runningMeanTy.getElementType().template isa<mlir::FloatType>() ||
|
|
|
|
!runningVarTy.getElementType().template isa<mlir::FloatType>()) {
|
2022-08-02 12:53:24 +08:00
|
|
|
return op.emitError("only float weight/bias/runningMean/runningVar tensor "
|
|
|
|
"of float type is supported");
|
2022-07-27 13:07:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
double eps = 0.0;
|
2022-12-08 04:20:41 +08:00
|
|
|
if (!matchPattern(op.getEps(), m_TorchConstantFloat(&eps))) {
|
2022-07-27 13:07:51 +08:00
|
|
|
return rewriter.notifyMatchFailure(op, "non-float(double) eps unsupported");
|
|
|
|
}
|
|
|
|
bool training = false;
|
2022-12-08 04:20:41 +08:00
|
|
|
if (!matchPattern(op.getTraining(), m_TorchConstantBool(&training))) {
|
2022-07-27 13:07:51 +08:00
|
|
|
return rewriter.notifyMatchFailure(op, "non-bool training unsupported");
|
|
|
|
}
|
|
|
|
// TODO: handle cudnnEnabled parameter. Here, we just ignore it!
|
|
|
|
bool cudnnEnabled = false;
|
2022-12-08 04:20:41 +08:00
|
|
|
if (!matchPattern(op.getCudnnEnabled(), m_TorchConstantBool(&cudnnEnabled))) {
|
2022-07-27 13:07:51 +08:00
|
|
|
return rewriter.notifyMatchFailure(op,
|
|
|
|
"non-bool cudnn_enabled unsupported");
|
|
|
|
}
|
|
|
|
if (training) {
|
|
|
|
Type outputTy = getTypeConverter()->convertType(op.getType());
|
|
|
|
Type batchMeanOrVarTy =
|
|
|
|
RankedTensorType::get(weightTy.getShape(), inputTy.getElementType());
|
2023-02-02 21:29:47 +08:00
|
|
|
auto batchNormTrainingResult =
|
|
|
|
rewriter.create<stablehlo::BatchNormTrainingOp>(
|
|
|
|
op.getLoc(), outputTy, batchMeanOrVarTy, batchMeanOrVarTy, input,
|
|
|
|
weight, bias, rewriter.getF32FloatAttr(eps),
|
2023-05-18 00:04:40 +08:00
|
|
|
rewriter.getI64IntegerAttr(feature_index));
|
2022-07-27 13:07:51 +08:00
|
|
|
rewriter.replaceOp(op, batchNormTrainingResult.getResult(0));
|
|
|
|
return success();
|
|
|
|
} else {
|
|
|
|
Type outputTy = getTypeConverter()->convertType(op.getType());
|
2022-09-23 20:50:29 +08:00
|
|
|
SmallVector<int64_t, 4> castShape{inputTy.getShape().begin(),
|
|
|
|
inputTy.getShape().end()};
|
|
|
|
castShape[1] = weightTy.getShape()[0];
|
|
|
|
auto castTy = RankedTensorType::get(castShape, inputTy.getElementType());
|
2023-02-02 21:29:47 +08:00
|
|
|
// Feature counts must match among operands of
|
|
|
|
// stablehlo::BatchNormInferenceOp.
|
2022-09-23 20:50:29 +08:00
|
|
|
Value inputCasted =
|
|
|
|
rewriter.create<tensor::CastOp>(op.getLoc(), castTy, input);
|
2023-02-02 21:29:47 +08:00
|
|
|
Value output = rewriter.create<stablehlo::BatchNormInferenceOp>(
|
2022-09-23 20:50:29 +08:00
|
|
|
op.getLoc(), inputCasted.getType(), inputCasted, weight, bias,
|
|
|
|
runningMean, runningVar,
|
|
|
|
// 'epsilon' must satisfy constraint: 32-bit float attribute.
|
2023-05-18 00:04:40 +08:00
|
|
|
rewriter.getF32FloatAttr(eps),
|
|
|
|
rewriter.getI64IntegerAttr(feature_index));
|
2022-09-23 20:50:29 +08:00
|
|
|
rewriter.replaceOpWithNewOp<tensor::CastOp>(op, outputTy, output);
|
2022-07-27 13:07:51 +08:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AtenNativeLayerNormOp
|
|
|
|
template <>
|
|
|
|
LogicalResult ConvertAtenOp<AtenNativeLayerNormOp>::matchAndRewrite(
|
|
|
|
AtenNativeLayerNormOp op, OpAdaptor adaptor,
|
|
|
|
ConversionPatternRewriter &rewriter) const {
|
2022-12-08 04:20:41 +08:00
|
|
|
Value input = adaptor.getInput();
|
2022-07-27 13:07:51 +08:00
|
|
|
auto inputTy = input.getType().cast<RankedTensorType>();
|
|
|
|
auto inputShape = inputTy.getShape();
|
|
|
|
auto inputRank = inputTy.getRank();
|
2022-12-08 04:20:41 +08:00
|
|
|
Value weight = adaptor.getWeight();
|
|
|
|
Value bias = adaptor.getBias();
|
2022-07-27 13:07:51 +08:00
|
|
|
|
2022-08-02 12:53:24 +08:00
|
|
|
if (!inputTy.hasStaticShape()) {
|
|
|
|
return op->emitError("dynamic shaped input is not supported");
|
|
|
|
}
|
|
|
|
|
2022-07-27 13:07:51 +08:00
|
|
|
SmallVector<int64_t> normalizedShape;
|
2022-12-08 04:20:41 +08:00
|
|
|
if (!matchPattern(op.getNormalizedShape(),
|
2022-11-17 04:33:12 +08:00
|
|
|
m_TorchListOfConstantInts(normalizedShape))) {
|
2022-07-27 13:07:51 +08:00
|
|
|
return rewriter.notifyMatchFailure(
|
|
|
|
op, "normalized_shape must be a list of const int");
|
|
|
|
}
|
|
|
|
double eps = 0;
|
2022-12-08 04:20:41 +08:00
|
|
|
if (!matchPattern(op.getEps(), m_TorchConstantFloat(&eps))) {
|
2022-08-02 12:53:24 +08:00
|
|
|
return rewriter.notifyMatchFailure(op,
|
|
|
|
"non const float eps is unsupported");
|
2022-07-27 13:07:51 +08:00
|
|
|
}
|
|
|
|
if (failed(checkNotNone(rewriter, op, weight)) ||
|
|
|
|
failed(checkNotNone(rewriter, op, bias))) {
|
2022-08-02 12:53:24 +08:00
|
|
|
return op->emitError("none weight or bias is unsupported");
|
2022-07-27 13:07:51 +08:00
|
|
|
}
|
|
|
|
auto weightTy = weight.getType().cast<RankedTensorType>();
|
|
|
|
auto biasTy = bias.getType().cast<RankedTensorType>();
|
|
|
|
|
|
|
|
if (!inputTy.getElementType().isa<mlir::FloatType>() ||
|
|
|
|
!biasTy.getElementType().isa<mlir::FloatType>() ||
|
|
|
|
!weightTy.getElementType().isa<mlir::FloatType>()) {
|
2022-08-02 12:53:24 +08:00
|
|
|
return op->emitError("currently only float data type are supported");
|
2022-07-27 13:07:51 +08:00
|
|
|
}
|
|
|
|
int64_t normalizedShapeRank = normalizedShape.size();
|
|
|
|
if (weightTy.getRank() != normalizedShapeRank ||
|
|
|
|
biasTy.getRank() != normalizedShapeRank ||
|
|
|
|
inputRank < normalizedShapeRank || normalizedShapeRank < 1) {
|
2022-08-02 12:53:24 +08:00
|
|
|
return rewriter.notifyMatchFailure(op, "input or weight or bias shape or"
|
2022-07-27 13:07:51 +08:00
|
|
|
"normalized shape not compatible");
|
|
|
|
}
|
|
|
|
for (int64_t i = 1; i <= normalizedShapeRank; i++) {
|
|
|
|
if (inputShape[inputRank - i] != normalizedShape[normalizedShapeRank - i] ||
|
|
|
|
weightTy.getShape()[normalizedShapeRank - i] !=
|
|
|
|
normalizedShape[normalizedShapeRank - i] ||
|
|
|
|
biasTy.getShape()[normalizedShapeRank - i] !=
|
|
|
|
normalizedShape[normalizedShapeRank - i]) {
|
|
|
|
return op.emitError("mismatching contracting dimension");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-02 12:53:24 +08:00
|
|
|
// Flatten dims to fit batch_norm operation.
|
2022-07-27 13:07:51 +08:00
|
|
|
int64_t numFeatureDimSize = 1;
|
|
|
|
int64_t numEmbeddingDimSize = 1;
|
|
|
|
for (int64_t i = 0; i < inputRank - normalizedShapeRank; i++) {
|
|
|
|
numFeatureDimSize *= inputShape[i];
|
|
|
|
}
|
|
|
|
for (int64_t i = 0; i < normalizedShapeRank; i++) {
|
|
|
|
numEmbeddingDimSize *= normalizedShape[i];
|
|
|
|
}
|
|
|
|
SmallVector<int64_t> inputFlattenShape{1, numFeatureDimSize,
|
|
|
|
numEmbeddingDimSize};
|
2023-02-02 21:29:47 +08:00
|
|
|
SmallVector<int64_t> meanOrVarStablehloOutShape{numFeatureDimSize};
|
2022-07-27 13:07:51 +08:00
|
|
|
|
2023-02-02 21:29:47 +08:00
|
|
|
auto stablehloBatchNormOutTy =
|
2022-07-27 13:07:51 +08:00
|
|
|
RankedTensorType::get(inputFlattenShape, inputTy.getElementType());
|
2023-02-02 21:29:47 +08:00
|
|
|
auto stablehloBathNormOutMeanOrVarTy = RankedTensorType::get(
|
|
|
|
meanOrVarStablehloOutShape, inputTy.getElementType());
|
2022-07-27 13:07:51 +08:00
|
|
|
|
2022-08-02 12:53:24 +08:00
|
|
|
// Reshape input
|
2023-02-02 21:29:47 +08:00
|
|
|
auto stablehloInput = rewriter.create<stablehlo::DynamicReshapeOp>(
|
|
|
|
op->getLoc(), stablehloBatchNormOutTy, input,
|
|
|
|
hlo::getConstTensor(rewriter, op, llvm::ArrayRef(inputFlattenShape),
|
|
|
|
{static_cast<int64_t>(inputFlattenShape.size())})
|
2022-08-09 11:17:35 +08:00
|
|
|
.value());
|
2022-07-27 13:07:51 +08:00
|
|
|
|
2023-02-02 21:29:47 +08:00
|
|
|
// Generate "scale" and "offset" Value for stablehlo.BatchNormTrainingOp.
|
2022-07-27 13:07:51 +08:00
|
|
|
SmallVector<APFloat> zeroConstVec(
|
|
|
|
numFeatureDimSize, APFloat::getZero(inputTy.getElementType()
|
|
|
|
.cast<mlir::FloatType>()
|
|
|
|
.getFloatSemantics()));
|
|
|
|
SmallVector<APFloat> oneConstVec(
|
|
|
|
numFeatureDimSize,
|
|
|
|
APFloat(
|
|
|
|
inputTy.getElementType().cast<mlir::FloatType>().getFloatSemantics(),
|
|
|
|
1));
|
|
|
|
auto oneOrZeroConstType =
|
|
|
|
RankedTensorType::get({numFeatureDimSize}, inputTy.getElementType());
|
|
|
|
|
2023-02-02 21:29:47 +08:00
|
|
|
Value scale = rewriter.create<stablehlo::ConstantOp>(
|
2022-07-27 13:07:51 +08:00
|
|
|
op->getLoc(), oneOrZeroConstType,
|
|
|
|
DenseElementsAttr::get(oneOrZeroConstType, oneConstVec));
|
2023-02-02 21:29:47 +08:00
|
|
|
Value offset = rewriter.create<stablehlo::ConstantOp>(
|
2022-07-27 13:07:51 +08:00
|
|
|
op->getLoc(), oneOrZeroConstType,
|
|
|
|
DenseElementsAttr::get(oneOrZeroConstType, zeroConstVec));
|
2023-02-02 21:29:47 +08:00
|
|
|
auto batchNormTrainingResult =
|
|
|
|
rewriter.create<stablehlo::BatchNormTrainingOp>(
|
|
|
|
op->getLoc(), stablehloBatchNormOutTy,
|
|
|
|
stablehloBathNormOutMeanOrVarTy, stablehloBathNormOutMeanOrVarTy,
|
|
|
|
stablehloInput, scale, offset, rewriter.getF32FloatAttr(eps),
|
|
|
|
rewriter.getI64IntegerAttr(1));
|
2022-07-27 13:07:51 +08:00
|
|
|
|
2022-08-02 12:53:24 +08:00
|
|
|
// Reshape back
|
2022-07-27 13:07:51 +08:00
|
|
|
auto outputTy =
|
|
|
|
getTypeConverter()->convertType(op.getType(0)).cast<RankedTensorType>();
|
|
|
|
auto outputMeanOrVarTy =
|
|
|
|
getTypeConverter()->convertType(op.getType(1)).cast<RankedTensorType>();
|
|
|
|
|
2023-02-02 21:29:47 +08:00
|
|
|
auto output = rewriter.create<stablehlo::DynamicReshapeOp>(
|
2022-07-27 13:07:51 +08:00
|
|
|
op->getLoc(), outputTy, batchNormTrainingResult.getResult(0),
|
2023-02-02 21:29:47 +08:00
|
|
|
hlo::getConstTensor(rewriter, op, outputTy.getShape(),
|
|
|
|
{static_cast<int64_t>(outputTy.getShape().size())})
|
2022-08-09 11:17:35 +08:00
|
|
|
.value());
|
2023-02-02 21:29:47 +08:00
|
|
|
auto mean = rewriter.create<stablehlo::DynamicReshapeOp>(
|
2022-07-27 13:07:51 +08:00
|
|
|
op->getLoc(), outputMeanOrVarTy, batchNormTrainingResult.getResult(1),
|
2023-02-02 21:29:47 +08:00
|
|
|
hlo::getConstTensor(
|
2022-07-27 13:07:51 +08:00
|
|
|
rewriter, op, outputMeanOrVarTy.getShape(),
|
|
|
|
{static_cast<int64_t>(outputMeanOrVarTy.getShape().size())})
|
2022-08-09 11:17:35 +08:00
|
|
|
.value());
|
2023-02-02 21:29:47 +08:00
|
|
|
auto var = rewriter.create<stablehlo::DynamicReshapeOp>(
|
2022-07-27 13:07:51 +08:00
|
|
|
op->getLoc(), outputMeanOrVarTy, batchNormTrainingResult.getResult(2),
|
2023-02-02 21:29:47 +08:00
|
|
|
hlo::getConstTensor(
|
2022-07-27 13:07:51 +08:00
|
|
|
rewriter, op, outputMeanOrVarTy.getShape(),
|
|
|
|
{static_cast<int64_t>(outputMeanOrVarTy.getShape().size())})
|
2022-08-09 11:17:35 +08:00
|
|
|
.value());
|
2022-07-27 13:07:51 +08:00
|
|
|
|
|
|
|
// Apply affine transform: output x weight + bias [element-wise]
|
2023-02-02 21:29:47 +08:00
|
|
|
auto bcastedWeight = hlo::promoteAndBroadcast(rewriter, weight, outputTy);
|
|
|
|
auto bcastedBias = hlo::promoteAndBroadcast(rewriter, bias, outputTy);
|
2022-07-27 13:07:51 +08:00
|
|
|
auto outputMulWeight =
|
2023-02-02 21:29:47 +08:00
|
|
|
rewriter.create<stablehlo::MulOp>(op->getLoc(), output, bcastedWeight);
|
|
|
|
auto finalOuput = rewriter.create<stablehlo::AddOp>(
|
|
|
|
op->getLoc(), outputMulWeight, bcastedBias);
|
2022-07-27 13:07:51 +08:00
|
|
|
rewriter.replaceOp(op, {finalOuput, mean, var});
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2022-08-10 13:12:34 +08:00
|
|
|
// AtenCatOp
|
|
|
|
template <>
|
|
|
|
LogicalResult ConvertAtenOp<AtenCatOp>::matchAndRewrite(
|
|
|
|
AtenCatOp op, OpAdaptor adaptor,
|
|
|
|
ConversionPatternRewriter &rewriter) const {
|
|
|
|
auto outType =
|
|
|
|
getTypeConverter()->convertType(op.getType()).cast<RankedTensorType>();
|
|
|
|
int64_t dim;
|
2022-12-08 04:20:41 +08:00
|
|
|
if (!matchPattern(op.getDim(), m_TorchConstantInt(&dim))) {
|
2022-08-10 13:12:34 +08:00
|
|
|
return rewriter.notifyMatchFailure(op,
|
|
|
|
"only constant dim param is supported");
|
|
|
|
}
|
2023-04-07 19:49:35 +08:00
|
|
|
dim = toPositiveDim(dim, outType.getRank());
|
|
|
|
if (!isValidDim(dim, outType.getRank()))
|
|
|
|
return rewriter.notifyMatchFailure(op, "dim is statically invalid");
|
2022-08-10 13:12:34 +08:00
|
|
|
|
|
|
|
SmallVector<Value> torchTensors;
|
2022-12-08 04:20:41 +08:00
|
|
|
if (!getListConstructElements(op.getTensors(), torchTensors)) {
|
2022-08-10 13:12:34 +08:00
|
|
|
return rewriter.notifyMatchFailure(
|
|
|
|
op, "input should comes from a PrimListConstructOp");
|
|
|
|
}
|
|
|
|
SmallVector<Value> builtinTensors = getTypeConvertedValues(
|
|
|
|
rewriter, op->getLoc(), getTypeConverter(), torchTensors);
|
|
|
|
|
|
|
|
// Promote type
|
|
|
|
for (auto &v : builtinTensors) {
|
2023-02-02 21:29:47 +08:00
|
|
|
v = hlo::promoteType(rewriter, v, outType);
|
2022-08-10 13:12:34 +08:00
|
|
|
}
|
|
|
|
|
2023-02-02 21:29:47 +08:00
|
|
|
rewriter.replaceOpWithNewOp<stablehlo::ConcatenateOp>(
|
2023-04-07 19:49:35 +08:00
|
|
|
op, outType, ValueRange(builtinTensors), dim);
|
2022-08-23 16:47:21 +08:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
|
|
|
// AtenNumelOp
|
|
|
|
template <>
|
|
|
|
LogicalResult ConvertAtenOp<AtenNumelOp>::matchAndRewrite(
|
2022-09-16 15:09:21 +08:00
|
|
|
AtenNumelOp op, OpAdaptor adaptor,
|
|
|
|
ConversionPatternRewriter &rewriter) const {
|
2022-12-08 04:20:41 +08:00
|
|
|
auto self = adaptor.getSelf();
|
2022-08-23 16:47:21 +08:00
|
|
|
auto selfTy = self.getType().dyn_cast<RankedTensorType>();
|
|
|
|
size_t rank = selfTy.getRank();
|
|
|
|
|
2022-09-01 10:36:02 +08:00
|
|
|
Type intType = rewriter.getIntegerType(options.dimSizeIndexBits);
|
2022-08-23 16:47:21 +08:00
|
|
|
auto loc = op->getLoc();
|
2022-09-16 15:09:21 +08:00
|
|
|
Value numel = rewriter.create<arith::ConstantOp>(
|
|
|
|
loc, rewriter.getIntegerAttr(intType, 1));
|
|
|
|
for (size_t d = 0; d < rank; ++d) {
|
|
|
|
Value dimSize = rewriter.create<arith::IndexCastOp>(
|
2022-08-23 16:47:21 +08:00
|
|
|
loc, intType, rewriter.create<tensor::DimOp>(loc, self, d));
|
2022-09-16 15:09:21 +08:00
|
|
|
numel = rewriter.create<arith::MulIOp>(loc, numel, dimSize);
|
2022-08-23 16:47:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
auto outTy = getTypeConverter()->convertType(op.getType());
|
|
|
|
if (outTy != numel.getType()) {
|
2022-09-16 15:09:21 +08:00
|
|
|
rewriter.replaceOpWithNewOp<arith::ExtSIOp>(op, outTy, numel);
|
2022-08-23 16:47:21 +08:00
|
|
|
} else {
|
|
|
|
rewriter.replaceOp(op, numel);
|
|
|
|
}
|
2022-08-10 13:12:34 +08:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2022-09-16 15:09:21 +08:00
|
|
|
// AtenClampOp
|
|
|
|
template <>
|
|
|
|
LogicalResult ConvertAtenOp<AtenClampOp>::matchAndRewrite(
|
|
|
|
AtenClampOp op, OpAdaptor adaptor,
|
|
|
|
ConversionPatternRewriter &rewriter) const {
|
2022-12-08 04:20:41 +08:00
|
|
|
Value input = adaptor.getSelf();
|
2022-09-16 15:09:21 +08:00
|
|
|
auto inputType = input.getType().cast<RankedTensorType>();
|
|
|
|
auto inputElemType = inputType.getElementType();
|
2022-12-08 04:20:41 +08:00
|
|
|
Value minValue = adaptor.getMin();
|
|
|
|
Value maxValue = adaptor.getMax();
|
2022-09-16 15:09:21 +08:00
|
|
|
if (failed(checkNotNone(rewriter, op, minValue)) &&
|
|
|
|
failed(checkNotNone(rewriter, op, maxValue))) {
|
|
|
|
return rewriter.notifyMatchFailure(
|
|
|
|
op, "this op should be folded as its `min` and `max` both are none");
|
|
|
|
} else if (failed(checkNotNone(rewriter, op, minValue))) {
|
2023-02-02 21:29:47 +08:00
|
|
|
maxValue =
|
|
|
|
hlo::scalarToStablehloTensor(rewriter, op, maxValue, inputElemType);
|
2022-09-16 15:09:21 +08:00
|
|
|
auto minInfo = getMinValueOfDtype(op, inputElemType, rewriter);
|
|
|
|
if (failed(minInfo)) {
|
|
|
|
return rewriter.notifyMatchFailure(
|
|
|
|
op, "failed to generate min value of dtype");
|
|
|
|
}
|
|
|
|
minValue = *minInfo;
|
|
|
|
} else if (failed(checkNotNone(rewriter, op, maxValue))) {
|
2023-02-02 21:29:47 +08:00
|
|
|
minValue =
|
|
|
|
hlo::scalarToStablehloTensor(rewriter, op, minValue, inputElemType);
|
2022-09-16 15:09:21 +08:00
|
|
|
auto maxInfo = getMaxValueOfDtype(op, inputElemType, rewriter);
|
|
|
|
if (failed(maxInfo)) {
|
|
|
|
return rewriter.notifyMatchFailure(
|
|
|
|
op, "failed to generate max value of dtype");
|
|
|
|
}
|
|
|
|
maxValue = *maxInfo;
|
|
|
|
} else {
|
2023-02-02 21:29:47 +08:00
|
|
|
minValue =
|
|
|
|
hlo::scalarToStablehloTensor(rewriter, op, minValue, inputElemType);
|
|
|
|
maxValue =
|
|
|
|
hlo::scalarToStablehloTensor(rewriter, op, maxValue, inputElemType);
|
2022-09-16 15:09:21 +08:00
|
|
|
}
|
2023-02-02 21:29:47 +08:00
|
|
|
rewriter.replaceOpWithNewOp<stablehlo::ClampOp>(op, minValue, input,
|
|
|
|
maxValue);
|
2022-09-16 15:09:21 +08:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2022-09-20 22:31:24 +08:00
|
|
|
// AtenArangeStartStepOp
|
|
|
|
// aten.arange.start_step = range(ceil((end-start)/step)) * step + start.
|
|
|
|
template <>
|
|
|
|
LogicalResult ConvertAtenOp<AtenArangeStartStepOp>::matchAndRewrite(
|
|
|
|
AtenArangeStartStepOp op, OpAdaptor adaptor,
|
|
|
|
ConversionPatternRewriter &rewriter) const {
|
|
|
|
Location loc = op->getLoc();
|
|
|
|
|
|
|
|
// Get element type of resultType as dtype
|
|
|
|
auto outType = this->getTypeConverter()
|
|
|
|
->convertType(op.getType())
|
|
|
|
.cast<RankedTensorType>();
|
|
|
|
auto dtype = outType.getElementType();
|
|
|
|
if (!dtype.isa<mlir::IntegerType>() && !dtype.isa<mlir::FloatType>()) {
|
|
|
|
return rewriter.notifyMatchFailure(
|
|
|
|
op, "unimplemented: only int or float dtype supported");
|
|
|
|
}
|
|
|
|
|
2023-02-02 21:29:47 +08:00
|
|
|
Value start =
|
|
|
|
hlo::scalarToStablehloTensor(rewriter, op, adaptor.getStart(), dtype);
|
|
|
|
Value end =
|
|
|
|
hlo::scalarToStablehloTensor(rewriter, op, adaptor.getEnd(), dtype);
|
|
|
|
Value step =
|
|
|
|
hlo::scalarToStablehloTensor(rewriter, op, adaptor.getStep(), dtype);
|
2022-09-20 22:31:24 +08:00
|
|
|
|
|
|
|
// Get length of the 1-d output tensor
|
2023-02-02 21:29:47 +08:00
|
|
|
Value subOut = rewriter.create<stablehlo::SubtractOp>(loc, end, start);
|
|
|
|
Value divOut = rewriter.create<stablehlo::DivOp>(loc, subOut, step);
|
2022-09-20 22:31:24 +08:00
|
|
|
|
2023-02-02 21:29:47 +08:00
|
|
|
Value resultLength = rewriter.create<stablehlo::ReshapeOp>(
|
2022-09-20 22:31:24 +08:00
|
|
|
loc, RankedTensorType::get({1}, dtype), divOut);
|
|
|
|
if (dtype.isa<mlir::FloatType>()) {
|
2023-02-02 21:29:47 +08:00
|
|
|
resultLength = rewriter.create<stablehlo::CeilOp>(loc, resultLength);
|
|
|
|
resultLength = rewriter.create<stablehlo::ConvertOp>(
|
2022-09-20 22:31:24 +08:00
|
|
|
loc, RankedTensorType::get({1}, rewriter.getI64Type()), resultLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value window =
|
2023-02-02 21:29:47 +08:00
|
|
|
rewriter.create<stablehlo::DynamicIotaOp>(loc, outType, resultLength, 0);
|
2022-09-20 22:31:24 +08:00
|
|
|
DenseIntElementsAttr broadcastDimensions;
|
|
|
|
Value mulOut = rewriter.create<chlo::BroadcastMulOp>(loc, window, step,
|
|
|
|
broadcastDimensions);
|
|
|
|
rewriter.replaceOpWithNewOp<chlo::BroadcastAddOp>(op, mulOut, start,
|
|
|
|
broadcastDimensions);
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2022-12-21 20:09:43 +08:00
|
|
|
template <>
|
|
|
|
LogicalResult ConvertAtenOp<AtenGeluBackwardOp>::matchAndRewrite(
|
|
|
|
AtenGeluBackwardOp op, OpAdaptor adaptor,
|
|
|
|
ConversionPatternRewriter &rewriter) const {
|
|
|
|
Location loc = op.getLoc();
|
|
|
|
Value input = adaptor.getSelf();
|
2023-02-02 21:29:47 +08:00
|
|
|
auto outType =
|
|
|
|
this->getTypeConverter()->convertType(op.getType()).cast<TensorType>();
|
2022-12-21 20:09:43 +08:00
|
|
|
if (!outType) {
|
|
|
|
return op.emitError("only tensor type is supported");
|
|
|
|
}
|
|
|
|
// TODO: Handle approximate.
|
|
|
|
std::string approximate;
|
|
|
|
if (!matchPattern(op.getApproximate(), m_TorchConstantStr(approximate)) ||
|
|
|
|
approximate != "none") {
|
|
|
|
return rewriter.notifyMatchFailure(op, "Unsupported value of approximate");
|
|
|
|
}
|
|
|
|
// Create constant value
|
|
|
|
Value kAlpha =
|
|
|
|
chlo::getConstantLike(rewriter, loc, 0.70710678118654752440, input);
|
|
|
|
Value cstAlpha0 =
|
|
|
|
chlo::getConstantLike(rewriter, loc, 1.12837916709551257390, input);
|
|
|
|
Value half = chlo::getConstantLike(rewriter, loc, .5, input);
|
|
|
|
Value one = chlo::getConstantLike(rewriter, loc, 1.0, input);
|
|
|
|
Value negHalf = chlo::getConstantLike(rewriter, loc, -0.5, input);
|
|
|
|
|
|
|
|
// Compute
|
2023-02-02 21:29:47 +08:00
|
|
|
Value kBeta0 =
|
|
|
|
rewriter.create<stablehlo::MulOp>(loc, outType, kAlpha, cstAlpha0);
|
|
|
|
Value kBeta = rewriter.create<stablehlo::MulOp>(loc, outType, kBeta0, half);
|
|
|
|
Value erfArg = rewriter.create<stablehlo::MulOp>(loc, outType, kAlpha,
|
|
|
|
adaptor.getSelf());
|
2022-12-21 20:09:43 +08:00
|
|
|
Value erf = rewriter.create<mlir::chlo::ErfOp>(loc, outType, erfArg);
|
2023-02-02 21:29:47 +08:00
|
|
|
Value erfAdd = rewriter.create<stablehlo::AddOp>(loc, outType, erf, one);
|
|
|
|
Value cdf = rewriter.create<stablehlo::MulOp>(loc, outType, erfAdd, half);
|
|
|
|
Value inputSquared = rewriter.create<stablehlo::MulOp>(
|
2022-12-21 20:09:43 +08:00
|
|
|
loc, outType, adaptor.getSelf(), adaptor.getSelf());
|
|
|
|
Value negHalfInputSquared =
|
2023-02-02 21:29:47 +08:00
|
|
|
rewriter.create<stablehlo::MulOp>(loc, outType, inputSquared, negHalf);
|
2022-12-21 20:09:43 +08:00
|
|
|
Value expRes =
|
2023-02-02 21:29:47 +08:00
|
|
|
rewriter.create<stablehlo::ExpOp>(loc, outType, negHalfInputSquared);
|
|
|
|
Value pdf = rewriter.create<stablehlo::MulOp>(loc, outType, kBeta, expRes);
|
2022-12-21 20:09:43 +08:00
|
|
|
Value pdfTimesInput =
|
2023-02-02 21:29:47 +08:00
|
|
|
rewriter.create<stablehlo::MulOp>(loc, outType, pdf, adaptor.getSelf());
|
2022-12-21 20:09:43 +08:00
|
|
|
Value pdfTimesInputAddCdf =
|
2023-02-02 21:29:47 +08:00
|
|
|
rewriter.create<stablehlo::AddOp>(loc, outType, pdfTimesInput, cdf);
|
|
|
|
rewriter.replaceOpWithNewOp<stablehlo::MulOp>(
|
|
|
|
op, outType, adaptor.getGradOutput(), pdfTimesInputAddCdf);
|
2022-12-21 20:09:43 +08:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2023-05-05 00:55:03 +08:00
|
|
|
template <>
|
|
|
|
LogicalResult ConvertAtenOp<AtenPowTensorTensorOp>::matchAndRewrite(
|
|
|
|
AtenPowTensorTensorOp op, OpAdaptor adaptor,
|
|
|
|
ConversionPatternRewriter &rewriter) const {
|
|
|
|
Value lhs = adaptor.getSelf();
|
|
|
|
auto lhsTy = lhs.getType().cast<TensorType>();
|
|
|
|
Value rhs = adaptor.getExponent();
|
|
|
|
auto rhsTy = rhs.getType().cast<TensorType>();
|
|
|
|
|
|
|
|
if (!lhsTy || !rhsTy)
|
|
|
|
return op.emitError("only Tensor types supported");
|
|
|
|
|
|
|
|
auto outTy =
|
|
|
|
this->getTypeConverter()->convertType(op.getType()).cast<TensorType>();
|
|
|
|
|
|
|
|
lhs = hlo::promoteType(rewriter, lhs, outTy);
|
|
|
|
rhs = hlo::promoteType(rewriter, rhs, outTy);
|
|
|
|
|
|
|
|
rewriter.replaceOpWithNewOp<chlo::BroadcastPowOp>(op, outTy, lhs, rhs,
|
|
|
|
/*broadcast_attr*/ nullptr);
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2023-05-19 10:07:35 +08:00
|
|
|
// Converts `aten.empty.memory_format` to `tensor.empty` op.
|
|
|
|
template <>
|
|
|
|
LogicalResult ConvertAtenOp<AtenEmptyMemoryFormatOp>::matchAndRewrite(
|
|
|
|
AtenEmptyMemoryFormatOp op, OpAdaptor adaptor,
|
|
|
|
ConversionPatternRewriter &rewriter) const {
|
|
|
|
|
|
|
|
// TODO: Add support pin_memory and memory_format features.
|
|
|
|
// At this point all tensors should have value semantics, and hence the
|
|
|
|
// `layout` check can be ignored.
|
|
|
|
|
|
|
|
// The pin_memory should be either `False` or `none`.
|
|
|
|
bool pinMemory;
|
|
|
|
if (!op.getPinMemory().getType().template isa<Torch::NoneType>() &&
|
|
|
|
(!matchPattern(op.getPinMemory(), m_TorchConstantBool(&pinMemory)) ||
|
|
|
|
pinMemory))
|
|
|
|
return rewriter.notifyMatchFailure(
|
|
|
|
op, "unimplemented: pin_memory must be either None or false");
|
|
|
|
|
|
|
|
// Only `none`, `contiguous` and `preserve` memory_format is supported.
|
|
|
|
if (!op.getMemoryFormat().getType().isa<Torch::NoneType>()) {
|
|
|
|
int64_t memoryFormat;
|
|
|
|
if (!matchPattern(op.getMemoryFormat(), m_TorchConstantInt(&memoryFormat)))
|
|
|
|
return rewriter.notifyMatchFailure(
|
|
|
|
op, "unimplemented: the memory format should be specified in "
|
|
|
|
"an integer constant");
|
|
|
|
if (memoryFormat != torch_upstream::MemoryFormat::Contiguous &&
|
|
|
|
memoryFormat != torch_upstream::MemoryFormat::Preserve)
|
|
|
|
return rewriter.notifyMatchFailure(
|
|
|
|
op, "unimplemented: only none, contiguous and preserve "
|
|
|
|
"memory_format is supported");
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Add support for device arg other than cpu.
|
|
|
|
if (!op.getDevice().getType().isa<Torch::NoneType>()) {
|
|
|
|
std::string device;
|
|
|
|
if (!matchPattern(op.getDevice(), m_TorchConstantDevice(device)))
|
|
|
|
return rewriter.notifyMatchFailure(
|
|
|
|
op, "unimplemented: device must be a constant str");
|
|
|
|
else if (device != "cpu")
|
|
|
|
return rewriter.notifyMatchFailure(
|
|
|
|
op, "unimplemented: device is expected to be cpu");
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Add support for non-strided layout.
|
|
|
|
// torch.layout is by default strided i.e. 0.
|
|
|
|
if (!op.getLayout().getType().isa<Torch::NoneType>()) {
|
|
|
|
int64_t tensorLayout;
|
|
|
|
if (!matchPattern(op.getLayout(), m_TorchConstantInt(&tensorLayout)))
|
|
|
|
return rewriter.notifyMatchFailure(
|
|
|
|
op, "unimplemented: layout must be a constant");
|
|
|
|
else if (tensorLayout != torch_upstream::Layout::Strided)
|
|
|
|
return rewriter.notifyMatchFailure(
|
|
|
|
op, "unimplemented: layout is expected to be strided");
|
|
|
|
}
|
|
|
|
|
|
|
|
Location loc = op.getLoc();
|
|
|
|
TypeConverter *typeConverter = this->getTypeConverter();
|
|
|
|
SmallVector<Value> resultSizeTorchInt, resultSize, resultSizeIndex;
|
|
|
|
if (!getListConstructElements(op.getSize(), resultSizeTorchInt)) {
|
|
|
|
return rewriter.notifyMatchFailure(
|
|
|
|
op, "unimplemented: size must be constructed using ListConstruct");
|
|
|
|
}
|
|
|
|
resultSize =
|
|
|
|
getTypeConvertedValues(rewriter, loc, typeConverter, resultSizeTorchInt);
|
|
|
|
for (auto size : resultSize)
|
|
|
|
resultSizeIndex.push_back(castIntToIndex(rewriter, loc, size));
|
|
|
|
|
|
|
|
auto resultType =
|
|
|
|
typeConverter->convertType(op.getType()).cast<RankedTensorType>();
|
|
|
|
Type resultElementType;
|
|
|
|
if (op.getDtype().getType().isa<Torch::NoneType>()) {
|
|
|
|
resultElementType =
|
|
|
|
getDefaultDtypeForTorchScalar(Torch::FloatType::get(op->getContext()));
|
|
|
|
} else {
|
|
|
|
int64_t dtypeInt;
|
|
|
|
if (!matchPattern(op.getDtype(), m_TorchConstantInt(&dtypeInt)))
|
|
|
|
return rewriter.notifyMatchFailure(
|
|
|
|
op, "unimplemented: dtype must be a constant integer or none");
|
|
|
|
FailureOr<Type> maybeResultElementType = getTypeForScalarType(
|
|
|
|
op->getContext(), (torch_upstream::ScalarType)dtypeInt,
|
|
|
|
IntegerType::Signless);
|
|
|
|
if (failed(maybeResultElementType)) {
|
|
|
|
return rewriter.notifyMatchFailure(
|
|
|
|
op, "unable to convert `dtypeInt` to builtin type");
|
|
|
|
}
|
|
|
|
resultElementType = *maybeResultElementType;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create an uninitialized tensor of `resultSize` shape.
|
|
|
|
Value initTensor = rewriter.create<tensor::EmptyOp>(
|
|
|
|
loc, getAsOpFoldResult(resultSizeIndex), resultElementType);
|
|
|
|
rewriter.replaceOpWithNewOp<tensor::CastOp>(op, resultType, initTensor);
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2022-12-22 17:12:52 +08:00
|
|
|
// RuntimeAssertOp
|
|
|
|
namespace {
|
|
|
|
class ConvertRuntimeAssertOp : public OpConversionPattern<RuntimeAssertOp> {
|
|
|
|
public:
|
|
|
|
using OpConversionPattern::OpConversionPattern;
|
|
|
|
|
|
|
|
LogicalResult
|
|
|
|
matchAndRewrite(RuntimeAssertOp op, OpAdaptor adaptor,
|
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
|
|
|
bool condition;
|
|
|
|
if (!matchPattern(op.getCondition(), m_TorchConstantBool(&condition))) {
|
|
|
|
return rewriter.notifyMatchFailure(
|
|
|
|
op, "unimplemented: condition must be a constant");
|
|
|
|
}
|
|
|
|
if (!condition) {
|
|
|
|
return op->emitError("condition must be true");
|
|
|
|
}
|
|
|
|
rewriter.eraseOp(op);
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
2023-05-12 07:41:46 +08:00
|
|
|
template <>
|
|
|
|
LogicalResult ConvertAtenOp<AtenFillScalarOp>::matchAndRewrite(
|
|
|
|
AtenFillScalarOp op, OpAdaptor adaptor,
|
|
|
|
ConversionPatternRewriter &rewriter) const {
|
|
|
|
auto outType =
|
|
|
|
getTypeConverter()->convertType(op.getType()).cast<RankedTensorType>();
|
|
|
|
auto dtype = outType.getElementType();
|
|
|
|
Value scalarTensor =
|
|
|
|
hlo::scalarToStablehloTensor(rewriter, op, adaptor.getValue(), dtype);
|
|
|
|
Value bcastScalar = rewriter.create<stablehlo::BroadcastInDimOp>(
|
|
|
|
op->getLoc(), outType, scalarTensor, rewriter.getI64TensorAttr({}));
|
|
|
|
rewriter.replaceOp(op, bcastScalar);
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2023-02-02 21:29:47 +08:00
|
|
|
void mlir::torch::torch_to_stablehlo::populateBasicOpPatternsAndLegality(
|
2022-07-21 07:18:16 +08:00
|
|
|
TypeConverter &typeConverter, RewritePatternSet &patterns,
|
2023-02-02 21:29:47 +08:00
|
|
|
ConversionTarget &target, const TorchToStablehloOptions &options) {
|
2022-07-21 07:18:16 +08:00
|
|
|
MLIRContext *context = patterns.getContext();
|
|
|
|
|
2022-07-27 13:07:51 +08:00
|
|
|
target.addIllegalOp<AtenTransposeIntOp>();
|
|
|
|
patterns.add<ConvertAtenTransposeIntOp>(typeConverter, context);
|
2022-12-22 17:12:52 +08:00
|
|
|
target.addIllegalOp<RuntimeAssertOp>();
|
|
|
|
patterns.add<ConvertRuntimeAssertOp>(typeConverter, context);
|
2022-07-27 13:07:51 +08:00
|
|
|
|
2023-02-02 21:29:47 +08:00
|
|
|
#define INSERT_UNARY_PATTERN(AtenOp, StablehloOp) \
|
2023-01-12 06:40:03 +08:00
|
|
|
target.addIllegalOp<AtenOp>(); \
|
2023-02-02 21:29:47 +08:00
|
|
|
patterns.add<ConvertAtenUnaryOp<AtenOp, StablehloOp>>(typeConverter, context)
|
|
|
|
INSERT_UNARY_PATTERN(AtenCloneOp, stablehlo::ConvertOp);
|
|
|
|
INSERT_UNARY_PATTERN(AtenNegOp, stablehlo::NegOp);
|
|
|
|
INSERT_UNARY_PATTERN(AtenLogicalNotOp, stablehlo::NotOp);
|
|
|
|
INSERT_UNARY_PATTERN(AtenBitwiseNotOp, stablehlo::NotOp);
|
2023-05-09 13:13:00 +08:00
|
|
|
INSERT_UNARY_PATTERN(AtenAbsOp, stablehlo::AbsOp);
|
2023-01-12 06:40:03 +08:00
|
|
|
#undef INSERT_UNARY_PATTERN
|
|
|
|
|
2023-02-02 21:29:47 +08:00
|
|
|
#define INSERT_UNARY_FPONLY_PATTERN(AtenOp, StablehloOp) \
|
2022-07-27 13:07:51 +08:00
|
|
|
target.addIllegalOp<AtenOp>(); \
|
2023-02-02 21:29:47 +08:00
|
|
|
patterns.add<ConvertAtenUnaryFPOnlyOp<AtenOp, StablehloOp>>(typeConverter, \
|
|
|
|
context)
|
|
|
|
INSERT_UNARY_FPONLY_PATTERN(AtenLogOp, stablehlo::LogOp);
|
|
|
|
INSERT_UNARY_FPONLY_PATTERN(AtenExpOp, stablehlo::ExpOp);
|
|
|
|
INSERT_UNARY_FPONLY_PATTERN(AtenSqrtOp, stablehlo::SqrtOp);
|
|
|
|
INSERT_UNARY_FPONLY_PATTERN(AtenRsqrtOp, stablehlo::RsqrtOp);
|
|
|
|
INSERT_UNARY_FPONLY_PATTERN(AtenSigmoidOp, stablehlo::LogisticOp);
|
2023-02-07 03:14:26 +08:00
|
|
|
INSERT_UNARY_FPONLY_PATTERN(AtenTanhOp, stablehlo::TanhOp);
|
|
|
|
INSERT_UNARY_FPONLY_PATTERN(AtenSinOp, stablehlo::SineOp);
|
|
|
|
INSERT_UNARY_FPONLY_PATTERN(AtenCosOp, stablehlo::CosineOp);
|
|
|
|
INSERT_UNARY_FPONLY_PATTERN(AtenCeilOp, stablehlo::CeilOp);
|
|
|
|
INSERT_UNARY_FPONLY_PATTERN(AtenFloorOp, stablehlo::FloorOp);
|
2022-07-27 13:07:51 +08:00
|
|
|
#undef INSERT_UNARY_FPONLY_PATTERN
|
|
|
|
|
|
|
|
#define INSERT_CONSTANT_FILL_PATTERN(AtenOp, fillVal) \
|
|
|
|
target.addIllegalOp<AtenOp>(); \
|
|
|
|
patterns.add<ConvertAtenConstPatternOp<AtenOp, fillVal>>(typeConverter, \
|
2022-09-01 10:36:02 +08:00
|
|
|
context)
|
2022-07-27 13:07:51 +08:00
|
|
|
INSERT_CONSTANT_FILL_PATTERN(AtenOnesOp, 1);
|
|
|
|
INSERT_CONSTANT_FILL_PATTERN(AtenZerosOp, 0);
|
|
|
|
#undef INSERT_CONSTANT_FILL_PATTERN
|
|
|
|
|
2022-08-02 12:53:24 +08:00
|
|
|
#define INSERT_BINARY_ADDSUB_PATTERN(AtenOp, ChloOp) \
|
2022-07-27 13:07:51 +08:00
|
|
|
target.addIllegalOp<AtenOp>(); \
|
2022-09-01 10:36:02 +08:00
|
|
|
patterns.add<ConvertAtenAddSubOp<AtenOp, ChloOp>>(typeConverter, context)
|
2022-08-02 12:53:24 +08:00
|
|
|
INSERT_BINARY_ADDSUB_PATTERN(AtenAddTensorOp, chlo::BroadcastAddOp);
|
|
|
|
INSERT_BINARY_ADDSUB_PATTERN(AtenAddScalarOp, chlo::BroadcastAddOp);
|
|
|
|
INSERT_BINARY_ADDSUB_PATTERN(AtenSubTensorOp, chlo::BroadcastSubOp);
|
|
|
|
INSERT_BINARY_ADDSUB_PATTERN(AtenSubScalarOp, chlo::BroadcastSubOp);
|
2022-08-17 09:07:36 +08:00
|
|
|
INSERT_BINARY_ADDSUB_PATTERN(AtenRsubScalarOp, chlo::BroadcastSubOp);
|
2022-07-27 13:07:51 +08:00
|
|
|
#undef INSERT_BINARY_ADDSUB_PATTERN
|
|
|
|
|
2022-08-02 12:53:24 +08:00
|
|
|
#define INSERT_BINARY_MULDIV_PATTERN(AtenOp, ChloOp) \
|
2022-07-27 13:07:51 +08:00
|
|
|
target.addIllegalOp<AtenOp>(); \
|
2022-09-01 10:36:02 +08:00
|
|
|
patterns.add<ConvertAtenMulDivOp<AtenOp, ChloOp>>(typeConverter, context)
|
2022-08-02 12:53:24 +08:00
|
|
|
INSERT_BINARY_MULDIV_PATTERN(AtenMulTensorOp, chlo::BroadcastMulOp);
|
|
|
|
INSERT_BINARY_MULDIV_PATTERN(AtenMulScalarOp, chlo::BroadcastMulOp);
|
|
|
|
INSERT_BINARY_MULDIV_PATTERN(AtenDivTensorOp, chlo::BroadcastDivOp);
|
2022-08-06 23:38:06 +08:00
|
|
|
INSERT_BINARY_MULDIV_PATTERN(AtenDivTensorModeOp, chlo::BroadcastDivOp);
|
2022-08-02 12:53:24 +08:00
|
|
|
INSERT_BINARY_MULDIV_PATTERN(AtenDivScalarOp, chlo::BroadcastDivOp);
|
2022-11-24 14:28:34 +08:00
|
|
|
INSERT_BINARY_MULDIV_PATTERN(AtenRemainderScalarOp, chlo::BroadcastRemOp);
|
2022-08-02 12:53:24 +08:00
|
|
|
#undef INSERT_BINARY_MULDIV_PATTERN
|
2022-07-27 13:07:51 +08:00
|
|
|
|
|
|
|
#define INSERT_BINARY_COMPARE_PATTERN(AtenOp) \
|
|
|
|
target.addIllegalOp<AtenOp>(); \
|
2022-09-01 10:36:02 +08:00
|
|
|
patterns.add<ConvertAtenCompareOp<AtenOp>>(typeConverter, context)
|
2022-08-02 12:53:24 +08:00
|
|
|
|
2022-07-27 13:07:51 +08:00
|
|
|
INSERT_BINARY_COMPARE_PATTERN(AtenGtTensorOp);
|
|
|
|
INSERT_BINARY_COMPARE_PATTERN(AtenGtScalarOp);
|
2022-11-24 14:28:34 +08:00
|
|
|
INSERT_BINARY_COMPARE_PATTERN(AtenGeTensorOp);
|
|
|
|
INSERT_BINARY_COMPARE_PATTERN(AtenGeScalarOp);
|
2022-07-27 13:07:51 +08:00
|
|
|
INSERT_BINARY_COMPARE_PATTERN(AtenLtTensorOp);
|
|
|
|
INSERT_BINARY_COMPARE_PATTERN(AtenLtScalarOp);
|
2022-11-24 14:28:34 +08:00
|
|
|
INSERT_BINARY_COMPARE_PATTERN(AtenLeTensorOp);
|
|
|
|
INSERT_BINARY_COMPARE_PATTERN(AtenLeScalarOp);
|
2022-07-27 13:07:51 +08:00
|
|
|
INSERT_BINARY_COMPARE_PATTERN(AtenEqTensorOp);
|
|
|
|
INSERT_BINARY_COMPARE_PATTERN(AtenEqScalarOp);
|
|
|
|
INSERT_BINARY_COMPARE_PATTERN(AtenNeTensorOp);
|
|
|
|
INSERT_BINARY_COMPARE_PATTERN(AtenNeScalarOp);
|
|
|
|
#undef INSERT_BINARY_COMPARE_PATTERN
|
|
|
|
|
2023-01-04 10:11:25 +08:00
|
|
|
#define INSERT_BINARY_LOGICAL_PATTERN(AtenOp, ChloOp) \
|
|
|
|
target.addIllegalOp<AtenOp>(); \
|
|
|
|
patterns.add<ConvertAtenLogicalBinaryOp<AtenOp, ChloOp>>(typeConverter, \
|
|
|
|
context)
|
|
|
|
|
|
|
|
INSERT_BINARY_LOGICAL_PATTERN(AtenLogicalOrOp, chlo::BroadcastOrOp);
|
|
|
|
INSERT_BINARY_LOGICAL_PATTERN(AtenLogicalAndOp, chlo::BroadcastAndOp);
|
|
|
|
INSERT_BINARY_LOGICAL_PATTERN(AtenLogicalXorOp, chlo::BroadcastXorOp);
|
|
|
|
#undef INSERT_BINARY_LOGICAL_PATTERN
|
|
|
|
|
2022-07-21 07:18:16 +08:00
|
|
|
#define INSERT_ATENOP_PATTERN(AtenOp) \
|
|
|
|
target.addIllegalOp<AtenOp>(); \
|
2022-09-01 10:36:02 +08:00
|
|
|
patterns.add<ConvertAtenOp<AtenOp>>(typeConverter, context, options)
|
|
|
|
|
|
|
|
INSERT_ATENOP_PATTERN(AtenBroadcastToOp);
|
|
|
|
INSERT_ATENOP_PATTERN(AtenPermuteOp);
|
|
|
|
|
2022-07-27 13:07:51 +08:00
|
|
|
INSERT_ATENOP_PATTERN(ValueTensorLiteralOp);
|
|
|
|
INSERT_ATENOP_PATTERN(AtenReciprocalOp);
|
2023-02-17 12:26:46 +08:00
|
|
|
INSERT_ATENOP_PATTERN(AtenPowTensorScalarOp);
|
2022-07-27 13:07:51 +08:00
|
|
|
INSERT_ATENOP_PATTERN(PrimNumToTensorScalarOp);
|
|
|
|
INSERT_ATENOP_PATTERN(AtenContiguousOp);
|
2022-07-21 07:18:16 +08:00
|
|
|
|
2022-07-27 13:07:51 +08:00
|
|
|
INSERT_ATENOP_PATTERN(AtenReluOp);
|
2022-08-02 15:01:30 +08:00
|
|
|
INSERT_ATENOP_PATTERN(AtenGeluOp);
|
2022-07-27 13:07:51 +08:00
|
|
|
INSERT_ATENOP_PATTERN(AtenErfOp);
|
2022-12-21 20:09:43 +08:00
|
|
|
INSERT_ATENOP_PATTERN(AtenGeluBackwardOp);
|
2022-07-27 13:07:51 +08:00
|
|
|
|
2022-08-10 13:12:34 +08:00
|
|
|
INSERT_ATENOP_PATTERN(AtenCatOp);
|
2022-09-16 15:09:21 +08:00
|
|
|
INSERT_ATENOP_PATTERN(AtenClampOp);
|
2022-09-20 22:31:24 +08:00
|
|
|
INSERT_ATENOP_PATTERN(AtenArangeStartStepOp);
|
2022-08-10 13:12:34 +08:00
|
|
|
|
2022-07-27 13:07:51 +08:00
|
|
|
INSERT_ATENOP_PATTERN(AtenBatchNormOp);
|
|
|
|
INSERT_ATENOP_PATTERN(AtenNativeLayerNormOp);
|
2022-08-23 16:47:21 +08:00
|
|
|
INSERT_ATENOP_PATTERN(AtenNumelOp);
|
2022-09-23 10:24:36 +08:00
|
|
|
INSERT_ATENOP_PATTERN(AtenSizeIntOp);
|
|
|
|
INSERT_ATENOP_PATTERN(AtenToDtypeOp);
|
2022-11-24 14:28:34 +08:00
|
|
|
INSERT_ATENOP_PATTERN(AtenWhereSelfOp);
|
2023-05-05 00:55:03 +08:00
|
|
|
INSERT_ATENOP_PATTERN(AtenPowTensorTensorOp);
|
2023-05-19 10:07:35 +08:00
|
|
|
INSERT_ATENOP_PATTERN(AtenEmptyMemoryFormatOp);
|
2023-05-12 07:41:46 +08:00
|
|
|
INSERT_ATENOP_PATTERN(AtenFillScalarOp);
|
2022-07-27 13:07:51 +08:00
|
|
|
#undef INSERT_ATENOP_PATTERN
|
2022-09-23 20:39:15 +08:00
|
|
|
|
2023-02-02 21:29:47 +08:00
|
|
|
#define INSERT_BINARY_BROADCAST_PATTERN(AtenOp, StablehloOp) \
|
2022-09-23 20:39:15 +08:00
|
|
|
target.addIllegalOp<AtenOp>(); \
|
2023-02-02 21:29:47 +08:00
|
|
|
patterns.add<ConvertAtenBinaryBroadcastOp<AtenOp, StablehloOp>>( \
|
|
|
|
typeConverter, context)
|
2022-09-23 20:39:15 +08:00
|
|
|
INSERT_BINARY_BROADCAST_PATTERN(AtenMaximumOp, chlo::BroadcastMaxOp);
|
|
|
|
INSERT_BINARY_BROADCAST_PATTERN(AtenMinimumOp, chlo::BroadcastMinOp);
|
|
|
|
INSERT_BINARY_BROADCAST_PATTERN(Aten__And__TensorOp, chlo::BroadcastAndOp);
|
2022-11-24 14:28:34 +08:00
|
|
|
INSERT_BINARY_BROADCAST_PATTERN(AtenBitwiseAndTensorOp, chlo::BroadcastAndOp);
|
2023-01-12 06:40:03 +08:00
|
|
|
INSERT_BINARY_BROADCAST_PATTERN(AtenBitwiseOrTensorOp, chlo::BroadcastOrOp);
|
|
|
|
INSERT_BINARY_BROADCAST_PATTERN(AtenBitwiseXorTensorOp, chlo::BroadcastXorOp);
|
2022-09-23 20:39:15 +08:00
|
|
|
#undef INSERT_BINARY_BROADCAST_PATTERN
|
2022-08-03 08:16:31 +08:00
|
|
|
}
|