blob: 2ca453ebde44a5eed069f3ebca3cccd2bf90c739 [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
Pablo Tello32521432018-11-15 14:43:10 +000097 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated "
98 << node.name()
Georgios Pinitase2220552018-07-20 13:23:44 +010099 << " Target " << GCTargetInfo::TargetType
100 << " Data Type: " << output->info()->data_type()
101 << " Shape: " << output->info()->tensor_shape()
102 << " Num Inputs: " << inputs.size()
103 << std::endl);
104
105 return std::move(func);
106}
107
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100108template <>
109std::unique_ptr<IFunction> create_convolution_layer<GCConvolutionLayerFunctions, GCTargetInfo>(ConvolutionLayerNode &node, GraphContext &ctx)
110{
111 validate_node<GCTargetInfo>(node, 3 /* expected inputs */, 1 /* expected outputs */);
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100112
113 // Extract IO and info
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100114 GCTargetInfo::TensorType *input = get_backing_tensor<GCTargetInfo>(node.input(0));
115 GCTargetInfo::TensorType *weights = get_backing_tensor<GCTargetInfo>(node.input(1));
116 GCTargetInfo::TensorType *biases = get_backing_tensor<GCTargetInfo>(node.input(2));
117 GCTargetInfo::TensorType *output = get_backing_tensor<GCTargetInfo>(node.output(0));
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100118
119 if(is_data_type_quantized_asymmetric(input->info()->data_type()))
120 {
121 biases->info()->set_data_type(DataType::S32);
122 }
123
Georgios Pinitas08346e92018-10-16 19:10:46 +0100124 const PadStrideInfo conv_info = node.convolution_info();
125 const ConvolutionMethod conv_algorithm = node.convolution_method();
126 const ActivationLayerInfo fused_act = node.fused_activation();
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100127
128 // Create and configure function (we assume that functions have been validated before creation)
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100129 std::shared_ptr<IMemoryManager> mm = get_memory_manager(ctx, GCTargetInfo::TargetType);
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100130 std::unique_ptr<IFunction> func;
131 std::string func_name;
132
Georgios Pinitase2220552018-07-20 13:23:44 +0100133 if(conv_algorithm == ConvolutionMethod::Direct)
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100134 {
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100135 std::tie(func, func_name) = create_named_function<GCConvolutionLayerFunctions::DirectConvolutionLayer>(
136 std::string("DirectConvolutionLayer"),
Georgios Pinitas08346e92018-10-16 19:10:46 +0100137 input, weights, biases, output, conv_info, fused_act);
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100138 }
139 else
140 {
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100141 std::tie(func, func_name) = create_named_memory_managed_function<GCConvolutionLayerFunctions::GenericConvolutionLayer>(
142 std::string("ConvolutionLayer"), mm,
Georgios Pinitas08346e92018-10-16 19:10:46 +0100143 input, weights, biases, output, conv_info, WeightsInfo(), Size2D(1U, 1U), fused_act);
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100144 }
145
146 // Log info
Pablo Tello32521432018-11-15 14:43:10 +0000147 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated "
148 << node.name()
149 << " Type: " << func_name
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100150 << " Data Type: " << input->info()->data_type()
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100151 << " Input QuantInfo: " << input->info()->quantization_info()
152 << " Weights QuantInfo: " << weights->info()->quantization_info()
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100153 << " Input shape: " << input->info()->tensor_shape()
154 << " Weights shape: " << weights->info()->tensor_shape()
155 << " Output shape: " << output->info()->tensor_shape()
Georgios Pinitas08346e92018-10-16 19:10:46 +0100156 << (fused_act.enabled() ? " " + to_string(fused_act.activation()) : "")
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100157 << std::endl);
158 return func;
159}
160
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100161template <>
162std::unique_ptr<IFunction> create_depthwise_convolution_layer<GCDepthwiseConvolutionLayerFunctions, GCTargetInfo>(DepthwiseConvolutionLayerNode &node)
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100163{
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100164 validate_node<GCTargetInfo>(node, 3 /* expected inputs */, 1 /* expected outputs */);
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100165
166 // Extract IO and info
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100167 GCTargetInfo::TensorType *input = get_backing_tensor<GCTargetInfo>(node.input(0));
168 GCTargetInfo::TensorType *weights = get_backing_tensor<GCTargetInfo>(node.input(1));
169 GCTargetInfo::TensorType *biases = get_backing_tensor<GCTargetInfo>(node.input(2));
170 GCTargetInfo::TensorType *output = get_backing_tensor<GCTargetInfo>(node.output(0));
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100171
172 if(is_data_type_quantized_asymmetric(input->info()->data_type()))
173 {
174 biases->info()->set_data_type(DataType::S32);
175 }
176
Georgios Pinitas60e98252018-10-22 16:17:20 +0100177 const PadStrideInfo conv_info = node.convolution_info();
178 const DepthwiseConvolutionMethod dwc_algorithm = node.depthwise_convolution_method();
179 const unsigned int depth_multiplier = 1;
180 const ActivationLayerInfo fused_act = node.fused_activation();
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100181
182 // Create and configure function (we assume that functions have been validated before creation)
183 std::unique_ptr<IFunction> func;
184 std::string func_name;
Georgios Pinitase2220552018-07-20 13:23:44 +0100185 if(dwc_algorithm == DepthwiseConvolutionMethod::Optimized3x3)
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100186 {
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100187 std::tie(func, func_name) = create_named_function<GCDepthwiseConvolutionLayerFunctions::DepthwiseConvolutionLayer3x3>(
188 std::string("DepthwiseConvolutionLayer3x3"),
Georgios Pinitas60e98252018-10-22 16:17:20 +0100189 input, weights, biases, output, conv_info, depth_multiplier, fused_act);
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100190 }
191 else
192 {
193 ARM_COMPUTE_ERROR("Generic DepthwiseConvolutionLayer is not supported in GLES backend");
194 }
195
196 // Log info
Pablo Tello32521432018-11-15 14:43:10 +0000197 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated "
198 << node.name()
199 << " Type: " << func_name
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100200 << " Target " << GCTargetInfo::TargetType
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100201 << " Data Type: " << input->info()->data_type()
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100202 << " Input QuantInfo: " << input->info()->quantization_info()
203 << " Weights QuantInfo: " << weights->info()->quantization_info()
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100204 << " Input shape: " << input->info()->tensor_shape()
205 << " Weights shape: " << weights->info()->tensor_shape()
206 << " Output shape: " << output->info()->tensor_shape()
Georgios Pinitas60e98252018-10-22 16:17:20 +0100207 << (fused_act.enabled() ? " " + to_string(fused_act.activation()) : "")
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100208 << std::endl);
209 return func;
210}
211
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100212template <>
213std::unique_ptr<IFunction> create_eltwise_layer<GCEltwiseFunctions, GCTargetInfo>(EltwiseLayerNode &node)
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100214{
215 ARM_COMPUTE_LOG_GRAPH_VERBOSE(
216 "Creating GC EltwiseLayer node with ID : " << node.id() << " and Name: " << node.name() << std::endl);
217 ARM_COMPUTE_ERROR_ON(node.num_inputs() != 2);
218 ARM_COMPUTE_ERROR_ON(node.num_outputs() != 1);
219
220 // Extract IO and info
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100221 GCTargetInfo::TensorType *input1 = get_backing_tensor<GCTargetInfo>(node.input(0));
222 GCTargetInfo::TensorType *input2 = get_backing_tensor<GCTargetInfo>(node.input(1));
223 GCTargetInfo::TensorType *output = get_backing_tensor<GCTargetInfo>(node.output(0));
224 const EltwiseOperation eltwise_op = node.eltwise_operation();
225 const ConvertPolicy convert_policy = node.convert_policy();
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100226 ARM_COMPUTE_ERROR_ON(input1 == nullptr);
227 ARM_COMPUTE_ERROR_ON(input2 == nullptr);
228 ARM_COMPUTE_ERROR_ON(output == nullptr);
229
230 std::unique_ptr<IFunction> func = nullptr;
231 std::string func_name;
Georgios Pinitase2220552018-07-20 13:23:44 +0100232 if(eltwise_op == EltwiseOperation::Add)
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::Addition>(
235 std::string("GCArithmeticAddition"),
236 input1, input2, output, convert_policy);
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100237 }
Georgios Pinitase2220552018-07-20 13:23:44 +0100238 else if(eltwise_op == EltwiseOperation::Sub)
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100239 {
240 ARM_COMPUTE_ERROR("Arithmetic subtraction is not supported in GLES backend");
241 }
Georgios Pinitase2220552018-07-20 13:23:44 +0100242 else if(eltwise_op == EltwiseOperation::Mul)
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100243 {
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100244 std::tie(func, func_name) = create_named_function<GCEltwiseFunctions::Multiplication>(
245 std::string("PixelWiseMultiplication"),
246 input1, input2, output, 1.f);
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100247 }
248 else
249 {
250 ARM_COMPUTE_ERROR("Unsupported element-wise operation!");
251 }
252
253 // Log info
Pablo Tello32521432018-11-15 14:43:10 +0000254 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated "
255 << node.name()
256 << " Type: " << node.type()
257 << " Target: " << GCTargetInfo::TargetType
258 << " Operation: " << func_name
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100259 << " Data Type: " << input1->info()->data_type()
Pablo Tello32521432018-11-15 14:43:10 +0000260 << " Shape: " << input1->info()->tensor_shape()
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100261 << std::endl);
262
263 return func;
264}
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100265} //namespace detail
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100266
267std::unique_ptr<IFunction> GCFunctionFactory::create(INode *node, GraphContext &ctx)
268{
269 if(node == nullptr)
270 {
271 return nullptr;
272 }
273
274 NodeType type = node->type();
275 switch(type)
276 {
277 case NodeType::ActivationLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100278 return detail::create_activation_layer<GCActivationLayer, GCTargetInfo>(*polymorphic_downcast<ActivationLayerNode *>(node));
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100279 case NodeType::BatchNormalizationLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100280 return detail::create_batch_normalization_layer<GCBatchNormalizationLayer, GCTargetInfo>(*polymorphic_downcast<BatchNormalizationLayerNode *>(node));
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100281 case NodeType::ConvolutionLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100282 return detail::create_convolution_layer<GCConvolutionLayerFunctions, GCTargetInfo>(*polymorphic_downcast<ConvolutionLayerNode *>(node), ctx);
Georgios Pinitase2220552018-07-20 13:23:44 +0100283 case NodeType::ConcatenateLayer:
284 return detail::create_concatenate_layer<GCDepthConcatenateLayer, GCTargetInfo>(*polymorphic_downcast<ConcatenateLayerNode *>(node));
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100285 case NodeType::DepthwiseConvolutionLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100286 return detail::create_depthwise_convolution_layer<GCDepthwiseConvolutionLayerFunctions, GCTargetInfo>(*polymorphic_downcast<DepthwiseConvolutionLayerNode *>(node));
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100287 case NodeType::EltwiseLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100288 return detail::create_eltwise_layer<GCEltwiseFunctions, GCTargetInfo>(*polymorphic_downcast<EltwiseLayerNode *>(node));
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100289 case NodeType::FullyConnectedLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100290 return detail::create_fully_connected_layer<GCFullyConnectedLayer, GCTargetInfo>(*polymorphic_downcast<FullyConnectedLayerNode *>(node), ctx);
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100291 case NodeType::NormalizationLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100292 return detail::create_normalization_layer<GCNormalizationLayer, GCTargetInfo>(*polymorphic_downcast<NormalizationLayerNode *>(node), ctx);
Michele Di Giorgio555d1102018-09-12 13:51:59 +0100293 case NodeType::NormalizePlanarYUVLayer:
294 return detail::create_normalize_planar_yuv_layer<GCNormalizePlanarYUVLayer, GCTargetInfo>(*polymorphic_downcast<NormalizePlanarYUVLayerNode *>(node));
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100295 case NodeType::PoolingLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100296 return detail::create_pooling_layer<GCPoolingLayer, GCTargetInfo>(*polymorphic_downcast<PoolingLayerNode *>(node));
297 case NodeType::ResizeLayer:
298 return detail::create_resize_layer<GCScale, GCTargetInfo>(*polymorphic_downcast<ResizeLayerNode *>(node));
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100299 case NodeType::SoftmaxLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100300 return detail::create_softmax_layer<GCSoftmaxLayer, GCTargetInfo>(*polymorphic_downcast<SoftmaxLayerNode *>(node), ctx);
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100301 default:
302 return nullptr;
303 }
304}
305} // namespace backends
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100306} // namespace graph
Michele Di Giorgio555d1102018-09-12 13:51:59 +0100307} // namespace arm_compute