Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 1 | /* |
| 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 | */ |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 24 | #include "arm_compute/graph/Tensor.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/CLTensor.h" |
| 30 | #include "arm_compute/runtime/Tensor.h" |
| 31 | #include "utils/TypePrinter.h" |
| 32 | |
| 33 | using namespace arm_compute::graph; |
| 34 | |
| 35 | namespace |
| 36 | { |
| 37 | template <typename TensorType> |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 38 | std::unique_ptr<arm_compute::ITensor> initialise_tensor(TensorInfo &info) |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 39 | { |
| 40 | auto tensor = arm_compute::support::cpp14::make_unique<TensorType>(); |
| 41 | tensor->allocator()->init(info); |
| 42 | return std::move(tensor); |
| 43 | } |
| 44 | |
| 45 | template <typename TensorType> |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 46 | void tensor_allocate(arm_compute::ITensor &tensor) |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 47 | { |
| 48 | auto itensor = dynamic_cast<TensorType *>(&tensor); |
| 49 | ARM_COMPUTE_ERROR_ON_NULLPTR(itensor); |
| 50 | itensor->allocator()->allocate(); |
| 51 | } |
| 52 | } // namespace |
| 53 | |
| 54 | Tensor::Tensor(TensorInfo &&info) |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 55 | : _target(TargetHint::DONT_CARE), _info(info), _accessor(nullptr), _tensor(nullptr) |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 56 | { |
| 57 | } |
| 58 | |
| 59 | Tensor::Tensor(Tensor &&src) noexcept |
| 60 | : _target(src._target), |
| 61 | _info(std::move(src._info)), |
| 62 | _accessor(std::move(src._accessor)), |
| 63 | _tensor(std::move(src._tensor)) |
| 64 | { |
| 65 | } |
| 66 | |
| 67 | void Tensor::set_info(TensorInfo &&info) |
| 68 | { |
| 69 | _info = info; |
| 70 | } |
| 71 | |
| 72 | bool Tensor::call_accessor() |
| 73 | { |
| 74 | ARM_COMPUTE_ERROR_ON_NULLPTR(_accessor.get()); |
| 75 | auto cl_tensor = dynamic_cast<arm_compute::CLTensor *>(_tensor.get()); |
| 76 | if(cl_tensor != nullptr && cl_tensor->buffer() == nullptr) |
| 77 | { |
| 78 | cl_tensor->map(); |
| 79 | } |
| 80 | bool retval = _accessor->access_tensor(*_tensor); |
| 81 | if(cl_tensor != nullptr) |
| 82 | { |
| 83 | cl_tensor->unmap(); |
| 84 | } |
| 85 | return retval; |
| 86 | } |
| 87 | |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 88 | bool Tensor::has_accessor() const |
| 89 | { |
| 90 | return (_accessor != nullptr); |
| 91 | } |
| 92 | |
| 93 | arm_compute::ITensor *Tensor::tensor() |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 94 | { |
| 95 | return _tensor.get(); |
| 96 | } |
| 97 | |
Michalis Spyrou | ed194b1 | 2017-10-31 15:04:34 +0000 | [diff] [blame^] | 98 | const arm_compute::ITensor *Tensor::tensor() const |
| 99 | { |
| 100 | return _tensor.get(); |
| 101 | } |
| 102 | |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 103 | const TensorInfo &Tensor::info() const |
| 104 | { |
| 105 | return _info; |
| 106 | } |
| 107 | |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 108 | arm_compute::ITensor *Tensor::set_target(TargetHint target) |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 109 | { |
| 110 | if(_tensor != nullptr) |
| 111 | { |
| 112 | ARM_COMPUTE_ERROR_ON(target != _target); |
| 113 | } |
| 114 | else |
| 115 | { |
| 116 | switch(target) |
| 117 | { |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 118 | case TargetHint::OPENCL: |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 119 | _tensor = initialise_tensor<arm_compute::CLTensor>(_info); |
| 120 | break; |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 121 | case TargetHint::NEON: |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 122 | _tensor = initialise_tensor<arm_compute::Tensor>(_info); |
| 123 | break; |
| 124 | default: |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 125 | ARM_COMPUTE_ERROR("Invalid TargetHint"); |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 126 | } |
| 127 | _target = target; |
| 128 | } |
| 129 | return _tensor.get(); |
| 130 | } |
| 131 | |
| 132 | void Tensor::allocate() |
| 133 | { |
| 134 | ARM_COMPUTE_ERROR_ON_NULLPTR(_tensor.get()); |
| 135 | switch(_target) |
| 136 | { |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 137 | case TargetHint::OPENCL: |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 138 | tensor_allocate<arm_compute::CLTensor>(*_tensor); |
| 139 | break; |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 140 | case TargetHint::NEON: |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 141 | tensor_allocate<arm_compute::Tensor>(*_tensor); |
| 142 | break; |
| 143 | default: |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 144 | ARM_COMPUTE_ERROR("Invalid TargetHint"); |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
| 148 | void Tensor::allocate_and_fill_if_needed() |
| 149 | { |
| 150 | allocate(); |
| 151 | if(_accessor != nullptr) |
| 152 | { |
| 153 | call_accessor(); |
| 154 | } |
| 155 | } |
| 156 | |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 157 | TargetHint Tensor::target() const |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 158 | { |
| 159 | return _target; |
| 160 | } |