2 * Copyright (c) 2017 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
25 #include "arm_compute/graph/Tensor.h"
27 #include "arm_compute/core/Error.h"
28 #include "arm_compute/core/Helpers.h"
29 #include "arm_compute/core/Validate.h"
30 #include "arm_compute/runtime/CL/CLTensor.h"
31 #include "arm_compute/runtime/Tensor.h"
32 #include "utils/TypePrinter.h"
34 using namespace arm_compute::graph;
38 template <typename TensorType>
39 std::unique_ptr<ITensor> initialise_tensor(TensorInfo &info)
41 auto tensor = arm_compute::support::cpp14::make_unique<TensorType>();
42 tensor->allocator()->init(info);
43 return std::move(tensor);
46 template <typename TensorType>
47 void tensor_allocate(ITensor &tensor)
49 auto itensor = dynamic_cast<TensorType *>(&tensor);
50 ARM_COMPUTE_ERROR_ON_NULLPTR(itensor);
51 itensor->allocator()->allocate();
55 Tensor::Tensor(TensorInfo &&info)
56 : _target(Hint::DONT_CARE), _info(info), _accessor(nullptr), _tensor(nullptr)
60 Tensor::Tensor(Tensor &&src) noexcept
61 : _target(src._target),
62 _info(std::move(src._info)),
63 _accessor(std::move(src._accessor)),
64 _tensor(std::move(src._tensor))
68 void Tensor::set_info(TensorInfo &&info)
73 bool Tensor::call_accessor()
75 ARM_COMPUTE_ERROR_ON_NULLPTR(_accessor.get());
76 auto cl_tensor = dynamic_cast<arm_compute::CLTensor *>(_tensor.get());
77 if(cl_tensor != nullptr && cl_tensor->buffer() == nullptr)
81 bool retval = _accessor->access_tensor(*_tensor);
82 if(cl_tensor != nullptr)
89 ITensor *Tensor::tensor()
94 const TensorInfo &Tensor::info() const
99 ITensor *Tensor::set_target(Hint target)
101 if(_tensor != nullptr)
103 ARM_COMPUTE_ERROR_ON(target != _target);
110 _tensor = initialise_tensor<arm_compute::CLTensor>(_info);
113 _tensor = initialise_tensor<arm_compute::Tensor>(_info);
116 ARM_COMPUTE_ERROR("Invalid Hint");
120 return _tensor.get();
123 void Tensor::allocate()
125 ARM_COMPUTE_ERROR_ON_NULLPTR(_tensor.get());
129 tensor_allocate<arm_compute::CLTensor>(*_tensor);
132 tensor_allocate<arm_compute::Tensor>(*_tensor);
135 ARM_COMPUTE_ERROR("Invalid Hint");
139 void Tensor::allocate_and_fill_if_needed()
142 if(_accessor != nullptr)
148 Hint Tensor::target() const