2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "pass/PassData.h"
19 #include "passes/transformations/DataFormatSwitcher.h"
20 #include "passes/transformations/LowerConv2D.h"
22 #include "backends/interpreter/InterpreterBackend.h"
23 #include "backends/soft_backend/CPPGenerator.h"
24 #include "passes/dot_dumper/DumperPass.h"
25 #include "backends/acl_soft_backend/AclCppGenerator.h"
27 #include "passes/optimizations/CombineTransposes.h"
28 #include "passes/optimizations/ConstantFoldTranspose.h"
29 #include "passes/optimizations/DeadCodeElimination.h"
30 #include "passes/optimizations/FuseArithmeticOps.h"
31 #include "passes/optimizations/SinkRelu.h"
32 #include "passes/optimizations/SinkTranspose.h"
34 #include "support/CommandLine.h"
35 #include "Definitions.h"
39 #ifdef NNC_FRONTEND_CAFFE2_ENABLED
40 #include <caffe2_importer.h>
41 #endif // NNC_FRONTEND_CAFFE2_ENABLED
42 #ifdef NNC_FRONTEND_CAFFE_ENABLED
43 #include <caffe_importer.h>
44 #endif // NNC_FRONTEND_CAFFE_ENABLED
45 #ifdef NNC_FRONTEND_TFLITE_ENABLED
46 #include <tflite_importer.h>
47 #endif // NNC_FRONTEND_TFLITE_ENABLED
48 #ifdef NNC_FRONTEND_ONNX_ENABLED
49 #include <ONNXImporterImpl.h>
50 #endif // NNC_FRONTEND_ONNX_ENABLED
57 static std::string getFrontendOptionsString()
61 if (!cli::caffeFrontend.isDisabled())
62 res += "'" + cli::caffeFrontend.getNames()[0] + "' ";
64 if (!cli::caffe2Frontend.isDisabled())
65 res += "'" + cli::caffe2Frontend.getNames()[0] + "' ";
67 if (!cli::onnxFrontend.isDisabled())
68 res += "'" + cli::onnxFrontend.getNames()[0] + "' ";
70 if (!cli::tflFrontend.isDisabled())
71 res += "'" + cli::tflFrontend.getNames()[0] + "'";
76 static std::unique_ptr<mir::Graph> importModel()
78 // For bool, the value false is converted to zero and the value true is converted to one
79 if (cli::caffeFrontend + cli::caffe2Frontend + cli::tflFrontend + cli::onnxFrontend != 1)
80 throw DriverException("One and only one of the following options are allowed and have to be set"
81 "in the same time: " +
82 getFrontendOptionsString());
84 if (cli::caffeFrontend)
86 #ifdef NNC_FRONTEND_CAFFE_ENABLED
87 return mir_caffe::loadModel(cli::inputFile.getRawValue());
88 #endif // NNC_FRONTEND_CAFFE_ENABLED
90 else if (cli::caffe2Frontend)
92 #ifdef NNC_FRONTEND_CAFFE2_ENABLED
93 // FIXME: caffe2 input shapes are not provided by model and must be set from cli
94 // current 'inputShapes' could provide only one shape, while model could has several inputs
95 return mir_caffe2::loadModel(cli::inputFile.getRawValue(), cli::initNet.getRawValue(),
96 {cli::inputShapes.getRawValue()});
97 #endif // NNC_FRONTEND_CAFFE2_ENABLED
99 else if (cli::onnxFrontend)
101 #ifdef NNC_FRONTEND_ONNX_ENABLED
102 return mir_onnx::loadModel(cli::inputFile.getRawValue());
103 #endif // NNC_FRONTEND_ONNX_ENABLED
105 else if (cli::tflFrontend)
107 #ifdef NNC_FRONTEND_TFLITE_ENABLED
108 return mir_tflite::loadModel(cli::inputFile.getRawValue());
109 #endif // NNC_FRONTEND_TFLITE_ENABLED
116 static void backend(mir::Graph *graph)
118 if (cli::target == NNC_TARGET_ARM_CPP || cli::target == NNC_TARGET_X86_CPP)
120 CPPCodeGenerator(cli::artifactDir, cli::artifactName).run(graph);
122 else if (cli::target == NNC_TARGET_ARM_GPU_CPP)
124 AclCppCodeGenerator(cli::artifactDir, cli::artifactName).run(graph);
126 else if (cli::target == NNC_TARGET_INTERPRETER)
128 InterpreterBackend(cli::interInputDataDir, cli::artifactDir).run(graph);
132 assert(false && "invalid option value");
137 * @brief run all registered passes
138 * @throw PassException, if errors occured
140 void Driver::runPasses()
142 auto graph = importModel();
143 PassData pass_data(graph.get());
145 for (const auto &pass : _passManager.getPasses())
147 pass_data = pass->run(pass_data);
148 if (cli::dumpGraph && static_cast<mir::Graph *>(pass_data))
150 DumperPass d(pass->getName());
157 // NOTE. Now we destroy data of all passes when PassManager is destroyed.
158 // In future to reduce memory consumption we can destory it when passes are being performed
163 * @brief Register backend specific passes
164 * @throw DriverException if errors occurred
166 void Driver::registerBackendSpecificPasses()
168 std::unique_ptr<Pass> data_format_pass;
170 if (cli::target == NNC_TARGET_ARM_CPP || cli::target == NNC_TARGET_X86_CPP)
172 _passManager.registerPass(std::make_unique<LowerConv2D>());
173 _passManager.registerPass(std::make_unique<DataFormatSwitcher>(mir::DataFormat::NHWC));
175 else if (cli::target == NNC_TARGET_ARM_GPU_CPP)
177 _passManager.registerPass(std::make_unique<LowerConv2D>());
178 _passManager.registerPass(std::make_unique<ConstantFoldTranspose>());
179 // TODO Change to DataFormat::NCHW when fix it in ACL
180 _passManager.registerPass(std::make_unique<DataFormatSwitcher>(mir::DataFormat::NHWC));
182 else if (cli::target == NNC_TARGET_INTERPRETER)
184 _passManager.registerPass(std::make_unique<DataFormatSwitcher>(mir::DataFormat::NHWC));
188 assert(false && "invalid option value");
192 void Driver::registerOptimizationPass()
194 if (cli::doOptimizationPass)
196 // TODO: maybe we should start managing the optimizations more intelligently?
197 _passManager.registerPass(std::unique_ptr<Pass>(new CombineTransposes()));
198 _passManager.registerPass(std::unique_ptr<Pass>(new SinkTranspose()));
199 _passManager.registerPass(std::unique_ptr<Pass>(new SinkRelu()));
201 // TODO Support broadcasting.
202 _passManager.registerPass(std::unique_ptr<Pass>(new FuseArithmeticOps()));
204 _passManager.registerPass(std::unique_ptr<Pass>(new DeadCodeElimination()));
206 } // registerOptimizationPass
208 void Driver::runDriver()
210 registerOptimizationPass();
211 registerBackendSpecificPasses();