Compute Library
 22.05
impl.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021-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 
28 #include "src/core/NEON/SVEMath.h"
30 #include <arm_sve.h>
31 namespace arm_compute
32 {
33 namespace cpu
34 {
35 template <typename ScalarType>
36 void add_same_sve(const ITensor *src0, const ITensor *src1, ITensor *dst, const ConvertPolicy &policy, const Window &window)
37 {
38  const auto all_true_pg = wrapper::svptrue<ScalarType>();
39  const auto window_start_x = static_cast<int>(window.x().start());
40  const auto window_end_x = static_cast<int>(window.x().end());
41  const bool is_broadcast_across_x = src0->info()->tensor_shape().x() != src1->info()->tensor_shape().x();
42  const bool is_sat = (policy == ConvertPolicy::SATURATE);
43 
44  // Clear X Dimension on execution window as we handle manually
45  Window win = window;
46  win.set(Window::DimX, Window::Dimension(0, 1, 1));
47 
48  // Create input windows
49  Window input1_win = window.broadcast_if_dimension_le_one(src0->info()->tensor_shape());
50  Window input2_win = window.broadcast_if_dimension_le_one(src1->info()->tensor_shape());
51 
52  Iterator input1(src0, window.broadcast_if_dimension_le_one(src0->info()->tensor_shape()));
53  Iterator input2(src1, window.broadcast_if_dimension_le_one(src1->info()->tensor_shape()));
54  Iterator output(dst, window);
55 
56  if(is_broadcast_across_x)
57  {
58  const bool is_broadcast_input_2 = input2_win.x().step() == 0;
59  Window broadcast_win = is_broadcast_input_2 ? input2_win : input1_win;
60  Window non_broadcast_win = !is_broadcast_input_2 ? input2_win : input1_win;
61  const ITensor *broadcast_tensor = is_broadcast_input_2 ? src1 : src0;
62  const ITensor *non_broadcast_tensor = !is_broadcast_input_2 ? src1 : src0;
63 
64  // Clear X Dimension on execution window as we handle manually
65  non_broadcast_win.set(Window::DimX, Window::Dimension(0, 1, 1));
66 
67  Iterator broadcast_input(broadcast_tensor, broadcast_win);
68  Iterator non_broadcast_input(non_broadcast_tensor, non_broadcast_win);
69  Iterator output(dst, win);
70 
71  execute_window_loop(win, [&](const Coordinates &)
72  {
73  const auto non_broadcast_input_ptr = reinterpret_cast<const ScalarType *>(non_broadcast_input.ptr());
74  const auto output_ptr = reinterpret_cast<ScalarType *>(output.ptr());
75 
76  const ScalarType broadcast_value = *reinterpret_cast<const ScalarType *>(broadcast_input.ptr());
77  const auto broadcast_value_vec = wrapper::svdup_n(broadcast_value);
78 
79  int x = window_start_x;
80  svbool_t pg = wrapper::svwhilelt<ScalarType>(x, window_end_x);
81  do
82  {
83  const auto non_broadcast_v = svld1(pg, non_broadcast_input_ptr + x);
84  auto res = is_sat ? wrapper::svqadd(broadcast_value_vec, non_broadcast_v) : svadd_z(pg, broadcast_value_vec, non_broadcast_v);
85  svst1(pg, output_ptr + x, res);
86 
87  x += wrapper::svcnt<ScalarType>();
88  pg = wrapper::svwhilelt<ScalarType>(x, window_end_x);
89  }
90  while(svptest_any(all_true_pg, pg));
91  },
92  broadcast_input, non_broadcast_input, output);
93  }
94  else
95  {
96  // Clear X Dimension on execution window as we handle manually
97  input1_win.set(Window::DimX, Window::Dimension(0, 1, 1));
98  input2_win.set(Window::DimX, Window::Dimension(0, 1, 1));
99 
100  Iterator input1(src0, input1_win);
101  Iterator input2(src1, input2_win);
102  Iterator output(dst, win);
103 
104  execute_window_loop(win, [&](const Coordinates &)
105  {
106  const auto input1_ptr = reinterpret_cast<const ScalarType *>(input1.ptr());
107  const auto input2_ptr = reinterpret_cast<const ScalarType *>(input2.ptr());
108  const auto output_ptr = reinterpret_cast<ScalarType *>(output.ptr());
109 
110  int x = window_start_x;
111  svbool_t pg = wrapper::svwhilelt<ScalarType>(x, window_end_x);
112  do
113  {
114  const auto val1 = svld1(pg, input1_ptr + x);
115  const auto val2 = svld1(pg, input2_ptr + x);
116  const auto res = is_sat ? wrapper::svqadd(val1, val2) : svadd_z(pg, val1, val2);
117  svst1(pg, output_ptr + x, res);
118 
119  x += wrapper::svcnt<ScalarType>();
120  pg = wrapper::svwhilelt<ScalarType>(x, window_end_x);
121  }
122  while(svptest_any(all_true_pg, pg));
123  },
124  input1, input2, output);
125  }
126 }
127 template void add_same_sve<float>(const ITensor *src0, const ITensor *src1, ITensor *dst, const ConvertPolicy &policy, const Window &window);
128 template void add_same_sve<uint8_t>(const ITensor *src0, const ITensor *src1, ITensor *dst, const ConvertPolicy &policy, const Window &window);
129 template void add_same_sve<int16_t>(const ITensor *src0, const ITensor *src1, ITensor *dst, const ConvertPolicy &policy, const Window &window);
130 template void add_same_sve<int32_t>(const ITensor *src0, const ITensor *src1, ITensor *dst, const ConvertPolicy &policy, const Window &window);
131 #if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) && defined(ENABLE_FP16_KERNELS)
132 template void add_same_sve<float16_t>(const ITensor *src0, const ITensor *src1, ITensor *dst, const ConvertPolicy &policy, const Window &window);
133 #endif /* (__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) && defined(ENABLE_FP16_KERNELS) */
134 } // namespace cpu
135 } // namespace arm_compute
template void add_same_sve< int32_t >(const ITensor *src0, const ITensor *src1, ITensor *dst, const ConvertPolicy &policy, const Window &window)
constexpr int step() const
Return the step of the dimension.
Definition: Window.h:106
Describe one of the image&#39;s dimensions with a start, end and step.
Definition: Window.h:79
template void add_same_sve< int16_t >(const ITensor *src0, const ITensor *src1, ITensor *dst, const ConvertPolicy &policy, const Window &window)
Interface for CPU tensor.
Definition: ITensor.h:36
Copyright (c) 2017-2022 Arm Limited.
template void add_same_sve< uint8_t >(const ITensor *src0, const ITensor *src1, ITensor *dst, const ConvertPolicy &policy, const Window &window)
T x() const
Alias to access the size of the first dimension.
Definition: Dimensions.h:87
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
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
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:49
Window broadcast_if_dimension_le_one(const TensorShape &shape) const
Don&#39;t advance in the dimension where shape is less equal to 1.
Definition: Window.inl:120
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
template void add_same_sve< float >(const ITensor *src0, const ITensor *src1, ITensor *dst, const ConvertPolicy &policy, const Window &window)
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
void add_same_sve(const ITensor *src0, const ITensor *src1, ITensor *dst, const ConvertPolicy &policy, const Window &window)
Definition: impl.cpp:36
Describe a multidimensional execution window.
Definition: Window.h:39
ConvertPolicy
Policy to handle integer overflow.
Definition: Types.h:404
constexpr const Dimension & x() const
Alias to access the first dimension of the window.
Definition: Window.h:158