blob: 157f306d0cda8405c8eda9b627ddfc53abdba9b4 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michalis Spyroud1794eb2018-06-15 16:15:26 +01002 * Copyright (c) 2016-2018 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{
Michalis Spyroud1794eb2018-06-15 16:15:26 +010069 // In the case of F16/F32 we call reduction operation for calculating CLMeanStdDev
70 _data_type = input->info()->data_type();
Anthony Barbier6ff3b192017-09-04 18:44:23 +010071
Michalis Spyroud1794eb2018-06-15 16:15:26 +010072 if(is_data_type_float(_data_type))
Anthony Barbier6ff3b192017-09-04 18:44:23 +010073 {
Michalis Spyroud1794eb2018-06-15 16:15:26 +010074 _num_pixels = input->info()->dimension(0) * input->info()->dimension(1);
75
76 _memory_group.manage(&_reduction_output_mean);
77 _reduction_operation_mean.configure(input, &_reduction_output_mean, 0, ReductionOperation::SUM);
78 _reduction_output_mean.allocator()->allocate();
79 _mean = mean;
80
81 if(stddev != nullptr)
82 {
83 _memory_group.manage(&_reduction_output_stddev);
84 _reduction_operation_stddev.configure(input, &_reduction_output_stddev, 0, ReductionOperation::SUM_SQUARE);
85 _reduction_output_stddev.allocator()->allocate();
86 _stddev = stddev;
87 _run_stddev = true;
88 }
89 }
90 else
91 {
92 _global_sum = cl::Buffer(CLScheduler::get().context(), CL_MEM_ALLOC_HOST_PTR | CL_MEM_READ_WRITE, sizeof(cl_ulong));
93
94 if(stddev != nullptr)
95 {
96 _global_sum_squared = cl::Buffer(CLScheduler::get().context(), CL_MEM_ALLOC_HOST_PTR | CL_MEM_READ_WRITE, sizeof(cl_ulong));
97 }
98
99 _mean_stddev_kernel.configure(input, mean, &_global_sum, stddev, &_global_sum_squared);
100 _fill_border_kernel.configure(input, _mean_stddev_kernel.border_size(), BorderMode::CONSTANT, PixelValue(static_cast<uint8_t>(0)));
101 }
102}
103
104template <typename T>
105void CLMeanStdDev::run_float()
106{
107 _memory_group.acquire();
108
109 // Perform reduction on x-axis
110 _reduction_operation_mean.run();
111 if(_run_stddev)
112 {
113 _reduction_operation_stddev.run();
114 _reduction_output_stddev.map(true);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100115 }
116
Michalis Spyroud1794eb2018-06-15 16:15:26 +0100117 _reduction_output_mean.map(true);
118
119 auto mean = static_cast<T>(0);
120
121 // Calculate final result for mean
122 for(unsigned int i = 0; i < _reduction_output_mean.info()->dimension(1); ++i)
123 {
124 mean += *reinterpret_cast<T *>(_reduction_output_mean.buffer() + _reduction_output_mean.info()->offset_element_in_bytes(Coordinates(0, i)));
125 }
126
127 mean /= _num_pixels;
128 *_mean = mean;
129
130 if(_run_stddev)
131 {
132 auto stddev = static_cast<T>(0);
133 // Calculate final result for stddev
134 for(unsigned int i = 0; i < _reduction_output_stddev.info()->dimension(1); ++i)
135 {
136 stddev += *reinterpret_cast<T *>(_reduction_output_stddev.buffer() + _reduction_output_stddev.info()->offset_element_in_bytes(Coordinates(0, i)));
137 }
138 *_stddev = std::sqrt((stddev / _num_pixels) - (mean * mean));
139
140 _reduction_output_stddev.unmap();
141 }
142 _reduction_output_mean.unmap();
143
144 _memory_group.release();
145}
146
147void CLMeanStdDev::run_int()
148{
149 CLScheduler::get().enqueue(_fill_border_kernel);
150 CLScheduler::get().enqueue(_mean_stddev_kernel);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100151}
152
153void CLMeanStdDev::run()
154{
Michalis Spyroud1794eb2018-06-15 16:15:26 +0100155 switch(_data_type)
156 {
157 case DataType::F16:
158 run_float<half>();
159 break;
160 case DataType::F32:
161 run_float<float>();
162 break;
163 case DataType::U8:
164 run_int();
165 break;
166 default:
167 ARM_COMPUTE_ERROR_ON("Not supported");
168 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100169}