1 /* 2 * Copyright (c) 2017-2020 Arm Limited. 3 * 4 * SPDX-License-Identifier: MIT 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a copy 7 * of this software and associated documentation files (the "Software"), to 8 * deal in the Software without restriction, including without limitation the 9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 10 * sell copies of the Software, and to permit persons to whom the Software is 11 * furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in all 14 * copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 * SOFTWARE. 23 */ 24 #ifndef ARM_COMPUTE_SUBTENSORINFO_H 25 #define ARM_COMPUTE_SUBTENSORINFO_H 26 27 #include "arm_compute/core/ITensorInfo.h" 28 29 #include "arm_compute/core/Coordinates.h" 30 #include "arm_compute/core/Helpers.h" 31 #include "arm_compute/core/Strides.h" 32 #include "arm_compute/core/TensorInfo.h" 33 #include "arm_compute/core/TensorShape.h" 34 35 #include <cstddef> 36 #include <memory> 37 38 namespace arm_compute 39 { 40 /** Store the sub tensor's metadata */ 41 class SubTensorInfo final : public ITensorInfo 42 { 43 public: 44 /** Default constructor */ 45 SubTensorInfo(); 46 /** Default constructor 47 * 48 * @param[in] parent Metadata of parent tensor. 49 * @param[in] tensor_shape Tensor shape. Shape must fit inside parent's shape. 50 * X and Y dimensions must match the parent's ones. 51 * @param[in] coords Coordinates of starting element inside parent tensor. 52 * @param[in] extend_parent (Optional) Extend parent with subtensor shape if subtensor indexes out of bounds 53 */ 54 SubTensorInfo(ITensorInfo *parent, TensorShape tensor_shape, Coordinates coords, bool extend_parent = false); 55 /** Default destructor */ 56 ~SubTensorInfo() = default; 57 /** Allow instances of this class to be copy constructed */ 58 SubTensorInfo(const SubTensorInfo &) = default; 59 /** Allow instances of this class to be copied */ 60 SubTensorInfo &operator=(const SubTensorInfo &) = default; 61 /** Allow instances of this class to be move constructed */ 62 SubTensorInfo(SubTensorInfo &&) = default; 63 /** Allow instances of this class to be moved */ 64 SubTensorInfo &operator=(SubTensorInfo &&) = default; 65 /** Returns the coordinates of the sub-tensor inside the parent tensor 66 * 67 * @return Sub-tensor coordinates 68 */ coords()69 Coordinates coords() const 70 { 71 return _coords; 72 } 73 74 // Inherited methods overridden: 75 std::unique_ptr<ITensorInfo> clone() const override; set_data_type(DataType data_type)76 ITensorInfo &set_data_type(DataType data_type) override 77 { 78 ARM_COMPUTE_ERROR_ON(_parent == nullptr); 79 _parent->set_data_type(data_type); 80 return *this; 81 }; set_data_layout(const DataLayout & data_layout)82 ITensorInfo &set_data_layout(const DataLayout &data_layout) override 83 { 84 ARM_COMPUTE_ERROR_ON(_parent == nullptr); 85 _parent->set_data_layout(data_layout); 86 return *this; 87 }; set_num_channels(int num_channels)88 ITensorInfo &set_num_channels(int num_channels) override 89 { 90 ARM_COMPUTE_ERROR_ON(_parent == nullptr); 91 _parent->set_num_channels(num_channels); 92 return *this; 93 }; set_format(Format format)94 ITensorInfo &set_format(Format format) override 95 { 96 ARM_COMPUTE_ERROR_ON(_parent == nullptr); 97 _parent->set_format(format); 98 return *this; 99 }; 100 ITensorInfo &set_tensor_shape(const TensorShape &shape) override; set_quantization_info(const QuantizationInfo & quantization_info)101 ITensorInfo &set_quantization_info(const QuantizationInfo &quantization_info) override 102 { 103 ARM_COMPUTE_ERROR_ON(_parent == nullptr); 104 _parent->set_quantization_info(quantization_info); 105 return *this; 106 } reset_padding()107 ITensorInfo &reset_padding() override 108 { 109 ARM_COMPUTE_ERROR_ON(_parent == nullptr); 110 _parent->reset_padding(); 111 return *this; 112 } auto_padding()113 bool auto_padding() override 114 { 115 ARM_COMPUTE_ERROR_ON(_parent == nullptr); 116 return _parent->auto_padding(); 117 }; 118 bool extend_padding(const PaddingSize &padding) override; dimension(size_t index)119 size_t dimension(size_t index) const override 120 { 121 return _tensor_shape[index]; 122 } dimension(DataLayoutDimension dimension)123 size_t dimension(DataLayoutDimension dimension) const override 124 { 125 ARM_COMPUTE_ERROR_ON(_parent == nullptr); 126 return get_data_layout_dimension_index(_parent->data_layout(), dimension); 127 } strides_in_bytes()128 const Strides &strides_in_bytes() const override 129 { 130 ARM_COMPUTE_ERROR_ON(_parent == nullptr); 131 return _parent->strides_in_bytes(); 132 } offset_first_element_in_bytes()133 size_t offset_first_element_in_bytes() const override 134 { 135 ARM_COMPUTE_ERROR_ON(_parent == nullptr); 136 return _parent->offset_element_in_bytes(_coords); 137 } 138 int32_t offset_element_in_bytes(const Coordinates &pos) const override; element_size()139 size_t element_size() const override 140 { 141 ARM_COMPUTE_ERROR_ON(_parent == nullptr); 142 return _parent->element_size(); 143 } num_dimensions()144 size_t num_dimensions() const override 145 { 146 return _tensor_shape.num_dimensions(); 147 } num_channels()148 size_t num_channels() const override 149 { 150 ARM_COMPUTE_ERROR_ON(_parent == nullptr); 151 return _parent->num_channels(); 152 } tensor_shape()153 const TensorShape &tensor_shape() const override 154 { 155 ARM_COMPUTE_ERROR_ON(_parent == nullptr); 156 return _tensor_shape; 157 } data_type()158 DataType data_type() const override 159 { 160 ARM_COMPUTE_ERROR_ON(_parent == nullptr); 161 return _parent->data_type(); 162 } format()163 Format format() const override 164 { 165 ARM_COMPUTE_ERROR_ON(_parent == nullptr); 166 return _parent->format(); 167 } total_size()168 size_t total_size() const override 169 { 170 ARM_COMPUTE_ERROR_ON(_parent == nullptr); 171 return _parent->total_size(); 172 } padding()173 PaddingSize padding() const override 174 { 175 ARM_COMPUTE_ERROR_ON(_parent == nullptr); 176 return _parent->padding(); 177 } has_padding()178 bool has_padding() const override 179 { 180 ARM_COMPUTE_ERROR_ON(_parent == nullptr); 181 return _parent->has_padding(); 182 } is_resizable()183 bool is_resizable() const override 184 { 185 ARM_COMPUTE_ERROR_ON(_parent == nullptr); 186 return _parent->is_resizable(); 187 } is_dynamic()188 bool is_dynamic() const override 189 { 190 ARM_COMPUTE_ERROR_ON(_parent == nullptr); 191 return _parent->is_dynamic(); 192 } set_is_resizable(bool is_resizable)193 ITensorInfo &set_is_resizable(bool is_resizable) override 194 { 195 ARM_COMPUTE_ERROR_ON(_parent == nullptr); 196 _parent->set_is_resizable(is_resizable); 197 return *this; 198 } set_is_dynamic(bool is_dynamic)199 ITensorInfo &set_is_dynamic(bool is_dynamic) override 200 { 201 ARM_COMPUTE_ERROR_ON(_parent == nullptr); 202 _parent->set_is_dynamic(is_dynamic); 203 return *this; 204 } valid_region()205 ValidRegion valid_region() const override 206 { 207 return _valid_region; 208 } set_valid_region(const ValidRegion & valid_region)209 void set_valid_region(const ValidRegion &valid_region) override 210 { 211 ARM_COMPUTE_ERROR_ON(_parent == nullptr); 212 // Check if subtensor is valid if parent is configured 213 if(_parent->tensor_shape().total_size() != 0) 214 { 215 ARM_COMPUTE_ERROR_ON_INVALID_SUBTENSOR_VALID_REGION(_parent->valid_region(), valid_region); 216 } 217 _valid_region = valid_region; 218 } quantization_info()219 QuantizationInfo quantization_info() const override 220 { 221 ARM_COMPUTE_ERROR_ON(_parent == nullptr); 222 return _parent->quantization_info(); 223 } data_layout()224 DataLayout data_layout() const override 225 { 226 ARM_COMPUTE_ERROR_ON(_parent == nullptr); 227 return _parent->data_layout(); 228 } 229 230 private: 231 ITensorInfo *_parent; 232 TensorShape _tensor_shape; 233 Coordinates _coords; 234 ValidRegion _valid_region; 235 bool _extend_parent; 236 }; 237 } // namespace arm_compute 238 #endif /*ARM_COMPUTE_SUBTENSORINFO_H */ 239