From e1651a55c637708347c9adda4a77f1deffacf7d2 Mon Sep 17 00:00:00 2001 From: Michalis Spyrou Date: Thu, 11 Jul 2019 15:00:49 +0100 Subject: COMPMID-2414 Add add_2D_tensor_argument_if for OpenCL Change-Id: I8675b4b4face0be9416ae6c7a7023234d50fc0cb Signed-off-by: Michalis Spyrou Reviewed-on: https://review.mlplatform.org/c/1524 Comments-Addressed: Arm Jenkins Reviewed-by: Michele Di Giorgio Tested-by: Arm Jenkins --- arm_compute/core/CL/ICLKernel.h | 28 ++++++++++++++++++++++ src/core/CL/kernels/CLChannelCombineKernel.cpp | 12 ++-------- src/core/CL/kernels/CLDerivativeKernel.cpp | 12 ++-------- .../kernels/CLGEMMLowpOffsetContributionKernel.cpp | 18 ++++---------- ...GEMMLowpOffsetContributionOutputStageKernel.cpp | 15 +++--------- .../kernels/CLGEMMMatrixMultiplyReshapedKernel.cpp | 5 +--- .../CLGEMMMatrixMultiplyReshapedOnlyRHSKernel.cpp | 5 +--- src/core/CL/kernels/CLMagnitudePhaseKernel.cpp | 12 ++-------- .../CL/kernels/CLMeanStdDevNormalizationKernel.cpp | 6 ++--- src/core/CL/kernels/CLScharr3x3Kernel.cpp | 14 +++-------- src/core/CL/kernels/CLSobel3x3Kernel.cpp | 12 ++-------- src/core/CL/kernels/CLSobel5x5Kernel.cpp | 28 +++++----------------- src/core/CL/kernels/CLSobel7x7Kernel.cpp | 27 +++++---------------- src/core/CL/kernels/CLSpaceToBatchLayerKernel.cpp | 13 +++++----- 14 files changed, 69 insertions(+), 138 deletions(-) diff --git a/arm_compute/core/CL/ICLKernel.h b/arm_compute/core/CL/ICLKernel.h index 590f8929cb..76b40b81d1 100644 --- a/arm_compute/core/CL/ICLKernel.h +++ b/arm_compute/core/CL/ICLKernel.h @@ -111,6 +111,20 @@ public: { add_tensor_argument<1>(idx, tensor, window); } + /** Add the passed 1D tensor's parameters to the object's kernel's arguments starting from the index idx if the condition is true. + * + * @param[in] cond Condition to check + * @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set. + * @param[in] tensor Tensor to set as an argument of the object's kernel. + * @param[in] window Window the kernel will be executed on. + */ + void add_1D_tensor_argument_if(bool cond, unsigned int &idx, const ICLTensor *tensor, const Window &window) + { + if(cond) + { + add_1D_tensor_argument(idx, tensor, window); + } + } /** Add the passed 2D tensor's parameters to the object's kernel's arguments starting from the index idx. * * @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set. @@ -121,6 +135,20 @@ public: { add_tensor_argument<2>(idx, tensor, window); } + /** Add the passed 2D tensor's parameters to the object's kernel's arguments starting from the index idx if the condition is true. + * + * @param[in] cond Condition to check + * @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set. + * @param[in] tensor Tensor to set as an argument of the object's kernel. + * @param[in] window Window the kernel will be executed on. + */ + void add_2D_tensor_argument_if(bool cond, unsigned int &idx, const ICLTensor *tensor, const Window &window) + { + if(cond) + { + add_2D_tensor_argument(idx, tensor, window); + } + } /** Add the passed 3D tensor's parameters to the object's kernel's arguments starting from the index idx. * * @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set. diff --git a/src/core/CL/kernels/CLChannelCombineKernel.cpp b/src/core/CL/kernels/CLChannelCombineKernel.cpp index 11cd8ec5d0..126614a599 100644 --- a/src/core/CL/kernels/CLChannelCombineKernel.cpp +++ b/src/core/CL/kernels/CLChannelCombineKernel.cpp @@ -262,11 +262,7 @@ void CLChannelCombineKernel::run(const Window &window, cl::CommandQueue &queue) add_2D_tensor_argument(idx, _planes[0], slice); add_2D_tensor_argument(idx, _planes[1], win_sub_plane1); add_2D_tensor_argument(idx, _planes[2], win_sub_plane2); - - if(nullptr != _planes[3]) - { - add_2D_tensor_argument(idx, _planes[3], slice); - } + add_2D_tensor_argument_if((nullptr != _planes[3]), idx, _planes[3], slice); // Set outputs if(nullptr != _output) // Single planar output @@ -280,11 +276,7 @@ void CLChannelCombineKernel::run(const Window &window, cl::CommandQueue &queue) add_2D_tensor_argument(idx, _output_multi->cl_plane(0), slice); add_2D_tensor_argument(idx, _output_multi->cl_plane(1), win_sub_plane1); - - if(3 == num_planes_from_format(_output_multi->info()->format())) - { - add_2D_tensor_argument(idx, _output_multi->cl_plane(2), win_sub_plane2); - } + add_2D_tensor_argument_if((3 == num_planes_from_format(_output_multi->info()->format())), idx, _output_multi->cl_plane(2), win_sub_plane2); _kernel.setArg(idx++, slice.y().end()); } diff --git a/src/core/CL/kernels/CLDerivativeKernel.cpp b/src/core/CL/kernels/CLDerivativeKernel.cpp index 0a247f9071..e6d694d875 100644 --- a/src/core/CL/kernels/CLDerivativeKernel.cpp +++ b/src/core/CL/kernels/CLDerivativeKernel.cpp @@ -142,16 +142,8 @@ void CLDerivativeKernel::run(const Window &window, cl::CommandQueue &queue) { unsigned int idx = 0; add_2D_tensor_argument(idx, _input, slice); - - if(_run_derivative_x) - { - add_2D_tensor_argument(idx, _output_x, slice); - } - - if(_run_derivative_y) - { - add_2D_tensor_argument(idx, _output_y, slice); - } + add_2D_tensor_argument_if((_run_derivative_x), idx, _output_x, slice); + add_2D_tensor_argument_if((_run_derivative_y), idx, _output_y, slice); enqueue(queue, *this, slice, lws_hint()); } diff --git a/src/core/CL/kernels/CLGEMMLowpOffsetContributionKernel.cpp b/src/core/CL/kernels/CLGEMMLowpOffsetContributionKernel.cpp index d348f2c06d..b0a3cf640a 100644 --- a/src/core/CL/kernels/CLGEMMLowpOffsetContributionKernel.cpp +++ b/src/core/CL/kernels/CLGEMMLowpOffsetContributionKernel.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2018 ARM Limited. + * Copyright (c) 2017-2019 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -243,18 +243,10 @@ void CLGEMMLowpOffsetContributionKernel::run(const Window &window, cl::CommandQu { unsigned int idx = 0; add_3D_tensor_argument(idx, _mm_result, slice); - if(_vector_sum_col != nullptr) - { - add_2D_tensor_argument(idx, _vector_sum_col, win_vector_sum_col); - } - if(_vector_sum_row != nullptr) - { - add_2D_tensor_argument(idx, _vector_sum_row, win_vector_sum_row); - } - if(_bias != nullptr) - { - add_1D_tensor_argument(idx, _bias, biases_slice); - } + add_2D_tensor_argument_if((_vector_sum_col != nullptr), idx, _vector_sum_col, win_vector_sum_col); + add_2D_tensor_argument_if((_vector_sum_row != nullptr), idx, _vector_sum_row, win_vector_sum_row); + add_1D_tensor_argument_if((_bias != nullptr), idx, _bias, biases_slice); + enqueue(queue, *this, slice, lws_hint()); } while(collapsed.slide_window_slice_3D(slice)); diff --git a/src/core/CL/kernels/CLGEMMLowpOffsetContributionOutputStageKernel.cpp b/src/core/CL/kernels/CLGEMMLowpOffsetContributionOutputStageKernel.cpp index 8fba342e74..1852262337 100644 --- a/src/core/CL/kernels/CLGEMMLowpOffsetContributionOutputStageKernel.cpp +++ b/src/core/CL/kernels/CLGEMMLowpOffsetContributionOutputStageKernel.cpp @@ -281,18 +281,9 @@ void CLGEMMLowpOffsetContributionOutputStageKernel::run(const Window &window, cl { unsigned int idx = 0; add_3D_tensor_argument(idx, _mm_result, slice); - if(_vector_sum_col != nullptr) - { - add_2D_tensor_argument(idx, _vector_sum_col, win_vector_sum_col); - } - if(_vector_sum_row != nullptr) - { - add_2D_tensor_argument(idx, _vector_sum_row, win_vector_sum_row); - } - if(_bias != nullptr) - { - add_1D_tensor_argument(idx, _bias, biases_slice); - } + add_2D_tensor_argument_if((_vector_sum_col != nullptr), idx, _vector_sum_col, win_vector_sum_col); + add_2D_tensor_argument_if((_vector_sum_row != nullptr), idx, _vector_sum_row, win_vector_sum_row); + add_1D_tensor_argument_if((_bias != nullptr), idx, _bias, biases_slice); add_3D_tensor_argument(idx, _output, slice); enqueue(queue, *this, slice, lws_hint()); } diff --git a/src/core/CL/kernels/CLGEMMMatrixMultiplyReshapedKernel.cpp b/src/core/CL/kernels/CLGEMMMatrixMultiplyReshapedKernel.cpp index fecb8b55f8..3ad0ffd514 100644 --- a/src/core/CL/kernels/CLGEMMMatrixMultiplyReshapedKernel.cpp +++ b/src/core/CL/kernels/CLGEMMMatrixMultiplyReshapedKernel.cpp @@ -362,10 +362,7 @@ void CLGEMMMatrixMultiplyReshapedKernel::run(const Window &window, cl::CommandQu unsigned int idx = 0; add_2D_tensor_argument(idx, _input0, slice); add_2D_tensor_argument(idx, _input1, slice_b); - if(_add_bias) - { - add_2D_tensor_argument(idx, _input2, slice); - } + add_2D_tensor_argument_if((_add_bias), idx, _input2, slice); add_2D_tensor_argument(idx, _output, slice); _kernel.setArg(idx++, static_cast(_k)); _kernel.setArg(idx++, static_cast(_input0->info()->strides_in_bytes()[2])); diff --git a/src/core/CL/kernels/CLGEMMMatrixMultiplyReshapedOnlyRHSKernel.cpp b/src/core/CL/kernels/CLGEMMMatrixMultiplyReshapedOnlyRHSKernel.cpp index 7258b7fb12..97c7984c0e 100644 --- a/src/core/CL/kernels/CLGEMMMatrixMultiplyReshapedOnlyRHSKernel.cpp +++ b/src/core/CL/kernels/CLGEMMMatrixMultiplyReshapedOnlyRHSKernel.cpp @@ -383,10 +383,7 @@ void CLGEMMMatrixMultiplyReshapedOnlyRHSKernel::run(const Window &window, cl::Co unsigned int idx = 0; add_2D_tensor_argument(idx, _input0, slice); add_2D_tensor_argument(idx, _input1, slice_b); - if(_add_bias) - { - add_2D_tensor_argument(idx, _input2, slice); - } + add_2D_tensor_argument_if((_add_bias), idx, _input2, slice); add_2D_tensor_argument(idx, _output, slice); _kernel.setArg(idx++, static_cast(_input0->info()->strides_in_bytes()[2])); _kernel.setArg(idx++, static_cast(_input1->info()->strides_in_bytes()[2])); diff --git a/src/core/CL/kernels/CLMagnitudePhaseKernel.cpp b/src/core/CL/kernels/CLMagnitudePhaseKernel.cpp index 354113bba5..0af738603e 100644 --- a/src/core/CL/kernels/CLMagnitudePhaseKernel.cpp +++ b/src/core/CL/kernels/CLMagnitudePhaseKernel.cpp @@ -161,16 +161,8 @@ void CLMagnitudePhaseKernel::run(const Window &window, cl::CommandQueue &queue) unsigned int idx = 0; add_2D_tensor_argument(idx, _gx, slice); add_2D_tensor_argument(idx, _gy, slice); - - if(_run_mag) - { - add_2D_tensor_argument(idx, _magnitude, slice); - } - - if(_run_phase) - { - add_2D_tensor_argument(idx, _phase, slice); - } + add_2D_tensor_argument_if((_run_mag), idx, _magnitude, slice); + add_2D_tensor_argument_if((_run_phase), idx, _phase, slice); enqueue(queue, *this, slice, lws_hint()); } diff --git a/src/core/CL/kernels/CLMeanStdDevNormalizationKernel.cpp b/src/core/CL/kernels/CLMeanStdDevNormalizationKernel.cpp index a9baf24fa6..b110bac6c2 100644 --- a/src/core/CL/kernels/CLMeanStdDevNormalizationKernel.cpp +++ b/src/core/CL/kernels/CLMeanStdDevNormalizationKernel.cpp @@ -140,10 +140,8 @@ void CLMeanStdDevNormalizationKernel::run(const Window &window, cl::CommandQueue { unsigned int idx = 0; add_2D_tensor_argument(idx, _input, slice); - if(!_run_in_place) - { - add_2D_tensor_argument(idx, _output, slice); - } + add_2D_tensor_argument_if((!_run_in_place), idx, _output, slice); + enqueue(queue, *this, slice, lws_hint()); } while(window.slide_window_slice_2D(slice)); diff --git a/src/core/CL/kernels/CLScharr3x3Kernel.cpp b/src/core/CL/kernels/CLScharr3x3Kernel.cpp index 5182390822..1c1fedca15 100644 --- a/src/core/CL/kernels/CLScharr3x3Kernel.cpp +++ b/src/core/CL/kernels/CLScharr3x3Kernel.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016-2018 ARM Limited. + * Copyright (c) 2016-2019 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -115,16 +115,8 @@ void CLScharr3x3Kernel::run(const Window &window, cl::CommandQueue &queue) { unsigned int idx = 0; add_2D_tensor_argument(idx, _input, slice); - - if(_run_scharr_x) - { - add_2D_tensor_argument(idx, _output_x, slice); - } - - if(_run_scharr_y) - { - add_2D_tensor_argument(idx, _output_y, slice); - } + add_2D_tensor_argument_if((_run_scharr_x), idx, _output_x, slice); + add_2D_tensor_argument_if((_run_scharr_y), idx, _output_y, slice); enqueue(queue, *this, slice); } diff --git a/src/core/CL/kernels/CLSobel3x3Kernel.cpp b/src/core/CL/kernels/CLSobel3x3Kernel.cpp index 1c2fbb0176..cde754184d 100644 --- a/src/core/CL/kernels/CLSobel3x3Kernel.cpp +++ b/src/core/CL/kernels/CLSobel3x3Kernel.cpp @@ -128,16 +128,8 @@ void CLSobel3x3Kernel::run(const Window &window, cl::CommandQueue &queue) { unsigned int idx = 0; add_2D_tensor_argument(idx, _input, slice); - - if(_run_sobel_x) - { - add_2D_tensor_argument(idx, _output_x, slice); - } - - if(_run_sobel_y) - { - add_2D_tensor_argument(idx, _output_y, slice); - } + add_2D_tensor_argument_if((_run_sobel_x), idx, _output_x, slice); + add_2D_tensor_argument_if((_run_sobel_y), idx, _output_y, slice); enqueue(queue, *this, slice, lws_hint()); } diff --git a/src/core/CL/kernels/CLSobel5x5Kernel.cpp b/src/core/CL/kernels/CLSobel5x5Kernel.cpp index f07c1c9098..09ccf140c6 100644 --- a/src/core/CL/kernels/CLSobel5x5Kernel.cpp +++ b/src/core/CL/kernels/CLSobel5x5Kernel.cpp @@ -127,16 +127,8 @@ void CLSobel5x5HorKernel::run(const Window &window, cl::CommandQueue &queue) { unsigned int idx = 0; add_2D_tensor_argument(idx, _input, slice); - - if(_run_sobel_x) - { - add_2D_tensor_argument(idx, _output_x, slice); - } - - if(_run_sobel_y) - { - add_2D_tensor_argument(idx, _output_y, slice); - } + add_2D_tensor_argument_if((_run_sobel_x), idx, _output_x, slice); + add_2D_tensor_argument_if((_run_sobel_y), idx, _output_y, slice); enqueue(queue, *this, slice, lws_hint()); } @@ -237,18 +229,10 @@ void CLSobel5x5VertKernel::run(const Window &window, cl::CommandQueue &queue) do { unsigned int idx = 0; - - if(_run_sobel_x) - { - add_2D_tensor_argument(idx, _input_x, slice); - add_2D_tensor_argument(idx, _output_x, slice); - } - - if(_run_sobel_y) - { - add_2D_tensor_argument(idx, _input_y, slice); - add_2D_tensor_argument(idx, _output_y, slice); - } + add_2D_tensor_argument_if((_run_sobel_x), idx, _input_x, slice); + add_2D_tensor_argument_if((_run_sobel_x), idx, _output_x, slice); + add_2D_tensor_argument_if((_run_sobel_y), idx, _input_y, slice); + add_2D_tensor_argument_if((_run_sobel_y), idx, _output_y, slice); _kernel.setArg(idx++, 0 /*dummy*/); diff --git a/src/core/CL/kernels/CLSobel7x7Kernel.cpp b/src/core/CL/kernels/CLSobel7x7Kernel.cpp index 8c0b6a239f..6d65ed9e38 100644 --- a/src/core/CL/kernels/CLSobel7x7Kernel.cpp +++ b/src/core/CL/kernels/CLSobel7x7Kernel.cpp @@ -129,16 +129,8 @@ void CLSobel7x7HorKernel::run(const Window &window, cl::CommandQueue &queue) { unsigned int idx = 0; add_2D_tensor_argument(idx, _input, slice); - - if(_run_sobel_x) - { - add_2D_tensor_argument(idx, _output_x, slice); - } - - if(_run_sobel_y) - { - add_2D_tensor_argument(idx, _output_y, slice); - } + add_2D_tensor_argument_if((_run_sobel_x), idx, _output_x, slice); + add_2D_tensor_argument_if((_run_sobel_y), idx, _output_y, slice); enqueue(queue, *this, slice, lws_hint()); } @@ -241,17 +233,10 @@ void CLSobel7x7VertKernel::run(const Window &window, cl::CommandQueue &queue) { unsigned int idx = 0; - if(_run_sobel_x) - { - add_2D_tensor_argument(idx, _input_x, slice); - add_2D_tensor_argument(idx, _output_x, slice); - } - - if(_run_sobel_y) - { - add_2D_tensor_argument(idx, _input_y, slice); - add_2D_tensor_argument(idx, _output_y, slice); - } + add_2D_tensor_argument_if((_run_sobel_x), idx, _input_x, slice); + add_2D_tensor_argument_if((_run_sobel_x), idx, _output_x, slice); + add_2D_tensor_argument_if((_run_sobel_y), idx, _input_y, slice); + add_2D_tensor_argument_if((_run_sobel_y), idx, _output_y, slice); _kernel.setArg(idx++, 0 /*dummy*/); diff --git a/src/core/CL/kernels/CLSpaceToBatchLayerKernel.cpp b/src/core/CL/kernels/CLSpaceToBatchLayerKernel.cpp index f0391989a7..1492f34f27 100644 --- a/src/core/CL/kernels/CLSpaceToBatchLayerKernel.cpp +++ b/src/core/CL/kernels/CLSpaceToBatchLayerKernel.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018 ARM Limited. + * Copyright (c) 2018-2019 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -192,13 +192,12 @@ void CLSpaceToBatchLayerKernel::run(const Window &window, cl::CommandQueue &queu int batch_id = 0; do { - unsigned int idx = 0; + unsigned int idx = 0; + const bool cond = (_paddings != nullptr && _block_shape != nullptr); add_4D_tensor_argument(idx, _input, slice_in); - if(_paddings != nullptr && _block_shape != nullptr) - { - add_2D_tensor_argument(idx, _paddings, padding_slice); - add_1D_tensor_argument(idx, _block_shape, vector_slice); - } + add_2D_tensor_argument_if(cond, idx, _paddings, padding_slice); + add_1D_tensor_argument_if(cond, idx, _block_shape, vector_slice); + add_argument(idx, batch_id); add_3D_tensor_argument(idx, _output, slice_out); enqueue(queue, *this, slice_out); -- cgit v1.2.1