diff options
author | Giorgio Arena <gioare01@e108627-lin.cambridge.arm.com> | 2018-03-01 11:13:45 +0000 |
---|---|---|
committer | Anthony Barbier <anthony.barbier@arm.com> | 2018-11-02 16:49:16 +0000 |
commit | 1f9ca1d7737846c74053d68ee0844b448bae298b (patch) | |
tree | c8f8c6850b59899a01efcde3b0a2e294af40c5b5 | |
parent | a9676118fd2a0e5bc916969af83ecee049bae76b (diff) | |
download | ComputeLibrary-1f9ca1d7737846c74053d68ee0844b448bae298b.tar.gz |
COMPMID-935 Implementing Convolution with Winograd on OpenCL (part 3)
Change-Id: I51f92f30602fb0a02314f344fa67061f448694bf
Reviewed-on: https://eu-gerrit-1.euhpc.arm.com/122793
Tested-by: Jenkins <bsgcomp@arm.com>
Reviewed-by: Giorgio Arena <giorgio.arena@arm.com>
Reviewed-by: Gian Marco Iodice <gianmarco.iodice@arm.com>
-rw-r--r-- | arm_compute/core/CL/CLKernels.h | 1 | ||||
-rw-r--r-- | arm_compute/core/CL/kernels/CLWinogradInputTransformKernel.h | 79 | ||||
-rw-r--r-- | arm_compute/core/utils/misc/ShapeCalculator.h | 18 | ||||
-rw-r--r-- | arm_compute/runtime/CL/CLFunctions.h | 1 | ||||
-rw-r--r-- | arm_compute/runtime/CL/functions/CLWinogradInputTransform.h | 60 | ||||
-rw-r--r-- | src/core/CL/CLKernelLibrary.cpp | 6 | ||||
-rw-r--r-- | src/core/CL/cl_kernels/winograd.cl | 208 | ||||
-rw-r--r-- | src/core/CL/kernels/CLWinogradInputTransformKernel.cpp | 180 | ||||
-rw-r--r-- | src/runtime/CL/functions/CLWinogradInputTransform.cpp | 45 | ||||
-rw-r--r-- | tests/datasets/WinogradInputTransformDataset.h | 140 | ||||
-rw-r--r-- | tests/validation/CL/Winograd.cpp | 135 | ||||
-rw-r--r-- | tests/validation/Helpers.cpp | 110 | ||||
-rwxr-xr-x | tests/validation/Helpers.h | 30 | ||||
-rw-r--r-- | tests/validation/fixtures/WinogradLayerFixture.h | 85 | ||||
-rw-r--r-- | tests/validation/reference/Winograd.cpp | 137 | ||||
-rw-r--r-- | tests/validation/reference/Winograd.h | 43 |
16 files changed, 1276 insertions, 2 deletions
diff --git a/arm_compute/core/CL/CLKernels.h b/arm_compute/core/CL/CLKernels.h index e7fa74d00e..ca2cb0411a 100644 --- a/arm_compute/core/CL/CLKernels.h +++ b/arm_compute/core/CL/CLKernels.h @@ -109,5 +109,6 @@ #include "arm_compute/core/CL/kernels/CLWarpAffineKernel.h" #include "arm_compute/core/CL/kernels/CLWarpPerspectiveKernel.h" #include "arm_compute/core/CL/kernels/CLWeightsReshapeKernel.h" +#include "arm_compute/core/CL/kernels/CLWinogradInputTransformKernel.h" #endif /* __ARM_COMPUTE_CLKERNELS_H__ */ diff --git a/arm_compute/core/CL/kernels/CLWinogradInputTransformKernel.h b/arm_compute/core/CL/kernels/CLWinogradInputTransformKernel.h new file mode 100644 index 0000000000..15cd6e2649 --- /dev/null +++ b/arm_compute/core/CL/kernels/CLWinogradInputTransformKernel.h @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2018 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. + */ +#ifndef __ARM_COMPUTE_CLWINOGRADINPUTTRANSFORMKERNEL_H__ +#define __ARM_COMPUTE_CLWINOGRADINPUTTRANSFORMKERNEL_H__ + +#include "arm_compute/core/CL/ICLKernel.h" + +namespace arm_compute +{ +class ICLTensor; + +/** OpenCL kernel to perform Winograd input transform.*/ +class CLWinogradInputTransformKernel : public ICLKernel +{ +public: + /** Default constructor */ + CLWinogradInputTransformKernel(); + /** Prevent instances of this class from being copied (As this class contains pointers) */ + CLWinogradInputTransformKernel(const CLWinogradInputTransformKernel &) = delete; + /** Prevent instances of this class from being copied (As this class contains pointers) */ + CLWinogradInputTransformKernel &operator=(const CLWinogradInputTransformKernel &) = delete; + /** Allow instances of this class to be moved */ + CLWinogradInputTransformKernel(CLWinogradInputTransformKernel &&) = default; + /** Allow instances of this class to be moved */ + CLWinogradInputTransformKernel &operator=(CLWinogradInputTransformKernel &&) = default; + /** Set the input and output of the kernel. + * + * @param[in] input The input tensor to permute. Data types supported: F32 + * @param[in] output The output tensor. Data types supported: Same as @p input + * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. Currently only unit strides are supported. + * @param[in] kernel_dims Kernel dimensions. Currently only 3x3 kernels are supported + */ + void configure(const ICLTensor *input, ICLTensor *output, const PadStrideInfo &conv_info, const Size2D &kernel_dims); + /** Static function to check if given info will lead to a valid configuration of @ref CLWinogradInputTransformKernel + * + * @param[in] input First tensor input info. Data types supported: F32. + * @param[in] output Output tensor info. Data types supported: same as @p input. + * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. Currently only unit strides are supported. + * @param[in] kernel_dims Kernel dimensions. Currently only 3x3 kernels are supported + * + * @return a status + */ + static Status validate(const ITensorInfo *input, const ITensorInfo *output, const PadStrideInfo &conv_info, const Size2D &kernel_dims); + + // Inherited methods overridden: + void run(const Window &window, cl::CommandQueue &queue) override; + BorderSize border_size() const override; + +private: + BorderSize _border_size; + const ICLTensor *_input; + ICLTensor *_output; + int _num_tiles_x; + int _num_tiles_y; + unsigned int _step_z; +}; +} // arm_compute +#endif /*__ARM_COMPUTE_CLWINOGRADINPUTTRANSFORMKERNEL_H__ */ diff --git a/arm_compute/core/utils/misc/ShapeCalculator.h b/arm_compute/core/utils/misc/ShapeCalculator.h index e21e5cd0d6..2919625511 100644 --- a/arm_compute/core/utils/misc/ShapeCalculator.h +++ b/arm_compute/core/utils/misc/ShapeCalculator.h @@ -194,6 +194,24 @@ inline TensorShape compute_fully_connected_reshaped_weights_shape(const ITensorI return output_shape; } + +inline TensorShape compute_winograd_input_transform_shape(const ITensorInfo &input, const PadStrideInfo &conv_info, const Size2D &kernel_size) +{ + // Compute height + const unsigned int num_tiles_x = std::ceil((input.tensor_shape().x() - (kernel_size.width - 1) + conv_info.pad_left() + conv_info.pad_right()) / 2.f); + const unsigned int num_tiles_y = std::ceil((input.tensor_shape().y() - (kernel_size.height - 1) + conv_info.pad_top() + conv_info.pad_bottom()) / 2.f); + + const unsigned int width = input.tensor_shape()[get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::CHANNEL)]; + const unsigned int height = num_tiles_x * num_tiles_y; + const unsigned int depth = 16; // COMPMID-990 + + TensorShape output_shape{ input.tensor_shape() }; + output_shape.set(0, width); + output_shape.set(1, height); + output_shape.set(2, depth); + + return output_shape; +} } // namespace shape_calculator } // namespace misc } // namespace arm_compute diff --git a/arm_compute/runtime/CL/CLFunctions.h b/arm_compute/runtime/CL/CLFunctions.h index a5bbc41a17..7c2377aa09 100644 --- a/arm_compute/runtime/CL/CLFunctions.h +++ b/arm_compute/runtime/CL/CLFunctions.h @@ -107,5 +107,6 @@ #include "arm_compute/runtime/CL/functions/CLTranspose.h" #include "arm_compute/runtime/CL/functions/CLWarpAffine.h" #include "arm_compute/runtime/CL/functions/CLWarpPerspective.h" +#include "arm_compute/runtime/CL/functions/CLWinogradInputTransform.h" #endif /* __ARM_COMPUTE_CLFUNCTIONS_H__ */ diff --git a/arm_compute/runtime/CL/functions/CLWinogradInputTransform.h b/arm_compute/runtime/CL/functions/CLWinogradInputTransform.h new file mode 100644 index 0000000000..54b8bdecba --- /dev/null +++ b/arm_compute/runtime/CL/functions/CLWinogradInputTransform.h @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2018 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. + */ +#ifndef __ARM_COMPUTE_CLWINOGRADINPUTTRANSFORM_H__ +#define __ARM_COMPUTE_CLWINOGRADINPUTTRANSFORM_H__ + +#include "arm_compute/core/Types.h" +#include "arm_compute/runtime/CL/ICLSimpleFunction.h" + +#include <cstdint> + +namespace arm_compute +{ +class ICLTensor; + +/** Basic function to execute a @ref CLWinogradInputTransformKernel. */ +class CLWinogradInputTransform : public ICLSimpleFunction +{ +public: + /** Set the input and output tensors. + * + * @param[in] input The input tensor to transform. Data types supported: F32 + * @param[in] output The output tensor. Data types supported: Same as @p input + * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. Currently only unit strides are supported. + * @param[in] kernel_dims Kernel dimensions. Currently only 3x3 kernels are supported + */ + void configure(ICLTensor *input, ICLTensor *output, const PadStrideInfo &conv_info, const Size2D &kernel_dims); + /** Static function to check if given info will lead to a valid configuration of @ref CLWinogradInputTransform. + * + * @param[in] input First tensor input info. Data types supported: F32. + * @param[in] output Output tensor info. Data types supported: same as @p input. + * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. Currently only unit strides are supported. + * @param[in] kernel_dims Kernel dimensions. Currently only 3x3 kernels are supported + * + * @return a status + */ + static Status validate(const ITensorInfo *input, const ITensorInfo *output, const PadStrideInfo &conv_info, const Size2D &kernel_dims); +}; +} +#endif /*__ARM_COMPUTE_CLWINOGRADINPUTTRANSFORM_H__ */ diff --git a/src/core/CL/CLKernelLibrary.cpp b/src/core/CL/CLKernelLibrary.cpp index aba20448e7..40aceb702a 100644 --- a/src/core/CL/CLKernelLibrary.cpp +++ b/src/core/CL/CLKernelLibrary.cpp @@ -351,6 +351,8 @@ const std::map<std::string, std::string> CLKernelLibrary::_kernel_program_map = { "warp_affine_bilinear", "warp_affine.cl" }, { "warp_perspective_nearest_neighbour", "warp_perspective.cl" }, { "warp_perspective_bilinear", "warp_perspective.cl" }, + { "winograd_input_transform_2x2_3x3_stepz1_nchw", "winograd.cl" }, + { "winograd_input_transform_2x2_3x3_stepz2_nchw", "winograd.cl" }, { "YUYV422_to_IYUV_bt709", "color_convert.cl" }, { "YUYV422_to_NV12_bt709", "color_convert.cl" }, { "YUYV422_to_RGB888_bt709", "color_convert.cl" }, @@ -676,6 +678,10 @@ const std::map<std::string, std::string> CLKernelLibrary::_program_source_map = "warp_perspective.cl", #include "./cl_kernels/warp_perspective.clembed" }, + { + "winograd.cl", +#include "./cl_kernels/winograd.clembed" + }, #endif /* EMBEDDED_KERNELS */ }; diff --git a/src/core/CL/cl_kernels/winograd.cl b/src/core/CL/cl_kernels/winograd.cl new file mode 100644 index 0000000000..fa06601c50 --- /dev/null +++ b/src/core/CL/cl_kernels/winograd.cl @@ -0,0 +1,208 @@ +/* + * Copyright (c) 2018 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 defined(NUM_TILES_X) + +/** This OpenCL kernel computes the input transform when the kernel size is 3x3 and the output tile is 2x2 + * + * @note The number of tiles in the x axis must be passed at compile time using -DNUM_TILES_X (i.e.-DNUM_TILES_X=5). + * @note The pad left and pad top must be passed at compile time using -DPAD_LEFT and -DPAD_TOP (i.e.-DPAD_LEFT=1 and -DPAD_TOP=0). + * + * @param[in] src_ptr Pointer to the source image. Supported data types: F32 + * @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: as @p src_ptr + * @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 + */ +__kernel void winograd_input_transform_2x2_3x3_stepz1_nchw( + TENSOR3D_DECLARATION(src), + TENSOR3D_DECLARATION(dst)) +{ + int x = get_global_id(0); + int y = get_global_id(1); + int z = get_global_id(2); + + // Compute input address + __global uchar *src_addr = src_ptr + src_offset_first_element_in_bytes + x * 2 * src_stride_x + y * 2 * src_stride_y + z * src_stride_z; + + src_addr = src_addr - ((int)PAD_LEFT * src_stride_x) - ((int)PAD_TOP * src_stride_y); + + float4 in_row0 = vload4(0, (__global float *)(src_addr + 0 * src_stride_y)); + float4 in_row1 = vload4(0, (__global float *)(src_addr + 1 * src_stride_y)); + float4 in_row2 = vload4(0, (__global float *)(src_addr + 2 * src_stride_y)); + float4 in_row3 = vload4(0, (__global float *)(src_addr + 3 * src_stride_y)); + + float4 tmp0 = in_row0 - in_row2; + float4 tmp1 = in_row1 + in_row2; + float4 tmp2 = in_row2 - in_row1; + float4 tmp3 = in_row1 - in_row3; + + float out00 = tmp0.s0 - tmp0.s2; + float out01 = tmp0.s1 + tmp0.s2; + float out02 = tmp0.s2 - tmp0.s1; + float out03 = tmp0.s1 - tmp0.s3; + + float out10 = tmp1.s0 - tmp1.s2; + float out11 = tmp1.s1 + tmp1.s2; + float out12 = tmp1.s2 - tmp1.s1; + float out13 = tmp1.s1 - tmp1.s3; + + float out20 = tmp2.s0 - tmp2.s2; + float out21 = tmp2.s1 + tmp2.s2; + float out22 = tmp2.s2 - tmp2.s1; + float out23 = tmp2.s1 - tmp2.s3; + + float out30 = tmp3.s0 - tmp3.s2; + float out31 = tmp3.s1 + tmp3.s2; + float out32 = tmp3.s2 - tmp3.s1; + float out33 = tmp3.s1 - tmp3.s3; + + __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + z * dst_stride_x + (x + y * (int)NUM_TILES_X) * dst_stride_y; + + *((__global float *)(dst_addr + 0 * dst_stride_z)) = out00; + *((__global float *)(dst_addr + 1 * dst_stride_z)) = out01; + *((__global float *)(dst_addr + 2 * dst_stride_z)) = out02; + *((__global float *)(dst_addr + 3 * dst_stride_z)) = out03; + *((__global float *)(dst_addr + 4 * dst_stride_z)) = out10; + *((__global float *)(dst_addr + 5 * dst_stride_z)) = out11; + *((__global float *)(dst_addr + 6 * dst_stride_z)) = out12; + *((__global float *)(dst_addr + 7 * dst_stride_z)) = out13; + *((__global float *)(dst_addr + 8 * dst_stride_z)) = out20; + *((__global float *)(dst_addr + 9 * dst_stride_z)) = out21; + *((__global float *)(dst_addr + 10 * dst_stride_z)) = out22; + *((__global float *)(dst_addr + 11 * dst_stride_z)) = out23; + *((__global float *)(dst_addr + 12 * dst_stride_z)) = out30; + *((__global float *)(dst_addr + 13 * dst_stride_z)) = out31; + *((__global float *)(dst_addr + 14 * dst_stride_z)) = out32; + *((__global float *)(dst_addr + 15 * dst_stride_z)) = out33; +} + +/** This OpenCL kernel computes the input transform when the kernel size is 3x3, the output tile is 2x2 and the number of channels is multiple of 2 + * + * @note The number of tiles in the x axis must be passed at compile time using -DNUM_TILES_X (i.e.-DNUM_TILES_X=5). + * @note The pad left and pad top must be passed at compile time using -DPAD_LEFT and -DPAD_TOP (i.e.-DPAD_LEFT=1 and -DPAD_TOP=0). + * + * @param[in] src_ptr Pointer to the source image. Supported data types: F32 + * @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: as @p src_ptr + * @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 + */ +__kernel void winograd_input_transform_2x2_3x3_stepz2_nchw( + TENSOR3D_DECLARATION(src), + TENSOR3D_DECLARATION(dst)) +{ + int x = get_global_id(0); + int y = get_global_id(1); + int z = get_global_id(2) * 2; + + // Compute input address + __global uchar *src_addr = src_ptr + src_offset_first_element_in_bytes + x * 2 * src_stride_x + y * 2 * src_stride_y + z * src_stride_z; + + src_addr = src_addr - ((int)PAD_LEFT * src_stride_x) - ((int)PAD_TOP * src_stride_y); + + float4 in_row0 = vload4(0, (__global float *)(src_addr + 0 * src_stride_y)); + float4 in_row1 = vload4(0, (__global float *)(src_addr + 1 * src_stride_y)); + float4 in_row2 = vload4(0, (__global float *)(src_addr + 2 * src_stride_y)); + float4 in_row3 = vload4(0, (__global float *)(src_addr + 3 * src_stride_y)); + + src_addr += src_stride_z; + float4 in_row4 = vload4(0, (__global float *)(src_addr + 0 * src_stride_y)); + float4 in_row5 = vload4(0, (__global float *)(src_addr + 1 * src_stride_y)); + float4 in_row6 = vload4(0, (__global float *)(src_addr + 2 * src_stride_y)); + float4 in_row7 = vload4(0, (__global float *)(src_addr + 3 * src_stride_y)); + + float4 tmp0 = in_row0 - in_row2; + float4 tmp1 = in_row1 + in_row2; + float4 tmp2 = in_row2 - in_row1; + float4 tmp3 = in_row1 - in_row3; + + float4 tmp4 = in_row4 - in_row6; + float4 tmp5 = in_row5 + in_row6; + float4 tmp6 = in_row6 - in_row5; + float4 tmp7 = in_row5 - in_row7; + + float2 out00 = (float2)(tmp0.s0 - tmp0.s2, tmp4.s0 - tmp4.s2); + float2 out01 = (float2)(tmp0.s1 + tmp0.s2, tmp4.s1 + tmp4.s2); + float2 out02 = (float2)(tmp0.s2 - tmp0.s1, tmp4.s2 - tmp4.s1); + float2 out03 = (float2)(tmp0.s1 - tmp0.s3, tmp4.s1 - tmp4.s3); + + float2 out10 = (float2)(tmp1.s0 - tmp1.s2, tmp5.s0 - tmp5.s2); + float2 out11 = (float2)(tmp1.s1 + tmp1.s2, tmp5.s1 + tmp5.s2); + float2 out12 = (float2)(tmp1.s2 - tmp1.s1, tmp5.s2 - tmp5.s1); + float2 out13 = (float2)(tmp1.s1 - tmp1.s3, tmp5.s1 - tmp5.s3); + + float2 out20 = (float2)(tmp2.s0 - tmp2.s2, tmp6.s0 - tmp6.s2); + float2 out21 = (float2)(tmp2.s1 + tmp2.s2, tmp6.s1 + tmp6.s2); + float2 out22 = (float2)(tmp2.s2 - tmp2.s1, tmp6.s2 - tmp6.s1); + float2 out23 = (float2)(tmp2.s1 - tmp2.s3, tmp6.s1 - tmp6.s3); + + float2 out30 = (float2)(tmp3.s0 - tmp3.s2, tmp7.s0 - tmp7.s2); + float2 out31 = (float2)(tmp3.s1 + tmp3.s2, tmp7.s1 + tmp7.s2); + float2 out32 = (float2)(tmp3.s2 - tmp3.s1, tmp7.s2 - tmp7.s1); + float2 out33 = (float2)(tmp3.s1 - tmp3.s3, tmp7.s1 - tmp7.s3); + + __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + z * dst_stride_x + (x + y * (int)NUM_TILES_X) * dst_stride_y; + + vstore2(out00, 0, (__global float *)(dst_addr + 0 * dst_stride_z)); + vstore2(out01, 0, (__global float *)(dst_addr + 1 * dst_stride_z)); + vstore2(out02, 0, (__global float *)(dst_addr + 2 * dst_stride_z)); + vstore2(out03, 0, (__global float *)(dst_addr + 3 * dst_stride_z)); + vstore2(out10, 0, (__global float *)(dst_addr + 4 * dst_stride_z)); + vstore2(out11, 0, (__global float *)(dst_addr + 5 * dst_stride_z)); + vstore2(out12, 0, (__global float *)(dst_addr + 6 * dst_stride_z)); + vstore2(out13, 0, (__global float *)(dst_addr + 7 * dst_stride_z)); + vstore2(out20, 0, (__global float *)(dst_addr + 8 * dst_stride_z)); + vstore2(out21, 0, (__global float *)(dst_addr + 9 * dst_stride_z)); + vstore2(out22, 0, (__global float *)(dst_addr + 10 * dst_stride_z)); + vstore2(out23, 0, (__global float *)(dst_addr + 11 * dst_stride_z)); + vstore2(out30, 0, (__global float *)(dst_addr + 12 * dst_stride_z)); + vstore2(out31, 0, (__global float *)(dst_addr + 13 * dst_stride_z)); + vstore2(out32, 0, (__global float *)(dst_addr + 14 * dst_stride_z)); + vstore2(out33, 0, (__global float *)(dst_addr + 15 * dst_stride_z)); +} +#endif //defined(NUM_TILES_X)
\ No newline at end of file diff --git a/src/core/CL/kernels/CLWinogradInputTransformKernel.cpp b/src/core/CL/kernels/CLWinogradInputTransformKernel.cpp new file mode 100644 index 0000000000..72adb5f358 --- /dev/null +++ b/src/core/CL/kernels/CLWinogradInputTransformKernel.cpp @@ -0,0 +1,180 @@ +/* + * Copyright (c) 2018 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/CLWinogradInputTransformKernel.h" + +#include "arm_compute/core/CL/CLHelpers.h" +#include "arm_compute/core/CL/CLKernelLibrary.h" +#include "arm_compute/core/CL/ICLTensor.h" +#include "arm_compute/core/CL/OpenCL.h" +#include "arm_compute/core/Error.h" +#include "arm_compute/core/Helpers.h" +#include "arm_compute/core/Types.h" +#include "arm_compute/core/utils/misc/ShapeCalculator.h" +#include "support/ToolchainSupport.h" + +using namespace arm_compute; + +namespace +{ +Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const PadStrideInfo &conv_info, const Size2D &kernel_dims) +{ + ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32); + ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.stride().first != 1 || conv_info.stride().second != 1, "Winograd input transform only supports unit strides"); + ARM_COMPUTE_RETURN_ERROR_ON_MSG(kernel_dims.width != 3 || kernel_dims.height != 3, "Winograd input transform only supports 3x3 kernels"); + ARM_COMPUTE_UNUSED(kernel_dims); + + const TensorShape output_shape = misc::shape_calculator::compute_winograd_input_transform_shape(*input, conv_info, Size2D(3U, 3U)); + + // Validate configured output + if(output->total_size() != 0) + { + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), output_shape); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); + } + + return Status{}; +} + +std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, const PadStrideInfo &conv_info, const Size2D &kernel_dims) +{ + ARM_COMPUTE_UNUSED(output); + ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); + ARM_COMPUTE_ERROR_ON(kernel_dims.width != 3 || kernel_dims.height != 3); + ARM_COMPUTE_UNUSED(kernel_dims); + + constexpr unsigned int num_elems_read_per_iteration_x = 4u; + constexpr unsigned int num_elems_read_per_iteration_y = 4u; + + Window win = calculate_max_window(*input, Steps(1, 1)); + + AccessWindowRectangle input_access(input, -conv_info.pad_left(), -conv_info.pad_top(), num_elems_read_per_iteration_x, num_elems_read_per_iteration_y); + + bool window_changed = update_window_and_padding(win, input_access); + + Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; + return std::make_pair(err, win); +} +} // namespace + +CLWinogradInputTransformKernel::CLWinogradInputTransformKernel() + : _border_size(0), _input(nullptr), _output(nullptr), _num_tiles_x(0), _num_tiles_y(0), _step_z(1) +{ +} + +BorderSize CLWinogradInputTransformKernel::border_size() const +{ + return _border_size; +} + +void CLWinogradInputTransformKernel::configure(const ICLTensor *input, ICLTensor *output, const PadStrideInfo &conv_info, const Size2D &kernel_dims) +{ + ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); + ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), conv_info, kernel_dims)); + + // Compute number of elements to process in the X and Y direction + const int num_elements_x = input->info()->dimension(0) - 2 + conv_info.pad_left() + conv_info.pad_right(); + const int num_elements_y = input->info()->dimension(1) - 2 + conv_info.pad_top() + conv_info.pad_bottom(); + + // Check if we need to extend the right or bottom border + const unsigned int extra_border_right = (num_elements_x % 2 == 0) ? 0u : 1u; + const unsigned int extra_border_bottom = (num_elements_y % 2 == 0) ? 0u : 1u; + + _input = input; + _output = output; + _border_size = BorderSize(conv_info.pad_top(), conv_info.pad_right() + extra_border_right, conv_info.pad_bottom() + extra_border_bottom, conv_info.pad_left()); + _num_tiles_x = std::ceil(num_elements_x / 2.0f); + _num_tiles_y = std::ceil(num_elements_y / 2.0f); + + const TensorShape output_shape = misc::shape_calculator::compute_winograd_input_transform_shape(*input->info(), conv_info, Size2D(3U, 3U)); + + // Output auto inizialitation if not yet initialized + auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape)); + + ARM_COMPUTE_ERROR_ON(_num_tiles_x * _num_tiles_y != static_cast<int>(output->info()->dimension(1))); + + CLBuildOptions build_opts; + build_opts.add_option("-DNUM_TILES_X=" + support::cpp11::to_string(_num_tiles_x)); + build_opts.add_option("-DPAD_LEFT=" + support::cpp11::to_string(conv_info.pad_left())); + build_opts.add_option("-DPAD_TOP=" + support::cpp11::to_string(conv_info.pad_top())); + + // Create kernel + if((_input->info()->dimension(2) % 2) != 0) + { + _step_z = 1; + _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("winograd_input_transform_2x2_3x3_stepz1_nchw", build_opts.options())); + } + else + { + _step_z = 2; + _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("winograd_input_transform_2x2_3x3_stepz2_nchw", build_opts.options())); + _lws_hint = cl::NDRange(1, 1, 8); + } + + // Create window and update padding + auto win_config = validate_and_configure_window(input->info(), output->info(), conv_info, kernel_dims); + ARM_COMPUTE_ERROR_THROW_ON(win_config.first); + ICLKernel::configure(win_config.second); + + _config_id = "winograd_transform_input_2x2_3x3_"; + _config_id += support::cpp11::to_string(input->info()->dimension(0)); + _config_id += "_"; + _config_id += support::cpp11::to_string(input->info()->dimension(1)); + _config_id += "_"; + _config_id += support::cpp11::to_string(input->info()->dimension(2)); + _config_id += "_"; + _config_id += support::cpp11::to_string(conv_info.pad_left()); + _config_id += "_"; + _config_id += support::cpp11::to_string(conv_info.pad_top()); +} + +Status CLWinogradInputTransformKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const PadStrideInfo &conv_info, const Size2D &kernel_dims) +{ + ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); + ARM_COMPUTE_RETURN_ERROR_ON(validate_arguments(input, output, conv_info, kernel_dims)); + + return Status{}; +} + +void CLWinogradInputTransformKernel::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 = window.first_slice_window_3D(); + slice.set(Window::DimX, Window::Dimension(0, _num_tiles_x, 1)); + slice.set(Window::DimY, Window::Dimension(0, _num_tiles_y, 1)); + + ARM_COMPUTE_ERROR_ON(((slice.z().end() - slice.z().start()) % _step_z) != 0); + slice.set(Window::DimZ, Window::Dimension(slice.z().start(), slice.z().end(), _step_z)); + + do + { + unsigned int idx = 0; + add_3D_tensor_argument(idx, _input, slice); + add_3D_tensor_argument(idx, _output, slice); + + enqueue(queue, *this, slice, _lws_hint); + } + while(window.slide_window_slice_3D(slice)); +} diff --git a/src/runtime/CL/functions/CLWinogradInputTransform.cpp b/src/runtime/CL/functions/CLWinogradInputTransform.cpp new file mode 100644 index 0000000000..652f31ac74 --- /dev/null +++ b/src/runtime/CL/functions/CLWinogradInputTransform.cpp @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2018 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/CLWinogradInputTransform.h" + +#include "arm_compute/core/CL/ICLTensor.h" +#include "arm_compute/core/CL/kernels/CLWinogradInputTransformKernel.h" +#include "arm_compute/core/Error.h" +#include "support/ToolchainSupport.h" + +using namespace arm_compute; + +void CLWinogradInputTransform::configure(ICLTensor *input, ICLTensor *output, const PadStrideInfo &conv_info, const Size2D &kernel_dims) +{ + auto k = arm_compute::support::cpp14::make_unique<CLWinogradInputTransformKernel>(); + k->configure(input, output, conv_info, kernel_dims); + _kernel = std::move(k); + _border_handler.configure(input, _kernel->border_size(), BorderMode::CONSTANT, PixelValue(0)); +} + +Status CLWinogradInputTransform::validate(const ITensorInfo *input, const ITensorInfo *output, const PadStrideInfo &conv_info, const Size2D &kernel_dims) +{ + ARM_COMPUTE_RETURN_ERROR_ON(CLWinogradInputTransformKernel::validate(input, output, conv_info, kernel_dims)); + return Status{}; +} diff --git a/tests/datasets/WinogradInputTransformDataset.h b/tests/datasets/WinogradInputTransformDataset.h new file mode 100644 index 0000000000..07e41ebf7a --- /dev/null +++ b/tests/datasets/WinogradInputTransformDataset.h @@ -0,0 +1,140 @@ +/* + * Copyright (c) 2018 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. + */ +#ifndef ARM_COMPUTE_TEST_WINOGRAD_INPUT_TRANSFORM_DATASET +#define ARM_COMPUTE_TEST_WINOGRAD_INPUT_TRANSFORM_DATASET + +#include "utils/TypePrinter.h" + +#include "arm_compute/core/Types.h" + +namespace arm_compute +{ +namespace test +{ +namespace datasets +{ +class WinogradInputTransformDataset +{ +public: + using type = std::tuple<TensorShape, PadStrideInfo, Size2D, bool>; + + struct iterator + { + iterator(std::vector<TensorShape>::const_iterator in_it, std::vector<PadStrideInfo>::const_iterator info_it, std::vector<Size2D>::const_iterator kernel_dims_it, + std::vector<bool>::const_iterator format_it) + : _in_it{ std::move(in_it) }, _info_it{ std::move(info_it) }, _kernel_dims_it{ std::move(kernel_dims_it) }, _format_it{ std::move(format_it) } + { + } + + std::string description() const + { + std::stringstream description; + description << "In=" << *_in_it << ":"; + description << "Info=" << *_info_it; + description << "KernelDims=" << *_kernel_dims_it; + description << "IsNCHW=" << *_format_it; + return description.str(); + } + + WinogradInputTransformDataset::type operator*() const + { + return std::make_tuple(*_in_it, *_info_it, *_kernel_dims_it, *_format_it); + } + + iterator &operator++() + { + ++_in_it; + ++_info_it; + ++_kernel_dims_it; + ++_format_it; + + return *this; + } + + private: + std::vector<TensorShape>::const_iterator _in_it; + std::vector<PadStrideInfo>::const_iterator _info_it; + std::vector<Size2D>::const_iterator _kernel_dims_it; + std::vector<bool>::const_iterator _format_it; + }; + + iterator begin() const + { + return iterator(_in_shapes.begin(), _infos.begin(), _kernel_dims.begin(), _format.begin()); + } + + int size() const + { + return std::min(_in_shapes.size(), std::min(_infos.size(), std::min(_kernel_dims.size(), _format.size()))); + } + + void add_config(TensorShape in, PadStrideInfo info, Size2D kernel_dims, bool format) + { + _in_shapes.emplace_back(std::move(in)); + _infos.emplace_back(std::move(info)); + _kernel_dims.emplace_back(std::move(kernel_dims)); + _format.emplace_back(std::move(format)); + } + +protected: + WinogradInputTransformDataset() = default; + WinogradInputTransformDataset(WinogradInputTransformDataset &&) = default; + +private: + std::vector<TensorShape> _in_shapes{}; + std::vector<PadStrideInfo> _infos{}; + std::vector<Size2D> _kernel_dims{}; + std::vector<bool> _format{}; +}; + +class SmallWinogradInputTransformDataset final : public WinogradInputTransformDataset +{ +public: + SmallWinogradInputTransformDataset() + { + add_config(TensorShape(9U, 9U), PadStrideInfo(1, 1, 1, 1), Size2D(3U, 3U), true); + add_config(TensorShape(27U, 13U, 2U), PadStrideInfo(1, 1, 0, 0), Size2D(3U, 3U), true); + add_config(TensorShape(128U, 64U, 1U, 3U), PadStrideInfo(1, 1, 1, 1), Size2D(3U, 3U), true); + add_config(TensorShape(9U, 9U, 3U, 4U), PadStrideInfo(1, 1, 0, 0), Size2D(3U, 3U), true); + add_config(TensorShape(27U, 13U, 2U, 4U), PadStrideInfo(1, 1, 1, 1), Size2D(3U, 3U), true); + add_config(TensorShape(9U, 9U, 3U, 5U), PadStrideInfo(1, 1, 0, 0), Size2D(3U, 3U), true); + add_config(TensorShape(14U, 14U, 512U, 2U), PadStrideInfo(1, 1, 1, 1), Size2D(3U, 3U), true); + } +}; + +class LargeWinogradInputTransformDataset final : public WinogradInputTransformDataset +{ +public: + LargeWinogradInputTransformDataset() + { + add_config(TensorShape(42U, 37U, 8U, 15U), PadStrideInfo(1, 1, 1, 1), Size2D(3U, 3U), true); + add_config(TensorShape(57U, 60U, 13U, 8U), PadStrideInfo(1, 1, 1, 1), Size2D(3U, 3U), true); + add_config(TensorShape(128U, 64U, 21U, 13U), PadStrideInfo(1, 1, 0, 0), Size2D(3U, 3U), true); + add_config(TensorShape(83U, 72U, 14U, 5U), PadStrideInfo(1, 1, 0, 0), Size2D(3U, 3U), true); + } +}; +} // namespace datasets +} // namespace test +} // namespace arm_compute +#endif /* ARM_COMPUTE_TEST_WINOGRAD_INPUT_TRANSFORM_DATASET */ diff --git a/tests/validation/CL/Winograd.cpp b/tests/validation/CL/Winograd.cpp new file mode 100644 index 0000000000..664b3f4ef8 --- /dev/null +++ b/tests/validation/CL/Winograd.cpp @@ -0,0 +1,135 @@ +/* + * Copyright (c) 2018 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 CONCLCTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "arm_compute/core/Types.h" +#include "arm_compute/core/utils/misc/ShapeCalculator.h" +#include "arm_compute/runtime/CL/CLTensor.h" +#include "arm_compute/runtime/CL/CLTensorAllocator.h" +#include "arm_compute/runtime/CL/functions/CLWinogradInputTransform.h" +#include "tests/CL/CLAccessor.h" +#include "tests/datasets/WinogradInputTransformDataset.h" +#include "tests/framework/Asserts.h" +#include "tests/framework/Macros.h" +#include "tests/framework/datasets/Datasets.h" +#include "tests/validation/Validation.h" +#include "tests/validation/fixtures/WinogradLayerFixture.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +TEST_SUITE(CL) +TEST_SUITE(Winograd) + +TEST_SUITE(InputTransform) + +// *INDENT-OFF* +// clang-format off +DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(zip( + framework::dataset::make("InputInfo",{ + TensorInfo(TensorShape(53U, 21U, 5U, 3U), 1, DataType::F16), // F16 not supported + TensorInfo(TensorShape(53U, 21U, 5U, 3U), 1, DataType::QASYMM8), // QASYMM8 not supported + TensorInfo(TensorShape(53U, 21U, 5U, 3U), 1, DataType::F32), // Kernel size not supported + TensorInfo(TensorShape(53U, 21U, 5U, 3U), 1, DataType::F32), // Strides not supported + TensorInfo(TensorShape(53U, 33U, 4U), 1, DataType::F32), // valid + TensorInfo(TensorShape(34U, 42U, 7U, 3U), 1, DataType::F32), // valid + TensorInfo(TensorShape(31U, 37U, 37U), 1, DataType::F32) // valid + }), + framework::dataset::make("OutputInfo", { + TensorInfo(TensorShape(5U, 5U, 16U, 3U), 1, DataType::F16), + TensorInfo(TensorShape(5U, 5U, 16U, 3U), 1, DataType::QASYMM8), + TensorInfo(TensorShape(5U, 5U, 16U, 3U), 1, DataType::F32), + TensorInfo(TensorShape(5U, 1U, 16U, 3U), 1, DataType::F32), + TensorInfo(TensorShape(4U, 442U, 16U), 1, DataType::F32), + TensorInfo(TensorShape(7U, 320U, 16U, 3U), 1, DataType::F32), + TensorInfo(TensorShape(37U, 304U, 16U), 1, DataType::F32) + })), + framework::dataset::make("PadStrideInfo", { + PadStrideInfo(1, 1, 1, 0), + PadStrideInfo(1, 1, 0, 0), + PadStrideInfo(1, 1, 1, 1), + PadStrideInfo(2, 1, 1, 1), + PadStrideInfo(1, 1, 0, 1), + PadStrideInfo(1, 1, 0, 0), + PadStrideInfo(1, 1, 1, 1) + })), + framework::dataset::make("KernelDims", { + Size2D(3U, 3U), + Size2D(3U, 3U), + Size2D(5U, 5U), + Size2D(3U, 3U), + Size2D(3U, 3U), + Size2D(3U, 3U), + Size2D(3U, 3U) + })), + framework::dataset::make("Expected", { false, false, false, false, true, true, true })), + input_info, output_info, conv_info, kernel_dims, expected) +{ + ARM_COMPUTE_EXPECT(bool(CLWinogradInputTransform::validate(&input_info.clone()->set_is_resizable(false), &output_info.clone()->set_is_resizable(false), conv_info, kernel_dims)) == expected, framework::LogLevel::ERRORS); +} +// clang-format on +// *INDENT-ON* + +using CLWinogradInputTransformFixture = WinogradInputTransformValidationFixture<CLTensor, CLAccessor, CLWinogradInputTransform, float>; + +DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(framework::dataset::concat(datasets::SmallWinogradInputTransformDataset(), datasets::LargeWinogradInputTransformDataset()), + framework::dataset::make("DataType", { DataType::F32 })), + shape_in, conv_info, kernel_dims, is_nchw_format, data_type) +{ + ARM_COMPUTE_UNUSED(is_nchw_format); + + TensorShape shape_out = compute_winograd_input_transform_shape(TensorInfo(shape_in, 1, data_type), conv_info, kernel_dims); + + // Create tensors + CLTensor in = create_tensor<CLTensor>(shape_in, data_type); + CLTensor out = create_tensor<CLTensor>(shape_out, data_type); + + ARM_COMPUTE_EXPECT(in.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(out.info()->is_resizable(), framework::LogLevel::ERRORS); + + // Create and configure function + CLWinogradInputTransform winograd_input_transform; + + // Configure the function + winograd_input_transform.configure(&in, &out, conv_info, kernel_dims); +} + +FIXTURE_DATA_TEST_CASE(RunSmall, CLWinogradInputTransformFixture, framework::DatasetMode::PRECOMMIT, combine(datasets::SmallWinogradInputTransformDataset(), framework::dataset::make("DataType", { DataType::F32 }))) +{ + validate(CLAccessor(_target), _reference); +} + +FIXTURE_DATA_TEST_CASE(RunLarge, CLWinogradInputTransformFixture, framework::DatasetMode::NIGHTLY, combine(datasets::LargeWinogradInputTransformDataset(), framework::dataset::make("DataType", { DataType::F32 }))) +{ + validate(CLAccessor(_target), _reference); +} + +TEST_SUITE_END() + +TEST_SUITE_END() +TEST_SUITE_END() +} // namespace validation +} // namespace test +} // namespace arm_compute diff --git a/tests/validation/Helpers.cpp b/tests/validation/Helpers.cpp index 313b059a8c..3d554f0d25 100644 --- a/tests/validation/Helpers.cpp +++ b/tests/validation/Helpers.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017 ARM Limited. + * Copyright (c) 2017-2018 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -23,6 +23,9 @@ */ #include "tests/validation/Helpers.h" +#include <algorithm> +#include <cmath> + namespace arm_compute { namespace test @@ -133,6 +136,111 @@ SimpleTensor<uint8_t> convert_to_asymmetric(const SimpleTensor<float> &src, cons } return dst; } + +void matrix_multiply(const SimpleTensor<float> &a, const SimpleTensor<float> &b, SimpleTensor<float> &out) +{ + ARM_COMPUTE_ERROR_ON(a.shape()[0] != b.shape()[1]); + ARM_COMPUTE_ERROR_ON(a.shape()[1] != out.shape()[1]); + ARM_COMPUTE_ERROR_ON(b.shape()[0] != out.shape()[0]); + + const int M = a.shape()[1]; // Rows + const int N = b.shape()[0]; // Cols + const int K = b.shape()[1]; + + for(int y = 0; y < M; ++y) + { + for(int x = 0; x < N; ++x) + { + float acc = 0.0f; + for(int k = 0; k < K; ++k) + { + acc += a[y * K + k] * b[x + k * N]; + } + + out[x + y * N] = acc; + } + } +} + +void transpose_matrix(const SimpleTensor<float> &in, SimpleTensor<float> &out) +{ + ARM_COMPUTE_ERROR_ON((in.shape()[0] != out.shape()[1]) || (in.shape()[1] != out.shape()[0])); + + const int width = in.shape()[0]; + const int height = in.shape()[1]; + + for(int y = 0; y < height; ++y) + { + for(int x = 0; x < width; ++x) + { + const float val = in[x + y * width]; + + out[x * height + y] = val; + } + } +} + +template <typename T> +void get_tile(const SimpleTensor<T> &in, SimpleTensor<T> &tile, const Coordinates &coord) +{ + ARM_COMPUTE_ERROR_ON(tile.shape().num_dimensions() != 2); + + const int w_tile = tile.shape()[0]; + const int h_tile = tile.shape()[1]; + + // Fill the tile with zeros + std::fill(tile.data() + 0, (tile.data() + (w_tile * h_tile)), static_cast<T>(0)); + + // Check if with the dimensions greater than 2 we could have out-of-bound reads + for(size_t d = 2; d < Coordinates::num_max_dimensions; ++d) + { + if(coord[d] < 0 || coord[d] >= static_cast<int>(in.shape()[d])) + { + ARM_COMPUTE_ERROR("coord[d] < 0 || coord[d] >= in.shape()[d] with d >= 2"); + } + } + + // Since we could have out-of-bound reads along the X and Y dimensions, + // we start calculating the input address with x = 0 and y = 0 + Coordinates start_coord = coord; + start_coord[0] = 0; + start_coord[1] = 0; + + // Get input and roi pointers + auto in_ptr = static_cast<const T *>(in(start_coord)); + auto roi_ptr = static_cast<T *>(tile.data()); + + const int x_in_start = std::max(0, coord[0]); + const int y_in_start = std::max(0, coord[1]); + const int x_in_end = std::min(static_cast<int>(in.shape()[0]), coord[0] + w_tile); + const int y_in_end = std::min(static_cast<int>(in.shape()[1]), coord[1] + h_tile); + + // Number of elements to copy per row + const int n = x_in_end - x_in_start; + + // Starting coordinates for the ROI + const int x_tile_start = coord[0] > 0 ? 0 : std::abs(coord[0]); + const int y_tile_start = coord[1] > 0 ? 0 : std::abs(coord[1]); + + // Update input pointer + in_ptr += x_in_start; + in_ptr += (y_in_start * in.shape()[0]); + + // Update ROI pointer + roi_ptr += x_tile_start; + roi_ptr += (y_tile_start * tile.shape()[0]); + + for(int y = y_in_start; y < y_in_end; ++y) + { + // Copy per row + std::copy(in_ptr, in_ptr + n, roi_ptr); + + in_ptr += in.shape()[0]; + roi_ptr += tile.shape()[0]; + } +} + +template void get_tile(const SimpleTensor<float> &in, SimpleTensor<float> &roi, const Coordinates &coord); } // namespace validation } // namespace test } // namespace arm_compute diff --git a/tests/validation/Helpers.h b/tests/validation/Helpers.h index ba45968392..b192f317b4 100755 --- a/tests/validation/Helpers.h +++ b/tests/validation/Helpers.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017 ARM Limited. + * Copyright (c) 2017-2018 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -232,6 +232,34 @@ SimpleTensor<float> convert_from_asymmetric(const SimpleTensor<uint8_t> &src); * @return Quantized tensor. */ SimpleTensor<uint8_t> convert_to_asymmetric(const SimpleTensor<float> &src, const QuantizationInfo &quantization_info); + +/** Matrix multiply between 2 float simple tensors + * + * @param[in] a Input tensor A + * @param[in] b Input tensor B + * @param[out] out Output tensor + * + */ +void matrix_multiply(const SimpleTensor<float> &a, const SimpleTensor<float> &b, SimpleTensor<float> &out); + +/** Transpose matrix + * + * @param[in] in Input tensor + * @param[out] out Output tensor + * + */ +void transpose_matrix(const SimpleTensor<float> &in, SimpleTensor<float> &out); + +/** Get a 2D tile from a tensor + * + * @note In case of out-of-bound reads, the tile will be filled with zeros + * + * @param[in] in Input tensor + * @param[out] tile Tile + * @param[in] coord Coordinates + */ +template <typename T> +void get_tile(const SimpleTensor<T> &in, SimpleTensor<T> &tile, const Coordinates &coord); } // namespace validation } // namespace test } // namespace arm_compute diff --git a/tests/validation/fixtures/WinogradLayerFixture.h b/tests/validation/fixtures/WinogradLayerFixture.h index d7f0cbfdf5..95e331560d 100644 --- a/tests/validation/fixtures/WinogradLayerFixture.h +++ b/tests/validation/fixtures/WinogradLayerFixture.h @@ -26,6 +26,7 @@ #include "arm_compute/core/TensorShape.h" #include "arm_compute/core/Types.h" +#include "arm_compute/core/utils/misc/ShapeCalculator.h" #include "arm_compute/runtime/NEON/NEScheduler.h" #include "tests/AssetsLibrary.h" #include "tests/Globals.h" @@ -35,6 +36,7 @@ #include "tests/validation/Helpers.h" #include "tests/validation/reference/ConvolutionLayer.h" #include "tests/validation/reference/Utils.h" +#include "tests/validation/reference/Winograd.h" #include <random> @@ -46,6 +48,8 @@ namespace test { namespace validation { +using namespace arm_compute::misc::shape_calculator; + template <typename TensorType, typename AccessorType, typename FunctionType, typename T> class WinogradLayerValidationFixture : public framework::Fixture { @@ -139,6 +143,87 @@ protected: DataType _data_type{}; }; +template <typename TensorType, typename AccessorType, typename FunctionType, typename T> +class WinogradInputTransformValidationFixture : public framework::Fixture +{ +public: + template <typename...> + void setup(TensorShape input_shape, PadStrideInfo conv_info, Size2D kernel_dims, bool is_nchw_format, DataType data_type) + { + TensorShape output_shape = compute_winograd_input_transform_shape(TensorInfo(input_shape, 1, data_type), conv_info, kernel_dims); + + _target = compute_target(input_shape, output_shape, conv_info, kernel_dims, is_nchw_format, data_type); + _reference = compute_reference(input_shape, output_shape, conv_info, kernel_dims, is_nchw_format, data_type); + } + +protected: + template <typename U> + void fill(U &&tensor, int i, float min, float max) + { + switch(tensor.data_type()) + { + case DataType::F32: + { + std::uniform_real_distribution<> distribution(min, max); + library->fill(tensor, distribution, i); + break; + } + default: + { + ARM_COMPUTE_ERROR("Not supported"); + library->fill_tensor_uniform(tensor, i); + break; + } + } + } + + TensorType compute_target(const TensorShape &input_shape, const TensorShape &output_shape, const PadStrideInfo &conv_info, const Size2D &kernel_dims, bool is_nchw_format, DataType data_type) + { + ARM_COMPUTE_UNUSED(is_nchw_format); + + // Create tensors + TensorType src = create_tensor<TensorType>(input_shape, data_type); + TensorType dst = create_tensor<TensorType>(output_shape, data_type); + + // Create and configure function + FunctionType transf; + transf.configure(&src, &dst, conv_info, kernel_dims); + + ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); + + // Allocate tensors + src.allocator()->allocate(); + dst.allocator()->allocate(); + + ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); + + // Fill tensors + fill(AccessorType(src), 0, -1.f, 1.f); + + // Compute CLWinogradInputTransform function + transf.run(); + + return dst; + } + + SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &output_shape, const PadStrideInfo &conv_info, const Size2D &kernel_dims, bool is_nchw_format, DataType data_type) + { + ARM_COMPUTE_UNUSED(is_nchw_format); + + // Create reference + SimpleTensor<T> src{ input_shape, data_type }; + + // Fill reference + fill(src, 0, -1.f, 1.f); + + return reference::winograd_input_transform<T>(src, output_shape, conv_info, kernel_dims); + } + + TensorType _target{}; + SimpleTensor<T> _reference{}; +}; } // namespace validation } // namespace test } // namespace arm_compute diff --git a/tests/validation/reference/Winograd.cpp b/tests/validation/reference/Winograd.cpp new file mode 100644 index 0000000000..371bb6348e --- /dev/null +++ b/tests/validation/reference/Winograd.cpp @@ -0,0 +1,137 @@ +/* + * Copyright (c) 2018 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 "Winograd.h" + +#include "tests/validation/Helpers.h" +#include "tests/validation/reference/Utils.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace reference +{ +namespace +{ +template <typename T> +void winograd_input_transform3x3(const SimpleTensor<T> &src, SimpleTensor<T> &dst, const PadStrideInfo &conv_info) +{ + TensorShape shape4x4(4u, 4u); + + // Simple tensor for the 4x4 input tile + SimpleTensor<T> src_tile{ shape4x4, src.data_type() }; + + // Simple tensor for the 4x4 temporary tile + SimpleTensor<T> tmp_tile{ shape4x4, src.data_type() }; + + // Simple tensor for the 4x4 output tile + SimpleTensor<T> dst_tile{ shape4x4, src.data_type() }; + + // Simple tensor for the transformation matrix + SimpleTensor<T> matrix{ shape4x4, src.data_type() }; + + // Simple tensor for the transformation matrix transposed + SimpleTensor<T> matrix_transposed{ shape4x4, src.data_type() }; + + const float matrix_values[] = { 1.f, 0.f, -1.f, 0.f, + 0.f, 1.f, 1.f, 0.f, + 0.f, -1.f, 1.f, 0.f, + 0.f, 1.f, 0.f, -1.f + }; + + for(int i = 0; i < matrix.num_elements(); ++i) + { + matrix[i] = matrix_values[i]; + } + + transpose_matrix(matrix, matrix_transposed); + + const int in_w = src.shape().x(); + const int in_h = src.shape().y(); + const int in_d = src.shape().z(); + const int num_batches = src.shape().total_size() / (in_w * in_h * in_d); + const int num_tiles_x = std::ceil((in_w - 2 + conv_info.pad_left() + conv_info.pad_right()) / 2.0f); + const int num_tiles_y = std::ceil((in_h - 2 + conv_info.pad_top() + conv_info.pad_bottom()) / 2.0f); + + ARM_COMPUTE_ERROR_ON((num_tiles_x * num_tiles_y) != static_cast<int>(dst.shape().y())); + + for(int b = 0; b < num_batches; ++b) + { + for(int z = 0; z < in_d; ++z) + { + for(int y = 0; y < num_tiles_y; ++y) + { + for(int x = 0; x < num_tiles_x; ++x) + { + int xi = x * 2 - conv_info.pad_left(); + int yi = y * 2 - conv_info.pad_top(); + + // Get the 4x4 tile from the input tensor + get_tile(src, src_tile, Coordinates(xi, yi, z, b)); + + // Compute the transformation + matrix_multiply(matrix, src_tile, tmp_tile); + matrix_multiply(tmp_tile, matrix_transposed, dst_tile); + + // Store the 4x4 output tile across the 16 channels + for(int i = 0; i < 16; ++i) + { + int xo = z; + int yo = x + y * num_tiles_x; + dst[coords2index(dst.shape(), Coordinates(xo, yo, i, b))] = dst_tile[i]; + } + } + } + } + } +} +} // namespace + +template <typename T> +SimpleTensor<T> winograd_input_transform(const SimpleTensor<T> &src, const TensorShape &dst_shape, const PadStrideInfo &conv_info, const Size2D &kernel_dims) +{ + ARM_COMPUTE_ERROR_ON(kernel_dims.width != kernel_dims.height); + ARM_COMPUTE_ERROR_ON(src.data_layout() != DataLayout::NCHW); + + SimpleTensor<T> dst{ dst_shape, src.data_type() }; + + switch(kernel_dims.width) + { + case 3: + winograd_input_transform3x3(src, dst, conv_info); + break; + default: + ARM_COMPUTE_ERROR("Only 3x3 kernels are supported"); + } + + return dst; +} + +template SimpleTensor<float> winograd_input_transform(const SimpleTensor<float> &src, const TensorShape &dst_shape, const PadStrideInfo &conv_info, const Size2D &kernel_dims); +} // namespace reference +} // namespace validation +} // namespace test +} // namespace arm_compute diff --git a/tests/validation/reference/Winograd.h b/tests/validation/reference/Winograd.h new file mode 100644 index 0000000000..ed95239db3 --- /dev/null +++ b/tests/validation/reference/Winograd.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2018 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. + */ +#ifndef __ARM_COMPUTE_TEST_WINOGRAD_H__ +#define __ARM_COMPUTE_TEST_WINOGRAD_H__ + +#include "tests/SimpleTensor.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace reference +{ +template <typename T> +SimpleTensor<T> winograd_input_transform(const SimpleTensor<T> &src, const TensorShape &dst_shape, const PadStrideInfo &conv_info, const Size2D &kernel_dims); +} // namespace reference +} // namespace validation +} // namespace test +} // namespace arm_compute +#endif /* __ARM_COMPUTE_TEST_WINOGRAD_H__ */ |