From 3e36369a5511c3028c30fc820752dc1248bddf5c Mon Sep 17 00:00:00 2001 From: SiCong Li Date: Tue, 4 Jul 2017 15:02:10 +0100 Subject: COMPMID-358 Implement OpenCL ROI Pooling * Implement OpenCL ROI Pooling * Add CLROIPoolingLayer benchmarks Change-Id: I8786d01d551850a1b4d599a48fabe3925e0a27d0 Reviewed-on: http://mpd-gerrit.cambridge.arm.com/79833 Reviewed-by: Anthony Barbier Tested-by: Kaizen --- src/core/CL/CLKernelLibrary.cpp | 5 + src/core/CL/ICLKernel.cpp | 11 +- src/core/CL/cl_kernels/roi_pooling_layer.cl | 169 ++++++++++++++++++++++++ src/core/CL/kernels/CLROIPoolingLayerKernel.cpp | 121 +++++++++++++++++ 4 files changed, 300 insertions(+), 6 deletions(-) create mode 100644 src/core/CL/cl_kernels/roi_pooling_layer.cl create mode 100644 src/core/CL/kernels/CLROIPoolingLayerKernel.cpp (limited to 'src/core/CL') diff --git a/src/core/CL/CLKernelLibrary.cpp b/src/core/CL/CLKernelLibrary.cpp index 2589bd12b5..ce2cfef67a 100644 --- a/src/core/CL/CLKernelLibrary.cpp +++ b/src/core/CL/CLKernelLibrary.cpp @@ -246,6 +246,7 @@ const std::map CLKernelLibrary::_kernel_program_map = { "RGBA8888_to_NV12_bt709", "color_convert.cl" }, { "RGBA8888_to_RGB888_bt709", "color_convert.cl" }, { "RGBA8888_to_YUV444_bt709", "color_convert.cl" }, + { "roi_pooling_layer", "roi_pooling_layer.cl" }, { "scale_nearest_neighbour", "scale.cl" }, { "scale_bilinear", "scale.cl" }, { "scharr3x3", "scharr_filter.cl" }, @@ -475,6 +476,10 @@ const std::map CLKernelLibrary::_program_source_map = { "remap.cl", #include "./cl_kernels/remap.clembed" + }, + { + "roi_pooling_layer.cl", +#include "./cl_kernels/roi_pooling_layer.clembed" }, { "scale.cl", diff --git a/src/core/CL/ICLKernel.cpp b/src/core/CL/ICLKernel.cpp index 12af8c68c1..7a95374bbf 100644 --- a/src/core/CL/ICLKernel.cpp +++ b/src/core/CL/ICLKernel.cpp @@ -69,12 +69,6 @@ cl::Kernel &ICLKernel::kernel() return _kernel; } -template -unsigned int ICLKernel::num_arguments_per_tensor() const -{ - return 2 + 2 * dimension_size; -} - template void ICLKernel::add_tensor_argument(unsigned &idx, const ICLTensor *tensor, const Window &window) { @@ -127,6 +121,11 @@ void ICLKernel::add_4D_tensor_argument(unsigned int &idx, const ICLTensor *tenso add_tensor_argument<4>(idx, tensor, window); } +unsigned int ICLKernel::num_arguments_per_1D_array() const +{ + return num_arguments_per_array<1>(); +} + unsigned int ICLKernel::num_arguments_per_1D_tensor() const { return num_arguments_per_tensor<1>(); diff --git a/src/core/CL/cl_kernels/roi_pooling_layer.cl b/src/core/CL/cl_kernels/roi_pooling_layer.cl new file mode 100644 index 0000000000..35a9c0a21f --- /dev/null +++ b/src/core/CL/cl_kernels/roi_pooling_layer.cl @@ -0,0 +1,169 @@ +/* + * Copyright (c) 2017 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "helpers.h" + +#if DATA_SIZE == 32 +#define VEC_SIZE 4 +#define VEC_MAX vec4_max +#elif DATA_SIZE == 16 +#define VEC_SIZE 8 +#define VEC_MAX vec8_max +#else /* DATA_SIZE not equals 32 or 16 */ +#error "Unsupported data size" +#endif /* DATA_SIZE == 32 */ + +inline DATA_TYPE vec4_max(VEC_DATA_TYPE(DATA_TYPE, 4) vec) +{ + VEC_DATA_TYPE(DATA_TYPE, 2) + temp = fmax(vec.lo, vec.hi); + return fmax(temp.x, temp.y); +} + +inline DATA_TYPE vec8_max(VEC_DATA_TYPE(DATA_TYPE, 8) vec) +{ + VEC_DATA_TYPE(DATA_TYPE, 4) + temp = fmax(vec.lo, vec.hi); + return vec4_max(temp); +} + +/** Performs a roi pooling on a single output pixel. + * + * @param[in] input Pointer to input Tensor3D struct. + * @param[in] region_start_x Start x index projected onto the input tensor. + * @param[in] region_end_x End x index projected onto the input tensor. + * @param[in] region_start_y Start y index projected onto the input tensor. + * @param[in] region_end_y End y index projected onto the input tensor. + * @param[in] pz z index of the input tensor. + * + * @return A max pooled value from the region specified in the input tensor. + */ +inline DATA_TYPE roi_pool_1x1(const Tensor3D *input, int region_start_x, int region_end_x, int region_start_y, int region_end_y, int pz) +{ + // Iterate through the pooling region + if((region_end_x <= region_start_x) || (region_end_y <= region_start_y)) + { + return (DATA_TYPE)0; + } + else + { + int num_iter = (int)((region_end_x - region_start_x) / VEC_SIZE); + VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) + curr_max = (VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE))(-FLT_MAX); + for(int j = region_start_y; j < region_end_y; ++j) + { + int i = region_start_x; + for(; i < region_start_x + num_iter * VEC_SIZE; i += VEC_SIZE) + { + VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) + val = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)tensor3D_offset(input, i, j, pz)); + curr_max = fmax(val, curr_max); + } + for(; i < region_end_x; ++i) + { + DATA_TYPE val = *(__global DATA_TYPE *)tensor3D_offset(input, i, j, pz); + curr_max = fmax(curr_max, val); + } + } + return (DATA_TYPE)VEC_MAX(curr_max); + } +} + +/** Performs a roi pooling function. + * + * @note Datatype must be passed using -DDATA_TYPE e.g. -DDATA_TYPE=float. Supported data types are F16, F32; + * @note Datasize must be passed using -DDATA_SIZE e.g. -DDATA_SIZE=32; + * @note Input dimensions must be passed using -DMAX_DIM_X, -DMAX_DIM_Y and -DMAX_DIM_Z; + * @note Pooled region dimensions must be passed using -DPOOLED_DIM_X and -DPOOLED_DIM_Y; + * @note Spatial scale must be passed using -DSPATIAL_SCALE; + * + * @param[in] input_ptr Pointer to the source image. Supported data types: F16, F32 + * @param[in] input_stride_x Stride of the source image in X dimension (in bytes) + * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] input_stride_y Stride of the source image in Y dimension (in bytes) + * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes) + * @param[in] input_stride_z Stride of the source tensor in Z dimension (in bytes) + * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes) + * @param[in] input_offset_first_element_in_bytes The offset of the first element in the pooled region of the source image as specifed by ROI + * @param[in] rois_ptr Pointer to the rois array. Layout: {x, y, width, height, batch_indx} + * @param[in] rois_stride_x Stride of the rois array in X dimension (in bytes) + * @param[in] rois_step_x rois_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] rois_offset_first_element_in_bytes The offset of the first element in the rois array + * @param[out] output_ptr Pointer to the destination image. Supported data types: F16, F32 + * @param[in] output_stride_x Stride of the destination image in X dimension (in bytes) + * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] output_stride_y Stride of the destination image in Y dimension (in bytes) + * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes) + * @param[in] output_stride_z Stride of the destination tensor in Z dimension (in bytes) + * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes) + * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination image + * @param[in] input_stride_w Stride of the source image in W dimension (in bytes) + * @param[in] output_stride_w Stride of the destination image in W dimension (in bytes) + */ +__kernel void roi_pooling_layer( + TENSOR3D_DECLARATION(input), + VECTOR_DECLARATION(rois), + TENSOR3D_DECLARATION(output), + unsigned int input_stride_w, unsigned int output_stride_w) +{ + // Get pixels pointer + Tensor3D input = CONVERT_TO_TENSOR3D_STRUCT_NO_STEP(input); + Vector rois = CONVERT_TO_VECTOR_STRUCT_NO_STEP(rois); + Tensor3D output = CONVERT_TO_TENSOR3D_STRUCT_NO_STEP(output); + + const int px = get_global_id(0); + const int py = get_global_id(1); + const int pw = get_global_id(2); + + // Load roi parameters + // roi is laid out as follows: + // { x, y, width, height, batch_index } + const ushort8 roi = vload8(0, (__global ushort *)vector_offset(&rois, pw)); + const int2 roi_anchor = convert_int2_sat(round(convert_float2(roi.s01) * (float)SPATIAL_SCALE)); + const int2 roi_dims = convert_int2_sat(fmax(round(convert_float2(roi.s23) * (float)SPATIAL_SCALE), 1.f)); + + // Determine pooled region in input image to pooled region in output image ratio + const float2 pool_region_ratio = convert_float2(roi_dims) / (float2)(POOLED_DIM_X, POOLED_DIM_Y); + + // Calculate pooled region start and end + const float2 spatial_indx = (float2)(px, py); + const int2 max_spatial_dims = (int2)(MAX_DIM_X, MAX_DIM_Y); + int2 region_start = convert_int2_sat(floor(spatial_indx * pool_region_ratio)) + roi_anchor; + int2 region_end = convert_int2_sat(ceil((spatial_indx + 1) * pool_region_ratio)) + roi_anchor; + + region_start = clamp(region_start, 0, max_spatial_dims); + region_end = clamp(region_end, 0, max_spatial_dims); + + // Move input and output pointer across the fourth dimension + input.ptr += roi.s4 * input_stride_w; + output.ptr += pw * output_stride_w; + + for(int pz = 0; pz < MAX_DIM_Z; ++pz) + { + *(__global DATA_TYPE *)tensor3D_offset(&output, px, py, pz) = (__global DATA_TYPE)roi_pool_1x1(&input, + region_start.x, + region_end.x, + region_start.y, + region_end.y, pz); + } +} diff --git a/src/core/CL/kernels/CLROIPoolingLayerKernel.cpp b/src/core/CL/kernels/CLROIPoolingLayerKernel.cpp new file mode 100644 index 0000000000..4e000c61b1 --- /dev/null +++ b/src/core/CL/kernels/CLROIPoolingLayerKernel.cpp @@ -0,0 +1,121 @@ +/* + * Copyright (c) 2017 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/CLROIPoolingLayerKernel.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/ICLArray.h" +#include "arm_compute/core/CL/ICLTensor.h" +#include "arm_compute/core/CL/OpenCL.h" +#include "arm_compute/core/Helpers.h" +#include "arm_compute/core/TensorInfo.h" +#include "arm_compute/core/Utils.h" +#include "arm_compute/core/Validate.h" +#include "arm_compute/core/Window.h" + +#include +#include +#include + +using namespace arm_compute; + +CLROIPoolingLayerKernel::CLROIPoolingLayerKernel() + : _input(nullptr), _rois(nullptr), _output(nullptr), _pool_info(0, 0, 0.f) +{ +} + +void CLROIPoolingLayerKernel::configure(const ICLTensor *input, const ICLROIArray *rois, ICLTensor *output, const ROIPoolingLayerInfo &pool_info) +{ + ARM_COMPUTE_ERROR_ON_NULLPTR(input, rois, output); + ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32); + ARM_COMPUTE_ERROR_ON((pool_info.pooled_width() == 0) || (pool_info.pooled_height() == 0)); + ARM_COMPUTE_ERROR_ON(rois->num_values() == 0); + + // Output auto inizialitation if not yet initialized + TensorShape output_shape(pool_info.pooled_width(), pool_info.pooled_height(), input->info()->dimension(2), rois->num_values()); + auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type(), input->info()->fixed_point_position()); + + ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); + ARM_COMPUTE_ERROR_ON((output->info()->dimension(0) != pool_info.pooled_width()) || (output->info()->dimension(1) != pool_info.pooled_height())); + ARM_COMPUTE_ERROR_ON(input->info()->dimension(2) != output->info()->dimension(2)); + ARM_COMPUTE_ERROR_ON(rois->num_values() != output->info()->dimension(3)); + + // Set instance variables + _input = input; + _rois = rois; + _output = output; + _pool_info = pool_info; + + // Set build options + std::set build_opts; + build_opts.emplace(("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()))); + build_opts.emplace(("-DDATA_SIZE=" + get_data_size_from_data_type(input->info()->data_type()))); + build_opts.emplace(("-DMAX_DIM_X=" + support::cpp11::to_string(_input->info()->dimension(Window::DimX)))); + build_opts.emplace(("-DMAX_DIM_Y=" + support::cpp11::to_string(_input->info()->dimension(Window::DimY)))); + build_opts.emplace(("-DMAX_DIM_Z=" + support::cpp11::to_string(_input->info()->dimension(Window::DimZ)))); + build_opts.emplace(("-DPOOLED_DIM_X=" + support::cpp11::to_string(pool_info.pooled_width()))); + build_opts.emplace(("-DPOOLED_DIM_Y=" + support::cpp11::to_string(pool_info.pooled_height()))); + build_opts.emplace(("-DSPATIAL_SCALE=" + support::cpp11::to_string(pool_info.spatial_scale()))); + + // Create kernel + std::string kernel_name = "roi_pooling_layer"; + _kernel = static_cast(CLKernelLibrary::get().create_kernel(kernel_name, build_opts)); + + // Set static kernel arguments + unsigned int idx = 2 * num_arguments_per_3D_tensor() + num_arguments_per_1D_array(); + add_argument(idx, _input->info()->strides_in_bytes()[3]); + add_argument(idx, _output->info()->strides_in_bytes()[3]); + + // Configure kernel window + const unsigned int num_elems_processed_per_iteration = 1; + Window window = calculate_max_window(*output->info(), Steps(num_elems_processed_per_iteration)); + AccessWindowStatic input_access(input->info(), + input->info()->valid_region().start(0), + input->info()->valid_region().start(1), + input->info()->valid_region().end(0), + input->info()->valid_region().end(1)); + AccessWindowStatic output_access(output->info(), 0, 0, pool_info.pooled_width(), pool_info.pooled_height()); + + update_window_and_padding(window, input_access, output_access); + output_access.set_valid_region(window, ValidRegion(Coordinates(), output->info()->tensor_shape())); + ICLKernel::configure(window); +} + +void CLROIPoolingLayerKernel::run(const Window &window, cl::CommandQueue &queue) +{ + ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); + ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); + + Window slice(window); + // Parallelize spatially and across the fourth dimension of the output tensor (also across ROIArray) + slice.set(Window::DimZ, window[3]); + + // Set arguments + unsigned int idx = 0; + add_3D_tensor_argument(idx, _input, slice); + add_1D_array_argument(idx, _rois, Strides(sizeof(ROI)), 1U, slice); + add_3D_tensor_argument(idx, _output, slice); + enqueue(queue, *this, slice); +} -- cgit v1.2.1