blob: a78f51cdbd03320e5676b655926e0e70596dde3c [file] [log] [blame]
Georgios Pinitasfbb80542018-03-27 17:15:49 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2020 Arm Limited.
Georgios Pinitasfbb80542018-03-27 17:15:49 +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 */
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"
Gian Marco Iodice5dea19e2019-11-08 12:13:48 +000028#include "arm_compute/graph/GraphContext.h"
Georgios Pinitasda2491f2018-06-01 17:49:09 +010029#include "arm_compute/graph/backends/FunctionHelpers.h"
Georgios Pinitasfbb80542018-03-27 17:15:49 +010030#include "arm_compute/runtime/GLES_COMPUTE/GCFunctions.h"
31
Georgios Pinitasfbb80542018-03-27 17:15:49 +010032using namespace arm_compute::utils::cast;
33
34namespace arm_compute
35{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010036namespace graph
Georgios Pinitasfbb80542018-03-27 17:15:49 +010037{
38namespace backends
39{
Georgios Pinitasda2491f2018-06-01 17:49:09 +010040/** Target specific information structure used to pass information to the layer templates */
41struct GCTargetInfo
Georgios Pinitasfbb80542018-03-27 17:15:49 +010042{
Georgios Pinitasda2491f2018-06-01 17:49:09 +010043 using TensorType = arm_compute::IGCTensor;
44 static Target TargetType;
45};
Georgios Pinitasfbb80542018-03-27 17:15:49 +010046
Georgios Pinitasda2491f2018-06-01 17:49:09 +010047Target GCTargetInfo::TargetType = Target::GC;
Georgios Pinitasfbb80542018-03-27 17:15:49 +010048
Georgios Pinitasda2491f2018-06-01 17:49:09 +010049/** Collection of GC convolution functions */
50struct GCConvolutionLayerFunctions
Georgios Pinitasfbb80542018-03-27 17:15:49 +010051{
Georgios Pinitasda2491f2018-06-01 17:49:09 +010052 using GenericConvolutionLayer = GCConvolutionLayer;
53 using GEMMConvolutionLayer = GCConvolutionLayer;
54 using DirectConvolutionLayer = GCDirectConvolutionLayer;
55};
56
57/** Collection of GC depthwise convolution functions */
58struct GCDepthwiseConvolutionLayerFunctions
59{
60 using DepthwiseConvolutionLayer3x3 = GCDepthwiseConvolutionLayer3x3;
61};
62
63/** Collection of GC element-wise functions */
64struct GCEltwiseFunctions
65{
66 using Addition = GCArithmeticAddition;
67 using Multiplication = GCPixelWiseMultiplication;
68};
69
70namespace detail
71{
72template <>
73std::unique_ptr<IFunction> create_convolution_layer<GCConvolutionLayerFunctions, GCTargetInfo>(ConvolutionLayerNode &node, GraphContext &ctx)
74{
75 validate_node<GCTargetInfo>(node, 3 /* expected inputs */, 1 /* expected outputs */);
Georgios Pinitasfbb80542018-03-27 17:15:49 +010076
77 // Extract IO and info
Georgios Pinitasda2491f2018-06-01 17:49:09 +010078 GCTargetInfo::TensorType *input = get_backing_tensor<GCTargetInfo>(node.input(0));
79 GCTargetInfo::TensorType *weights = get_backing_tensor<GCTargetInfo>(node.input(1));
80 GCTargetInfo::TensorType *biases = get_backing_tensor<GCTargetInfo>(node.input(2));
81 GCTargetInfo::TensorType *output = get_backing_tensor<GCTargetInfo>(node.output(0));
Giorgio Arenabb54e4e2018-04-05 17:20:34 +010082
83 if(is_data_type_quantized_asymmetric(input->info()->data_type()))
84 {
85 biases->info()->set_data_type(DataType::S32);
86 }
87
Georgios Pinitas08346e92018-10-16 19:10:46 +010088 const PadStrideInfo conv_info = node.convolution_info();
89 const ConvolutionMethod conv_algorithm = node.convolution_method();
90 const ActivationLayerInfo fused_act = node.fused_activation();
Georgios Pinitasfbb80542018-03-27 17:15:49 +010091
92 // Create and configure function (we assume that functions have been validated before creation)
Georgios Pinitasda2491f2018-06-01 17:49:09 +010093 std::shared_ptr<IMemoryManager> mm = get_memory_manager(ctx, GCTargetInfo::TargetType);
Georgios Pinitasfbb80542018-03-27 17:15:49 +010094 std::unique_ptr<IFunction> func;
95 std::string func_name;
96
Georgios Pinitase2220552018-07-20 13:23:44 +010097 if(conv_algorithm == ConvolutionMethod::Direct)
Georgios Pinitasfbb80542018-03-27 17:15:49 +010098 {
Georgios Pinitasda2491f2018-06-01 17:49:09 +010099 std::tie(func, func_name) = create_named_function<GCConvolutionLayerFunctions::DirectConvolutionLayer>(
100 std::string("DirectConvolutionLayer"),
Georgios Pinitas08346e92018-10-16 19:10:46 +0100101 input, weights, biases, output, conv_info, fused_act);
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100102 }
103 else
104 {
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100105 std::tie(func, func_name) = create_named_memory_managed_function<GCConvolutionLayerFunctions::GenericConvolutionLayer>(
106 std::string("ConvolutionLayer"), mm,
Georgios Pinitas08346e92018-10-16 19:10:46 +0100107 input, weights, biases, output, conv_info, WeightsInfo(), Size2D(1U, 1U), fused_act);
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100108 }
109
110 // Log info
Pablo Tello32521432018-11-15 14:43:10 +0000111 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated "
112 << node.name()
113 << " Type: " << func_name
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100114 << " Data Type: " << input->info()->data_type()
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100115 << " Input QuantInfo: " << input->info()->quantization_info()
116 << " Weights QuantInfo: " << weights->info()->quantization_info()
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100117 << " Input shape: " << input->info()->tensor_shape()
118 << " Weights shape: " << weights->info()->tensor_shape()
119 << " Output shape: " << output->info()->tensor_shape()
Georgios Pinitas08346e92018-10-16 19:10:46 +0100120 << (fused_act.enabled() ? " " + to_string(fused_act.activation()) : "")
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100121 << std::endl);
122 return func;
123}
124
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100125template <>
126std::unique_ptr<IFunction> create_depthwise_convolution_layer<GCDepthwiseConvolutionLayerFunctions, GCTargetInfo>(DepthwiseConvolutionLayerNode &node)
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100127{
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100128 validate_node<GCTargetInfo>(node, 3 /* expected inputs */, 1 /* expected outputs */);
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100129
130 // Extract IO and info
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100131 GCTargetInfo::TensorType *input = get_backing_tensor<GCTargetInfo>(node.input(0));
132 GCTargetInfo::TensorType *weights = get_backing_tensor<GCTargetInfo>(node.input(1));
133 GCTargetInfo::TensorType *biases = get_backing_tensor<GCTargetInfo>(node.input(2));
134 GCTargetInfo::TensorType *output = get_backing_tensor<GCTargetInfo>(node.output(0));
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100135
136 if(is_data_type_quantized_asymmetric(input->info()->data_type()))
137 {
138 biases->info()->set_data_type(DataType::S32);
139 }
140
Georgios Pinitas60e98252018-10-22 16:17:20 +0100141 const PadStrideInfo conv_info = node.convolution_info();
142 const DepthwiseConvolutionMethod dwc_algorithm = node.depthwise_convolution_method();
Georgios Pinitas60e98252018-10-22 16:17:20 +0100143 const ActivationLayerInfo fused_act = node.fused_activation();
Georgios Pinitas05045c12018-12-07 18:31:47 +0000144 const int depth_multiplier = node.depth_multiplier();
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100145
146 // Create and configure function (we assume that functions have been validated before creation)
147 std::unique_ptr<IFunction> func;
148 std::string func_name;
Georgios Pinitase2220552018-07-20 13:23:44 +0100149 if(dwc_algorithm == DepthwiseConvolutionMethod::Optimized3x3)
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100150 {
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100151 std::tie(func, func_name) = create_named_function<GCDepthwiseConvolutionLayerFunctions::DepthwiseConvolutionLayer3x3>(
152 std::string("DepthwiseConvolutionLayer3x3"),
Georgios Pinitas60e98252018-10-22 16:17:20 +0100153 input, weights, biases, output, conv_info, depth_multiplier, fused_act);
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100154 }
155 else
156 {
157 ARM_COMPUTE_ERROR("Generic DepthwiseConvolutionLayer is not supported in GLES backend");
158 }
159
160 // Log info
Pablo Tello32521432018-11-15 14:43:10 +0000161 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated "
162 << node.name()
163 << " Type: " << func_name
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100164 << " Target " << GCTargetInfo::TargetType
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100165 << " Data Type: " << input->info()->data_type()
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100166 << " Input QuantInfo: " << input->info()->quantization_info()
167 << " Weights QuantInfo: " << weights->info()->quantization_info()
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100168 << " Input shape: " << input->info()->tensor_shape()
169 << " Weights shape: " << weights->info()->tensor_shape()
170 << " Output shape: " << output->info()->tensor_shape()
Georgios Pinitas05045c12018-12-07 18:31:47 +0000171 << " Depth multiplier: " << depth_multiplier
Georgios Pinitas60e98252018-10-22 16:17:20 +0100172 << (fused_act.enabled() ? " " + to_string(fused_act.activation()) : "")
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100173 << std::endl);
174 return func;
175}
176
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100177template <>
178std::unique_ptr<IFunction> create_eltwise_layer<GCEltwiseFunctions, GCTargetInfo>(EltwiseLayerNode &node)
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100179{
180 ARM_COMPUTE_LOG_GRAPH_VERBOSE(
181 "Creating GC EltwiseLayer node with ID : " << node.id() << " and Name: " << node.name() << std::endl);
182 ARM_COMPUTE_ERROR_ON(node.num_inputs() != 2);
183 ARM_COMPUTE_ERROR_ON(node.num_outputs() != 1);
184
185 // Extract IO and info
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100186 GCTargetInfo::TensorType *input1 = get_backing_tensor<GCTargetInfo>(node.input(0));
187 GCTargetInfo::TensorType *input2 = get_backing_tensor<GCTargetInfo>(node.input(1));
188 GCTargetInfo::TensorType *output = get_backing_tensor<GCTargetInfo>(node.output(0));
189 const EltwiseOperation eltwise_op = node.eltwise_operation();
190 const ConvertPolicy convert_policy = node.convert_policy();
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100191 ARM_COMPUTE_ERROR_ON(input1 == nullptr);
192 ARM_COMPUTE_ERROR_ON(input2 == nullptr);
193 ARM_COMPUTE_ERROR_ON(output == nullptr);
194
195 std::unique_ptr<IFunction> func = nullptr;
196 std::string func_name;
Georgios Pinitase2220552018-07-20 13:23:44 +0100197 if(eltwise_op == EltwiseOperation::Add)
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100198 {
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100199 std::tie(func, func_name) = create_named_function<GCEltwiseFunctions::Addition>(
200 std::string("GCArithmeticAddition"),
201 input1, input2, output, convert_policy);
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100202 }
Georgios Pinitase2220552018-07-20 13:23:44 +0100203 else if(eltwise_op == EltwiseOperation::Sub)
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100204 {
205 ARM_COMPUTE_ERROR("Arithmetic subtraction is not supported in GLES backend");
206 }
Georgios Pinitase2220552018-07-20 13:23:44 +0100207 else if(eltwise_op == EltwiseOperation::Mul)
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100208 {
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100209 std::tie(func, func_name) = create_named_function<GCEltwiseFunctions::Multiplication>(
210 std::string("PixelWiseMultiplication"),
211 input1, input2, output, 1.f);
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100212 }
213 else
214 {
215 ARM_COMPUTE_ERROR("Unsupported element-wise operation!");
216 }
217
218 // Log info
Pablo Tello32521432018-11-15 14:43:10 +0000219 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated "
220 << node.name()
221 << " Type: " << node.type()
222 << " Target: " << GCTargetInfo::TargetType
223 << " Operation: " << func_name
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100224 << " Data Type: " << input1->info()->data_type()
Pablo Tello32521432018-11-15 14:43:10 +0000225 << " Shape: " << input1->info()->tensor_shape()
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100226 << std::endl);
227
228 return func;
229}
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100230} //namespace detail
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100231
232std::unique_ptr<IFunction> GCFunctionFactory::create(INode *node, GraphContext &ctx)
233{
234 if(node == nullptr)
235 {
236 return nullptr;
237 }
238
239 NodeType type = node->type();
240 switch(type)
241 {
242 case NodeType::ActivationLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100243 return detail::create_activation_layer<GCActivationLayer, GCTargetInfo>(*polymorphic_downcast<ActivationLayerNode *>(node));
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100244 case NodeType::BatchNormalizationLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100245 return detail::create_batch_normalization_layer<GCBatchNormalizationLayer, GCTargetInfo>(*polymorphic_downcast<BatchNormalizationLayerNode *>(node));
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100246 case NodeType::ConvolutionLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100247 return detail::create_convolution_layer<GCConvolutionLayerFunctions, GCTargetInfo>(*polymorphic_downcast<ConvolutionLayerNode *>(node), ctx);
Georgios Pinitase2220552018-07-20 13:23:44 +0100248 case NodeType::ConcatenateLayer:
Georgios Pinitas09f24972019-05-17 18:14:40 +0100249 return detail::create_concatenate_layer<GCConcatenateLayer, GCTargetInfo>(*polymorphic_downcast<ConcatenateLayerNode *>(node));
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100250 case NodeType::DepthwiseConvolutionLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100251 return detail::create_depthwise_convolution_layer<GCDepthwiseConvolutionLayerFunctions, GCTargetInfo>(*polymorphic_downcast<DepthwiseConvolutionLayerNode *>(node));
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100252 case NodeType::EltwiseLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100253 return detail::create_eltwise_layer<GCEltwiseFunctions, GCTargetInfo>(*polymorphic_downcast<EltwiseLayerNode *>(node));
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100254 case NodeType::FullyConnectedLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100255 return detail::create_fully_connected_layer<GCFullyConnectedLayer, GCTargetInfo>(*polymorphic_downcast<FullyConnectedLayerNode *>(node), ctx);
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100256 case NodeType::NormalizationLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100257 return detail::create_normalization_layer<GCNormalizationLayer, GCTargetInfo>(*polymorphic_downcast<NormalizationLayerNode *>(node), ctx);
Michele Di Giorgio555d1102018-09-12 13:51:59 +0100258 case NodeType::NormalizePlanarYUVLayer:
259 return detail::create_normalize_planar_yuv_layer<GCNormalizePlanarYUVLayer, GCTargetInfo>(*polymorphic_downcast<NormalizePlanarYUVLayerNode *>(node));
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100260 case NodeType::PoolingLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100261 return detail::create_pooling_layer<GCPoolingLayer, GCTargetInfo>(*polymorphic_downcast<PoolingLayerNode *>(node));
Giorgio Arena6e9d0e02020-01-03 15:02:04 +0000262 case NodeType::PrintLayer:
263 return detail::create_print_layer<GCTargetInfo>(*polymorphic_downcast<PrintLayerNode *>(node));
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100264 case NodeType::ResizeLayer:
265 return detail::create_resize_layer<GCScale, GCTargetInfo>(*polymorphic_downcast<ResizeLayerNode *>(node));
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100266 case NodeType::SoftmaxLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100267 return detail::create_softmax_layer<GCSoftmaxLayer, GCTargetInfo>(*polymorphic_downcast<SoftmaxLayerNode *>(node), ctx);
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100268 default:
269 return nullptr;
270 }
271}
272} // namespace backends
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100273} // namespace graph
Michele Di Giorgio555d1102018-09-12 13:51:59 +0100274} // namespace arm_compute