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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "torch-mlir/Conversion/TorchToMhlo/TorchToMhlo.h"
|
|
|
|
|
|
|
|
#include "../PassDetail.h"
|
|
|
|
#include "./PopulatePatterns.h"
|
|
|
|
#include "mlir-hlo/Dialect/mhlo/IR/hlo_ops.h"
|
2022-10-05 21:28:06 +08:00
|
|
|
#include "mlir/Dialect/Arith/IR/Arith.h"
|
2022-07-21 07:18:16 +08:00
|
|
|
#include "mlir/Dialect/Tensor/IR/Tensor.h"
|
|
|
|
#include "mlir/Dialect/Traits.h"
|
|
|
|
#include "mlir/IR/Matchers.h"
|
|
|
|
#include "mlir/Transforms/DialectConversion.h"
|
2022-08-31 03:44:00 +08:00
|
|
|
#include "stablehlo/dialect/ChloOps.h"
|
2022-07-21 07:18:16 +08:00
|
|
|
#include "torch-mlir/Dialect/Torch/IR/TorchDialect.h"
|
|
|
|
#include "torch-mlir/Dialect/Torch/IR/TorchOps.h"
|
|
|
|
#include "torch-mlir/Dialect/Torch/Utils/Utils.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 {
|
|
|
|
|
|
|
|
class ConvertTorchToMhlo : public ConvertTorchToMhloBase<ConvertTorchToMhlo> {
|
|
|
|
public:
|
2022-09-01 10:36:02 +08:00
|
|
|
ConvertTorchToMhlo() = default;
|
|
|
|
ConvertTorchToMhlo(bool enableStaticShape, bool enableI32Index) {
|
|
|
|
this->enableStaticShape = enableStaticShape;
|
|
|
|
this->enableI32Index = enableI32Index;
|
|
|
|
}
|
|
|
|
|
2022-07-21 07:18:16 +08:00
|
|
|
void getDependentDialects(DialectRegistry ®istry) const override {
|
2022-08-02 12:53:24 +08:00
|
|
|
registry.insert<chlo::ChloDialect>();
|
2022-07-21 07:18:16 +08:00
|
|
|
registry.insert<mhlo::MhloDialect>();
|
|
|
|
registry.insert<tensor::TensorDialect>();
|
2022-10-05 21:28:06 +08:00
|
|
|
registry.insert<arith::ArithDialect>();
|
2022-07-21 07:18:16 +08:00
|
|
|
TorchConversion::getBackendTypeConversionDependentDialects(registry);
|
|
|
|
}
|
|
|
|
void runOnOperation() override {
|
|
|
|
MLIRContext *context = &getContext();
|
|
|
|
ConversionTarget target(*context);
|
2022-08-04 12:34:22 +08:00
|
|
|
target.addLegalDialect<chlo::ChloDialect, mhlo::MhloDialect,
|
2022-10-05 21:28:06 +08:00
|
|
|
tensor::TensorDialect, arith::ArithDialect>();
|
2022-07-21 07:18:16 +08:00
|
|
|
|
|
|
|
TypeConverter typeConverter;
|
|
|
|
typeConverter.addConversion([](Type type) { return type; });
|
|
|
|
TorchConversion::setupBackendTypeConversion(target, typeConverter);
|
|
|
|
|
|
|
|
RewritePatternSet patterns(context);
|
|
|
|
|
2022-09-01 10:36:02 +08:00
|
|
|
torch_to_mhlo::TorchToMhloOptions options{enableStaticShape,
|
|
|
|
enableI32Index ? 32u : 64u};
|
2022-07-21 07:18:16 +08:00
|
|
|
torch_to_mhlo::populateBasicOpPatternsAndLegality(typeConverter, patterns,
|
2022-09-01 10:36:02 +08:00
|
|
|
target, options);
|
|
|
|
torch_to_mhlo::populateViewLikeOpPatternsAndLegality(
|
|
|
|
typeConverter, patterns, target, options);
|
2022-07-25 23:47:46 +08:00
|
|
|
torch_to_mhlo::populateGatherOpPatternsAndLegality(typeConverter, patterns,
|
2022-09-01 10:36:02 +08:00
|
|
|
target, options);
|
|
|
|
torch_to_mhlo::populateReductionOpPatternsAndLegality(
|
|
|
|
typeConverter, patterns, target, options);
|
2022-08-04 10:10:54 +08:00
|
|
|
torch_to_mhlo::populateLinearOpPatternsAndLegality(typeConverter, patterns,
|
2022-09-01 10:36:02 +08:00
|
|
|
target, options);
|
2022-08-04 12:34:22 +08:00
|
|
|
torch_to_mhlo::populatePoolingOpPatternsAndLegality(typeConverter, patterns,
|
2022-09-01 10:36:02 +08:00
|
|
|
target, options);
|
2022-07-25 23:47:46 +08:00
|
|
|
|
2022-07-21 07:18:16 +08:00
|
|
|
if (failed(applyPartialConversion(getOperation(), target,
|
|
|
|
std::move(patterns)))) {
|
|
|
|
return signalPassFailure();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
std::unique_ptr<OperationPass<func::FuncOp>>
|
|
|
|
mlir::torch::createConvertTorchToMhloPass() {
|
2022-09-01 10:36:02 +08:00
|
|
|
return std::make_unique<ConvertTorchToMhlo>(false, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<OperationPass<func::FuncOp>>
|
|
|
|
mlir::torch::createConvertTorchToMhloPass(bool enableStaticShape,
|
|
|
|
bool enableI32Index) {
|
|
|
|
return std::make_unique<ConvertTorchToMhlo>(enableStaticShape,
|
|
|
|
enableI32Index);
|
2022-08-04 10:10:54 +08:00
|
|
|
}
|