blob: 3d4723430f832998fd10a95f7afff1c0485e57f9 [file] [log] [blame]
Anthony Barbier2a07e182017-08-04 18:20:27 +01001/*
Giorgio Arena9c67d382021-08-20 15:24:03 +01002 * Copyright (c) 2018-2019,2021 Arm Limited.
Anthony Barbier2a07e182017-08-04 18:20:27 +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 */
Anthony Barbier2a07e182017-08-04 18:20:27 +010024#include "arm_compute/graph/Tensor.h"
25
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010026namespace arm_compute
Anthony Barbier2a07e182017-08-04 18:20:27 +010027{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010028namespace graph
Anthony Barbier2a07e182017-08-04 18:20:27 +010029{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010030Tensor::Tensor(TensorID id, TensorDescriptor desc)
Anthony Barbieraa3240d2018-05-10 16:39:25 +010031 : _id(id), _desc(std::move(desc)), _handle(nullptr), _accessor(nullptr), _bound_edges()
Anthony Barbier2a07e182017-08-04 18:20:27 +010032{
33}
34
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010035TensorID Tensor::id() const
Anthony Barbier2a07e182017-08-04 18:20:27 +010036{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010037 return _id;
Anthony Barbier2a07e182017-08-04 18:20:27 +010038}
39
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010040TensorDescriptor &Tensor::desc()
Anthony Barbier2a07e182017-08-04 18:20:27 +010041{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010042 return _desc;
43}
44
45const TensorDescriptor &Tensor::desc() const
46{
47 return _desc;
48}
49
50void Tensor::set_handle(std::unique_ptr<ITensorHandle> backend_tensor)
51{
52 _handle = std::move(backend_tensor);
53}
54
55ITensorHandle *Tensor::handle()
56{
57 return _handle.get();
58}
59
60void Tensor::set_accessor(std::unique_ptr<ITensorAccessor> accessor)
61{
62 _accessor = std::move(accessor);
63}
64
65ITensorAccessor *Tensor::accessor()
66{
67 return _accessor.get();
Anthony Barbier2a07e182017-08-04 18:20:27 +010068}
69
Georgios Pinitasd3a78ab2018-06-18 15:35:09 +010070std::unique_ptr<ITensorAccessor> Tensor::extract_accessor()
71{
72 return std::move(_accessor);
73}
74
Anthony Barbier2a07e182017-08-04 18:20:27 +010075bool Tensor::call_accessor()
76{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010077 // Early exit guard
78 if(!_accessor || !_handle)
Anthony Barbier2a07e182017-08-04 18:20:27 +010079 {
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010080 return false;
Anthony Barbier2a07e182017-08-04 18:20:27 +010081 }
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010082
Giorgio Arena9c67d382021-08-20 15:24:03 +010083 const bool access_data = _accessor->access_tensor_data();
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010084
Giorgio Arena9c67d382021-08-20 15:24:03 +010085 if(access_data)
Anthony Barbier2a07e182017-08-04 18:20:27 +010086 {
Giorgio Arena9c67d382021-08-20 15:24:03 +010087 // Map tensor
88 _handle->map(true);
89
90 // Return in case of null backend buffer
91 if(_handle->tensor().buffer() == nullptr)
92 {
93 return false;
94 }
Anthony Barbier2a07e182017-08-04 18:20:27 +010095 }
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010096
97 // Call accessor
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010098 bool retval = _accessor->access_tensor(_handle->tensor());
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010099
Giorgio Arena9c67d382021-08-20 15:24:03 +0100100 if(access_data)
101 {
102 // Unmap tensor
103 _handle->unmap();
104 }
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100105
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100106 return retval;
Anthony Barbier2a07e182017-08-04 18:20:27 +0100107}
108
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100109void Tensor::bind_edge(EdgeID eid)
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100110{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100111 _bound_edges.insert(eid);
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100112}
113
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100114void Tensor::unbind_edge(EdgeID eid)
Anthony Barbier2a07e182017-08-04 18:20:27 +0100115{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100116 _bound_edges.erase(eid);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100117}
118
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100119std::set<EdgeID> Tensor::bound_edges() const
Michalis Spyroued194b12017-10-31 15:04:34 +0000120{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100121 return _bound_edges;
Michalis Spyroued194b12017-10-31 15:04:34 +0000122}
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100123} // namespace graph
Anthony Barbieraa3240d2018-05-10 16:39:25 +0100124} // namespace arm_compute