From 4a626a7d52e9c4759bdc16b65401a53779dd975f Mon Sep 17 00:00:00 2001 From: Pablo Tello Date: Wed, 4 Apr 2018 10:01:14 +0100 Subject: COMPMID-801: NHWC support in CLIm2Col. And extended tests coverage adding kernel shapes 3x1, 1x5 and 7x7 Change-Id: Ia7c1d4da2368d5f5fbc1a41187f4ac1aca5f150f Reviewed-on: https://eu-gerrit-1.euhpc.arm.com/127727 Tested-by: Jenkins Reviewed-by: Gian Marco Iodice --- src/core/CL/CLKernelLibrary.cpp | 2 + src/core/CL/cl_kernels/im2col.cl | 202 +++++++++++++++++++++++- src/core/CL/kernels/CLIm2ColKernel.cpp | 273 +++++++++++++++++++++------------ 3 files changed, 382 insertions(+), 95 deletions(-) (limited to 'src/core') diff --git a/src/core/CL/CLKernelLibrary.cpp b/src/core/CL/CLKernelLibrary.cpp index 009d4db535..207efa6aa1 100644 --- a/src/core/CL/CLKernelLibrary.cpp +++ b/src/core/CL/CLKernelLibrary.cpp @@ -272,6 +272,8 @@ const std::map CLKernelLibrary::_kernel_program_map = { "im2col_generic_dchw", "im2col.cl" }, { "im2col_generic_padx0_pady0_dchw", "im2col.cl" }, { "im2col_reduced_dchw", "im2col.cl" }, + { "im2col3x3_nhwc", "im2col.cl" }, + { "im2col_generic_nhwc", "im2col.cl" }, { "init_level", "optical_flow_pyramid_lk.cl" }, { "init_level_max", "optical_flow_pyramid_lk.cl" }, { "init_level_max_initial_estimate", "optical_flow_pyramid_lk.cl" }, diff --git a/src/core/CL/cl_kernels/im2col.cl b/src/core/CL/cl_kernels/im2col.cl index 1e85e1b303..f53ce21d05 100644 --- a/src/core/CL/cl_kernels/im2col.cl +++ b/src/core/CL/cl_kernels/im2col.cl @@ -123,7 +123,207 @@ __kernel void im2col1x1_stridex1_dchw( } #endif // defined(CONVOLVED_WIDTH) && defined(STRIDE_Y) && defined(KERNEL_DEPTH) +#define PTR_TO_VALUE(PTR, DATA_TYPE) *((DATA_TYPE *)(PTR)) + #if defined(CONVOLVED_WIDTH) && defined(SRC_WIDTH) && defined(SRC_HEIGHT) && defined(STRIDE_X) && defined(STRIDE_Y) && defined(KERNEL_DEPTH) && defined(PAD_LEFT) && defined(PAD_RIGHT) && defined(PAD_TOP) && defined(PAD_BOTTOM) && defined(PAD_VALUE) + +/** This kernel performs a reshaping of the input tensor to a tensor used to perform convolution using GEMM when the kernel size is 5x5 + * + * @note The data type must be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=float + * @note The width and height of the input tensor must be passed at compile time using -DSRC_WIDTH and -DSRC_HEIGHT: e.g. -DSRC_WIDTH=128 and -DSRC_HEIGHT=128 + * @note The width of output tensor after matrix multiplication must be passed at compile time using -DCONVOLVED_WIDTH: e.g. -DCONVOLVED_WIDTH=34 + * @note The kernel depth must be passed at compile time using -DKERNEL_DEPTH: e.g. -DKERNEL_DEPTH=3 + * @note The pad_left, pad_right, pad_top and pad_bottom must be passed at compile time using -DPAD_LEFT, -DPAD_RIGHT, -DPAD_TOP and -DPAD_BOTTOM: e.g. -DPAD_LEFT=1, -DPAD_RIGHT=2, -DPAD_TOP=3 and -DPAD_BOTTOM=2 + * @note The zero value to store in case we load values out-of-bounds must be passed at compile time using -DPAD_VALUE: e.g. -DPAD_VALUE=0.0 + * @note The stride along the X and Y directions must be passed at compile time using -DSTRIDE_X and -DSTRIDE_Y: e.g. -DSTRIDE_X=1 and -DSTRIDE_Y=1 + * @note In case biases will be added to the convolution -DHAS_BIAS has to be passed to append the final matrix with 1 in each row. + * + * @param[in] src_ptr Pointer to the source tensor. Supported data types: QS8/QASYMM8/QS16/F16/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 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] src_stride_w Stride of the source tensor in W dimension (in bytes). + * @param[in] dst_stride_w Stride of the destination tensor in W dimension (in bytes). + */ +__kernel void im2col_generic_nhwc( + TENSOR3D_DECLARATION(src), + IMAGE_DECLARATION(dst), + uint src_stride_w, + uint dst_stride_w) +{ + const int src_stride_y_int = (int)src_stride_y; + const int src_stride_z_int = (int)src_stride_z; + const int xc = get_global_id(1); // x coordinate in the convolved tensor + const int yc = get_global_id(2) % CONVOLVED_HEIGHT; // y coordinate in the convolved tensor + const int ch = get_global_id(0); // input feature map + const int batch = get_global_id(2) / CONVOLVED_HEIGHT; // batch size + + // Calculate input indices + const int xi = xc * STRIDE_X - PAD_LEFT; + const int yi = yc * STRIDE_Y - PAD_TOP; + + // Calculate output indices + const int xo = ch * KERNEL_HEIGHT * KERNEL_WIDTH; + const int yo = xc + yc * CONVOLVED_WIDTH; // Index of the convolution + + // Get input and output address + __global uchar *input_ptr = src_ptr + src_offset_first_element_in_bytes + xi * src_stride_y_int + yi * src_stride_z_int + ch * src_stride_x + batch * src_stride_w; + __global uchar *output_ptr = dst_ptr + dst_offset_first_element_in_bytes + xo * dst_stride_x + yo * dst_stride_y + batch * dst_stride_w; + + for(int yk = 0; yk < KERNEL_HEIGHT; ++yk) + { + const int y0 = yi + yk; + if(y0 >= 0 && y0 < SRC_HEIGHT) + { + int xk; + for(xk = 0; xk < KERNEL_WIDTH; xk++) + { + const int x0 = xi + xk; + if(x0 >= 0 && x0 < SRC_WIDTH) + { + *((__global DATA_TYPE *)output_ptr) = PTR_TO_VALUE(input_ptr + xk * src_stride_y + yk * src_stride_z, DATA_TYPE); + } + else + { + *((__global DATA_TYPE *)output_ptr) = PAD_VALUE; + } + output_ptr += 1 * sizeof(DATA_TYPE); + } + } + else + { + for(int xk = 0; xk < KERNEL_WIDTH; xk++) + { + *((__global DATA_TYPE *)output_ptr) = (DATA_TYPE)PAD_VALUE; + output_ptr += 1 * dst_stride_x; + } + } + } +#ifdef HAS_BIAS + if(ch == (KERNEL_DEPTH - 1)) + { + *((__global DATA_TYPE *)output_ptr) = 1.0f; + output_ptr += 1 * dst_stride_x; + } +#endif // HAS_BIAS +} + +/** This kernel performs a reshaping of the input tensor (with layout NHWC) to a tensor used to perform convolution using GEMM when the kernel size is 3x3 + * + * @note The data type must be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=float + * @note The width and height of the input tensor must be passed at compile time using -DSRC_WIDTH and -DSRC_HEIGHT: e.g. -DSRC_WIDTH=128 and -DSRC_HEIGHT=128 + * @note The width of output tensor after matrix multiplication must be passed at compile time using -DCONVOLVED_WIDTH: e.g. -DCONVOLVED_WIDTH=34 + * @note The kernel depth must be passed at compile time using -DKERNEL_DEPTH: e.g. -DKERNEL_DEPTH=3 + * @note The pad_left, pad_right, pad_top and pad_bottom must be passed at compile time using -DPAD_LEFT, -DPAD_RIGHT, -DPAD_TOP and -DPAD_BOTTOM: e.g. -DPAD_LEFT=1, -DPAD_RIGHT=2, -DPAD_TOP=3 and -DPAD_BOTTOM=2 + * @note The zero value to store in case we load values out-of-bounds must be passed at compile time using -DPAD_VALUE: e.g. -DPAD_VALUE=0.0 + * @note The stride along the X and Y directions must be passed at compile time using -DSTRIDE_X and -DSTRIDE_Y: e.g. -DSTRIDE_X=1 and -DSTRIDE_Y=1 + * @note In case biases will be added to the convolution -DHAS_BIAS has to be passed to append the final matrix with 1 in each row. + * + * @param[in] src_ptr Pointer to the source tensor. Supported data types: QS8/QASYMM8/QS16/F16/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 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] src_stride_w Stride of the source tensor in W dimension (in bytes). + * @param[in] dst_stride_w Stride of the destination tensor in W dimension (in bytes). + */ +__kernel void im2col3x3_nhwc( + TENSOR3D_DECLARATION(src), + IMAGE_DECLARATION(dst), + uint src_stride_w, + uint dst_stride_w) +{ + const int src_stride_y_int = (int)src_stride_y; + const int src_stride_z_int = (int)src_stride_z; + const int xc = get_global_id(1); // x coordinate in the convolved tensor + const int yc = get_global_id(2) % CONVOLVED_HEIGHT; // y coordinate in the convolved tensor + const int ch = get_global_id(0); // input feature map + const int batch = get_global_id(2) / CONVOLVED_HEIGHT; // batch size + + // Calculate input indices + const int xi = xc * STRIDE_X - PAD_LEFT; + const int yi = yc * STRIDE_Y - PAD_TOP; + + // Calculate output indices + const int xo = ch * 9; // 3x3 + const int yo = xc + yc * CONVOLVED_WIDTH; // Index of the convolution + + // Get input and output address + __global uchar *input_ptr = src_ptr + src_offset_first_element_in_bytes + xi * src_stride_y_int + yi * src_stride_z_int + ch * src_stride_x + batch * src_stride_w; + __global uchar *output_ptr = dst_ptr + dst_offset_first_element_in_bytes + xo * dst_stride_x + yo * dst_stride_y + batch * dst_stride_w; + + VEC_DATA_TYPE(DATA_TYPE, 3) + row0 = (VEC_DATA_TYPE(DATA_TYPE, 3))(PAD_VALUE); + VEC_DATA_TYPE(DATA_TYPE, 3) + row1 = (VEC_DATA_TYPE(DATA_TYPE, 3))(PAD_VALUE); + VEC_DATA_TYPE(DATA_TYPE, 3) + row2 = (VEC_DATA_TYPE(DATA_TYPE, 3))(PAD_VALUE); + + const int3 y = (int3)yi + (int3)(0, 1, 2); + // Guard against reading outside the input buffer, there is no padding in Z so we check if ry is inside the buffer. + if(y.s0 >= 0 && y.s0 < SRC_HEIGHT) + { + row0 = (VEC_DATA_TYPE(DATA_TYPE, 3))( + PTR_TO_VALUE(input_ptr + 0 * src_stride_y, DATA_TYPE), + PTR_TO_VALUE(input_ptr + 1 * src_stride_y, DATA_TYPE), + PTR_TO_VALUE(input_ptr + 2 * src_stride_y, DATA_TYPE)); + } + + if(y.s1 >= 0 && y.s1 < SRC_HEIGHT) + { + row1 = (VEC_DATA_TYPE(DATA_TYPE, 3))( + PTR_TO_VALUE(input_ptr + 0 * src_stride_y + 1 * src_stride_z, DATA_TYPE), + PTR_TO_VALUE(input_ptr + 1 * src_stride_y + 1 * src_stride_z, DATA_TYPE), + PTR_TO_VALUE(input_ptr + 2 * src_stride_y + 1 * src_stride_z, DATA_TYPE)); + } + + if(y.s2 >= 0 && y.s2 < SRC_HEIGHT) + { + row2 = (VEC_DATA_TYPE(DATA_TYPE, 3))( + PTR_TO_VALUE(input_ptr + 0 * src_stride_y + 2 * src_stride_z, DATA_TYPE), + PTR_TO_VALUE(input_ptr + 1 * src_stride_y + 2 * src_stride_z, DATA_TYPE), + PTR_TO_VALUE(input_ptr + 2 * src_stride_y + 2 * src_stride_z, DATA_TYPE)); + } + +#if PAD_LEFT != 0 || PAD_TOP != 0 || PAD_RIGHT != 0 || PAD_BOTTOM != 0 + // Put 0 if the value is out-of-bound + const int3 x = (int3)xi + (int3)(0, 1, 2); + VEC_DATA_TYPE(COND_DATA_TYPE, 3) + cond0 = CONVERT((x >= (int3)0 && x < (int3)SRC_WIDTH), VEC_DATA_TYPE(COND_DATA_TYPE, 3)); + row0 = select((VEC_DATA_TYPE(DATA_TYPE, 3))PAD_VALUE, row0, cond0); + row1 = select((VEC_DATA_TYPE(DATA_TYPE, 3))PAD_VALUE, row1, cond0); + row2 = select((VEC_DATA_TYPE(DATA_TYPE, 3))PAD_VALUE, row2, cond0); +#endif // PAD_LEFT != 0 || PAD_TOP != 0 || PAD_RIGHT != 0 || PAD_BOTTOM != 0 + vstore8((VEC_DATA_TYPE(DATA_TYPE, 8))(row0.s012, row1.s012, row2.s01), 0, (__global DATA_TYPE *)output_ptr); + *((__global DATA_TYPE *)output_ptr + 8) = row2.s2; + +#ifdef HAS_BIAS + if(ch == (KERNEL_DEPTH - 1)) + { + *((__global DATA_TYPE *)output_ptr + 9) = 1.0f; + } +#endif // HAS_BIAS +} + /** This kernel performs a reshaping of the input tensor to a tensor used to perform convolution using GEMM when the kernel size is 3x3 * * @note The data type must be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=float @@ -804,4 +1004,4 @@ __kernel void im2col_reduced_dchw( } #endif // HAS_BIAS } -#endif // defined(DATA_TYPE) && defined(ELEMENT_SIZE) \ No newline at end of file +#endif // defined(DATA_TYPE) && defined(ELEMENT_SIZE) diff --git a/src/core/CL/kernels/CLIm2ColKernel.cpp b/src/core/CL/kernels/CLIm2ColKernel.cpp index 53a4dca9a3..00d9fcb0e0 100644 --- a/src/core/CL/kernels/CLIm2ColKernel.cpp +++ b/src/core/CL/kernels/CLIm2ColKernel.cpp @@ -31,7 +31,10 @@ #include "arm_compute/core/Error.h" #include "arm_compute/core/Helpers.h" #include "arm_compute/core/Size2D.h" +#include "arm_compute/core/TensorInfo.h" #include "arm_compute/core/Types.h" +#include "arm_compute/core/Validate.h" +#include "arm_compute/core/utils/misc/ShapeCalculator.h" #include "support/ToolchainSupport.h" #include @@ -48,6 +51,7 @@ Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, b ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::QASYMM8 && has_bias); ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output); ARM_COMPUTE_RETURN_ERROR_ON((dilation.x() < 1) || (dilation.y() < 1)); + ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() == DataLayout::UNKNOWN); // Checks performed when output is configured if(output->total_size() != 0) @@ -58,63 +62,63 @@ Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, b return Status{}; } -} // namespace -CLIm2ColKernel::CLIm2ColKernel() - : _input(nullptr), _output(nullptr), _conv_info(), _convolved_dims(), _num_elems_processed_per_iteration(1), _run_func(nullptr), _kernel_dims() +inline bool run_im2col_reduced(ITensorInfo *input, ITensorInfo *output, const PadStrideInfo &conv_info) { -} - -void CLIm2ColKernel::configure(const ICLTensor *input, ICLTensor *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, const Size2D &dilation) -{ - ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); - - // Perform validation step - ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), has_bias, dilation)); - - _input = input; - _output = output; - _conv_info = conv_info; - _kernel_dims = kernel_dims; - - const DataType data_type = input->info()->data_type(); - const GPUTarget gpu_target = get_target(); - - // Create kernel - CLBuildOptions build_opts; - build_opts.add_option(("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type))); - build_opts.add_option("-DELEMENT_SIZE=" + support::cpp11::to_string(input->info()->element_size())); - build_opts.add_option_if(has_bias, "-DHAS_BIAS"); - build_opts.add_option_if(is_data_type_fixed_point(data_type), "-DFIXED_POINT_POSITION=" + support::cpp11::to_string(input->info()->fixed_point_position())); - int stride_x = 0; int stride_y = 0; std::tie(stride_x, stride_y) = conv_info.stride(); - const bool run_img2col_reduced = (output->info()->dimension(0) == (input->info()->dimension(0) * input->info()->dimension(1) * input->info()->dimension(2))) && (TensorShape::num_max_dimensions >= 4) - && (std::equal(input->info()->tensor_shape().cbegin() + 3, - input->info()->tensor_shape().cend(), - output->info()->tensor_shape().cbegin() + 1)) - && ((stride_x == 1) && (stride_y == 1) && !conv_info.has_padding()); + return (output->dimension(0) == (input->dimension(0) * input->dimension(1) * input->dimension(2))) && (TensorShape::num_max_dimensions >= 4) + && (std::equal(input->tensor_shape().cbegin() + 3, + input->tensor_shape().cend(), + output->tensor_shape().cbegin() + 1)) + && ((stride_x == 1) && (stride_y == 1) && !conv_info.has_padding()); +} - bool is_optimized_path = false; +} // namespace - _num_elems_processed_per_iteration = 1; +CLIm2ColKernel::CLIm2ColKernel() + : _input(nullptr), _output(nullptr), _conv_info(), _convolved_dims(), _num_elems_processed_per_iteration(1), _run_func(nullptr), _kernel_dims() +{ +} - std::string kernel_name; - if(!run_img2col_reduced) +std::string +CLIm2ColKernel::configure_window(const ICLTensor *input, ICLTensor *output, const Size2D &kernel_dims, + const Size2D &dilation, const PadStrideInfo &conv_info, CLBuildOptions &build_opts) +{ + std::string kernel_name; + bool is_optimized_path = false; + const bool reduced = run_im2col_reduced(input->info(), output->info(), conv_info); + const DataType data_type = input->info()->data_type(); + const bool squared_im2col = kernel_dims.width == kernel_dims.height; + const DataLayout data_layout = input->info()->data_layout(); + + if(!reduced) { // Default kernel name - kernel_name = "im2col_generic_dchw"; + if(data_layout == DataLayout::NCHW) + { + kernel_name = "im2col_generic_dchw"; + } + else + { + kernel_name = "im2col_generic_nhwc"; + } - _convolved_dims = scaled_dimensions(input->info()->dimension(0), input->info()->dimension(1), - kernel_dims.width, kernel_dims.height, - conv_info, dilation); + const unsigned int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); + const unsigned int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); + const unsigned int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL); + const unsigned int input_width = input->info()->dimension(width_idx); + const unsigned int input_height = input->info()->dimension(height_idx); + const unsigned int input_channel = input->info()->dimension(channel_idx); + + _convolved_dims = scaled_dimensions(input_width, input_height, kernel_dims.width, kernel_dims.height, conv_info, dilation); build_opts.add_option("-DKERNEL_WIDTH=" + support::cpp11::to_string(kernel_dims.width)); build_opts.add_option("-DKERNEL_HEIGHT=" + support::cpp11::to_string(kernel_dims.height)); - build_opts.add_option("-DKERNEL_DEPTH=" + support::cpp11::to_string(input->info()->dimension(2))); + build_opts.add_option("-DKERNEL_DEPTH=" + support::cpp11::to_string(input_channel)); build_opts.add_option("-DCONVOLVED_WIDTH=" + support::cpp11::to_string(_convolved_dims.first)); build_opts.add_option("-DCONVOLVED_HEIGHT=" + support::cpp11::to_string(_convolved_dims.second)); build_opts.add_option("-DSTRIDE_X=" + support::cpp11::to_string(conv_info.stride().first)); @@ -123,14 +127,12 @@ void CLIm2ColKernel::configure(const ICLTensor *input, ICLTensor *output, const build_opts.add_option("-DPAD_TOP=" + support::cpp11::to_string(conv_info.pad_top())); build_opts.add_option("-DPAD_RIGHT=" + support::cpp11::to_string(conv_info.pad_right())); build_opts.add_option("-DPAD_BOTTOM=" + support::cpp11::to_string(conv_info.pad_bottom())); - build_opts.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(input->info()->dimension(0))); - build_opts.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(input->info()->dimension(1))); + build_opts.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(input_width)); + build_opts.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(input_height)); build_opts.add_option("-DDILATION_X=" + support::cpp11::to_string(dilation.x())); build_opts.add_option("-DDILATION_Y=" + support::cpp11::to_string(dilation.y())); build_opts.add_option_if_else(is_data_type_quantized(data_type), "-DPAD_VALUE=" + support::cpp11::to_string(input->info()->quantization_info().offset), "-DPAD_VALUE=0"); - const bool squared_im2col = kernel_dims.width == kernel_dims.height; - if(dilation == Size2D(1U, 1U)) { if(squared_im2col && !is_data_type_fixed_point(data_type)) @@ -153,12 +155,31 @@ void CLIm2ColKernel::configure(const ICLTensor *input, ICLTensor *output, const _lws_hint = cl::NDRange(1, 1, 8); _num_elems_processed_per_iteration = 1; is_optimized_path = true; - kernel_name = "im2col3x3_dchw"; + switch(data_layout) + { + case DataLayout::NCHW: + kernel_name = "im2col3x3_dchw"; + break; + case DataLayout::NHWC: + kernel_name = "im2col3x3_nhwc"; + break; + default: + ARM_COMPUTE_ERROR("Not supported."); + break; + } break; case 5: _num_elems_processed_per_iteration = 1; is_optimized_path = true; - kernel_name = "im2col5x5_dchw"; + switch(data_layout) + { + case DataLayout::NCHW: + kernel_name = "im2col5x5_dchw"; + break; + default: + // using generic_nhwc + break; + } break; case 11: // Optimized im2col11x11 if pad_x = pad_y = 0 @@ -177,28 +198,34 @@ void CLIm2ColKernel::configure(const ICLTensor *input, ICLTensor *output, const else if(kernel_dims.width > 1 && !conv_info.has_padding()) { _num_elems_processed_per_iteration = 1; - kernel_name = "im2col_generic_padx0_pady0_dchw"; - - // Optimized im2col is performed using one or more vector operations with the specified vector size - // and a remainder. For example, for 5x5 convolutions, im2col is performed using vectors of size 4 - // and scalars; for 7x7 convolutions, using vectors of size 4 and vectors of size 3. - // Using the vector size of 4 is always safe since OpenCL supports vectors of size 2 and 3. - // Using the vector size of 8, however, may be faster. - size_t vector_size = 4; - // For 2x2 convolutions, use vectors of size 2. (For 3x3 convolutions, im2col_kernel3x3_padx0_pady0 - // is used instead.) - if(kernel_dims.width < vector_size) - { - vector_size = kernel_dims.width; - } - // Vector size optimized for the 11x11 AlexNet convolution on Bifrost. - if(gpu_target_is_in(gpu_target, GPUTarget::G71, GPUTarget::G72, GPUTarget::G51, GPUTarget::G51BIG, GPUTarget::G51LIT, GPUTarget::TNOX) && kernel_dims.width == 11) + is_optimized_path = false; + + if(data_layout == DataLayout::NCHW) { - vector_size = 8; + kernel_name = "im2col_generic_padx0_pady0_dchw"; + + // Optimized im2col is performed using one or more vector operations with the specified vector size + // and a remainder. For example, for 5x5 convolutions, im2col is performed using vectors of size 4 + // and scalars; for 7x7 convolutions, using vectors of size 4 and vectors of size 3. + // Using the vector size of 4 is always safe since OpenCL supports vectors of size 2 and 3. + // Using the vector size of 8, however, may be faster. + size_t vector_size = 4; + // For 2x2 convolutions, use vectors of size 2. (For 3x3 convolutions, im2col_kernel3x3_padx0_pady0 + // is used instead.) + if(kernel_dims.width < vector_size) + { + vector_size = kernel_dims.width; + } + // Vector size optimized for the 11x11 AlexNet convolution on Bifrost. + const GPUTarget gpu_target = get_target(); + if(gpu_target_is_in(gpu_target, GPUTarget::G71, GPUTarget::G72, GPUTarget::G51, GPUTarget::G51BIG, GPUTarget::G51LIT, GPUTarget::TNOX) && kernel_dims.width == 11) + { + vector_size = 8; + } + const size_t width_mod_vector_size = kernel_dims.width % vector_size; + build_opts.add_option("-DVECTOR_SIZE=" + support::cpp11::to_string(vector_size)); + build_opts.add_option("-DWIDTH_MOD_VECTOR_SIZE=" + support::cpp11::to_string(width_mod_vector_size)); } - const size_t width_mod_vector_size = kernel_dims.width % vector_size; - build_opts.add_option("-DVECTOR_SIZE=" + support::cpp11::to_string(vector_size)); - build_opts.add_option("-DWIDTH_MOD_VECTOR_SIZE=" + support::cpp11::to_string(width_mod_vector_size)); } } _run_func = &CLIm2ColKernel::run_generic; @@ -209,27 +236,37 @@ void CLIm2ColKernel::configure(const ICLTensor *input, ICLTensor *output, const kernel_name = "im2col_reduced_dchw"; _run_func = &CLIm2ColKernel::run_reduced; } - - // Create kernel - _kernel = static_cast(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options())); - // Configure kernel window Window win; if(is_optimized_path) { - win = calculate_max_window(*input->info(), - Steps(_num_elems_processed_per_iteration), - false, - BorderSize(conv_info.pad_top(), conv_info.pad_right(), conv_info.pad_bottom(), conv_info.pad_left())); - - const int x = -conv_info.pad_left(); - const int y = -conv_info.pad_top(); - const int w = kernel_dims.width * _num_elems_processed_per_iteration; - const int h = kernel_dims.height; - - AccessWindowRectangle input_access(input->info(), x, y, w, h); - - update_window_and_padding(win, input_access); + if(data_layout == DataLayout::NHWC) + { + win = calculate_max_window(*input->info(), + Steps(_num_elems_processed_per_iteration), + false, + BorderSize(conv_info.pad_top(), conv_info.pad_right(), conv_info.pad_bottom(), conv_info.pad_left())); + const int x = -conv_info.pad_left(); + const int y = -conv_info.pad_top(); + const int h = kernel_dims.width * _num_elems_processed_per_iteration; + const int w = 1; + AccessWindowRectangle input_access(input->info(), x, y, w, h); + update_window_and_padding(win, input_access); + } + else + { + win = calculate_max_window(*input->info(), + Steps(_num_elems_processed_per_iteration), + false, + BorderSize(conv_info.pad_top(), conv_info.pad_right(), conv_info.pad_bottom(), conv_info.pad_left())); + + const int x = -conv_info.pad_left(); + const int y = -conv_info.pad_top(); + const int w = kernel_dims.width * _num_elems_processed_per_iteration; + const int h = kernel_dims.height; + AccessWindowRectangle input_access(input->info(), x, y, w, h); + update_window_and_padding(win, input_access); + } } else { @@ -239,13 +276,41 @@ void CLIm2ColKernel::configure(const ICLTensor *input, ICLTensor *output, const } output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape())); - if(!run_img2col_reduced) + if(!reduced) { // set the Z dimension's step same size as the whole dimension so that one can't split across the Z dimension win.set_dimension_step(Window::DimZ, win[Window::DimZ].end() - win[Window::DimZ].start()); } - ICLKernel::configure(win); + return kernel_name; +} + +void CLIm2ColKernel::configure(const ICLTensor *input, ICLTensor *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, const Size2D &dilation) +{ + ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); + ARM_COMPUTE_ERROR_ON(input->info()->data_layout() == DataLayout::UNKNOWN); + ARM_COMPUTE_ERROR_ON_MSG(output->info()->data_layout() != DataLayout::NCHW, "Special case Im2Col output layout is NCHW"); + ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), has_bias, dilation)); + + _input = input; + _output = output; + _kernel_dims = kernel_dims; + _conv_info = conv_info; + + const DataType data_type = input->info()->data_type(); + + // Create kernel + CLBuildOptions build_opts; + build_opts.add_option(("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type))); + build_opts.add_option("-DELEMENT_SIZE=" + support::cpp11::to_string(input->info()->element_size())); + build_opts.add_option_if(has_bias, "-DHAS_BIAS"); + build_opts.add_option_if(is_data_type_fixed_point(data_type), "-DFIXED_POINT_POSITION=" + support::cpp11::to_string(input->info()->fixed_point_position())); + + _num_elems_processed_per_iteration = 1; + + const std::string kernel_name = configure_window(input, output, kernel_dims, dilation, conv_info, build_opts); + // Create kernel + _kernel = static_cast(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options())); // Set config_id for enabling LWS tuning _config_id = kernel_name; @@ -277,23 +342,43 @@ void CLIm2ColKernel::run_generic(const Window &window, cl::CommandQueue &queue) ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window); + const DataLayout data_layout = _input->info()->data_layout(); + const unsigned int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); + const unsigned int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); + // Get initial windows Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ); // Change the Z dimension's step back to 1 window_collapsed.set_dimension_step(Window::DimZ, 1); - Window slice = window_collapsed.first_slice_window_3D(); - Window slice_in = window_collapsed.first_slice_window_3D(); - Window slice_out = window_collapsed.first_slice_window_3D(); + const Window first_slice_3d = window_collapsed.first_slice_window_3D(); + + Window slice = first_slice_3d; + Window slice_in = first_slice_3d; + Window slice_out = first_slice_3d; - // Setup slice if stride_x != 0 or stride_y != 0 - if(_convolved_dims.first != _input->info()->dimension(0) || _convolved_dims.second != _input->info()->dimension(1)) + const bool out_dim_not_same_input_dim = _convolved_dims.first != _input->info()->dimension(width_idx) || _convolved_dims.second != _input->info()->dimension(height_idx); + + // Setup slice if convolved dims are not the same as input dims + if(out_dim_not_same_input_dim) { // If the stride_x or stride_y are not 1, the output tensor of matrix multiply (Convolved tensor) will not // have the same shape of the im2col input tensor // In this case we need to re-compute the window using the shape of the tensor after matrix multiply (convolved_dims) - slice.set(Window::DimX, Window::Dimension(0, static_cast(_convolved_dims.first), 1)); - slice.set(Window::DimY, Window::Dimension(0, static_cast(_convolved_dims.second), 1)); + slice.set(width_idx, Window::Dimension(0, static_cast(_convolved_dims.first), 1)); + if(data_layout == DataLayout::NHWC) + { + // if layout is NHWC, we need to multiply convolved_dims.height by the number of batches as for this + // format we collapsed HEIGHT and all subsequent dimensions (batches) together. This is necessary to ensure + // global_id(2) values are in the correct range. + const Window tmp_win = window.collapse_if_possible(ICLKernel::window(), 3); + const int num_batches = tmp_win[3].end(); + slice.set(height_idx, Window::Dimension(0, static_cast(_convolved_dims.second) * num_batches, 1)); + } + else + { + slice.set(height_idx, Window::Dimension(0, static_cast(_convolved_dims.second), 1)); + } } // Setup input slice @@ -304,7 +389,7 @@ void CLIm2ColKernel::run_generic(const Window &window, cl::CommandQueue &queue) // Setup output slice slice_out.set(Window::DimX, Window::Dimension(0, _output->info()->dimension(0), _kernel_dims.area())); - slice_out.set(Window::DimY, Window::Dimension(0, _output->info()->dimension(1), 1)); + slice_out.set(Window::DimY, Window::Dimension(0, _output->info()->dimension(1), _output->info()->dimension(1))); slice_out.set(Window::DimZ, Window::Dimension(0, 1, 1)); do -- cgit v1.2.1