Imported Upstream version 1.9.0
[platform/core/ml/nnfw.git] / runtime / onert / core / src / exec / FunctionSequence.cc
1 /*
2  * Copyright (c) 2019 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 "exec/FunctionSequence.h"
18
19 #include "ir/Operation.h"
20 #include "backend/IDynamicTensorManager.h"
21 #include "backend/ITensorRegistry.h"
22 #include "util/logging.h"
23
24 namespace onert
25 {
26 namespace exec
27 {
28
29 void FunctionSequence::run()
30 {
31   // TODO Find out when `_enable_dynamic_shape_inferer` is true but `_dynamic_tensor_ctx` is false
32   if (_enable_dynamic_shape_inferer && _dynamic_tensor_ctx)
33   {
34     if (_dynamic_tensor_ctx->op_seq->size() != _functions.size())
35       throw std::runtime_error("operation and functions should be mapped one by one");
36
37     auto op_seq_iter = _dynamic_tensor_ctx->op_seq->begin();
38     for (const auto &function : _functions)
39     {
40       // set shape of output and allocate memory when needed
41       auto &op = _dynamic_tensor_ctx->operations->at(*op_seq_iter);
42       op.accept(*_dynamic_tensor_ctx->dynamic_shape_inferer);
43
44       auto *sub_func_seq = dynamic_cast<FunctionSequence *>(function.get());
45       if (sub_func_seq != nullptr)
46       {
47         sub_func_seq->enableDynamicShapeInferer(true);
48         sub_func_seq->dynamic_tensor_ctx(dynamic_tensor_ctx());
49       }
50
51       // run kernel
52       function->run();
53
54       // deallocate input tensors which is no longer used
55       _dynamic_tensor_ctx->dynamic_tensor_manager->deallocInput(*op_seq_iter);
56
57       op_seq_iter++;
58     }
59   }
60   else
61   {
62     for (const auto &function : _functions)
63     {
64       auto *sub_func_seq = dynamic_cast<FunctionSequence *>(function.get());
65       if (sub_func_seq != nullptr)
66       {
67         sub_func_seq->enableDynamicShapeInferer(false);
68       }
69       function->run();
70     }
71   }
72 }
73
74 void FunctionSequence::prepare()
75 {
76   for (const auto &function : _functions)
77   {
78     function->prepare();
79   }
80 }
81
82 void FunctionSequence::append(std::unique_ptr<IFunction> &&function)
83 {
84   _functions.push_back(std::move(function));
85 }
86
87 void FunctionSequence::iterate(const std::function<void(IFunction &)> &fn)
88 {
89   for (const auto &func : _functions)
90   {
91     fn(*func);
92   }
93 }
94
95 } // namespace exec
96 } // namespace onert