From c42f28d45e9b990276d54880d2cee9c9ee675a41 Mon Sep 17 00:00:00 2001 From: Giorgio Arena Date: Thu, 26 Apr 2018 11:33:05 +0100 Subject: COMPMID-1048 Add NHWC data format support to Winograd input transform 4x4_3x3 https://confluence.arm.com/display/MLENG/Winograd+Input+Transform%3A+NCHW+vs+NHWC+on+OpenCL Change-Id: Iac35a54389266701b7d8f5434a7a37df85b7b187 Reviewed-on: https://eu-gerrit-1.euhpc.arm.com/133315 Reviewed-by: Gian Marco Iodice Tested-by: Jenkins --- src/core/CL/CLKernelLibrary.cpp | 1 + src/core/CL/cl_kernels/winograd.cl | 360 ++++++++++++++++++++- .../CL/kernels/CLWinogradInputTransformKernel.cpp | 70 +++- 3 files changed, 405 insertions(+), 26 deletions(-) (limited to 'src') diff --git a/src/core/CL/CLKernelLibrary.cpp b/src/core/CL/CLKernelLibrary.cpp index 0b2f414c71..75d4feb637 100644 --- a/src/core/CL/CLKernelLibrary.cpp +++ b/src/core/CL/CLKernelLibrary.cpp @@ -377,6 +377,7 @@ const std::map CLKernelLibrary::_kernel_program_map = { "winograd_input_transform_2x2_3x3_stepz1_nchw", "winograd.cl" }, { "winograd_input_transform_2x2_3x3_stepz2_nchw", "winograd.cl" }, { "winograd_input_transform_4x4_3x3_stepz1_nchw", "winograd.cl" }, + { "winograd_input_transform_4x4_3x3_stepz1_nhwc", "winograd.cl" }, { "winograd_output_transform_2x2_3x3_nchw", "winograd.cl" }, { "winograd_output_transform_4x4_3x3_nchw", "winograd.cl" }, { "winograd_output_transform_4x4_5x5_nchw", "winograd.cl" }, diff --git a/src/core/CL/cl_kernels/winograd.cl b/src/core/CL/cl_kernels/winograd.cl index c7ca8f6752..383a3a718a 100644 --- a/src/core/CL/cl_kernels/winograd.cl +++ b/src/core/CL/cl_kernels/winograd.cl @@ -25,7 +25,7 @@ #if defined(SRC_DIM_Z) -/** This OpenCL kernel performs Winograd filter transform 3x3 when the data format is NCHW and the output tile is 2x2 +/** This OpenCL kernel performs Winograd filter transform 3x3 when the data layout is NCHW and the output tile is 2x2 * * @note In order to correctly split the input tensor in batches, its dimension across the Z axis (channels for NCHW, height for NHWC) must be passed at compile time using -DSRC_DIM_Z: e.g. -DSRC_DIM_Z=64 * @@ -117,7 +117,7 @@ __kernel void winograd_filter_transform_2x2_3x3_nchw( *(__global float *)(dst_addr + 15 * dst_stride_z) = out3.s3; } -/** This OpenCL kernel performs Winograd filter transform 3x3 when the data format is NCHW and the output tile is 4x4 +/** This OpenCL kernel performs Winograd filter transform 3x3 when the data layout is NCHW and the output tile is 4x4 * * @note In order to correctly split the input tensor in batches, its dimension across the Z axis (channels for NCHW, height for NHWC) must be passed at compile time using -DSRC_DIM_Z: e.g. -DSRC_DIM_Z=64 * @@ -255,7 +255,7 @@ __kernel void winograd_filter_transform_4x4_3x3_nchw( *(__global float *)(dst_addr + 35 * dst_stride_z) = out5.s5; } -/** This OpenCL kernel performs Winograd filter transform 3x3 when the data format is NHWC and the output tile is 4x4 +/** This OpenCL kernel performs Winograd filter transform 3x3 when the data layout is NHWC and the output tile is 4x4 * * @note In order to correctly split the input tensor in batches, its dimension across the Z axis (channels for NCHW, height for NHWC) must be passed at compile time using -DSRC_DIM_Z: e.g. -DSRC_DIM_Z=64 * @@ -404,7 +404,7 @@ __kernel void winograd_filter_transform_4x4_3x3_nhwc( *(__global float *)(dst_addr + 34 * dst_stride_z) = out54; *(__global float *)(dst_addr + 35 * dst_stride_z) = out55; } -/** This OpenCL kernel performs Winograd filter transform 5x5 when the data format is NCHW and the output tile is 4x4 +/** This OpenCL kernel performs Winograd filter transform 5x5 when the data layout is NCHW and the output tile is 4x4 * * @note In order to correctly split the input tensor in batches, its dimension across the Z axis (channels for NCHW, height for NHWC) must be passed at compile time using -DSRC_DIM_Z: e.g. -DSRC_DIM_Z=64 * @@ -857,7 +857,7 @@ __kernel void winograd_input_transform_2x2_3x3_stepz2_nchw( vstore2(out33, 0, (__global float *)(dst_addr + 15 * dst_stride_z)); } -/** This OpenCL kernel computes the input transform when the output tile is 4x4, the filter size 3x3 and the data format is NCHW +/** This OpenCL kernel computes the input transform when the output tile is 4x4, the filter size 3x3 and the data layout is NCHW * * @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). @@ -1116,6 +1116,348 @@ __kernel void winograd_input_transform_4x4_3x3_stepz1_nchw( dst_addr += dst_plane_stride; } +#if defined(SRC_DIM_1) && defined(SRC_DIM_2) +/** This OpenCL kernel computes the input transform when the output tile is 4x4, the filter size 3x3 and the data layout is NHWC + * + * @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_4x4_3x3_stepz1_nhwc( + TENSOR3D_DECLARATION(src), + TENSOR3D_DECLARATION(dst)) +{ + int x = get_global_id(0); + int y = get_global_id(1); + int z = get_global_id(2); + + __global uchar *src_addr = src_ptr + src_offset_first_element_in_bytes + x * src_stride_x; + + // Clamp coordinates. This clamp is valid for all rows + int4 y_coord0 = (int4)(y * 4) + (int4)(0, 1, 2, 3) - (int4)PAD_LEFT; + int2 y_coord1 = (int2)(y * 4) + (int2)(4, 5) - (int2)PAD_LEFT; + y_coord0 = clamp(y_coord0, -1, SRC_DIM_1); + y_coord1 = clamp(y_coord1, -1, SRC_DIM_1); + + // Row4 + int z_coord = (z * 4) - PAD_TOP + 4; + + // If z < 0, set y to -1 + int4 valid_y0 = select(y_coord0, -1, (int4)z_coord < 0); + int2 valid_y1 = select(y_coord1, -1, (int2)z_coord < 0); + // If z >= SRC_DIM_2, set y to SRC_DIM_2 + valid_y0 = select(valid_y0, SRC_DIM_1, (int4)z_coord >= SRC_DIM_2); + valid_y1 = select(valid_y1, SRC_DIM_1, (int2)z_coord >= SRC_DIM_2); + + // Clamp z coordinate + z_coord = clamp(z_coord, 0, SRC_DIM_2 - 1); + + float d40 = *(__global float *)(src_addr + valid_y0.s0 * (int)src_stride_y + z_coord * src_stride_z); + float d41 = *(__global float *)(src_addr + valid_y0.s1 * (int)src_stride_y + z_coord * src_stride_z); + float d42 = *(__global float *)(src_addr + valid_y0.s2 * (int)src_stride_y + z_coord * src_stride_z); + float d43 = *(__global float *)(src_addr + valid_y0.s3 * (int)src_stride_y + z_coord * src_stride_z); + float d44 = *(__global float *)(src_addr + valid_y1.s0 * (int)src_stride_y + z_coord * src_stride_z); + float d45 = *(__global float *)(src_addr + valid_y1.s1 * (int)src_stride_y + z_coord * src_stride_z); + + float k0 = d44; + float k1 = d44; + float k2 = d44; + float k3 = d44; + float k4 = d44; + float k5 = (float)0.0f; + + k0 += 4.0f * d40 - 5.0f * d42; + k1 += -4.0f * d41 - 4.0f * d42 + d43; + k2 += 4.0f * d41 - 4.0f * d42 - d43; + k3 += -2.0f * d41 + 2.0f * d43 - d42; + k4 += 2.0f * d41 - 2.0f * d43 - d42; + k5 += 4.0f * d41 - 5.0f * d43 + d45; + + // Row0 + z_coord = (z * 4) - PAD_TOP + 0; + +#if PAD_TOP != 0 + valid_y0 = select(y_coord0, -1, (int4)z_coord < 0); + valid_y1 = select(y_coord1, -1, (int2)z_coord < 0); + valid_y0 = select(valid_y0, SRC_DIM_1, (int4)z_coord >= SRC_DIM_2); + valid_y1 = select(valid_y1, SRC_DIM_1, (int2)z_coord >= SRC_DIM_2); + z_coord = clamp(z_coord, 0, SRC_DIM_2 - 1); +#else // PAD_TOP != 0 + valid_y0 = y_coord0; + valid_y1 = y_coord1; +#endif // if PAD_TOP == 0, we cannot read out of bound + + float d00 = *(__global float *)(src_addr + valid_y0.s0 * (int)src_stride_y + z_coord * src_stride_z); + float d01 = *(__global float *)(src_addr + valid_y0.s1 * (int)src_stride_y + z_coord * src_stride_z); + float d02 = *(__global float *)(src_addr + valid_y0.s2 * (int)src_stride_y + z_coord * src_stride_z); + float d03 = *(__global float *)(src_addr + valid_y0.s3 * (int)src_stride_y + z_coord * src_stride_z); + float d04 = *(__global float *)(src_addr + valid_y1.s0 * (int)src_stride_y + z_coord * src_stride_z); + float d05 = *(__global float *)(src_addr + valid_y1.s1 * (int)src_stride_y + z_coord * src_stride_z); + + // Row2 + z_coord = (z * 4) - PAD_TOP + 2; + valid_y0 = select(y_coord0, -1, (int4)z_coord < 0); + valid_y1 = select(y_coord1, -1, (int2)z_coord < 0); + valid_y0 = select(valid_y0, SRC_DIM_1, (int4)z_coord >= SRC_DIM_2); + valid_y1 = select(valid_y1, SRC_DIM_1, (int2)z_coord >= SRC_DIM_2); + z_coord = clamp(z_coord, 0, SRC_DIM_2 - 1); + + float d20 = *(__global float *)(src_addr + valid_y0.s0 * (int)src_stride_y + z_coord * src_stride_z); + float d21 = *(__global float *)(src_addr + valid_y0.s1 * (int)src_stride_y + z_coord * src_stride_z); + float d22 = *(__global float *)(src_addr + valid_y0.s2 * (int)src_stride_y + z_coord * src_stride_z); + float d23 = *(__global float *)(src_addr + valid_y0.s3 * (int)src_stride_y + z_coord * src_stride_z); + float d24 = *(__global float *)(src_addr + valid_y1.s0 * (int)src_stride_y + z_coord * src_stride_z); + float d25 = *(__global float *)(src_addr + valid_y1.s1 * (int)src_stride_y + z_coord * src_stride_z); + + // Compute destination address + __global float *dst_addr = (__global float *)(dst_ptr + dst_offset_first_element_in_bytes + x * dst_stride_x + (y + z * (int)NUM_TILES_X) * dst_stride_y); + + uint dst_plane_stride = dst_stride_z / sizeof(float); + + float out0 = k0; + float out1 = k1; + float out2 = k2; + float out3 = k3; + float out4 = k4; + float out5 = k5; + float out6 = k0; + float out7 = k1; + float out8 = k2; + float out9 = k3; + float out10 = k4; + float out11 = k5; + float out12 = k0; + float out13 = k1; + float out14 = k2; + float out15 = k3; + float out16 = k4; + float out17 = k5; + float out18 = k0; + float out19 = k1; + float out20 = k2; + float out21 = k3; + float out22 = k4; + float out23 = k5; + float out24 = k0; + float out25 = k1; + float out26 = k2; + float out27 = k3; + float out28 = k4; + float out29 = k5; + + // Channels [0, 5]: [out00, out01, out02, out03, out04, out05] + out0 += 16.0f * d00 - 20.0f * d02 - 20.0f * d20 + 25.0f * d22 + 4.0f * d04 - 5.0f * d24; + out1 += -16.0f * d01 - 16.0f * d02 + 4.0f * d03 + 20.0f * d21 + 20.0f * d22 - 5.0f * d23 + 4.0f * d04 - 5.0f * d24; + out2 += 16.0f * d01 - 16.0f * d02 - 4.0f * d03 - 20.0f * d21 + 20.0f * d22 + 5.0f * d23 + 4.0f * d04 - 5.0f * d24; + out3 += -8.0f * d01 - 4.0f * d02 + 8.0f * d03 + 10.0f * d21 + 5.0f * d22 - 10.0f * d23 + 4.0f * d04 - 5.0f * d24; + out4 += 8.0f * d01 - 4.0f * d02 - 8.0f * d03 - 10.0f * d21 + 5.0f * d22 + 10.0f * d23 + 4.0f * d04 - 5.0f * d24; + out5 += 16.0f * d01 - 20.0f * d03 - 20.0f * d21 + 4.0f * d05 + 25.0f * d23 - 5.0f * d25; + + *((__global float *)dst_addr) = out0; + dst_addr += dst_plane_stride; + *((__global float *)dst_addr) = out1; + dst_addr += dst_plane_stride; + *((__global float *)dst_addr) = out2; + dst_addr += dst_plane_stride; + *((__global float *)dst_addr) = out3; + dst_addr += dst_plane_stride; + *((__global float *)dst_addr) = out4; + dst_addr += dst_plane_stride; + *((__global float *)dst_addr) = out5; + dst_addr += dst_plane_stride; + + // Row1 + z_coord = (z * 4) - PAD_TOP + 1; + // Row1 can never be out of bounds + valid_y0 = y_coord0; + valid_y1 = y_coord1; + + float d10 = *(__global float *)(src_addr + valid_y0.s0 * (int)src_stride_y + z_coord * src_stride_z); + float d11 = *(__global float *)(src_addr + valid_y0.s1 * (int)src_stride_y + z_coord * src_stride_z); + float d12 = *(__global float *)(src_addr + valid_y0.s2 * (int)src_stride_y + z_coord * src_stride_z); + float d13 = *(__global float *)(src_addr + valid_y0.s3 * (int)src_stride_y + z_coord * src_stride_z); + float d14 = *(__global float *)(src_addr + valid_y1.s0 * (int)src_stride_y + z_coord * src_stride_z); + float d15 = *(__global float *)(src_addr + valid_y1.s1 * (int)src_stride_y + z_coord * src_stride_z); + + // Row3 + z_coord = (z * 4) - PAD_TOP + 3; + valid_y0 = select(y_coord0, -1, (int4)z_coord < 0); + valid_y1 = select(y_coord1, -1, (int2)z_coord < 0); + valid_y0 = select(valid_y0, SRC_DIM_1, (int4)z_coord >= SRC_DIM_2); + valid_y1 = select(valid_y1, SRC_DIM_1, (int2)z_coord >= SRC_DIM_2); + z_coord = clamp(z_coord, 0, SRC_DIM_2 - 1); + z_coord = clamp(z_coord, 0, SRC_DIM_2 - 1); + + float d30 = *(__global float *)(src_addr + valid_y0.s0 * (int)src_stride_y + z_coord * src_stride_z); + float d31 = *(__global float *)(src_addr + valid_y0.s1 * (int)src_stride_y + z_coord * src_stride_z); + float d32 = *(__global float *)(src_addr + valid_y0.s2 * (int)src_stride_y + z_coord * src_stride_z); + float d33 = *(__global float *)(src_addr + valid_y0.s3 * (int)src_stride_y + z_coord * src_stride_z); + float d34 = *(__global float *)(src_addr + valid_y1.s0 * (int)src_stride_y + z_coord * src_stride_z); + float d35 = *(__global float *)(src_addr + valid_y1.s1 * (int)src_stride_y + z_coord * src_stride_z); + + // Compute common parts for the channels between [6, 29] + // Channels [6, 11]: [out10, out11, out12, out13, out14, out15] + // Channels [12, 17]: [out20, out21, out22, out23, out24, out25] + float part0 = -16.0f * d20 + 20.0f * d22 - 4.0f * d24; + float part1 = 16.0f * d10 - 20.0f * d12 + 4.0f * d14 - 4.0f * d30 + 5.0f * d32 - d34; + float part2 = 16.0f * d22 - 4.0f * d24; + float part3 = 16.0f * d21 - 4.0f * d23; + float part4 = 16.0f * d12 - 4.0f * d14 - 4.0f * d32 + d34; + float part5 = 16.0f * d11 - 4.0f * d13 - 4.0f * d31 + d33; + float part6 = 4.0f * d22 - 4.0f * d24; + float part7 = 8.0f * d11 - 8.0f * d13 - 2.0f * d31 + 2.0f * d33; + float part8 = 4.0f * d12 - 4.0f * d14 - d32 + d34; + float part9 = 8.0f * d21 - 8.0f * d23; + float part10 = -16.0f * d21 + 20.0f * d23 - 4.0f * d25; + float part11 = -16.0f * d11 + 20.0f * d13 - 4.0f * d15 + 4.0f * d31 - 5.0f * d33 + d35; + + // Channels [18, 23]: [out30, out31, out32, out33, out34, out35] + // Channels [24, 29]: [out40, out41, out42, out43, out44, out45] + float part12 = 8.0f * d10 - 10.0f * d12 + 2.0f * d14 - 8.0f * d30 + 10.0f * d32 - 2.0f * d34; + float part13 = part0 * 0.25f; // -4.0f * d20 + 5.0f * d22 - d24 + float part14 = part2 * 0.25f; // 4.0f * d22 - d24 + float part15 = 8.0f * d11 - 2.0f * d13 - 8.0f * d31 + 2.0f * d33; + float part16 = 8.0f * d12 - 2.0f * d14 - 8.0f * d32 + 2.0f * d34; + float part17 = part3 * 0.25f; // 4.0f * d21 - d23 + float part18 = part6 * 0.25f; // d22 - d24 + float part19 = 4.0f * d11 - 4.0f * d13 - 4.0f * d31 + 4.0f * d33; + float part20 = 2.0f * d12 - 2.0f * d14 - 2.0f * d32 + 2.0f * d34; + float part21 = part9 * 0.25f; // 2.0f * (d21 - d23) + float part22 = part10 * 0.25f; // - 4.0f * d21 + 5.0f * d23 - d25 + float part23 = part11 * 0.5f + 6.0f * d31 - 7.5f * d33 + 1.5f * d35; // - 8.0f * d11 + 10.0f * d13 - 2.0f * d15 + 8.0f * d31 - 10.0f * d33 + 2.0f * d35; + + out6 += part0 - part1; + out12 += part0 + part1; + out7 += part2 + part3 + part4 + part5; + out8 += part2 - part3 + part4 - part5; + out13 += part2 + part3 - part4 - part5; + out14 += part2 - part3 - part4 + part5; + out9 += part6 + part7 + part8 + part9; + out10 += part6 - part7 + part8 - part9; + out15 += part6 - part7 - part8 + part9; + out16 += part6 + part7 - part8 - part9; + out11 += part10 + part11; + out17 += part10 - part11; + + out18 += part13 - part12; + out24 += part13 + part12; + out19 += part14 + part15 + part16 + part17; + out20 += part14 - part15 + part16 - part17; + out25 += part14 - part15 - part16 + part17; + out26 += part14 + part15 - part16 - part17; + out21 += part18 + part19 + part20 + part21; + out22 += part18 - part19 + part20 - part21; + out27 += part18 - part19 - part20 + part21; + out28 += part18 + part19 - part20 - part21; + out23 += part22 + part23; + out29 += part22 - part23; + + *((__global float *)dst_addr) = out6; + dst_addr += dst_plane_stride; + *((__global float *)dst_addr) = out7; + dst_addr += dst_plane_stride; + *((__global float *)dst_addr) = out8; + dst_addr += dst_plane_stride; + *((__global float *)dst_addr) = out9; + dst_addr += dst_plane_stride; + *((__global float *)dst_addr) = out10; + dst_addr += dst_plane_stride; + *((__global float *)dst_addr) = out11; + dst_addr += dst_plane_stride; + *((__global float *)dst_addr) = out12; + dst_addr += dst_plane_stride; + *((__global float *)dst_addr) = out13; + dst_addr += dst_plane_stride; + *((__global float *)dst_addr) = out14; + dst_addr += dst_plane_stride; + *((__global float *)dst_addr) = out15; + dst_addr += dst_plane_stride; + *((__global float *)dst_addr) = out16; + dst_addr += dst_plane_stride; + *((__global float *)dst_addr) = out17; + dst_addr += dst_plane_stride; + + *((__global float *)dst_addr) = out18; + dst_addr += dst_plane_stride; + *((__global float *)dst_addr) = out19; + dst_addr += dst_plane_stride; + *((__global float *)dst_addr) = out20; + dst_addr += dst_plane_stride; + *((__global float *)dst_addr) = out21; + dst_addr += dst_plane_stride; + *((__global float *)dst_addr) = out22; + dst_addr += dst_plane_stride; + *((__global float *)dst_addr) = out23; + dst_addr += dst_plane_stride; + *((__global float *)dst_addr) = out24; + dst_addr += dst_plane_stride; + *((__global float *)dst_addr) = out25; + dst_addr += dst_plane_stride; + *((__global float *)dst_addr) = out26; + dst_addr += dst_plane_stride; + *((__global float *)dst_addr) = out27; + dst_addr += dst_plane_stride; + *((__global float *)dst_addr) = out28; + dst_addr += dst_plane_stride; + *((__global float *)dst_addr) = out29; + dst_addr += dst_plane_stride; + + // Row5 + z_coord = (z * 4) - PAD_TOP + 5; + valid_y0 = select(y_coord0, -1, (int4)z_coord < 0); + valid_y1 = select(y_coord1, -1, (int2)z_coord < 0); + valid_y0 = select(valid_y0, SRC_DIM_1, (int4)z_coord >= SRC_DIM_2); + valid_y1 = select(valid_y1, SRC_DIM_1, (int2)z_coord >= SRC_DIM_2); + z_coord = clamp(z_coord, 0, SRC_DIM_2 - 1); + z_coord = clamp(z_coord, 0, SRC_DIM_2 - 1); + + float d50 = *(__global float *)(src_addr + valid_y0.s0 * (int)src_stride_y + z_coord * src_stride_z); + float d51 = *(__global float *)(src_addr + valid_y0.s1 * (int)src_stride_y + z_coord * src_stride_z); + float d52 = *(__global float *)(src_addr + valid_y0.s2 * (int)src_stride_y + z_coord * src_stride_z); + float d53 = *(__global float *)(src_addr + valid_y0.s3 * (int)src_stride_y + z_coord * src_stride_z); + float d54 = *(__global float *)(src_addr + valid_y1.s0 * (int)src_stride_y + z_coord * src_stride_z); + float d55 = *(__global float *)(src_addr + valid_y1.s1 * (int)src_stride_y + z_coord * src_stride_z); + + // Channels [30, 35] + out0 = 16.0f * d10 - 20.0f * d12 - 20.0f * d30 + 25.0f * d32 + 4.0f * d50 - 5.0f * d52 + d54 + 4.0f * d14 - 5.0f * d34; + out1 = -16.0f * d11 - 16.0f * d12 + 4.0f * d13 + 20.0f * d31 + 20.0f * d32 - 5.0f * d33 - 4.0f * d51 - 4.0f * d52 + d53 + d54 + 4.0f * d14 - 5.0f * d34; + out2 = 16.0f * d11 - 16.0f * d12 - 4.0f * d13 - 20.0f * d31 + 20.0f * d32 + 5.0f * d33 + 4.0f * d51 - 4.0f * d52 - d53 + d54 + 4.0f * d14 - 5.0f * d34; + out3 = -8.0f * d11 - 4.0f * d12 + 8.0f * d13 + 10.0f * d31 - 10.0f * d33 + 5.0f * d32 - 2.0f * d51 + 2.0f * d53 - d52 + d54 + 4.0f * d14 - 5.0f * d34; + out4 = 8.0f * d11 - 4.0f * d12 - 8.0f * d13 - 10.0f * d31 + 5.0f * d32 + 10.0f * d33 + 2.0f * d51 - 2.0f * d53 - d52 + d54 + 4.0f * d14 - 5.0f * d34; + out5 = 16.0f * d11 - 20.0f * d13 + 4.0f * d15 - 20.0f * d31 + 25.0f * d33 - 5.0f * d35 + 4.0f * d51 - 5.0f * d53 + d55; + + *((__global float *)dst_addr) = out0; + dst_addr += dst_plane_stride; + *((__global float *)dst_addr) = out1; + dst_addr += dst_plane_stride; + *((__global float *)dst_addr) = out2; + dst_addr += dst_plane_stride; + *((__global float *)dst_addr) = out3; + dst_addr += dst_plane_stride; + *((__global float *)dst_addr) = out4; + dst_addr += dst_plane_stride; + *((__global float *)dst_addr) = out5; + dst_addr += dst_plane_stride; +} + +#endif /* defined(SRC_DIM_1) && defined(SRC_DIM_2) */ + #define OUTPUT_ROW_4x4_5x5(out, tmp, comm_fact) \ ({ \ comm_fact.s0 = tmp.s2 - 4.25f * tmp.s4 + tmp.s6; \ @@ -1287,7 +1629,7 @@ __kernel void winograd_input_transform_4x4_5x5_stepz1_nchw( #endif // defined(NUM_TILES_X) && defined(PAD_LEFT) && defined(PAD_TOP) #if defined(NUM_TILES_X) -/** This OpenCL kernel performs Winograd output transform when the output tile is 2x2, the filter size 3x3 and the data format is NCHW +/** This OpenCL kernel performs Winograd output transform when the output tile is 2x2, the filter size 3x3 and the data layout is NCHW * * @note The number of tiles along the X direction must be passed at compile time using -DNUM_TILES_X: e.g. -DNUM_TILES_X=16 * @@ -1389,7 +1731,7 @@ __kernel void winograd_output_transform_2x2_3x3_nchw( vstore2((float2)(out10, out11), 0, (__global float *)(dst_addr + 1 * dst_stride_y)); } -/** This OpenCL kernel performs Winograd output transform when the output tile is 4x4, the filter size 3x3 and the data format is NCHW +/** This OpenCL kernel performs Winograd output transform when the output tile is 4x4, the filter size 3x3 and the data layout is NCHW * * @note The number of tiles along the X direction must be passed at compile time using -DNUM_TILES_X: e.g. -DNUM_TILES_X=16 * @@ -1566,7 +1908,7 @@ __kernel void winograd_output_transform_4x4_3x3_nchw( vstore4((float4)(out30, out31, out32, out33), 0, (__global float *)(dst_addr + 3 * dst_stride_y)); } -/** This OpenCL kernel performs Winograd output transform when the output tile is 4x4, the filter size 3x3 and the data format is NHWC +/** This OpenCL kernel performs Winograd output transform when the output tile is 4x4, the filter size 3x3 and the data layout is NHWC * * @note The number of tiles along the X direction must be passed at compile time using -DNUM_TILES_X: e.g. -DNUM_TILES_X=16 * @@ -1774,7 +2116,7 @@ __kernel void winograd_output_transform_4x4_3x3_nhwc( col.s3 = comm_fact.s0 + 8.f * comm_fact.s1 + comm_fact.s2 + d7; \ }) -/** This OpenCL kernel performs Winograd output transform when the output tile is 4x4, the filter size 5x5 and the data format is NCHW +/** This OpenCL kernel performs Winograd output transform when the output tile is 4x4, the filter size 5x5 and the data layout is NCHW * * @note The number of tiles along the X direction must be passed at compile time using -DNUM_TILES_X: e.g. -DNUM_TILES_X=16 * diff --git a/src/core/CL/kernels/CLWinogradInputTransformKernel.cpp b/src/core/CL/kernels/CLWinogradInputTransformKernel.cpp index febd22b04e..e73ac7df76 100644 --- a/src/core/CL/kernels/CLWinogradInputTransformKernel.cpp +++ b/src/core/CL/kernels/CLWinogradInputTransformKernel.cpp @@ -40,13 +40,13 @@ namespace Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const WinogradInfo &winograd_info) { ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32); - ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() != DataLayout::NCHW); const PadStrideInfo conv_info = winograd_info.convolution_info; const Size2D output_tile_size = winograd_info.output_tile_size; const Size2D kernel_size = winograd_info.kernel_size; 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_size != Size2D(3U, 3U) && kernel_size != Size2D(5U, 5U), "Winograd input transform only supports 3x3 and 5x5 kernels"); + ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() == DataLayout::NHWC && (output_tile_size != Size2D(4U, 4U) || kernel_size != Size2D(3U, 3U))); ARM_COMPUTE_RETURN_ERROR_ON_MSG(kernel_size == Size2D(3U, 3U) && output_tile_size != Size2D(2U, 2U) && output_tile_size != Size2D(4U, 4U), "Winograd input transform only supports 2x2 or 4x4 output tile for 3x3 kernels"); @@ -75,12 +75,28 @@ std::pair validate_and_configure_window(ITensorInfo *input, ITen const Size2D output_tile_size = winograd_info.output_tile_size; const Size2D kernel_size = winograd_info.kernel_size; - const unsigned int num_elems_read_per_iteration_x = output_tile_size.width + kernel_size.width - 1; - const unsigned int num_elems_read_per_iteration_y = output_tile_size.height + kernel_size.height - 1; + unsigned int num_elems_read_per_iteration_x = 0; + unsigned int num_elems_read_per_iteration_y = 0; + unsigned int pad_left = 0; + unsigned int pad_top = 0; + + if(input->data_layout() == DataLayout::NCHW) + { + num_elems_read_per_iteration_x = output_tile_size.width + kernel_size.width - 1; + num_elems_read_per_iteration_y = output_tile_size.height + kernel_size.height - 1; + pad_left = conv_info.pad_left(); + pad_top = conv_info.pad_top(); + } + else + { + num_elems_read_per_iteration_x = 1; + num_elems_read_per_iteration_y = output_tile_size.width + kernel_size.width - 1; + pad_top = 1; + } 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); + AccessWindowRectangle input_access(input, -pad_left, -pad_top, num_elems_read_per_iteration_x, num_elems_read_per_iteration_y); bool window_changed = update_window_and_padding(win, input_access); @@ -108,17 +124,27 @@ void CLWinogradInputTransformKernel::configure(const ICLTensor *input, ICLTensor const Size2D output_tile_size = winograd_info.output_tile_size; const Size2D kernel_size = winograd_info.kernel_size; + const size_t idx_w = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::WIDTH); + const size_t idx_h = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::HEIGHT); + // Compute number of elements to process in the X and Y direction - const int num_elements_x = input->info()->dimension(0) - (kernel_size.width - 1) + conv_info.pad_left() + conv_info.pad_right(); - const int num_elements_y = input->info()->dimension(1) - (kernel_size.height - 1) + conv_info.pad_top() + conv_info.pad_bottom(); + const int num_elements_x = input->info()->dimension(idx_w) - (kernel_size.width - 1) + conv_info.pad_left() + conv_info.pad_right(); + const int num_elements_y = input->info()->dimension(idx_h) - (kernel_size.height - 1) + 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 % output_tile_size.width) == 0) ? 0u : static_cast(output_tile_size.width - 1); - const unsigned int extra_border_bottom = ((num_elements_y % output_tile_size.height) == 0) ? 0u : static_cast(output_tile_size.height - 1); + _input = input; + _output = output; + if(input->info()->data_layout() == DataLayout::NCHW) + { + // Check if we need to extend the right or bottom border + const unsigned int extra_border_right = ((num_elements_x % output_tile_size.width) == 0) ? 0u : static_cast(output_tile_size.width - 1); + const unsigned int extra_border_bottom = ((num_elements_y % output_tile_size.height) == 0) ? 0u : static_cast(output_tile_size.height - 1); - _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()); + _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()); + } + else + { + _border_size = BorderSize(1U, 0U, 1U, 0); + } _num_tiles_x = std::ceil(num_elements_x / static_cast(output_tile_size.width)); _num_tiles_y = std::ceil(num_elements_y / static_cast(output_tile_size.height)); @@ -134,6 +160,12 @@ void CLWinogradInputTransformKernel::configure(const ICLTensor *input, ICLTensor 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())); + if(input->info()->data_layout() == DataLayout::NHWC) + { + build_opts.add_option("-DSRC_DIM_1=" + support::cpp11::to_string(_input->info()->dimension(1))); + build_opts.add_option("-DSRC_DIM_2=" + support::cpp11::to_string(_input->info()->dimension(2))); + } + // Create kernel std::string kernel_name = "winograd_input_transform_" + output_tile_size.to_string() + "_" + kernel_size.to_string(); @@ -148,7 +180,7 @@ void CLWinogradInputTransformKernel::configure(const ICLTensor *input, ICLTensor // Append stepz and data layout kernel_name += "_stepz"; kernel_name += support::cpp11::to_string(_step_z); - kernel_name += "_nchw"; + kernel_name += "_" + lower_string(string_from_data_layout(input->info()->data_layout())); _kernel = static_cast(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options())); @@ -183,12 +215,16 @@ void CLWinogradInputTransformKernel::run(const Window &window, cl::CommandQueue ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); + const size_t idx_w = get_data_layout_dimension_index(_input->info()->data_layout(), DataLayoutDimension::WIDTH); + const size_t idx_h = get_data_layout_dimension_index(_input->info()->data_layout(), DataLayoutDimension::HEIGHT); + const size_t idx_c = get_data_layout_dimension_index(_input->info()->data_layout(), DataLayoutDimension::CHANNEL); + 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)); + slice.set(idx_w, Window::Dimension(0, _num_tiles_x, 1)); + slice.set(idx_h, 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)); + ARM_COMPUTE_ERROR_ON(((slice[idx_c].end() - slice[idx_c].start()) % _step_z) != 0); + slice.set(idx_c, Window::Dimension(slice[idx_c].start(), slice[idx_c].end(), _step_z)); do { -- cgit v1.2.1