Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / runtime / onert / core / src / compiler / CompilerOptions.cc
1 /*
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "compiler/CompilerOptions.h"
18
19 #include "../backend/builtin/Backend.h"
20
21 #include "util/ConfigSource.h"
22 #include "util/logging.h"
23
24 #include <misc/string_helpers.h>
25
26 namespace
27 {
28
29 using namespace onert;
30
31 std::string getOpBackends(std::unordered_map<ir::OpCode, std::string> &opcode_to_backend)
32 {
33   std::unordered_map<ir::OpCode, std::string>::iterator it;
34   std::string opbackends;
35
36   for (it = opcode_to_backend.begin(); it != opcode_to_backend.end(); ++it)
37   {
38     if (!opbackends.empty())
39       opbackends = opbackends + ", ";
40
41     auto opcode = it->first;
42     const std::string opname = ir::toString(opcode);
43     opbackends += opname + "=" + it->second;
44   }
45   return opbackends;
46 }
47
48 } // namespace
49
50 namespace onert
51 {
52 namespace compiler
53 {
54
55 void ManualSchedulerOptions::setBackendMap(const std::string &str)
56 {
57   // TODO Support multiple subgraphs for manual scheduling
58   auto key_val_list = nnfw::misc::split(str, ';');
59   for (const auto &key_val_str : key_val_list)
60   {
61     if (key_val_str.empty())
62     {
63       continue;
64     }
65
66     auto key_val = nnfw::misc::split(key_val_str, '=');
67     const auto &key_str = key_val.at(0);
68     const auto &val = key_val.at(1);
69     auto key = static_cast<uint32_t>(std::stoi(key_str));
70     this->index_to_backend.emplace(ir::OperationIndex{key}, val);
71   }
72 }
73
74 std::unique_ptr<CompilerOptions> CompilerOptions::fromGlobalConfig()
75 {
76   auto o = std::make_unique<CompilerOptions>();
77   o->backend_list = nnfw::misc::split(util::getConfigString(util::config::BACKENDS), ';');
78   o->minmax_filepath = util::getConfigString(util::config::MINMAX_FILEPATH);
79   o->trace_filepath = util::getConfigString(util::config::TRACE_FILEPATH);
80   o->graph_dump_level = util::getConfigInt(util::config::GRAPH_DOT_DUMP);
81   o->executor = util::getConfigString(util::config::EXECUTOR);
82   o->he_scheduler = util::getConfigBool(util::config::USE_SCHEDULER);
83   o->he_profiling_mode = util::getConfigBool(util::config::PROFILING_MODE);
84   o->fp16_enable = util::getConfigBool(util::config::FP16_ENABLE);
85   {
86     // Backend for all
87     auto &ms_options = o->manual_scheduler_options;
88
89     // Default value for op_backend_all is first element in the backend list
90     ms_options.backend_for_all = util::getConfigString(util::config::OP_BACKEND_ALLOPS);
91
92 // Opcode to Backend
93 #define OP(OpName)                                                                      \
94   {                                                                                     \
95     const auto &backend_str = util::getConfigString(util::config::OP_BACKEND_##OpName); \
96     if (!backend_str.empty())                                                           \
97     {                                                                                   \
98       ms_options.opcode_to_backend[ir::OpCode::OpName] = backend_str;                   \
99     }                                                                                   \
100   }
101 #include "ir/Operations.lst"
102 #undef OP
103
104     // Index to Backend
105     auto map_str = util::getConfigString(util::config::OP_BACKEND_MAP);
106     ms_options.setBackendMap(map_str);
107   }
108   return o;
109 }
110
111 void CompilerOptions::forceInternalOptions()
112 {
113   // Set control flow backend for control flow operators
114   auto &builtin_id = backend::builtin::Config::ID;
115   manual_scheduler_options.opcode_to_backend[ir::OpCode::If] = builtin_id;
116   manual_scheduler_options.opcode_to_backend[ir::OpCode::While] = builtin_id;
117   manual_scheduler_options.opcode_to_backend[ir::OpCode::Permute] = builtin_id;
118
119   // FIXME This is a workaround for bcq operations, should remove it
120   manual_scheduler_options.opcode_to_backend[ir::OpCode::BCQFullyConnected] = "bcq";
121   manual_scheduler_options.opcode_to_backend[ir::OpCode::BCQGather] = "bcq";
122
123   // FIXME This is a workaround for bulk operations, should remove it
124   manual_scheduler_options.opcode_to_backend[ir::OpCode::Bulk] = "trix";
125 }
126
127 void CompilerOptions::verboseOptions()
128 {
129   VERBOSE(Compiler) << std::boolalpha << "==== Compiler Options ====" << std::endl;
130   VERBOSE(Compiler) << "backend_list             : "
131                     << nnfw::misc::join(backend_list.begin(), backend_list.end(), "/") << std::endl;
132   VERBOSE(Compiler) << "trace_filepath           : " << trace_filepath << std::endl;
133   VERBOSE(Compiler) << "graph_dump_level         : " << graph_dump_level << std::endl;
134   VERBOSE(Compiler) << "executor                 : " << executor << std::endl;
135   VERBOSE(Compiler) << "manual backend_for_all   : " << manual_scheduler_options.backend_for_all
136                     << std::endl;
137   VERBOSE(Compiler) << "manual_scheduler_options : "
138                     << getOpBackends(manual_scheduler_options.opcode_to_backend) << std::endl;
139   VERBOSE(Compiler) << "he_scheduler             : " << he_scheduler << std::endl;
140   VERBOSE(Compiler) << "he_profiling_mode        : " << he_profiling_mode << std::endl;
141   VERBOSE(Compiler) << "fp16_enable              : " << fp16_enable << std::endl
142                     << std::noboolalpha;
143 }
144
145 } // namespace compiler
146 } // namespace onert