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-07-21 07:18:16 +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/Dialect/Torch/Utils/Utils.h"
|
|
|
|
#include "torch-mlir/Dialect/TorchConversion/Transforms/BackendTypeConversion.h"
|
|
|
|
|
|
|
|
using namespace mlir;
|
|
|
|
using namespace mlir::torch;
|
|
|
|
using namespace mlir::torch::Torch;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2023-02-02 21:29:47 +08:00
|
|
|
class ConvertTorchToStablehlo
|
|
|
|
: public ConvertTorchToStablehloBase<ConvertTorchToStablehlo> {
|
2022-07-21 07:18:16 +08:00
|
|
|
public:
|
2023-02-02 21:29:47 +08:00
|
|
|
ConvertTorchToStablehlo() = default;
|
|
|
|
ConvertTorchToStablehlo(bool enableStaticShape, bool enableI32Index) {
|
2022-09-01 10:36:02 +08:00
|
|
|
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>();
|
2023-02-02 21:29:47 +08:00
|
|
|
registry.insert<stablehlo::StablehloDialect>();
|
2022-07-21 07:18:16 +08:00
|
|
|
registry.insert<tensor::TensorDialect>();
|
2023-07-27 18:35:25 +08:00
|
|
|
registry.insert<shape::ShapeDialect>();
|
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);
|
2023-02-02 21:29:47 +08:00
|
|
|
target.addLegalDialect<chlo::ChloDialect, stablehlo::StablehloDialect,
|
2023-07-27 18:35:25 +08:00
|
|
|
tensor::TensorDialect, arith::ArithDialect,
|
|
|
|
shape::ShapeDialect>();
|
2022-07-21 07:18:16 +08:00
|
|
|
|
|
|
|
TypeConverter typeConverter;
|
|
|
|
typeConverter.addConversion([](Type type) { return type; });
|
|
|
|
TorchConversion::setupBackendTypeConversion(target, typeConverter);
|
|
|
|
|
|
|
|
RewritePatternSet patterns(context);
|
|
|
|
|
2023-02-02 21:29:47 +08:00
|
|
|
torch_to_stablehlo::TorchToStablehloOptions options{
|
|
|
|
enableStaticShape, enableI32Index ? 32u : 64u};
|
|
|
|
torch_to_stablehlo::populateBasicOpPatternsAndLegality(
|
|
|
|
typeConverter, patterns, target, options);
|
|
|
|
torch_to_stablehlo::populateViewLikeOpPatternsAndLegality(
|
|
|
|
typeConverter, patterns, target, options);
|
2023-03-23 04:41:04 +08:00
|
|
|
torch_to_stablehlo::populateGatherScatterOpPatternsAndLegality(
|
2023-02-02 21:29:47 +08:00
|
|
|
typeConverter, patterns, target, options);
|
|
|
|
torch_to_stablehlo::populateReductionOpPatternsAndLegality(
|
|
|
|
typeConverter, patterns, target, options);
|
|
|
|
torch_to_stablehlo::populateLinearOpPatternsAndLegality(
|
2022-09-01 10:36:02 +08:00
|
|
|
typeConverter, patterns, target, options);
|
2023-02-02 21:29:47 +08:00
|
|
|
torch_to_stablehlo::populatePoolingOpPatternsAndLegality(
|
2022-09-01 10:36:02 +08:00
|
|
|
typeConverter, patterns, target, options);
|
2024-05-11 15:33:37 +08:00
|
|
|
torch_to_stablehlo::populateRngOpPatternsAndLegality(
|
|
|
|
typeConverter, patterns, 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>>
|
2023-02-02 21:29:47 +08:00
|
|
|
mlir::torch::createConvertTorchToStablehloPass() {
|
|
|
|
return std::make_unique<ConvertTorchToStablehlo>(false, false);
|
2022-09-01 10:36:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<OperationPass<func::FuncOp>>
|
2023-02-02 21:29:47 +08:00
|
|
|
mlir::torch::createConvertTorchToStablehloPass(bool enableStaticShape,
|
|
|
|
bool enableI32Index) {
|
|
|
|
return std::make_unique<ConvertTorchToStablehlo>(enableStaticShape,
|
|
|
|
enableI32Index);
|
2022-08-04 10:10:54 +08:00
|
|
|
}
|