Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 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" |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 30 | #include "arm_compute/core/Helpers.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 31 | #include "arm_compute/core/Strides.h" |
| 32 | #include "arm_compute/core/TensorInfo.h" |
| 33 | #include "arm_compute/core/TensorShape.h" |
| 34 | #include "arm_compute/core/Validate.h" |
| 35 | |
| 36 | #include <cstddef> |
Georgios Pinitas | 283c179 | 2017-11-10 18:14:06 +0000 | [diff] [blame] | 37 | #include <memory> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 38 | |
| 39 | namespace arm_compute |
| 40 | { |
| 41 | /** Store the sub tensor's metadata */ |
| 42 | class SubTensorInfo final : public ITensorInfo |
| 43 | { |
| 44 | public: |
| 45 | /** Default constructor */ |
| 46 | SubTensorInfo(); |
| 47 | /** Default constructor |
| 48 | * |
| 49 | * @param[in] parent Metadata of parent tensor. |
| 50 | * @param[in] tensor_shape Tensor shape. Shape must fit inside parent's shape. |
| 51 | * X and Y dimensions must match the parent's ones. |
| 52 | * @param[in] coords Coordinates of starting element inside parent tensor. |
| 53 | */ |
Georgios Pinitas | 283c179 | 2017-11-10 18:14:06 +0000 | [diff] [blame] | 54 | SubTensorInfo(ITensorInfo *parent, TensorShape tensor_shape, Coordinates coords); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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; |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 65 | /** Returns the coordinates of the sub-tensor inside the parent tensor |
| 66 | * |
| 67 | * @return Sub-tensor coordinates |
| 68 | */ |
| 69 | Coordinates coords() const |
| 70 | { |
| 71 | return _coords; |
| 72 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 73 | |
| 74 | // Inherited methods overridden: |
Georgios Pinitas | 283c179 | 2017-11-10 18:14:06 +0000 | [diff] [blame] | 75 | std::unique_ptr<ITensorInfo> clone() const override; |
| 76 | ITensorInfo &set_data_type(DataType data_type) override |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 77 | { |
| 78 | ARM_COMPUTE_ERROR_ON(_parent == nullptr); |
| 79 | _parent->set_data_type(data_type); |
Georgios Pinitas | 283c179 | 2017-11-10 18:14:06 +0000 | [diff] [blame] | 80 | return *this; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 81 | }; |
Georgios Pinitas | 283c179 | 2017-11-10 18:14:06 +0000 | [diff] [blame] | 82 | ITensorInfo &set_num_channels(int num_channels) override |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 83 | { |
| 84 | ARM_COMPUTE_ERROR_ON(_parent == nullptr); |
| 85 | _parent->set_num_channels(num_channels); |
Georgios Pinitas | 283c179 | 2017-11-10 18:14:06 +0000 | [diff] [blame] | 86 | return *this; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 87 | }; |
Georgios Pinitas | 283c179 | 2017-11-10 18:14:06 +0000 | [diff] [blame] | 88 | ITensorInfo &set_format(Format format) override |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 89 | { |
| 90 | ARM_COMPUTE_ERROR_ON(_parent == nullptr); |
| 91 | _parent->set_format(format); |
Georgios Pinitas | 283c179 | 2017-11-10 18:14:06 +0000 | [diff] [blame] | 92 | return *this; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 93 | }; |
Georgios Pinitas | 283c179 | 2017-11-10 18:14:06 +0000 | [diff] [blame] | 94 | ITensorInfo &set_fixed_point_position(int fixed_point_position) override |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 95 | { |
| 96 | ARM_COMPUTE_ERROR_ON(_parent == nullptr); |
| 97 | _parent->set_fixed_point_position(fixed_point_position); |
Georgios Pinitas | 283c179 | 2017-11-10 18:14:06 +0000 | [diff] [blame] | 98 | return *this; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 99 | }; |
Georgios Pinitas | 283c179 | 2017-11-10 18:14:06 +0000 | [diff] [blame] | 100 | ITensorInfo &set_tensor_shape(TensorShape shape) override; |
| 101 | ITensorInfo &set_quantization_info(QuantizationInfo quantization_info) override |
| 102 | { |
| 103 | ARM_COMPUTE_ERROR_ON(_parent == nullptr); |
| 104 | _parent->set_quantization_info(quantization_info); |
| 105 | return *this; |
| 106 | } |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 107 | ITensorInfo &reset_padding() override |
| 108 | { |
| 109 | ARM_COMPUTE_ERROR_ON(_parent == nullptr); |
| 110 | _parent->reset_padding(); |
| 111 | return *this; |
| 112 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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; |
| 119 | size_t dimension(size_t index) const override |
| 120 | { |
| 121 | return _tensor_shape[index]; |
| 122 | } |
| 123 | const Strides &strides_in_bytes() const override |
| 124 | { |
| 125 | ARM_COMPUTE_ERROR_ON(_parent == nullptr); |
| 126 | return _parent->strides_in_bytes(); |
| 127 | } |
| 128 | size_t offset_first_element_in_bytes() const override |
| 129 | { |
| 130 | ARM_COMPUTE_ERROR_ON(_parent == nullptr); |
| 131 | return _parent->offset_element_in_bytes(_coords); |
| 132 | } |
| 133 | size_t offset_element_in_bytes(const Coordinates &pos) const override; |
| 134 | int fixed_point_position() const override |
| 135 | { |
| 136 | ARM_COMPUTE_ERROR_ON(_parent == nullptr); |
| 137 | return _parent->fixed_point_position(); |
| 138 | } |
| 139 | size_t element_size() const override |
| 140 | { |
| 141 | ARM_COMPUTE_ERROR_ON(_parent == nullptr); |
| 142 | return _parent->element_size(); |
| 143 | } |
| 144 | size_t num_dimensions() const override |
| 145 | { |
| 146 | return _tensor_shape.num_dimensions(); |
| 147 | } |
| 148 | size_t num_channels() const override |
| 149 | { |
| 150 | ARM_COMPUTE_ERROR_ON(_parent == nullptr); |
| 151 | return _parent->num_channels(); |
| 152 | } |
| 153 | const TensorShape &tensor_shape() const override |
| 154 | { |
| 155 | ARM_COMPUTE_ERROR_ON(_parent == nullptr); |
| 156 | return _tensor_shape; |
| 157 | } |
| 158 | DataType data_type() const override |
| 159 | { |
| 160 | ARM_COMPUTE_ERROR_ON(_parent == nullptr); |
| 161 | return _parent->data_type(); |
| 162 | } |
| 163 | Format format() const override |
| 164 | { |
| 165 | ARM_COMPUTE_ERROR_ON(_parent == nullptr); |
| 166 | return _parent->format(); |
| 167 | } |
| 168 | size_t total_size() const override |
| 169 | { |
| 170 | ARM_COMPUTE_ERROR_ON(_parent == nullptr); |
| 171 | return _parent->total_size(); |
| 172 | } |
| 173 | PaddingSize padding() const override |
| 174 | { |
| 175 | ARM_COMPUTE_ERROR_ON(_parent == nullptr); |
| 176 | return _parent->padding(); |
| 177 | } |
| 178 | bool has_padding() const override |
| 179 | { |
| 180 | ARM_COMPUTE_ERROR_ON(_parent == nullptr); |
| 181 | return _parent->has_padding(); |
| 182 | } |
| 183 | bool is_resizable() const override |
| 184 | { |
| 185 | ARM_COMPUTE_ERROR_ON(_parent == nullptr); |
| 186 | return _parent->is_resizable(); |
| 187 | } |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 188 | ITensorInfo &set_is_resizable(bool is_resizable) override |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 189 | { |
| 190 | ARM_COMPUTE_ERROR_ON(_parent == nullptr); |
| 191 | _parent->set_is_resizable(is_resizable); |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 192 | return *this; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 193 | } |
| 194 | ValidRegion valid_region() const override |
| 195 | { |
| 196 | return _valid_region; |
| 197 | } |
| 198 | void set_valid_region(ValidRegion valid_region) override |
| 199 | { |
| 200 | ARM_COMPUTE_ERROR_ON(_parent == nullptr); |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 201 | // Check if subtensor is valid if parent is configured |
| 202 | if(_parent->tensor_shape().total_size() != 0) |
| 203 | { |
| 204 | ARM_COMPUTE_ERROR_ON_INVALID_SUBTENSOR_VALID_REGION(_parent->valid_region(), valid_region); |
| 205 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 206 | _valid_region = std::move(valid_region); |
| 207 | } |
Michel Iwaniec | 0063380 | 2017-10-12 14:14:15 +0100 | [diff] [blame] | 208 | QuantizationInfo quantization_info() const override |
| 209 | { |
| 210 | ARM_COMPUTE_ERROR_ON(_parent == nullptr); |
| 211 | return _parent->quantization_info(); |
| 212 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 213 | |
| 214 | private: |
| 215 | ITensorInfo *_parent; |
| 216 | TensorShape _tensor_shape; |
| 217 | Coordinates _coords; |
| 218 | ValidRegion _valid_region; |
| 219 | }; |
| 220 | } |
| 221 | #endif /*__ARM_COMPUTE_SUBTENSORINFO_H__ */ |