2 * Copyright (c) 2022 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 "compiler/CompilerOptions.h"
19 #include "../backend/builtin/Backend.h"
21 #include "util/ConfigSource.h"
22 #include "util/logging.h"
24 #include <misc/string_helpers.h>
29 using namespace onert;
31 std::string getOpBackends(std::unordered_map<ir::OpCode, std::string> &opcode_to_backend)
33 std::unordered_map<ir::OpCode, std::string>::iterator it;
34 std::string opbackends;
36 for (it = opcode_to_backend.begin(); it != opcode_to_backend.end(); ++it)
38 if (!opbackends.empty())
39 opbackends = opbackends + ", ";
41 auto opcode = it->first;
42 const std::string opname = ir::toString(opcode);
43 opbackends += opname + "=" + it->second;
55 void ManualSchedulerOptions::setBackendMap(const std::string &str)
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)
61 if (key_val_str.empty())
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);
74 std::unique_ptr<CompilerOptions> CompilerOptions::fromGlobalConfig()
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);
87 auto &ms_options = o->manual_scheduler_options;
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);
95 const auto &backend_str = util::getConfigString(util::config::OP_BACKEND_##OpName); \
96 if (!backend_str.empty()) \
98 ms_options.opcode_to_backend[ir::OpCode::OpName] = backend_str; \
101 #include "ir/Operations.lst"
105 auto map_str = util::getConfigString(util::config::OP_BACKEND_MAP);
106 ms_options.setBackendMap(map_str);
111 void CompilerOptions::forceInternalOptions()
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;
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";
123 // FIXME This is a workaround for bulk operations, should remove it
124 manual_scheduler_options.opcode_to_backend[ir::OpCode::Bulk] = "trix";
127 void CompilerOptions::verboseOptions()
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
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
145 } // namespace compiler