Compute Library
 23.11
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 
26 
27 #include "arm_compute/core/Types.h"
28 
30 
31 namespace arm_compute
32 {
33 namespace cpu
34 {
35 template <typename ScalarType>
37  const ITensor *in, const ITensor *max, void *const tmp, ITensor *out, float beta, bool is_log, const Window &window)
38 {
39  const int start_x = in->info()->valid_region().anchor.x();
40  const int input_width = in->info()->valid_region().shape.x();
41 
42  const float scale_beta = -beta * in->info()->quantization_info().uniform().scale;
43  const auto scale_beta_vec = svdup_n_f32(scale_beta);
44 
45  Iterator in_it(in, window);
46  Iterator max_it(max, window);
47  Iterator out_it(out, window);
48  const auto all_true_pg = wrapper::svptrue<ScalarType>();
49  using SVEType = typename wrapper::traits::sve_vector<ScalarType>::type;
50 
51  const int inc_1 = static_cast<int>(svcntw());
52  const int inc_2 = static_cast<int>(2 * svcntw());
53  const int inc_3 = static_cast<int>(3 * svcntw());
54 
56  window,
57  [&](const Coordinates &)
58  {
59  /* Get pointers */
60  const auto in_ptr = reinterpret_cast<const ScalarType *>(in_it.ptr()) + start_x;
61  const auto out_ptr = reinterpret_cast<ScalarType *>(out_it.ptr()) + start_x;
62  const auto tmp_ptr = reinterpret_cast<float *>(tmp);
63 
64  float sum{};
65 
66  /* Compute exponentials and sum */
67  {
68  /* Get max value */
69  const auto max_val = *reinterpret_cast<const ScalarType *>(max_it.ptr());
70  const auto vec_max = wrapper::svdup_n(max_val);
71 
72  /* Init sum to zero */
73  auto vec_sum_0 = svdup_n_f32(0.f);
74  auto vec_sum_1 = svdup_n_f32(0.f);
75  auto vec_sum_2 = svdup_n_f32(0.f);
76  auto vec_sum_3 = svdup_n_f32(0.f);
77 
78  /* Loop over row and compute exponentials and sum */
79  int x = 0;
80  svbool_t pg = wrapper::svwhilelt<ScalarType>(x, input_width);
81  svbool_t pg_0 = svunpklo(svunpklo(pg));
82  svbool_t pg_1 = svunpkhi(svunpklo(pg));
83  svbool_t pg_2 = svunpklo(svunpkhi(pg));
84  svbool_t pg_3 = svunpkhi(svunpkhi(pg));
85  do
86  {
87  const auto vec_elements = svld1(pg, in_ptr + x);
88  const auto vec_elements_sub = svreinterpret_u8(svsub_z(pg, vec_max, vec_elements));
89 
90  auto vec_elements_flt_0 = svcvt_f32_z(pg_0, svunpklo(svunpklo(vec_elements_sub)));
91  auto vec_elements_flt_1 = svcvt_f32_z(pg_1, svunpkhi(svunpklo(vec_elements_sub)));
92  auto vec_elements_flt_2 = svcvt_f32_z(pg_2, svunpklo(svunpkhi(vec_elements_sub)));
93  auto vec_elements_flt_3 = svcvt_f32_z(pg_3, svunpkhi(svunpkhi(vec_elements_sub)));
94 
95  if (is_log)
96  {
97  vec_elements_flt_0 = svmul_f32_z(pg_0, vec_elements_flt_0, scale_beta_vec);
98  vec_elements_flt_1 = svmul_f32_z(pg_1, vec_elements_flt_1, scale_beta_vec);
99  vec_elements_flt_2 = svmul_f32_z(pg_2, vec_elements_flt_2, scale_beta_vec);
100  vec_elements_flt_3 = svmul_f32_z(pg_3, vec_elements_flt_3, scale_beta_vec);
101  vec_sum_0 = svadd_f32_m(pg_0, vec_sum_0, svexp_f32_z(pg_0, vec_elements_flt_0));
102  vec_sum_1 = svadd_f32_m(pg_1, vec_sum_1, svexp_f32_z(pg_1, vec_elements_flt_1));
103  vec_sum_2 = svadd_f32_m(pg_2, vec_sum_2, svexp_f32_z(pg_2, vec_elements_flt_2));
104  vec_sum_3 = svadd_f32_m(pg_3, vec_sum_3, svexp_f32_z(pg_3, vec_elements_flt_3));
105  }
106  else
107  {
108  vec_elements_flt_0 = svexp_f32_z(pg_0, svmul_f32_z(pg_0, vec_elements_flt_0, scale_beta_vec));
109  vec_elements_flt_1 = svexp_f32_z(pg_1, svmul_f32_z(pg_1, vec_elements_flt_1, scale_beta_vec));
110  vec_elements_flt_2 = svexp_f32_z(pg_2, svmul_f32_z(pg_2, vec_elements_flt_2, scale_beta_vec));
111  vec_elements_flt_3 = svexp_f32_z(pg_3, svmul_f32_z(pg_3, vec_elements_flt_3, scale_beta_vec));
112  vec_sum_0 = svadd_f32_m(pg_0, vec_sum_0, vec_elements_flt_0);
113  vec_sum_1 = svadd_f32_m(pg_1, vec_sum_1, vec_elements_flt_1);
114  vec_sum_2 = svadd_f32_m(pg_2, vec_sum_2, vec_elements_flt_2);
115  vec_sum_3 = svadd_f32_m(pg_3, vec_sum_3, vec_elements_flt_3);
116  }
117 
118  svst1_f32(pg_0, tmp_ptr + x, vec_elements_flt_0);
119  svst1_f32(pg_1, tmp_ptr + x + inc_1, vec_elements_flt_1);
120  svst1_f32(pg_2, tmp_ptr + x + inc_2, vec_elements_flt_2);
121  svst1_f32(pg_3, tmp_ptr + x + inc_3, vec_elements_flt_3);
122 
123  x += wrapper::svcnt<ScalarType>();
124  pg = wrapper::svwhilelt<ScalarType>(x, input_width);
125  pg_0 = svunpklo(svunpklo(pg));
126  pg_1 = svunpkhi(svunpklo(pg));
127  pg_2 = svunpklo(svunpkhi(pg));
128  pg_3 = svunpkhi(svunpkhi(pg));
129  } while (svptest_any(all_true_pg, pg));
130 
131  /* Reduce sum */
132  const auto vec_sum = svadd_f32_z(all_true_pg, svadd_f32_z(all_true_pg, vec_sum_0, vec_sum_1),
133  svadd_f32_z(all_true_pg, vec_sum_2, vec_sum_3));
134  sum = svaddv_f32(all_true_pg, vec_sum);
135 
136  /* Run remaining elements */
137  x = 0;
138  if (is_log)
139  {
140  sum = std::log(sum);
141  }
142  else
143  {
144  sum = 256.f / sum;
145  }
146  }
147 
148  /* Normalize exponentials */
149  {
150  constexpr bool is_qasymm8_signed = std::is_same<ScalarType, qasymm8_signed_t>::value;
151  /* Loop over row and compute softmax */
152  int x = 0;
153  svbool_t pg = wrapper::svwhilelt<ScalarType>(x, input_width);
154  svbool_t pg_0 = svunpklo(svunpklo(pg));
155  svbool_t pg_1 = svunpkhi(svunpklo(pg));
156  svbool_t pg_2 = svunpklo(svunpkhi(pg));
157  svbool_t pg_3 = svunpkhi(svunpkhi(pg));
158  do
159  {
160  auto vec_in_0 = svld1_f32(pg_0, tmp_ptr + x);
161  auto vec_in_1 = svld1_f32(pg_1, tmp_ptr + x + inc_1);
162  auto vec_in_2 = svld1_f32(pg_2, tmp_ptr + x + inc_2);
163  auto vec_in_3 = svld1_f32(pg_3, tmp_ptr + x + inc_3);
164 
165  svfloat32_t res_0{};
166  svfloat32_t res_1{};
167  svfloat32_t res_2{};
168  svfloat32_t res_3{};
169 
170  if (is_log)
171  {
172  res_0 = svsub_f32_z(pg_0, vec_in_0, svdup_n_f32(sum));
173  res_1 = svsub_f32_z(pg_1, vec_in_1, svdup_n_f32(sum));
174  res_2 = svsub_f32_z(pg_2, vec_in_2, svdup_n_f32(sum));
175  res_3 = svsub_f32_z(pg_3, vec_in_3, svdup_n_f32(sum));
176  }
177  else
178  {
179  res_0 = svmul_f32_z(pg_0, vec_in_0, svdup_n_f32(sum));
180  res_1 = svmul_f32_z(pg_1, vec_in_1, svdup_n_f32(sum));
181  res_2 = svmul_f32_z(pg_2, vec_in_2, svdup_n_f32(sum));
182  res_3 = svmul_f32_z(pg_3, vec_in_3, svdup_n_f32(sum));
183 
184  if (is_qasymm8_signed)
185  {
186  const auto offset_vec = svdup_n_f32(128.f);
187  res_0 = svsub_z(pg_0, res_0, offset_vec);
188  res_1 = svsub_z(pg_1, res_1, offset_vec);
189  res_2 = svsub_z(pg_2, res_2, offset_vec);
190  res_3 = svsub_z(pg_3, res_3, offset_vec);
191  }
192  }
193 
194  // Store value
195  const auto out = convert_float_to_int<SVEType>(res_0, res_1, res_2, res_3);
196  svst1(pg, out_ptr + x, out);
197  x += wrapper::svcnt<ScalarType>();
198  pg = wrapper::svwhilelt<ScalarType>(x, input_width);
199  pg_0 = svunpklo(svunpklo(pg));
200  pg_1 = svunpkhi(svunpklo(pg));
201  pg_2 = svunpklo(svunpkhi(pg));
202  pg_3 = svunpkhi(svunpkhi(pg));
203  } while (svptest_any(all_true_pg, pg));
204  }
205  },
206  in_it, max_it, out_it);
207 }
208 
210  const ITensor *max,
211  void *const tmp,
212  ITensor *out,
213  float beta,
214  bool is_log,
215  const Window &window);
217  const ITensor *max,
218  void *const tmp,
219  ITensor *out,
220  float beta,
221  bool is_log,
222  const Window &window);
223 } // namespace cpu
224 } // namespace arm_compute
arm_compute::ITensorInfo::valid_region
virtual ValidRegion valid_region() const =0
Valid region of the tensor.
type
decltype(strategy::transforms) typedef type
Definition: gemm_interleaved.hpp:261
Types.h
arm_compute::ITensor
Interface for CPU tensor.
Definition: ITensor.h:36
wrapper.h
Includes all wrapper headers at once.
arm_compute::ITensor::info
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor's metadata.
arm_compute::Iterator::ptr
constexpr uint8_t * ptr() const
Return a pointer to the current pixel.
Definition: Helpers.inl:147
arm_compute::cpu::sve2_softmax_logits_1d_quantized
void sve2_softmax_logits_1d_quantized(const ITensor *in, const ITensor *max, void *const tmp, ITensor *out, float beta, bool is_log, const Window &window)
Definition: impl.cpp:36
arm_compute::execute_window_loop
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:74
arm_compute::Iterator
Iterator updated by execute_window_loop for each window element.
Definition: Helpers.h:46
impl.h
arm_compute::Dimensions::x
T x() const
Alias to access the size of the first dimension.
Definition: Dimensions.h:86
arm_compute::QuantizationInfo::uniform
UniformQuantizationInfo uniform() const
Return per layer quantization info.
Definition: QuantizationInfo.h:140
arm_compute::cpu::sve2_softmax_logits_1d_quantized< qasymm8_signed_t >
template void sve2_softmax_logits_1d_quantized< qasymm8_signed_t >(const ITensor *in, const ITensor *max, void *const tmp, ITensor *out, float beta, bool is_log, const Window &window)
arm_compute::Coordinates
Coordinates of an item.
Definition: Coordinates.h:37
arm_compute::ITensorInfo::quantization_info
virtual QuantizationInfo quantization_info() const =0
Get the quantization settings (scale and offset) of the tensor.
arm_compute::UniformQuantizationInfo::scale
float scale
Definition: QuantizationInfo.h:62
arm_compute::Window
Describe a multidimensional execution window.
Definition: Window.h:39
arm_compute::ValidRegion::shape
TensorShape shape
Shape of the valid region.
Definition: Types.h:223
arm_compute
Copyright (c) 2017-2023 Arm Limited.
Definition: introduction.dox:24
arm_compute::cpu::sve2_softmax_logits_1d_quantized< qasymm8_t >
template void sve2_softmax_logits_1d_quantized< qasymm8_t >(const ITensor *in, const ITensor *max, void *const tmp, ITensor *out, float beta, bool is_log, const Window &window)
arm_compute::ValidRegion::anchor
Coordinates anchor
Anchor for the start of the valid region.
Definition: Types.h:222