blob: e14bea0846f0d04577a792dc6207613c8c2f18e7 [file] [log] [blame]
Anthony Barbier2a07e182017-08-04 18:20:27 +01001/*
Gian Marco36a0a462018-01-12 10:21:40 +00002 * Copyright (c) 2017-2018 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 */
24#include "arm_compute/graph/Graph.h"
25
26#include "arm_compute/graph/CL/CLMap.h"
27#include "arm_compute/graph/CL/CLUnmap.h"
28#include "arm_compute/graph/INode.h"
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010029#include "arm_compute/graph/ITensorObject.h"
Anthony Barbier2a07e182017-08-04 18:20:27 +010030#include "arm_compute/graph/Tensor.h"
Isabella Gottardib28f29d2017-11-09 17:05:07 +000031#include "arm_compute/runtime/CL/CLScheduler.h"
Anthony Barbier2a07e182017-08-04 18:20:27 +010032#include "arm_compute/runtime/CL/CLTensor.h"
33#include "arm_compute/runtime/Tensor.h"
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010034#include "support/ToolchainSupport.h"
Anthony Barbier2a07e182017-08-04 18:20:27 +010035
36using namespace arm_compute::graph;
37
38struct Stage
39{
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010040 ITensorObject *_input;
41 ITensorObject *_output;
Anthony Barbier2a07e182017-08-04 18:20:27 +010042 std::unique_ptr<arm_compute::IFunction> _function;
43};
44
45struct Graph::Private
46{
47public:
48 /** Finalizes the current node's configuration
49 *
50 * @param _next_hint Device execution hint
51 */
Georgios Pinitasff421f22017-10-04 16:53:58 +010052 void configure(GraphHints _next_hints);
Anthony Barbier2a07e182017-08-04 18:20:27 +010053
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010054 GraphContext _ctx{};
55 std::vector<Stage> _pipeline{};
56 std::vector<std::unique_ptr<ITensorObject>> _tensors{};
57 std::vector<std::unique_ptr<INode>> _nodes{};
58 GraphHints _current_hints{};
59 GraphHints _next_hints{};
60 std::unique_ptr<ITensorObject> _graph_input{ nullptr };
61 std::unique_ptr<ITensorObject> _graph_output{ nullptr };
62 std::unique_ptr<INode> _current_node{ nullptr };
63 ITensorObject *_current_output{ nullptr };
64 bool _info_enabled{ false };
Anthony Barbier2a07e182017-08-04 18:20:27 +010065
66private:
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010067 ITensorObject *_current_input{ nullptr };
68 GraphHints _previous_hints{};
Anthony Barbier2a07e182017-08-04 18:20:27 +010069};
70
71Graph::~Graph() //NOLINT
72{
73 //Can't use =default because the destructor must be defined after Graph::Private's definition
74}
75
76Graph::Graph()
77 : _pimpl{ new Private() }
78{
Isabella Gottardib28f29d2017-11-09 17:05:07 +000079 // Check if OpenCL is available and initialize the scheduler
80 if(opencl_is_available())
81 {
82 arm_compute::CLScheduler::get().default_init();
83 }
Anthony Barbier2a07e182017-08-04 18:20:27 +010084}
85
86void Graph::run()
87{
88 while(true)
89 {
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010090 if(_pimpl->_graph_input->has_accessor() && !_pimpl->_graph_input->call_accessor())
Anthony Barbier2a07e182017-08-04 18:20:27 +010091 {
92 return;
93 }
94
95 for(auto &stage : _pimpl->_pipeline)
96 {
97 stage._function->run();
98 }
99
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100100 if((_pimpl->_graph_output->has_accessor() && !_pimpl->_graph_output->call_accessor())
101 || (!_pimpl->_graph_output->has_accessor()))
Anthony Barbier2a07e182017-08-04 18:20:27 +0100102 {
103 return;
104 }
105 }
106}
107
108//Finalize current node's configuration
Georgios Pinitasff421f22017-10-04 16:53:58 +0100109void Graph::Private::configure(GraphHints _next_hints)
Anthony Barbier2a07e182017-08-04 18:20:27 +0100110{
111 ARM_COMPUTE_ERROR_ON(_current_node == nullptr);
112 ARM_COMPUTE_ERROR_ON(_graph_input == nullptr);
113
114 // Is it the first node of the graph ?
115 if(_current_input == nullptr)
116 {
Georgios Pinitasff421f22017-10-04 16:53:58 +0100117 _graph_input->set_target(_current_hints.target_hint());
118 _current_input = _graph_input.get();
119 _previous_hints = _current_hints; // For the first node just assume the previous node was of the same type as this one
Anthony Barbier2a07e182017-08-04 18:20:27 +0100120 }
121
122 //Automatic output configuration ?
123 if(_current_output == nullptr)
124 {
125 _tensors.push_back(arm_compute::support::cpp14::make_unique<Tensor>(TensorInfo()));
126 _current_output = _tensors.back().get();
127 }
128
129 // If either the writer or reader node needs OpenCL then use OpenCL memory:
Georgios Pinitasff421f22017-10-04 16:53:58 +0100130 if((_next_hints.target_hint() == TargetHint::OPENCL || _current_hints.target_hint() == TargetHint::OPENCL))
Anthony Barbier2a07e182017-08-04 18:20:27 +0100131 {
Georgios Pinitasff421f22017-10-04 16:53:58 +0100132 _current_output->set_target(TargetHint::OPENCL);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100133 }
134 else
135 {
Georgios Pinitasff421f22017-10-04 16:53:58 +0100136 _current_output->set_target(TargetHint::NEON);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100137 }
138
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100139 // Instantiate Node
Georgios Pinitasff421f22017-10-04 16:53:58 +0100140 _ctx.hints() = _current_hints;
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100141 std::unique_ptr<arm_compute::IFunction> func = _current_node->instantiate_node(_ctx, _current_input, _current_output);
142
143 // Allocate current input
Anthony Barbier2a07e182017-08-04 18:20:27 +0100144 _current_input->allocate();
145
Georgios Pinitasff421f22017-10-04 16:53:58 +0100146 // Map input if needed
147 if(_current_input->target() == TargetHint::OPENCL)
Anthony Barbier2a07e182017-08-04 18:20:27 +0100148 {
Georgios Pinitasff421f22017-10-04 16:53:58 +0100149 if(_previous_hints.target_hint() == TargetHint::NEON)
Anthony Barbier2a07e182017-08-04 18:20:27 +0100150 {
Georgios Pinitasff421f22017-10-04 16:53:58 +0100151 ARM_COMPUTE_ERROR_ON(_current_hints.target_hint() == TargetHint::NEON);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100152 _pipeline.push_back({ _current_input, _current_input, arm_compute::support::cpp14::make_unique<CLUnmap>(_current_input) });
153 }
Georgios Pinitasff421f22017-10-04 16:53:58 +0100154 if(_current_hints.target_hint() == TargetHint::NEON)
Anthony Barbier2a07e182017-08-04 18:20:27 +0100155 {
Georgios Pinitasff421f22017-10-04 16:53:58 +0100156 ARM_COMPUTE_ERROR_ON(_previous_hints.target_hint() == TargetHint::NEON);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100157 _pipeline.push_back({ _current_input, _current_input, arm_compute::support::cpp14::make_unique<CLMap>(_current_input, true) });
158 }
159 }
160
161 _pipeline.push_back({ _current_input, _current_output, std::move(func) });
162
163 _current_input = _current_output;
164 _current_output = nullptr;
Georgios Pinitasff421f22017-10-04 16:53:58 +0100165 std::swap(_previous_hints, _current_hints);
166 std::swap(_current_hints, _next_hints);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100167}
168
Anthony Barbier2a07e182017-08-04 18:20:27 +0100169void Graph::add_node(std::unique_ptr<INode> node)
170{
171 ARM_COMPUTE_ERROR_ON_MSG(_pimpl->_graph_input == nullptr, "The graph's input must be set before the first node is added");
172 ARM_COMPUTE_ERROR_ON_MSG(_pimpl->_graph_output != nullptr, "Nothing can be added after the output tensor");
173 //Trigger the creation of the current Node:
174
Georgios Pinitasff421f22017-10-04 16:53:58 +0100175 GraphHints _next_hints = _pimpl->_next_hints;
176 _next_hints.set_target_hint(node->override_target_hint(_pimpl->_next_hints.target_hint()));
177 ARM_COMPUTE_ERROR_ON(_next_hints.target_hint() == TargetHint::DONT_CARE);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100178 if(_pimpl->_current_node)
179 {
180 //Finalize the previous Node:
Georgios Pinitasff421f22017-10-04 16:53:58 +0100181 _pimpl->configure(_pimpl->_next_hints);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100182 }
183 else
184 {
Georgios Pinitasff421f22017-10-04 16:53:58 +0100185 // If that's the first node then use the same TargetHint before and after the node.
186 _pimpl->_current_hints = _next_hints;
Anthony Barbier2a07e182017-08-04 18:20:27 +0100187 }
188 if(_pimpl->_current_node)
189 {
190 _pimpl->_nodes.push_back(std::move(_pimpl->_current_node));
191 }
192 _pimpl->_current_node = std::move(node);
193}
Anthony Barbier2a07e182017-08-04 18:20:27 +0100194
195//Add a tensor with an Accessor (i.e either the input or output of the graph)
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100196void Graph::add_tensor_object(std::unique_ptr<ITensorObject> tensor)
Anthony Barbier2a07e182017-08-04 18:20:27 +0100197{
198 // If it's the first Tensor added then it will be the input of the Graph.
199 if(_pimpl->_graph_input == nullptr)
200 {
201 ARM_COMPUTE_ERROR_ON(_pimpl->_graph_output != nullptr);
202 ARM_COMPUTE_ERROR_ON(_pimpl->_current_node != nullptr);
203 _pimpl->_graph_input = std::move(tensor);
204 }
205 else
206 {
207 // Else it will be the output of the Graph
208 ARM_COMPUTE_ERROR_ON(_pimpl->_graph_output != nullptr);
209 ARM_COMPUTE_ERROR_ON(_pimpl->_current_node == nullptr);
210 _pimpl->_graph_output = std::move(tensor);
211 _pimpl->_current_output = _pimpl->_graph_output.get();
212
213 // Finalize the graph by configuring the last Node of the graph:
Georgios Pinitasff421f22017-10-04 16:53:58 +0100214 _pimpl->configure(_pimpl->_current_hints); // Ignore _next_hint as this is the last node, and just use the same hint as before this node.
Anthony Barbier2a07e182017-08-04 18:20:27 +0100215 _pimpl->_graph_output->allocate();
216 }
217}
Gian Marco36a0a462018-01-12 10:21:40 +0000218
Isabella Gottardib28f29d2017-11-09 17:05:07 +0000219bool Graph::opencl_is_available()
220{
221 return arm_compute::opencl_is_available();
222}
Anthony Barbier2a07e182017-08-04 18:20:27 +0100223
Gian Marco36a0a462018-01-12 10:21:40 +0000224arm_compute::GPUTarget Graph::gpu_target()
225{
226 // Check if OpenCL is available before returning the GPU target
227 if(opencl_is_available())
228 {
229 return arm_compute::CLScheduler::get().target();
230 }
231 else
232 {
233 return GPUTarget::MIDGARD;
234 }
235}
236
Anthony Barbier2a07e182017-08-04 18:20:27 +0100237void Graph::set_temp(TensorInfo &&tmp)
238{
239 ARM_COMPUTE_ERROR_ON(_pimpl->_graph_input == nullptr);
240 ARM_COMPUTE_ERROR_ON(_pimpl->_graph_output != nullptr);
241 ARM_COMPUTE_ERROR_ON_MSG(_pimpl->_current_output != nullptr, "TensorInfo for temporary tensor already set");
242
243 _pimpl->_tensors.push_back(arm_compute::support::cpp14::make_unique<Tensor>(std::move(tmp)));
244 _pimpl->_current_output = _pimpl->_tensors.back().get();
245}
246
Georgios Pinitasff421f22017-10-04 16:53:58 +0100247GraphHints &Graph::hints()
248{
249 return _pimpl->_next_hints;
250}
251
Anthony Barbier2a07e182017-08-04 18:20:27 +0100252Graph &arm_compute::graph::operator<<(Graph &graph, TensorInfo &&info)
253{
254 graph.set_temp(std::move(info));
255 return graph;
256}
257
258Graph &arm_compute::graph::operator<<(Graph &graph, Tensor &&tensor)
259{
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100260 graph.add_tensor_object(arm_compute::support::cpp14::make_unique<Tensor>(std::move(tensor)));
261 return graph;
262}
263
264Graph &arm_compute::graph::operator<<(Graph &graph, SubTensor &&sub_tensor)
265{
266 graph.add_tensor_object(arm_compute::support::cpp14::make_unique<SubTensor>(std::move(sub_tensor)));
Anthony Barbier2a07e182017-08-04 18:20:27 +0100267 return graph;
268}
269
Georgios Pinitasff421f22017-10-04 16:53:58 +0100270Graph &arm_compute::graph::operator<<(Graph &graph, TargetHint target_hint)
Anthony Barbier2a07e182017-08-04 18:20:27 +0100271{
Georgios Pinitasff421f22017-10-04 16:53:58 +0100272 graph.hints().set_target_hint(target_hint);
273 return graph;
274}
275
276Graph &arm_compute::graph::operator<<(Graph &graph, ConvolutionMethodHint conv_method_hint)
277{
278 graph.hints().set_convolution_method_hint(conv_method_hint);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100279 return graph;
280}