From 8aa985e6cd553f4e2cee6cab74b82fa626896299 Mon Sep 17 00:00:00 2001 From: Gian Marco Iodice Date: Tue, 27 Nov 2018 15:58:08 +0000 Subject: COMPMID-1725: Implement Pack Change-Id: I13f6e4c600f39355f69e015409bf30dafdc5e3aa Reviewed-on: https://review.mlplatform.org/332 Tested-by: Arm Jenkins Reviewed-by: Michele Di Giorgio --- src/core/CL/CLKernelLibrary.cpp | 5 + src/core/CL/cl_kernels/stack_layer.cl | 113 ++++++++++++++++++++++ src/core/CL/kernels/CLStackLayerKernel.cpp | 145 +++++++++++++++++++++++++++++ src/runtime/CL/functions/CLStackLayer.cpp | 85 +++++++++++++++++ 4 files changed, 348 insertions(+) create mode 100644 src/core/CL/cl_kernels/stack_layer.cl create mode 100644 src/core/CL/kernels/CLStackLayerKernel.cpp create mode 100644 src/runtime/CL/functions/CLStackLayer.cpp (limited to 'src') diff --git a/src/core/CL/CLKernelLibrary.cpp b/src/core/CL/CLKernelLibrary.cpp index ac1d4b349e..e48ff03e05 100644 --- a/src/core/CL/CLKernelLibrary.cpp +++ b/src/core/CL/CLKernelLibrary.cpp @@ -423,6 +423,7 @@ const std::map CLKernelLibrary::_kernel_program_map = { "space_to_batch_nhwc", "space_to_batch.cl" }, { "space_to_batch_static_nhwc", "space_to_batch.cl" }, { "softmax_layer_max_shift_exp_sum_parallel", "softmax_layer.cl" }, + { "stack_layer", "stack_layer.cl" }, { "strided_slice", "slice_ops.cl" }, { "suppress_non_maximum", "canny.cl" }, { "tablelookup_U8", "tablelookup.cl" }, @@ -863,6 +864,10 @@ const std::map CLKernelLibrary::_program_source_map = { "space_to_batch.cl", #include "./cl_kernels/space_to_batch.clembed" + }, + { + "stack_layer.cl", +#include "./cl_kernels/stack_layer.clembed" }, { "tablelookup.cl", diff --git a/src/core/CL/cl_kernels/stack_layer.cl b/src/core/CL/cl_kernels/stack_layer.cl new file mode 100644 index 0000000000..bed62662ad --- /dev/null +++ b/src/core/CL/cl_kernels/stack_layer.cl @@ -0,0 +1,113 @@ +/* + * 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 "helpers.h" + +#if defined(DATA_TYPE) && defined(AXIS) && defined(SRC_DIM2) && defined(DST_DIM3) + +#if AXIS == 0 +#define X_DST (idx_input) +#define Y_DST (x_src) +#define Z_DST (y_src) +#define W_DST (z_src) +#define K_DST (w_src) +#elif AXIS == 1 // AXIS == 1 +#define X_DST (x_src) +#define Y_DST (idx_input) +#define Z_DST (y_src) +#define W_DST (z_src) +#define K_DST (w_src) +#elif AXIS == 2 // AXIS == 2 +#define X_DST (x_src) +#define Y_DST (y_src) +#define Z_DST (idx_input) +#define W_DST (z_src) +#define K_DST (w_src) +#elif AXIS == 3 // AXIS == 3 +#define X_DST (x_src) +#define Y_DST (y_src) +#define Z_DST (z_src) +#define W_DST (idx_input) +#define K_DST (w_src) +#elif AXIS == 4 // AXIS == 4 +#define X_DST (x_src) +#define Y_DST (y_src) +#define Z_DST (z_src) +#define W_DST (w_src) +#define K_DST (idx_input) +#else // AXIS not supported +#error "Not supported axis" +#endif // AXIS == 0 + +/** OpenCL kernel to stack a rank-R tensor into one with rank-(R+1) along the axis dimension + * + * @note The data type has to be passed at compile time using -DDATA_TYPE. i.e. -DDATA_TYPE=float + * @note The dimension to stack the tensors along has to be passed at compile time using -DAXIS. i.e. -DAXIS=1 + * @note Dimension 2 of the input tensor must be passed at compile time using -DSRC_DIM2 (e.g. -DSRC_DIM2=112) + * @note Dimension 3 of the output tensor must be passed at compile time using -DDST_DIM3 (e.g. -DDST_DIM3=112) + * + * @param[in] src_ptr Pointer to the source tensor. Supported data types: U8/S8/QASYMM8/U16/S16/F16/U32/S32/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_stride_w Stride of the source tensor in W dimension (in bytes) + * @param[in] src_step_w src_stride_w * number of elements along W 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_stride_z Stride of the source tensor in Z dimension (in bytes) + * @param[in] dst_step_z dst_stride_z * number of elements along Z processed per workitem(in bytes) + * @param[in] dst_stride_w Stride of the source tensor in W dimension (in bytes) + * @param[in] dst_step_w dst_stride_w * number of elements along W 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] idx_input Index of the input tensor in the list of tensors to stack + */ +__kernel void stack_layer( + TENSOR4D_DECLARATION(src), + TENSOR4D_DECLARATION(dst), + unsigned int idx_input) +{ + uint x_src = get_global_id(0); + uint y_src = get_global_id(1); + uint z_src = (get_global_id(2) % SRC_DIM2); + uint w_src = (get_global_id(2) / SRC_DIM2); + + __global DATA_TYPE *src = (__global DATA_TYPE *)(src_ptr + src_offset_first_element_in_bytes + x_src * sizeof(DATA_TYPE) + y_src * src_stride_y + z_src * src_stride_z + w_src * src_stride_w); + + __global DATA_TYPE *dst = (__global DATA_TYPE *)(dst_ptr + dst_offset_first_element_in_bytes + X_DST * sizeof(DATA_TYPE) + Y_DST * dst_stride_y + Z_DST * dst_stride_z + W_DST * dst_stride_w + K_DST * + dst_stride_w * (uint)DST_DIM3); + + *dst = *src; +} + +#undef X_DST +#undef Y_DST +#undef Z_DST +#undef W_DST +#endif // defined(DATA_TYPE) && defined(AXIS) && defined(SRC_DIM2) && defined(DST_DIM3) diff --git a/src/core/CL/kernels/CLStackLayerKernel.cpp b/src/core/CL/kernels/CLStackLayerKernel.cpp new file mode 100644 index 0000000000..bac8992f7b --- /dev/null +++ b/src/core/CL/kernels/CLStackLayerKernel.cpp @@ -0,0 +1,145 @@ +/* + * 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/CLStackLayerKernel.h" + +#include "arm_compute/core/CL/CLHelpers.h" +#include "arm_compute/core/CL/CLKernelLibrary.h" +#include "arm_compute/core/CL/CLValidate.h" +#include "arm_compute/core/CL/ICLTensor.h" +#include "arm_compute/core/CL/OpenCL.h" +#include "arm_compute/core/Error.h" +#include "arm_compute/core/Helpers.h" +#include "arm_compute/core/IAccessWindow.h" +#include "arm_compute/core/TensorInfo.h" +#include "arm_compute/core/Utils.h" +#include "arm_compute/core/Window.h" +#include "arm_compute/core/utils/misc/ShapeCalculator.h" + +#include "support/ToolchainSupport.h" + +using namespace arm_compute; +using namespace arm_compute::misc::shape_calculator; + +namespace +{ +Status validate_arguments(const ITensorInfo *input, unsigned int axis, unsigned int idx_input, unsigned int num_tensors, const ITensorInfo *output) +{ + ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); + ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input); + ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::U8, DataType::S8, + DataType::U16, DataType::S16, DataType::U32, DataType::S32, + DataType::F16, DataType::F32); + ARM_COMPUTE_RETURN_ERROR_ON(idx_input >= num_tensors); + ARM_COMPUTE_RETURN_ERROR_ON(axis > input->num_dimensions()); + ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 4); + + if(output->total_size() != 0) + { + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), compute_stack_shape(*input, axis, num_tensors)); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); + } + + return Status{}; +} + +std::pair validate_and_configure_window(ITensorInfo *input, unsigned int axis, unsigned int num_tensors, ITensorInfo *output) +{ + // Output auto inizialitation if not yet initialized + auto_init_if_empty(*output, input->clone()->set_tensor_shape(compute_stack_shape(*input, axis, num_tensors))); + + // Configure kernel window + constexpr unsigned int num_elems_processed_per_iteration = 1; + + // The window needs to be based on input as we copy all the depths of input + Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration)); + + AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration); + AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration); + + bool window_changed = update_window_and_padding(win, input_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 + +CLStackLayerKernel::CLStackLayerKernel() + : _input(nullptr), _output(nullptr) +{ +} + +void CLStackLayerKernel::configure(const ICLTensor *input, unsigned int axis, unsigned int idx_input, unsigned int num_tensors, ICLTensor *output) +{ + ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); + ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), axis, idx_input, num_tensors, output->info())); + + _input = input; + _output = output; + + // Configure kernel window + auto win_config = validate_and_configure_window(input->info(), axis, num_tensors, output->info()); + + // Add build options + CLBuildOptions build_opts; + build_opts.add_option("-DDATA_TYPE=" + get_underlying_cl_type_from_data_type(input->info()->data_type())); + build_opts.add_option("-DAXIS=" + support::cpp11::to_string(axis)); + build_opts.add_option("-DSRC_DIM2=" + support::cpp11::to_string(input->info()->dimension(2))); + build_opts.add_option("-DDST_DIM3=" + support::cpp11::to_string(output->info()->dimension(3))); + + // Create kernel + _kernel = static_cast(CLKernelLibrary::get().create_kernel("stack_layer", build_opts.options())); + + ARM_COMPUTE_ERROR_THROW_ON(win_config.first); + ICLKernel::configure_internal(win_config.second); + + const unsigned int idx = 2 * num_arguments_per_4D_tensor(); + _kernel.setArg(idx, idx_input); +} + +Status CLStackLayerKernel::validate(const ITensorInfo *input, unsigned int axis, unsigned int idx_input, unsigned int num_tensors, const ITensorInfo *output) +{ + ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, axis, idx_input, num_tensors, output)); + ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), axis, num_tensors, output->clone().get()).first); + return Status{}; +} + +void CLStackLayerKernel::run(const Window &window, cl::CommandQueue &queue) +{ + ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); + ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); + + Window window_out; + window_out.use_tensor_dimensions(_output->info()->tensor_shape()); + + Window collapsed = window.collapse(ICLKernel::window(), Window::DimZ); + + Window slice_in = collapsed.first_slice_window_4D(); + Window slice_out = window_out.first_slice_window_4D(); + + unsigned int idx = 0; + add_4D_tensor_argument(idx, _input, slice_in); + add_4D_tensor_argument(idx, _output, slice_out); + enqueue(queue, *this, slice_in); +} diff --git a/src/runtime/CL/functions/CLStackLayer.cpp b/src/runtime/CL/functions/CLStackLayer.cpp new file mode 100644 index 0000000000..85adcad90c --- /dev/null +++ b/src/runtime/CL/functions/CLStackLayer.cpp @@ -0,0 +1,85 @@ +/* + * 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 + +#include "arm_compute/runtime/CL/functions/CLStackLayer.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/misc/ShapeCalculator.h" +#include "arm_compute/runtime/CL/CLScheduler.h" + +#include "support/ToolchainSupport.h" + +using namespace arm_compute; + +CLStackLayer::CLStackLayer() // NOLINT + : _input(), + _stack_kernels(), + _num_inputs(0) +{ +} + +void CLStackLayer::configure(const std::vector &input, int axis, ICLTensor *output) +{ + _num_inputs = input.size(); + _stack_kernels = arm_compute::support::cpp14::make_unique(_num_inputs); + + // Wrap around negative values + const unsigned int axis_u = wrap_around(axis, static_cast(input[0]->info()->num_dimensions() + 1)); + + for(unsigned int i = 0; i < _num_inputs; i++) + { + _stack_kernels[i].configure(input[i], axis_u, i, _num_inputs, output); + } +} + +Status CLStackLayer::validate(const std::vector &input, int axis, const ITensorInfo *output) +{ + ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output); + ARM_COMPUTE_RETURN_ERROR_ON(input.size() < 2); + + // Wrap around negative values + const unsigned int axis_u = wrap_around(axis, static_cast(input[0]->num_dimensions() + 1)); + + const unsigned int num_inputs = input.size(); + + for(unsigned int i = 0; i < num_inputs; i++) + { + ARM_COMPUTE_RETURN_ON_ERROR(CLStackLayerKernel::validate(input[i], axis_u, i, num_inputs, output)); + } + + return Status{}; +} + +void CLStackLayer::run() +{ + for(unsigned i = 0; i < _num_inputs; i++) + { + CLScheduler::get().enqueue(_stack_kernels[i], false); + } +} -- cgit v1.2.1