blob: 2e640dd93c8aa4856bf6368d4355324498c3acaa [file] [log] [blame]
Kaizenbf8b01d2017-10-12 14:26:51 +01001/*
Anthony Barbierf45d5a92018-01-24 16:23:15 +00002 * Copyright (c) 2017-2018 ARM Limited.
Kaizenbf8b01d2017-10-12 14:26:51 +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/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"
Anthony Barbier8140e1e2017-12-14 23:48:46 +000030#include "arm_compute/runtime/CL/CLTensor.h"
Kaizenbf8b01d2017-10-12 14:26:51 +010031#include "arm_compute/runtime/SubTensor.h"
Anthony Barbier8140e1e2017-12-14 23:48:46 +000032#include "arm_compute/runtime/Tensor.h"
Kaizenbf8b01d2017-10-12 14:26:51 +010033#include "utils/TypePrinter.h"
34
35using namespace arm_compute::graph;
36
37namespace
38{
39template <typename SubTensorType, typename ParentTensorType>
Anthony Barbierf45d5a92018-01-24 16:23:15 +000040std::unique_ptr<arm_compute::ITensor> initialise_subtensor(arm_compute::ITensor *parent, TensorShape shape, Coordinates coords, bool extend_parent)
Kaizenbf8b01d2017-10-12 14:26:51 +010041{
42 auto ptensor = dynamic_cast<ParentTensorType *>(parent);
Anthony Barbierf45d5a92018-01-24 16:23:15 +000043 auto subtensor = arm_compute::support::cpp14::make_unique<SubTensorType>(ptensor, shape, coords, extend_parent);
Kaizenbf8b01d2017-10-12 14:26:51 +010044 return std::move(subtensor);
45}
46} // namespace
47
48SubTensor::SubTensor()
Anthony Barbierf45d5a92018-01-24 16:23:15 +000049 : _target(TargetHint::DONT_CARE), _tensor_shape(), _coords(), _parent(nullptr), _subtensor(nullptr), _extend_parent(false)
Kaizenbf8b01d2017-10-12 14:26:51 +010050{
51}
52
Anthony Barbierf45d5a92018-01-24 16:23:15 +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)
Kaizenbf8b01d2017-10-12 14:26:51 +010055{
56 ARM_COMPUTE_ERROR_ON(parent.tensor() == nullptr);
57 _parent = parent.tensor();
Kaizenbf8b01d2017-10-12 14:26:51 +010058 _target = parent.target();
59
60 instantiate_subtensor();
61}
62
Anthony Barbierf45d5a92018-01-24 16:23:15 +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)
Kaizenbf8b01d2017-10-12 14:26:51 +010065{
66 ARM_COMPUTE_ERROR_ON(parent == nullptr);
Kaizenbf8b01d2017-10-12 14:26:51 +010067 instantiate_subtensor();
68}
69
Anthony Barbier8140e1e2017-12-14 23:48:46 +000070bool SubTensor::call_accessor()
Kaizenbf8b01d2017-10-12 14:26:51 +010071{
Anthony Barbier8140e1e2017-12-14 23:48:46 +000072 return true;
Kaizenbf8b01d2017-10-12 14:26:51 +010073}
74
Anthony Barbier8140e1e2017-12-14 23:48:46 +000075bool SubTensor::has_accessor() const
Kaizenbf8b01d2017-10-12 14:26:51 +010076{
Anthony Barbier8140e1e2017-12-14 23:48:46 +000077 return false;
Kaizenbf8b01d2017-10-12 14:26:51 +010078}
79
Anthony Barbier8140e1e2017-12-14 23:48:46 +000080arm_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()
87{
88 return _subtensor.get();
89}
90
91const arm_compute::ITensor *SubTensor::tensor() const
Kaizenbf8b01d2017-10-12 14:26:51 +010092{
93 return _subtensor.get();
94}
95
96TargetHint SubTensor::target() const
97{
98 return _target;
99}
100
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000101void SubTensor::allocate()
102{
103 // NOP for sub-tensors
104}
105
Kaizenbf8b01d2017-10-12 14:26:51 +0100106void SubTensor::instantiate_subtensor()
107{
108 switch(_target)
109 {
110 case TargetHint::OPENCL:
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000111 _subtensor = initialise_subtensor<arm_compute::CLSubTensor, arm_compute::ICLTensor>(_parent, _tensor_shape, _coords, _extend_parent);
Kaizenbf8b01d2017-10-12 14:26:51 +0100112 break;
113 case TargetHint::NEON:
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000114 _subtensor = initialise_subtensor<arm_compute::SubTensor, arm_compute::ITensor>(_parent, _tensor_shape, _coords, _extend_parent);
Kaizenbf8b01d2017-10-12 14:26:51 +0100115 break;
116 default:
117 ARM_COMPUTE_ERROR("Invalid TargetHint");
118 }
119}