Compute Library
 22.05
impl.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-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  */
25 
30 
31 #include <cstddef>
32 
33 namespace arm_compute
34 {
35 namespace cpu
36 {
37 template <typename T, int S>
38 void l2_normalize_x(const ITensor *in, const ITensor *sum, ITensor *out, float epsilon, const Window &window)
39 {
40  using ExactTagType = typename wrapper::traits::neon_vector<T, S>::tag_type;
41 
42  const int window_step_x = 16 / data_size_from_type(in->info()->data_type());
43  const auto window_start_x = static_cast<int>(window.x().start());
44  const auto window_end_x = static_cast<int>(window.x().end());
45 
46  Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
47  win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
48 
49  Iterator input_it(in, win_collapsed);
50  Iterator sum_it(sum, win_collapsed);
51  Iterator output_it(out, win_collapsed);
52 
53  execute_window_loop(win_collapsed, [&](const Coordinates &)
54  {
55  const auto in_ptr = reinterpret_cast<const T *>(input_it.ptr());
56  const auto out_ptr = reinterpret_cast<T *>(output_it.ptr());
57 
58  const T sum_value = *reinterpret_cast<const T *>(sum_it.ptr());
59  const T norm_value = static_cast<T>(1.f) / std::sqrt(std::max(sum_value, static_cast<T>(epsilon)));
60  const auto vec_norm_value = wrapper::vdup_n(norm_value, ExactTagType{});
61 
62  // Compute elements over vector steps
63  int x = window_start_x;
64  for(; x <= (window_end_x - window_step_x); x += window_step_x)
65  {
66  wrapper::vstore(out_ptr + x, wrapper::vmul(wrapper::vloadq(in_ptr + x), vec_norm_value));
67  }
68 
69  // Compute left-over elements
70  for(; x < window_end_x; ++x)
71  {
72  out_ptr[x] = in_ptr[x] * norm_value;
73  }
74  },
75  input_it, sum_it, output_it);
76 }
77 
78 template <typename T, int S>
79 void l2_normalize_yz(const ITensor *in, const ITensor *sum, ITensor *out, float epsilon, const Window &window, size_t axis)
80 {
81  using ExactTagType = typename wrapper::traits::neon_vector<T, S>::tag_type;
82 
83  const int window_step_x = 16 / data_size_from_type(in->info()->data_type());
84  const auto window_start_x = static_cast<int>(window.x().start());
85  const auto window_end_x = static_cast<int>(window.x().end());
86 
87  Window win = window;
88  win.set(Window::DimX, Window::Dimension(0, 1, 1));
89 
90  Window window_sum(win);
91  window_sum.set(axis, Window::Dimension(0, 0, 0));
92 
93  Iterator input_it(in, win);
94  Iterator sum_it(sum, window_sum);
95  Iterator output_it(out, win);
96 
97  const auto vec_eps = wrapper::vdup_n(static_cast<T>(epsilon), ExactTagType{});
98 
99  execute_window_loop(win, [&](const Coordinates &)
100  {
101  const auto in_ptr = reinterpret_cast<const T *>(input_it.ptr());
102  const auto sum_ptr = reinterpret_cast<const T *>(sum_it.ptr());
103  const auto out_ptr = reinterpret_cast<T *>(output_it.ptr());
104 
105  // Compute elements over vector steps
106  int x = window_start_x;
107  for(; x <= (window_end_x - window_step_x); x += window_step_x)
108  {
109  const auto vec_norm_value = wrapper::vinvsqrt(wrapper::vmax(wrapper::vloadq(sum_ptr + x), vec_eps));
110  wrapper::vstore(out_ptr + x, wrapper::vmul(wrapper::vloadq(in_ptr + x), vec_norm_value));
111  }
112 
113  // Compute left-over elements
114  for(; x < window_end_x; ++x)
115  {
116  const T norm_value = static_cast<T>(1.f) / std::sqrt(std::max(sum_ptr[x], static_cast<T>(epsilon)));
117  out_ptr[x] = in_ptr[x] * norm_value;
118  }
119  },
120  input_it, sum_it, output_it);
121 }
122 
123 template void l2_normalize_yz<float, 4>(const ITensor *in, const ITensor *sum, ITensor *out, float epsilon, const Window &window, size_t axis);
124 template void l2_normalize_x<float, 4>(const ITensor *in, const ITensor *sum, ITensor *out, float epsilon, const Window &window);
125 
126 #if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) && defined(ENABLE_FP16_KERNELS)
127 template void l2_normalize_yz<float16_t, 8>(const ITensor *in, const ITensor *sum, ITensor *out, float epsilon, const Window &window, size_t axis);
128 template void l2_normalize_x<float16_t, 8>(const ITensor *in, const ITensor *sum, ITensor *out, float epsilon, const Window &window);
129 #endif //defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) && defined(ENABLE_FP16_KERNELS)
130 } // namespace cpu
131 } // namespace arm_compute
void l2_normalize_yz(const ITensor *in, const ITensor *sum, ITensor *out, float epsilon, const Window &window, size_t axis)
Definition: impl.cpp:79
void l2_normalize_x(const ITensor *in, const ITensor *sum, ITensor *out, float epsilon, const Window &window)
Definition: impl.cpp:38
template void l2_normalize_yz< float, 4 >(const ITensor *in, const ITensor *sum, ITensor *out, float epsilon, const Window &window, size_t axis)
float32x2_t vinvsqrt(const float32x2_t &a)
Definition: invsqrt.h:47
uint8x16_t vloadq(const uint8_t *ptr)
Definition: load.h:58
virtual DataType data_type() const =0
Data type used for each element of the tensor.
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.
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
Window collapse_if_possible(const Window &full_window, size_t first, size_t last, bool *has_collapsed=nullptr) const
Collapse the dimensions between first and last if possible.
Definition: Window.inl:68
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.
size_t data_size_from_type(DataType data_type)
The size in bytes of the data type.
Definition: Utils.h:106
constexpr uint8_t * ptr() const
Return a pointer to the current pixel.
Definition: Helpers.inl:139
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:49
template void l2_normalize_x< float, 4 >(const ITensor *in, const ITensor *sum, ITensor *out, float epsilon, const Window &window)
uint8x8_t vmul(const uint8x8_t &a, const uint8x8_t &b)
Definition: mul.h:39
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
void vstore(uint8_t *ptr, uint8x8_t val)
Definition: store.h:39
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
uint8x8_t vmax(const uint8x8_t &a, const uint8x8_t &b)
Definition: max.h:39
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