blob: b4c217b1a4287860d61c0bf3044f9e3f83dd855b [file] [log] [blame]
Georgios Pinitas407c3e62017-10-25 18:26:46 +01001/*
2 * Copyright (c) 2017 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/core/CL/ICLTensor.h"
25#include "arm_compute/core/Error.h"
26#include "arm_compute/graph/IOperation.h"
27#include "arm_compute/graph/NodeContext.h"
28#include "arm_compute/graph/OperationRegistrar.h"
29#include "arm_compute/graph/Types.h"
30#include "arm_compute/runtime/CL/CLFunctions.h"
31#include "support/ToolchainSupport.h"
32#include "utils/GraphTypePrinter.h"
33#include "utils/TypePrinter.h"
34
35#include <memory>
36
37using namespace arm_compute::graph;
38
39/* Activation Layer */
40REGISTER_SIMPLE_OPERATION(CLActivationLayerOperation, OPENCL, OperationType::ActivationLayer)
41{
42 ARM_COMPUTE_ERROR_ON(ctx.num_inputs() != 1);
43 ARM_COMPUTE_ERROR_ON(ctx.num_outputs() != 1);
44 ARM_COMPUTE_ERROR_ON(dynamic_cast<arm_compute::ICLTensor *>(ctx.input(0)) == nullptr);
45 ARM_COMPUTE_ERROR_ON(dynamic_cast<arm_compute::ICLTensor *>(ctx.output(0)) == nullptr);
46
47 // Extract IO and info
48 auto *in = dynamic_cast<arm_compute::ICLTensor *>(ctx.input(0));
49 auto *out = dynamic_cast<arm_compute::ICLTensor *>(ctx.output(0));
50 const auto act_info = ctx.parameter<ActivationLayerInfo>("ActivationLayerInfo");
51
52 // Create and configure function
53 auto activation = arm_compute::support::cpp14::make_unique<arm_compute::CLActivationLayer>();
54 activation->configure(in, out, act_info);
55
56 // Log info
57 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiating CLActivationLayer"
58 << " Data Type: " << in->info()->data_type()
59 << " Input shape: " << in->info()->tensor_shape()
60 << " Output shape: " << out->info()->tensor_shape()
61 << " Activation function: " << act_info.activation()
62 << " a: " << act_info.a()
63 << " b: " << act_info.b()
64 << std::endl);
65
66 return std::move(activation);
67}
68
69/* Batch Normalization Layer */
70REGISTER_SIMPLE_OPERATION(CLBatchNormalizationLayerOperation, OPENCL, OperationType::BatchNormalizationLayer)
71{
72 ARM_COMPUTE_ERROR_ON(ctx.num_inputs() != 5);
73 ARM_COMPUTE_ERROR_ON(ctx.num_outputs() != 1);
74 ARM_COMPUTE_ERROR_ON(dynamic_cast<arm_compute::ICLTensor *>(ctx.input(0)) == nullptr);
75 ARM_COMPUTE_ERROR_ON(dynamic_cast<arm_compute::ICLTensor *>(ctx.input(1)) == nullptr);
76 ARM_COMPUTE_ERROR_ON(dynamic_cast<arm_compute::ICLTensor *>(ctx.input(2)) == nullptr);
77 ARM_COMPUTE_ERROR_ON(dynamic_cast<arm_compute::ICLTensor *>(ctx.input(3)) == nullptr);
78 ARM_COMPUTE_ERROR_ON(dynamic_cast<arm_compute::ICLTensor *>(ctx.input(4)) == nullptr);
79 ARM_COMPUTE_ERROR_ON(dynamic_cast<arm_compute::ICLTensor *>(ctx.output(0)) == nullptr);
80
81 // Extract IO and info
82 auto *in = dynamic_cast<arm_compute::ICLTensor *>(ctx.input(0));
83 auto *mean = dynamic_cast<arm_compute::ICLTensor *>(ctx.input(1));
84 auto *var = dynamic_cast<arm_compute::ICLTensor *>(ctx.input(2));
85 auto *beta = dynamic_cast<arm_compute::ICLTensor *>(ctx.input(3));
86 auto *gamma = dynamic_cast<arm_compute::ICLTensor *>(ctx.input(4));
87 auto *out = dynamic_cast<arm_compute::ICLTensor *>(ctx.output(0));
88 const auto epsilon = ctx.parameter<float>("epsilon");
89
90 // Create and configure function
91 auto batch_norm = arm_compute::support::cpp14::make_unique<arm_compute::CLBatchNormalizationLayer>();
92 batch_norm->configure(in, out, mean, var, beta, gamma, epsilon);
93
94 // Log info
95 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiating CLBatchNormalizationLayer"
96 << " Data Type: " << in->info()->data_type()
97 << " Input shape: " << in->info()->tensor_shape()
98 << " Output shape: " << out->info()->tensor_shape()
99 << " Mean shape: " << mean->info()->tensor_shape()
100 << " Var shape: " << var->info()->tensor_shape()
101 << " Beta shape: " << beta->info()->tensor_shape()
102 << " Gamma shape: " << gamma->info()->tensor_shape()
103 << " Epsilon: " << epsilon
104 << std::endl);
105
106 return std::move(batch_norm);
107}
108
109/* Floor Layer */
110REGISTER_SIMPLE_OPERATION(CLFloorLayerOperation, OPENCL, OperationType::FloorLayer)
111{
112 ARM_COMPUTE_ERROR_ON(ctx.num_inputs() != 1);
113 ARM_COMPUTE_ERROR_ON(ctx.num_outputs() != 1);
114 ARM_COMPUTE_ERROR_ON(dynamic_cast<arm_compute::ICLTensor *>(ctx.input(0)) == nullptr);
115 ARM_COMPUTE_ERROR_ON(dynamic_cast<arm_compute::ICLTensor *>(ctx.output(0)) == nullptr);
116
117 // Extract IO and info
118 auto *in = dynamic_cast<arm_compute::ICLTensor *>(ctx.input(0));
119 auto *out = dynamic_cast<arm_compute::ICLTensor *>(ctx.output(0));
120
121 // Create and configure function
122 auto floor = arm_compute::support::cpp14::make_unique<arm_compute::CLFloor>();
123 floor->configure(in, out);
124
125 // Log info
126 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiating CLFloorLayer"
127 << " Data Type: " << in->info()->data_type()
128 << " Input shape: " << in->info()->tensor_shape()
129 << " Output shape: " << out->info()->tensor_shape()
130 << std::endl);
131
132 return std::move(floor);
133}
134
135/* Fully Connected Layer */
136REGISTER_SIMPLE_OPERATION(CLFullyConnectedLayer, OPENCL, OperationType::FullyConnectedLayer)
137{
138 ARM_COMPUTE_ERROR_ON(ctx.num_inputs() != 3);
139 ARM_COMPUTE_ERROR_ON(ctx.num_outputs() != 1);
140 ARM_COMPUTE_ERROR_ON(dynamic_cast<arm_compute::ICLTensor *>(ctx.input(0)) == nullptr);
141 ARM_COMPUTE_ERROR_ON(dynamic_cast<arm_compute::ICLTensor *>(ctx.input(1)) == nullptr);
142 ARM_COMPUTE_ERROR_ON(dynamic_cast<arm_compute::ICLTensor *>(ctx.input(2)) == nullptr);
143 ARM_COMPUTE_ERROR_ON(dynamic_cast<arm_compute::ICLTensor *>(ctx.output(0)) == nullptr);
144
145 // Extract IO and info
146 auto *in = dynamic_cast<arm_compute::ICLTensor *>(ctx.input(0));
147 auto *weights = dynamic_cast<arm_compute::ICLTensor *>(ctx.input(1));
148 auto *biases = dynamic_cast<arm_compute::ICLTensor *>(ctx.input(2));
149 auto *out = dynamic_cast<arm_compute::ICLTensor *>(ctx.output(0));
150
151 // Create and configure function
152 auto fc = arm_compute::support::cpp14::make_unique<arm_compute::CLFullyConnectedLayer>();
153 fc->configure(in, weights, biases, out);
154
155 // Log info
156 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiating NEFullyConnectedLayer"
157 << " Data Type: " << in->info()->data_type()
158 << " Input shape: " << in->info()->tensor_shape()
159 << " Weights shape: " << weights->info()->tensor_shape()
160 << " Biases Shape: " << biases->info()->tensor_shape()
161 << " Output shape: " << out->info()->tensor_shape()
162 << std::endl);
163
164 return std::move(fc);
165}
166
167/* L2 Normalize Layer */
168REGISTER_SIMPLE_OPERATION(CLL2NormalizeLayerOperation, OPENCL, OperationType::L2NormalizeLayer)
169{
170 ARM_COMPUTE_ERROR_ON(ctx.num_inputs() != 1);
171 ARM_COMPUTE_ERROR_ON(ctx.num_outputs() != 1);
172 ARM_COMPUTE_ERROR_ON(dynamic_cast<arm_compute::ICLTensor *>(ctx.input(0)) == nullptr);
173 ARM_COMPUTE_ERROR_ON(dynamic_cast<arm_compute::ICLTensor *>(ctx.output(0)) == nullptr);
174
175 // Extract IO and info
176 auto *in = dynamic_cast<arm_compute::ICLTensor *>(ctx.input(0));
177 auto *out = dynamic_cast<arm_compute::ICLTensor *>(ctx.output(0));
178 const auto axis = ctx.parameter<unsigned int>("axis");
179 const auto epsilon = ctx.parameter<float>("epsilon");
180
181 // Create and configure function
182 auto l2_norm = arm_compute::support::cpp14::make_unique<arm_compute::CLL2Normalize>();
183 l2_norm->configure(in, out, axis, epsilon);
184
185 // Log info
186 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiating CLL2NormalizeLayer"
187 << " Data Type: " << in->info()->data_type()
188 << " Input shape: " << in->info()->tensor_shape()
189 << " Output shape: " << out->info()->tensor_shape()
190 << " Axis: " << axis
191 << " Epsilon: " << epsilon
192 << std::endl);
193
194 return std::move(l2_norm);
195}
196
197/* Normalization Layer */
198REGISTER_SIMPLE_OPERATION(CLNormalizationLayerOperation, OPENCL, OperationType::NormalizationLayer)
199{
200 ARM_COMPUTE_ERROR_ON(ctx.num_inputs() != 1);
201 ARM_COMPUTE_ERROR_ON(ctx.num_outputs() != 1);
202 ARM_COMPUTE_ERROR_ON(dynamic_cast<arm_compute::ICLTensor *>(ctx.input(0)) == nullptr);
203 ARM_COMPUTE_ERROR_ON(dynamic_cast<arm_compute::ICLTensor *>(ctx.output(0)) == nullptr);
204
205 // Extract IO and info
206 auto *in = dynamic_cast<arm_compute::ICLTensor *>(ctx.input(0));
207 auto *out = dynamic_cast<arm_compute::ICLTensor *>(ctx.output(0));
208 const auto norm_info = ctx.parameter<NormalizationLayerInfo>("NormalizationLayerInfo");
209
210 // Create and configure function
211 auto norm = arm_compute::support::cpp14::make_unique<arm_compute::CLNormalizationLayer>();
212 norm->configure(in, out, norm_info);
213
214 // Log info
215 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiating CLNormalizationLayer"
216 << " Data Type: " << in->info()->data_type()
217 << " Input shape: " << in->info()->tensor_shape()
218 << " Output shape: " << out->info()->tensor_shape()
219 << " Normalization info: " << norm_info
220 << std::endl);
221
222 return std::move(norm);
223}
224
225/* Pooling Layer */
226REGISTER_SIMPLE_OPERATION(CLPoolingLayerOperation, OPENCL, OperationType::PoolingLayer)
227{
228 ARM_COMPUTE_ERROR_ON(ctx.num_inputs() != 1);
229 ARM_COMPUTE_ERROR_ON(ctx.num_outputs() != 1);
230 ARM_COMPUTE_ERROR_ON(dynamic_cast<arm_compute::ICLTensor *>(ctx.input(0)) == nullptr);
231 ARM_COMPUTE_ERROR_ON(dynamic_cast<arm_compute::ICLTensor *>(ctx.output(0)) == nullptr);
232
233 // Extract IO and info
234 auto *in = dynamic_cast<arm_compute::ICLTensor *>(ctx.input(0));
235 auto *out = dynamic_cast<arm_compute::ICLTensor *>(ctx.output(0));
236 const auto pool_info = ctx.parameter<PoolingLayerInfo>("PoolingLayerInfo");
237
238 // Create and configure function
239 auto pool = arm_compute::support::cpp14::make_unique<arm_compute::CLPoolingLayer>();
240 pool->configure(in, out, pool_info);
241
242 // Log info
243 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiating CLPoolingLayer"
244 << " Data Type: " << in->info()->data_type()
245 << " Input shape: " << in->info()->tensor_shape()
246 << " Output shape: " << out->info()->tensor_shape()
247 << " Pooling info: " << pool_info
248 << std::endl);
249
250 return std::move(pool);
251}
252
253/* Softmax Layer */
254REGISTER_SIMPLE_OPERATION(CLSoftmaxLayerOperation, OPENCL, OperationType::SoftmaxLayer)
255{
256 ARM_COMPUTE_ERROR_ON(ctx.num_inputs() != 1);
257 ARM_COMPUTE_ERROR_ON(ctx.num_outputs() != 1);
258 ARM_COMPUTE_ERROR_ON(dynamic_cast<arm_compute::ICLTensor *>(ctx.input(0)) == nullptr);
259 ARM_COMPUTE_ERROR_ON(dynamic_cast<arm_compute::ICLTensor *>(ctx.output(0)) == nullptr);
260
261 // Extract IO and info
262 auto *in = dynamic_cast<arm_compute::ICLTensor *>(ctx.input(0));
263 auto *out = dynamic_cast<arm_compute::ICLTensor *>(ctx.output(0));
264
265 // Create and configure function
266 auto smx = arm_compute::support::cpp14::make_unique<arm_compute::CLSoftmaxLayer>();
267 smx->configure(in, out);
268
269 // Log info
270 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiating CLSoftmaxLayer"
271 << " Data Type: " << in->info()->data_type()
272 << " Input shape: " << in->info()->tensor_shape()
273 << " Output shape: " << out->info()->tensor_shape()
274 << std::endl);
275
276 return std::move(smx);
277}