blob: 8acd71ceb0bce205779fc2c0fbac60895f5eb244 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
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 Pinitas283c1792017-11-10 18:14:06 +000029#include "support/ToolchainSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030
31using namespace arm_compute;
32
33SubTensorInfo::SubTensorInfo()
34 : _parent(nullptr), _tensor_shape(), _coords(), _valid_region{ Coordinates(), _tensor_shape }
35{
36}
37
Georgios Pinitas283c1792017-11-10 18:14:06 +000038SubTensorInfo::SubTensorInfo(ITensorInfo *parent, TensorShape tensor_shape, Coordinates coords)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010039 : _parent(parent), _tensor_shape(tensor_shape), _coords(coords), _valid_region{ Coordinates(), _tensor_shape }
40{
41 ARM_COMPUTE_ERROR_ON(parent == nullptr);
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010042 // 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 Barbier6ff3b192017-09-04 18:44:23 +010047
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 Pinitas283c1792017-11-10 18:14:06 +000054std::unique_ptr<ITensorInfo> SubTensorInfo::clone() const
55{
Georgios Pinitas9d364842017-12-12 12:20:55 +000056 // Clone creates a TensorInfo object from SubTensorInfo's parent which will conclude to a TensorInfo
57 // For now it does not make sense to copy a SubTensorInfo explicitly
58 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
59 auto clone_obj = _parent->clone();
60 clone_obj->set_tensor_shape(_tensor_shape);
61 clone_obj->set_valid_region(_valid_region);
62 return clone_obj;
Georgios Pinitas283c1792017-11-10 18:14:06 +000063}
64
65ITensorInfo &SubTensorInfo::set_tensor_shape(TensorShape shape)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010066{
67 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010068 // Check if subtensor is valid if parent is configured
69 if(_parent->tensor_shape().total_size() != 0)
70 {
71 ARM_COMPUTE_ERROR_ON_INVALID_SUBTENSOR(_parent->tensor_shape(), _coords, shape);
72 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010073 _tensor_shape = shape;
Georgios Pinitas283c1792017-11-10 18:14:06 +000074 return *this;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010075}
76
77bool SubTensorInfo::extend_padding(const PaddingSize &padding)
78{
79 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
80 ARM_COMPUTE_ERROR_ON(!_parent->is_resizable());
81
82 // Extend parent padding if required
83 return _parent->extend_padding(padding);
84}
85
86size_t SubTensorInfo::offset_element_in_bytes(const Coordinates &pos) const
87{
88 ARM_COMPUTE_ERROR_ON_COORDINATES_DIMENSIONS_GTE(pos, _tensor_shape.num_dimensions());
89
90 size_t offset = offset_first_element_in_bytes();
91 const Strides &strides = strides_in_bytes();
92
93 for(size_t i = 0; i < _tensor_shape.num_dimensions(); ++i)
94 {
95 offset += pos[i] * strides[i];
96 }
97
98 return offset;
99}