diff options
author | Georgios Pinitas <georgios.pinitas@arm.com> | 2018-12-07 18:31:47 +0000 |
---|---|---|
committer | Georgios Pinitas <georgios.pinitas@arm.com> | 2018-12-13 10:42:12 +0000 |
commit | 05045c1e052dbba4e44bf0bb8ead3e9b5220d04e (patch) | |
tree | e17a64e9cd0f0927bd75f540b6aeb55ba24953d4 | |
parent | 35767bc09f21050a9767a91b086b327afc928a81 (diff) | |
download | ComputeLibrary-05045c1e052dbba4e44bf0bb8ead3e9b5220d04e.tar.gz |
COMPMID-1071: (3RDPARTY_UPDATE) Add depth multiplier on DepthwiseConv 3x3 NHWC
Change-Id: I316ff40dda379d4b84fac5d63f0c56efbacbc2b4
Reviewed-on: https://review.mlplatform.org/371
Tested-by: Arm Jenkins <bsgcomp@arm.com>
Reviewed-by: Michele Di Giorgio <michele.digiorgio@arm.com>
m--------- | 3rdparty | 0 | ||||
-rw-r--r-- | arm_compute/graph/GraphBuilder.h | 3 | ||||
-rw-r--r-- | arm_compute/graph/backends/FunctionHelpers.h | 3 | ||||
-rw-r--r-- | arm_compute/graph/backends/ValidateHelpers.h | 9 | ||||
-rw-r--r-- | arm_compute/graph/frontend/Layers.h | 20 | ||||
-rw-r--r-- | arm_compute/graph/nodes/DepthwiseConvolutionLayerNode.h | 19 | ||||
-rw-r--r-- | arm_compute/runtime/CL/functions/CLDepthwiseConvolutionLayer.h | 25 | ||||
-rw-r--r-- | arm_compute/runtime/CL/functions/CLPermute.h | 2 | ||||
-rw-r--r-- | examples/graph_mobilenet.cpp | 2 | ||||
-rw-r--r-- | examples/graph_mobilenet_v2.cpp | 4 | ||||
-rw-r--r-- | src/graph/GraphBuilder.cpp | 10 | ||||
-rw-r--r-- | src/graph/backends/GLES/GCFunctionsFactory.cpp | 3 | ||||
-rw-r--r-- | src/graph/nodes/DepthwiseConvolutionLayerNode.cpp | 24 | ||||
-rw-r--r-- | src/runtime/CL/functions/CLDepthwiseConvolutionLayer.cpp | 114 | ||||
-rw-r--r-- | tests/validation/CL/DepthwiseConvolutionLayer.cpp | 12 |
15 files changed, 198 insertions, 52 deletions
diff --git a/3rdparty b/3rdparty -Subproject 75a851597b1a6f53725b04858cdfad10fdac783 +Subproject 4c420dacce49edb8cf7cbc21fdbc5de533f0460 diff --git a/arm_compute/graph/GraphBuilder.h b/arm_compute/graph/GraphBuilder.h index 57ce349984..33a13f1836 100644 --- a/arm_compute/graph/GraphBuilder.h +++ b/arm_compute/graph/GraphBuilder.h @@ -178,6 +178,7 @@ public: * @param[in] input Input to the depthwise convolution layer node as a NodeID-Index pair * @param[in] kernel_spatial_extend Spatial extend of convolution kernels * @param[in] conv_info Convolution layer information + * @param[in] depth_multiplier (Optional) Depth multiplier parameter. * @param[in] method (Optional) Convolution method to use * @param[in] weights_accessor (Optional) Accessor of the weights node data * @param[in] bias_accessor (Optional) Accessor of the bias node data @@ -186,7 +187,7 @@ public: * @return Node ID of the created node, EmptyNodeID in case of error */ static NodeID add_depthwise_convolution_node(Graph &g, NodeParams params, NodeIdxPair input, - Size2D kernel_spatial_extend, PadStrideInfo conv_info, + Size2D kernel_spatial_extend, PadStrideInfo conv_info, int depth_multiplier = 1, DepthwiseConvolutionMethod method = DepthwiseConvolutionMethod::Default, ITensorAccessorUPtr weights_accessor = nullptr, ITensorAccessorUPtr bias_accessor = nullptr, const QuantizationInfo quant_info = QuantizationInfo()); /** Adds an element-wise layer node to the graph diff --git a/arm_compute/graph/backends/FunctionHelpers.h b/arm_compute/graph/backends/FunctionHelpers.h index 0d7210f7f8..3e71e3922a 100644 --- a/arm_compute/graph/backends/FunctionHelpers.h +++ b/arm_compute/graph/backends/FunctionHelpers.h @@ -447,7 +447,7 @@ std::unique_ptr<IFunction> create_depthwise_convolution_layer(DepthwiseConvoluti const PadStrideInfo conv_info = node.convolution_info(); const DepthwiseConvolutionMethod dwc_algorithm = node.depthwise_convolution_method(); - const unsigned int depth_multiplier = 1; + const unsigned int depth_multiplier = node.depth_multiplier(); const ActivationLayerInfo fused_act = node.fused_activation(); // Create and configure function (we assume that functions have been validated before creation) @@ -483,6 +483,7 @@ std::unique_ptr<IFunction> create_depthwise_convolution_layer(DepthwiseConvoluti << " Input shape: " << input->info()->tensor_shape() << " Weights shape: " << weights->info()->tensor_shape() << " Output shape: " << output->info()->tensor_shape() + << " Depth multiplier: " << depth_multiplier << (fused_act.enabled() ? " " + to_string(fused_act.activation()) : "") << std::endl); return func; diff --git a/arm_compute/graph/backends/ValidateHelpers.h b/arm_compute/graph/backends/ValidateHelpers.h index a6864c2286..75e2363f82 100644 --- a/arm_compute/graph/backends/ValidateHelpers.h +++ b/arm_compute/graph/backends/ValidateHelpers.h @@ -182,8 +182,9 @@ Status validate_depthwise_convolution_layer(DepthwiseConvolutionLayerNode &node) arm_compute::ITensorInfo *biases = get_backing_tensor_info(node.input(2)); arm_compute::ITensorInfo *output = get_backing_tensor_info(node.output(0)); - const PadStrideInfo conv_info = node.convolution_info(); - const DepthwiseConvolutionMethod dwc_algorithm = node.depthwise_convolution_method(); + const PadStrideInfo conv_info = node.convolution_info(); + const DepthwiseConvolutionMethod dwc_algorithm = node.depthwise_convolution_method(); + const int depth_multiplier = node.depth_multiplier(); // Validate function Status status{}; @@ -191,10 +192,10 @@ Status validate_depthwise_convolution_layer(DepthwiseConvolutionLayerNode &node) { case DepthwiseConvolutionMethod::Default: case DepthwiseConvolutionMethod::GEMV: - status = DepthwiseConvolutionLayer::validate(input, weights, biases, output, conv_info); + status = DepthwiseConvolutionLayer::validate(input, weights, biases, output, conv_info, depth_multiplier); break; case DepthwiseConvolutionMethod::Optimized3x3: - status = DepthwiseConvolutionLayer3x3::validate(input, weights, biases, output, conv_info); + status = DepthwiseConvolutionLayer3x3::validate(input, weights, biases, output, conv_info, depth_multiplier); break; default: ARM_COMPUTE_RETURN_ERROR_MSG("Unsupported depthwise convolution method"); diff --git a/arm_compute/graph/frontend/Layers.h b/arm_compute/graph/frontend/Layers.h index 78a3f20f1f..d0703317cd 100644 --- a/arm_compute/graph/frontend/Layers.h +++ b/arm_compute/graph/frontend/Layers.h @@ -414,24 +414,27 @@ class DepthwiseConvolutionLayer final : public ILayer public: /** Construct a depthwise convolution layer. * - * @param[in] conv_width Convolution width. - * @param[in] conv_height Convolution height. - * @param[in] weights Accessor to get kernel weights from. - * @param[in] bias Accessor to get kernel bias from. - * @param[in] conv_info Padding and stride information. - * @param[in] quant_info (Optional) Quantization info used for weights + * @param[in] conv_width Convolution width. + * @param[in] conv_height Convolution height. + * @param[in] weights Accessor to get kernel weights from. + * @param[in] bias Accessor to get kernel bias from. + * @param[in] conv_info Padding and stride information. + * @param[in] depth_multiplier (Optional) Depth multiplier parameter. + * @param[in] quant_info (Optional) Quantization info used for weights */ DepthwiseConvolutionLayer(unsigned int conv_width, unsigned int conv_height, ITensorAccessorUPtr weights, ITensorAccessorUPtr bias, PadStrideInfo conv_info, - const QuantizationInfo quant_info = QuantizationInfo()) + int depth_multiplier = 1, + const QuantizationInfo quant_info = QuantizationInfo()) : _conv_width(conv_width), _conv_height(conv_height), _conv_info(std::move(conv_info)), _weights(std::move(weights)), _bias(std::move(bias)), + _depth_multiplier(depth_multiplier), _quant_info(std::move(quant_info)) { } @@ -441,7 +444,7 @@ public: NodeIdxPair input = { s.tail_node(), 0 }; NodeParams common_params = { name(), s.hints().target_hint }; return GraphBuilder::add_depthwise_convolution_node(s.graph(), common_params, - input, Size2D(_conv_width, _conv_height), _conv_info, + input, Size2D(_conv_width, _conv_height), _conv_info, _depth_multiplier, s.hints().depthwise_convolution_method_hint, std::move(_weights), std::move(_bias), std::move(_quant_info)); } @@ -452,6 +455,7 @@ private: const PadStrideInfo _conv_info; ITensorAccessorUPtr _weights; ITensorAccessorUPtr _bias; + int _depth_multiplier; const QuantizationInfo _quant_info; }; diff --git a/arm_compute/graph/nodes/DepthwiseConvolutionLayerNode.h b/arm_compute/graph/nodes/DepthwiseConvolutionLayerNode.h index 7fa44b798f..8c0aae13c9 100644 --- a/arm_compute/graph/nodes/DepthwiseConvolutionLayerNode.h +++ b/arm_compute/graph/nodes/DepthwiseConvolutionLayerNode.h @@ -36,10 +36,13 @@ class DepthwiseConvolutionLayerNode final : public INode public: /** Constructor * - * @param[in] info Convolution layer attributes - * @param[in] method Depthwise convolution method to use + * @param[in] info Convolution layer attributes + * @param[in] depth_multiplier (Optional) Depth multiplier parameter. + * @param[in] method (Optional) Depthwise convolution method to use */ - DepthwiseConvolutionLayerNode(PadStrideInfo info, DepthwiseConvolutionMethod method = DepthwiseConvolutionMethod::Default); + DepthwiseConvolutionLayerNode(PadStrideInfo info, + int depth_multiplier = 1, + DepthwiseConvolutionMethod method = DepthwiseConvolutionMethod::Default); /** Sets the depthwise convolution method to use * * @param[in] method Depthwise convolution method to use @@ -53,6 +56,11 @@ public: * @return Depthwise convolution layer method do be used by the node */ DepthwiseConvolutionMethod depthwise_convolution_method() const; + /** Depth multiplier accessor + * + * @return Depth multiplier + */ + int depth_multiplier() const; /** Convolution metadata accessor * * @return Convolution information @@ -73,12 +81,14 @@ public: * @param[in] input_descriptor Input descriptor * @param[in] weights_descriptor Weights descriptor * @param[in] info Convolution operation attributes + * @param[in] depth_multiplier (Optional) Depth multiplier parameter. * * @return Output descriptor */ static TensorDescriptor compute_output_descriptor(const TensorDescriptor &input_descriptor, const TensorDescriptor &weights_descriptor, - const PadStrideInfo &info); + const PadStrideInfo &info, + int depth_multiplier = 1); // Inherited overridden methods: NodeType type() const override; @@ -91,6 +101,7 @@ public: private: PadStrideInfo _info; + int _depth_multiplier; DepthwiseConvolutionMethod _method; ActivationLayerInfo _fused_activation; }; diff --git a/arm_compute/runtime/CL/functions/CLDepthwiseConvolutionLayer.h b/arm_compute/runtime/CL/functions/CLDepthwiseConvolutionLayer.h index 4863101ccf..60dddbb853 100644 --- a/arm_compute/runtime/CL/functions/CLDepthwiseConvolutionLayer.h +++ b/arm_compute/runtime/CL/functions/CLDepthwiseConvolutionLayer.h @@ -34,8 +34,10 @@ #include "arm_compute/core/CL/kernels/CLGEMMMatrixVectorMultiplyKernel.h" #include "arm_compute/core/CL/kernels/ICLDepthwiseConvolutionLayer3x3Kernel.h" #include "arm_compute/core/Types.h" +#include "arm_compute/runtime/CL/CLMemoryGroup.h" #include "arm_compute/runtime/CL/CLTensor.h" #include "arm_compute/runtime/CL/functions/CLActivationLayer.h" +#include "arm_compute/runtime/CL/functions/CLPermute.h" #include "arm_compute/runtime/IFunction.h" namespace arm_compute @@ -53,7 +55,15 @@ class CLDepthwiseConvolutionLayer3x3 : public IFunction { public: /** Default constructor */ - CLDepthwiseConvolutionLayer3x3(); + CLDepthwiseConvolutionLayer3x3(std::shared_ptr<IMemoryManager> memory_manager = nullptr); + /** Prevent instances of this class from being copied (As this class contains pointers) */ + CLDepthwiseConvolutionLayer3x3(const CLDepthwiseConvolutionLayer3x3 &) = delete; + /** Default move constructor */ + CLDepthwiseConvolutionLayer3x3(CLDepthwiseConvolutionLayer3x3 &&) = default; + /** Prevent instances of this class from being copied (As this class contains pointers) */ + CLDepthwiseConvolutionLayer3x3 &operator=(const CLDepthwiseConvolutionLayer3x3 &) = delete; + /** Default move assignment operator */ + CLDepthwiseConvolutionLayer3x3 &operator=(CLDepthwiseConvolutionLayer3x3 &&) = default; /** Initialize the function's source, destination, conv and border_size. * * @param[in, out] input Source tensor. Data type supported: QASYMM8/F16/F32. (Written to only for border filling). @@ -86,10 +96,21 @@ public: ActivationLayerInfo act_info = ActivationLayerInfo(), GPUTarget gpu_target = GPUTarget::MIDGARD); // Inherited methods overriden: void run() override; + void prepare() override; private: + CLMemoryGroup _memory_group; std::unique_ptr<ICLDepthwiseConvolutionLayer3x3Kernel> _kernel; CLFillBorderKernel _border_handler; + CLPermute _permute_input_to_nchw; + CLPermute _permute_weights_to_nchw; + CLPermute _permute_output_to_nhwc; + CLTensor _permuted_input; + CLTensor _permuted_weights; + CLTensor _permuted_output; + const ITensor *_original_weights; + bool _needs_permute; + bool _is_prepared; }; /** Basic function to execute a generic depthwise convolution. This function calls the following OpenCL kernels: @@ -166,5 +187,5 @@ private: const ICLTensor *_original_weights; std::unique_ptr<IFunction> _optimised_function; }; -} +} // namespace arm_compute #endif /*__ARM_COMPUTE_CLDEPTHWISECONVOLUTION_H__ */ diff --git a/arm_compute/runtime/CL/functions/CLPermute.h b/arm_compute/runtime/CL/functions/CLPermute.h index 638207fc48..e1e3ce7334 100644 --- a/arm_compute/runtime/CL/functions/CLPermute.h +++ b/arm_compute/runtime/CL/functions/CLPermute.h @@ -54,5 +54,5 @@ public: */ static Status validate(const ITensorInfo *input, const ITensorInfo *output, const PermutationVector &perm); }; -} +} // namespace arm_compute #endif /*__ARM_COMPUTE_CLPERMUTE_H__ */ diff --git a/examples/graph_mobilenet.cpp b/examples/graph_mobilenet.cpp index 19a395d492..8d5e7f6dc4 100644 --- a/examples/graph_mobilenet.cpp +++ b/examples/graph_mobilenet.cpp @@ -323,7 +323,7 @@ private: 3U, 3U, get_weights_accessor(data_path, total_path + "depthwise_weights.npy"), get_weights_accessor(data_path, total_path + "depthwise_bias.npy"), - dwc_pad_stride_info, depth_weights_quant_info) + dwc_pad_stride_info, 1, depth_weights_quant_info) .set_name(total_path + "depthwise/depthwise") << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f)).set_name(total_path + "depthwise/Relu6") << ConvolutionLayer( diff --git a/examples/graph_mobilenet_v2.cpp b/examples/graph_mobilenet_v2.cpp index 2f15d0b0ea..8805999f99 100644 --- a/examples/graph_mobilenet_v2.cpp +++ b/examples/graph_mobilenet_v2.cpp @@ -364,7 +364,7 @@ private: << DepthwiseConvolutionLayer(3U, 3U, get_weights_accessor(data_path, "expanded_conv_depthwise_depthwise_weights.npy"), get_weights_accessor(data_path, "expanded_conv_depthwise_depthwise_biases.npy"), - PadStrideInfo(1, 1, 1, 1), dwc_q.at(0)) + PadStrideInfo(1, 1, 1, 1), 1, dwc_q.at(0)) .set_name("expanded_conv/depthwise/depthwise") << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f)).set_name("expanded_conv/depthwise/Relu6") << ConvolutionLayer(1U, 1U, 16U, @@ -425,7 +425,7 @@ private: << DepthwiseConvolutionLayer(3U, 3U, get_weights_accessor(data_path, total_path + "depthwise_depthwise_weights.npy"), get_weights_accessor(data_path, total_path + "depthwise_depthwise_biases.npy"), - dwc_pad_stride_info, dwi) + dwc_pad_stride_info, 1, dwi) .set_name(param_path + "/depthwise/depthwise") << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f)).set_name(param_path + "/depthwise/Relu6") << ConvolutionLayer(1U, 1U, output_channels, diff --git a/src/graph/GraphBuilder.cpp b/src/graph/GraphBuilder.cpp index b2ca28da57..3fc258d8bd 100644 --- a/src/graph/GraphBuilder.cpp +++ b/src/graph/GraphBuilder.cpp @@ -310,8 +310,8 @@ NodeID GraphBuilder::add_concatenate_node(Graph &g, NodeParams params, std::vect return nid; } -NodeID GraphBuilder::add_depthwise_convolution_node(Graph &g, NodeParams params, NodeIdxPair input, Size2D kernel_spatial_extend, PadStrideInfo conv_info, - DepthwiseConvolutionMethod method, +NodeID GraphBuilder::add_depthwise_convolution_node(Graph &g, NodeParams params, NodeIdxPair input, Size2D kernel_spatial_extend, + PadStrideInfo conv_info, int depth_multiplier, DepthwiseConvolutionMethod method, ITensorAccessorUPtr weights_accessor, ITensorAccessorUPtr bias_accessor, const QuantizationInfo quant_info) { CHECK_NODEIDX_PAIR(input, g); @@ -327,7 +327,7 @@ NodeID GraphBuilder::add_depthwise_convolution_node(Graph &g, NodeParams params, w_desc.shape.set(get_dimension_idx(input_tensor_desc, DataLayoutDimension::WIDTH), kernel_spatial_extend.width); w_desc.shape.set(get_dimension_idx(input_tensor_desc, DataLayoutDimension::HEIGHT), kernel_spatial_extend.height); w_desc.shape.set(get_dimension_idx(input_tensor_desc, DataLayoutDimension::CHANNEL), - get_dimension_size(input_tensor_desc, DataLayoutDimension::CHANNEL)); + get_dimension_size(input_tensor_desc, DataLayoutDimension::CHANNEL) * depth_multiplier); if(!quant_info.empty()) { w_desc.quant_info = quant_info; @@ -340,7 +340,7 @@ NodeID GraphBuilder::add_depthwise_convolution_node(Graph &g, NodeParams params, if(has_bias) { TensorDescriptor b_desc = input_tensor_desc; - b_desc.shape = TensorShape(get_dimension_size(input_tensor_desc, DataLayoutDimension::CHANNEL)); + b_desc.shape = TensorShape(get_dimension_size(input_tensor_desc, DataLayoutDimension::CHANNEL) * depth_multiplier); if(is_data_type_quantized_asymmetric(b_desc.data_type)) { @@ -351,7 +351,7 @@ NodeID GraphBuilder::add_depthwise_convolution_node(Graph &g, NodeParams params, } // Create convolution node and connect - NodeID conv_nid = g.add_node<DepthwiseConvolutionLayerNode>(conv_info, method); + NodeID conv_nid = g.add_node<DepthwiseConvolutionLayerNode>(conv_info, depth_multiplier, method); g.add_connection(input.node_id, input.index, conv_nid, 0); g.add_connection(w_nid, 0, conv_nid, 1); if(has_bias) diff --git a/src/graph/backends/GLES/GCFunctionsFactory.cpp b/src/graph/backends/GLES/GCFunctionsFactory.cpp index 2ca453ebde..0de58f5c28 100644 --- a/src/graph/backends/GLES/GCFunctionsFactory.cpp +++ b/src/graph/backends/GLES/GCFunctionsFactory.cpp @@ -176,8 +176,8 @@ std::unique_ptr<IFunction> create_depthwise_convolution_layer<GCDepthwiseConvolu const PadStrideInfo conv_info = node.convolution_info(); const DepthwiseConvolutionMethod dwc_algorithm = node.depthwise_convolution_method(); - const unsigned int depth_multiplier = 1; const ActivationLayerInfo fused_act = node.fused_activation(); + const int depth_multiplier = node.depth_multiplier(); // Create and configure function (we assume that functions have been validated before creation) std::unique_ptr<IFunction> func; @@ -204,6 +204,7 @@ std::unique_ptr<IFunction> create_depthwise_convolution_layer<GCDepthwiseConvolu << " Input shape: " << input->info()->tensor_shape() << " Weights shape: " << weights->info()->tensor_shape() << " Output shape: " << output->info()->tensor_shape() + << " Depth multiplier: " << depth_multiplier << (fused_act.enabled() ? " " + to_string(fused_act.activation()) : "") << std::endl); return func; diff --git a/src/graph/nodes/DepthwiseConvolutionLayerNode.cpp b/src/graph/nodes/DepthwiseConvolutionLayerNode.cpp index 02d16328b1..75ca5f4e03 100644 --- a/src/graph/nodes/DepthwiseConvolutionLayerNode.cpp +++ b/src/graph/nodes/DepthwiseConvolutionLayerNode.cpp @@ -32,13 +32,18 @@ namespace arm_compute { namespace graph { -DepthwiseConvolutionLayerNode::DepthwiseConvolutionLayerNode(PadStrideInfo info, DepthwiseConvolutionMethod method) - : _info(std::move(info)), _method(method), _fused_activation() +DepthwiseConvolutionLayerNode::DepthwiseConvolutionLayerNode(PadStrideInfo info, int depth_multiplier, DepthwiseConvolutionMethod method) + : _info(std::move(info)), _depth_multiplier(depth_multiplier), _method(method), _fused_activation() { _input_edges.resize(3, EmptyEdgeID); _outputs.resize(1, NullTensorID); } +int DepthwiseConvolutionLayerNode::depth_multiplier() const +{ + return _depth_multiplier; +} + void DepthwiseConvolutionLayerNode::set_depthwise_convolution_method(DepthwiseConvolutionMethod method) { _method = method; @@ -66,21 +71,24 @@ void DepthwiseConvolutionLayerNode::set_fused_activation(ActivationLayerInfo fus TensorDescriptor DepthwiseConvolutionLayerNode::compute_output_descriptor(const TensorDescriptor &input_descriptor, const TensorDescriptor &weights_descriptor, - const PadStrideInfo &info) + const PadStrideInfo &info, + int depth_multiplier) { unsigned int output_width = 0; unsigned int output_height = 0; - const unsigned int input_width = get_dimension_size(input_descriptor, DataLayoutDimension::WIDTH); - const unsigned int input_height = get_dimension_size(input_descriptor, DataLayoutDimension::HEIGHT); - const unsigned int kernel_width = get_dimension_size(weights_descriptor, DataLayoutDimension::WIDTH); - const unsigned int kernel_height = get_dimension_size(weights_descriptor, DataLayoutDimension::HEIGHT); + const unsigned int input_width = get_dimension_size(input_descriptor, DataLayoutDimension::WIDTH); + const unsigned int input_height = get_dimension_size(input_descriptor, DataLayoutDimension::HEIGHT); + const unsigned int input_channels = get_dimension_size(input_descriptor, DataLayoutDimension::CHANNEL); + const unsigned int kernel_width = get_dimension_size(weights_descriptor, DataLayoutDimension::WIDTH); + const unsigned int kernel_height = get_dimension_size(weights_descriptor, DataLayoutDimension::HEIGHT); std::tie(output_width, output_height) = scaled_dimensions(input_width, input_height, kernel_width, kernel_height, info); TensorDescriptor output_descriptor = input_descriptor; output_descriptor.shape.set(get_dimension_idx(output_descriptor, DataLayoutDimension::WIDTH), output_width); output_descriptor.shape.set(get_dimension_idx(output_descriptor, DataLayoutDimension::HEIGHT), output_height); + output_descriptor.shape.set(get_dimension_idx(output_descriptor, DataLayoutDimension::CHANNEL), input_channels * depth_multiplier); return output_descriptor; } @@ -105,7 +113,7 @@ TensorDescriptor DepthwiseConvolutionLayerNode::configure_output(size_t idx) con ARM_COMPUTE_ERROR_ON(src == nullptr || weights == nullptr); - return compute_output_descriptor(src->desc(), weights->desc(), _info); + return compute_output_descriptor(src->desc(), weights->desc(), _info, _depth_multiplier); } NodeType DepthwiseConvolutionLayerNode::type() const diff --git a/src/runtime/CL/functions/CLDepthwiseConvolutionLayer.cpp b/src/runtime/CL/functions/CLDepthwiseConvolutionLayer.cpp index 03cd5fd54f..c2782aaa89 100644 --- a/src/runtime/CL/functions/CLDepthwiseConvolutionLayer.cpp +++ b/src/runtime/CL/functions/CLDepthwiseConvolutionLayer.cpp @@ -26,6 +26,7 @@ #include "arm_compute/core/CL/ICLTensor.h" #include "arm_compute/core/CL/kernels/CLDepthwiseConvolutionLayer3x3NCHWKernel.h" #include "arm_compute/core/CL/kernels/CLDepthwiseConvolutionLayer3x3NHWCKernel.h" +#include "arm_compute/core/Helpers.h" #include "arm_compute/core/PixelValue.h" #include "arm_compute/core/utils/misc/ShapeCalculator.h" #include "arm_compute/core/utils/quantization/AsymmHelpers.h" @@ -36,8 +37,9 @@ using namespace arm_compute; using namespace arm_compute::misc; using namespace arm_compute::misc::shape_calculator; -CLDepthwiseConvolutionLayer3x3::CLDepthwiseConvolutionLayer3x3() - : _kernel(nullptr), _border_handler() +CLDepthwiseConvolutionLayer3x3::CLDepthwiseConvolutionLayer3x3(std::shared_ptr<IMemoryManager> memory_manager) + : _memory_group(std::move(memory_manager)), _kernel(nullptr), _border_handler(), _permute_input_to_nchw(), _permute_weights_to_nchw(), _permute_output_to_nhwc(), _permuted_input(), + _permuted_weights(), _permuted_output(), _original_weights(nullptr), _needs_permute(false), _is_prepared(false) { } @@ -47,17 +49,59 @@ void CLDepthwiseConvolutionLayer3x3::configure(ICLTensor *input, const ICLTensor ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32); ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights); - if(input->info()->data_layout() == DataLayout::NCHW) + const bool is_nhwc = input->info()->data_layout() == DataLayout::NHWC; + + _needs_permute = is_nhwc && (depth_multiplier > 1); + _is_prepared = false; + _original_weights = weights; + + ICLTensor *input_to_use = input; + const ICLTensor *weights_to_use = weights; + ICLTensor *output_to_use = output; + + if(_needs_permute) { + _memory_group.manage(&_permuted_input); + _memory_group.manage(&_permuted_output); + + // Configure the function to transform the input tensor from NHWC -> NCHW + _permute_input_to_nchw.configure(input, &_permuted_input, PermutationVector(1U, 2U, 0U)); + _permuted_input.info()->set_data_layout(DataLayout::NCHW); + + // Configure the function to transform the weights tensor from HWI -> IHW + _permute_weights_to_nchw.configure(weights, &_permuted_weights, PermutationVector(1U, 2U, 0U)); + _permuted_weights.info()->set_data_layout(DataLayout::NCHW); + + input_to_use = &_permuted_input; + weights_to_use = &_permuted_weights; + output_to_use = &_permuted_output; + _kernel = arm_compute::support::cpp14::make_unique<CLDepthwiseConvolutionLayer3x3NCHWKernel>(); } - else + else if(is_nhwc) { _kernel = arm_compute::support::cpp14::make_unique<CLDepthwiseConvolutionLayer3x3NHWCKernel>(); } + else + { + _kernel = arm_compute::support::cpp14::make_unique<CLDepthwiseConvolutionLayer3x3NCHWKernel>(); + } + // Configure kernel _kernel->set_target(CLScheduler::get().target()); - _kernel->configure(input, weights, biases, output, conv_info, depth_multiplier, act_info); + _kernel->configure(input_to_use, weights_to_use, biases, output_to_use, conv_info, depth_multiplier, act_info); + + // Permute output if needed + if(_needs_permute) + { + // Configure the function to transform the convoluted output to ACL's native ordering format NCHW + _permuted_output.info()->set_data_layout(DataLayout::NHWC); + _permute_output_to_nhwc.configure(&_permuted_output, output, PermutationVector(2U, 0U, 1U)); + + // Allocate tensors + _permuted_input.allocator()->allocate(); + _permuted_output.allocator()->allocate(); + } // Configure border handler PixelValue &&zero_value(0.f); @@ -75,18 +119,72 @@ Status CLDepthwiseConvolutionLayer3x3::validate(const ITensorInfo *input, const ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output); ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() == DataLayout::UNKNOWN); - if(input->data_layout() == DataLayout::NCHW) + const bool is_nhwc = input->data_layout() == DataLayout::NHWC; + const bool needs_permute = is_nhwc && (depth_multiplier > 1); + + if(needs_permute) { - return CLDepthwiseConvolutionLayer3x3NCHWKernel::validate(input, weights, biases, output, conv_info, depth_multiplier, act_info, gpu_target); + TensorShape permuted_input_shape = input->tensor_shape(); + TensorShape permuted_weights_shape = weights->tensor_shape(); + TensorShape permuted_output_shape = shape_calculator::compute_depthwise_convolution_shape(*input, *weights, conv_info, depth_multiplier); + + permute(permuted_input_shape, PermutationVector(1U, 2U, 0U)); + permute(permuted_weights_shape, PermutationVector(1U, 2U, 0U)); + permute(permuted_output_shape, PermutationVector(1U, 2U, 0U)); + + const TensorInfo permuted_input = input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_input_shape).set_data_layout(DataLayout::NCHW); + const TensorInfo permuted_weights = weights->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_weights_shape).set_data_layout(DataLayout::NCHW); + const TensorInfo permuted_output = output->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_output_shape).set_data_layout(DataLayout::NCHW); + + ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseConvolutionLayer3x3NCHWKernel::validate(&permuted_input, &permuted_weights, biases, &permuted_output, conv_info, depth_multiplier, act_info, gpu_target)); + } + else if(is_nhwc) + { + ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseConvolutionLayer3x3NHWCKernel::validate(input, weights, biases, output, conv_info, depth_multiplier, act_info)); + } + else + { + ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseConvolutionLayer3x3NCHWKernel::validate(input, weights, biases, output, conv_info, depth_multiplier, act_info, gpu_target)); } - return CLDepthwiseConvolutionLayer3x3NHWCKernel::validate(input, weights, biases, output, conv_info, depth_multiplier, act_info); + return Status{}; } void CLDepthwiseConvolutionLayer3x3::run() { + prepare(); + + _memory_group.acquire(); + + if(_needs_permute) + { + _permute_input_to_nchw.run(); + } CLScheduler::get().enqueue(_border_handler); CLScheduler::get().enqueue(*_kernel); + + if(_needs_permute) + { + _permute_output_to_nhwc.run(); + } + + _memory_group.release(); +} + +void CLDepthwiseConvolutionLayer3x3::prepare() +{ + if(!_is_prepared) + { + if(_needs_permute) + { + ARM_COMPUTE_ERROR_ON(!_original_weights->is_used()); + + _permuted_weights.allocator()->allocate(); + _permute_weights_to_nchw.run(); + _original_weights->mark_as_unused(); + } + _is_prepared = true; + } } namespace diff --git a/tests/validation/CL/DepthwiseConvolutionLayer.cpp b/tests/validation/CL/DepthwiseConvolutionLayer.cpp index 4d67dd9a01..dd2d9f344a 100644 --- a/tests/validation/CL/DepthwiseConvolutionLayer.cpp +++ b/tests/validation/CL/DepthwiseConvolutionLayer.cpp @@ -248,7 +248,7 @@ TEST_SUITE_END() TEST_SUITE(NHWC) FIXTURE_DATA_TEST_CASE(RunSmall, CLDepthwiseConvolutionLayerFixture<half>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SmallDepthwiseConvolutionLayerDataset3x3(), - framework::dataset::make("DepthMultiplier", 1)), // COMPMID-1071 Add depth multiplier support for NHWC + depth_multipliers), framework::dataset::make("DataType", DataType::F16)), framework::dataset::make("DataLayout", DataLayout::NHWC))) @@ -256,7 +256,7 @@ FIXTURE_DATA_TEST_CASE(RunSmall, CLDepthwiseConvolutionLayerFixture<half>, frame validate(CLAccessor(_target), _reference, tolerance_f16); } FIXTURE_DATA_TEST_CASE(RunLarge, CLDepthwiseConvolutionLayerFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeDepthwiseConvolutionLayerDataset3x3(), - framework::dataset::make("DepthMultiplier", 1)), // COMPMID-1071 Add depth multiplier support for NHWC + depth_multipliers), framework::dataset::make("DataType", DataType::F16)), framework::dataset::make("DataLayout", DataLayout::NHWC))) @@ -310,7 +310,7 @@ TEST_SUITE_END() TEST_SUITE(NHWC) FIXTURE_DATA_TEST_CASE(RunSmall, CLDepthwiseConvolutionLayerFixture<float>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SmallDepthwiseConvolutionLayerDataset3x3(), - framework::dataset::make("DepthMultiplier", 1)), // COMPMID-1071 Add depth multiplier support for NHWC + depth_multipliers), framework::dataset::make("DataType", DataType::F32)), framework::dataset::make("DataLayout", DataLayout::NHWC))) @@ -318,7 +318,7 @@ FIXTURE_DATA_TEST_CASE(RunSmall, CLDepthwiseConvolutionLayerFixture<float>, fram validate(CLAccessor(_target), _reference, tolerance_f32); } FIXTURE_DATA_TEST_CASE(RunLarge, CLDepthwiseConvolutionLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeDepthwiseConvolutionLayerDataset3x3(), - framework::dataset::make("DepthMultiplier", 1)), // COMPMID-1071 Add depth multiplier support for NHWC + depth_multipliers), framework::dataset::make("DataType", DataType::F32)), framework::dataset::make("DataLayout", DataLayout::NHWC))) @@ -376,7 +376,7 @@ TEST_SUITE_END() TEST_SUITE(W3x3) FIXTURE_DATA_TEST_CASE(RunSmall, CLDepthwiseConvolutionLayerQuantizedFixture<uint8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(combine(datasets::SmallDepthwiseConvolutionLayerDataset3x3(), - framework::dataset::make("DepthMultiplier", 1)), // COMPMID-1071 Add depth multiplier support for NHWC + depth_multipliers), framework::dataset::make("DataType", DataType::QASYMM8)), framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, 10) })), framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC }))) @@ -385,7 +385,7 @@ FIXTURE_DATA_TEST_CASE(RunSmall, CLDepthwiseConvolutionLayerQuantizedFixture<uin } FIXTURE_DATA_TEST_CASE(RunLarge, CLDepthwiseConvolutionLayerQuantizedFixture<uint8_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(combine(datasets::LargeDepthwiseConvolutionLayerDataset3x3(), - framework::dataset::make("DepthMultiplier", 1)), // COMPMID-1071 Add depth multiplier support for NHWC + depth_multipliers), framework::dataset::make("DataType", DataType::QASYMM8)), framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, 10) })), framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC }))) |