• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2018-2019,2021 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  */
24 #include "arm_compute/graph/Tensor.h"
25 
26 namespace arm_compute
27 {
28 namespace graph
29 {
Tensor(TensorID id,TensorDescriptor desc)30 Tensor::Tensor(TensorID id, TensorDescriptor desc)
31     : _id(id), _desc(std::move(desc)), _handle(nullptr), _accessor(nullptr), _bound_edges()
32 {
33 }
34 
id() const35 TensorID Tensor::id() const
36 {
37     return _id;
38 }
39 
desc()40 TensorDescriptor &Tensor::desc()
41 {
42     return _desc;
43 }
44 
desc() const45 const TensorDescriptor &Tensor::desc() const
46 {
47     return _desc;
48 }
49 
set_handle(std::unique_ptr<ITensorHandle> backend_tensor)50 void Tensor::set_handle(std::unique_ptr<ITensorHandle> backend_tensor)
51 {
52     _handle = std::move(backend_tensor);
53 }
54 
handle()55 ITensorHandle *Tensor::handle()
56 {
57     return _handle.get();
58 }
59 
set_accessor(std::unique_ptr<ITensorAccessor> accessor)60 void Tensor::set_accessor(std::unique_ptr<ITensorAccessor> accessor)
61 {
62     _accessor = std::move(accessor);
63 }
64 
accessor()65 ITensorAccessor *Tensor::accessor()
66 {
67     return _accessor.get();
68 }
69 
extract_accessor()70 std::unique_ptr<ITensorAccessor> Tensor::extract_accessor()
71 {
72     return std::move(_accessor);
73 }
74 
call_accessor()75 bool Tensor::call_accessor()
76 {
77     // Early exit guard
78     if(!_accessor || !_handle)
79     {
80         return false;
81     }
82 
83     const bool access_data = _accessor->access_tensor_data();
84 
85     if(access_data)
86     {
87         // 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         }
95     }
96 
97     // Call accessor
98     bool retval = _accessor->access_tensor(_handle->tensor());
99 
100     if(access_data)
101     {
102         // Unmap tensor
103         _handle->unmap();
104     }
105 
106     return retval;
107 }
108 
bind_edge(EdgeID eid)109 void Tensor::bind_edge(EdgeID eid)
110 {
111     _bound_edges.insert(eid);
112 }
113 
unbind_edge(EdgeID eid)114 void Tensor::unbind_edge(EdgeID eid)
115 {
116     _bound_edges.erase(eid);
117 }
118 
bound_edges() const119 std::set<EdgeID> Tensor::bound_edges() const
120 {
121     return _bound_edges;
122 }
123 } // namespace graph
124 } // namespace arm_compute
125