blob: 5acc3ac3d688d7eaeddc46578f7122c3f8d870a7 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2016-2020 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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 Spyroud1794eb2018-06-15 16:15:26 +010026#include "arm_compute/core/CL/CLHelpers.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#include "arm_compute/core/CL/CLKernelLibrary.h"
28#include "arm_compute/core/CL/ICLTensor.h"
29#include "arm_compute/core/CL/OpenCL.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030#include "arm_compute/core/Helpers.h"
31#include "arm_compute/core/TensorInfo.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010032#include "src/core/CL/CLValidate.h"
33#include "src/core/helpers/WindowHelpers.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034
35#include <cmath>
36#include <set>
37#include <string>
38
39using namespace arm_compute;
40
41CLMeanStdDevKernel::CLMeanStdDevKernel()
Giorgio Arenaa2611812017-07-21 10:08:48 +010042 : _input(nullptr), _mean(nullptr), _stddev(nullptr), _global_sum(nullptr), _global_sum_squared(nullptr), _border_size(0)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010043{
44}
45
Giorgio Arenaa2611812017-07-21 10:08:48 +010046BorderSize CLMeanStdDevKernel::border_size() const
47{
48 return _border_size;
49}
50
Michalis Spyroud1794eb2018-06-15 16:15:26 +010051Status CLMeanStdDevKernel::validate(const ITensorInfo *input, float *mean, cl::Buffer *global_sum, float *stddev, cl::Buffer *global_sum_squared)
52{
53 ARM_COMPUTE_UNUSED(mean);
54 ARM_COMPUTE_UNUSED(stddev);
55 ARM_COMPUTE_UNUSED(global_sum);
56 ARM_COMPUTE_UNUSED(global_sum_squared);
57 ARM_COMPUTE_RETURN_ERROR_ON_INT64_BASE_ATOMICS_UNSUPPORTED();
58 ARM_COMPUTE_RETURN_ERROR_ON_TENSOR_NOT_2D(input);
59 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
60
61 return Status{};
62}
63
Anthony Barbier6ff3b192017-09-04 18:44:23 +010064void CLMeanStdDevKernel::configure(const ICLImage *input, float *mean, cl::Buffer *global_sum, float *stddev, cl::Buffer *global_sum_squared)
65{
Manuel Bottini4c6bd512020-04-08 10:15:51 +010066 configure(CLKernelLibrary::get().get_compile_context(), input, mean, global_sum, stddev, global_sum_squared);
67}
68
Manuel Bottini679fc962020-04-21 16:08:53 +010069void CLMeanStdDevKernel::configure(const CLCompileContext &compile_context, const ICLImage *input, float *mean, cl::Buffer *global_sum, float *stddev, cl::Buffer *global_sum_squared)
Manuel Bottini4c6bd512020-04-08 10:15:51 +010070{
Michalis Spyroud1794eb2018-06-15 16:15:26 +010071 ARM_COMPUTE_ERROR_ON_NULLPTR(input, mean, global_sum);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010072 ARM_COMPUTE_ERROR_ON(stddev && nullptr == global_sum_squared);
Michalis Spyroud1794eb2018-06-15 16:15:26 +010073 ARM_COMPUTE_ERROR_THROW_ON(CLMeanStdDevKernel::validate(input->info(), mean, global_sum, stddev, global_sum_squared));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010074
75 _input = input;
76 _mean = mean;
77 _stddev = stddev;
78 _global_sum = global_sum;
79 _global_sum_squared = global_sum_squared;
80
81 // Create kernel
82 std::set<std::string> build_opts;
83
84 if(_stddev != nullptr)
85 {
86 build_opts.insert("-DSTDDEV");
87 }
88
Manuel Bottini4c6bd512020-04-08 10:15:51 +010089 _kernel = create_kernel(compile_context, "mean_stddev_accumulate", build_opts);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010090
91 // Set fixed arguments
92 unsigned int idx = num_arguments_per_2D_tensor(); //Skip the input parameters
93
94 _kernel.setArg(idx++, static_cast<cl_uint>(input->info()->dimension(1)));
95 _kernel.setArg(idx++, *_global_sum);
96
97 if(_stddev != nullptr)
98 {
99 _kernel.setArg(idx++, *_global_sum_squared);
100 }
101
102 // Configure kernel window
103 constexpr unsigned int num_elems_processed_per_iteration_x = 8;
104 const unsigned int num_elems_processed_per_iteration_y = input->info()->dimension(1);
105
Giorgio Arenafc2817d2017-06-27 17:26:37 +0100106 _border_size = BorderSize(ceil_to_multiple(input->info()->dimension(0), num_elems_processed_per_iteration_x) - input->info()->dimension(0));
Giorgio Arenaa2611812017-07-21 10:08:48 +0100107
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100108 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y));
109 AccessWindowRectangle input_access(input->info(), 0, 0, num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y);
110 update_window_and_padding(win, input_access);
111
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100112 ICLKernel::configure_internal(win);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100113}
114
115void CLMeanStdDevKernel::run(const Window &window, cl::CommandQueue &queue)
116{
117 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
118 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
119
120 // Clear sums
121 static const cl_ulong zero = 0;
122 queue.enqueueWriteBuffer(*_global_sum, CL_FALSE, 0, sizeof(cl_ulong), &zero);
123
124 if(_stddev != nullptr)
125 {
126 queue.enqueueWriteBuffer(*_global_sum_squared, CL_FALSE, 0, sizeof(cl_ulong), &zero);
127 }
128
129 Window slice = window.first_slice_window_2D();
130
131 do
132 {
133 unsigned int idx = 0;
134 add_2D_tensor_argument(idx, _input, slice);
135 // Set slice step equal to height to force gws[1] to 1,
136 // as each thread calculates the sum across all rows and columns equal to the number of elements processed by each work-item
137 slice.set_dimension_step(Window::DimY, _input->info()->dimension(1));
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100138 enqueue(queue, *this, slice, lws_hint());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100139 }
140 while(window.slide_window_slice_2D(slice));
141
142 // Calculate mean and stddev
143 cl_ulong global_sum = 0;
144 cl_ulong global_sum_squared = 0;
145 const float num_pixels = _input->info()->dimension(0) * _input->info()->dimension(1);
146
147 queue.enqueueReadBuffer(*_global_sum, CL_TRUE, 0, sizeof(cl_ulong), static_cast<void *>(&global_sum));
148 const float mean = global_sum / num_pixels;
149 *_mean = mean;
150
151 if(_stddev != nullptr)
152 {
153 queue.enqueueReadBuffer(*_global_sum_squared, CL_TRUE, 0, sizeof(cl_ulong), static_cast<void *>(&global_sum_squared));
154 *_stddev = std::sqrt((global_sum_squared / num_pixels) - (mean * mean));
155 }
156}