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_ITENSOR_BUILDER_H__
18 #define __ONERT_BACKEND_ITENSOR_BUILDER_H__
23 #include "ir/OperandInfo.h"
24 #include "ir/Operation.h"
25 #include "ir/Layout.h"
27 #include "ITensorManager.h"
28 #include "ITensorRegistry.h"
29 #include "IDynamicTensorManager.h"
38 using IterateFunction = std::function<void(const ir::OperandIndex &)>;
40 virtual ~ITensorBuilder(void) = default;
43 * @brief Returns true if this TensorBuilder support dynamic tensor
45 virtual bool supportDynamicTensor() = 0;
48 * @brief Register tensor information to allocate on backend
52 * @param backend_layout Backend layout
53 * @param as_const Whether this tensor is constant
55 virtual void registerTensorInfo(const ir::OperandIndex &ind, const ir::OperandInfo &info,
56 ir::Layout backend_layout) = 0;
59 * @brief Check if the tensor has been registered with @c registerTensorInfo
61 * @return true If the tensor has been registered
62 * @return false Otherwise
64 virtual bool isRegistered(const ir::OperandIndex &) const = 0;
67 * @brief Get tensor registry
69 * @return std::shared_ptr<backend::ITensorRegistry> tensor registry object
71 * @note Backend should implement this when it has StaticTensorManager and DynamicTensorManager
73 virtual std::shared_ptr<backend::ITensorRegistry> tensorRegistry() = 0;
75 public: // methods for static tensor allocation
77 * @brief Let the tensor builder know first use(start of lifetime) of a tensor
78 * Must be called before calling @c prepare
79 * Must be run up to once for each tensor before calling @c notifyLastUse
80 * NOTE: Useful only for static models
82 virtual void notifyFirstUse(const ir::OperandIndex &) = 0;
84 * @brief Let the tensor builder know last use(end of lifetime) of a tensor
85 * Must be run up to once for each tensor after calling @c notifyFirstUse
86 * NOTE: Useful only for static models
88 virtual void notifyLastUse(const ir::OperandIndex &) = 0;
90 * @brief Prepare the tensors
91 * Before calling this, all the tensors must be registered
93 virtual void prepare(void) = 0;
95 * @brief Allocate the tensors
96 * Before calling this, @c prepare must be called
98 virtual void allocate() = 0;
100 * @brief Some actions after functions' @c IFunction::prepare method.
101 * This is called right after each function's @c IFunction::prepare function has been
104 virtual void postFunctionPrepare() = 0;
107 * @brief Get the tensor object
109 * @param ind Index of the tensor
110 * @return std::shared_ptr<ITensor> The tensor object
112 virtual std::shared_ptr<ITensor> tensorAt(const ir::OperandIndex &ind) = 0;
115 * @brief Set the External Tensor object
117 * @return true if succeeded
118 * @return false if failed or unsupported
120 virtual bool setExternalTensor(const ir::OperandIndex &, const std::shared_ptr<IPortableTensor> &)
126 * @brief Iterate over tensors
128 * @param fn The function to be run
130 virtual void iterate(const IterateFunction &fn) = 0;
133 * @brief Release static @c ITensorManger object which was built
134 * Before calling this, @c allocate must have been called
136 * @return std::unique_ptr<ITensorManager> Tensor Manager object
138 virtual std::unique_ptr<ITensorManager> releaseStaticTensorManager(void) = 0;
140 public: // methods for dynamic tensor allocation
142 * @brief Get dynamicTensorManager. If a backend does not support dynamic tensor, exception
145 * @return pointer of IDynamicTensorManager object
147 * @note Since it is a pointer, its life time is from the cration of TensorBuilder
148 * to the end of execution
150 virtual IDynamicTensorManager *dynamicTensorManager(void)
152 throw std::runtime_error("dynamicTensorManager(): NYI");
156 * @brief Release dynamic @c ITensorManger object which was built
157 * Before calling this, @c allocate must have been called
159 * @return std::unique_ptr<ITensorManager> Tensor Manager object
161 virtual std::unique_ptr<ITensorManager> releaseDynamicTensorManager(void)
163 throw std::runtime_error("releaseDynamicTensorManager() for this backend is not supported");
167 } // namespace backend
170 #endif // __ONERT_BACKEND_ITENSOR_BUILDER_H__