Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 2 | * Copyright (c) 2016-2020 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #include "arm_compute/core/CL/kernels/CLMeanStdDevKernel.h" |
| 25 | |
Michalis Spyrou | d1794eb | 2018-06-15 16:15:26 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/CL/CLHelpers.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
Vidhya Sudhan Loganathan | 76c8564 | 2018-05-25 13:53:02 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/CL/CLValidate.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/CL/ICLTensor.h" |
| 30 | #include "arm_compute/core/CL/OpenCL.h" |
| 31 | #include "arm_compute/core/Error.h" |
| 32 | #include "arm_compute/core/Helpers.h" |
| 33 | #include "arm_compute/core/TensorInfo.h" |
| 34 | #include "arm_compute/core/Types.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 35 | #include "arm_compute/core/Window.h" |
| 36 | |
| 37 | #include <cmath> |
| 38 | #include <set> |
| 39 | #include <string> |
| 40 | |
| 41 | using namespace arm_compute; |
| 42 | |
| 43 | CLMeanStdDevKernel::CLMeanStdDevKernel() |
Giorgio Arena | a261181 | 2017-07-21 10:08:48 +0100 | [diff] [blame] | 44 | : _input(nullptr), _mean(nullptr), _stddev(nullptr), _global_sum(nullptr), _global_sum_squared(nullptr), _border_size(0) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 45 | { |
| 46 | } |
| 47 | |
Giorgio Arena | a261181 | 2017-07-21 10:08:48 +0100 | [diff] [blame] | 48 | BorderSize CLMeanStdDevKernel::border_size() const |
| 49 | { |
| 50 | return _border_size; |
| 51 | } |
| 52 | |
Michalis Spyrou | d1794eb | 2018-06-15 16:15:26 +0100 | [diff] [blame] | 53 | Status CLMeanStdDevKernel::validate(const ITensorInfo *input, float *mean, cl::Buffer *global_sum, float *stddev, cl::Buffer *global_sum_squared) |
| 54 | { |
| 55 | ARM_COMPUTE_UNUSED(mean); |
| 56 | ARM_COMPUTE_UNUSED(stddev); |
| 57 | ARM_COMPUTE_UNUSED(global_sum); |
| 58 | ARM_COMPUTE_UNUSED(global_sum_squared); |
| 59 | ARM_COMPUTE_RETURN_ERROR_ON_INT64_BASE_ATOMICS_UNSUPPORTED(); |
| 60 | ARM_COMPUTE_RETURN_ERROR_ON_TENSOR_NOT_2D(input); |
| 61 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8); |
| 62 | |
| 63 | return Status{}; |
| 64 | } |
| 65 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 66 | void CLMeanStdDevKernel::configure(const ICLImage *input, float *mean, cl::Buffer *global_sum, float *stddev, cl::Buffer *global_sum_squared) |
| 67 | { |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 68 | configure(CLKernelLibrary::get().get_compile_context(), input, mean, global_sum, stddev, global_sum_squared); |
| 69 | } |
| 70 | |
Manuel Bottini | 679fc96 | 2020-04-21 16:08:53 +0100 | [diff] [blame^] | 71 | void CLMeanStdDevKernel::configure(const CLCompileContext &compile_context, const ICLImage *input, float *mean, cl::Buffer *global_sum, float *stddev, cl::Buffer *global_sum_squared) |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 72 | { |
Michalis Spyrou | d1794eb | 2018-06-15 16:15:26 +0100 | [diff] [blame] | 73 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, mean, global_sum); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 74 | ARM_COMPUTE_ERROR_ON(stddev && nullptr == global_sum_squared); |
Michalis Spyrou | d1794eb | 2018-06-15 16:15:26 +0100 | [diff] [blame] | 75 | ARM_COMPUTE_ERROR_THROW_ON(CLMeanStdDevKernel::validate(input->info(), mean, global_sum, stddev, global_sum_squared)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 76 | |
| 77 | _input = input; |
| 78 | _mean = mean; |
| 79 | _stddev = stddev; |
| 80 | _global_sum = global_sum; |
| 81 | _global_sum_squared = global_sum_squared; |
| 82 | |
| 83 | // Create kernel |
| 84 | std::set<std::string> build_opts; |
| 85 | |
| 86 | if(_stddev != nullptr) |
| 87 | { |
| 88 | build_opts.insert("-DSTDDEV"); |
| 89 | } |
| 90 | |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 91 | _kernel = create_kernel(compile_context, "mean_stddev_accumulate", build_opts); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 92 | |
| 93 | // Set fixed arguments |
| 94 | unsigned int idx = num_arguments_per_2D_tensor(); //Skip the input parameters |
| 95 | |
| 96 | _kernel.setArg(idx++, static_cast<cl_uint>(input->info()->dimension(1))); |
| 97 | _kernel.setArg(idx++, *_global_sum); |
| 98 | |
| 99 | if(_stddev != nullptr) |
| 100 | { |
| 101 | _kernel.setArg(idx++, *_global_sum_squared); |
| 102 | } |
| 103 | |
| 104 | // Configure kernel window |
| 105 | constexpr unsigned int num_elems_processed_per_iteration_x = 8; |
| 106 | const unsigned int num_elems_processed_per_iteration_y = input->info()->dimension(1); |
| 107 | |
Giorgio Arena | fc2817d | 2017-06-27 17:26:37 +0100 | [diff] [blame] | 108 | _border_size = BorderSize(ceil_to_multiple(input->info()->dimension(0), num_elems_processed_per_iteration_x) - input->info()->dimension(0)); |
Giorgio Arena | a261181 | 2017-07-21 10:08:48 +0100 | [diff] [blame] | 109 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 110 | Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y)); |
| 111 | AccessWindowRectangle input_access(input->info(), 0, 0, num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y); |
| 112 | update_window_and_padding(win, input_access); |
| 113 | |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 114 | ICLKernel::configure_internal(win); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | void CLMeanStdDevKernel::run(const Window &window, cl::CommandQueue &queue) |
| 118 | { |
| 119 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 120 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 121 | |
| 122 | // Clear sums |
| 123 | static const cl_ulong zero = 0; |
| 124 | queue.enqueueWriteBuffer(*_global_sum, CL_FALSE, 0, sizeof(cl_ulong), &zero); |
| 125 | |
| 126 | if(_stddev != nullptr) |
| 127 | { |
| 128 | queue.enqueueWriteBuffer(*_global_sum_squared, CL_FALSE, 0, sizeof(cl_ulong), &zero); |
| 129 | } |
| 130 | |
| 131 | Window slice = window.first_slice_window_2D(); |
| 132 | |
| 133 | do |
| 134 | { |
| 135 | unsigned int idx = 0; |
| 136 | add_2D_tensor_argument(idx, _input, slice); |
| 137 | // Set slice step equal to height to force gws[1] to 1, |
| 138 | // as each thread calculates the sum across all rows and columns equal to the number of elements processed by each work-item |
| 139 | slice.set_dimension_step(Window::DimY, _input->info()->dimension(1)); |
Georgios Pinitas | 275f99c | 2019-08-23 12:44:11 +0100 | [diff] [blame] | 140 | enqueue(queue, *this, slice, lws_hint()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 141 | } |
| 142 | while(window.slide_window_slice_2D(slice)); |
| 143 | |
| 144 | // Calculate mean and stddev |
| 145 | cl_ulong global_sum = 0; |
| 146 | cl_ulong global_sum_squared = 0; |
| 147 | const float num_pixels = _input->info()->dimension(0) * _input->info()->dimension(1); |
| 148 | |
| 149 | queue.enqueueReadBuffer(*_global_sum, CL_TRUE, 0, sizeof(cl_ulong), static_cast<void *>(&global_sum)); |
| 150 | const float mean = global_sum / num_pixels; |
| 151 | *_mean = mean; |
| 152 | |
| 153 | if(_stddev != nullptr) |
| 154 | { |
| 155 | queue.enqueueReadBuffer(*_global_sum_squared, CL_TRUE, 0, sizeof(cl_ulong), static_cast<void *>(&global_sum_squared)); |
| 156 | *_stddev = std::sqrt((global_sum_squared / num_pixels) - (mean * mean)); |
| 157 | } |
| 158 | } |