2 * Copyright (c) 2019 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 "ir/Operand.h"
18 #include "compiler/HEScheduler.h"
20 #include "util/ConfigSource.h"
21 #include "compiler/BackendResolver.h"
22 #include "util/logging.h"
23 #include "util/Utils.h"
24 #include "exec/FunctionSequence.h"
34 static uint32_t getOperationsFlattenedIOSize(const ir::Graph &graph, const ir::Operation &node)
37 for (const auto &ind : (node.getInputs() | ir::Remove::UNDEFINED) + node.getOutputs())
39 size += graph.operands().at(ind).info().total_size();
44 static bool isQuant(const ir::Graph &graph, const ir::Operation &node)
46 for (const auto &input : node.getInputs() | ir::Remove::UNDEFINED)
48 const auto &obj = graph.operands().at(input);
49 if (obj.typeInfo().type() == ir::DataType::QUANT_UINT8_ASYMM)
57 static bool isWorkaroundSkip(const ir::Graph &, const backend::Backend *, const ir::Operation &,
60 // Now, there is no workaround
64 // if a node can be merged into op_seq
65 static bool isMergeable(const ir::Graph &graph, const ir::Operation &node)
67 size_t prev_op_cnt = 0;
68 for (const auto &input : node.getInputs())
71 const auto &operand = graph.operands().at(input);
72 if (operand.isConstant())
75 // This operand is output of operation, not weight or bias
76 if (operand.getDef().valid())
79 // Current node has multiple inputs as concat or at the beginning of the separated branch
80 if (prev_op_cnt > 1 || operand.getUses().size() > 1)
88 void HEScheduler::scheduleShufflingBackends()
90 VERBOSE(HEScheduler::schedule)
91 << "Started task scheduling: uses all backends to get more metrics for data transfer"
93 size_t backend_ind = 0;
94 for (const auto &rank : _rank_to_op)
96 VERBOSE(HEScheduler::schedule) << "scheduling (" << rank.second.value() << ")" << std::endl;
97 const auto &node = _graph->operations().at(rank.second);
98 const bool quant = isQuant(*_graph, node);
99 const auto size = getOperationsFlattenedIOSize(*_graph, node);
100 for (size_t i = 0;; ++i)
102 if (i == _all_backends.size())
104 // wasn't able to find backend
108 if (backend_ind == _all_backends.size())
112 if (isWorkaroundSkip(*_graph, _all_backends[backend_ind], node, quant))
117 const auto exec_time =
118 _exec_time->getOperationExecTime(_all_backends[backend_ind], node.name(), quant, size);
119 // Scheduling to measure data transfer must be done after measuring all backends separately
120 assert(exec_time != _exec_time->NOT_FOUND);
121 if (exec_time == _exec_time->getMax())
126 _backend_resolver->setBackend(rank.second, _all_backends[backend_ind]);
127 VERBOSE(HEScheduler::schedule) << "backend for " << node.name() << " is "
128 << _all_backends[backend_ind]->config()->id() << std::endl;
135 bool HEScheduler::isNodeProfiled(const ir::Operation &node)
137 const bool quant = isQuant(*_graph, node);
138 const auto size = getOperationsFlattenedIOSize(*_graph, node);
139 for (const auto *backend : _all_backends)
141 const auto exec_time = _exec_time->getOperationExecTime(backend, node.name(), quant, size);
142 if (exec_time == _exec_time->NOT_FOUND)
148 void HEScheduler::scheduleBranch(const ir::OperationIndex &index,
149 ir::OperationIndexMap<bool> &scheduled)
151 auto loc_index = index;
152 const backend::Backend *parent_backend = nullptr;
155 if (scheduled[loc_index])
159 if (!schedule(loc_index, parent_backend))
163 scheduled[loc_index] = true;
164 parent_backend = _backend_resolver->getBackend(loc_index);
166 const auto &node = _graph->operations().at(loc_index);
167 /* get the only output operand, that is input of the next single operation
168 * and just this nodes output.*/
169 if (node.getOutputs().size() != 1)
173 const auto &only_out_operand = _graph->operands().at(*node.getOutputs().begin());
174 // One of the last nodes
175 if (only_out_operand.getUses().size() == 0)
179 loc_index = *only_out_operand.getUses().begin();
180 /* verify, that next node is neither beginning nor ending node of a branch*/
181 const auto &next_node = _graph->operations().at(loc_index);
182 if (!isMergeable(*_graph, next_node))
189 std::unique_ptr<compiler::BackendResolver> HEScheduler::schedule(const ir::Graph &graph)
192 VERBOSE(HEScheduler::schedule) << "task scheduling started" << std::endl;
193 // Make ranks and save in descending order
196 for (const auto *backend : _all_backends)
198 _backends_avail_time.emplace(backend, std::map<int64_t, int64_t>{{0, 0}});
201 if (_is_profiling_mode)
203 // Check if profiling info about all backend/node pairs already exists
204 bool all_nodes_are_profiled = true;
205 _graph->operations().iterate([&](const ir::OperationIndex &, const ir::Operation &op) {
206 if (all_nodes_are_profiled)
207 all_nodes_are_profiled = isNodeProfiled(op);
210 // If all nodes are already profiled - schedule backends in such order, so more profiling
211 // information about between-backends data transfer could be collected
212 if (all_nodes_are_profiled)
214 scheduleShufflingBackends();
215 VERBOSE(HEScheduler::schedule) << "task scheduling finished" << std::endl;
216 return std::move(_backend_resolver);
220 ir::OperationIndexMap<bool> visited;
221 graph.operations().iterate(
222 [&](const ir::OperationIndex &index, const ir::Operation &) { visited[index] = false; });
223 // for each task select the backend with the smallest earliest finishing time(eft)
224 for (const auto &rank : _rank_to_op)
226 scheduleBranch(rank.second, visited);
228 VERBOSE(HEScheduler::schedule) << "task scheduling finished" << std::endl;
229 return std::move(_backend_resolver);
232 int64_t HEScheduler::getOpTime(const backend::Backend *backend, const std::string &operation,
233 bool quant, uint32_t size)
235 const auto time = _exec_time->getOperationExecTime(backend, operation, quant, size);
236 if (time != _exec_time->NOT_FOUND)
239 return _is_supported.at(backend).at(operation) ? 1 : _exec_time->getMax();
242 int64_t HEScheduler::getPermuteTime(const backend::Backend *src_backend,
243 const backend::Backend *dst_backend, bool quant, uint32_t size)
245 // TODO Change it to getOperationExecTime()
246 const auto time = _exec_time->getPermuteTime(src_backend, dst_backend, quant, size);
248 if (time != _exec_time->NOT_FOUND)
251 // Makes the scheduler prefer keeping computations on one backend
255 int64_t HEScheduler::tryBackend(const ir::Operation &node, const backend::Backend *backend)
257 // if there is no profiling info don't use this backend during scheduling
258 if (!_is_profiling_mode)
260 VERBOSE(HEScheduler::tryBackend)
261 << "Trying to HE schedule while there is no profiling info for " << node.name()
262 << " on backend " << backend->config()->id() << ". So this backend won't be used. "
264 _is_supported[backend][node.name()] = false;
265 return _exec_time->getMax();
267 auto iter = _is_supported.find(backend);
268 if (iter != _is_supported.end())
270 auto it2 = iter->second.find(node.name());
271 if (it2 != iter->second.end())
273 return _is_supported[backend][node.name()] ? 1 : _exec_time->getMax();
280 _is_supported[backend][node.name()] = true;
282 catch (std::runtime_error &e)
284 _is_supported[backend][node.name()] = false;
286 return _is_supported[backend][node.name()] ? 1 : _exec_time->getMax();
289 void HEScheduler::makeRank()
291 VERBOSE(HEScheduler::makeRank) << "task prioritizing" << std::endl;
293 _graph->operations().iterate(
294 [&](const ir::OperationIndex &index, const ir::Operation &) { DFSMaxRank(index); });
296 // Check that ranks are calculated for all operations(nodes)
297 _graph->operations().iterate([&](const ir::OperationIndex &index, const ir::Operation &) {
298 UNUSED_RELEASE(index);
299 assert(_op_to_rank->find(index) != _op_to_rank->end());
301 VERBOSE(HEScheduler::makeRank) << "task prioritizing finished" << std::endl;
304 int64_t HEScheduler::DFSMaxRank(const ir::OperationIndex &index)
306 auto op_to_rank_it = _op_to_rank->find(index);
307 if (op_to_rank_it != _op_to_rank->end())
308 return op_to_rank_it->second;
310 const auto &node = _graph->operations().at(index);
312 const bool quant = isQuant(*_graph, node);
313 const auto size = getOperationsFlattenedIOSize(*_graph, node);
314 auto supported_backends_quantity = static_cast<int64_t>(_all_backends.size());
316 const auto max_child_rank = DFSChildrenMaxRank(index);
318 // get average exec time of this op
319 for (const auto &backend : _all_backends)
321 auto exec_time = _exec_time->getOperationExecTime(backend, node.name(), quant, size);
322 if (exec_time == _exec_time->NOT_FOUND)
324 exec_time = tryBackend(node, backend);
326 if (exec_time < _exec_time->getMax())
332 // this operation isn't supported in this backend
333 --supported_backends_quantity;
336 if (supported_backends_quantity == 0)
338 throw std::runtime_error{"Encountered unsupported op: " + node.name()};
340 rank /= supported_backends_quantity;
342 // get standard deviation
344 for (const auto backend : _all_backends)
346 const auto exec_time = getOpTime(backend, node.name(), quant, size);
347 if (exec_time < _exec_time->getMax())
349 std += (exec_time - rank) * (exec_time - rank);
352 std /= supported_backends_quantity;
355 std = static_cast<int>(std::sqrt(std));
358 rank += max_child_rank;
361 _rank_to_op.emplace(rank, index);
362 _op_to_rank->emplace(index, rank);
363 VERBOSE(HEScheduler::DFSMaxRank) << "rank of operation (" << index.value() << ")" << node.name()
364 << " is " << rank << std::endl;
369 int64_t HEScheduler::DFSChildrenMaxRank(const ir::OperationIndex &index)
371 const auto &node = _graph->operations().at(index);
372 int64_t max_child_rank = 0;
373 for (const auto &output : node.getOutputs())
375 const auto &operand = _graph->operands().at(output);
376 const bool quant = operand.typeInfo().type() == ir::DataType::QUANT_UINT8_ASYMM;
377 // average data transfer cost of this operand's data
378 int64_t avg_transfer_cost = 1;
379 for (const auto *backend : _all_backends)
381 for (const auto *other_backend : _all_backends)
383 if (backend == other_backend)
387 // TODO Change it to controlflow backend
389 getPermuteTime(backend, other_backend, quant, operand.info().total_size());
390 avg_transfer_cost += transfer_cost;
393 avg_transfer_cost /= _all_backends.size();
394 for (const auto &use : operand.getUses())
396 const auto cur_child_rank = DFSMaxRank(use);
397 max_child_rank = std::max(max_child_rank, cur_child_rank + avg_transfer_cost);
400 return max_child_rank;
403 int64_t HEScheduler::backendAvailableTime(const backend::Backend *backend,
404 const int64_t &starting_time, const int64_t &time_amount)
406 const auto backend_times = _backends_avail_time.at(backend);
407 // finishing and starting times of an op, that will come after current op
408 auto next_op_fst = backend_times.upper_bound(starting_time);
409 // finishing time of an op, that will come before current op
410 auto prev_op_ft = starting_time;
411 // until reach the "hole/gap", that is enough to run this op
412 while (next_op_fst != backend_times.end() && next_op_fst->second - prev_op_ft <= time_amount)
414 prev_op_ft = next_op_fst->first + 1;
420 bool HEScheduler::schedule(const ir::OperationIndex &index, const backend::Backend *parent_backend)
422 VERBOSE(HEScheduler::schedule) << "scheduling (" << index.value() << ")" << std::endl;
423 int64_t eft = std::numeric_limits<int64_t>::max(), selected_exec_time = 0;
424 const auto &node = _graph->operations().at(index);
426 std::multimap<int64_t, int64_t> selected_transfer_st_exec_time;
427 // select the backend with the smallest eft of this task
428 const backend::Backend *chosen_backend = nullptr;
429 for (const auto *backend : _all_backends)
431 std::multimap<int64_t, int64_t> transfer_st_exec_time;
432 const auto est_and_et = ESTAndExecTime(backend, index, transfer_st_exec_time);
434 if (eft > est_and_et.first + est_and_et.second)
436 eft = est_and_et.first + est_and_et.second;
437 selected_exec_time = est_and_et.second;
438 chosen_backend = backend;
439 selected_transfer_st_exec_time = transfer_st_exec_time;
443 if (chosen_backend == nullptr)
445 throw std::runtime_error{"Fail to choose backend on scheduler"};
448 // this is part of a branch and it is assigned another backend
449 if (parent_backend && parent_backend != chosen_backend)
453 for (const auto &it : selected_transfer_st_exec_time)
455 auto prev_op_ft = backendAvailableTime(_cpu_backend, it.first, it.second);
456 _backends_avail_time[_cpu_backend].insert({prev_op_ft + it.second, prev_op_ft});
459 _ops_eft[index] = eft;
460 _backends_avail_time[chosen_backend].emplace(eft, eft - selected_exec_time);
461 _backend_resolver->setBackend(index, chosen_backend);
463 VERBOSE(HEScheduler::schedule) << "backend for " << node.name() << " is "
464 << chosen_backend->config()->id() << ". Its eft: " << eft
469 std::pair<int64_t, int64_t>
470 HEScheduler::ESTAndExecTime(const backend::Backend *backend, const ir::OperationIndex &index,
471 std::multimap<int64_t, int64_t> &transfer_st_exec_time)
473 // Permutation will cause creating a separate op_seq that contains just this permutation node.
474 // This isn't needed for Linear executor since it doesn't use op_seqs
475 // Number 1 ms is picked experimentally
476 int64_t permute_fine = 1000;
477 // Multiply cpu operations' exec time by 2 because in parallel executor it might be busy with
478 // permutation on other branches or non-nnfw specific tasks and have to wait for it.
479 // Number 2 is picked experimentally
480 const int64_t CPU_DELAY = 2;
481 const auto &node = _graph->operations().at(index);
482 const bool quant = isQuant(*_graph, node);
483 const auto size = getOperationsFlattenedIOSize(*_graph, node);
484 // if this node can be part of a op_seq, then assigning different backend will cause creating
486 if (isMergeable(*_graph, node))
490 if (isWorkaroundSkip(*_graph, backend, node, quant))
492 return {_exec_time->getMax(), _exec_time->getMax()};
494 // get average exec time of the op on this backend
495 auto exec_time = getOpTime(backend, node.name(), quant, size);
496 if (backend->config()->id() == "cpu" && _is_parallel_exec)
498 exec_time *= CPU_DELAY;
501 // get max eft of direct (one level above) predecessors
502 auto max_pred_eft = predMaxEFT(backend, node, transfer_st_exec_time);
504 int64_t total_transfer_cost = 0;
505 std::vector<std::multimap<int64_t, int64_t>::iterator> inserted_permutations;
506 // Find free time for data transferring and insert it into backend taskset. This is needed:
507 // 1. Time for multiple permutations for this node's input is found correctly
508 // 2. If backend==cpu, then free time for this node must come after permutations
509 for (auto &it : transfer_st_exec_time)
511 if (_is_parallel_exec)
513 it.second *= CPU_DELAY;
515 if (!_is_linear_exec)
517 it.second += permute_fine;
519 total_transfer_cost += it.second;
521 const auto prev_op_ft = backendAvailableTime(_cpu_backend, it.first, it.second);
523 max_pred_eft = std::max(max_pred_eft, prev_op_ft + it.second);
525 const auto tmp = _backends_avail_time[_cpu_backend].emplace(prev_op_ft + it.second, prev_op_ft);
526 inserted_permutations.push_back(tmp.first);
528 // find the hole/gap, where this op can be put or the finishing time of the last assigned op
529 auto prev_op_ft = backendAvailableTime(backend, max_pred_eft, exec_time);
531 // Remove inserted permutation from cpu's task set
532 for (const auto &it : inserted_permutations)
534 _backends_avail_time[_cpu_backend].erase(it);
537 /* In case non-parallel executor measure just exec time and data transfer time
538 * because EFT(prev_op_ft) is the same for all backends. Since two operations
539 * can't be run simultaneously, finish of running operation must be waited for.
540 * When an operation starts, all backends are free. So, they need time just for
542 if (!_is_parallel_exec)
544 VERBOSE(HEScheduler::ESTAndExecTime)
545 << "exec_time of (" << index.value() << ") " << node.name() << " quant==" << quant << " on "
546 << backend->config()->id() << " is " << exec_time
547 << " microseconds. Data transfer cost: " << total_transfer_cost << std::endl;
549 return {total_transfer_cost, exec_time};
551 VERBOSE(HEScheduler::ESTAndExecTime)
552 << "exec_time of (" << index.value() << ") " << node.name() << " quant==" << quant << " on "
553 << backend->config()->id() << ": " << exec_time
554 << " microseconds. Backend available time: " << prev_op_ft
555 << " Parent's max eft: " << max_pred_eft - total_transfer_cost
556 << " data transfer cost: " << total_transfer_cost << std::endl;
558 return {prev_op_ft, exec_time};
561 int64_t HEScheduler::predMaxEFT(const backend::Backend *backend, const ir::Operation &node,
562 std::multimap<int64_t, int64_t> &transfer_st_exec_time)
564 int64_t max_pred_eft = 0;
565 for (const auto &input_operand_idx : node.getInputs())
567 const auto &input_operand = _graph->operands().at(input_operand_idx);
568 const bool quant = input_operand.typeInfo().type() == ir::DataType::QUANT_UINT8_ASYMM;
570 auto input_node_idx = input_operand.getDef();
571 if (input_node_idx.valid())
573 // Data transfer cost from parent's node backend to current node's backend:
574 auto parent_backend = _backend_resolver->getBackend(input_node_idx);
576 max_pred_eft = std::max(max_pred_eft, _ops_eft.at(input_node_idx));
577 if (parent_backend != backend)
579 // Multiply operand size by 2 because size must describe input+output size
580 int64_t transfer_cost =
581 getPermuteTime(parent_backend, backend, quant, input_operand.info().total_size() * 2);
582 transfer_st_exec_time.emplace(_ops_eft.at(input_node_idx), transfer_cost);
589 } // namespace compiler