blob: 6268583938efaa461607b3a9c978c3dd13a8572a [file] [log] [blame]
Georgios Pinitasfbb80542018-03-27 17:15:49 +01001/*
2 * Copyright (c) 2018 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 */
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010024#include "arm_compute/graph/backends/GLES/GCFunctionFactory.h"
Georgios Pinitasfbb80542018-03-27 17:15:49 +010025
26#include "arm_compute/core/utils/misc/Cast.h"
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010027#include "arm_compute/graph/Graph.h"
Georgios Pinitasda2491f2018-06-01 17:49:09 +010028#include "arm_compute/graph/backends/FunctionHelpers.h"
Georgios Pinitasfbb80542018-03-27 17:15:49 +010029#include "arm_compute/runtime/GLES_COMPUTE/GCFunctions.h"
30
Georgios Pinitasfbb80542018-03-27 17:15:49 +010031using namespace arm_compute::utils::cast;
32
33namespace arm_compute
34{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010035namespace graph
Georgios Pinitasfbb80542018-03-27 17:15:49 +010036{
37namespace backends
38{
Georgios Pinitasda2491f2018-06-01 17:49:09 +010039/** Target specific information structure used to pass information to the layer templates */
40struct GCTargetInfo
Georgios Pinitasfbb80542018-03-27 17:15:49 +010041{
Georgios Pinitasda2491f2018-06-01 17:49:09 +010042 using TensorType = arm_compute::IGCTensor;
43 static Target TargetType;
44};
Georgios Pinitasfbb80542018-03-27 17:15:49 +010045
Georgios Pinitasda2491f2018-06-01 17:49:09 +010046Target GCTargetInfo::TargetType = Target::GC;
Georgios Pinitasfbb80542018-03-27 17:15:49 +010047
Georgios Pinitasda2491f2018-06-01 17:49:09 +010048/** Collection of GC convolution functions */
49struct GCConvolutionLayerFunctions
Georgios Pinitasfbb80542018-03-27 17:15:49 +010050{
Georgios Pinitasda2491f2018-06-01 17:49:09 +010051 using GenericConvolutionLayer = GCConvolutionLayer;
52 using GEMMConvolutionLayer = GCConvolutionLayer;
53 using DirectConvolutionLayer = GCDirectConvolutionLayer;
54};
55
56/** Collection of GC depthwise convolution functions */
57struct GCDepthwiseConvolutionLayerFunctions
58{
59 using DepthwiseConvolutionLayer3x3 = GCDepthwiseConvolutionLayer3x3;
60};
61
62/** Collection of GC element-wise functions */
63struct GCEltwiseFunctions
64{
65 using Addition = GCArithmeticAddition;
66 using Multiplication = GCPixelWiseMultiplication;
67};
68
69namespace detail
70{
Georgios Pinitase2220552018-07-20 13:23:44 +010071// Specialize functions
72template <>
73std::unique_ptr<IFunction> create_concatenate_layer<GCDepthConcatenateLayer, GCTargetInfo>(ConcatenateLayerNode &node)
74{
75 ARM_COMPUTE_LOG_GRAPH_VERBOSE("Creating Concatenate node with ID : " << node.id() << " and Name: " << node.name() << std::endl);
76 ARM_COMPUTE_ERROR_ON(node.num_outputs() != 1);
77
78 // Return nullptr if depth concatenate is switched off
79 if(!node.is_enabled())
80 {
81 return nullptr;
82 }
83
84 // Extract IO and info
85 std::vector<GCTargetInfo::TensorType *> inputs;
86 for(unsigned int i = 0; i < node.num_inputs(); ++i)
87 {
88 inputs.push_back(get_backing_tensor<GCTargetInfo>(node.input(i)));
89 }
90 typename GCTargetInfo::TensorType *output = get_backing_tensor<GCTargetInfo>(node.output(0));
91
92 // Create and configure function
93 auto func = support::cpp14::make_unique<GCDepthConcatenateLayer>();
94 func->configure(inputs, output);
95
96 // Log info
97 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << node.type()
98 << " Target " << GCTargetInfo::TargetType
99 << " Data Type: " << output->info()->data_type()
100 << " Shape: " << output->info()->tensor_shape()
101 << " Num Inputs: " << inputs.size()
102 << std::endl);
103
104 return std::move(func);
105}
106
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100107template <>
108std::unique_ptr<IFunction> create_convolution_layer<GCConvolutionLayerFunctions, GCTargetInfo>(ConvolutionLayerNode &node, GraphContext &ctx)
109{
110 validate_node<GCTargetInfo>(node, 3 /* expected inputs */, 1 /* expected outputs */);
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100111
112 // Extract IO and info
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100113 GCTargetInfo::TensorType *input = get_backing_tensor<GCTargetInfo>(node.input(0));
114 GCTargetInfo::TensorType *weights = get_backing_tensor<GCTargetInfo>(node.input(1));
115 GCTargetInfo::TensorType *biases = get_backing_tensor<GCTargetInfo>(node.input(2));
116 GCTargetInfo::TensorType *output = get_backing_tensor<GCTargetInfo>(node.output(0));
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100117
118 if(is_data_type_quantized_asymmetric(input->info()->data_type()))
119 {
120 biases->info()->set_data_type(DataType::S32);
121 }
122
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100123 const PadStrideInfo conv_info = node.convolution_info();
124 const ConvolutionMethod conv_algorithm = node.convolution_method();
125
126 // Create and configure function (we assume that functions have been validated before creation)
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100127 std::shared_ptr<IMemoryManager> mm = get_memory_manager(ctx, GCTargetInfo::TargetType);
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100128 std::unique_ptr<IFunction> func;
129 std::string func_name;
130
Georgios Pinitase2220552018-07-20 13:23:44 +0100131 if(conv_algorithm == ConvolutionMethod::Direct)
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100132 {
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100133 std::tie(func, func_name) = create_named_function<GCConvolutionLayerFunctions::DirectConvolutionLayer>(
134 std::string("DirectConvolutionLayer"),
135 input, weights, biases, output, conv_info);
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100136 }
137 else
138 {
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100139 std::tie(func, func_name) = create_named_memory_managed_function<GCConvolutionLayerFunctions::GenericConvolutionLayer>(
140 std::string("ConvolutionLayer"), mm,
141 input, weights, biases, output, conv_info);
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100142 }
143
144 // Log info
145 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << func_name
146 << " Data Type: " << input->info()->data_type()
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100147 << " Input QuantInfo: " << input->info()->quantization_info()
148 << " Weights QuantInfo: " << weights->info()->quantization_info()
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100149 << " Input shape: " << input->info()->tensor_shape()
150 << " Weights shape: " << weights->info()->tensor_shape()
151 << " Output shape: " << output->info()->tensor_shape()
152 << std::endl);
153 return func;
154}
155
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100156template <>
157std::unique_ptr<IFunction> create_depthwise_convolution_layer<GCDepthwiseConvolutionLayerFunctions, GCTargetInfo>(DepthwiseConvolutionLayerNode &node)
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100158{
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100159 validate_node<GCTargetInfo>(node, 3 /* expected inputs */, 1 /* expected outputs */);
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100160
161 // Extract IO and info
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100162 GCTargetInfo::TensorType *input = get_backing_tensor<GCTargetInfo>(node.input(0));
163 GCTargetInfo::TensorType *weights = get_backing_tensor<GCTargetInfo>(node.input(1));
164 GCTargetInfo::TensorType *biases = get_backing_tensor<GCTargetInfo>(node.input(2));
165 GCTargetInfo::TensorType *output = get_backing_tensor<GCTargetInfo>(node.output(0));
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100166
167 if(is_data_type_quantized_asymmetric(input->info()->data_type()))
168 {
169 biases->info()->set_data_type(DataType::S32);
170 }
171
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100172 const PadStrideInfo conv_info = node.convolution_info();
173 const DepthwiseConvolutionMethod dwc_algorithm = node.depthwise_convolution_method();
174
175 // Create and configure function (we assume that functions have been validated before creation)
176 std::unique_ptr<IFunction> func;
177 std::string func_name;
Georgios Pinitase2220552018-07-20 13:23:44 +0100178 if(dwc_algorithm == DepthwiseConvolutionMethod::Optimized3x3)
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100179 {
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100180 std::tie(func, func_name) = create_named_function<GCDepthwiseConvolutionLayerFunctions::DepthwiseConvolutionLayer3x3>(
181 std::string("DepthwiseConvolutionLayer3x3"),
182 input, weights, biases, output, conv_info);
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100183 }
184 else
185 {
186 ARM_COMPUTE_ERROR("Generic DepthwiseConvolutionLayer is not supported in GLES backend");
187 }
188
189 // Log info
190 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << func_name
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100191 << " Target " << GCTargetInfo::TargetType
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100192 << " Data Type: " << input->info()->data_type()
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100193 << " Input QuantInfo: " << input->info()->quantization_info()
194 << " Weights QuantInfo: " << weights->info()->quantization_info()
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100195 << " Input shape: " << input->info()->tensor_shape()
196 << " Weights shape: " << weights->info()->tensor_shape()
197 << " Output shape: " << output->info()->tensor_shape()
198 << std::endl);
199 return func;
200}
201
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100202template <>
203std::unique_ptr<IFunction> create_eltwise_layer<GCEltwiseFunctions, GCTargetInfo>(EltwiseLayerNode &node)
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100204{
205 ARM_COMPUTE_LOG_GRAPH_VERBOSE(
206 "Creating GC EltwiseLayer node with ID : " << node.id() << " and Name: " << node.name() << std::endl);
207 ARM_COMPUTE_ERROR_ON(node.num_inputs() != 2);
208 ARM_COMPUTE_ERROR_ON(node.num_outputs() != 1);
209
210 // Extract IO and info
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100211 GCTargetInfo::TensorType *input1 = get_backing_tensor<GCTargetInfo>(node.input(0));
212 GCTargetInfo::TensorType *input2 = get_backing_tensor<GCTargetInfo>(node.input(1));
213 GCTargetInfo::TensorType *output = get_backing_tensor<GCTargetInfo>(node.output(0));
214 const EltwiseOperation eltwise_op = node.eltwise_operation();
215 const ConvertPolicy convert_policy = node.convert_policy();
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100216 ARM_COMPUTE_ERROR_ON(input1 == nullptr);
217 ARM_COMPUTE_ERROR_ON(input2 == nullptr);
218 ARM_COMPUTE_ERROR_ON(output == nullptr);
219
220 std::unique_ptr<IFunction> func = nullptr;
221 std::string func_name;
Georgios Pinitase2220552018-07-20 13:23:44 +0100222 if(eltwise_op == EltwiseOperation::Add)
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100223 {
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100224 std::tie(func, func_name) = create_named_function<GCEltwiseFunctions::Addition>(
225 std::string("GCArithmeticAddition"),
226 input1, input2, output, convert_policy);
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100227 }
Georgios Pinitase2220552018-07-20 13:23:44 +0100228 else if(eltwise_op == EltwiseOperation::Sub)
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100229 {
230 ARM_COMPUTE_ERROR("Arithmetic subtraction is not supported in GLES backend");
231 }
Georgios Pinitase2220552018-07-20 13:23:44 +0100232 else if(eltwise_op == EltwiseOperation::Mul)
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100233 {
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100234 std::tie(func, func_name) = create_named_function<GCEltwiseFunctions::Multiplication>(
235 std::string("PixelWiseMultiplication"),
236 input1, input2, output, 1.f);
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100237 }
238 else
239 {
240 ARM_COMPUTE_ERROR("Unsupported element-wise operation!");
241 }
242
243 // Log info
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100244 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << node.type()
245 << " Target " << GCTargetInfo::TargetType
246 << " Operation " << func_name
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100247 << " Data Type: " << input1->info()->data_type()
248 << " Shape : " << input1->info()->tensor_shape()
249 << std::endl);
250
251 return func;
252}
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100253} //namespace detail
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100254
255std::unique_ptr<IFunction> GCFunctionFactory::create(INode *node, GraphContext &ctx)
256{
257 if(node == nullptr)
258 {
259 return nullptr;
260 }
261
262 NodeType type = node->type();
263 switch(type)
264 {
265 case NodeType::ActivationLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100266 return detail::create_activation_layer<GCActivationLayer, GCTargetInfo>(*polymorphic_downcast<ActivationLayerNode *>(node));
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100267 case NodeType::BatchNormalizationLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100268 return detail::create_batch_normalization_layer<GCBatchNormalizationLayer, GCTargetInfo>(*polymorphic_downcast<BatchNormalizationLayerNode *>(node));
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100269 case NodeType::ConvolutionLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100270 return detail::create_convolution_layer<GCConvolutionLayerFunctions, GCTargetInfo>(*polymorphic_downcast<ConvolutionLayerNode *>(node), ctx);
Georgios Pinitase2220552018-07-20 13:23:44 +0100271 case NodeType::ConcatenateLayer:
272 return detail::create_concatenate_layer<GCDepthConcatenateLayer, GCTargetInfo>(*polymorphic_downcast<ConcatenateLayerNode *>(node));
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100273 case NodeType::DepthwiseConvolutionLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100274 return detail::create_depthwise_convolution_layer<GCDepthwiseConvolutionLayerFunctions, GCTargetInfo>(*polymorphic_downcast<DepthwiseConvolutionLayerNode *>(node));
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100275 case NodeType::EltwiseLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100276 return detail::create_eltwise_layer<GCEltwiseFunctions, GCTargetInfo>(*polymorphic_downcast<EltwiseLayerNode *>(node));
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100277 case NodeType::FullyConnectedLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100278 return detail::create_fully_connected_layer<GCFullyConnectedLayer, GCTargetInfo>(*polymorphic_downcast<FullyConnectedLayerNode *>(node), ctx);
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100279 case NodeType::NormalizationLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100280 return detail::create_normalization_layer<GCNormalizationLayer, GCTargetInfo>(*polymorphic_downcast<NormalizationLayerNode *>(node), ctx);
Michele Di Giorgio555d1102018-09-12 13:51:59 +0100281 case NodeType::NormalizePlanarYUVLayer:
282 return detail::create_normalize_planar_yuv_layer<GCNormalizePlanarYUVLayer, GCTargetInfo>(*polymorphic_downcast<NormalizePlanarYUVLayerNode *>(node));
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100283 case NodeType::PoolingLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100284 return detail::create_pooling_layer<GCPoolingLayer, GCTargetInfo>(*polymorphic_downcast<PoolingLayerNode *>(node));
285 case NodeType::ResizeLayer:
286 return detail::create_resize_layer<GCScale, GCTargetInfo>(*polymorphic_downcast<ResizeLayerNode *>(node));
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100287 case NodeType::SoftmaxLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100288 return detail::create_softmax_layer<GCSoftmaxLayer, GCTargetInfo>(*polymorphic_downcast<SoftmaxLayerNode *>(node), ctx);
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100289 default:
290 return nullptr;
291 }
292}
293} // namespace backends
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100294} // namespace graph
Michele Di Giorgio555d1102018-09-12 13:51:59 +0100295} // namespace arm_compute