Compute Library
 22.05
impl.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2022 Arm Limited.
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 
27 
28 namespace arm_compute
29 {
30 namespace cpu
31 {
32 template <typename ScalarType, int size>
33 void mean_stddev_normalization(ITensor *input, ITensor *output, float epsilon, const Window &window)
34 {
36 
37  // Set build options
38  Window win = window;
39  win.set(Window::DimX, Window::Dimension(0, 1, 1));
40 
41  const int window_step_x = size;
42  const auto window_start_x = static_cast<int>(window.x().start());
43  const auto window_end_x = static_cast<int>(window.x().end());
44 
45  Iterator input_itr(input, win);
46  Iterator output_itr(output, win);
47 
48  execute_window_loop(win, [&](const Coordinates &)
49  {
50  int x = window_start_x;
51  auto in_ptr = reinterpret_cast<const ScalarType *>(input_itr.ptr());
52  auto out_ptr = reinterpret_cast<ScalarType *>(output_itr.ptr());
53 
54  auto sum_vec = wrapper::vdup_n(static_cast<ScalarType>(0.f), ExactTagType{});
55  auto sum_sq_vec = wrapper::vdup_n(static_cast<ScalarType>(0.f), ExactTagType{});
56 
57  for(; x <= (window_end_x - window_step_x); x += window_step_x)
58  {
59  auto data = wrapper::vloadq(in_ptr + x);
60  sum_vec = wrapper::vadd(sum_vec, data);
61  sum_sq_vec = wrapper::vadd(sum_sq_vec, wrapper::vmul(data, data));
62  }
63 
64  auto sum_carry_res = wrapper::vpadd(wrapper::vgethigh(sum_vec), wrapper::vgetlow(sum_vec));
65  auto sum_sq_carry_res = wrapper::vpadd(wrapper::vgethigh(sum_sq_vec), wrapper::vgetlow(sum_sq_vec));
66  for(int i = 0; i < size / 4; ++i)
67  {
68  sum_carry_res = wrapper::vpadd(sum_carry_res, sum_carry_res);
69  sum_sq_carry_res = wrapper::vpadd(sum_sq_carry_res, sum_sq_carry_res);
70  }
71 
72  auto sum = wrapper::vgetlane(sum_carry_res, 0);
73  auto sum_sq = wrapper::vgetlane(sum_sq_carry_res, 0);
74 
75  // Compute left-over elements
76  for(; x < window_end_x; ++x)
77  {
78  ScalarType data = *(in_ptr + x);
79  sum += data;
80  sum_sq += data * data;
81  }
82 
83  ScalarType mean = sum / input->info()->dimension(0);
84  ScalarType var = (sum_sq / input->info()->dimension(0)) - (mean * mean);
85  ScalarType stddev_inv = 1.f / sqrt(var + epsilon);
86 
87  auto mean_vec = wrapper::vdup_n(mean, ExactTagType{});
88  auto stddev_inv_vec = wrapper::vdup_n(stddev_inv, ExactTagType{});
89  for(x = window_start_x; x <= (window_end_x - window_step_x); x += window_step_x)
90  {
91  auto data = wrapper::vloadq(in_ptr + x);
92  auto res = wrapper::vmul(wrapper::vsub(data, mean_vec), stddev_inv_vec);
93  // Store results
94  wrapper::vstore(out_ptr + x, res);
95  }
96  for(; x < window_end_x; ++x)
97  {
98  *(out_ptr + x) = (*(in_ptr + x) - mean) * stddev_inv;
99  }
100  },
101  input_itr, output_itr);
102 }
103 template void mean_stddev_normalization<float, 4>(ITensor *input, ITensor *output, float epsilon, const Window &window);
104 
105 #if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) && defined(ENABLE_FP16_KERNELS)
106 template void mean_stddev_normalization<float16_t, 8>(ITensor *input, ITensor *output, float epsilon, const Window &window);
107 #endif //defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) && defined(ENABLE_FP16_KERNELS)
108 
109 } // namespace cpu
110 } // namespace arm_compute
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
uint8x16_t vloadq(const uint8_t *ptr)
Definition: load.h:58
template void mean_stddev_normalization< float, 4 >(ITensor *input, ITensor *output, float epsilon, const Window &window)
uint8x8_t vadd(const uint8x8_t &a, const uint8x8_t &b)
Definition: add.h:39
uint8x8_t vsub(const uint8x8_t &a, const uint8x8_t &b)
Definition: sub.h:39
Describe one of the image&#39;s dimensions with a start, end and step.
Definition: Window.h:79
Interface for CPU tensor.
Definition: ITensor.h:36
Copyright (c) 2017-2022 Arm Limited.
uint8x8_t vpadd(const uint8x8_t &a, const uint8x8_t &b)
Definition: add.h:187
uint8_t vgetlane(const uint8x8_t vector, const unsigned int lane)
Definition: getlane.h:91
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
Create the appropriate SIMD vector given its type and size in terms of elements.
Definition: traits.h:48
Coordinates of an item.
Definition: Coordinates.h:37
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
constexpr uint8_t * ptr() const
Return a pointer to the current pixel.
Definition: Helpers.inl:139
uint8x8_t vgetlow(const uint8x16_t val)
Definition: getlow.h:39
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:49
uint8x8_t vgethigh(const uint8x16_t val)
Definition: gethigh.h:39
uint8x8_t vmul(const uint8x8_t &a, const uint8x8_t &b)
Definition: mul.h:39
void vstore(uint8_t *ptr, uint8x8_t val)
Definition: store.h:39
void mean_stddev_normalization(ITensor *input, ITensor *output, float epsilon, const Window &window)
Definition: impl.cpp:33
uint8x8_t vdup_n(uint8_t value, traits::vector_64_tag)
Definition: dup_n.h:41
void execute_window_loop(const Window &w, L &&lambda_function, Ts &&... iterators)
Iterate through the passed window, automatically adjusting the iterators and calling the lambda_funct...
Definition: Helpers.inl:77
Includes all wrapper headers at once.
constexpr int end() const
Return the end of the dimension.
Definition: Window.h:101
Iterator updated by execute_window_loop for each window element.
Definition: Helpers.h:46
constexpr int start() const
Return the start of the dimension.
Definition: Window.h:96
Describe a multidimensional execution window.
Definition: Window.h:39
constexpr const Dimension & x() const
Alias to access the first dimension of the window.
Definition: Window.h:158