Imported Upstream version 1.7.0
[platform/core/ml/nnfw.git] / runtime / onert / core / include / ir / OperandInfo.h
1 /*
2  * Copyright (c) 2018 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 /**
18  * @file  OperandInfo.h
19  * @brief This file contains OperandInfo class
20  */
21 #ifndef __ONERT_IR_OPERAND_INFO_H__
22 #define __ONERT_IR_OPERAND_INFO_H__
23
24 #include "ir/Shape.h"
25 #include "ir/TypeInfo.h"
26 #include "ir/Layout.h"
27
28 namespace onert
29 {
30 namespace ir
31 {
32
33 /**
34  * @brief enum class indicating when the memory for a tensor is allocated
35  */
36 enum class MemAllocType
37 {
38   /**
39    * @brief At compile time, shape for a tensor is known, thus requried memory capacity can be
40    *        calculated
41    */
42   STATIC,
43
44   /**
45    * @brief At kernel execution time, shape for a tensor is known, thus requried memory capacity
46    *        can be calculated
47    */
48   DYNAMIC
49 };
50
51 /**
52  * @brief Class to save tensor's shape and type
53  */
54 class OperandInfo
55 {
56 public:
57   /**
58    * @brief Construct a new OperandInfo object (deleted)
59    */
60   OperandInfo() = delete;
61
62   /**
63    * @brief     Construct a new OperandInfo object
64    * @param[in] shape     Tensor shape
65    * @param[in] typeInfo  Tensor data type
66    * @param[in] alloc_type  When the thesor needs memory allocation
67    */
68   OperandInfo(const Shape &shape, const TypeInfo &typeInfo, MemAllocType alloc_type,
69               bool is_const = false)
70       : _shape(shape), _typeInfo(typeInfo), _alloc_type(alloc_type), _const(is_const)
71   {
72     // DO NOTHING
73   }
74   /**
75    * @brief     Construct a new OperandInfo object
76    * @param[in] origin info for copy
77    */
78   OperandInfo(const OperandInfo &origin) = default;
79
80   /**
81    * @brief Create a static OperandInfo object
82    */
83   static OperandInfo createStaticInfo(const Shape &shape, const TypeInfo &typeInfo)
84   {
85     return OperandInfo(shape, typeInfo, MemAllocType::STATIC);
86   }
87
88 public:
89   /**
90    * @brief   Return tensor shape
91    * @return  Tensor shape
92    */
93   const Shape &shape() const { return _shape; }
94   /**
95    * @brief   Return mutable tensor shape
96    * @return  Tensor shape
97    */
98   Shape &shape() { return _shape; }
99   /**
100    * @brief set shape
101    */
102   void shape(const ir::Shape &new_shape) { _shape = new_shape; }
103   /**
104    * @brief   Return tensor data type info
105    * @return  Tensor data type
106    */
107   const TypeInfo &typeInfo() const { return _typeInfo; }
108   /**
109    * @brief   Set tensor data type
110    */
111   void type(const DataType type) { _typeInfo.type(type); }
112   /**
113    * @brief   Return size of tensor (bytes)
114    * @return  Tensor size
115    */
116   size_t total_size() const { return _shape.num_elements() * sizeOfDataType(_typeInfo.type()); }
117
118   MemAllocType memAllocType() const { return _alloc_type; }
119   void setAsConstant() { _const = true; }
120   bool isConstant() const
121   {
122     // Impossible case: constant and dynamic operand
123     assert(!(isDynamic() && _const));
124     return _const;
125   }
126   bool isDynamic() const { return _alloc_type == MemAllocType::DYNAMIC; }
127   void setDynamic() { _alloc_type = MemAllocType::DYNAMIC; }
128
129 private:
130   Shape _shape;
131   TypeInfo _typeInfo;
132
133   MemAllocType _alloc_type;
134   bool _const;
135 };
136
137 } // namespace ir
138 } // namespace onert
139
140 #endif // __ONERT_IR_OPERAND_INFO_H__