Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2020 Arm Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 | #include "arm_compute/core/SubTensorInfo.h" |
| 25 | |
| 26 | #include "arm_compute/core/Error.h" |
| 27 | #include "arm_compute/core/Helpers.h" |
| 28 | #include "arm_compute/core/Validate.h" |
| 29 | |
| 30 | using namespace arm_compute; |
| 31 | |
Georgios Pinitas | 652bde5 | 2018-01-10 15:33:28 +0000 | [diff] [blame] | 32 | namespace |
| 33 | { |
| 34 | /** Extends parent shape depending on subtensor's coordinates and shape |
| 35 | * |
| 36 | * @param parent_shape Parent shape |
| 37 | * @param shape Subtensor shape |
| 38 | * @param coords Subtensor coordinates inside parent tensor |
| 39 | * |
| 40 | * @return Extended parent shape |
| 41 | */ |
| 42 | TensorShape extend_parent_shape(TensorShape parent_shape, TensorShape shape, Coordinates coords) |
| 43 | { |
Georgios Pinitas | 652bde5 | 2018-01-10 15:33:28 +0000 | [diff] [blame] | 44 | // Extend shape |
| 45 | for(unsigned int i = 0; i < TensorShape::num_max_dimensions; ++i) |
| 46 | { |
| 47 | int dimension_extend = coords[i] + static_cast<int>(shape[i]); |
| 48 | if((dimension_extend > static_cast<int>(parent_shape[i])) && (dimension_extend > 0)) |
| 49 | { |
| 50 | parent_shape.set(i, static_cast<size_t>(dimension_extend)); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | return parent_shape; |
| 55 | } |
| 56 | } // namespace |
| 57 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 58 | SubTensorInfo::SubTensorInfo() |
Georgios Pinitas | 652bde5 | 2018-01-10 15:33:28 +0000 | [diff] [blame] | 59 | : _parent(nullptr), _tensor_shape(), _coords(), _valid_region{ Coordinates(), _tensor_shape }, _extend_parent(false) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 60 | { |
| 61 | } |
| 62 | |
Georgios Pinitas | 652bde5 | 2018-01-10 15:33:28 +0000 | [diff] [blame] | 63 | SubTensorInfo::SubTensorInfo(ITensorInfo *parent, TensorShape tensor_shape, Coordinates coords, bool extend_parent) |
| 64 | : _parent(parent), _tensor_shape(tensor_shape), _coords(coords), _valid_region{ Coordinates(), _tensor_shape }, _extend_parent(extend_parent) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 65 | { |
| 66 | ARM_COMPUTE_ERROR_ON(parent == nullptr); |
Georgios Pinitas | 0b5af9f | 2020-06-19 23:22:08 +0100 | [diff] [blame] | 67 | |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 68 | // Check if subtensor is valid if parent is configured |
Georgios Pinitas | 652bde5 | 2018-01-10 15:33:28 +0000 | [diff] [blame] | 69 | if(parent->tensor_shape().total_size() != 0 && !_extend_parent) |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 70 | { |
| 71 | ARM_COMPUTE_ERROR_ON_INVALID_SUBTENSOR(parent->tensor_shape(), coords, tensor_shape); |
| 72 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 73 | |
| 74 | // Initialize valid region |
Diego Lopez Recas | bcbc970 | 2017-12-18 11:28:27 +0000 | [diff] [blame] | 75 | _valid_region = ValidRegion{ Coordinates(), _tensor_shape }; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 76 | } |
| 77 | |
Georgios Pinitas | 283c179 | 2017-11-10 18:14:06 +0000 | [diff] [blame] | 78 | std::unique_ptr<ITensorInfo> SubTensorInfo::clone() const |
| 79 | { |
Georgios Pinitas | 9d36484 | 2017-12-12 12:20:55 +0000 | [diff] [blame] | 80 | // Clone creates a TensorInfo object from SubTensorInfo's parent which will conclude to a TensorInfo |
| 81 | // For now it does not make sense to copy a SubTensorInfo explicitly |
| 82 | ARM_COMPUTE_ERROR_ON(_parent == nullptr); |
| 83 | auto clone_obj = _parent->clone(); |
| 84 | clone_obj->set_tensor_shape(_tensor_shape); |
| 85 | clone_obj->set_valid_region(_valid_region); |
| 86 | return clone_obj; |
Georgios Pinitas | 283c179 | 2017-11-10 18:14:06 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 89 | ITensorInfo &SubTensorInfo::set_tensor_shape(const TensorShape &shape) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 90 | { |
| 91 | ARM_COMPUTE_ERROR_ON(_parent == nullptr); |
Georgios Pinitas | 652bde5 | 2018-01-10 15:33:28 +0000 | [diff] [blame] | 92 | |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 93 | // Check if subtensor is valid if parent is configured |
Georgios Pinitas | 652bde5 | 2018-01-10 15:33:28 +0000 | [diff] [blame] | 94 | if(_parent->tensor_shape().total_size() != 0 && !_extend_parent) |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 95 | { |
| 96 | ARM_COMPUTE_ERROR_ON_INVALID_SUBTENSOR(_parent->tensor_shape(), _coords, shape); |
Georgios Pinitas | 6528aa2 | 2018-03-15 18:02:21 +0000 | [diff] [blame] | 97 | _valid_region = ValidRegion{ _coords, shape }; |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 98 | } |
Georgios Pinitas | 652bde5 | 2018-01-10 15:33:28 +0000 | [diff] [blame] | 99 | else if(_extend_parent) // Extend parent shape, configure if specified |
| 100 | { |
| 101 | ARM_COMPUTE_ERROR_ON((_parent->data_type() == DataType::UNKNOWN) && (_parent->format() == Format::UNKNOWN)); |
| 102 | TensorShape parent_extended_shape = extend_parent_shape(_parent->tensor_shape(), shape, _coords); |
| 103 | _parent->set_tensor_shape(parent_extended_shape); |
| 104 | _parent->set_valid_region(ValidRegion{ Coordinates(), parent_extended_shape }); |
| 105 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 106 | _tensor_shape = shape; |
Georgios Pinitas | 283c179 | 2017-11-10 18:14:06 +0000 | [diff] [blame] | 107 | return *this; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | bool SubTensorInfo::extend_padding(const PaddingSize &padding) |
| 111 | { |
| 112 | ARM_COMPUTE_ERROR_ON(_parent == nullptr); |
| 113 | ARM_COMPUTE_ERROR_ON(!_parent->is_resizable()); |
Georgios Pinitas | 652bde5 | 2018-01-10 15:33:28 +0000 | [diff] [blame] | 114 | ARM_COMPUTE_ERROR_ON(_parent->total_size() == 0); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 115 | |
Georgios Pinitas | 0b5af9f | 2020-06-19 23:22:08 +0100 | [diff] [blame] | 116 | // Check that you do not extend padding on sub-tensors unless XY shape matches parent tensor |
| 117 | // TODO(COMPMID-3558): Remove _extend_parent check |
| 118 | if(!_extend_parent && (padding.left || padding.right)) |
| 119 | { |
| 120 | ARM_COMPUTE_ERROR_ON(_parent->tensor_shape().x() != tensor_shape().x()); |
| 121 | } |
| 122 | if(!_extend_parent && (padding.top || padding.bottom)) |
| 123 | { |
| 124 | ARM_COMPUTE_ERROR_ON(_parent->tensor_shape().y() != tensor_shape().y()); |
| 125 | } |
| 126 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 127 | // Extend parent padding if required |
| 128 | return _parent->extend_padding(padding); |
| 129 | } |
| 130 | |
Michalis Spyrou | 7c60c99 | 2019-10-10 14:33:47 +0100 | [diff] [blame] | 131 | int32_t SubTensorInfo::offset_element_in_bytes(const Coordinates &pos) const |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 132 | { |
| 133 | ARM_COMPUTE_ERROR_ON_COORDINATES_DIMENSIONS_GTE(pos, _tensor_shape.num_dimensions()); |
| 134 | |
Matthew Bentham | 9204646 | 2020-03-07 22:15:55 +0000 | [diff] [blame] | 135 | int32_t offset = offset_first_element_in_bytes(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 136 | const Strides &strides = strides_in_bytes(); |
| 137 | |
| 138 | for(size_t i = 0; i < _tensor_shape.num_dimensions(); ++i) |
| 139 | { |
| 140 | offset += pos[i] * strides[i]; |
| 141 | } |
| 142 | |
| 143 | return offset; |
| 144 | } |