Compute Library
 23.11
impl.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021-2023 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 
26 #include "support/SaturateCast.h"
27 
28 namespace arm_compute
29 {
30 namespace cpu
31 {
32 template void neon_logits_1d_max<qasymm8_signed_t>(const ITensor *in, ITensor *out, const Window &window);
33 template void neon_logits_1d_max<qasymm8_t>(const ITensor *in, ITensor *out, const Window &window);
34 
35 template <typename T>
37  const ITensor *in, const ITensor *max, void *const tmp, ITensor *out, float beta, bool is_log, const Window &window)
38 {
39  static_assert(std::is_same<T, qasymm8_t>::value || std::is_same<T, qasymm8_signed_t>::value,
40  "quantized type should be either qasymm8_t or qasymm8_signed_t.");
41 
42  const int start_x = in->info()->valid_region().anchor.x();
43  const int input_width = in->info()->valid_region().shape.x();
44 
45  const float scale_beta = -beta * in->info()->quantization_info().uniform().scale;
46  const auto scale_beta_vec = vdupq_n_f32(scale_beta);
47 
48  Iterator in_it(in, window);
49  Iterator max_it(max, window);
50  Iterator out_it(out, window);
51  constexpr int vec_size = 16;
52 
54  window,
55  [&](const Coordinates &)
56  {
57  /* Get pointers */
58  const auto in_ptr = reinterpret_cast<const T *>(in_it.ptr()) + start_x;
59  const auto out_ptr = reinterpret_cast<T *>(out_it.ptr()) + start_x;
60  const auto tmp_ptr = reinterpret_cast<float *>(tmp);
61 
62  float sum{};
63  float sum_inversed{};
64 
65  /* Compute exponentials and sum */
66  {
67  /* Get max value */
68  const auto max_val = *reinterpret_cast<const T *>(max_it.ptr());
69  const auto vec_max = wrapper::vdup_n(max_val, wrapper::traits::vector_128_tag{});
70 
71  /* Init sum to zero */
72  float32x4x4_t vec_sum = {
73  vdupq_n_f32(0.f),
74  vdupq_n_f32(0.f),
75  vdupq_n_f32(0.f),
76  vdupq_n_f32(0.f),
77  };
78 
79  /* Loop over row and compute exponentials and sum */
80  int x = 0;
81  for (; x <= (input_width - vec_size); x += vec_size)
82  {
83  auto vec_elements = wrapper::vloadq(in_ptr + x);
84  vec_elements = wrapper::vqsub(vec_max, vec_elements);
85  auto vec_elements_flt = convert_int_to_float<float32x4x4_t>(vec_elements);
86 
87  if (is_log)
88  {
89  vec_elements_flt.val[0] = vmulq_f32(vec_elements_flt.val[0], scale_beta_vec);
90  vec_elements_flt.val[1] = vmulq_f32(vec_elements_flt.val[1], scale_beta_vec);
91  vec_elements_flt.val[2] = vmulq_f32(vec_elements_flt.val[2], scale_beta_vec);
92  vec_elements_flt.val[3] = vmulq_f32(vec_elements_flt.val[3], scale_beta_vec);
93  vec_sum.val[0] = vaddq_f32(vec_sum.val[0], vexpq_f32(vec_elements_flt.val[0]));
94  vec_sum.val[1] = vaddq_f32(vec_sum.val[1], vexpq_f32(vec_elements_flt.val[1]));
95  vec_sum.val[2] = vaddq_f32(vec_sum.val[2], vexpq_f32(vec_elements_flt.val[2]));
96  vec_sum.val[3] = vaddq_f32(vec_sum.val[3], vexpq_f32(vec_elements_flt.val[3]));
97  }
98  else
99  {
100  vec_elements_flt.val[0] = vexpq_f32(vmulq_f32(vec_elements_flt.val[0], scale_beta_vec));
101  vec_elements_flt.val[1] = vexpq_f32(vmulq_f32(vec_elements_flt.val[1], scale_beta_vec));
102  vec_elements_flt.val[2] = vexpq_f32(vmulq_f32(vec_elements_flt.val[2], scale_beta_vec));
103  vec_elements_flt.val[3] = vexpq_f32(vmulq_f32(vec_elements_flt.val[3], scale_beta_vec));
104  vec_sum.val[0] = vaddq_f32(vec_sum.val[0], vec_elements_flt.val[0]);
105  vec_sum.val[1] = vaddq_f32(vec_sum.val[1], vec_elements_flt.val[1]);
106  vec_sum.val[2] = vaddq_f32(vec_sum.val[2], vec_elements_flt.val[2]);
107  vec_sum.val[3] = vaddq_f32(vec_sum.val[3], vec_elements_flt.val[3]);
108  }
109 
110  vst4q_f32(tmp_ptr + x, vec_elements_flt);
111  }
112 
113  /* Reduce sum */
114  const auto sum_16_byte =
115  vaddq_f32(vaddq_f32(vec_sum.val[0], vec_sum.val[1]), vaddq_f32(vec_sum.val[2], vec_sum.val[3]));
116  auto sum_res = vpadd_f32(vget_high_f32(sum_16_byte), vget_low_f32(sum_16_byte));
117  sum_res = vpadd_f32(sum_res, sum_res);
118  sum = wrapper::vgetlane(sum_res, 0);
119 
120  /* Run remaining elements */
121  for (; x < input_width; ++x)
122  {
123  float element{};
124  if (is_log)
125  {
126  element = (max_val - in_ptr[x]) * scale_beta;
127  sum += std::exp(element);
128  }
129  else
130  {
131  element = std::exp((max_val - in_ptr[x]) * scale_beta);
132  sum += element;
133  }
134 
135  tmp_ptr[x] = element;
136  }
137 
138  if (!is_log)
139  {
140  sum_inversed = 256.f / sum;
141  }
142  else
143  {
144  sum = std::log(sum);
145  }
146  }
147 
148  /* Normalize exponentials */
149  {
150  constexpr bool is_qasymm8_signed = std::is_same<T, qasymm8_signed_t>::value;
151  /* Loop over row and compute softmax */
152  int x = 0;
153  for (; x <= (input_width - vec_size); x += vec_size)
154  {
155  using int_vec_type = wrapper::traits::neon_vector_t<T, 16>;
156  float32x4x4_t vec_in = vld4q_f32(tmp_ptr + x);
157  int_vec_type normalized_value{};
158  if (is_log)
159  {
160  const float32x4x4_t sub = {
161  vsubq_f32(vec_in.val[0], vdupq_n_f32(sum)),
162  vsubq_f32(vec_in.val[1], vdupq_n_f32(sum)),
163  vsubq_f32(vec_in.val[2], vdupq_n_f32(sum)),
164  vsubq_f32(vec_in.val[3], vdupq_n_f32(sum)),
165  };
166  normalized_value = convert_float_to_int<float32x4x4_t, int_vec_type>(sub);
167  }
168  else
169  {
170  float32x4x4_t mul = {
171  vmulq_f32(vec_in.val[0], vdupq_n_f32(sum_inversed)),
172  vmulq_f32(vec_in.val[1], vdupq_n_f32(sum_inversed)),
173  vmulq_f32(vec_in.val[2], vdupq_n_f32(sum_inversed)),
174  vmulq_f32(vec_in.val[3], vdupq_n_f32(sum_inversed)),
175  };
176 
177  if (is_qasymm8_signed)
178  {
179  const auto offset_vec = wrapper::vdup_n(128.f, wrapper::traits::vector_128_tag{});
180  mul.val[0] = wrapper::vsub(mul.val[0], offset_vec);
181  mul.val[1] = wrapper::vsub(mul.val[1], offset_vec);
182  mul.val[2] = wrapper::vsub(mul.val[2], offset_vec);
183  mul.val[3] = wrapper::vsub(mul.val[3], offset_vec);
184  }
185 
186  normalized_value = convert_float_to_int<float32x4x4_t, int_vec_type>(mul);
187  }
188  wrapper::vstore(out_ptr + x, normalized_value);
189  }
190  /* Run remaining elements */
191  for (; x < input_width; ++x)
192  {
193  if (is_log)
194  {
195  out_ptr[x] = utils::cast::saturate_cast<T>(tmp_ptr[x] - sum);
196  }
197  else
198  {
199  out_ptr[x] = utils::cast::saturate_cast<T>((tmp_ptr[x] * sum_inversed) -
200  (is_qasymm8_signed ? 128.f : 0));
201  }
202  }
203  }
204  },
205  in_it, max_it, out_it);
206 }
207 
209  const ITensor *max,
210  void *const tmp,
211  ITensor *out,
212  float beta,
213  bool is_log,
214  const Window &window);
216  const ITensor *max,
217  void *const tmp,
218  ITensor *out,
219  float beta,
220  bool is_log,
221  const Window &window);
222 } // namespace cpu
223 } // namespace arm_compute
arm_compute::ITensorInfo::valid_region
virtual ValidRegion valid_region() const =0
Valid region of the tensor.
arm_compute::wrapper::vqsub
uint8x8_t vqsub(const uint8x8_t &a, const uint8x8_t &b)
Definition: sub.h:74
arm_compute::wrapper::vsub
uint8x8_t vsub(const uint8x8_t &a, const uint8x8_t &b)
Definition: sub.h:39
arm_compute::wrapper::vgetlane
uint8_t vgetlane(const uint8x8_t vector, const unsigned int lane)
Definition: getlane.h:91
arm_compute::cpu::neon_logits_1d_max< qasymm8_t >
template void neon_logits_1d_max< qasymm8_t >(const ITensor *in, ITensor *out, const Window &window)
arm_compute::ITensor
Interface for CPU tensor.
Definition: ITensor.h:36
arm_compute::wrapper::vloadq
uint8x16_t vloadq(const uint8_t *ptr)
Definition: load.h:58
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::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
SaturateCast.h
arm_compute::Iterator
Iterator updated by execute_window_loop for each window element.
Definition: Helpers.h:46
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::Coordinates
Coordinates of an item.
Definition: Coordinates.h:37
arm_compute::wrapper::traits::neon_vector_t
typename neon_vector< T, S >::type neon_vector_t
Helper type template to get the type of a neon vector.
Definition: traits.h:89
arm_compute::ITensorInfo::quantization_info
virtual QuantizationInfo quantization_info() const =0
Get the quantization settings (scale and offset) of the tensor.
arm_compute::cpu::neon_logits_1d_max< qasymm8_signed_t >
template void neon_logits_1d_max< qasymm8_signed_t >(const ITensor *in, ITensor *out, const Window &window)
arm_compute::UniformQuantizationInfo::scale
float scale
Definition: QuantizationInfo.h:62
arm_compute::wrapper::traits::vector_128_tag
128-bit vector tag
Definition: traits.h:54
arm_compute::wrapper::vstore
void vstore(uint8_t *ptr, uint8x8_t val)
Definition: store.h:39
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::cpu::neon_softmax_logits_1d_quantized< qasymm8_signed_t >
template void neon_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
Copyright (c) 2017-2023 Arm Limited.
Definition: introduction.dox:24
arm_compute::cpu::neon_softmax_logits_1d_quantized
void neon_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
impl.h
arm_compute::cpu::neon_softmax_logits_1d_quantized< qasymm8_t >
template void neon_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::vexpq_f32
float32x4_t vexpq_f32(float32x4_t x)
Calculate exponential.
arm_compute::ValidRegion::anchor
Coordinates anchor
Anchor for the start of the valid region.
Definition: Types.h:222
arm_compute::wrapper::vdup_n
uint8x8_t vdup_n(uint8_t value, traits::vector_64_tag)
Definition: dup_n.h:41