blob: 237f1333f2506f437ba17f693ba128a24c10a661 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Georgios Pinitas652bde52018-01-10 15:33:28 +00002 * Copyright (c) 2017-2018 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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 Pinitas283c1792017-11-10 18:14:06 +000029#include "support/ToolchainSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030
31using namespace arm_compute;
32
Georgios Pinitas652bde52018-01-10 15:33:28 +000033namespace
34{
35/** Extends parent shape depending on subtensor's coordinates and shape
36 *
37 * @param parent_shape Parent shape
38 * @param shape Subtensor shape
39 * @param coords Subtensor coordinates inside parent tensor
40 *
41 * @return Extended parent shape
42 */
43TensorShape extend_parent_shape(TensorShape parent_shape, TensorShape shape, Coordinates coords)
44{
45 // Subtensor should not index in x, y dimensions.
46 ARM_COMPUTE_ERROR_ON((coords.x() != 0) || (coords.y() != 0));
47
48 // Cannot extend on x, y ?
49 ARM_COMPUTE_ERROR_ON((parent_shape.total_size() != 0) && (parent_shape.x() != shape.x()) && (parent_shape.y() != shape.y()));
50
51 // Extend shape
52 for(unsigned int i = 0; i < TensorShape::num_max_dimensions; ++i)
53 {
54 int dimension_extend = coords[i] + static_cast<int>(shape[i]);
55 if((dimension_extend > static_cast<int>(parent_shape[i])) && (dimension_extend > 0))
56 {
57 parent_shape.set(i, static_cast<size_t>(dimension_extend));
58 }
59 }
60
61 return parent_shape;
62}
63} // namespace
64
Anthony Barbier6ff3b192017-09-04 18:44:23 +010065SubTensorInfo::SubTensorInfo()
Georgios Pinitas652bde52018-01-10 15:33:28 +000066 : _parent(nullptr), _tensor_shape(), _coords(), _valid_region{ Coordinates(), _tensor_shape }, _extend_parent(false)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010067{
68}
69
Georgios Pinitas652bde52018-01-10 15:33:28 +000070SubTensorInfo::SubTensorInfo(ITensorInfo *parent, TensorShape tensor_shape, Coordinates coords, bool extend_parent)
71 : _parent(parent), _tensor_shape(tensor_shape), _coords(coords), _valid_region{ Coordinates(), _tensor_shape }, _extend_parent(extend_parent)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010072{
73 ARM_COMPUTE_ERROR_ON(parent == nullptr);
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010074 // Check if subtensor is valid if parent is configured
Georgios Pinitas652bde52018-01-10 15:33:28 +000075 if(parent->tensor_shape().total_size() != 0 && !_extend_parent)
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010076 {
77 ARM_COMPUTE_ERROR_ON_INVALID_SUBTENSOR(parent->tensor_shape(), coords, tensor_shape);
78 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010079
80 // Initialize valid region
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +000081 _valid_region = ValidRegion{ Coordinates(), _tensor_shape };
Anthony Barbier6ff3b192017-09-04 18:44:23 +010082}
83
Georgios Pinitas283c1792017-11-10 18:14:06 +000084std::unique_ptr<ITensorInfo> SubTensorInfo::clone() const
85{
Georgios Pinitas9d364842017-12-12 12:20:55 +000086 // Clone creates a TensorInfo object from SubTensorInfo's parent which will conclude to a TensorInfo
87 // For now it does not make sense to copy a SubTensorInfo explicitly
88 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
89 auto clone_obj = _parent->clone();
90 clone_obj->set_tensor_shape(_tensor_shape);
91 clone_obj->set_valid_region(_valid_region);
92 return clone_obj;
Georgios Pinitas283c1792017-11-10 18:14:06 +000093}
94
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +000095ITensorInfo &SubTensorInfo::set_tensor_shape(const TensorShape &shape)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010096{
97 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
Georgios Pinitas652bde52018-01-10 15:33:28 +000098
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010099 // Check if subtensor is valid if parent is configured
Georgios Pinitas652bde52018-01-10 15:33:28 +0000100 if(_parent->tensor_shape().total_size() != 0 && !_extend_parent)
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100101 {
102 ARM_COMPUTE_ERROR_ON_INVALID_SUBTENSOR(_parent->tensor_shape(), _coords, shape);
Georgios Pinitas6528aa22018-03-15 18:02:21 +0000103 _valid_region = ValidRegion{ _coords, shape };
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100104 }
Georgios Pinitas652bde52018-01-10 15:33:28 +0000105 else if(_extend_parent) // Extend parent shape, configure if specified
106 {
107 ARM_COMPUTE_ERROR_ON((_parent->data_type() == DataType::UNKNOWN) && (_parent->format() == Format::UNKNOWN));
108 TensorShape parent_extended_shape = extend_parent_shape(_parent->tensor_shape(), shape, _coords);
109 _parent->set_tensor_shape(parent_extended_shape);
110 _parent->set_valid_region(ValidRegion{ Coordinates(), parent_extended_shape });
111 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100112 _tensor_shape = shape;
Georgios Pinitas283c1792017-11-10 18:14:06 +0000113 return *this;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100114}
115
116bool SubTensorInfo::extend_padding(const PaddingSize &padding)
117{
118 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
119 ARM_COMPUTE_ERROR_ON(!_parent->is_resizable());
Georgios Pinitas652bde52018-01-10 15:33:28 +0000120 ARM_COMPUTE_ERROR_ON(_parent->total_size() == 0);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100121
122 // Extend parent padding if required
123 return _parent->extend_padding(padding);
124}
125
126size_t SubTensorInfo::offset_element_in_bytes(const Coordinates &pos) const
127{
128 ARM_COMPUTE_ERROR_ON_COORDINATES_DIMENSIONS_GTE(pos, _tensor_shape.num_dimensions());
129
130 size_t offset = offset_first_element_in_bytes();
131 const Strides &strides = strides_in_bytes();
132
133 for(size_t i = 0; i < _tensor_shape.num_dimensions(); ++i)
134 {
135 offset += pos[i] * strides[i];
136 }
137
138 return offset;
139}