1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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
|