Compute Library
 21.08
NEFunctionFactory.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2021 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 CPU 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 CPU element-wise functions */
67 struct NEEltwiseFunctions
68 {
69  using Addition = NEArithmeticAddition;
70  using Subtraction = NEArithmeticSubtraction;
71  using Multiplication = NEPixelWiseMultiplication;
72  using Maximum = NEElementwiseMax;
73  using Division = NEElementwiseDivision;
74 };
75 
76 /** Collection of CPU unary element-wise functions */
77 struct NEUnaryEltwiseFunctions
78 {
79  using Exp = NEExpLayer;
80 };
81 
82 /** Function and tensor types to be used inside a fused convolution/batch normalization layer */
83 struct NEFusedLayerTypes
84 {
85  using ConvolutionLayer = NEConvolutionLayer;
86  using DepthwiseConvolutionLayer = NEDepthwiseConvolutionLayer;
87  using FuseBatchNormalization = NEFuseBatchNormalization;
88 };
89 
90 namespace detail
91 {
92 template <>
94 {
95  validate_node<NETargetInfo>(node, 1 /* expected inputs */, 1 /* expected outputs */);
96 
97  // Extract IO and info
98  NETargetInfo::TensorType *input = get_backing_tensor<NETargetInfo>(node.input(0));
99  NETargetInfo::TensorType *output = get_backing_tensor<NETargetInfo>(node.output(0));
100  const NormalizationLayerInfo norm_info = node.normalization_info();
101  ARM_COMPUTE_ERROR_ON(input == nullptr);
102  ARM_COMPUTE_ERROR_ON(output == nullptr);
103 
104  // Create and configure function
105  auto func = std::make_unique<NENormalizationLayer>(get_memory_manager(ctx, NETargetInfo::TargetType));
106  func->configure(input, output, norm_info);
107 
108  // Log info
109  ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated "
110  << node.name()
111  << " Type: " << node.type()
112  << " Target: " << NETargetInfo::TargetType
113  << " Data Type: " << input->info()->data_type()
114  << " Input shape: " << input->info()->tensor_shape()
115  << " Output shape: " << output->info()->tensor_shape()
116  << " Normalization info: " << norm_info.type()
117  << std::endl);
118 
119  return std::move(func);
120 }
121 } // namespace detail
122 
123 std::unique_ptr<IFunction> NEFunctionFactory::create(INode *node, GraphContext &ctx)
124 {
125  if(node == nullptr)
126  {
127  return nullptr;
128  }
129 
130  NodeType type = node->type();
131  switch(type)
132  {
133  case NodeType::ActivationLayer:
134  return detail::create_activation_layer<NEActivationLayer, NETargetInfo>(*polymorphic_downcast<ActivationLayerNode *>(node));
135  case NodeType::ArgMinMaxLayer:
136  return detail::create_arg_min_max_layer<NEArgMinMaxLayer, NETargetInfo>(*polymorphic_downcast<ArgMinMaxLayerNode *>(node));
137  case NodeType::BatchNormalizationLayer:
138  return detail::create_batch_normalization_layer<NEBatchNormalizationLayer, NETargetInfo>(*polymorphic_downcast<BatchNormalizationLayerNode *>(node));
139  case NodeType::ChannelShuffleLayer:
140  return detail::create_channel_shuffle_layer<NEChannelShuffleLayer, NETargetInfo>(*polymorphic_downcast<ChannelShuffleLayerNode *>(node));
141  case NodeType::ConvolutionLayer:
142  return detail::create_convolution_layer<NEConvolutionLayerFunctions, NETargetInfo>(*polymorphic_downcast<ConvolutionLayerNode *>(node), ctx);
143  case NodeType::DepthToSpaceLayer:
144  return detail::create_depth_to_space_layer<NEDepthToSpaceLayer, NETargetInfo>(*polymorphic_downcast<DepthToSpaceLayerNode *>(node));
145  case NodeType::DeconvolutionLayer:
146  return detail::create_deconvolution_layer<NEDeconvolutionLayer, NETargetInfo>(*polymorphic_downcast<DeconvolutionLayerNode *>(node), ctx);
147  case NodeType::ConcatenateLayer:
148  return detail::create_concatenate_layer<NEConcatenateLayer, NETargetInfo>(*polymorphic_downcast<ConcatenateLayerNode *>(node));
149  case NodeType::DepthwiseConvolutionLayer:
150  return detail::create_depthwise_convolution_layer<NEDepthwiseConvolutionLayer, NETargetInfo>(*polymorphic_downcast<DepthwiseConvolutionLayerNode *>(node));
151  case NodeType::DequantizationLayer:
152  return detail::create_dequantization_layer<NEDequantizationLayer, NETargetInfo>(*polymorphic_downcast<DequantizationLayerNode *>(node));
153  case NodeType::DetectionOutputLayer:
154  return detail::create_detection_output_layer<CPPDetectionOutputLayer, NETargetInfo>(*polymorphic_downcast<DetectionOutputLayerNode *>(node));
155  case NodeType::DetectionPostProcessLayer:
156  return detail::create_detection_post_process_layer<NEDetectionPostProcessLayer, NETargetInfo>(*polymorphic_downcast<DetectionPostProcessLayerNode *>(node));
157  case NodeType::EltwiseLayer:
158  return detail::create_eltwise_layer<NEEltwiseFunctions, NETargetInfo>(*polymorphic_downcast<EltwiseLayerNode *>(node));
159  case NodeType::UnaryEltwiseLayer:
160  return detail::create_unary_eltwise_layer<NEUnaryEltwiseFunctions, NETargetInfo>(*polymorphic_downcast<UnaryEltwiseLayerNode *>(node));
161  case NodeType::FlattenLayer:
162  return detail::create_flatten_layer<NEFlattenLayer, NETargetInfo>(*polymorphic_downcast<FlattenLayerNode *>(node));
163  case NodeType::FullyConnectedLayer:
164  return detail::create_fully_connected_layer<NEFullyConnectedLayer, NETargetInfo>(*polymorphic_downcast<FullyConnectedLayerNode *>(node), ctx);
165  case NodeType::FusedConvolutionBatchNormalizationLayer:
166  return detail::create_fused_convolution_batch_normalization_layer<NEFusedLayerTypes, NETargetInfo>(*polymorphic_downcast<FusedConvolutionBatchNormalizationNode *>(node), ctx);
167  case NodeType::FusedDepthwiseConvolutionBatchNormalizationLayer:
168  return detail::create_fused_depthwise_convolution_batch_normalization_layer<NEFusedLayerTypes, NETargetInfo>(*polymorphic_downcast<FusedDepthwiseConvolutionBatchNormalizationNode *>(node), ctx);
169  case NodeType::L2NormalizeLayer:
170  return detail::create_l2_normalize_layer<NEL2NormalizeLayer, NETargetInfo>(*polymorphic_downcast<L2NormalizeLayerNode *>(node), ctx);
171  case NodeType::NormalizationLayer:
172  return detail::create_normalization_layer<NENormalizationLayer, NETargetInfo>(*polymorphic_downcast<NormalizationLayerNode *>(node), ctx);
173  case NodeType::PadLayer:
174  return detail::create_pad_layer<NEPadLayer, NETargetInfo>(*polymorphic_downcast<PadLayerNode *>(node));
175  case NodeType::PermuteLayer:
176  return detail::create_permute_layer<NEPermute, NETargetInfo>(*polymorphic_downcast<PermuteLayerNode *>(node));
177  case NodeType::PoolingLayer:
178  return detail::create_pooling_layer<NEPoolingLayer, NETargetInfo>(*polymorphic_downcast<PoolingLayerNode *>(node));
179  case NodeType::PReluLayer:
180  return detail::create_prelu_layer<NEPReluLayer, NETargetInfo>(*polymorphic_downcast<PReluLayerNode *>(node));
181  case NodeType::PrintLayer:
182  return detail::create_print_layer<NETargetInfo>(*polymorphic_downcast<PrintLayerNode *>(node));
183  case NodeType::PriorBoxLayer:
184  return detail::create_priorbox_layer<NEPriorBoxLayer, NETargetInfo>(*polymorphic_downcast<PriorBoxLayerNode *>(node));
185  case NodeType::QuantizationLayer:
186  return detail::create_quantization_layer<NEQuantizationLayer, NETargetInfo>(*polymorphic_downcast<QuantizationLayerNode *>(node));
187  case NodeType::ReductionOperationLayer:
188  return detail::create_reduction_operation_layer<NEReductionOperation, NETargetInfo>(*polymorphic_downcast<ReductionLayerNode *>(node), ctx);
189  case NodeType::ReorgLayer:
190  return detail::create_reorg_layer<NEReorgLayer, NETargetInfo>(*polymorphic_downcast<ReorgLayerNode *>(node));
191  case NodeType::ReshapeLayer:
192  return detail::create_reshape_layer<NEReshapeLayer, NETargetInfo>(*polymorphic_downcast<ReshapeLayerNode *>(node));
193  case NodeType::ResizeLayer:
194  return detail::create_resize_layer<NEScale, NETargetInfo>(*polymorphic_downcast<ResizeLayerNode *>(node));
195  case NodeType::SliceLayer:
196  return detail::create_slice_layer<NESlice, NETargetInfo>(*polymorphic_downcast<SliceLayerNode *>(node));
197  case NodeType::SoftmaxLayer:
198  return detail::create_softmax_layer<NESoftmaxLayer, NETargetInfo>(*polymorphic_downcast<SoftmaxLayerNode *>(node), ctx);
199  case NodeType::StackLayer:
200  return detail::create_stack_layer<NEStackLayer, NETargetInfo>(*polymorphic_downcast<StackLayerNode *>(node));
201  case NodeType::StridedSliceLayer:
202  return detail::create_strided_slice_layer<NEStridedSlice, NETargetInfo>(*polymorphic_downcast<StridedSliceLayerNode *>(node));
203  default:
204  return nullptr;
205  }
206 }
207 } // namespace backends
208 } // namespace graph
209 } // namespace arm_compute
TensorType
Memory type.
Definition: Types.h:38
Normalization Layer Information class.
Definition: Types.h:1576
#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 CPU tensor.
Definition: ITensor.h:36
Includes all the Arm® 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:149
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.