2 * Copyright (c) 2018 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 #ifndef __ONERT_BACKEND_CPU_TENSOR_BUILDER_H__
18 #define __ONERT_BACKEND_CPU_TENSOR_BUILDER_H__
20 #include <backend/cpu_common/DynamicTensorManager.h>
21 #include <backend/cpu_common/StaticTensorManager.h>
22 #include <backend/cpu_common/TensorRegistry.h>
23 #include <backend/cpu_common/Tensor.h>
25 #include <backend/ITensorBuilder.h>
26 #include <ir/OperandIndexMap.h>
28 #include <unordered_map>
37 class TensorBuilder : public ITensorBuilder
42 bool supportDynamicTensor() override { return true; }
45 * @brief Register tensor information to allocate on CPU backend
46 * @param[in] ind Operand index
47 * @param[in] info Operand information
48 * @param[in] layout Operand data layout
50 void registerTensorInfo(const ir::OperandIndex &ind, const ir::OperandInfo &info,
51 ir::Layout backend_layout) override;
53 void notifyFirstUse(const ir::OperandIndex &) override;
54 void notifyLastUse(const ir::OperandIndex &) override;
56 bool isRegistered(const ir::OperandIndex &) const override;
58 void prepare(void) override;
59 void allocate() override;
60 void postFunctionPrepare() override { /* DO NOTHING */}
63 * @brief Get tensor with a specific OperandIndex
65 * @return shared_ptr<ITensor> if a tensor with given OperandIndex exists. nullptr otherwise.
67 std::shared_ptr<ITensor> tensorAt(const ir::OperandIndex &ind) override;
69 void iterate(const IterateFunction &fn) override;
71 std::unique_ptr<ITensorManager> releaseStaticTensorManager(void) override;
73 IDynamicTensorManager *dynamicTensorManager(void) override { return _dynamic_tensor_mgr.get(); }
75 std::unique_ptr<ITensorManager> releaseDynamicTensorManager(void) override;
78 * @brief Get tensor with a specific OperandIndex.
79 * @param ind OperandIndex for the tensor. There must exist a tensor with this ind.
80 * If not, program will crash with assert or exception.
81 * @return shared_ptr<Tensor>
83 std::shared_ptr<cpu_common::Tensor> at(const ir::OperandIndex &ind);
84 std::shared_ptr<IPortableTensor> portableAt(const ir::OperandIndex &ind);
85 bool setExternalTensor(const ir::OperandIndex &ind,
86 const std::shared_ptr<IPortableTensor> &tensor) override;
88 std::shared_ptr<ITensorRegistry> tensorRegistry() override { return _tensor_reg; }
91 const std::shared_ptr<cpu_common::TensorRegistry> _tensor_reg;
92 std::unique_ptr<cpu_common::StaticTensorManager> _static_tensor_mgr;
93 std::unique_ptr<cpu_common::DynamicTensorManager> _dynamic_tensor_mgr;
94 ir::OperandIndexMap<ir::OperandInfo> _tensor_info_map;
98 } // namespace backend
101 #endif // __ONERT_BACKEND_CPU_TENSOR_BUILDER_H__