blob: 2fe3a90aef7afc8527403db39cb258a8d6f9ce31 [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
Anthony Barbier8b811952018-02-28 13:47:58 +000036#include <sys/stat.h>
37
Anthony Barbier2a07e182017-08-04 18:20:27 +010038using namespace arm_compute::graph;
39
Anthony Barbier8b811952018-02-28 13:47:58 +000040namespace
41{
42bool file_exists(const std::string &filename)
43{
44 std::ifstream file(filename);
45 return file.good();
46}
47
48} // namespace
Anthony Barbier2a07e182017-08-04 18:20:27 +010049struct Stage
50{
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010051 ITensorObject *_input;
52 ITensorObject *_output;
Anthony Barbier2a07e182017-08-04 18:20:27 +010053 std::unique_ptr<arm_compute::IFunction> _function;
54};
55
56struct Graph::Private
57{
58public:
59 /** Finalizes the current node's configuration
60 *
61 * @param _next_hint Device execution hint
62 */
Georgios Pinitasff421f22017-10-04 16:53:58 +010063 void configure(GraphHints _next_hints);
Anthony Barbier2a07e182017-08-04 18:20:27 +010064
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010065 GraphContext _ctx{};
66 std::vector<Stage> _pipeline{};
67 std::vector<std::unique_ptr<ITensorObject>> _tensors{};
68 std::vector<std::unique_ptr<INode>> _nodes{};
69 GraphHints _current_hints{};
70 GraphHints _next_hints{};
71 std::unique_ptr<ITensorObject> _graph_input{ nullptr };
72 std::unique_ptr<ITensorObject> _graph_output{ nullptr };
73 std::unique_ptr<INode> _current_node{ nullptr };
74 ITensorObject *_current_output{ nullptr };
75 bool _info_enabled{ false };
Anthony Barbier8b811952018-02-28 13:47:58 +000076 CLTuner _tuner{};
Anthony Barbier2a07e182017-08-04 18:20:27 +010077
78private:
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010079 ITensorObject *_current_input{ nullptr };
80 GraphHints _previous_hints{};
Anthony Barbier2a07e182017-08-04 18:20:27 +010081};
82
Anthony Barbier8b811952018-02-28 13:47:58 +000083static const std::string tuner_data_filename = "acl_tuner.csv";
Anthony Barbier2a07e182017-08-04 18:20:27 +010084Graph::~Graph() //NOLINT
85{
Anthony Barbier8b811952018-02-28 13:47:58 +000086 if(_pimpl->_tuner.tune_new_kernels() && !_pimpl->_tuner.lws_table().empty())
87 {
88 _pimpl->_tuner.save_to_file(tuner_data_filename);
89 }
Anthony Barbier2a07e182017-08-04 18:20:27 +010090}
91
92Graph::Graph()
93 : _pimpl{ new Private() }
94{
Alex Gilday8913d8d2018-02-15 11:07:18 +000095 graph_init();
Michele Di Giorgioe3fba0a2018-02-14 14:18:01 +000096}
97
98void Graph::graph_init(const bool use_cl_tuner)
99{
Isabella Gottardib28f29d2017-11-09 17:05:07 +0000100 // Check if OpenCL is available and initialize the scheduler
101 if(opencl_is_available())
102 {
Anthony Barbier8b811952018-02-28 13:47:58 +0000103 if(_pimpl->_tuner.lws_table().empty() && file_exists(tuner_data_filename))
104 {
105 _pimpl->_tuner.load_from_file(tuner_data_filename);
106 }
Anthony Barbier8db83182018-02-27 13:08:00 +0000107 _pimpl->_tuner.set_tune_new_kernels(use_cl_tuner);
Anthony Barbier8db83182018-02-27 13:08:00 +0000108 arm_compute::CLScheduler::get().default_init(&_pimpl->_tuner);
Isabella Gottardib28f29d2017-11-09 17:05:07 +0000109 }
Anthony Barbier2a07e182017-08-04 18:20:27 +0100110}
Anthony Barbier2a07e182017-08-04 18:20:27 +0100111void Graph::run()
112{
113 while(true)
114 {
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100115 if(_pimpl->_graph_input->has_accessor() && !_pimpl->_graph_input->call_accessor())
Anthony Barbier2a07e182017-08-04 18:20:27 +0100116 {
117 return;
118 }
119
120 for(auto &stage : _pimpl->_pipeline)
121 {
122 stage._function->run();
123 }
124
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100125 if((_pimpl->_graph_output->has_accessor() && !_pimpl->_graph_output->call_accessor())
126 || (!_pimpl->_graph_output->has_accessor()))
Anthony Barbier2a07e182017-08-04 18:20:27 +0100127 {
128 return;
129 }
130 }
131}
132
133//Finalize current node's configuration
Georgios Pinitasff421f22017-10-04 16:53:58 +0100134void Graph::Private::configure(GraphHints _next_hints)
Anthony Barbier2a07e182017-08-04 18:20:27 +0100135{
136 ARM_COMPUTE_ERROR_ON(_current_node == nullptr);
137 ARM_COMPUTE_ERROR_ON(_graph_input == nullptr);
138
139 // Is it the first node of the graph ?
140 if(_current_input == nullptr)
141 {
Georgios Pinitasff421f22017-10-04 16:53:58 +0100142 _graph_input->set_target(_current_hints.target_hint());
143 _current_input = _graph_input.get();
144 _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 +0100145 }
146
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000147 if(_current_node->supports_in_place())
148 {
149 _current_output = _current_input;
150 }
151
Anthony Barbier2a07e182017-08-04 18:20:27 +0100152 //Automatic output configuration ?
153 if(_current_output == nullptr)
154 {
155 _tensors.push_back(arm_compute::support::cpp14::make_unique<Tensor>(TensorInfo()));
156 _current_output = _tensors.back().get();
157 }
158
159 // If either the writer or reader node needs OpenCL then use OpenCL memory:
Georgios Pinitasff421f22017-10-04 16:53:58 +0100160 if((_next_hints.target_hint() == TargetHint::OPENCL || _current_hints.target_hint() == TargetHint::OPENCL))
Anthony Barbier2a07e182017-08-04 18:20:27 +0100161 {
Georgios Pinitasff421f22017-10-04 16:53:58 +0100162 _current_output->set_target(TargetHint::OPENCL);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100163 }
164 else
165 {
Georgios Pinitasff421f22017-10-04 16:53:58 +0100166 _current_output->set_target(TargetHint::NEON);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100167 }
168
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100169 // Instantiate Node
Georgios Pinitasff421f22017-10-04 16:53:58 +0100170 _ctx.hints() = _current_hints;
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100171 std::unique_ptr<arm_compute::IFunction> func = _current_node->instantiate_node(_ctx, _current_input, _current_output);
172
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000173 // If the operation is done in-place, do not allocate or it will prevent following layers from performing the configuration
174 if(!_current_node->supports_in_place())
175 {
176 // Allocate current input
177 _current_input->allocate();
178 }
Anthony Barbier2a07e182017-08-04 18:20:27 +0100179
Georgios Pinitasff421f22017-10-04 16:53:58 +0100180 // Map input if needed
181 if(_current_input->target() == TargetHint::OPENCL)
Anthony Barbier2a07e182017-08-04 18:20:27 +0100182 {
Georgios Pinitasff421f22017-10-04 16:53:58 +0100183 if(_previous_hints.target_hint() == TargetHint::NEON)
Anthony Barbier2a07e182017-08-04 18:20:27 +0100184 {
Georgios Pinitasff421f22017-10-04 16:53:58 +0100185 ARM_COMPUTE_ERROR_ON(_current_hints.target_hint() == TargetHint::NEON);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100186 _pipeline.push_back({ _current_input, _current_input, arm_compute::support::cpp14::make_unique<CLUnmap>(_current_input) });
187 }
Georgios Pinitasff421f22017-10-04 16:53:58 +0100188 if(_current_hints.target_hint() == TargetHint::NEON)
Anthony Barbier2a07e182017-08-04 18:20:27 +0100189 {
Georgios Pinitasff421f22017-10-04 16:53:58 +0100190 ARM_COMPUTE_ERROR_ON(_previous_hints.target_hint() == TargetHint::NEON);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100191 _pipeline.push_back({ _current_input, _current_input, arm_compute::support::cpp14::make_unique<CLMap>(_current_input, true) });
192 }
193 }
194
195 _pipeline.push_back({ _current_input, _current_output, std::move(func) });
196
197 _current_input = _current_output;
198 _current_output = nullptr;
Georgios Pinitasff421f22017-10-04 16:53:58 +0100199 std::swap(_previous_hints, _current_hints);
200 std::swap(_current_hints, _next_hints);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100201}
202
Anthony Barbier2a07e182017-08-04 18:20:27 +0100203void Graph::add_node(std::unique_ptr<INode> node)
204{
205 ARM_COMPUTE_ERROR_ON_MSG(_pimpl->_graph_input == nullptr, "The graph's input must be set before the first node is added");
206 ARM_COMPUTE_ERROR_ON_MSG(_pimpl->_graph_output != nullptr, "Nothing can be added after the output tensor");
207 //Trigger the creation of the current Node:
208
Georgios Pinitasff421f22017-10-04 16:53:58 +0100209 GraphHints _next_hints = _pimpl->_next_hints;
210 _next_hints.set_target_hint(node->override_target_hint(_pimpl->_next_hints.target_hint()));
211 ARM_COMPUTE_ERROR_ON(_next_hints.target_hint() == TargetHint::DONT_CARE);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100212 if(_pimpl->_current_node)
213 {
214 //Finalize the previous Node:
Georgios Pinitasff421f22017-10-04 16:53:58 +0100215 _pimpl->configure(_pimpl->_next_hints);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100216 }
217 else
218 {
Georgios Pinitasff421f22017-10-04 16:53:58 +0100219 // If that's the first node then use the same TargetHint before and after the node.
220 _pimpl->_current_hints = _next_hints;
Anthony Barbier2a07e182017-08-04 18:20:27 +0100221 }
222 if(_pimpl->_current_node)
223 {
224 _pimpl->_nodes.push_back(std::move(_pimpl->_current_node));
225 }
226 _pimpl->_current_node = std::move(node);
227}
Anthony Barbier2a07e182017-08-04 18:20:27 +0100228
229//Add a tensor with an Accessor (i.e either the input or output of the graph)
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100230void Graph::add_tensor_object(std::unique_ptr<ITensorObject> tensor)
Anthony Barbier2a07e182017-08-04 18:20:27 +0100231{
232 // If it's the first Tensor added then it will be the input of the Graph.
233 if(_pimpl->_graph_input == nullptr)
234 {
235 ARM_COMPUTE_ERROR_ON(_pimpl->_graph_output != nullptr);
236 ARM_COMPUTE_ERROR_ON(_pimpl->_current_node != nullptr);
237 _pimpl->_graph_input = std::move(tensor);
238 }
239 else
240 {
241 // Else it will be the output of the Graph
242 ARM_COMPUTE_ERROR_ON(_pimpl->_graph_output != nullptr);
243 ARM_COMPUTE_ERROR_ON(_pimpl->_current_node == nullptr);
244 _pimpl->_graph_output = std::move(tensor);
245 _pimpl->_current_output = _pimpl->_graph_output.get();
246
247 // Finalize the graph by configuring the last Node of the graph:
Georgios Pinitasff421f22017-10-04 16:53:58 +0100248 _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 +0100249 _pimpl->_graph_output->allocate();
250 }
251}
Gian Marco36a0a462018-01-12 10:21:40 +0000252
Isabella Gottardib28f29d2017-11-09 17:05:07 +0000253bool Graph::opencl_is_available()
254{
255 return arm_compute::opencl_is_available();
256}
Anthony Barbier2a07e182017-08-04 18:20:27 +0100257
Gian Marco36a0a462018-01-12 10:21:40 +0000258arm_compute::GPUTarget Graph::gpu_target()
259{
260 // Check if OpenCL is available before returning the GPU target
261 if(opencl_is_available())
262 {
263 return arm_compute::CLScheduler::get().target();
264 }
265 else
266 {
267 return GPUTarget::MIDGARD;
268 }
269}
270
Anthony Barbier2a07e182017-08-04 18:20:27 +0100271void Graph::set_temp(TensorInfo &&tmp)
272{
273 ARM_COMPUTE_ERROR_ON(_pimpl->_graph_input == nullptr);
274 ARM_COMPUTE_ERROR_ON(_pimpl->_graph_output != nullptr);
275 ARM_COMPUTE_ERROR_ON_MSG(_pimpl->_current_output != nullptr, "TensorInfo for temporary tensor already set");
276
277 _pimpl->_tensors.push_back(arm_compute::support::cpp14::make_unique<Tensor>(std::move(tmp)));
278 _pimpl->_current_output = _pimpl->_tensors.back().get();
279}
280
Georgios Pinitasff421f22017-10-04 16:53:58 +0100281GraphHints &Graph::hints()
282{
283 return _pimpl->_next_hints;
284}
285
Anthony Barbier2a07e182017-08-04 18:20:27 +0100286Graph &arm_compute::graph::operator<<(Graph &graph, TensorInfo &&info)
287{
288 graph.set_temp(std::move(info));
289 return graph;
290}
291
292Graph &arm_compute::graph::operator<<(Graph &graph, Tensor &&tensor)
293{
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100294 graph.add_tensor_object(arm_compute::support::cpp14::make_unique<Tensor>(std::move(tensor)));
295 return graph;
296}
297
298Graph &arm_compute::graph::operator<<(Graph &graph, SubTensor &&sub_tensor)
299{
300 graph.add_tensor_object(arm_compute::support::cpp14::make_unique<SubTensor>(std::move(sub_tensor)));
Anthony Barbier2a07e182017-08-04 18:20:27 +0100301 return graph;
302}
303
Georgios Pinitasff421f22017-10-04 16:53:58 +0100304Graph &arm_compute::graph::operator<<(Graph &graph, TargetHint target_hint)
Anthony Barbier2a07e182017-08-04 18:20:27 +0100305{
Georgios Pinitasff421f22017-10-04 16:53:58 +0100306 graph.hints().set_target_hint(target_hint);
307 return graph;
308}
309
310Graph &arm_compute::graph::operator<<(Graph &graph, ConvolutionMethodHint conv_method_hint)
311{
312 graph.hints().set_convolution_method_hint(conv_method_hint);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100313 return graph;
314}