Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 1 | /* |
Giuseppe Rossini | bb365de | 2019-02-15 10:24:47 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2019 ARM Limited. |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 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 Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 24 | #include "arm_compute/graph/backends/CL/CLFunctionFactory.h" |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/utils/misc/Cast.h" |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 27 | #include "arm_compute/graph/Graph.h" |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 28 | #include "arm_compute/graph/backends/FunctionHelpers.h" |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 29 | #include "arm_compute/runtime/CL/CLFunctions.h" |
Isabella Gottardi | 7234ed8 | 2018-11-27 08:51:10 +0000 | [diff] [blame] | 30 | #include "arm_compute/runtime/CPP/CPPFunctions.h" |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 31 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 32 | using namespace arm_compute::utils::cast; |
| 33 | |
| 34 | namespace arm_compute |
| 35 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 36 | namespace graph |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 37 | { |
| 38 | namespace backends |
| 39 | { |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 40 | /** Target specific information structure used to pass information to the layer templates */ |
| 41 | struct CLTargetInfo |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 42 | { |
giuros01 | acce504 | 2019-02-21 17:32:34 +0000 | [diff] [blame] | 43 | using TensorType = arm_compute::ICLTensor; |
| 44 | using TensorConcreteType = CLTensor; |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 45 | static Target TargetType; |
| 46 | }; |
| 47 | |
| 48 | Target CLTargetInfo::TargetType = Target::CL; |
| 49 | |
| 50 | /** Collection of CL convolution functions */ |
| 51 | struct CLConvolutionLayerFunctions |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 52 | { |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 53 | using GenericConvolutionLayer = CLConvolutionLayer; |
| 54 | using GEMMConvolutionLayer = CLGEMMConvolutionLayer; |
| 55 | using DirectConvolutionLayer = CLDirectConvolutionLayer; |
| 56 | using WinogradConvolutionLayer = CLWinogradConvolutionLayer; |
| 57 | }; |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 58 | |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 59 | /** Collection of CL depthwise convolution functions */ |
| 60 | struct CLDepthwiseConvolutionLayerFunctions |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 61 | { |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 62 | using GenericDepthwiseConvolutionLayer = CLDepthwiseConvolutionLayer; |
| 63 | using DepthwiseConvolutionLayer3x3 = CLDepthwiseConvolutionLayer3x3; |
| 64 | }; |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 65 | |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 66 | /** Collection of CL element-wise functions */ |
| 67 | struct CLEltwiseFunctions |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 68 | { |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 69 | using Addition = CLArithmeticAddition; |
| 70 | using Subtraction = CLArithmeticSubtraction; |
| 71 | using Multiplication = CLPixelWiseMultiplication; |
| 72 | }; |
giuros01 | acce504 | 2019-02-21 17:32:34 +0000 | [diff] [blame] | 73 | |
| 74 | /** Function and tensor types to be used inside a CL fused convolution/batch normalization layer */ |
| 75 | struct CLFusedLayerTypes |
| 76 | { |
| 77 | using ConvolutionLayer = CLConvolutionLayer; |
| 78 | using FuseBatchNormalization = CLFuseBatchNormalization; |
| 79 | }; |
| 80 | |
Isabella Gottardi | 7234ed8 | 2018-11-27 08:51:10 +0000 | [diff] [blame] | 81 | // TODO (isagot01): Remove once we support heterogeneous scheduling at function level |
| 82 | /** Wrapper for the CPP Function in the OpenCL backend **/ |
| 83 | class CPPWrapperFunction : public IFunction |
| 84 | { |
| 85 | public: |
| 86 | /* Default constructor */ |
| 87 | CPPWrapperFunction() |
| 88 | : _tensors(), _func(nullptr) |
| 89 | { |
| 90 | } |
| 91 | |
| 92 | void run() override |
| 93 | { |
| 94 | for(auto &tensor : _tensors) |
| 95 | { |
| 96 | tensor->map(CLScheduler::get().queue()); |
| 97 | } |
| 98 | _func->run(); |
| 99 | |
| 100 | for(auto &tensor : _tensors) |
| 101 | { |
| 102 | tensor->unmap(CLScheduler::get().queue()); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | void register_tensor(ICLTensor *tensor) |
| 107 | { |
| 108 | _tensors.push_back(tensor); |
| 109 | } |
| 110 | |
| 111 | void register_function(std::unique_ptr<IFunction> function) |
| 112 | { |
| 113 | _func = std::move(function); |
| 114 | } |
| 115 | |
| 116 | private: |
| 117 | std::vector<arm_compute::ICLTensor *> _tensors; |
| 118 | std::unique_ptr<IFunction> _func; |
| 119 | }; |
| 120 | |
| 121 | namespace detail |
| 122 | { |
| 123 | // Specialized functions |
| 124 | template <> |
| 125 | std::unique_ptr<IFunction> create_detection_output_layer<CPPDetectionOutputLayer, CLTargetInfo>(DetectionOutputLayerNode &node) |
| 126 | { |
| 127 | validate_node<CLTargetInfo>(node, 3 /* expected inputs */, 1 /* expected outputs */); |
| 128 | |
| 129 | // Extract IO and info |
| 130 | CLTargetInfo::TensorType *input0 = get_backing_tensor<CLTargetInfo>(node.input(0)); |
| 131 | CLTargetInfo::TensorType *input1 = get_backing_tensor<CLTargetInfo>(node.input(1)); |
| 132 | CLTargetInfo::TensorType *input2 = get_backing_tensor<CLTargetInfo>(node.input(2)); |
| 133 | CLTargetInfo::TensorType *output = get_backing_tensor<CLTargetInfo>(node.output(0)); |
| 134 | const DetectionOutputLayerInfo detect_info = node.detection_output_info(); |
| 135 | |
| 136 | ARM_COMPUTE_ERROR_ON(input0 == nullptr); |
| 137 | ARM_COMPUTE_ERROR_ON(input1 == nullptr); |
| 138 | ARM_COMPUTE_ERROR_ON(input2 == nullptr); |
| 139 | ARM_COMPUTE_ERROR_ON(output == nullptr); |
| 140 | |
| 141 | // Create and configure function |
| 142 | auto func = support::cpp14::make_unique<CPPDetectionOutputLayer>(); |
| 143 | func->configure(input0, input1, input2, output, detect_info); |
| 144 | |
| 145 | // Log info |
| 146 | ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " |
| 147 | << node.name() |
| 148 | << " Type: " << node.type() |
| 149 | << " Target: " << CLTargetInfo::TargetType |
| 150 | << " Data Type: " << input0->info()->data_type() |
| 151 | << " Input0 shape: " << input0->info()->tensor_shape() |
| 152 | << " Input1 shape: " << input1->info()->tensor_shape() |
| 153 | << " Input2 shape: " << input2->info()->tensor_shape() |
| 154 | << " Output shape: " << output->info()->tensor_shape() |
| 155 | << " DetectionOutputLayer info: " << detect_info |
| 156 | << std::endl); |
| 157 | |
| 158 | auto wrap_function = support::cpp14::make_unique<CPPWrapperFunction>(); |
| 159 | ; |
| 160 | wrap_function->register_function(std::move(func)); |
| 161 | wrap_function->register_tensor(input0); |
| 162 | wrap_function->register_tensor(input1); |
| 163 | wrap_function->register_tensor(input2); |
| 164 | wrap_function->register_tensor(output); |
| 165 | |
| 166 | return std::move(wrap_function); |
| 167 | } |
| 168 | } // namespace detail |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 169 | |
| 170 | std::unique_ptr<IFunction> CLFunctionFactory::create(INode *node, GraphContext &ctx) |
| 171 | { |
| 172 | if(node == nullptr) |
| 173 | { |
| 174 | return nullptr; |
| 175 | } |
| 176 | |
| 177 | NodeType type = node->type(); |
| 178 | switch(type) |
| 179 | { |
| 180 | case NodeType::ActivationLayer: |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 181 | return detail::create_activation_layer<CLActivationLayer, CLTargetInfo>(*polymorphic_downcast<ActivationLayerNode *>(node)); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 182 | case NodeType::BatchNormalizationLayer: |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 183 | return detail::create_batch_normalization_layer<CLBatchNormalizationLayer, CLTargetInfo>(*polymorphic_downcast<BatchNormalizationLayerNode *>(node)); |
Manuel Bottini | d2048ce | 2018-10-23 17:00:42 +0100 | [diff] [blame] | 184 | case NodeType::BoundingBoxTransformLayer: |
| 185 | return detail::create_bounding_box_transform_layer<CLBoundingBoxTransform, CLTargetInfo>(*polymorphic_downcast<BoundingBoxTransformLayerNode *>(node)); |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 186 | case NodeType::ChannelShuffleLayer: |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 187 | return detail::create_channel_shuffle_layer<CLChannelShuffleLayer, CLTargetInfo>(*polymorphic_downcast<ChannelShuffleLayerNode *>(node)); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 188 | case NodeType::ConvolutionLayer: |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 189 | return detail::create_convolution_layer<CLConvolutionLayerFunctions, CLTargetInfo>(*polymorphic_downcast<ConvolutionLayerNode *>(node), ctx); |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 190 | case NodeType::DeconvolutionLayer: |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 191 | return detail::create_deconvolution_layer<CLDeconvolutionLayer, CLTargetInfo>(*polymorphic_downcast<DeconvolutionLayerNode *>(node), ctx); |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 192 | case NodeType::ConcatenateLayer: |
| 193 | return detail::create_concatenate_layer<CLConcatenateLayer, CLTargetInfo>(*polymorphic_downcast<ConcatenateLayerNode *>(node)); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 194 | case NodeType::DepthwiseConvolutionLayer: |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 195 | return detail::create_depthwise_convolution_layer<CLDepthwiseConvolutionLayerFunctions, CLTargetInfo>(*polymorphic_downcast<DepthwiseConvolutionLayerNode *>(node)); |
Isabella Gottardi | 7234ed8 | 2018-11-27 08:51:10 +0000 | [diff] [blame] | 196 | case NodeType::DetectionOutputLayer: |
| 197 | return detail::create_detection_output_layer<CPPDetectionOutputLayer, CLTargetInfo>(*polymorphic_downcast<DetectionOutputLayerNode *>(node)); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 198 | case NodeType::EltwiseLayer: |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 199 | return detail::create_eltwise_layer<CLEltwiseFunctions, CLTargetInfo>(*polymorphic_downcast<EltwiseLayerNode *>(node)); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 200 | case NodeType::FlattenLayer: |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 201 | return detail::create_flatten_layer<CLFlattenLayer, CLTargetInfo>(*polymorphic_downcast<FlattenLayerNode *>(node)); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 202 | case NodeType::FullyConnectedLayer: |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 203 | return detail::create_fully_connected_layer<CLFullyConnectedLayer, CLTargetInfo>(*polymorphic_downcast<FullyConnectedLayerNode *>(node), ctx); |
giuros01 | acce504 | 2019-02-21 17:32:34 +0000 | [diff] [blame] | 204 | case NodeType::FusedConvolutionBatchNormalizationLayer: |
| 205 | return detail::create_fused_convolution_batch_normalization_layer<CLFusedLayerTypes, CLTargetInfo>(*polymorphic_downcast<FusedConvolutionBatchNormalizationNode *>(node)); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 206 | case NodeType::GenerateProposalsLayer: |
| 207 | return detail::create_generate_proposals_layer<CLGenerateProposalsLayer, CLTargetInfo>(*polymorphic_downcast<GenerateProposalsLayerNode *>(node), ctx); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 208 | case NodeType::NormalizationLayer: |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 209 | return detail::create_normalization_layer<CLNormalizationLayer, CLTargetInfo>(*polymorphic_downcast<NormalizationLayerNode *>(node), ctx); |
Michele Di Giorgio | 555d110 | 2018-09-12 13:51:59 +0100 | [diff] [blame] | 210 | case NodeType::NormalizePlanarYUVLayer: |
| 211 | return detail::create_normalize_planar_yuv_layer<CLNormalizePlanarYUVLayer, CLTargetInfo>(*polymorphic_downcast<NormalizePlanarYUVLayerNode *>(node)); |
Michele Di Giorgio | 4bb1733 | 2018-09-26 13:56:51 +0100 | [diff] [blame] | 212 | case NodeType::PadLayer: |
| 213 | return detail::create_pad_layer<CLPadLayer, CLTargetInfo>(*polymorphic_downcast<PadLayerNode *>(node)); |
Georgios Pinitas | 57c4824 | 2018-08-02 13:41:49 +0100 | [diff] [blame] | 214 | case NodeType::PermuteLayer: |
| 215 | return detail::create_permute_layer<CLPermute, CLTargetInfo>(*polymorphic_downcast<PermuteLayerNode *>(node)); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 216 | case NodeType::PoolingLayer: |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 217 | return detail::create_pooling_layer<CLPoolingLayer, CLTargetInfo>(*polymorphic_downcast<PoolingLayerNode *>(node)); |
Pablo Tello | 3252143 | 2018-11-15 14:43:10 +0000 | [diff] [blame] | 218 | case NodeType::PriorBoxLayer: |
| 219 | return detail::create_priorbox_layer<CLPriorBoxLayer, CLTargetInfo>(*polymorphic_downcast<PriorBoxLayerNode *>(node)); |
Gian Marco Iodice | 23e2479 | 2018-09-07 15:32:14 +0100 | [diff] [blame] | 220 | case NodeType::ReorgLayer: |
| 221 | return detail::create_reorg_layer<CLReorgLayer, CLTargetInfo>(*polymorphic_downcast<ReorgLayerNode *>(node)); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 222 | case NodeType::ReshapeLayer: |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 223 | return detail::create_reshape_layer<CLReshapeLayer, CLTargetInfo>(*polymorphic_downcast<ReshapeLayerNode *>(node)); |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 224 | case NodeType::ResizeLayer: |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 225 | return detail::create_resize_layer<CLScale, CLTargetInfo>(*polymorphic_downcast<ResizeLayerNode *>(node)); |
Manuel Bottini | 3f9d4d7 | 2018-10-19 14:04:42 +0100 | [diff] [blame] | 226 | case NodeType::ROIAlignLayer: |
| 227 | return detail::create_roi_align_layer<CLROIAlignLayer, CLTargetInfo>(*polymorphic_downcast<ROIAlignLayerNode *>(node)); |
Michele Di Giorgio | c30b668 | 2018-09-12 17:44:08 +0100 | [diff] [blame] | 228 | case NodeType::SliceLayer: |
| 229 | return detail::create_slice_layer<CLSlice, CLTargetInfo>(*polymorphic_downcast<SliceLayerNode *>(node)); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 230 | case NodeType::SoftmaxLayer: |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 231 | return detail::create_softmax_layer<CLSoftmaxLayer, CLTargetInfo>(*polymorphic_downcast<SoftmaxLayerNode *>(node), ctx); |
Michele Di Giorgio | ec69975 | 2019-03-22 15:25:32 +0000 | [diff] [blame^] | 232 | case NodeType::StackLayer: |
| 233 | return detail::create_stack_layer<CLStackLayer, CLTargetInfo>(*polymorphic_downcast<StackLayerNode *>(node)); |
Michalis Spyrou | 4e1c3f3 | 2018-09-20 17:14:03 +0100 | [diff] [blame] | 234 | case NodeType::UpsampleLayer: |
| 235 | return detail::create_upsample_layer<CLUpsampleLayer, CLTargetInfo>(*polymorphic_downcast<UpsampleLayerNode *>(node), ctx); |
Michalis Spyrou | 96f6769 | 2018-09-13 11:39:28 +0100 | [diff] [blame] | 236 | case NodeType::YOLOLayer: |
| 237 | return detail::create_yolo_layer<CLYOLOLayer, CLTargetInfo>(*polymorphic_downcast<YOLOLayerNode *>(node), ctx); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 238 | default: |
| 239 | return nullptr; |
| 240 | } |
| 241 | } |
| 242 | } // namespace backends |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 243 | } // namespace graph |
Michele Di Giorgio | 555d110 | 2018-09-12 13:51:59 +0100 | [diff] [blame] | 244 | } // namespace arm_compute |