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 | #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" |
Georgios Pinitas | 283c179 | 2017-11-10 18:14:06 +0000 | [diff] [blame] | 29 | #include "support/ToolchainSupport.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 30 | |
| 31 | using namespace arm_compute; |
| 32 | |
| 33 | SubTensorInfo::SubTensorInfo() |
| 34 | : _parent(nullptr), _tensor_shape(), _coords(), _valid_region{ Coordinates(), _tensor_shape } |
| 35 | { |
| 36 | } |
| 37 | |
Georgios Pinitas | 283c179 | 2017-11-10 18:14:06 +0000 | [diff] [blame] | 38 | SubTensorInfo::SubTensorInfo(ITensorInfo *parent, TensorShape tensor_shape, Coordinates coords) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 39 | : _parent(parent), _tensor_shape(tensor_shape), _coords(coords), _valid_region{ Coordinates(), _tensor_shape } |
| 40 | { |
| 41 | ARM_COMPUTE_ERROR_ON(parent == nullptr); |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 42 | // Check if subtensor is valid if parent is configured |
| 43 | if(parent->tensor_shape().total_size() != 0) |
| 44 | { |
| 45 | ARM_COMPUTE_ERROR_ON_INVALID_SUBTENSOR(parent->tensor_shape(), coords, tensor_shape); |
| 46 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 47 | |
| 48 | // Initialize valid region |
| 49 | Coordinates coordinates; |
| 50 | coordinates.set_num_dimensions(_tensor_shape.num_dimensions()); |
| 51 | _valid_region = ValidRegion{ coordinates, _tensor_shape }; |
| 52 | } |
| 53 | |
Georgios Pinitas | 283c179 | 2017-11-10 18:14:06 +0000 | [diff] [blame] | 54 | std::unique_ptr<ITensorInfo> SubTensorInfo::clone() const |
| 55 | { |
| 56 | return support::cpp14::make_unique<SubTensorInfo>(*this); |
| 57 | } |
| 58 | |
| 59 | ITensorInfo &SubTensorInfo::set_tensor_shape(TensorShape shape) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 60 | { |
| 61 | ARM_COMPUTE_ERROR_ON(_parent == nullptr); |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 62 | // Check if subtensor is valid if parent is configured |
| 63 | if(_parent->tensor_shape().total_size() != 0) |
| 64 | { |
| 65 | ARM_COMPUTE_ERROR_ON_INVALID_SUBTENSOR(_parent->tensor_shape(), _coords, shape); |
| 66 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 67 | _tensor_shape = shape; |
Georgios Pinitas | 283c179 | 2017-11-10 18:14:06 +0000 | [diff] [blame] | 68 | return *this; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | bool SubTensorInfo::extend_padding(const PaddingSize &padding) |
| 72 | { |
| 73 | ARM_COMPUTE_ERROR_ON(_parent == nullptr); |
| 74 | ARM_COMPUTE_ERROR_ON(!_parent->is_resizable()); |
| 75 | |
| 76 | // Extend parent padding if required |
| 77 | return _parent->extend_padding(padding); |
| 78 | } |
| 79 | |
| 80 | size_t SubTensorInfo::offset_element_in_bytes(const Coordinates &pos) const |
| 81 | { |
| 82 | ARM_COMPUTE_ERROR_ON_COORDINATES_DIMENSIONS_GTE(pos, _tensor_shape.num_dimensions()); |
| 83 | |
| 84 | size_t offset = offset_first_element_in_bytes(); |
| 85 | const Strides &strides = strides_in_bytes(); |
| 86 | |
| 87 | for(size_t i = 0; i < _tensor_shape.num_dimensions(); ++i) |
| 88 | { |
| 89 | offset += pos[i] * strides[i]; |
| 90 | } |
| 91 | |
| 92 | return offset; |
| 93 | } |