From 93a690edc48ad78cc9d7a580638475c6e77b32c2 Mon Sep 17 00:00:00 2001 From: Giorgio Arena Date: Tue, 1 Aug 2017 16:09:33 +0100 Subject: COMPMID-452 CL Depthwise Separable Convolution Layer kernel implementation, validation and benchmarking for 3x3xC depthwise filter and DataType::F32. Change-Id: I95c0c87709763cdbf58d0de66025eac86e30791b Reviewed-on: http://mpd-gerrit.cambridge.arm.com/82768 Tested-by: Kaizen Reviewed-by: Steven Niu --- src/core/CL/CLKernelLibrary.cpp | 5 + src/core/CL/cl_kernels/depthwise_convolution.cl | 189 +++++++++++++++++++++ .../CL/kernels/CLDepthwiseConvolutionKernel.cpp | 120 +++++++++++++ .../CL/functions/CLDepthwiseConvolution.cpp | 52 ++++++ .../CLDepthwiseSeparableConvolutionLayer.cpp | 50 ++++++ 5 files changed, 416 insertions(+) create mode 100644 src/core/CL/cl_kernels/depthwise_convolution.cl create mode 100644 src/core/CL/kernels/CLDepthwiseConvolutionKernel.cpp create mode 100644 src/runtime/CL/functions/CLDepthwiseConvolution.cpp create mode 100644 src/runtime/CL/functions/CLDepthwiseSeparableConvolutionLayer.cpp (limited to 'src') diff --git a/src/core/CL/CLKernelLibrary.cpp b/src/core/CL/CLKernelLibrary.cpp index 000cffa9ee..b5b331b1c1 100644 --- a/src/core/CL/CLKernelLibrary.cpp +++ b/src/core/CL/CLKernelLibrary.cpp @@ -143,6 +143,7 @@ const std::map CLKernelLibrary::_kernel_program_map = { "copy_plane", "channel_extract.cl" }, { "copy_planes_3p", "channel_combine.cl" }, { "copy_to_keypoint", "fast_corners.cl" }, + { "depthwise_convolution_3x3", "depthwise_convolution.cl" }, { "derivative", "derivative.cl" }, { "dilate", "dilate.cl" }, { "direct_convolution1x1", "direct_convolution1x1.cl" }, @@ -346,6 +347,10 @@ const std::map CLKernelLibrary::_program_source_map = { "depth_convert.cl", #include "./cl_kernels/depth_convert.clembed" + }, + { + "depthwise_convolution.cl", +#include "./cl_kernels/depthwise_convolution.clembed" }, { "derivative.cl", diff --git a/src/core/CL/cl_kernels/depthwise_convolution.cl b/src/core/CL/cl_kernels/depthwise_convolution.cl new file mode 100644 index 0000000000..cbcdbf2a34 --- /dev/null +++ b/src/core/CL/cl_kernels/depthwise_convolution.cl @@ -0,0 +1,189 @@ +/* + * Copyright (c) 2017 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "helpers.h" + +#if CONV_STRIDE_X == 1 +#define convolution1x3 convolution1x3_stride_1 +#elif CONV_STRIDE_X == 2 +#define convolution1x3 convolution1x3_stride_2 +#elif CONV_STRIDE_X == 3 +#define convolution1x3 convolution1x3_stride_3 +#else /* CONV_STRIDE_X */ +#error "Stride not supported" +#endif /* CONV_STRIDE_X */ + +/** Compute a 1D horizontal convolution of size 3 and stride 1 for floating point type. + * + * @param[in] left_pixel Pointer to the left pixel. + * @param[in] left_coeff Weight of the left pixel + * @param[in] middle_coeff Weight of the middle pixel + * @param[in] right_coeff Weight of the right pixel + * + * @return a float2 containing 2 convoluted values. + */ +inline float2 convolution1x3_stride_1(__global const uchar *left_pixel, + const float left_coeff, + const float middle_coeff, + const float right_coeff) +{ + float4 temp = vload4(0, (__global float *)left_pixel); + + float2 left = CONVERT(temp.s01, float2); + float2 middle = CONVERT(temp.s12, float2); + float2 right = CONVERT(temp.s23, float2); + + return left * (float2)left_coeff + middle * (float2)middle_coeff + right * (float2)right_coeff; +} + +/** Compute a 1D horizontal convolution of size 3 and stride 2 for floating point type. + * + * @param[in] left_pixel Pointer to the left pixel. + * @param[in] left_coeff Weight of the left pixel + * @param[in] middle_coeff Weight of the middle pixel + * @param[in] right_coeff Weight of the right pixel + * + * @return a float2 containing 2 convoluted values. + */ +inline float2 convolution1x3_stride_2(__global const uchar *left_pixel, + const float left_coeff, + const float middle_coeff, + const float right_coeff) +{ + float4 temp0 = vload4(0, (__global float *)left_pixel); + float temp1 = *((__global float *)(left_pixel + 4 * sizeof(float))); + + float2 left = CONVERT(temp0.s02, float2); + float2 middle = CONVERT(temp0.s13, float2); + float2 right = CONVERT((float2)(temp0.s2, temp1), float2); + + return left * (float2)left_coeff + middle * (float2)middle_coeff + right * (float2)right_coeff; +} + +/** Compute a 1D horizontal convolution of size 3 and stride 3 for floating point type. + * + * @param[in] left_pixel Pointer to the left pixel. + * @param[in] left_coeff Weight of the left pixel + * @param[in] middle_coeff Weight of the middle pixel + * @param[in] right_coeff Weight of the right pixel + * + * @return a float2 containing 2 convoluted values. + */ +inline float2 convolution1x3_stride_3(__global const uchar *left_pixel, + const float left_coeff, + const float middle_coeff, + const float right_coeff) +{ + float4 temp0 = vload4(0, (__global float *)left_pixel); + float2 temp1 = vload2(0, (__global float *)(left_pixel + 4 * sizeof(float))); + + float2 left = CONVERT(temp0.s03, float2); + float2 middle = CONVERT((float2)(temp0.s1, temp1.s0), float2); + float2 right = CONVERT((float2)(temp0.s2, temp1.s1), float2); + + return left * (float2)left_coeff + middle * (float2)middle_coeff + right * (float2)right_coeff; +} + +/** Apply a 3x3 convolution matrix to a single channel F32 input image and return the result. + * + * Convolution matrix layout: + * + * [ mat0, mat1, mat2 ]\n + * [ mat3, mat4, mat5 ]\n + * [ mat6, mat7, mat8 ]\n + * + * @param[in] src A pointer to source Image structure + * @param[in] mat0 Coefficient from the convolution matrix + * @param[in] mat1 Coefficient from the convolution matrix + * @param[in] mat2 Coefficient from the convolution matrix + * @param[in] mat3 Coefficient from the convolution matrix + * @param[in] mat4 Coefficient from the convolution matrix + * @param[in] mat5 Coefficient from the convolution matrix + * @param[in] mat6 Coefficient from the convolution matrix + * @param[in] mat0 Coefficient from the convolution matrix + * @param[in] mat7 Coefficient from the convolution matrix + * @param[in] mat8 Coefficient from the convolution matrix + * + * @return a float2 containing 2 convoluted values. + */ +inline float2 convolution3x3( + Image *src, + const float mat0, const float mat1, const float mat2, + const float mat3, const float mat4, const float mat5, + const float mat6, const float mat7, const float mat8) +{ + float2 pixels; + + pixels = convolution1x3(offset(src, 0, 0), mat0, mat1, mat2); + pixels += convolution1x3(offset(src, 0, 1), mat3, mat4, mat5); + pixels += convolution1x3(offset(src, 0, 2), mat6, mat7, mat8); + + return pixels; +} + +/** This function computes the horizontal integral of the image. + * + * @param[in] src_ptr Pointer to the source image. Supported data types: U8 + * @param[in] src_stride_x Stride of the source image in X dimension (in bytes) + * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] src_stride_y Stride of the source image in Y dimension (in bytes) + * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) + * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source image + * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes) + * @param[in] src_step_z src_stride_z * number of elements along Y processed per workitem(in bytes) + * @param[in] dst_ptr Pointer to the destination tensor. Supported data types: F16/F32 + * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes) + * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes) + * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes) + * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes) + * @param[in] dst_step_z dst_stride_z * number of elements along Y processed per workitem(in bytes) + * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor + * @param[in] weights_ptr Pointer to the weights tensor. Supported data types: F16/F32 + * @param[in] weights_stride_x Stride of the weights tensor in X dimension (in bytes) + * @param[in] weights_step_x weights_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] weights_stride_y Stride of the weights tensor in Y dimension (in bytes) + * @param[in] weights_step_y weights_stride_y * number of elements along Y processed per workitem(in bytes) + * @param[in] weights_stride_z Stride of the weights tensor in Z dimension (in bytes) + * @param[in] weights_step_z weights_stride_z * number of elements along Y processed per workitem(in bytes) + * @param[in] weights_offset_first_element_in_bytes The offset of the first element in the weights tensor + */ + +__kernel void depthwise_convolution_3x3(TENSOR3D_DECLARATION(src), TENSOR3D_DECLARATION(dst), TENSOR3D_DECLARATION(weights)) +{ + Image src = CONVERT_TENSOR3D_TO_IMAGE_STRUCT(src); + Image dst = CONVERT_TENSOR3D_TO_IMAGE_STRUCT(dst); + Tensor3D weights = CONVERT_TO_TENSOR3D_STRUCT(weights); + + uchar3 offset = (uchar3)(0, 1, 2) * (uchar3)weights_stride_y; + float3 weights_values0 = vload3(0, (__global float *)(weights.ptr + offset.s0)); + float3 weights_values1 = vload3(0, (__global float *)(weights.ptr + offset.s1)); + float3 weights_values2 = vload3(0, (__global float *)(weights.ptr + offset.s2)); + + float2 pixels = convolution3x3(&src, weights_values0.s0, weights_values0.s1, weights_values0.s2, + weights_values1.s0, weights_values1.s1, weights_values1.s2, + weights_values2.s0, weights_values2.s1, weights_values2.s2); + + vstore2(pixels, 0, (__global float *)dst.ptr); +} \ No newline at end of file diff --git a/src/core/CL/kernels/CLDepthwiseConvolutionKernel.cpp b/src/core/CL/kernels/CLDepthwiseConvolutionKernel.cpp new file mode 100644 index 0000000000..a24e304359 --- /dev/null +++ b/src/core/CL/kernels/CLDepthwiseConvolutionKernel.cpp @@ -0,0 +1,120 @@ +/* + * Copyright (c) 2017 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "arm_compute/core/CL/kernels/CLDepthwiseConvolutionKernel.h" + +#include "arm_compute/core/AccessWindowStatic.h" +#include "arm_compute/core/CL/CLHelpers.h" +#include "arm_compute/core/CL/CLKernelLibrary.h" +#include "arm_compute/core/CL/ICLKernel.h" +#include "arm_compute/core/CL/ICLTensor.h" +#include "arm_compute/core/Error.h" +#include "arm_compute/core/Helpers.h" +#include "arm_compute/core/TensorInfo.h" +#include "arm_compute/core/Types.h" +#include "arm_compute/core/Utils.h" + +using namespace arm_compute; + +CLDepthwiseConvolutionKernel::CLDepthwiseConvolutionKernel() + : _border_size(0), _input(), _output(), _weights(), _conv_stride_x(0), _conv_stride_y(0), _conv_pad_x(0), _conv_pad_y(0) +{ +} + +BorderSize CLDepthwiseConvolutionKernel::border_size() const +{ + return _border_size; +} + +void CLDepthwiseConvolutionKernel::configure(const ICLTensor *input, ICLTensor *output, const ICLTensor *weights, const PadStrideInfo &conv_info) +{ + ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32); + ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::F32); + ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::F32); + ARM_COMPUTE_ERROR_ON(weights->info()->dimension(0) != 3 || weights->info()->dimension(1) != 3); + + _input = input; + _output = output; + _weights = weights; + _conv_stride_x = conv_info.stride().first; + _conv_stride_y = conv_info.stride().second; + _border_size = BorderSize(weights->info()->dimension(1) / 2, weights->info()->dimension(0) / 2); + _conv_pad_x = std::min(border_size().right, conv_info.pad().first); + _conv_pad_y = std::min(border_size().bottom, conv_info.pad().second); + + // Set build options + std::set options; + + options.emplace("-DCONV_STRIDE_X=" + support::cpp11::to_string(_conv_stride_x)); + + _kernel = static_cast(CLKernelLibrary::get().create_kernel("depthwise_convolution_3x3", options)); + + // Configure kernel window + const unsigned int num_elems_processed_per_iteration = 2; + const unsigned int num_elems_written_per_iteration = 2; + const unsigned int num_elems_read_per_iteration = (_conv_stride_x == 1) ? 4 : (_conv_stride_x == 2) ? 5 : 6; + const unsigned int num_rows_read_per_iteration = 3; + + Window win = calculate_max_window(*output->info(), Steps(num_elems_processed_per_iteration)); + + const int access_right = border_size().left + ceil_to_multiple(border_size().left + input->info()->dimension(0), num_elems_read_per_iteration); + const int access_bottom = border_size().bottom + ceil_to_multiple(border_size().bottom + input->info()->dimension(1), num_rows_read_per_iteration); + + AccessWindowStatic input_access(input->info(), -border_size().left, -border_size().bottom, access_right, access_bottom); + AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration); + AccessWindowStatic weights_access(weights->info(), 0, 0, weights->info()->dimension(0), weights->info()->dimension(1)); + + update_window_and_padding(win, input_access, weights_access, output_access); + + output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape())); + + ICLKernel::configure(win); +} + +void CLDepthwiseConvolutionKernel::run(const Window &window, cl::CommandQueue &queue) +{ + ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); + ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); + + Window slice_in = window.first_slice_window_3D(); + Window slice_out = window.first_slice_window_3D(); + Window slice_weights = window.first_slice_window_3D(); + + slice_in.adjust(Window::DimX, -_conv_pad_x, true); + slice_in.adjust(Window::DimY, -_conv_pad_y, true); + slice_in.set_dimension_step(Window::DimX, window.x().step() * _conv_stride_x); + slice_in.set_dimension_step(Window::DimY, window.y().step() * _conv_stride_y); + slice_weights.set_dimension_step(Window::DimX, 0); + slice_weights.set_dimension_step(Window::DimY, 0); + + do + { + unsigned int idx = 0; + add_3D_tensor_argument(idx, _input, slice_in); + add_3D_tensor_argument(idx, _output, slice_out); + add_3D_tensor_argument(idx, _weights, slice_weights); + + enqueue(queue, *this, slice_out); + } + while(window.slide_window_slice_3D(slice_out)); +} \ No newline at end of file diff --git a/src/runtime/CL/functions/CLDepthwiseConvolution.cpp b/src/runtime/CL/functions/CLDepthwiseConvolution.cpp new file mode 100644 index 0000000000..7dac885ed0 --- /dev/null +++ b/src/runtime/CL/functions/CLDepthwiseConvolution.cpp @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2017 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "arm_compute/runtime/CL/functions/CLDepthwiseConvolution.h" + +#include "arm_compute/core/CL/ICLTensor.h" +#include "arm_compute/core/PixelValue.h" +#include "arm_compute/runtime/CL/CLScheduler.h" +#include "support/ToolchainSupport.h" + +using namespace arm_compute; + +CLDepthwiseConvolution::CLDepthwiseConvolution() + : _kernel(), _border_handler() +{ +} + +void CLDepthwiseConvolution::configure(ICLTensor *input, ICLTensor *output, const ICLTensor *weights, const PadStrideInfo &conv_info) +{ + ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32); + ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::F32); + ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::F32); + + _kernel.configure(input, output, weights, conv_info); + _border_handler.configure(input, _kernel.border_size(), BorderMode::CONSTANT, PixelValue(0)); +} + +void CLDepthwiseConvolution::run() +{ + CLScheduler::get().enqueue(_border_handler); + CLScheduler::get().enqueue(_kernel); +} \ No newline at end of file diff --git a/src/runtime/CL/functions/CLDepthwiseSeparableConvolutionLayer.cpp b/src/runtime/CL/functions/CLDepthwiseSeparableConvolutionLayer.cpp new file mode 100644 index 0000000000..c325b3e01f --- /dev/null +++ b/src/runtime/CL/functions/CLDepthwiseSeparableConvolutionLayer.cpp @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2017 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "arm_compute/runtime/CL/functions/CLDepthwiseSeparableConvolutionLayer.h" + +#include "arm_compute/core/CL/ICLTensor.h" +#include "arm_compute/core/PixelValue.h" +#include "arm_compute/runtime/CL/CLScheduler.h" +#include "support/ToolchainSupport.h" + +using namespace arm_compute; + +CLDepthwiseSeparableConvolutionLayer::CLDepthwiseSeparableConvolutionLayer() + : _depthwise_conv(), _pointwise_conv() +{ +} + +void CLDepthwiseSeparableConvolutionLayer::configure(ICLTensor *input, const ICLTensor *depthwise_weights, ICLTensor *depthwise_out, const ICLTensor *pointwise_weights, const ICLTensor *biases, + ICLTensor *output, + const PadStrideInfo &depthwise_conv_info, const PadStrideInfo &pointwise_conv_info) +{ + _depthwise_conv.configure(input, depthwise_out, depthwise_weights, depthwise_conv_info); + _pointwise_conv.configure(depthwise_out, pointwise_weights, biases, output, pointwise_conv_info); +} + +void CLDepthwiseSeparableConvolutionLayer::run() +{ + _depthwise_conv.run(); + _pointwise_conv.run(); +} \ No newline at end of file -- cgit v1.2.1