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 "exec/FunctionSequence.h"
19 #include "ir/Operation.h"
20 #include "backend/IDynamicTensorManager.h"
21 #include "backend/ITensorRegistry.h"
22 #include "util/logging.h"
29 void FunctionSequence::run()
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)
34 if (_dynamic_tensor_ctx->op_seq->size() != _functions.size())
35 throw std::runtime_error("operation and functions should be mapped one by one");
37 auto op_seq_iter = _dynamic_tensor_ctx->op_seq->begin();
38 for (const auto &function : _functions)
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);
44 auto *sub_func_seq = dynamic_cast<FunctionSequence *>(function.get());
45 if (sub_func_seq != nullptr)
47 sub_func_seq->enableDynamicShapeInferer(true);
48 sub_func_seq->dynamic_tensor_ctx(dynamic_tensor_ctx());
54 // deallocate input tensors which is no longer used
55 _dynamic_tensor_ctx->dynamic_tensor_manager->deallocInput(*op_seq_iter);
62 for (const auto &function : _functions)
64 auto *sub_func_seq = dynamic_cast<FunctionSequence *>(function.get());
65 if (sub_func_seq != nullptr)
67 sub_func_seq->enableDynamicShapeInferer(false);
74 void FunctionSequence::prepare()
76 for (const auto &function : _functions)
82 void FunctionSequence::append(std::unique_ptr<IFunction> &&function)
84 _functions.push_back(std::move(function));
87 void FunctionSequence::iterate(const std::function<void(IFunction &)> &fn)
89 for (const auto &func : _functions)