From dfca60b8e8805966624c7c941f289e090e3d73bb Mon Sep 17 00:00:00 2001 From: Giorgio Arena Date: Wed, 31 Jan 2018 10:30:59 +0000 Subject: COMPMID-811 Add NHWC data format support for CL depthwise convolution QASYMM8 Change-Id: I89de432f3fbcba7abf9e1d4f8396a4334b4fa2c2 Reviewed-on: https://eu-gerrit-1.euhpc.arm.com/118324 Tested-by: Jenkins Reviewed-by: Gian Marco Iodice --- src/core/CL/CLKernelLibrary.cpp | 4 +- .../cl_kernels/depthwise_convolution_quantized.cl | 486 ++++++++++++++++++++- .../CLDepthwiseConvolutionLayer3x3Kernel.cpp | 308 ------------- .../CLDepthwiseConvolutionLayer3x3NCHWKernel.cpp | 306 +++++++++++++ .../CLDepthwiseConvolutionLayer3x3NHWCKernel.cpp | 254 +++++++++++ 5 files changed, 1045 insertions(+), 313 deletions(-) delete mode 100644 src/core/CL/kernels/CLDepthwiseConvolutionLayer3x3Kernel.cpp create mode 100644 src/core/CL/kernels/CLDepthwiseConvolutionLayer3x3NCHWKernel.cpp create mode 100644 src/core/CL/kernels/CLDepthwiseConvolutionLayer3x3NHWCKernel.cpp (limited to 'src/core/CL') diff --git a/src/core/CL/CLKernelLibrary.cpp b/src/core/CL/CLKernelLibrary.cpp index 8405754f52..0509f1e785 100644 --- a/src/core/CL/CLKernelLibrary.cpp +++ b/src/core/CL/CLKernelLibrary.cpp @@ -191,7 +191,9 @@ const std::map CLKernelLibrary::_kernel_program_map = { "deconvolution_upsample", "deconvolution_layer.cl" }, { "depthwise_convolution_3x3", "depthwise_convolution.cl" }, { "depthwise_convolution_3x3_f16", "depthwise_convolution.cl" }, - { "depthwise_convolution_3x3_quantized", "depthwise_convolution_quantized.cl" }, + { "depthwise_convolution_3x3_quantized_nchw", "depthwise_convolution_quantized.cl" }, + { "depthwise_convolution_3x3_quantized_nhwc_stride1", "depthwise_convolution_quantized.cl" }, + { "depthwise_convolution_3x3_quantized_nhwc_stride2", "depthwise_convolution_quantized.cl" }, { "depthwise_convolution_3x3_stridex1_stridey1_bifrost_f16", "depthwise_convolution.cl" }, { "depthwise_convolution_3x3_stridex2_stridey2_bifrost_f16", "depthwise_convolution.cl" }, { "depthwise_convolution_3x3_stridex1_stridey1_bifrost_f32", "depthwise_convolution.cl" }, diff --git a/src/core/CL/cl_kernels/depthwise_convolution_quantized.cl b/src/core/CL/cl_kernels/depthwise_convolution_quantized.cl index 21daee8230..635bc9d50b 100644 --- a/src/core/CL/cl_kernels/depthwise_convolution_quantized.cl +++ b/src/core/CL/cl_kernels/depthwise_convolution_quantized.cl @@ -24,17 +24,21 @@ #include "helpers_asymm.h" -#if defined(CONV_STRIDE_X) && defined(CONV_STRIDE_Y) && defined(WEIGHTS_OFFSET) && defined(INPUT_OFFSET) && defined(K_OFFSET) && defined(OUTPUT_OFFSET) && defined(OUTPUT_MULTIPLIER) && defined(OUTPUT_SHIFT) +#if defined(WEIGHTS_OFFSET) && defined(INPUT_OFFSET) && defined(K_OFFSET) && defined(OUTPUT_OFFSET) && defined(OUTPUT_MULTIPLIER) && defined(OUTPUT_SHIFT) #if defined(FUSED_ACTIVATION) #define DATA_TYPE uchar +#ifndef VEC_SIZE #define VEC_SIZE 8 +#endif /* VEC_SIZE */ #include "activation_layer_qa8.cl" #define ACTIVATION_FUNC(x) PERFORM_ACTIVATION_QA8(FUSED_ACTIVATION, x) #else /* defined(FUSED_ACTIVATION) */ #define ACTIVATION_FUNC(x) (x) #endif /* defined(FUSED_ACTIVATION) */ +#if defined(CONV_STRIDE_Y) && defined(CONV_STRIDE_X) + #if CONV_STRIDE_X > 3 #error "Stride X not supported" #endif /* CONV_STRIDE_X > 3 */ @@ -71,7 +75,7 @@ }) #endif /* CONV_STRIDE_X */ -/** This function computes the horizontal integral of the image and adds offsets. +/** This function computes the depthwise convolution quantized. * * @param[in] src_ptr Pointer to the source image. Supported data types: QASYMM8 * @param[in] src_stride_x Stride of the source image in X dimension (in bytes) @@ -103,7 +107,7 @@ * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases vector */ -__kernel void depthwise_convolution_3x3_quantized( +__kernel void depthwise_convolution_3x3_quantized_nchw( TENSOR3D_DECLARATION(src), TENSOR3D_DECLARATION(dst), TENSOR3D_DECLARATION(weights) @@ -244,4 +248,478 @@ __kernel void depthwise_convolution_3x3_quantized( #endif /* CONV_STRIDE_Y == 1 */ } -#endif /* defined(CONV_STRIDE_X) && defined(CONV_STRIDE_Y) && defined(WEIGHTS_OFFSET) && defined(INPUT_OFFSET) && defined(K_OFFSET) && defined(OUTPUT_OFFSET) && defined(OUTPUT_MULTIPLIER) && defined(OUTPUT_SHIFT) */ +#endif /* defined(CONV_STRIDE_Y) && defined(CONV_STRIDE_X) */ + +#if defined(VEC_SIZE) && defined(SRC_DEPTH) && defined(CONV_PAD_TOP) && defined(ROWS_READ) + +#define asymm_mult_by_quant_multiplier_less_than_one(x, y, z) ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(x, y, z, VEC_SIZE) + +#define VEC_INT VEC_DATA_TYPE(int, VEC_SIZE) +#define VEC_UCHAR VEC_DATA_TYPE(uchar, VEC_SIZE) + +#define BIFROST_MAD_4(acc, x, y) \ + ({ \ + acc.s0 += (ushort)x.s0 * (ushort)y.s0; \ + acc.s1 += (ushort)x.s1 * (ushort)y.s1; \ + acc.s2 += (ushort)x.s2 * (ushort)y.s2; \ + acc.s3 += (ushort)x.s3 * (ushort)y.s3; \ + }) + +#if WEIGHTS_OFFSET != 0 +#define BIFROST_MAD_ACC_4(acc, sum, x, y) \ + ({ \ + sum += CONVERT(x, VEC_INT); \ + BIFROST_MAD_4(acc, x, y); \ + }) +#else /* WEIGHTS_OFFSET != 0 */ +#define BIFROST_MAD_ACC_4(acc, sum, x, y) BIFROST_MAD_4(acc, x, y) +#endif /* WEIGHTS_OFFSET != 0 */ + +/** This function computes the depthwise convolution quantized. + * + * @param[in] src_ptr Pointer to the source image. Supported data types: QASYMM8 + * @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: QASYMM8 + * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes) + * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes) + * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes) + * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes) + * @param[in] dst_step_z dst_stride_z * number of elements along Y processed per workitem(in bytes) + * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor + * @param[in] weights_ptr Pointer to the weights tensor. Supported data types: QASYMM8 + * @param[in] weights_stride_x Stride of the weights tensor in X dimension (in bytes) + * @param[in] weights_step_x weights_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] weights_stride_y Stride of the weights tensor in Y dimension (in bytes) + * @param[in] weights_step_y weights_stride_y * number of elements along Y processed per workitem(in bytes) + * @param[in] weights_stride_z Stride of the weights tensor in Z dimension (in bytes) + * @param[in] weights_step_z weights_stride_z * number of elements along Y processed per workitem(in bytes) + * @param[in] weights_offset_first_element_in_bytes The offset of the first element in the weights tensor + * @param[in] biases_ptr (Optional) Pointer to the biases vector. Supported data types: QASYMM8 + * @param[in] biases_stride_x (Optional) Stride of the biases vector in X dimension (in bytes) + * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases vector + */ + +__kernel void depthwise_convolution_3x3_quantized_nhwc_stride1( + TENSOR3D_DECLARATION(src), + TENSOR3D_DECLARATION(dst), + TENSOR3D_DECLARATION(weights), +#if defined(HAS_BIAS) + VECTOR_DECLARATION(biases) +#endif /* defined(HAS_BIAS) */ +) +{ + Image dst = CONVERT_TENSOR3D_TO_IMAGE_STRUCT(dst); + Vector weights = CONVERT_TO_VECTOR_STRUCT(weights); +#if defined(HAS_BIAS) + Vector biases = CONVERT_TO_VECTOR_STRUCT(biases); + + VEC_INT bias_values = VLOAD(VEC_SIZE)(0, (__global int *)biases.ptr); +#endif /* defined(HAS_BIAS) */ + + __global uchar *first_elem = src_ptr + src_offset_first_element_in_bytes; + + const int z = get_global_id(2); + const int pad_offs = -ROWS_READ * src_stride_y; + const int src_offs0 = get_global_id(0) * src_step_x + get_global_id(1) * src_step_y + z * src_step_z - CONV_PAD_TOP * src_stride_z; + const int src_offs1 = src_offs0 + src_stride_z; + const int src_offs2 = src_offs1 + src_stride_z; + + const int cond_top = z - CONV_PAD_TOP < 0; + const int cond_bottom = z * (src_step_z / src_stride_z) + 2 >= SRC_DEPTH; + + __global uchar *src_addr0 = first_elem + select(src_offs0, pad_offs, cond_top); + __global uchar *src_addr1 = first_elem + src_offs1; + __global uchar *src_addr2 = first_elem + select(src_offs2, pad_offs, cond_bottom); + + VEC_INT sum_we = 0; + VEC_INT acc0 = 0, acc1 = 0, acc2 = 0, acc3 = 0; + VEC_INT sum0 = 0, sum1 = 0, sum2 = 0, sum3 = 0; + + // z == 0 + VEC_UCHAR w0, w1, w2; + w0 = VLOAD(VEC_SIZE)(0, weights.ptr + 0 * weights_stride_y); + w1 = VLOAD(VEC_SIZE)(0, weights.ptr + 1 * weights_stride_y); + w2 = VLOAD(VEC_SIZE)(0, weights.ptr + 2 * weights_stride_y); + +#if INPUT_OFFSET != 0 + sum_we += CONVERT(w0, VEC_INT) + CONVERT(w1, VEC_INT) + CONVERT(w2, VEC_INT); +#endif /* INPUT_OFFSET != 0 */ + + VEC_UCHAR values = VLOAD(VEC_SIZE)(0, src_addr0); + BIFROST_MAD_ACC_4(acc0, sum0, values, w0); + + src_addr0 += src_stride_y; + values = VLOAD(VEC_SIZE)(0, src_addr0); + BIFROST_MAD_ACC_4(acc0, sum0, values, w1); + BIFROST_MAD_ACC_4(acc1, sum1, values, w0); + + src_addr0 += src_stride_y; + values = VLOAD(VEC_SIZE)(0, src_addr0); + BIFROST_MAD_ACC_4(acc0, sum0, values, w2); + BIFROST_MAD_ACC_4(acc1, sum1, values, w1); + BIFROST_MAD_ACC_4(acc2, sum2, values, w0); + + src_addr0 += src_stride_y; + values = VLOAD(VEC_SIZE)(0, src_addr0); + BIFROST_MAD_ACC_4(acc1, sum1, values, w2); + BIFROST_MAD_ACC_4(acc2, sum2, values, w1); + BIFROST_MAD_ACC_4(acc3, sum3, values, w0); + + src_addr0 += src_stride_y; + values = VLOAD(VEC_SIZE)(0, src_addr0); + BIFROST_MAD_ACC_4(acc2, sum2, values, w2); + BIFROST_MAD_ACC_4(acc3, sum3, values, w1); + + src_addr0 += src_stride_y; + values = VLOAD(VEC_SIZE)(0, src_addr0); + BIFROST_MAD_ACC_4(acc3, sum3, values, w2); + + weights.ptr += weights_stride_z; + + // z == 1 + w0 = VLOAD(VEC_SIZE)(0, weights.ptr + 0 * weights_stride_y); + w1 = VLOAD(VEC_SIZE)(0, weights.ptr + 1 * weights_stride_y); + w2 = VLOAD(VEC_SIZE)(0, weights.ptr + 2 * weights_stride_y); + +#if INPUT_OFFSET != 0 + sum_we += CONVERT(w0, VEC_INT) + CONVERT(w1, VEC_INT) + CONVERT(w2, VEC_INT); +#endif /* INPUT_OFFSET != 0 */ + + values = VLOAD(VEC_SIZE)(0, src_addr1); + BIFROST_MAD_ACC_4(acc0, sum0, values, w0); + + src_addr1 += src_stride_y; + values = VLOAD(VEC_SIZE)(0, src_addr1); + BIFROST_MAD_ACC_4(acc0, sum0, values, w1); + BIFROST_MAD_ACC_4(acc1, sum1, values, w0); + + src_addr1 += src_stride_y; + values = VLOAD(VEC_SIZE)(0, src_addr1); + BIFROST_MAD_ACC_4(acc0, sum0, values, w2); + BIFROST_MAD_ACC_4(acc1, sum1, values, w1); + BIFROST_MAD_ACC_4(acc2, sum2, values, w0); + + src_addr1 += src_stride_y; + values = VLOAD(VEC_SIZE)(0, src_addr1); + BIFROST_MAD_ACC_4(acc1, sum1, values, w2); + BIFROST_MAD_ACC_4(acc2, sum2, values, w1); + BIFROST_MAD_ACC_4(acc3, sum3, values, w0); + + src_addr1 += src_stride_y; + values = VLOAD(VEC_SIZE)(0, src_addr1); + BIFROST_MAD_ACC_4(acc2, sum2, values, w2); + BIFROST_MAD_ACC_4(acc3, sum3, values, w1); + + src_addr1 += src_stride_y; + values = VLOAD(VEC_SIZE)(0, src_addr1); + BIFROST_MAD_ACC_4(acc3, sum3, values, w2); + + weights.ptr += weights_stride_z; + + // z == 2 + w0 = VLOAD(VEC_SIZE)(0, weights.ptr + 0 * weights_stride_y); + w1 = VLOAD(VEC_SIZE)(0, weights.ptr + 1 * weights_stride_y); + w2 = VLOAD(VEC_SIZE)(0, weights.ptr + 2 * weights_stride_y); + +#if INPUT_OFFSET != 0 + sum_we += CONVERT(w0, VEC_INT) + CONVERT(w1, VEC_INT) + CONVERT(w2, VEC_INT); +#endif /* INPUT_OFFSET != 0 */ + + values = VLOAD(VEC_SIZE)(0, src_addr2); + BIFROST_MAD_ACC_4(acc0, sum0, values, w0); + + src_addr2 += src_stride_y; + values = VLOAD(VEC_SIZE)(0, src_addr2); + BIFROST_MAD_ACC_4(acc0, sum0, values, w1); + BIFROST_MAD_ACC_4(acc1, sum1, values, w0); + + src_addr2 += src_stride_y; + values = VLOAD(VEC_SIZE)(0, src_addr2); + BIFROST_MAD_ACC_4(acc0, sum0, values, w2); + BIFROST_MAD_ACC_4(acc1, sum1, values, w1); + BIFROST_MAD_ACC_4(acc2, sum2, values, w0); + + src_addr2 += src_stride_y; + values = VLOAD(VEC_SIZE)(0, src_addr2); + BIFROST_MAD_ACC_4(acc1, sum1, values, w2); + BIFROST_MAD_ACC_4(acc2, sum2, values, w1); + BIFROST_MAD_ACC_4(acc3, sum3, values, w0); + + src_addr2 += src_stride_y; + values = VLOAD(VEC_SIZE)(0, src_addr2); + BIFROST_MAD_ACC_4(acc2, sum2, values, w2); + BIFROST_MAD_ACC_4(acc3, sum3, values, w1); + + src_addr2 += src_stride_y; + values = VLOAD(VEC_SIZE)(0, src_addr2); + BIFROST_MAD_ACC_4(acc3, sum3, values, w2); + +#if defined(HAS_BIAS) + acc0 += bias_values; + acc1 += bias_values; + acc2 += bias_values; + acc3 += bias_values; +#endif /* defined(HAS_BIAS) */ + +#if WEIGHTS_OFFSET != 0 + acc0 += WEIGHTS_OFFSET * sum0; + acc1 += WEIGHTS_OFFSET * sum1; + acc2 += WEIGHTS_OFFSET * sum2; + acc3 += WEIGHTS_OFFSET * sum3; +#endif /* WEIGHTS_OFFSET != 0 */ + +#if INPUT_OFFSET != 0 + VEC_INT offs = INPUT_OFFSET * sum_we; + + acc0 += offs; + acc1 += offs; + acc2 += offs; + acc3 += offs; +#endif /* INPUT_OFFSET != 0 */ + +#if K_OFFSET != 0 + acc0 += (VEC_INT)K_OFFSET; + acc1 += (VEC_INT)K_OFFSET; + acc2 += (VEC_INT)K_OFFSET; + acc3 += (VEC_INT)K_OFFSET; +#endif /* K_OFFSET != 0 */ + + acc0 = asymm_mult_by_quant_multiplier_less_than_one(acc0, OUTPUT_MULTIPLIER, OUTPUT_SHIFT); + acc1 = asymm_mult_by_quant_multiplier_less_than_one(acc1, OUTPUT_MULTIPLIER, OUTPUT_SHIFT); + acc2 = asymm_mult_by_quant_multiplier_less_than_one(acc2, OUTPUT_MULTIPLIER, OUTPUT_SHIFT); + acc3 = asymm_mult_by_quant_multiplier_less_than_one(acc3, OUTPUT_MULTIPLIER, OUTPUT_SHIFT); + + acc0 += (VEC_INT)OUTPUT_OFFSET; + acc1 += (VEC_INT)OUTPUT_OFFSET; + acc2 += (VEC_INT)OUTPUT_OFFSET; + acc3 += (VEC_INT)OUTPUT_OFFSET; + + VEC_UCHAR res0 = CONVERT_SAT(acc0, VEC_UCHAR); + VEC_UCHAR res1 = CONVERT_SAT(acc1, VEC_UCHAR); + VEC_UCHAR res2 = CONVERT_SAT(acc2, VEC_UCHAR); + VEC_UCHAR res3 = CONVERT_SAT(acc3, VEC_UCHAR); + + res0 = CLAMP(res0, (VEC_UCHAR)0, (VEC_UCHAR)255); + res1 = CLAMP(res1, (VEC_UCHAR)0, (VEC_UCHAR)255); + res2 = CLAMP(res2, (VEC_UCHAR)0, (VEC_UCHAR)255); + res3 = CLAMP(res3, (VEC_UCHAR)0, (VEC_UCHAR)255); + + VSTORE(VEC_SIZE) + (res0, 0, dst.ptr + 0 * dst_stride_y); + VSTORE(VEC_SIZE) + (res1, 0, dst.ptr + 1 * dst_stride_y); + VSTORE(VEC_SIZE) + (res2, 0, dst.ptr + 2 * dst_stride_y); + VSTORE(VEC_SIZE) + (res3, 0, dst.ptr + 3 * dst_stride_y); +} + +/** This function computes the depthwise convolution quantized. + * + * @param[in] src_ptr Pointer to the source image. Supported data types: QASYMM8 + * @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: QASYMM8 + * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes) + * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes) + * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes) + * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes) + * @param[in] dst_step_z dst_stride_z * number of elements along Y processed per workitem(in bytes) + * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor + * @param[in] weights_ptr Pointer to the weights tensor. Supported data types: QASYMM8 + * @param[in] weights_stride_x Stride of the weights tensor in X dimension (in bytes) + * @param[in] weights_step_x weights_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] weights_stride_y Stride of the weights tensor in Y dimension (in bytes) + * @param[in] weights_step_y weights_stride_y * number of elements along Y processed per workitem(in bytes) + * @param[in] weights_stride_z Stride of the weights tensor in Z dimension (in bytes) + * @param[in] weights_step_z weights_stride_z * number of elements along Y processed per workitem(in bytes) + * @param[in] weights_offset_first_element_in_bytes The offset of the first element in the weights tensor + * @param[in] biases_ptr (Optional) Pointer to the biases vector. Supported data types: QASYMM8 + * @param[in] biases_stride_x (Optional) Stride of the biases vector in X dimension (in bytes) + * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases vector + */ + +__kernel void depthwise_convolution_3x3_quantized_nhwc_stride2( + TENSOR3D_DECLARATION(src), + TENSOR3D_DECLARATION(dst), + TENSOR3D_DECLARATION(weights), +#if defined(HAS_BIAS) + VECTOR_DECLARATION(biases) +#endif /* defined(HAS_BIAS) */ +) +{ + Image dst = CONVERT_TENSOR3D_TO_IMAGE_STRUCT(dst); + Vector weights = CONVERT_TO_VECTOR_STRUCT(weights); +#if defined(HAS_BIAS) + Vector biases = CONVERT_TO_VECTOR_STRUCT(biases); + + VEC_INT bias_values = VLOAD(VEC_SIZE)(0, (__global int *)biases.ptr); +#endif /* defined(HAS_BIAS) */ + + __global uchar *first_elem = src_ptr + src_offset_first_element_in_bytes; + + const int z = get_global_id(2); + const int pad_offs = -ROWS_READ * src_stride_y; + const int src_offs0 = get_global_id(0) * src_step_x + get_global_id(1) * src_step_y + z * src_step_z - CONV_PAD_TOP * src_stride_z; + const int src_offs1 = src_offs0 + src_stride_z; + const int src_offs2 = src_offs1 + src_stride_z; + + const int cond_top = z - CONV_PAD_TOP < 0; + const int cond_bottom = z * (src_step_z / src_stride_z) + 2 >= SRC_DEPTH; + ; + + __global uchar *src_addr0 = first_elem + select(src_offs0, pad_offs, cond_top); + __global uchar *src_addr1 = first_elem + src_offs1; + __global uchar *src_addr2 = first_elem + select(src_offs2, pad_offs, cond_bottom); + + VEC_INT sum_we = 0; + VEC_INT acc0 = 0, acc2 = 0; + VEC_INT sum0 = 0, sum2 = 0; + + // z == 0 + VEC_UCHAR w0, w1, w2; + w0 = VLOAD(VEC_SIZE)(0, weights.ptr + 0 * weights_stride_y); + w1 = VLOAD(VEC_SIZE)(0, weights.ptr + 1 * weights_stride_y); + w2 = VLOAD(VEC_SIZE)(0, weights.ptr + 2 * weights_stride_y); + +#if INPUT_OFFSET != 0 + sum_we += CONVERT(w0, VEC_INT) + CONVERT(w1, VEC_INT) + CONVERT(w2, VEC_INT); +#endif /* INPUT_OFFSET != 0 */ + + VEC_UCHAR values = VLOAD(VEC_SIZE)(0, src_addr0); + BIFROST_MAD_ACC_4(acc0, sum0, values, w0); + + src_addr0 += src_stride_y; + values = VLOAD(VEC_SIZE)(0, src_addr0); + BIFROST_MAD_ACC_4(acc0, sum0, values, w1); + + src_addr0 += src_stride_y; + values = VLOAD(VEC_SIZE)(0, src_addr0); + BIFROST_MAD_ACC_4(acc0, sum0, values, w2); + BIFROST_MAD_ACC_4(acc2, sum2, values, w0); + + src_addr0 += src_stride_y; + values = VLOAD(VEC_SIZE)(0, src_addr0); + BIFROST_MAD_ACC_4(acc2, sum2, values, w1); + + src_addr0 += src_stride_y; + values = VLOAD(VEC_SIZE)(0, src_addr0); + BIFROST_MAD_ACC_4(acc2, sum2, values, w2); + + weights.ptr += weights_stride_z; + + // z == 1 + w0 = VLOAD(VEC_SIZE)(0, weights.ptr + 0 * weights_stride_y); + w1 = VLOAD(VEC_SIZE)(0, weights.ptr + 1 * weights_stride_y); + w2 = VLOAD(VEC_SIZE)(0, weights.ptr + 2 * weights_stride_y); + +#if INPUT_OFFSET != 0 + sum_we += CONVERT(w0, VEC_INT) + CONVERT(w1, VEC_INT) + CONVERT(w2, VEC_INT); +#endif /* INPUT_OFFSET != 0 */ + + values = VLOAD(VEC_SIZE)(0, src_addr1); + BIFROST_MAD_ACC_4(acc0, sum0, values, w0); + + src_addr1 += src_stride_y; + values = VLOAD(VEC_SIZE)(0, src_addr1); + BIFROST_MAD_ACC_4(acc0, sum0, values, w1); + + src_addr1 += src_stride_y; + values = VLOAD(VEC_SIZE)(0, src_addr1); + BIFROST_MAD_ACC_4(acc0, sum0, values, w2); + BIFROST_MAD_ACC_4(acc2, sum2, values, w0); + + src_addr1 += src_stride_y; + values = VLOAD(VEC_SIZE)(0, src_addr1); + BIFROST_MAD_ACC_4(acc2, sum2, values, w1); + + src_addr1 += src_stride_y; + values = VLOAD(VEC_SIZE)(0, src_addr1); + BIFROST_MAD_ACC_4(acc2, sum2, values, w2); + + weights.ptr += weights_stride_z; + + // z == 2 + w0 = VLOAD(VEC_SIZE)(0, weights.ptr + 0 * weights_stride_y); + w1 = VLOAD(VEC_SIZE)(0, weights.ptr + 1 * weights_stride_y); + w2 = VLOAD(VEC_SIZE)(0, weights.ptr + 2 * weights_stride_y); + +#if INPUT_OFFSET != 0 + sum_we += CONVERT(w0, VEC_INT) + CONVERT(w1, VEC_INT) + CONVERT(w2, VEC_INT); +#endif /* INPUT_OFFSET != 0 */ + + values = VLOAD(VEC_SIZE)(0, src_addr2); + BIFROST_MAD_ACC_4(acc0, sum0, values, w0); + + src_addr2 += src_stride_y; + values = VLOAD(VEC_SIZE)(0, src_addr2); + BIFROST_MAD_ACC_4(acc0, sum0, values, w1); + + src_addr2 += src_stride_y; + values = VLOAD(VEC_SIZE)(0, src_addr2); + BIFROST_MAD_ACC_4(acc0, sum0, values, w2); + BIFROST_MAD_ACC_4(acc2, sum2, values, w0); + + src_addr2 += src_stride_y; + values = VLOAD(VEC_SIZE)(0, src_addr2); + BIFROST_MAD_ACC_4(acc2, sum2, values, w1); + + src_addr2 += src_stride_y; + values = VLOAD(VEC_SIZE)(0, src_addr2); + BIFROST_MAD_ACC_4(acc2, sum2, values, w2); + +#if defined(HAS_BIAS) + acc0 += bias_values; + acc2 += bias_values; +#endif /* defined(HAS_BIAS) */ + +#if WEIGHTS_OFFSET != 0 + acc0 += WEIGHTS_OFFSET * sum0; + acc2 += WEIGHTS_OFFSET * sum2; +#endif /* WEIGHTS_OFFSET != 0 */ + +#if INPUT_OFFSET != 0 + VEC_INT offs = INPUT_OFFSET * sum_we; + + acc0 += offs; + acc2 += offs; +#endif /* INPUT_OFFSET != 0 */ + +#if K_OFFSET != 0 + acc0 += (VEC_INT)K_OFFSET; + acc2 += (VEC_INT)K_OFFSET; +#endif /* K_OFFSET != 0 */ + + acc0 = asymm_mult_by_quant_multiplier_less_than_one(acc0, OUTPUT_MULTIPLIER, OUTPUT_SHIFT); + acc2 = asymm_mult_by_quant_multiplier_less_than_one(acc2, OUTPUT_MULTIPLIER, OUTPUT_SHIFT); + acc0 += (VEC_INT)OUTPUT_OFFSET; + acc2 += (VEC_INT)OUTPUT_OFFSET; + VEC_UCHAR res0 = CONVERT_SAT(acc0, VEC_UCHAR); + VEC_UCHAR res2 = CONVERT_SAT(acc2, VEC_UCHAR); + res0 = CLAMP(res0, (VEC_UCHAR)0, (VEC_UCHAR)255); + res2 = CLAMP(res2, (VEC_UCHAR)0, (VEC_UCHAR)255); + + VSTORE(VEC_SIZE) + (res0, 0, dst.ptr + 0 * dst_stride_y); + VSTORE(VEC_SIZE) + (res2, 0, dst.ptr + 1 * dst_stride_y); +} + +#endif /* defined(VEC_SIZE) && defined(SRC_DEPTH) && defined(CONV_PAD_TOP) && defined(ROWS_READ) */ + +#endif /* defined(WEIGHTS_OFFSET) && defined(INPUT_OFFSET) && defined(K_OFFSET) && defined(OUTPUT_OFFSET) && defined(OUTPUT_MULTIPLIER) && defined(OUTPUT_SHIFT) */ diff --git a/src/core/CL/kernels/CLDepthwiseConvolutionLayer3x3Kernel.cpp b/src/core/CL/kernels/CLDepthwiseConvolutionLayer3x3Kernel.cpp deleted file mode 100644 index c9fe1cfc0b..0000000000 --- a/src/core/CL/kernels/CLDepthwiseConvolutionLayer3x3Kernel.cpp +++ /dev/null @@ -1,308 +0,0 @@ -/* - * Copyright (c) 2017-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/CLDepthwiseConvolutionLayer3x3Kernel.h" - -#include "arm_compute/core/AccessWindowStatic.h" -#include "arm_compute/core/CL/CLHelpers.h" -#include "arm_compute/core/CL/CLKernelLibrary.h" -#include "arm_compute/core/CL/ICLKernel.h" -#include "arm_compute/core/CL/ICLTensor.h" -#include "arm_compute/core/Error.h" -#include "arm_compute/core/Helpers.h" -#include "arm_compute/core/TensorInfo.h" -#include "arm_compute/core/Types.h" -#include "arm_compute/core/Utils.h" -#include "arm_compute/core/utils/misc/ShapeCalculator.h" -#include "arm_compute/core/utils/quantization/AsymmHelpers.h" - -using namespace arm_compute; -using namespace arm_compute::misc::shape_calculator; - -CLDepthwiseConvolutionLayer3x3Kernel::CLDepthwiseConvolutionLayer3x3Kernel() - : _border_size(0), _input(), _output(), _weights(), _biases(), _conv_stride_x(0), _conv_stride_y(0), _conv_pad_left(0), _conv_pad_top(0) -{ -} - -BorderSize CLDepthwiseConvolutionLayer3x3Kernel::border_size() const -{ - return _border_size; -} - -void CLDepthwiseConvolutionLayer3x3Kernel::configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, - ActivationLayerInfo act_info) -{ - ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32); - ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights); - ARM_COMPUTE_ERROR_ON(weights->info()->dimension(0) != 3 || weights->info()->dimension(1) != 3); - - bool is_qasymm = is_data_type_quantized_asymmetric(input->info()->data_type()); - - if(biases != nullptr) - { - if(is_qasymm) - { - ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32); - } - else - { - ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases); - } - ARM_COMPUTE_ERROR_ON(biases->info()->dimension(0) != weights->info()->dimension(2)); - ARM_COMPUTE_ERROR_ON(biases->info()->num_dimensions() > 1); - } - - // Get convolved dimensions - const TensorShape output_shape = compute_depthwise_convolution_shape(*input->info(), *weights->info(), conv_info); - - // Output auto inizialitation if not yet initialized - auto_init_if_empty(*output->info(), - output_shape, - 1, - input->info()->data_type(), - input->info()->fixed_point_position(), - input->info()->quantization_info()); - - ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->info()->tensor_shape(), output_shape); - - _input = input; - _output = output; - _weights = weights; - _biases = biases; - _conv_stride_x = conv_info.stride().first; - _conv_stride_y = conv_info.stride().second; - _conv_pad_left = conv_info.pad_left(); - _conv_pad_top = conv_info.pad_top(); - _border_size = BorderSize(_conv_pad_top, conv_info.pad_right(), conv_info.pad_bottom(), _conv_pad_left); - - // Set build options - ARM_COMPUTE_ERROR_ON(_conv_stride_x < 1 || _conv_stride_x > 3); - CLBuildOptions build_opts; - build_opts.add_option("-DCONV_STRIDE_X=" + support::cpp11::to_string(_conv_stride_x)); - build_opts.add_option_if(_biases != nullptr, "-DHAS_BIAS"); - - if(is_qasymm) - { - float multiplier = _input->info()->quantization_info().scale * _weights->info()->quantization_info().scale / _output->info()->quantization_info().scale; - int output_multiplier = 0; - int output_shift = 0; - quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift); - - build_opts.add_option("-DCONV_STRIDE_Y=" + support::cpp11::to_string(_conv_stride_y)); - build_opts.add_option("-DINPUT_OFFSET=" + support::cpp11::to_string(-_input->info()->quantization_info().offset)); - build_opts.add_option("-DWEIGHTS_OFFSET=" + support::cpp11::to_string(-_weights->info()->quantization_info().offset)); - build_opts.add_option("-DOUTPUT_OFFSET=" + support::cpp11::to_string(_output->info()->quantization_info().offset)); - build_opts.add_option("-DK_OFFSET=" + support::cpp11::to_string(9 * input->info()->quantization_info().offset * weights->info()->quantization_info().offset)); - build_opts.add_option("-DOUTPUT_MULTIPLIER=" + support::cpp11::to_string(output_multiplier)); - build_opts.add_option("-DOUTPUT_SHIFT=" + support::cpp11::to_string(output_shift)); - - if(act_info.enabled()) - { - const int a_val = input->info()->quantization_info().quantize(act_info.a(), RoundingPolicy::TO_NEAREST_UP); - const int b_val = input->info()->quantization_info().quantize(act_info.b(), RoundingPolicy::TO_NEAREST_UP); - const int o1 = input->info()->quantization_info().offset; - - build_opts.add_option("-DFUSED_ACTIVATION=" + lower_string(string_from_activation_func(act_info.activation()))); - build_opts.add_option("-DA_VAL=" + support::cpp11::to_string(a_val)); - build_opts.add_option("-DB_VAL=" + support::cpp11::to_string(b_val)); - build_opts.add_option("-DCONST_0=" + support::cpp11::to_string(o1)); - - if(output != nullptr) - { - const float s1 = input->info()->quantization_info().scale; - const float s2 = output->info()->quantization_info().scale; - const int o2 = output->info()->quantization_info().offset; - - if(o1 != o2 || s1 != s2) - { - build_opts.add_option("-DS1_VAL=" + float_to_string_with_full_precision(s1)); - build_opts.add_option("-DS2_VAL=" + float_to_string_with_full_precision(s2)); - build_opts.add_option("-DO1_VAL=" + support::cpp11::to_string(o1)); - build_opts.add_option("-DO2_VAL=" + support::cpp11::to_string(o2)); - } - } - } - } - - // Configure the local work size for Bifrost with a value obtained - // via exhaustive autotuning for the MobileNets tensor shapes. - const GPUTarget gpu_target = get_target(); - const bool is_bifrost = gpu_target_is_in(gpu_target, GPUTarget::G71, GPUTarget::G72, GPUTarget::G51, GPUTarget::G51BIG, GPUTarget::G51LIT, GPUTarget::TNOX); - - // Configure kernel window - unsigned int num_elems_read_per_iteration_x = 0; - unsigned int num_elems_read_per_iteration_y = 0; - unsigned int num_elems_written_per_iteration_x = 0; - unsigned int num_elems_written_per_iteration_y = 0; - - // Create kernel - std::string kernel_name; - - if(input->info()->data_type() == DataType::F16) - { - kernel_name = "depthwise_convolution_3x3_f16"; - num_elems_written_per_iteration_x = 8 / data_size_from_type(input->info()->data_type()); - num_elems_written_per_iteration_y = 1; - num_elems_read_per_iteration_y = 3; - switch(_conv_stride_x) - { - case 1: - num_elems_read_per_iteration_x = 8; - break; - case 2: - num_elems_read_per_iteration_x = 9; - break; - case 3: - num_elems_read_per_iteration_x = 16; - break; - default: - num_elems_read_per_iteration_x = 3 + (num_elems_written_per_iteration_x - 1) * _conv_stride_x; - break; - } - if(is_bifrost) - { - if(_conv_stride_x == 1 && _conv_stride_y == 1) - { - kernel_name = "depthwise_convolution_3x3_stridex1_stridey1_bifrost_f16"; - num_elems_read_per_iteration_x = 8; - num_elems_written_per_iteration_x = 4; - num_elems_read_per_iteration_y = 6; - num_elems_written_per_iteration_y = 4; - } - else if(_conv_stride_x == 2 && _conv_stride_y == 2) - { - kernel_name = "depthwise_convolution_3x3_stridex2_stridey2_bifrost_f16"; - num_elems_read_per_iteration_x = 10; - num_elems_written_per_iteration_x = 4; - num_elems_read_per_iteration_y = 5; - num_elems_written_per_iteration_y = 2; - } - } - } - else if(input->info()->data_type() == DataType::F32 && is_bifrost) - { - if(_conv_stride_x == 1 && _conv_stride_y == 1) - { - kernel_name = "depthwise_convolution_3x3_stridex1_stridey1_bifrost_f32"; - num_elems_read_per_iteration_x = 4; - num_elems_read_per_iteration_y = 6; - num_elems_written_per_iteration_x = 2; - num_elems_written_per_iteration_y = 4; - } - else if(_conv_stride_x == 2 && _conv_stride_y == 2) - { - kernel_name = "depthwise_convolution_3x3_stridex2_stridey2_bifrost_f32"; - num_elems_read_per_iteration_x = 6; - num_elems_read_per_iteration_y = 5; - num_elems_written_per_iteration_x = 2; - num_elems_written_per_iteration_y = 2; - } - else - { - kernel_name = "depthwise_convolution_3x3"; - num_elems_written_per_iteration_x = 8 / data_size_from_type(input->info()->data_type()); - num_elems_written_per_iteration_y = 1; - num_elems_read_per_iteration_x = 3 + (num_elems_written_per_iteration_x - 1) * _conv_stride_x; - num_elems_read_per_iteration_y = 3; - } - } - else - { - kernel_name = is_qasymm ? "depthwise_convolution_3x3_quantized" : "depthwise_convolution_3x3"; - num_elems_written_per_iteration_x = 8 / data_size_from_type(input->info()->data_type()); - num_elems_written_per_iteration_y = (is_qasymm && _conv_stride_y < 3) ? (2 / _conv_stride_y) : 1; - num_elems_read_per_iteration_x = 3 + (num_elems_written_per_iteration_x - 1) * _conv_stride_x; - num_elems_read_per_iteration_y = num_elems_written_per_iteration_y + 2; - } - - // Create window and update padding - Window win = calculate_max_window(*output->info(), Steps(num_elems_written_per_iteration_x, num_elems_written_per_iteration_y)); - - AccessWindowRectangle input_access(input->info(), -_conv_pad_left, -_conv_pad_top, - num_elems_read_per_iteration_x, num_elems_read_per_iteration_y, - _conv_stride_x, _conv_stride_y); - AccessWindowStatic weights_access(weights->info(), 0, 0, 3, 3); - AccessWindowRectangle output_access(output->info(), 0, 0, num_elems_written_per_iteration_x, num_elems_written_per_iteration_y); - - update_window_and_padding(win, input_access, weights_access, output_access); - - output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape())); - - ICLKernel::configure(win); - - _kernel = static_cast(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options())); - - // Set config_id for enabling LWS tuning - _config_id = kernel_name; - _config_id += "_"; - _config_id += lower_string(string_from_data_type(input->info()->data_type())); - _config_id += "_"; - _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(output->info()->dimension(0)); - _config_id += "_"; - _config_id += support::cpp11::to_string(output->info()->dimension(1)); -} - -void CLDepthwiseConvolutionLayer3x3Kernel::run(const Window &window, cl::CommandQueue &queue) -{ - ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); - ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); - - // Create input window and adjust - Window win_in = window; - win_in.adjust(Window::DimX, -_conv_pad_left, true); - win_in.adjust(Window::DimY, -_conv_pad_top, true); - win_in.set_dimension_step(Window::DimX, window.x().step() * _conv_stride_x); - win_in.set_dimension_step(Window::DimY, window.y().step() * _conv_stride_y); - - Window slice_in = win_in.first_slice_window_3D(); - Window slice_out = window.first_slice_window_3D(); - Window slice_weights = window.first_slice_window_3D(); - slice_weights.set_dimension_step(Window::DimX, 0); - slice_weights.set_dimension_step(Window::DimY, 0); - - // Set biases - if(_biases != nullptr) - { - unsigned int idx = 3 * num_arguments_per_3D_tensor(); - Window slice_biases; - slice_biases.use_tensor_dimensions(_biases->info()->tensor_shape()); - add_1D_tensor_argument(idx, _biases, slice_biases); - } - - do - { - unsigned int idx = 0; - add_3D_tensor_argument(idx, _input, slice_in); - add_3D_tensor_argument(idx, _output, slice_out); - add_3D_tensor_argument(idx, _weights, slice_weights); - - enqueue(queue, *this, slice_out, _lws_hint); - } - while(window.slide_window_slice_3D(slice_out) && win_in.slide_window_slice_3D(slice_in)); -} diff --git a/src/core/CL/kernels/CLDepthwiseConvolutionLayer3x3NCHWKernel.cpp b/src/core/CL/kernels/CLDepthwiseConvolutionLayer3x3NCHWKernel.cpp new file mode 100644 index 0000000000..de68ceda11 --- /dev/null +++ b/src/core/CL/kernels/CLDepthwiseConvolutionLayer3x3NCHWKernel.cpp @@ -0,0 +1,306 @@ +/* + * 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/CLDepthwiseConvolutionLayer3x3NCHWKernel.h" + +#include "arm_compute/core/AccessWindowStatic.h" +#include "arm_compute/core/CL/CLHelpers.h" +#include "arm_compute/core/CL/CLKernelLibrary.h" +#include "arm_compute/core/CL/ICLKernel.h" +#include "arm_compute/core/CL/ICLTensor.h" +#include "arm_compute/core/Error.h" +#include "arm_compute/core/Helpers.h" +#include "arm_compute/core/TensorInfo.h" +#include "arm_compute/core/Types.h" +#include "arm_compute/core/Utils.h" +#include "arm_compute/core/utils/misc/ShapeCalculator.h" +#include "arm_compute/core/utils/quantization/AsymmHelpers.h" + +using namespace arm_compute; +using namespace arm_compute::misc::shape_calculator; + +CLDepthwiseConvolutionLayer3x3NCHWKernel::CLDepthwiseConvolutionLayer3x3NCHWKernel() + : _conv_stride_x(0), _conv_pad_top(0) +{ +} + +BorderSize CLDepthwiseConvolutionLayer3x3NCHWKernel::border_size() const +{ + return _border_size; +} + +void CLDepthwiseConvolutionLayer3x3NCHWKernel::configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, + ActivationLayerInfo act_info) +{ + ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32); + ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights); + ARM_COMPUTE_ERROR_ON(weights->info()->dimension(0) != 3 || weights->info()->dimension(1) != 3); + + bool is_qasymm = is_data_type_quantized_asymmetric(input->info()->data_type()); + + if(biases != nullptr) + { + if(is_qasymm) + { + ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32); + } + else + { + ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases); + } + ARM_COMPUTE_ERROR_ON(biases->info()->dimension(0) != weights->info()->dimension(2)); + ARM_COMPUTE_ERROR_ON(biases->info()->num_dimensions() > 1); + } + + // Get convolved dimensions + const TensorShape output_shape = compute_depthwise_convolution_shape(*input->info(), *weights->info(), conv_info); + + // Output auto inizialitation if not yet initialized + auto_init_if_empty(*output->info(), + output_shape, + 1, + input->info()->data_type(), + input->info()->fixed_point_position(), + input->info()->quantization_info()); + + ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->info()->tensor_shape(), output_shape); + + _input = input; + _output = output; + _weights = weights; + _biases = biases; + _conv_stride_x = conv_info.stride().first; + _conv_stride_y = conv_info.stride().second; + _conv_pad_left = conv_info.pad_left(); + _conv_pad_top = conv_info.pad_top(); + _border_size = BorderSize(_conv_pad_top, conv_info.pad_right(), conv_info.pad_bottom(), _conv_pad_left); + + // Set build options + ARM_COMPUTE_ERROR_ON(_conv_stride_x < 1 || _conv_stride_x > 3); + CLBuildOptions build_opts; + build_opts.add_option("-DCONV_STRIDE_X=" + support::cpp11::to_string(_conv_stride_x)); + build_opts.add_option_if(_biases != nullptr, "-DHAS_BIAS"); + + if(is_qasymm) + { + float multiplier = _input->info()->quantization_info().scale * _weights->info()->quantization_info().scale / _output->info()->quantization_info().scale; + int output_multiplier = 0; + int output_shift = 0; + quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift); + + build_opts.add_option("-DCONV_STRIDE_Y=" + support::cpp11::to_string(_conv_stride_y)); + build_opts.add_option("-DINPUT_OFFSET=" + support::cpp11::to_string(-_input->info()->quantization_info().offset)); + build_opts.add_option("-DWEIGHTS_OFFSET=" + support::cpp11::to_string(-_weights->info()->quantization_info().offset)); + build_opts.add_option("-DOUTPUT_OFFSET=" + support::cpp11::to_string(_output->info()->quantization_info().offset)); + build_opts.add_option("-DK_OFFSET=" + support::cpp11::to_string(9 * input->info()->quantization_info().offset * weights->info()->quantization_info().offset)); + build_opts.add_option("-DOUTPUT_MULTIPLIER=" + support::cpp11::to_string(output_multiplier)); + build_opts.add_option("-DOUTPUT_SHIFT=" + support::cpp11::to_string(output_shift)); + + if(act_info.enabled()) + { + const int a_val = input->info()->quantization_info().quantize(act_info.a(), RoundingPolicy::TO_NEAREST_UP); + const int b_val = input->info()->quantization_info().quantize(act_info.b(), RoundingPolicy::TO_NEAREST_UP); + const int o1 = input->info()->quantization_info().offset; + + build_opts.add_option("-DFUSED_ACTIVATION=" + lower_string(string_from_activation_func(act_info.activation()))); + build_opts.add_option("-DA_VAL=" + support::cpp11::to_string(a_val)); + build_opts.add_option("-DB_VAL=" + support::cpp11::to_string(b_val)); + build_opts.add_option("-DCONST_0=" + support::cpp11::to_string(o1)); + + if(output != nullptr) + { + const float s1 = input->info()->quantization_info().scale; + const float s2 = output->info()->quantization_info().scale; + const int o2 = output->info()->quantization_info().offset; + + if(o1 != o2 || s1 != s2) + { + build_opts.add_option("-DS1_VAL=" + float_to_string_with_full_precision(s1)); + build_opts.add_option("-DS2_VAL=" + float_to_string_with_full_precision(s2)); + build_opts.add_option("-DO1_VAL=" + support::cpp11::to_string(o1)); + build_opts.add_option("-DO2_VAL=" + support::cpp11::to_string(o2)); + } + } + } + } + + const GPUTarget gpu_target = get_target(); + const bool is_bifrost = gpu_target_is_in(gpu_target, GPUTarget::G71, GPUTarget::G72, GPUTarget::G51, GPUTarget::G51BIG, GPUTarget::G51LIT, GPUTarget::TNOX); + + // Configure kernel window + unsigned int num_elems_read_per_iteration_x = 0; + unsigned int num_elems_read_per_iteration_y = 0; + unsigned int num_elems_written_per_iteration_x = 0; + unsigned int num_elems_written_per_iteration_y = 0; + + // Create kernel + std::string kernel_name; + + if(input->info()->data_type() == DataType::F16) + { + kernel_name = "depthwise_convolution_3x3_f16"; + num_elems_written_per_iteration_x = 8 / data_size_from_type(input->info()->data_type()); + num_elems_written_per_iteration_y = 1; + num_elems_read_per_iteration_y = 3; + switch(_conv_stride_x) + { + case 1: + num_elems_read_per_iteration_x = 8; + break; + case 2: + num_elems_read_per_iteration_x = 9; + break; + case 3: + num_elems_read_per_iteration_x = 16; + break; + default: + num_elems_read_per_iteration_x = 3 + (num_elems_written_per_iteration_x - 1) * _conv_stride_x; + break; + } + if(is_bifrost) + { + if(_conv_stride_x == 1 && _conv_stride_y == 1) + { + kernel_name = "depthwise_convolution_3x3_stridex1_stridey1_bifrost_f16"; + num_elems_read_per_iteration_x = 8; + num_elems_written_per_iteration_x = 4; + num_elems_read_per_iteration_y = 6; + num_elems_written_per_iteration_y = 4; + } + else if(_conv_stride_x == 2 && _conv_stride_y == 2) + { + kernel_name = "depthwise_convolution_3x3_stridex2_stridey2_bifrost_f16"; + num_elems_read_per_iteration_x = 10; + num_elems_written_per_iteration_x = 4; + num_elems_read_per_iteration_y = 5; + num_elems_written_per_iteration_y = 2; + } + } + } + else if(input->info()->data_type() == DataType::F32 && is_bifrost) + { + if(_conv_stride_x == 1 && _conv_stride_y == 1) + { + kernel_name = "depthwise_convolution_3x3_stridex1_stridey1_bifrost_f32"; + num_elems_read_per_iteration_x = 4; + num_elems_read_per_iteration_y = 6; + num_elems_written_per_iteration_x = 2; + num_elems_written_per_iteration_y = 4; + } + else if(_conv_stride_x == 2 && _conv_stride_y == 2) + { + kernel_name = "depthwise_convolution_3x3_stridex2_stridey2_bifrost_f32"; + num_elems_read_per_iteration_x = 6; + num_elems_read_per_iteration_y = 5; + num_elems_written_per_iteration_x = 2; + num_elems_written_per_iteration_y = 2; + } + else + { + kernel_name = "depthwise_convolution_3x3"; + num_elems_written_per_iteration_x = 8 / data_size_from_type(input->info()->data_type()); + num_elems_written_per_iteration_y = 1; + num_elems_read_per_iteration_x = 3 + (num_elems_written_per_iteration_x - 1) * _conv_stride_x; + num_elems_read_per_iteration_y = 3; + } + } + else + { + kernel_name = is_qasymm ? "depthwise_convolution_3x3_quantized_nchw" : "depthwise_convolution_3x3"; + num_elems_written_per_iteration_x = 8 / data_size_from_type(input->info()->data_type()); + num_elems_written_per_iteration_y = (is_qasymm && _conv_stride_y < 3) ? (2 / _conv_stride_y) : 1; + num_elems_read_per_iteration_x = 3 + (num_elems_written_per_iteration_x - 1) * _conv_stride_x; + num_elems_read_per_iteration_y = num_elems_written_per_iteration_y + 2; + } + + // Create window and update padding + Window win = calculate_max_window(*output->info(), Steps(num_elems_written_per_iteration_x, num_elems_written_per_iteration_y)); + + AccessWindowRectangle input_access(input->info(), -_conv_pad_left, -_conv_pad_top, + num_elems_read_per_iteration_x, num_elems_read_per_iteration_y, + _conv_stride_x, _conv_stride_y); + AccessWindowStatic weights_access(weights->info(), 0, 0, 3, 3); + AccessWindowRectangle output_access(output->info(), 0, 0, num_elems_written_per_iteration_x, num_elems_written_per_iteration_y); + + update_window_and_padding(win, input_access, weights_access, output_access); + + output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape())); + + ICLKernel::configure(win); + + _kernel = static_cast(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options())); + + // Set config_id for enabling LWS tuning + _config_id = kernel_name; + _config_id += "_"; + _config_id += lower_string(string_from_data_type(input->info()->data_type())); + _config_id += "_"; + _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(output->info()->dimension(0)); + _config_id += "_"; + _config_id += support::cpp11::to_string(output->info()->dimension(1)); +} + +void CLDepthwiseConvolutionLayer3x3NCHWKernel::run(const Window &window, cl::CommandQueue &queue) +{ + ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); + ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); + + // Create input window and adjust + Window win_in = window; + win_in.adjust(Window::DimX, -_conv_pad_left, true); + win_in.adjust(Window::DimY, -_conv_pad_top, true); + win_in.set_dimension_step(Window::DimX, window.x().step() * _conv_stride_x); + win_in.set_dimension_step(Window::DimY, window.y().step() * _conv_stride_y); + + Window slice_in = win_in.first_slice_window_3D(); + Window slice_out = window.first_slice_window_3D(); + Window slice_weights = window.first_slice_window_3D(); + slice_weights.set_dimension_step(Window::DimX, 0); + slice_weights.set_dimension_step(Window::DimY, 0); + + // Set biases + if(_biases != nullptr) + { + unsigned int idx = 3 * num_arguments_per_3D_tensor(); + Window slice_biases; + slice_biases.use_tensor_dimensions(_biases->info()->tensor_shape()); + add_1D_tensor_argument(idx, _biases, slice_biases); + } + + do + { + unsigned int idx = 0; + add_3D_tensor_argument(idx, _input, slice_in); + add_3D_tensor_argument(idx, _output, slice_out); + add_3D_tensor_argument(idx, _weights, slice_weights); + + enqueue(queue, *this, slice_out, _lws_hint); + } + while(window.slide_window_slice_3D(slice_out) && win_in.slide_window_slice_3D(slice_in)); +} diff --git a/src/core/CL/kernels/CLDepthwiseConvolutionLayer3x3NHWCKernel.cpp b/src/core/CL/kernels/CLDepthwiseConvolutionLayer3x3NHWCKernel.cpp new file mode 100644 index 0000000000..d783b9e159 --- /dev/null +++ b/src/core/CL/kernels/CLDepthwiseConvolutionLayer3x3NHWCKernel.cpp @@ -0,0 +1,254 @@ +/* + * 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/CLDepthwiseConvolutionLayer3x3NHWCKernel.h" + +#include "arm_compute/core/AccessWindowStatic.h" +#include "arm_compute/core/CL/CLHelpers.h" +#include "arm_compute/core/CL/CLKernelLibrary.h" +#include "arm_compute/core/CL/ICLKernel.h" +#include "arm_compute/core/CL/ICLTensor.h" +#include "arm_compute/core/Error.h" +#include "arm_compute/core/Helpers.h" +#include "arm_compute/core/TensorInfo.h" +#include "arm_compute/core/Types.h" +#include "arm_compute/core/Utils.h" +#include "arm_compute/core/utils/misc/ShapeCalculator.h" +#include "arm_compute/core/utils/quantization/AsymmHelpers.h" + +using namespace arm_compute; +using namespace arm_compute::misc::shape_calculator; + +namespace +{ +Status validate_arguments(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, + const ActivationLayerInfo &act_info) +{ + ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8); + ARM_COMPUTE_RETURN_ERROR_ON_MSG((act_info.enabled()) && (act_info.activation() != ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU) + && (act_info.activation() != ActivationLayerInfo::ActivationFunction::BOUNDED_RELU) + && (act_info.activation() != ActivationLayerInfo::ActivationFunction::RELU), + "For QASYMM8 only relu, lower bounded relu and lower-upper bounded relu are supported"); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights); + ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(1) != 3 || weights->dimension(2) != 3); + + if(biases != nullptr) + { + ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32); + ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(0)); + ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1); + } + + if(output->total_size() != 0) + { + const TensorShape output_shape = compute_depthwise_convolution_shape(*input, *weights, conv_info); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), output_shape); + } + + return Status{}; +} + +std::pair validate_and_configure_window(ITensorInfo *input, ITensorInfo *weights, ITensorInfo *output, const PadStrideInfo &conv_info) +{ + const unsigned int num_rows_processed_per_iteration = 4; + const unsigned int num_elems_accessed_per_iteration = 4; + const unsigned int num_rows_read_per_iteration = num_rows_processed_per_iteration + 2; + const unsigned int num_rows_written_per_iteration = num_rows_processed_per_iteration / conv_info.stride().first; + + const BorderSize border_size(conv_info.pad_left() + num_rows_read_per_iteration * std::max(conv_info.pad_top(), conv_info.pad_bottom()), 0, conv_info.pad_right(), 0); + + // Configure kernel window + Window win = calculate_max_window(*output, Steps(num_elems_accessed_per_iteration, num_rows_written_per_iteration)); + + AccessWindowStatic input_access(input, 0, -border_size.top, ceil_to_multiple(input->dimension(0), num_elems_accessed_per_iteration), + ceil_to_multiple(input->dimension(1) + border_size.bottom, num_rows_read_per_iteration)); + AccessWindowRectangle output_access(output, 0, 0, num_elems_accessed_per_iteration, num_rows_written_per_iteration); + AccessWindowHorizontal weights_access(weights, 0, num_elems_accessed_per_iteration); + + bool window_changed = update_window_and_padding(win, input_access, weights_access, output_access); + + output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape())); + + Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; + return std::make_pair(err, win); +} +} // namespace + +CLDepthwiseConvolutionLayer3x3NHWCKernel::CLDepthwiseConvolutionLayer3x3NHWCKernel() + : _num_rows_processed_per_iteration(1) +{ +} + +BorderSize CLDepthwiseConvolutionLayer3x3NHWCKernel::border_size() const +{ + return _border_size; +} + +void CLDepthwiseConvolutionLayer3x3NHWCKernel::configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, + ActivationLayerInfo act_info) +{ + ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output); + + // Get convolved dimensions + const TensorShape output_shape = compute_depthwise_convolution_shape(*input->info(), *weights->info(), conv_info); + + // Output auto inizialitation if not yet initialized + auto_init_if_empty(*output->info(), + output_shape, + 1, + input->info()->data_type(), + input->info()->fixed_point_position(), + input->info()->quantization_info()); + + ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), weights->info(), (biases != nullptr) ? biases->info() : nullptr, output->info(), conv_info, act_info)); + + const unsigned int conv_stride_x = conv_info.stride().first; + ARM_COMPUTE_ERROR_ON(conv_stride_x < 1 || conv_stride_x > 2); + ARM_COMPUTE_ERROR_ON(std::max(conv_info.pad_top(), conv_info.pad_bottom()) > 1); + + _input = input; + _output = output; + _weights = weights; + _biases = biases; + _conv_stride_y = conv_info.stride().second; + _conv_pad_left = conv_info.pad_left(); + _num_rows_processed_per_iteration = 4; + + const unsigned int num_elems_accessed_per_iteration = 4; + const unsigned int num_rows_read_per_iteration = _num_rows_processed_per_iteration + 2; + + _border_size = BorderSize(_conv_pad_left + num_rows_read_per_iteration * std::max(conv_info.pad_top(), conv_info.pad_bottom()), 0, conv_info.pad_right(), 0); + + float multiplier = _input->info()->quantization_info().scale * _weights->info()->quantization_info().scale / _output->info()->quantization_info().scale; + int output_multiplier = 0; + int output_shift = 0; + quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift); + + CLBuildOptions build_opts; + build_opts.add_option_if(_biases != nullptr, "-DHAS_BIAS"); + build_opts.add_option("-DINPUT_OFFSET=" + support::cpp11::to_string(-_input->info()->quantization_info().offset)); + build_opts.add_option("-DWEIGHTS_OFFSET=" + support::cpp11::to_string(-_weights->info()->quantization_info().offset)); + build_opts.add_option("-DOUTPUT_OFFSET=" + support::cpp11::to_string(_output->info()->quantization_info().offset)); + build_opts.add_option("-DK_OFFSET=" + support::cpp11::to_string(9 * input->info()->quantization_info().offset * weights->info()->quantization_info().offset)); + build_opts.add_option("-DOUTPUT_MULTIPLIER=" + support::cpp11::to_string(output_multiplier)); + build_opts.add_option("-DOUTPUT_SHIFT=" + support::cpp11::to_string(output_shift)); + build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_accessed_per_iteration)); + build_opts.add_option("-DSRC_DEPTH=" + support::cpp11::to_string(_input->info()->dimension(2))); + build_opts.add_option("-DCONV_PAD_TOP=" + support::cpp11::to_string(conv_info.pad_top())); + build_opts.add_option("-DROWS_READ=" + support::cpp11::to_string(num_rows_read_per_iteration)); + + if(act_info.enabled()) + { + const int a_val = input->info()->quantization_info().quantize(act_info.a(), RoundingPolicy::TO_NEAREST_UP); + const int b_val = input->info()->quantization_info().quantize(act_info.b(), RoundingPolicy::TO_NEAREST_UP); + const int o1 = input->info()->quantization_info().offset; + + build_opts.add_option("-DFUSED_ACTIVATION=" + lower_string(string_from_activation_func(act_info.activation()))); + build_opts.add_option("-DA_VAL=" + support::cpp11::to_string(a_val)); + build_opts.add_option("-DB_VAL=" + support::cpp11::to_string(b_val)); + build_opts.add_option("-DCONST_0=" + support::cpp11::to_string(o1)); + + if(output != nullptr) + { + const float s1 = input->info()->quantization_info().scale; + const float s2 = output->info()->quantization_info().scale; + const int o2 = output->info()->quantization_info().offset; + + if(o1 != o2 || s1 != s2) + { + build_opts.add_option("-DS1_VAL=" + float_to_string_with_full_precision(s1)); + build_opts.add_option("-DS2_VAL=" + float_to_string_with_full_precision(s2)); + build_opts.add_option("-DO1_VAL=" + support::cpp11::to_string(o1)); + build_opts.add_option("-DO2_VAL=" + support::cpp11::to_string(o2)); + } + } + } + + // Create kernel + std::string kernel_name = std::string("depthwise_convolution_3x3_quantized_nhwc_stride") + support::cpp11::to_string(conv_stride_x); + _kernel = static_cast(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options())); + + // Configure kernel window + auto win_config = validate_and_configure_window(input->info(), weights->info(), output->info(), conv_info); + ARM_COMPUTE_ERROR_THROW_ON(win_config.first); + ICLKernel::configure(win_config.second); + + // Set config_id for enabling LWS tuning + _config_id = kernel_name; + _config_id += "_"; + _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(output->info()->dimension(0)); + _config_id += "_"; + _config_id += support::cpp11::to_string(output->info()->dimension(1)); +} + +Status CLDepthwiseConvolutionLayer3x3NHWCKernel::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, + ActivationLayerInfo act_info) +{ + ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, weights, biases, output, conv_info, act_info)); + ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), weights->clone().get(), output->clone().get(), conv_info).first); + + return Status{}; +} + +void CLDepthwiseConvolutionLayer3x3NHWCKernel::run(const Window &window, cl::CommandQueue &queue) +{ + ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); + ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); + + // Create input window and adjust + Window win_in = window; + win_in.adjust(Window::DimY, -_conv_pad_left, true); + win_in.set_dimension_step(Window::DimY, _num_rows_processed_per_iteration); + win_in.set_dimension_step(Window::DimZ, _conv_stride_y); + + ARM_COMPUTE_ERROR_ON((win_in.y().step() < window.y().step()) || (win_in.z().step() < window.z().step())); + + Window slice_in = win_in.first_slice_window_3D(); + Window slice_out = window.first_slice_window_3D(); + + if(_biases != nullptr) + { + unsigned int idx = 3 * num_arguments_per_3D_tensor(); + Window win_biases; + win_biases.use_tensor_dimensions(_biases->info()->tensor_shape()); + win_biases.set_dimension_step(Window::DimX, window.x().step()); + add_1D_tensor_argument(idx, _biases, win_biases); + } + + do + { + unsigned int idx = 0; + add_3D_tensor_argument(idx, _input, slice_in); + add_3D_tensor_argument(idx, _output, slice_out); + add_3D_tensor_argument(idx, _weights, slice_out); + + enqueue(queue, *this, slice_out, _lws_hint); + } + while(window.slide_window_slice_3D(slice_out) && win_in.slide_window_slice_3D(slice_in)); +} -- cgit v1.2.1