diff options
Diffstat (limited to 'runtimes/neurun/core/src/compiler/ManualScheduler.cc')
-rw-r--r-- | runtimes/neurun/core/src/compiler/ManualScheduler.cc | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/runtimes/neurun/core/src/compiler/ManualScheduler.cc b/runtimes/neurun/core/src/compiler/ManualScheduler.cc new file mode 100644 index 000000000..efd5ccc31 --- /dev/null +++ b/runtimes/neurun/core/src/compiler/ManualScheduler.cc @@ -0,0 +1,111 @@ +/* + * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ManualScheduler.h" +#include "model/Operations.Include.h" +#include "backend/Backend.h" +#include "backend/BackendManager.h" +#include "backend/IConfig.h" +#include "util/ConfigSource.h" +#include "misc/string_helpers.h" + +namespace neurun +{ +namespace compiler +{ + +std::unique_ptr<BackendResolver> ManualScheduler::schedule(const graph::Graph &graph) +{ + auto backend_resolver = nnfw::cpp14::make_unique<compiler::BackendResolver>( + graph.operands(), backend::BackendManager::instance().getAll(), graph.getKernelRegistry()); + + // 1. Backend for All operations + const auto backend_all_str = util::getConfigString(util::config::OP_BACKEND_ALLOPS); + auto backend_all = backend::BackendManager::instance().get(backend_all_str); + + VERBOSE(ManualScheduler) << "Default backend for all ops: " << backend_all_str << std::endl; + + graph.operations().iterate([&](const model::OperationIndex &index, const model::Operation &) { + backend_resolver->setBackend(index, backend_all); + }); + + // 2. Backend per operation type + std::unordered_map<std::type_index, backend::Backend *> op_type_map; + // By default, CustomNode uses cpu backend + op_type_map[typeid(model::operation::CustomNode)] = + backend::BackendManager::instance().get("cpu"); +#define OP(InternalName, IsNnApi) \ + if (IsNnApi) \ + { \ + const auto &backend_str = util::getConfigString(util::config::OP_BACKEND_##InternalName); \ + if (!backend_str.empty()) \ + { \ + auto backend = backend::BackendManager::instance().get(backend_str); \ + VERBOSE(Lower) << "backend for " << #InternalName << ": " << backend_str << std::endl; \ + op_type_map[typeid(model::operation::InternalName)] = backend; \ + } \ + } +#include "model/Operations.lst" +#undef OP + graph.operations().iterate( + [&](const model::OperationIndex &index, const model::Operation &operation) { + auto itr = op_type_map.find(typeid(operation)); + if (itr != op_type_map.end()) + { + backend_resolver->setBackend(index, itr->second); + } + }); + + // 3. Backend per operation + try + { + auto map_str = util::getConfigString(util::config::OP_BACKEND_MAP); + auto key_val_list = nnfw::misc::split(map_str, ';'); + for (const auto &key_val_str : key_val_list) + { + if (key_val_str.empty()) + { + continue; + } + + auto key_val = nnfw::misc::split(key_val_str, '='); + const auto &key_str = key_val.at(0); + const auto &val = key_val.at(1); + auto key = static_cast<uint32_t>(std::stoi(key_str)); + + graph.operations().at(model::OperationIndex{key}); // Check if exist, or this wil throw + backend_resolver->setBackend(model::OperationIndex{key}, + backend::BackendManager::instance().get(val)); + } + } + catch (...) + { + VERBOSE(ManualScheduler) << "Invalid value from " << util::config::OP_BACKEND_MAP + << ". Some of the given values are ignored" << std::endl; + } + + // Dump final assignment + backend_resolver->iterate( + [&](const model::OperationIndex &index, const backend::BackendContext &backend_ctx) { + VERBOSE(ManualScheduler) << "backend for operation #" << index.value() << ": " + << backend_ctx.backend->config()->id() << std::endl; + }); + + return backend_resolver; +} + +} // namespace compiler +} // namespace neurun |