Compute Library
 21.02
NEFunctionFactory.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2020 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  */
25 
35 #include "support/Cast.h"
37 
38 using namespace arm_compute::utils::cast;
39 
40 namespace arm_compute
41 {
42 namespace graph
43 {
44 namespace backends
45 {
46 /** Target specific information structure used to pass information to the layer templates */
47 struct NETargetInfo
48 {
50  using SrcTensorType = const arm_compute::ITensor;
51  using TensorConcreteType = arm_compute::Tensor;
52  static Target TargetType;
53 };
54 
55 Target NETargetInfo::TargetType = Target::NEON;
56 
57 /** Collection of Neon convolution functions */
58 struct NEConvolutionLayerFunctions
59 {
60  using GenericConvolutionLayer = NEConvolutionLayer;
61  using GEMMConvolutionLayer = NEGEMMConvolutionLayer;
62  using DirectConvolutionLayer = NEDirectConvolutionLayer;
63  using WinogradConvolutionLayer = NEWinogradConvolutionLayer;
64 };
65 
66 /** Collection of Neon element-wise functions */
67 struct NEEltwiseFunctions
68 {
69  using Addition = NEArithmeticAddition;
70  using Subtraction = NEArithmeticSubtraction;
71  using Multiplication = NEPixelWiseMultiplication;
72  using Maximum = NEElementwiseMax;
73 };
74 
75 /** Collection of Neon unary element-wise functions */
76 struct NEUnaryEltwiseFunctions
77 {
78  using Exp = NEExpLayer;
79 };
80 
81 /** Function and tensor types to be used inside a Neon fused convolution/batch normalization layer */
82 struct NEFusedLayerTypes
83 {
84  using ConvolutionLayer = NEConvolutionLayer;
85  using DepthwiseConvolutionLayer = NEDepthwiseConvolutionLayer;
86  using FuseBatchNormalization = NEFuseBatchNormalization;
87 };
88 
89 namespace detail
90 {
91 template <>
93 {
94  validate_node<NETargetInfo>(node, 1 /* expected inputs */, 1 /* expected outputs */);
95 
96  // Extract IO and info
97  NETargetInfo::TensorType *input = get_backing_tensor<NETargetInfo>(node.input(0));
98  NETargetInfo::TensorType *output = get_backing_tensor<NETargetInfo>(node.output(0));
99  const NormalizationLayerInfo norm_info = node.normalization_info();
100  ARM_COMPUTE_ERROR_ON(input == nullptr);
101  ARM_COMPUTE_ERROR_ON(output == nullptr);
102 
103  // Create and configure function
104  auto func = std::make_unique<NENormalizationLayer>(get_memory_manager(ctx, NETargetInfo::TargetType));
105  func->configure(input, output, norm_info);
106 
107  // Log info
108  ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated "
109  << node.name()
110  << " Type: " << node.type()
111  << " Target: " << NETargetInfo::TargetType
112  << " Data Type: " << input->info()->data_type()
113  << " Input shape: " << input->info()->tensor_shape()
114  << " Output shape: " << output->info()->tensor_shape()
115  << " Normalization info: " << norm_info.type()
116  << std::endl);
117 
118  return std::move(func);
119 }
120 } // namespace detail
121 
122 std::unique_ptr<IFunction> NEFunctionFactory::create(INode *node, GraphContext &ctx)
123 {
124  if(node == nullptr)
125  {
126  return nullptr;
127  }
128 
129  NodeType type = node->type();
130  switch(type)
131  {
132  case NodeType::ActivationLayer:
133  return detail::create_activation_layer<NEActivationLayer, NETargetInfo>(*polymorphic_downcast<ActivationLayerNode *>(node));
134  case NodeType::ArgMinMaxLayer:
135  return detail::create_arg_min_max_layer<NEArgMinMaxLayer, NETargetInfo>(*polymorphic_downcast<ArgMinMaxLayerNode *>(node));
136  case NodeType::BatchNormalizationLayer:
137  return detail::create_batch_normalization_layer<NEBatchNormalizationLayer, NETargetInfo>(*polymorphic_downcast<BatchNormalizationLayerNode *>(node));
138  case NodeType::ChannelShuffleLayer:
139  return detail::create_channel_shuffle_layer<NEChannelShuffleLayer, NETargetInfo>(*polymorphic_downcast<ChannelShuffleLayerNode *>(node));
140  case NodeType::ConvolutionLayer:
141  return detail::create_convolution_layer<NEConvolutionLayerFunctions, NETargetInfo>(*polymorphic_downcast<ConvolutionLayerNode *>(node), ctx);
142  case NodeType::DepthToSpaceLayer:
143  return detail::create_depth_to_space_layer<NEDepthToSpaceLayer, NETargetInfo>(*polymorphic_downcast<DepthToSpaceLayerNode *>(node));
144  case NodeType::DeconvolutionLayer:
145  return detail::create_deconvolution_layer<NEDeconvolutionLayer, NETargetInfo>(*polymorphic_downcast<DeconvolutionLayerNode *>(node), ctx);
146  case NodeType::ConcatenateLayer:
147  return detail::create_concatenate_layer<NEConcatenateLayer, NETargetInfo>(*polymorphic_downcast<ConcatenateLayerNode *>(node));
148  case NodeType::DepthwiseConvolutionLayer:
149  return detail::create_depthwise_convolution_layer<NEDepthwiseConvolutionLayer, NETargetInfo>(*polymorphic_downcast<DepthwiseConvolutionLayerNode *>(node));
150  case NodeType::DequantizationLayer:
151  return detail::create_dequantization_layer<NEDequantizationLayer, NETargetInfo>(*polymorphic_downcast<DequantizationLayerNode *>(node));
152  case NodeType::DetectionOutputLayer:
153  return detail::create_detection_output_layer<CPPDetectionOutputLayer, NETargetInfo>(*polymorphic_downcast<DetectionOutputLayerNode *>(node));
154  case NodeType::DetectionPostProcessLayer:
155  return detail::create_detection_post_process_layer<NEDetectionPostProcessLayer, NETargetInfo>(*polymorphic_downcast<DetectionPostProcessLayerNode *>(node));
156  case NodeType::EltwiseLayer:
157  return detail::create_eltwise_layer<NEEltwiseFunctions, NETargetInfo>(*polymorphic_downcast<EltwiseLayerNode *>(node));
158  case NodeType::UnaryEltwiseLayer:
159  return detail::create_unary_eltwise_layer<NEUnaryEltwiseFunctions, NETargetInfo>(*polymorphic_downcast<UnaryEltwiseLayerNode *>(node));
160  case NodeType::FlattenLayer:
161  return detail::create_flatten_layer<NEFlattenLayer, NETargetInfo>(*polymorphic_downcast<FlattenLayerNode *>(node));
162  case NodeType::FullyConnectedLayer:
163  return detail::create_fully_connected_layer<NEFullyConnectedLayer, NETargetInfo>(*polymorphic_downcast<FullyConnectedLayerNode *>(node), ctx);
164  case NodeType::FusedConvolutionBatchNormalizationLayer:
165  return detail::create_fused_convolution_batch_normalization_layer<NEFusedLayerTypes, NETargetInfo>(*polymorphic_downcast<FusedConvolutionBatchNormalizationNode *>(node), ctx);
166  case NodeType::FusedDepthwiseConvolutionBatchNormalizationLayer:
167  return detail::create_fused_depthwise_convolution_batch_normalization_layer<NEFusedLayerTypes, NETargetInfo>(*polymorphic_downcast<FusedDepthwiseConvolutionBatchNormalizationNode *>(node), ctx);
168  case NodeType::L2NormalizeLayer:
169  return detail::create_l2_normalize_layer<NEL2NormalizeLayer, NETargetInfo>(*polymorphic_downcast<L2NormalizeLayerNode *>(node), ctx);
170  case NodeType::NormalizationLayer:
171  return detail::create_normalization_layer<NENormalizationLayer, NETargetInfo>(*polymorphic_downcast<NormalizationLayerNode *>(node), ctx);
172  case NodeType::PadLayer:
173  return detail::create_pad_layer<NEPadLayer, NETargetInfo>(*polymorphic_downcast<PadLayerNode *>(node));
174  case NodeType::PermuteLayer:
175  return detail::create_permute_layer<NEPermute, NETargetInfo>(*polymorphic_downcast<PermuteLayerNode *>(node));
176  case NodeType::PoolingLayer:
177  return detail::create_pooling_layer<NEPoolingLayer, NETargetInfo>(*polymorphic_downcast<PoolingLayerNode *>(node));
178  case NodeType::PReluLayer:
179  return detail::create_prelu_layer<NEPReluLayer, NETargetInfo>(*polymorphic_downcast<PReluLayerNode *>(node));
180  case NodeType::PrintLayer:
181  return detail::create_print_layer<NETargetInfo>(*polymorphic_downcast<PrintLayerNode *>(node));
182  case NodeType::PriorBoxLayer:
183  return detail::create_priorbox_layer<NEPriorBoxLayer, NETargetInfo>(*polymorphic_downcast<PriorBoxLayerNode *>(node));
184  case NodeType::QuantizationLayer:
185  return detail::create_quantization_layer<NEQuantizationLayer, NETargetInfo>(*polymorphic_downcast<QuantizationLayerNode *>(node));
186  case NodeType::ReductionOperationLayer:
187  return detail::create_reduction_operation_layer<NEReductionOperation, NETargetInfo>(*polymorphic_downcast<ReductionLayerNode *>(node), ctx);
188  case NodeType::ReorgLayer:
189  return detail::create_reorg_layer<NEReorgLayer, NETargetInfo>(*polymorphic_downcast<ReorgLayerNode *>(node));
190  case NodeType::ReshapeLayer:
191  return detail::create_reshape_layer<NEReshapeLayer, NETargetInfo>(*polymorphic_downcast<ReshapeLayerNode *>(node));
192  case NodeType::ResizeLayer:
193  return detail::create_resize_layer<NEScale, NETargetInfo>(*polymorphic_downcast<ResizeLayerNode *>(node));
194  case NodeType::SliceLayer:
195  return detail::create_slice_layer<NESlice, NETargetInfo>(*polymorphic_downcast<SliceLayerNode *>(node));
196  case NodeType::SoftmaxLayer:
197  return detail::create_softmax_layer<NESoftmaxLayer, NETargetInfo>(*polymorphic_downcast<SoftmaxLayerNode *>(node), ctx);
198  case NodeType::StackLayer:
199  return detail::create_stack_layer<NEStackLayer, NETargetInfo>(*polymorphic_downcast<StackLayerNode *>(node));
200  case NodeType::StridedSliceLayer:
201  return detail::create_strided_slice_layer<NEStridedSlice, NETargetInfo>(*polymorphic_downcast<StridedSliceLayerNode *>(node));
202  default:
203  return nullptr;
204  }
205 }
206 } // namespace backends
207 } // namespace graph
208 } // namespace arm_compute
TensorType
Memory type.
Definition: Types.h:38
Normalization Layer Information class.
Definition: Types.h:1647
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
NEElementwiseUnaryLayer< ElementWiseUnary::EXP > NEExpLayer
decltype(strategy::transforms) typedef type
Interface for Neon tensor.
Definition: ITensor.h:36
Includes all the Neon functions at once.
#define ARM_COMPUTE_LOG_GRAPH_INFO(x)
Definition: Logger.h:54
Copyright (c) 2017-2021 Arm Limited.
std::unique_ptr< IFunction > create_normalization_layer< NENormalizationLayer, NETargetInfo >(NormalizationLayerNode &node, GraphContext &ctx)
Node interface.
Definition: INode.h:45
Basic implementation of the tensor interface.
Definition: Tensor.h:37
NodeType
Supported nodes.
Definition: Types.h:142
FloorUKernelPtr func
std::shared_ptr< IMemoryManager > get_memory_manager(GraphContext &ctx, Target target)
Returns the memory manager for a given target.
Definition: Utils.h:89
virtual NodeType type() const =0
Returns node&#39;s type.