From 1c8409d7ce90ea449437076574c98a4ea90d9368 Mon Sep 17 00:00:00 2001 From: Gian Marco Iodice Date: Wed, 6 Sep 2017 17:24:25 +0100 Subject: COMPMID-477 - Optimized CLDirectConvolution1x1 for Bifrost - Fixed bug in CLDirectConvolution3x3 Change-Id: Iaf34ef44f0b7bc02e66f3eb4452ff7a90ef83523 Reviewed-on: http://mpd-gerrit.cambridge.arm.com/86725 Tested-by: Kaizen Reviewed-by: Georgios Pinitas Reviewed-by: Moritz Pflanzer --- src/core/CL/CLKernelLibrary.cpp | 1 + src/core/CL/cl_kernels/direct_convolution1x1.cl | 122 ++++++++++++++++++++- src/core/CL/cl_kernels/direct_convolution3x3.cl | 8 +- .../CL/kernels/CLDirectConvolutionLayerKernel.cpp | 10 +- 4 files changed, 135 insertions(+), 6 deletions(-) (limited to 'src/core') diff --git a/src/core/CL/CLKernelLibrary.cpp b/src/core/CL/CLKernelLibrary.cpp index 03c9903d6d..dd549f035b 100644 --- a/src/core/CL/CLKernelLibrary.cpp +++ b/src/core/CL/CLKernelLibrary.cpp @@ -148,6 +148,7 @@ const std::map CLKernelLibrary::_kernel_program_map = { "derivative", "derivative.cl" }, { "dilate", "dilate.cl" }, { "direct_convolution1x1", "direct_convolution1x1.cl" }, + { "direct_convolution1x1_f32_bifrost", "direct_convolution1x1.cl" }, { "direct_convolution3x3", "direct_convolution3x3.cl" }, { "direct_convolution3x3_f32_bifrost", "direct_convolution3x3.cl" }, { "direct_convolution5x5", "direct_convolution5x5.cl" }, diff --git a/src/core/CL/cl_kernels/direct_convolution1x1.cl b/src/core/CL/cl_kernels/direct_convolution1x1.cl index 2aa999a80f..fb516ddd6e 100644 --- a/src/core/CL/cl_kernels/direct_convolution1x1.cl +++ b/src/core/CL/cl_kernels/direct_convolution1x1.cl @@ -41,6 +41,8 @@ MULQ_SAT_IMPL(qs32x8, qs32x8) #endif /* FIXED_POINT_POSITION */ +#if defined(DATA_TYPE) && defined(DATA_SIZE) && defined(STRIDE_X) && defined(WEIGHTS_DEPTH) + #if STRIDE_X == 3 #define INPUT_PIXEL_STR(data_size) extract_input_stride3_##data_size #define INPUT_PIXEL(data_size) INPUT_PIXEL_STR(data_size) @@ -165,7 +167,6 @@ inline VEC_DATA_TYPE(DATA_TYPE, 8) extract_input_stride3_8(__global const DATA_T * @param[in] biases_offset_first_element_in_bytes The offset of the first element in the biases tensor * @param[in] weights_stride_w Stride of the weights tensor in the 4th dimension */ -#if defined(DATA_TYPE) && defined(DATA_SIZE) && defined(STRIDE_X) && defined(WEIGHTS_DEPTH) __kernel void direct_convolution1x1( TENSOR3D_DECLARATION(src), TENSOR3D_DECLARATION(dst), @@ -207,3 +208,122 @@ __kernel void direct_convolution1x1( vstore8(CONVERT_SAT(pixels, VEC_DATA_TYPE(DATA_TYPE, 8)), 0, (__global DATA_TYPE *)dst.ptr); } #endif // defined(DATA_TYPE) && defined(DATA_SIZE) && defined(STRIDE_X) && defined(WEIGHTS_DEPTH) + +#if defined(WEIGHTS_DEPTH) + +#define CONVOLUTION1x1_BIFROST(acc, src, weight_value) \ + ({ \ + acc.s0 = mad(src.s0, weight_value, acc.s0); \ + acc.s1 = mad(src.s1, weight_value, acc.s1); \ + acc.s2 = mad(src.s2, weight_value, acc.s2); \ + acc.s3 = mad(src.s3, weight_value, acc.s3); \ + }) + +/** An optimized direct convolution 1x1 OpenCL kernel for Bifrost architectures when the data type is F32 + * + * @note This OpenCL kernel works only with stride_x and stride_y equal to 1 + * @note The third dimensions of the weights tensors must be passed at compile time using -DWEIGHTS_DEPTH + * @note In case biases, -DHAS_BIAS must to be passed at compile + * + * @param[in] src_ptr Pointer to the source tensor. Supported data types: F32 + * @param[in] src_stride_x Stride of the source tensor 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 tensor 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_stride_z Stride of the source tensor in Z dimension (in bytes) + * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes) + * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor + * @param[out] dst_ptr Pointer to the destination tensor. Supported data types: same 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 Z 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 Z processed per workitem(in bytes) + * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor + * @param[out] weights_ptr Pointer to the weights tensor. Supported data types: same as @p weights_ptr + * @param[in] weights_stride_x Stride of the weights tensor in X dimension (in bytes) + * @param[in] weights_step_x weights_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] weights_stride_y Stride of the weights tensor in Y dimension (in bytes) + * @param[in] weights_step_y weights_stride_y * number of elements along y processed per workitem(in bytes) + * @param[in] weights_stride_z Stride of the weights tensor in Z dimension (in bytes) + * @param[in] weights_step_z weights_stride_z * number of elements along Z processed per workitem(in bytes) + * @param[in] weights_offset_first_element_in_bytes The offset of the first element in the weights tensor + * @param[in] biases_ptr Pointer to the biases tensor. Same as @p src_ptr + * @param[in] biases_stride_x Stride of the biases tensor in X dimension (in bytes) + * @param[in] biases_step_x biases_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] biases_offset_first_element_in_bytes The offset of the first element in the biases tensor + * @param[in] weights_stride_w Stride of the weights tensor in the 4th dimension + */ +__kernel void direct_convolution1x1_f32_bifrost( + TENSOR3D_DECLARATION(src), + TENSOR3D_DECLARATION(dst), + TENSOR3D_DECLARATION(weights), +#ifdef HAS_BIAS + VECTOR_DECLARATION(biases), +#endif /* defined(HAS_BIAS) */ + unsigned int weights_stride_w) +{ + // Get the kernel index + const int kernel_index = get_global_id(2); + + Image src = CONVERT_TO_IMAGE_STRUCT(src); + Tensor3D dst = CONVERT_TO_TENSOR3D_STRUCT(dst); + + float4 acc0 = 0.0f; + float4 acc1 = 0.0f; + float4 acc2 = 0.0f; + float4 acc3 = 0.0f; + + __global uchar *weights_addr = (__global uchar *)(weights_ptr + weights_offset_first_element_in_bytes + kernel_index * weights_stride_w); + __global uchar *src_addr = (__global uchar *)offset(&src, 0, 0); + + for(ushort d = 0; d < (ushort)WEIGHTS_DEPTH; ++d) + { + // Load the weights + float weight = *((__global float *)weights_addr); + + // Load values from row0 of input tensor + float4 src0 = vload4(0, (__global float *)(src_addr + 0 * src_stride_y)); + float4 src1 = vload4(0, (__global float *)(src_addr + 1 * src_stride_y)); + float4 src2 = vload4(0, (__global float *)(src_addr + 2 * src_stride_y)); + float4 src3 = vload4(0, (__global float *)(src_addr + 3 * src_stride_y)); + + CONVOLUTION1x1_BIFROST(acc0, src0, weight); + CONVOLUTION1x1_BIFROST(acc1, src1, weight); + CONVOLUTION1x1_BIFROST(acc2, src2, weight); + CONVOLUTION1x1_BIFROST(acc3, src3, weight); + + src_addr += src_stride_z; + weights_addr += weights_stride_z; + } + +#ifdef HAS_BIAS + Vector biases = CONVERT_TO_VECTOR_STRUCT_NO_STEP(biases); + + float bias = (float) * ((__global float *)(vector_offset(&biases, kernel_index))); + + acc0.s0 += bias; + acc0.s1 += bias; + acc0.s2 += bias; + acc0.s3 += bias; + acc1.s0 += bias; + acc1.s1 += bias; + acc1.s2 += bias; + acc1.s3 += bias; + acc2.s0 += bias; + acc2.s1 += bias; + acc2.s2 += bias; + acc2.s3 += bias; + acc3.s0 += bias; + acc3.s1 += bias; + acc3.s2 += bias; + acc3.s3 += bias; +#endif /* defined(HAS_BIAS) */ + + vstore4(acc0, 0, (__global float *)(dst.ptr + 0 * dst_stride_y)); + vstore4(acc1, 0, (__global float *)(dst.ptr + 1 * dst_stride_y)); + vstore4(acc2, 0, (__global float *)(dst.ptr + 2 * dst_stride_y)); + vstore4(acc3, 0, (__global float *)(dst.ptr + 3 * dst_stride_y)); +} +#endif // defined(WEIGHTS_DEPTH) \ No newline at end of file diff --git a/src/core/CL/cl_kernels/direct_convolution3x3.cl b/src/core/CL/cl_kernels/direct_convolution3x3.cl index 26f24e187b..1a228dd74c 100644 --- a/src/core/CL/cl_kernels/direct_convolution3x3.cl +++ b/src/core/CL/cl_kernels/direct_convolution3x3.cl @@ -288,11 +288,11 @@ __kernel void direct_convolution3x3_f32_bifrost( #ifdef HAS_BIAS Vector biases = CONVERT_TO_VECTOR_STRUCT_NO_STEP(biases); - float4 bias = (float4) * ((__global float *)(vector_offset(&biases, kernel_index))); + float bias = (float) * ((__global float *)(vector_offset(&biases, kernel_index))); - pixels0 += bias; - pixels1 += bias; - pixels2 += bias; + pixels0 += (float4)bias; + pixels1 += (float4)bias; + pixels2 += (float4)bias; #endif /* defined(HAS_BIAS) */ vstore4(pixels0, 0, (__global float *)(dst.ptr + 0 * dst_stride_y)); diff --git a/src/core/CL/kernels/CLDirectConvolutionLayerKernel.cpp b/src/core/CL/kernels/CLDirectConvolutionLayerKernel.cpp index 265c5074c5..75e6d5e971 100644 --- a/src/core/CL/kernels/CLDirectConvolutionLayerKernel.cpp +++ b/src/core/CL/kernels/CLDirectConvolutionLayerKernel.cpp @@ -108,7 +108,7 @@ void CLDirectConvolutionLayerKernel::configure(const ICLTensor *input, const ICL options.emplace("-DHAS_BIAS"); } - if((gpu_target == GPUTarget::BIFROST) && (kernel_size <= 5) && (kernel_size != 1) && (_conv_stride_x == 1) && (_conv_stride_y == 1) && (input->info()->data_type() == DataType::F32)) + if((gpu_target == GPUTarget::BIFROST) && (kernel_size <= 5) && (_conv_stride_x == 1) && (_conv_stride_y == 1) && (input->info()->data_type() == DataType::F32)) { options.emplace("-DWEIGHTS_DEPTH=" + support::cpp11::to_string(_weights->info()->dimension(2))); @@ -125,6 +125,14 @@ void CLDirectConvolutionLayerKernel::configure(const ICLTensor *input, const ICL switch(kernel_size) { + case 1: + { + num_elems_read_per_iteration_x = 4; + num_elems_read_per_iteration_y = 4; + num_elems_written_per_iteration_x = 4; + num_elems_written_per_iteration_y = 4; + break; + } case 3: { num_elems_read_per_iteration_x = 6; -- cgit v1.2.1