a10cc2bf9f952b8c5c3de4a909e102b2e2116611
[platform/core/ml/nnfw.git] / runtime / onert / core / src / backend / basic / TensorBuilder.cc
1 /*
2  * Copyright (c) 2020 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 <backend/basic/TensorBuilder.h>
18
19 #include <util/logging.h>
20
21 #include <cassert>
22
23 namespace onert
24 {
25 namespace backend
26 {
27 namespace basic
28 {
29
30 TensorBuilder::TensorBuilder(const std::shared_ptr<TensorRegistry> &tensor_reg)
31   : _tensor_reg{tensor_reg}, _dynamic_tensor_mgr{new DynamicTensorManager(_tensor_reg)},
32     _static_tensor_mgr{new StaticTensorManager(_tensor_reg, _dynamic_tensor_mgr.get())}
33 {
34   /* empty */
35 }
36
37 void TensorBuilder::registerTensorInfo(const ir::OperandIndex &ind, const ir::OperandInfo &info,
38                                        ir::Layout layout)
39 {
40   _tensor_info_map.emplace(ind, info);
41
42   // CPU backend supports only one layout as NHWC
43   assert(layout == ir::Layout::NHWC);
44   if (info.isDynamic())
45   {
46     _dynamic_tensor_mgr->buildTensor(ind, info, layout);
47   }
48   else
49   {
50     _static_tensor_mgr->buildTensor(ind, info, layout, info.isConstant());
51   }
52 }
53
54 void TensorBuilder::notifyFirstUse(const ir::OperandIndex &ind)
55 {
56   assert(_tensor_info_map.find(ind) != _tensor_info_map.end());
57   const auto tensor_info = _tensor_info_map.at(ind);
58
59   if (!_tensor_reg->getNativeTensor(ind)->is_dynamic())
60   {
61     const auto size = tensor_info.total_size();
62     _static_tensor_mgr->claimPlan(ind, size);
63   }
64 }
65
66 void TensorBuilder::notifyLastUse(const ir::OperandIndex &ind)
67 {
68   if (!_tensor_reg->getNativeTensor(ind)->is_dynamic())
69   {
70     _static_tensor_mgr->releasePlan(ind);
71   }
72 }
73
74 bool TensorBuilder::isRegistered(const ir::OperandIndex &ind) const
75 {
76   return _tensor_info_map.find(ind) != _tensor_info_map.end();
77 }
78
79 void TensorBuilder::allocate(void) { _static_tensor_mgr->allocateNonconsts(); }
80
81 } // namespace basic
82 } // namespace backend
83 } // namespace onert