blob: a6bb9f2ef72c96865647cd961e8498892bbb5900 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +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 Spyrouebcebf12020-10-21 00:04:14 +010024#include "src/core/NEON/kernels/NEMeanStdDevKernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010025
26#include "arm_compute/core/Error.h"
27#include "arm_compute/core/Helpers.h"
28#include "arm_compute/core/IAccessWindow.h"
29#include "arm_compute/core/TensorInfo.h"
30#include "arm_compute/core/Types.h"
31#include "arm_compute/core/Validate.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010032#include "src/core/helpers/AutoConfiguration.h"
33#include "src/core/helpers/WindowHelpers.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034
35#include <arm_neon.h>
36#include <cmath>
37#include <tuple>
38#include <utility>
39
40using namespace arm_compute;
41
42namespace arm_compute
43{
44class Coordinates;
45} // namespace arm_compute
46
47namespace
48{
49template <bool calc_sum_squared>
50std::pair<uint64x1_t, uint64x1_t> accumulate(const Window &window, Iterator &iterator)
51{
52 uint64x1_t sum = vdup_n_u64(0);
53 uint64x1_t sum_squared = vdup_n_u64(0);
54
55 // Calculate sum
Michalis Spyroua4f378d2019-04-26 14:54:54 +010056 execute_window_loop(window, [&](const Coordinates &)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010057 {
58 const uint8x16_t in_data = vld1q_u8(iterator.ptr());
59
60 // Sum of the low and high elements of data
61 const uint16x8_t tmp0 = vaddl_u8(vget_low_u8(in_data), vget_high_u8(in_data));
62 const uint32x4_t tmp1 = vaddl_u16(vget_low_u16(tmp0), vget_high_u16(tmp0));
63 const uint32x2_t tmp2 = vadd_u32(vget_low_u32(tmp1), vget_high_u32(tmp1));
64
65 // Update sum
66 sum = vpadal_u32(sum, tmp2);
67
68 if(calc_sum_squared)
69 {
70 const uint16x8_t square_data_low = vmull_u8(vget_low_u8(in_data), vget_low_u8(in_data));
71 const uint16x8_t square_data_high = vmull_u8(vget_high_u8(in_data), vget_high_u8(in_data));
72
73 // Sum of the low and high elements of data
74 const uint32x4_t tmp0_low = vaddl_u16(vget_low_u16(square_data_low), vget_high_u16(square_data_low));
75 const uint32x4_t tmp0_high = vaddl_u16(vget_low_u16(square_data_high), vget_high_u16(square_data_high));
76 const uint32x4_t tmp1 = vaddq_u32(tmp0_low, tmp0_high);
77 const uint32x2_t tmp2 = vadd_u32(vget_low_u32(tmp1), vget_high_u32(tmp1));
78
79 // Update sum
80 sum_squared = vpadal_u32(sum_squared, tmp2);
81 }
82 },
83 iterator);
84
85 return std::make_pair(sum, sum_squared);
86}
87} // namespace
88
89NEMeanStdDevKernel::NEMeanStdDevKernel()
Giorgio Arenaa2611812017-07-21 10:08:48 +010090 : _input(nullptr), _mean(nullptr), _stddev(nullptr), _global_sum(nullptr), _global_sum_squared(nullptr), _mtx(), _border_size(0)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010091{
92}
93
Giorgio Arenaa2611812017-07-21 10:08:48 +010094BorderSize NEMeanStdDevKernel::border_size() const
95{
96 return _border_size;
97}
98
Anthony Barbier6ff3b192017-09-04 18:44:23 +010099void NEMeanStdDevKernel::configure(const IImage *input, float *mean, uint64_t *global_sum, float *stddev, uint64_t *global_sum_squared)
100{
101 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(input);
102 ARM_COMPUTE_ERROR_ON(nullptr == mean);
103 ARM_COMPUTE_ERROR_ON(nullptr == global_sum);
104 ARM_COMPUTE_ERROR_ON(stddev && nullptr == global_sum_squared);
105 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
106
107 _input = input;
108 _mean = mean;
109 _stddev = stddev;
110 _global_sum = global_sum;
111 _global_sum_squared = global_sum_squared;
112
113 constexpr unsigned int num_elems_processed_per_iteration = 16;
114
Giorgio Arenafc2817d2017-06-27 17:26:37 +0100115 _border_size = BorderSize(ceil_to_multiple(input->info()->dimension(0), num_elems_processed_per_iteration) - input->info()->dimension(0));
Giorgio Arenaa2611812017-07-21 10:08:48 +0100116
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100117 // Configure kernel window
118 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
119
120 update_window_and_padding(win, AccessWindowHorizontal(input->info(), 0, num_elems_processed_per_iteration));
121
122 INEKernel::configure(win);
123}
124
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100125void NEMeanStdDevKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100126{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100127 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100128 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
129 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
130 Iterator input(_input, window);
131
132 uint64x1_t local_sum = vdup_n_u64(0);
133 uint64x1_t local_sum_squared = vdup_n_u64(0);
134
135 if(_stddev != nullptr)
136 {
137 std::tie(local_sum, local_sum_squared) = accumulate<true>(window, input);
138 }
139 else
140 {
141 std::tie(local_sum, local_sum_squared) = accumulate<false>(window, input);
142 }
143
144 const float num_pixels = _input->info()->dimension(0) * _input->info()->dimension(1);
145
146 // Merge sum and calculate mean and stddev
Georgios Pinitase874ef92019-09-09 17:40:33 +0100147 arm_compute::unique_lock<arm_compute::Mutex> lock(_mtx);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100148
149 *_global_sum += vget_lane_u64(local_sum, 0);
150
151 const float mean = *_global_sum / num_pixels;
152 *_mean = mean;
153
154 if(_stddev != nullptr)
155 {
156 const uint64_t tmp_sum_squared = vget_lane_u64(local_sum_squared, 0);
157 *_global_sum_squared += tmp_sum_squared;
158 *_stddev = std::sqrt((*_global_sum_squared / num_pixels) - (mean * mean));
159 }
160
161 lock.unlock();
162}