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