blob: b1698e4672085665445800b626985b8a55ff2859 [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 Barbier8db83182018-02-27 13:08:00 +000065 CLFileTuner _tuner{};
Anthony Barbier2a07e182017-08-04 18:20:27 +010066
67private:
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010068 ITensorObject *_current_input{ nullptr };
69 GraphHints _previous_hints{};
Anthony Barbier2a07e182017-08-04 18:20:27 +010070};
71
72Graph::~Graph() //NOLINT
73{
74 //Can't use =default because the destructor must be defined after Graph::Private's definition
75}
76
77Graph::Graph()
78 : _pimpl{ new Private() }
79{
Alex Gilday8913d8d2018-02-15 11:07:18 +000080 graph_init();
Michele Di Giorgioe3fba0a2018-02-14 14:18:01 +000081}
82
83void Graph::graph_init(const bool use_cl_tuner)
84{
Isabella Gottardib28f29d2017-11-09 17:05:07 +000085 // Check if OpenCL is available and initialize the scheduler
86 if(opencl_is_available())
87 {
Anthony Barbier8db83182018-02-27 13:08:00 +000088 _pimpl->_tuner.set_tune_new_kernels(use_cl_tuner);
89 _pimpl->_tuner.set_update_file(use_cl_tuner);
90 arm_compute::CLScheduler::get().default_init(&_pimpl->_tuner);
Isabella Gottardib28f29d2017-11-09 17:05:07 +000091 }
Anthony Barbier2a07e182017-08-04 18:20:27 +010092}
93
94void Graph::run()
95{
96 while(true)
97 {
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010098 if(_pimpl->_graph_input->has_accessor() && !_pimpl->_graph_input->call_accessor())
Anthony Barbier2a07e182017-08-04 18:20:27 +010099 {
100 return;
101 }
102
103 for(auto &stage : _pimpl->_pipeline)
104 {
105 stage._function->run();
106 }
107
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100108 if((_pimpl->_graph_output->has_accessor() && !_pimpl->_graph_output->call_accessor())
109 || (!_pimpl->_graph_output->has_accessor()))
Anthony Barbier2a07e182017-08-04 18:20:27 +0100110 {
111 return;
112 }
113 }
114}
115
116//Finalize current node's configuration
Georgios Pinitasff421f22017-10-04 16:53:58 +0100117void Graph::Private::configure(GraphHints _next_hints)
Anthony Barbier2a07e182017-08-04 18:20:27 +0100118{
119 ARM_COMPUTE_ERROR_ON(_current_node == nullptr);
120 ARM_COMPUTE_ERROR_ON(_graph_input == nullptr);
121
122 // Is it the first node of the graph ?
123 if(_current_input == nullptr)
124 {
Georgios Pinitasff421f22017-10-04 16:53:58 +0100125 _graph_input->set_target(_current_hints.target_hint());
126 _current_input = _graph_input.get();
127 _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 +0100128 }
129
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000130 if(_current_node->supports_in_place())
131 {
132 _current_output = _current_input;
133 }
134
Anthony Barbier2a07e182017-08-04 18:20:27 +0100135 //Automatic output configuration ?
136 if(_current_output == nullptr)
137 {
138 _tensors.push_back(arm_compute::support::cpp14::make_unique<Tensor>(TensorInfo()));
139 _current_output = _tensors.back().get();
140 }
141
142 // If either the writer or reader node needs OpenCL then use OpenCL memory:
Georgios Pinitasff421f22017-10-04 16:53:58 +0100143 if((_next_hints.target_hint() == TargetHint::OPENCL || _current_hints.target_hint() == TargetHint::OPENCL))
Anthony Barbier2a07e182017-08-04 18:20:27 +0100144 {
Georgios Pinitasff421f22017-10-04 16:53:58 +0100145 _current_output->set_target(TargetHint::OPENCL);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100146 }
147 else
148 {
Georgios Pinitasff421f22017-10-04 16:53:58 +0100149 _current_output->set_target(TargetHint::NEON);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100150 }
151
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100152 // Instantiate Node
Georgios Pinitasff421f22017-10-04 16:53:58 +0100153 _ctx.hints() = _current_hints;
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100154 std::unique_ptr<arm_compute::IFunction> func = _current_node->instantiate_node(_ctx, _current_input, _current_output);
155
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000156 // If the operation is done in-place, do not allocate or it will prevent following layers from performing the configuration
157 if(!_current_node->supports_in_place())
158 {
159 // Allocate current input
160 _current_input->allocate();
161 }
Anthony Barbier2a07e182017-08-04 18:20:27 +0100162
Georgios Pinitasff421f22017-10-04 16:53:58 +0100163 // Map input if needed
164 if(_current_input->target() == TargetHint::OPENCL)
Anthony Barbier2a07e182017-08-04 18:20:27 +0100165 {
Georgios Pinitasff421f22017-10-04 16:53:58 +0100166 if(_previous_hints.target_hint() == TargetHint::NEON)
Anthony Barbier2a07e182017-08-04 18:20:27 +0100167 {
Georgios Pinitasff421f22017-10-04 16:53:58 +0100168 ARM_COMPUTE_ERROR_ON(_current_hints.target_hint() == TargetHint::NEON);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100169 _pipeline.push_back({ _current_input, _current_input, arm_compute::support::cpp14::make_unique<CLUnmap>(_current_input) });
170 }
Georgios Pinitasff421f22017-10-04 16:53:58 +0100171 if(_current_hints.target_hint() == TargetHint::NEON)
Anthony Barbier2a07e182017-08-04 18:20:27 +0100172 {
Georgios Pinitasff421f22017-10-04 16:53:58 +0100173 ARM_COMPUTE_ERROR_ON(_previous_hints.target_hint() == TargetHint::NEON);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100174 _pipeline.push_back({ _current_input, _current_input, arm_compute::support::cpp14::make_unique<CLMap>(_current_input, true) });
175 }
176 }
177
178 _pipeline.push_back({ _current_input, _current_output, std::move(func) });
179
180 _current_input = _current_output;
181 _current_output = nullptr;
Georgios Pinitasff421f22017-10-04 16:53:58 +0100182 std::swap(_previous_hints, _current_hints);
183 std::swap(_current_hints, _next_hints);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100184}
185
Anthony Barbier2a07e182017-08-04 18:20:27 +0100186void Graph::add_node(std::unique_ptr<INode> node)
187{
188 ARM_COMPUTE_ERROR_ON_MSG(_pimpl->_graph_input == nullptr, "The graph's input must be set before the first node is added");
189 ARM_COMPUTE_ERROR_ON_MSG(_pimpl->_graph_output != nullptr, "Nothing can be added after the output tensor");
190 //Trigger the creation of the current Node:
191
Georgios Pinitasff421f22017-10-04 16:53:58 +0100192 GraphHints _next_hints = _pimpl->_next_hints;
193 _next_hints.set_target_hint(node->override_target_hint(_pimpl->_next_hints.target_hint()));
194 ARM_COMPUTE_ERROR_ON(_next_hints.target_hint() == TargetHint::DONT_CARE);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100195 if(_pimpl->_current_node)
196 {
197 //Finalize the previous Node:
Georgios Pinitasff421f22017-10-04 16:53:58 +0100198 _pimpl->configure(_pimpl->_next_hints);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100199 }
200 else
201 {
Georgios Pinitasff421f22017-10-04 16:53:58 +0100202 // If that's the first node then use the same TargetHint before and after the node.
203 _pimpl->_current_hints = _next_hints;
Anthony Barbier2a07e182017-08-04 18:20:27 +0100204 }
205 if(_pimpl->_current_node)
206 {
207 _pimpl->_nodes.push_back(std::move(_pimpl->_current_node));
208 }
209 _pimpl->_current_node = std::move(node);
210}
Anthony Barbier2a07e182017-08-04 18:20:27 +0100211
212//Add a tensor with an Accessor (i.e either the input or output of the graph)
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100213void Graph::add_tensor_object(std::unique_ptr<ITensorObject> tensor)
Anthony Barbier2a07e182017-08-04 18:20:27 +0100214{
215 // If it's the first Tensor added then it will be the input of the Graph.
216 if(_pimpl->_graph_input == nullptr)
217 {
218 ARM_COMPUTE_ERROR_ON(_pimpl->_graph_output != nullptr);
219 ARM_COMPUTE_ERROR_ON(_pimpl->_current_node != nullptr);
220 _pimpl->_graph_input = std::move(tensor);
221 }
222 else
223 {
224 // Else it will be the output of the Graph
225 ARM_COMPUTE_ERROR_ON(_pimpl->_graph_output != nullptr);
226 ARM_COMPUTE_ERROR_ON(_pimpl->_current_node == nullptr);
227 _pimpl->_graph_output = std::move(tensor);
228 _pimpl->_current_output = _pimpl->_graph_output.get();
229
230 // Finalize the graph by configuring the last Node of the graph:
Georgios Pinitasff421f22017-10-04 16:53:58 +0100231 _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 +0100232 _pimpl->_graph_output->allocate();
233 }
234}
Gian Marco36a0a462018-01-12 10:21:40 +0000235
Isabella Gottardib28f29d2017-11-09 17:05:07 +0000236bool Graph::opencl_is_available()
237{
238 return arm_compute::opencl_is_available();
239}
Anthony Barbier2a07e182017-08-04 18:20:27 +0100240
Gian Marco36a0a462018-01-12 10:21:40 +0000241arm_compute::GPUTarget Graph::gpu_target()
242{
243 // Check if OpenCL is available before returning the GPU target
244 if(opencl_is_available())
245 {
246 return arm_compute::CLScheduler::get().target();
247 }
248 else
249 {
250 return GPUTarget::MIDGARD;
251 }
252}
253
Anthony Barbier2a07e182017-08-04 18:20:27 +0100254void Graph::set_temp(TensorInfo &&tmp)
255{
256 ARM_COMPUTE_ERROR_ON(_pimpl->_graph_input == nullptr);
257 ARM_COMPUTE_ERROR_ON(_pimpl->_graph_output != nullptr);
258 ARM_COMPUTE_ERROR_ON_MSG(_pimpl->_current_output != nullptr, "TensorInfo for temporary tensor already set");
259
260 _pimpl->_tensors.push_back(arm_compute::support::cpp14::make_unique<Tensor>(std::move(tmp)));
261 _pimpl->_current_output = _pimpl->_tensors.back().get();
262}
263
Georgios Pinitasff421f22017-10-04 16:53:58 +0100264GraphHints &Graph::hints()
265{
266 return _pimpl->_next_hints;
267}
268
Anthony Barbier2a07e182017-08-04 18:20:27 +0100269Graph &arm_compute::graph::operator<<(Graph &graph, TensorInfo &&info)
270{
271 graph.set_temp(std::move(info));
272 return graph;
273}
274
275Graph &arm_compute::graph::operator<<(Graph &graph, Tensor &&tensor)
276{
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100277 graph.add_tensor_object(arm_compute::support::cpp14::make_unique<Tensor>(std::move(tensor)));
278 return graph;
279}
280
281Graph &arm_compute::graph::operator<<(Graph &graph, SubTensor &&sub_tensor)
282{
283 graph.add_tensor_object(arm_compute::support::cpp14::make_unique<SubTensor>(std::move(sub_tensor)));
Anthony Barbier2a07e182017-08-04 18:20:27 +0100284 return graph;
285}
286
Georgios Pinitasff421f22017-10-04 16:53:58 +0100287Graph &arm_compute::graph::operator<<(Graph &graph, TargetHint target_hint)
Anthony Barbier2a07e182017-08-04 18:20:27 +0100288{
Georgios Pinitasff421f22017-10-04 16:53:58 +0100289 graph.hints().set_target_hint(target_hint);
290 return graph;
291}
292
293Graph &arm_compute::graph::operator<<(Graph &graph, ConvolutionMethodHint conv_method_hint)
294{
295 graph.hints().set_convolution_method_hint(conv_method_hint);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100296 return graph;
297}