2 * Copyright (c) 2017-2018 ARM Limited.
4 * SPDX-License-Identifier: MIT
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:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
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
24 #include "arm_compute/graph/SubTensor.h"
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"
30 #include "arm_compute/runtime/CL/CLTensor.h"
31 #include "arm_compute/runtime/SubTensor.h"
32 #include "arm_compute/runtime/Tensor.h"
33 #include "utils/TypePrinter.h"
35 using namespace arm_compute::graph;
39 template <typename SubTensorType, typename ParentTensorType>
40 std::unique_ptr<arm_compute::ITensor> initialise_subtensor(arm_compute::ITensor *parent, TensorShape shape, Coordinates coords, bool extend_parent)
42 auto ptensor = dynamic_cast<ParentTensorType *>(parent);
43 auto subtensor = arm_compute::support::cpp14::make_unique<SubTensorType>(ptensor, shape, coords, extend_parent);
44 return std::move(subtensor);
48 SubTensor::SubTensor()
49 : _target(TargetHint::DONT_CARE), _tensor_shape(), _coords(), _parent(nullptr), _subtensor(nullptr), _extend_parent(false)
53 SubTensor::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)
56 ARM_COMPUTE_ERROR_ON(parent.tensor() == nullptr);
57 _parent = parent.tensor();
58 _target = parent.target();
60 instantiate_subtensor();
63 SubTensor::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)
66 ARM_COMPUTE_ERROR_ON(parent == nullptr);
67 instantiate_subtensor();
70 bool SubTensor::call_accessor()
75 bool SubTensor::has_accessor() const
80 arm_compute::ITensor *SubTensor::set_target(TargetHint target)
82 ARM_COMPUTE_ERROR_ON(target != _target);
83 return (target == _target) ? _subtensor.get() : nullptr;
86 arm_compute::ITensor *SubTensor::tensor()
88 return _subtensor.get();
91 const arm_compute::ITensor *SubTensor::tensor() const
93 return _subtensor.get();
96 TargetHint SubTensor::target() const
101 void SubTensor::allocate()
103 // NOP for sub-tensors
106 void SubTensor::instantiate_subtensor()
110 case TargetHint::OPENCL:
111 _subtensor = initialise_subtensor<arm_compute::CLSubTensor, arm_compute::ICLTensor>(_parent, _tensor_shape, _coords, _extend_parent);
113 case TargetHint::NEON:
114 _subtensor = initialise_subtensor<arm_compute::SubTensor, arm_compute::ITensor>(_parent, _tensor_shape, _coords, _extend_parent);
117 ARM_COMPUTE_ERROR("Invalid TargetHint");