diff --git a/src/Accelerators/NNPA/Compiler/NNPACompilerUtils.cpp b/src/Accelerators/NNPA/Compiler/NNPACompilerUtils.cpp index b078640e82..e4145407de 100644 --- a/src/Accelerators/NNPA/Compiler/NNPACompilerUtils.cpp +++ b/src/Accelerators/NNPA/Compiler/NNPACompilerUtils.cpp @@ -111,15 +111,7 @@ void addONNXToZHighPasses(mlir::PassManager &pm) { onnx_mlir::zhigh::createZHighConstPropagationPass()); // One more call to ONNX shape inference/canonicalization/... to update shape // if possible. - if (enableONNXHybridPass) { - // For starters only illustrating the new hybrid pass by replacing 3 passes - // here. The plan is to replace most of the passes in addONNXToMLIRPasses. - pm.addNestedPass(onnx_mlir::createONNXHybridTransformPass()); - } else { - pm.addNestedPass(onnx_mlir::createShapeInferencePass()); - pm.addPass(mlir::createCanonicalizerPass()); - pm.addNestedPass(onnx_mlir::createShapeInferencePass()); - } + pm.addNestedPass(onnx_mlir::createONNXHybridTransformPass()); // Remove common sub-expressions. pm.addPass(mlir::createCSEPass()); diff --git a/src/Compiler/CompilerOptions.cpp b/src/Compiler/CompilerOptions.cpp index 4b4b9a5126..cc137bf464 100644 --- a/src/Compiler/CompilerOptions.cpp +++ b/src/Compiler/CompilerOptions.cpp @@ -34,7 +34,6 @@ std::string march; // common for both InstrumentStages instrumentStage; // common for both int onnxConstPropExpansionBound; // common for both std::vector onnxConstPropDisablePatterns; // common for both -bool enableONNXHybridPass; // common for both std::vector functionsToDecompose; // common for both std::string opsForCall; // common for both EmissionTargetType emissionTarget; // onnx-mlir only @@ -59,8 +58,6 @@ std::string instrumentOps; // onnx-mlir only unsigned instrumentControlBits; // onnx-mlir only bool instrumentONNXSignature; // onnx-mlir only std::string ONNXOpStats; // onnx-mlir only -int onnxOpTransformThreshold; // onnx-mlir only -bool onnxOpTransformReport; // onnx-mlir only bool enableParallel; // onnx-mlir only bool disableSimdOption; // onnx-mlir only bool enableSimdDataLayout; // onnx-mlir only @@ -173,12 +170,6 @@ static llvm::cl::list> llvm::cl::location(onnxConstPropDisablePatterns), llvm::cl::cat(OnnxMlirCommonOptions)); -static llvm::cl::opt enableONNXHybridPassOpt("onnx-hybrid-pass", - llvm::cl::desc("Enable ONNX hybrid pass (default=true)\n" - "Set to 'false' if you want to disable ONNX hybrid pass."), - llvm::cl::location(enableONNXHybridPass), llvm::cl::init(true), - llvm::cl::cat(OnnxMlirCommonOptions)); - static llvm::cl::list> functionsToDecomposeOpt("functions-to-decompose", llvm::cl::desc("Specify ONNX functions to decompose"), @@ -377,22 +368,6 @@ static llvm::cl::opt ONNXOpStatsOpt("onnx-op-stats", llvm::cl::location(ONNXOpStats), llvm::cl::init(""), llvm::cl::cat(OnnxMlirOptions)); -static llvm::cl::opt onnxOpTransformThresholdOpt( - "onnx-op-transform-threshold", - llvm::cl::desc( - "Max iteration for dynamic op transform passes (default=3).\n" - "If set to 0, onnxOpTransformPass will be disabled, and\n" - "static iteration will be used"), - llvm::cl::location(onnxOpTransformThreshold), llvm::cl::init(3), - llvm::cl::cat(OnnxMlirOptions)); - -static llvm::cl::opt onnxOpTransformReportOpt( - "onnx-op-transform-report", - llvm::cl::desc( - "Report diagnostic info for ONNX op transform/optimization passes."), - llvm::cl::location(onnxOpTransformReport), llvm::cl::init(false), - llvm::cl::cat(OnnxMlirOptions)); - static llvm::cl::opt enableParallelOpt("parallel", llvm::cl::desc("Enable parallelization (default=false)\n" "Set to 'true' if you want to enable parallelization."), diff --git a/src/Compiler/CompilerOptions.hpp b/src/Compiler/CompilerOptions.hpp index f781e03424..0da570532b 100644 --- a/src/Compiler/CompilerOptions.hpp +++ b/src/Compiler/CompilerOptions.hpp @@ -76,7 +76,6 @@ extern std::string march; // common for both extern InstrumentStages instrumentStage; // common for both extern int onnxConstPropExpansionBound; // common for both extern std::vector onnxConstPropDisablePatterns; // common for both -extern bool enableONNXHybridPass; // common for both extern std::vector functionsToDecompose; // common for both extern std::string opsForCall; // common for both extern EmissionTargetType emissionTarget; // onnx-mlir only @@ -101,8 +100,6 @@ extern std::string instrumentOps; // onnx-mlir only extern unsigned instrumentControlBits; // onnx-mlir only extern bool instrumentONNXSignature; // onnx-mlir only extern std::string ONNXOpStats; // onnx-mlir only -extern int onnxOpTransformThreshold; // onnx-mlir only -extern bool onnxOpTransformReport; // onnx-mlir only extern bool enableParallel; // onnx-mlir only extern bool disableSimdOption; // onnx-mlir only extern bool enableSimdDataLayout; // onnx-mlir only diff --git a/src/Compiler/CompilerPasses.cpp b/src/Compiler/CompilerPasses.cpp index 706df8eccf..b0f035309b 100644 --- a/src/Compiler/CompilerPasses.cpp +++ b/src/Compiler/CompilerPasses.cpp @@ -68,40 +68,12 @@ void addONNXToMLIRPasses(mlir::PassManager &pm, bool targetCPU) { // Decompose first. Eliminates some unsupported ops without shape inference. pm.addNestedPass(onnx_mlir::createDecomposeONNXToONNXPass()); - if (enableONNXHybridPass) { + pm.addNestedPass(onnx_mlir::createONNXHybridTransformPass()); + // Convolution Optimization for CPU: enable when there are no accelerators. + if (targetCPU && enableConvOptPass) { + pm.addNestedPass(onnx_mlir::createConvOptONNXToONNXPass( + enableSimdDataLayout && !disableSimdOption)); pm.addNestedPass(onnx_mlir::createONNXHybridTransformPass()); - // Convolution Optimization for CPU: enable when there are no accelerators. - if (targetCPU && enableConvOptPass) { - pm.addNestedPass(onnx_mlir::createConvOptONNXToONNXPass( - enableSimdDataLayout && !disableSimdOption)); - pm.addNestedPass( - onnx_mlir::createONNXHybridTransformPass()); - } - } else { - pm.addNestedPass(onnx_mlir::createShapeInferencePass()); - pm.addPass(mlir::createCanonicalizerPass()); - pm.addNestedPass(onnx_mlir::createShapeInferencePass()); - // Convolution Optimization for CPU: enable when there are no accelerators. - if (targetCPU && enableConvOptPass) { - pm.addNestedPass(onnx_mlir::createConvOptONNXToONNXPass( - enableSimdDataLayout && !disableSimdOption)); - pm.addNestedPass(onnx_mlir::createShapeInferencePass()); - } - pm.addNestedPass(onnx_mlir::createConstPropONNXToONNXPass()); - if (onnxOpTransformThreshold > 0) { - // Dynamic iterate in ONNXOpTransformPass - pm.addPass(onnx_mlir::createONNXOpTransformPass(onnxOpTransformThreshold, - onnxOpTransformReport, targetCPU, - enableSimdDataLayout && !disableSimdOption, enableConvOptPass)); - } else { - // Statically add extra passes - for (int i = 0; i < repeatOnnxTransform; i++) { - pm.addPass(mlir::createCanonicalizerPass()); - pm.addNestedPass(onnx_mlir::createShapeInferencePass()); - pm.addNestedPass( - onnx_mlir::createConstPropONNXToONNXPass()); - } - } } // Simplify shape-related ops. @@ -109,13 +81,7 @@ void addONNXToMLIRPasses(mlir::PassManager &pm, bool targetCPU) { // One more call to ONNX shape inference/canonicalization/... to update // shape if possible. - if (enableONNXHybridPass) { - pm.addNestedPass(onnx_mlir::createONNXHybridTransformPass()); - } else { - pm.addNestedPass(onnx_mlir::createShapeInferencePass()); - pm.addPass(mlir::createCanonicalizerPass()); - pm.addNestedPass(onnx_mlir::createShapeInferencePass()); - } + pm.addNestedPass(onnx_mlir::createONNXHybridTransformPass()); // Replace ONNXReturnOp with func::ReturnOp. pm.addPass(onnx_mlir::createStandardFuncReturnPass()); diff --git a/src/Pass/Passes.hpp b/src/Pass/Passes.hpp index d228d1bde7..076fd8e2b5 100644 --- a/src/Pass/Passes.hpp +++ b/src/Pass/Passes.hpp @@ -29,12 +29,6 @@ namespace onnx_mlir { /// Pass for removing DisposableElementsAttr attributes. std::unique_ptr createScrubDisposablePass(bool closeAfter = true); -/// Pass for ONNX graph level optimization -std::unique_ptr createONNXOpTransformPass(); -std::unique_ptr createONNXOpTransformPass(int threshold, - bool report, bool targetCPU, bool enableSimdDataLayoutOpt, - bool enableConvOptPass); - /// Pass for rewriting inside frontend dialect. std::unique_ptr createDecomposeONNXToONNXPass( const std::string &target = ""); diff --git a/src/Tools/onnx-mlir-opt/RegisterPasses.cpp b/src/Tools/onnx-mlir-opt/RegisterPasses.cpp index c31875b14f..25025c91a0 100644 --- a/src/Tools/onnx-mlir-opt/RegisterPasses.cpp +++ b/src/Tools/onnx-mlir-opt/RegisterPasses.cpp @@ -39,10 +39,6 @@ void registerOMPasses(int optLevel) { return createScrubDisposablePass(); }); - mlir::registerPass([]() -> std::unique_ptr { - return createONNXOpTransformPass(); - }); - mlir::registerPass([]() -> std::unique_ptr { return createDecomposeONNXToONNXPass(); }); diff --git a/src/Transform/ONNX/CMakeLists.txt b/src/Transform/ONNX/CMakeLists.txt index 8a551687bd..126ace32a8 100644 --- a/src/Transform/ONNX/CMakeLists.txt +++ b/src/Transform/ONNX/CMakeLists.txt @@ -55,17 +55,6 @@ add_onnx_mlir_library(OMInstrumentONNX MLIRPass ) -add_onnx_mlir_library(OMOpTransform - ONNXOpTransformPass.cpp - - LINK_LIBS PUBLIC - OMONNXOps - MLIRPass - OMONNXRewrite - OMShapeInferencePass - MLIRTransforms - ) - add_onnx_mlir_library(OMHybridTransform ONNXHybridTransformPass.cpp diff --git a/src/Transform/ONNX/ONNXOpTransformPass.cpp b/src/Transform/ONNX/ONNXOpTransformPass.cpp deleted file mode 100644 index f371e65e25..0000000000 --- a/src/Transform/ONNX/ONNXOpTransformPass.cpp +++ /dev/null @@ -1,127 +0,0 @@ -/* - * SPDX-License-Identifier: Apache-2.0 - */ - -//===------- ONNXOpTransformPass.cpp - ONNX Op Transform ------------------===// -// -// Copyright 2019-2020 The IBM Research Authors. -// -// ============================================================================= -// -// This file implements a combined pass that dynamically invoke several -// transformation on ONNX ops. -// -//===----------------------------------------------------------------------===// - -#include "mlir/IR/OperationSupport.h" -#include "mlir/Pass/PassManager.h" -#include "mlir/Transforms/Passes.h" - -#include "src/Dialect/ONNX/ONNXOps.hpp" -#include "src/Pass/Passes.hpp" - -using namespace mlir; - -namespace { - -struct ONNXOpTransformPass : public mlir::PassWrapper> { - MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(ONNXOpTransformPass) - - StringRef getArgument() const override { return "onnx-op-transform"; } - - StringRef getDescription() const override { - return "Invoke passes iteratively that transform ONNX operation."; - } - - Option onnxOpTransformThreshold{*this, "onnx-op-transform-threshold", - llvm::cl::desc("max iteration for op transform passes."), - llvm::cl::init(10)}; - Option onnxOpTransformReport{*this, "onnx-op-transform-report", - llvm::cl::desc("Report diagnostic info for op transform passes."), - llvm::cl::init(false)}; - Option onnxOpTransformTargetCPU{*this, "onnx-op-transform-target-cpu", - llvm::cl::desc("Target CPU op transform passes."), llvm::cl::init(true)}; - Option onnxOpTransformEnableSimdDataLayout{*this, - "onnx-op-transform-simd-data-layout", - llvm::cl::desc("Enable SIMD data layout opt in op transform passes."), - llvm::cl::init(false)}; - Option enableConvOptPass{*this, "enable-conv-opt-pass", - llvm::cl::desc("Enable the ConvOptPass. Default is true."), - llvm::cl::init(true)}; - - ONNXOpTransformPass() = default; - ONNXOpTransformPass(const ONNXOpTransformPass &pass) - : mlir::PassWrapper>() {} - ONNXOpTransformPass(int threshold, bool report, bool targetCPU, - bool enableSimdDataLayoutOpt, bool enableConvOptPass) { - this->onnxOpTransformThreshold = threshold; - this->onnxOpTransformReport = report; - this->onnxOpTransformTargetCPU = targetCPU; - this->onnxOpTransformEnableSimdDataLayout = enableSimdDataLayoutOpt; - this->enableConvOptPass = enableConvOptPass; - } - - void runOnOperation() final; -}; - -void ONNXOpTransformPass::runOnOperation() { - auto module = getOperation(); - - assert(onnxOpTransformThreshold > 0); - int n = onnxOpTransformThreshold; - OperationFingerPrint before(module); - do { - OpPassManager dynamicPM("builtin.module"); - dynamicPM.addNestedPass( - onnx_mlir::createDecomposeONNXToONNXPass()); - dynamicPM.addNestedPass( - onnx_mlir::createShapeInferencePass()); - dynamicPM.addPass(mlir::createCanonicalizerPass()); - dynamicPM.addNestedPass( - onnx_mlir::createShapeInferencePass()); - // Convolution Optimization currently only for CPU. - if (onnxOpTransformTargetCPU && enableConvOptPass) { - dynamicPM.addNestedPass( - onnx_mlir::createConvOptONNXToONNXPass( - onnxOpTransformEnableSimdDataLayout)); - dynamicPM.addNestedPass( - onnx_mlir::createShapeInferencePass()); - } - dynamicPM.addNestedPass( - onnx_mlir::createConstPropONNXToONNXPass()); - if (failed(runPipeline(dynamicPM, module))) - return signalPassFailure(); - OperationFingerPrint after(module); - if (after == before) - break; - before = after; - } while (--n > 0); - if (n == 0) { - module->emitWarning() - << "ONNXOpTransform did not converge after " << onnxOpTransformThreshold - << "iterations. " - << "You may set a higher threshold with command option"; - } - if (onnxOpTransformReport) { - llvm::outs() << "ONNXOpTransform iterated " << onnxOpTransformThreshold - n - << " times, converged " << (n > 0 ? "true" : "false") << "\n"; - } -} - -} // end anonymous namespace - -/*! - * Create an instrumentation pass. - */ -std::unique_ptr onnx_mlir::createONNXOpTransformPass() { - return std::make_unique(); -} - -std::unique_ptr onnx_mlir::createONNXOpTransformPass(int threshold, - bool report, bool targetCPU, bool enableSimdDataLayoutOpt, - bool enableConvOptPass) { - return std::make_unique( - threshold, report, targetCPU, enableSimdDataLayoutOpt, enableConvOptPass); -}