f87c271f76b4484b10225dc4ef90e14c7ecab419
[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/ITensorRegistry.h"
21 #include "util/logging.h"
22
23 namespace onert
24 {
25 namespace exec
26 {
27
28 void FunctionSequence::run()
29 {
30   if (_enable_dynamic_shape_inferer && _dynamic_tensor_ctx)
31   {
32     // acl_cl and acl_neon backend don't support dynamic shape.
33     // _dynamic_tensor_ctx is always nullptr for acl_cl and acl_neon
34     // Thus, those two bakends cannot reach here.
35
36     // Do dynamic shape inference
37     _dynamic_tensor_ctx->op->accept(*_dynamic_tensor_ctx->dynamic_shape_inferer);
38
39     for (const auto &function : _functions)
40     {
41       // NOTE the function could be also FunctionSequence so we do this
42       // TODO Remove this or do this recursively
43       auto *sub_func_seq = dynamic_cast<FunctionSequence *>(function.get());
44       if (sub_func_seq != nullptr)
45       {
46         sub_func_seq->enableDynamicShapeInferer(true);
47         sub_func_seq->dynamic_tensor_ctx(dynamic_tensor_ctx());
48       }
49
50       // run kernel
51       function->run();
52     }
53   }
54   else
55   {
56     for (const auto &function : _functions)
57     {
58       function->run();
59     }
60   }
61 }
62
63 void FunctionSequence::prepare()
64 {
65   for (const auto &function : _functions)
66   {
67     function->prepare();
68   }
69 }
70
71 void FunctionSequence::append(std::unique_ptr<IFunction> &&function)
72 {
73   _functions.push_back(std::move(function));
74 }
75
76 void FunctionSequence::iterate(const std::function<void(IFunction &)> &fn)
77 {
78   for (const auto &func : _functions)
79   {
80     fn(*func);
81   }
82 }
83
84 } // namespace exec
85 } // namespace onert