blob: e3ce704bfb3b7e4604be2b32b2580607ec9c23f4 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Manuel Bottini2b84be52020-04-08 10:15:51 +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 */
Michalis Spyroud1794eb2018-06-15 16:15:26 +010024#include "arm_compute/core/TensorInfo.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010025
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026#include "arm_compute/runtime/CL/CLScheduler.h"
Michalis Spyroud1794eb2018-06-15 16:15:26 +010027#include "arm_compute/runtime/CL/functions/CLMeanStdDev.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028
29using namespace arm_compute;
30
Michalis Spyroud1794eb2018-06-15 16:15:26 +010031CLMeanStdDev::CLMeanStdDev(std::shared_ptr<IMemoryManager> memory_manager) // NOLINT
32 : _memory_group(std::move(memory_manager)),
33 _data_type(),
34 _num_pixels(),
35 _run_stddev(),
36 _reduction_operation_mean(),
37 _reduction_operation_stddev(),
38 _reduction_output_mean(),
39 _reduction_output_stddev(),
40 _mean(nullptr),
41 _stddev(nullptr),
42 _mean_stddev_kernel(),
Giorgio Arenaa2611812017-07-21 10:08:48 +010043 _fill_border_kernel(),
Anthony Barbier6ff3b192017-09-04 18:44:23 +010044 _global_sum(),
45 _global_sum_squared()
46{
47}
48
Michalis Spyroud1794eb2018-06-15 16:15:26 +010049Status CLMeanStdDev::validate(ITensorInfo *input, float *mean, float *stddev)
50{
51 ARM_COMPUTE_RETURN_ERROR_ON_TENSOR_NOT_2D(input);
52 if(is_data_type_float(input->data_type()))
53 {
54 ARM_COMPUTE_UNUSED(mean);
55 ARM_COMPUTE_UNUSED(stddev);
56
57 TensorShape output_shape = TensorShape{ 1, input->dimension(1) };
58 TensorInfo output_shape_info = TensorInfo(output_shape, 1, DataType::U8);
59 return CLReductionOperation::validate(input, &output_shape_info, 0, ReductionOperation::SUM);
60 }
61 else
62 {
63 return CLMeanStdDevKernel::validate(input, mean, nullptr, stddev, nullptr);
64 }
65}
66
Giorgio Arenaa2611812017-07-21 10:08:48 +010067void CLMeanStdDev::configure(ICLImage *input, float *mean, float *stddev)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010068{
Manuel Bottini2b84be52020-04-08 10:15:51 +010069 configure(CLKernelLibrary::get().get_compile_context(), input, mean, stddev);
70}
71
72void CLMeanStdDev::configure(const CLCompileContext &compile_context, ICLImage *input, float *mean, float *stddev)
73{
Michalis Spyroud1794eb2018-06-15 16:15:26 +010074 // In the case of F16/F32 we call reduction operation for calculating CLMeanStdDev
75 _data_type = input->info()->data_type();
Anthony Barbier6ff3b192017-09-04 18:44:23 +010076
Michalis Spyroud1794eb2018-06-15 16:15:26 +010077 if(is_data_type_float(_data_type))
Anthony Barbier6ff3b192017-09-04 18:44:23 +010078 {
Michalis Spyroud1794eb2018-06-15 16:15:26 +010079 _num_pixels = input->info()->dimension(0) * input->info()->dimension(1);
80
81 _memory_group.manage(&_reduction_output_mean);
Manuel Bottini2b84be52020-04-08 10:15:51 +010082 _reduction_operation_mean.configure(compile_context, input, &_reduction_output_mean, 0, ReductionOperation::SUM);
Michalis Spyroud1794eb2018-06-15 16:15:26 +010083 _reduction_output_mean.allocator()->allocate();
84 _mean = mean;
85
86 if(stddev != nullptr)
87 {
88 _memory_group.manage(&_reduction_output_stddev);
Manuel Bottini2b84be52020-04-08 10:15:51 +010089 _reduction_operation_stddev.configure(compile_context, input, &_reduction_output_stddev, 0, ReductionOperation::SUM_SQUARE);
Michalis Spyroud1794eb2018-06-15 16:15:26 +010090 _reduction_output_stddev.allocator()->allocate();
91 _stddev = stddev;
92 _run_stddev = true;
93 }
94 }
95 else
96 {
97 _global_sum = cl::Buffer(CLScheduler::get().context(), CL_MEM_ALLOC_HOST_PTR | CL_MEM_READ_WRITE, sizeof(cl_ulong));
98
99 if(stddev != nullptr)
100 {
101 _global_sum_squared = cl::Buffer(CLScheduler::get().context(), CL_MEM_ALLOC_HOST_PTR | CL_MEM_READ_WRITE, sizeof(cl_ulong));
102 }
103
Manuel Bottini2b84be52020-04-08 10:15:51 +0100104 _mean_stddev_kernel.configure(compile_context, input, mean, &_global_sum, stddev, &_global_sum_squared);
105 _fill_border_kernel.configure(compile_context, input, _mean_stddev_kernel.border_size(), BorderMode::CONSTANT, PixelValue(static_cast<uint8_t>(0)));
Michalis Spyroud1794eb2018-06-15 16:15:26 +0100106 }
107}
108
109template <typename T>
110void CLMeanStdDev::run_float()
111{
Georgios Pinitasda953f22019-04-02 17:27:03 +0100112 MemoryGroupResourceScope scope_mg(_memory_group);
Michalis Spyroud1794eb2018-06-15 16:15:26 +0100113
114 // Perform reduction on x-axis
115 _reduction_operation_mean.run();
116 if(_run_stddev)
117 {
118 _reduction_operation_stddev.run();
119 _reduction_output_stddev.map(true);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100120 }
121
Michalis Spyroud1794eb2018-06-15 16:15:26 +0100122 _reduction_output_mean.map(true);
123
124 auto mean = static_cast<T>(0);
125
126 // Calculate final result for mean
127 for(unsigned int i = 0; i < _reduction_output_mean.info()->dimension(1); ++i)
128 {
129 mean += *reinterpret_cast<T *>(_reduction_output_mean.buffer() + _reduction_output_mean.info()->offset_element_in_bytes(Coordinates(0, i)));
130 }
131
132 mean /= _num_pixels;
133 *_mean = mean;
134
135 if(_run_stddev)
136 {
137 auto stddev = static_cast<T>(0);
138 // Calculate final result for stddev
139 for(unsigned int i = 0; i < _reduction_output_stddev.info()->dimension(1); ++i)
140 {
141 stddev += *reinterpret_cast<T *>(_reduction_output_stddev.buffer() + _reduction_output_stddev.info()->offset_element_in_bytes(Coordinates(0, i)));
142 }
143 *_stddev = std::sqrt((stddev / _num_pixels) - (mean * mean));
144
145 _reduction_output_stddev.unmap();
146 }
147 _reduction_output_mean.unmap();
Michalis Spyroud1794eb2018-06-15 16:15:26 +0100148}
149
150void CLMeanStdDev::run_int()
151{
152 CLScheduler::get().enqueue(_fill_border_kernel);
153 CLScheduler::get().enqueue(_mean_stddev_kernel);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100154}
155
156void CLMeanStdDev::run()
157{
Michalis Spyroud1794eb2018-06-15 16:15:26 +0100158 switch(_data_type)
159 {
160 case DataType::F16:
161 run_float<half>();
162 break;
163 case DataType::F32:
164 run_float<float>();
165 break;
166 case DataType::U8:
167 run_int();
168 break;
169 default:
170 ARM_COMPUTE_ERROR_ON("Not supported");
171 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100172}