blob: 47bd67211425fe17ec268896245738b860bc0c60 [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"
Georgios Pinitasc0d1c862018-03-23 15:13:15 +000033#include "arm_compute/runtime/CL/CLTuner.h"
Anthony Barbier2a07e182017-08-04 18:20:27 +010034#include "arm_compute/runtime/Tensor.h"
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010035#include "support/ToolchainSupport.h"
Anthony Barbier2a07e182017-08-04 18:20:27 +010036
Anthony Barbier8b811952018-02-28 13:47:58 +000037#include <sys/stat.h>
38
Anthony Barbier2a07e182017-08-04 18:20:27 +010039using namespace arm_compute::graph;
40
Anthony Barbier8b811952018-02-28 13:47:58 +000041namespace
42{
43bool file_exists(const std::string &filename)
44{
45 std::ifstream file(filename);
46 return file.good();
47}
48
49} // namespace
Anthony Barbier2a07e182017-08-04 18:20:27 +010050struct Stage
51{
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010052 ITensorObject *_input;
53 ITensorObject *_output;
Anthony Barbier2a07e182017-08-04 18:20:27 +010054 std::unique_ptr<arm_compute::IFunction> _function;
55};
56
57struct Graph::Private
58{
59public:
60 /** Finalizes the current node's configuration
61 *
62 * @param _next_hint Device execution hint
63 */
Georgios Pinitasff421f22017-10-04 16:53:58 +010064 void configure(GraphHints _next_hints);
Anthony Barbier2a07e182017-08-04 18:20:27 +010065
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010066 GraphContext _ctx{};
67 std::vector<Stage> _pipeline{};
68 std::vector<std::unique_ptr<ITensorObject>> _tensors{};
69 std::vector<std::unique_ptr<INode>> _nodes{};
70 GraphHints _current_hints{};
71 GraphHints _next_hints{};
72 std::unique_ptr<ITensorObject> _graph_input{ nullptr };
73 std::unique_ptr<ITensorObject> _graph_output{ nullptr };
74 std::unique_ptr<INode> _current_node{ nullptr };
75 ITensorObject *_current_output{ nullptr };
76 bool _info_enabled{ false };
Anthony Barbier8b811952018-02-28 13:47:58 +000077 CLTuner _tuner{};
Anthony Barbier2a07e182017-08-04 18:20:27 +010078
79private:
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010080 ITensorObject *_current_input{ nullptr };
81 GraphHints _previous_hints{};
Anthony Barbier2a07e182017-08-04 18:20:27 +010082};
83
Anthony Barbier8b811952018-02-28 13:47:58 +000084static const std::string tuner_data_filename = "acl_tuner.csv";
Anthony Barbier2a07e182017-08-04 18:20:27 +010085Graph::~Graph() //NOLINT
86{
Anthony Barbier8b811952018-02-28 13:47:58 +000087 if(_pimpl->_tuner.tune_new_kernels() && !_pimpl->_tuner.lws_table().empty())
88 {
89 _pimpl->_tuner.save_to_file(tuner_data_filename);
90 }
Anthony Barbier2a07e182017-08-04 18:20:27 +010091}
92
93Graph::Graph()
94 : _pimpl{ new Private() }
95{
Alex Gilday8913d8d2018-02-15 11:07:18 +000096 graph_init();
Michele Di Giorgioe3fba0a2018-02-14 14:18:01 +000097}
98
99void Graph::graph_init(const bool use_cl_tuner)
100{
Isabella Gottardib28f29d2017-11-09 17:05:07 +0000101 // Check if OpenCL is available and initialize the scheduler
102 if(opencl_is_available())
103 {
Anthony Barbier8b811952018-02-28 13:47:58 +0000104 if(_pimpl->_tuner.lws_table().empty() && file_exists(tuner_data_filename))
105 {
106 _pimpl->_tuner.load_from_file(tuner_data_filename);
107 }
Anthony Barbier8db83182018-02-27 13:08:00 +0000108 _pimpl->_tuner.set_tune_new_kernels(use_cl_tuner);
Anthony Barbier8db83182018-02-27 13:08:00 +0000109 arm_compute::CLScheduler::get().default_init(&_pimpl->_tuner);
Isabella Gottardib28f29d2017-11-09 17:05:07 +0000110 }
Anthony Barbier2a07e182017-08-04 18:20:27 +0100111}
Anthony Barbier2a07e182017-08-04 18:20:27 +0100112void Graph::run()
113{
114 while(true)
115 {
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100116 if(_pimpl->_graph_input->has_accessor() && !_pimpl->_graph_input->call_accessor())
Anthony Barbier2a07e182017-08-04 18:20:27 +0100117 {
118 return;
119 }
120
121 for(auto &stage : _pimpl->_pipeline)
122 {
123 stage._function->run();
124 }
125
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100126 if((_pimpl->_graph_output->has_accessor() && !_pimpl->_graph_output->call_accessor())
127 || (!_pimpl->_graph_output->has_accessor()))
Anthony Barbier2a07e182017-08-04 18:20:27 +0100128 {
129 return;
130 }
131 }
132}
133
134//Finalize current node's configuration
Georgios Pinitasff421f22017-10-04 16:53:58 +0100135void Graph::Private::configure(GraphHints _next_hints)
Anthony Barbier2a07e182017-08-04 18:20:27 +0100136{
137 ARM_COMPUTE_ERROR_ON(_current_node == nullptr);
138 ARM_COMPUTE_ERROR_ON(_graph_input == nullptr);
139
140 // Is it the first node of the graph ?
141 if(_current_input == nullptr)
142 {
Georgios Pinitasff421f22017-10-04 16:53:58 +0100143 _graph_input->set_target(_current_hints.target_hint());
144 _current_input = _graph_input.get();
145 _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 +0100146 }
147
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000148 if(_current_node->supports_in_place())
149 {
150 _current_output = _current_input;
151 }
152
Anthony Barbier2a07e182017-08-04 18:20:27 +0100153 //Automatic output configuration ?
154 if(_current_output == nullptr)
155 {
156 _tensors.push_back(arm_compute::support::cpp14::make_unique<Tensor>(TensorInfo()));
157 _current_output = _tensors.back().get();
158 }
159
160 // If either the writer or reader node needs OpenCL then use OpenCL memory:
Georgios Pinitasff421f22017-10-04 16:53:58 +0100161 if((_next_hints.target_hint() == TargetHint::OPENCL || _current_hints.target_hint() == TargetHint::OPENCL))
Anthony Barbier2a07e182017-08-04 18:20:27 +0100162 {
Georgios Pinitasff421f22017-10-04 16:53:58 +0100163 _current_output->set_target(TargetHint::OPENCL);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100164 }
165 else
166 {
Georgios Pinitasff421f22017-10-04 16:53:58 +0100167 _current_output->set_target(TargetHint::NEON);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100168 }
169
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100170 // Instantiate Node
Georgios Pinitasff421f22017-10-04 16:53:58 +0100171 _ctx.hints() = _current_hints;
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100172 std::unique_ptr<arm_compute::IFunction> func = _current_node->instantiate_node(_ctx, _current_input, _current_output);
173
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000174 // If the operation is done in-place, do not allocate or it will prevent following layers from performing the configuration
175 if(!_current_node->supports_in_place())
176 {
177 // Allocate current input
178 _current_input->allocate();
179 }
Anthony Barbier2a07e182017-08-04 18:20:27 +0100180
Georgios Pinitasff421f22017-10-04 16:53:58 +0100181 // Map input if needed
182 if(_current_input->target() == TargetHint::OPENCL)
Anthony Barbier2a07e182017-08-04 18:20:27 +0100183 {
Georgios Pinitasff421f22017-10-04 16:53:58 +0100184 if(_previous_hints.target_hint() == TargetHint::NEON)
Anthony Barbier2a07e182017-08-04 18:20:27 +0100185 {
Georgios Pinitasff421f22017-10-04 16:53:58 +0100186 ARM_COMPUTE_ERROR_ON(_current_hints.target_hint() == TargetHint::NEON);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100187 _pipeline.push_back({ _current_input, _current_input, arm_compute::support::cpp14::make_unique<CLUnmap>(_current_input) });
188 }
Georgios Pinitasff421f22017-10-04 16:53:58 +0100189 if(_current_hints.target_hint() == TargetHint::NEON)
Anthony Barbier2a07e182017-08-04 18:20:27 +0100190 {
Georgios Pinitasff421f22017-10-04 16:53:58 +0100191 ARM_COMPUTE_ERROR_ON(_previous_hints.target_hint() == TargetHint::NEON);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100192 _pipeline.push_back({ _current_input, _current_input, arm_compute::support::cpp14::make_unique<CLMap>(_current_input, true) });
193 }
194 }
195
196 _pipeline.push_back({ _current_input, _current_output, std::move(func) });
197
198 _current_input = _current_output;
199 _current_output = nullptr;
Georgios Pinitasff421f22017-10-04 16:53:58 +0100200 std::swap(_previous_hints, _current_hints);
201 std::swap(_current_hints, _next_hints);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100202}
203
Anthony Barbier2a07e182017-08-04 18:20:27 +0100204void Graph::add_node(std::unique_ptr<INode> node)
205{
206 ARM_COMPUTE_ERROR_ON_MSG(_pimpl->_graph_input == nullptr, "The graph's input must be set before the first node is added");
207 ARM_COMPUTE_ERROR_ON_MSG(_pimpl->_graph_output != nullptr, "Nothing can be added after the output tensor");
208 //Trigger the creation of the current Node:
209
Georgios Pinitasff421f22017-10-04 16:53:58 +0100210 GraphHints _next_hints = _pimpl->_next_hints;
211 _next_hints.set_target_hint(node->override_target_hint(_pimpl->_next_hints.target_hint()));
212 ARM_COMPUTE_ERROR_ON(_next_hints.target_hint() == TargetHint::DONT_CARE);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100213 if(_pimpl->_current_node)
214 {
215 //Finalize the previous Node:
Georgios Pinitasff421f22017-10-04 16:53:58 +0100216 _pimpl->configure(_pimpl->_next_hints);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100217 }
218 else
219 {
Georgios Pinitasff421f22017-10-04 16:53:58 +0100220 // If that's the first node then use the same TargetHint before and after the node.
221 _pimpl->_current_hints = _next_hints;
Anthony Barbier2a07e182017-08-04 18:20:27 +0100222 }
223 if(_pimpl->_current_node)
224 {
225 _pimpl->_nodes.push_back(std::move(_pimpl->_current_node));
226 }
227 _pimpl->_current_node = std::move(node);
228}
Anthony Barbier2a07e182017-08-04 18:20:27 +0100229
230//Add a tensor with an Accessor (i.e either the input or output of the graph)
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100231void Graph::add_tensor_object(std::unique_ptr<ITensorObject> tensor)
Anthony Barbier2a07e182017-08-04 18:20:27 +0100232{
233 // If it's the first Tensor added then it will be the input of the Graph.
234 if(_pimpl->_graph_input == nullptr)
235 {
236 ARM_COMPUTE_ERROR_ON(_pimpl->_graph_output != nullptr);
237 ARM_COMPUTE_ERROR_ON(_pimpl->_current_node != nullptr);
238 _pimpl->_graph_input = std::move(tensor);
239 }
240 else
241 {
242 // Else it will be the output of the Graph
243 ARM_COMPUTE_ERROR_ON(_pimpl->_graph_output != nullptr);
244 ARM_COMPUTE_ERROR_ON(_pimpl->_current_node == nullptr);
245 _pimpl->_graph_output = std::move(tensor);
246 _pimpl->_current_output = _pimpl->_graph_output.get();
247
248 // Finalize the graph by configuring the last Node of the graph:
Georgios Pinitasff421f22017-10-04 16:53:58 +0100249 _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 +0100250 _pimpl->_graph_output->allocate();
251 }
252}
Gian Marco36a0a462018-01-12 10:21:40 +0000253
Isabella Gottardib28f29d2017-11-09 17:05:07 +0000254bool Graph::opencl_is_available()
255{
256 return arm_compute::opencl_is_available();
257}
Anthony Barbier2a07e182017-08-04 18:20:27 +0100258
Gian Marco36a0a462018-01-12 10:21:40 +0000259arm_compute::GPUTarget Graph::gpu_target()
260{
261 // Check if OpenCL is available before returning the GPU target
262 if(opencl_is_available())
263 {
264 return arm_compute::CLScheduler::get().target();
265 }
266 else
267 {
268 return GPUTarget::MIDGARD;
269 }
270}
271
Anthony Barbier2a07e182017-08-04 18:20:27 +0100272void Graph::set_temp(TensorInfo &&tmp)
273{
274 ARM_COMPUTE_ERROR_ON(_pimpl->_graph_input == nullptr);
275 ARM_COMPUTE_ERROR_ON(_pimpl->_graph_output != nullptr);
276 ARM_COMPUTE_ERROR_ON_MSG(_pimpl->_current_output != nullptr, "TensorInfo for temporary tensor already set");
277
278 _pimpl->_tensors.push_back(arm_compute::support::cpp14::make_unique<Tensor>(std::move(tmp)));
279 _pimpl->_current_output = _pimpl->_tensors.back().get();
280}
281
Georgios Pinitasff421f22017-10-04 16:53:58 +0100282GraphHints &Graph::hints()
283{
284 return _pimpl->_next_hints;
285}
286
Anthony Barbier2a07e182017-08-04 18:20:27 +0100287Graph &arm_compute::graph::operator<<(Graph &graph, TensorInfo &&info)
288{
289 graph.set_temp(std::move(info));
290 return graph;
291}
292
293Graph &arm_compute::graph::operator<<(Graph &graph, Tensor &&tensor)
294{
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100295 graph.add_tensor_object(arm_compute::support::cpp14::make_unique<Tensor>(std::move(tensor)));
296 return graph;
297}
298
299Graph &arm_compute::graph::operator<<(Graph &graph, SubTensor &&sub_tensor)
300{
301 graph.add_tensor_object(arm_compute::support::cpp14::make_unique<SubTensor>(std::move(sub_tensor)));
Anthony Barbier2a07e182017-08-04 18:20:27 +0100302 return graph;
303}
304
Georgios Pinitasff421f22017-10-04 16:53:58 +0100305Graph &arm_compute::graph::operator<<(Graph &graph, TargetHint target_hint)
Anthony Barbier2a07e182017-08-04 18:20:27 +0100306{
Georgios Pinitasff421f22017-10-04 16:53:58 +0100307 graph.hints().set_target_hint(target_hint);
308 return graph;
309}
310
311Graph &arm_compute::graph::operator<<(Graph &graph, ConvolutionMethodHint conv_method_hint)
312{
313 graph.hints().set_convolution_method_hint(conv_method_hint);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100314 return graph;
315}