blob: 2e640dd93c8aa4856bf6368d4355324498c3acaa [file] [log] [blame]
Georgios Pinitas6f669f02017-09-26 12:32:57 +01001/*
Georgios Pinitas652bde52018-01-10 15:33:28 +00002 * Copyright (c) 2017-2018 ARM Limited.
Georgios Pinitas6f669f02017-09-26 12:32:57 +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 */
Georgios Pinitas6f669f02017-09-26 12:32:57 +010024#include "arm_compute/graph/SubTensor.h"
25
26#include "arm_compute/core/Error.h"
27#include "arm_compute/core/Helpers.h"
28#include "arm_compute/core/Validate.h"
29#include "arm_compute/runtime/CL/CLSubTensor.h"
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010030#include "arm_compute/runtime/CL/CLTensor.h"
Georgios Pinitas6f669f02017-09-26 12:32:57 +010031#include "arm_compute/runtime/SubTensor.h"
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010032#include "arm_compute/runtime/Tensor.h"
Georgios Pinitas6f669f02017-09-26 12:32:57 +010033#include "utils/TypePrinter.h"
34
35using namespace arm_compute::graph;
36
37namespace
38{
39template <typename SubTensorType, typename ParentTensorType>
Georgios Pinitas652bde52018-01-10 15:33:28 +000040std::unique_ptr<arm_compute::ITensor> initialise_subtensor(arm_compute::ITensor *parent, TensorShape shape, Coordinates coords, bool extend_parent)
Georgios Pinitas6f669f02017-09-26 12:32:57 +010041{
42 auto ptensor = dynamic_cast<ParentTensorType *>(parent);
Georgios Pinitas652bde52018-01-10 15:33:28 +000043 auto subtensor = arm_compute::support::cpp14::make_unique<SubTensorType>(ptensor, shape, coords, extend_parent);
Georgios Pinitas6f669f02017-09-26 12:32:57 +010044 return std::move(subtensor);
45}
46} // namespace
47
48SubTensor::SubTensor()
Georgios Pinitas652bde52018-01-10 15:33:28 +000049 : _target(TargetHint::DONT_CARE), _tensor_shape(), _coords(), _parent(nullptr), _subtensor(nullptr), _extend_parent(false)
Georgios Pinitas6f669f02017-09-26 12:32:57 +010050{
51}
52
Georgios Pinitas652bde52018-01-10 15:33:28 +000053SubTensor::SubTensor(Tensor &parent, TensorShape tensor_shape, Coordinates coords, bool extend_parent)
54 : _target(TargetHint::DONT_CARE), _tensor_shape(tensor_shape), _coords(coords), _parent(nullptr), _subtensor(nullptr), _extend_parent(extend_parent)
Georgios Pinitas6f669f02017-09-26 12:32:57 +010055{
56 ARM_COMPUTE_ERROR_ON(parent.tensor() == nullptr);
57 _parent = parent.tensor();
Georgios Pinitas6f669f02017-09-26 12:32:57 +010058 _target = parent.target();
59
60 instantiate_subtensor();
61}
62
Georgios Pinitas652bde52018-01-10 15:33:28 +000063SubTensor::SubTensor(arm_compute::ITensor *parent, TensorShape tensor_shape, Coordinates coords, TargetHint target, bool extend_parent)
64 : _target(target), _tensor_shape(tensor_shape), _coords(coords), _parent(parent), _subtensor(nullptr), _extend_parent(extend_parent)
Georgios Pinitas6f669f02017-09-26 12:32:57 +010065{
66 ARM_COMPUTE_ERROR_ON(parent == nullptr);
Georgios Pinitas6f669f02017-09-26 12:32:57 +010067 instantiate_subtensor();
68}
69
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010070bool SubTensor::call_accessor()
Georgios Pinitas6f669f02017-09-26 12:32:57 +010071{
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010072 return true;
Georgios Pinitas6f669f02017-09-26 12:32:57 +010073}
74
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010075bool SubTensor::has_accessor() const
Georgios Pinitas6f669f02017-09-26 12:32:57 +010076{
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010077 return false;
Georgios Pinitas6f669f02017-09-26 12:32:57 +010078}
79
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010080arm_compute::ITensor *SubTensor::set_target(TargetHint target)
81{
82 ARM_COMPUTE_ERROR_ON(target != _target);
83 return (target == _target) ? _subtensor.get() : nullptr;
84}
85
86arm_compute::ITensor *SubTensor::tensor()
Georgios Pinitas6f669f02017-09-26 12:32:57 +010087{
88 return _subtensor.get();
89}
90
Michalis Spyroued194b12017-10-31 15:04:34 +000091const arm_compute::ITensor *SubTensor::tensor() const
92{
93 return _subtensor.get();
94}
95
Georgios Pinitasff421f22017-10-04 16:53:58 +010096TargetHint SubTensor::target() const
Georgios Pinitas6f669f02017-09-26 12:32:57 +010097{
98 return _target;
99}
100
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100101void SubTensor::allocate()
102{
103 // NOP for sub-tensors
104}
105
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100106void SubTensor::instantiate_subtensor()
107{
108 switch(_target)
109 {
Georgios Pinitasff421f22017-10-04 16:53:58 +0100110 case TargetHint::OPENCL:
Georgios Pinitas652bde52018-01-10 15:33:28 +0000111 _subtensor = initialise_subtensor<arm_compute::CLSubTensor, arm_compute::ICLTensor>(_parent, _tensor_shape, _coords, _extend_parent);
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100112 break;
Georgios Pinitasff421f22017-10-04 16:53:58 +0100113 case TargetHint::NEON:
Georgios Pinitas652bde52018-01-10 15:33:28 +0000114 _subtensor = initialise_subtensor<arm_compute::SubTensor, arm_compute::ITensor>(_parent, _tensor_shape, _coords, _extend_parent);
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100115 break;
116 default:
Georgios Pinitasff421f22017-10-04 16:53:58 +0100117 ARM_COMPUTE_ERROR("Invalid TargetHint");
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100118 }
119}