24.02.1
|
Go to the documentation of this file.
47 inline std::pair<int64_t, int64_t> compute_mean_variance(int64_t sum, int64_t sum_sq, uint32_t num_input)
49 const auto temp =
static_cast<int64_t
>(0x100000) / num_input;
50 const auto mean = sum * 1024 /
static_cast<int64_t
>(num_input);
51 const int64_t variance = ((sum_sq * temp) - (mean * mean)) / 0x100000;
53 return std::make_pair(mean, variance);
56 inline int64x2x2_t mul_add(
const int32x4_t &a,
const int32x4_t &
b,
const int32x4_t &
bias)
58 using namespace wrapper;
64 const int64_t a_0 =
vgetlane(a_low, 0);
65 const int64_t a_1 =
vgetlane(a_low, 1);
66 const int64_t a_2 =
vgetlane(a_high, 0);
67 const int64_t a_3 =
vgetlane(a_high, 1);
69 const int64_t b_0 =
vgetlane(b_low, 0);
70 const int64_t b_1 =
vgetlane(b_low, 1);
71 const int64_t b_2 =
vgetlane(b_high, 0);
72 const int64_t b_3 =
vgetlane(b_high, 1);
75 const int64x2_t result_0{a_0 * b_0, a_1 * b_1};
76 const int64x2_t result_1{a_2 * b_2, a_3 * b_3};
93 static const std::map<DataType, ComputeFuncType> fn_map = {
94 {
DataType::QSYMM16, std::mem_fn(&NEQLSTMLayerNormalizationKernel::compute_qsymm16)},
112 _output_multiplier = 0;
116 Window win = configure_window(output);
117 INEKernel::configure(win);
120 Window NEQLSTMLayerNormalizationKernel::configure_window(
ITensor *target)
124 _window_start_x =
static_cast<int32_t
>(
window.
x().
start());
125 _window_end_x =
static_cast<int32_t
>(
window.
x().
end());
126 _window_step_x =
static_cast<int32_t
>(vector_size_byte) / _output->
info()->
element_size();
133 _weight_window = _inout_window;
178 inline QuantizationInfo NEQLSTMLayerNormalizationKernel::compute_output_qinfo()
183 inline std::pair<int64_t, int64_t> NEQLSTMLayerNormalizationKernel::sum_qsymm16(
const int16_t *input_ptr)
187 using AccType = int64_t;
188 using InputDataType = int16_t;
193 int32_t x = _window_start_x;
194 for (; x <= _window_end_x && _window_step_x <= (_window_end_x - x); x += _window_step_x)
196 using namespace wrapper;
197 const int16x8_t val =
vloadq(input_ptr + x);
201 #if defined(__aarch64__)
202 sum +=
static_cast<AccType
>(vaddv(val_low));
203 sum +=
static_cast<AccType
>(vaddv(val_high));
205 sum_sq +=
static_cast<AccType
>(vaddv(
vmul(val_low, val_low)));
206 sum_sq +=
static_cast<AccType
>(vaddv(
vmul(val_high, val_high)));
210 const int64x2_t pair_sum_low =
vpaddl(val_low);
211 const int64x2_t pair_sum_high =
vpaddl(val_high);
212 const int64x2_t pair_sum =
vadd(pair_sum_low, pair_sum_high);
215 const int32x4_t square_low =
vmul(val_low, val_low);
216 const int32x4_t square_high =
vmul(val_high, val_high);
217 const int64x2_t pair_sum_sq_low =
vpaddl(square_low);
218 const int64x2_t pair_sum_sq_high =
vpaddl(square_high);
219 const int64x2_t pair_sum_sq =
vadd(pair_sum_sq_low, pair_sum_sq_high);
221 #endif // __aarch64__
224 for (; x < _window_end_x; ++x)
226 const InputDataType val = input_ptr[x];
227 sum +=
static_cast<AccType
>(val);
228 sum_sq +=
static_cast<AccType
>(val * val);
231 return std::make_pair(sum, sum_sq);
234 inline void NEQLSTMLayerNormalizationKernel::normalize_qasymm16(
const int16_t *input_ptr,
236 const int16_t *weight_ptr,
237 const int32_t *bias_ptr,
240 int32_t inv_std_shift)
242 using OutputDataType = int16_t;
244 using namespace wrapper;
245 const int32x4_t mean_vec =
vdup_n(mean, wrapper::traits::vector_128_tag{});
247 int32_t x = _window_start_x;
248 for (; x <= _window_end_x && _window_step_x <= (_window_end_x - x); x += _window_step_x)
250 const int16x8_t val =
vloadq(input_ptr + x);
257 const int16x8_t weight_val =
vloadq(weight_ptr + x);
258 const int32x4_t weight_low =
vmovl(
vgetlow(weight_val));
261 const int32x4_t bias_low =
vloadq(bias_ptr + x);
262 const int32x4_t bias_high =
vloadq(bias_ptr + 4 + x);
264 int64x2x2_t result_0 = mul_add(rescaled.val[0], weight_low, bias_low);
265 int64x2x2_t result_1 = mul_add(rescaled.val[1], weight_high, bias_high);
267 int32x4x2_t combined;
268 combined.val[0] =
vcombine(
vmovn(vrshrq_n_s64(result_0.val[0], 10)),
vmovn(vrshrq_n_s64(result_0.val[1], 10)));
269 combined.val[1] =
vcombine(
vmovn(vrshrq_n_s64(result_1.val[0], 10)),
vmovn(vrshrq_n_s64(result_1.val[1], 10)));
277 for (; x < _window_end_x; ++x)
279 const auto val =
static_cast<int32_t
>(input_ptr[x]);
280 const int32_t shifted = (val << 10) - mean;
282 const int64_t weighted = rescaled * weight_ptr[x] + bias_ptr[x];
283 const auto reverse_shifted =
static_cast<int32_t
>((weighted + 512) >> 10);
287 utility::clamp<decltype(out_val), OutputDataType>(out_val, std::numeric_limits<OutputDataType>::min());
288 output_ptr[x] =
static_cast<OutputDataType
>(out_val);
292 void NEQLSTMLayerNormalizationKernel::compute_qsymm16()
294 using InputDataType = int16_t;
295 using OutputDataType = int16_t;
296 using BiasDataType = int32_t;
297 using AccType = int64_t;
299 Iterator input_iterator{_input, _inout_window};
300 Iterator output_iterator{_output, _inout_window};
301 Iterator weight_iterator{_weight, _weight_window};
302 Iterator bias_iterator{_bias, _weight_window};
304 const auto weight_ptr =
reinterpret_cast<const InputDataType *
>(weight_iterator.ptr());
305 const auto bias_ptr =
reinterpret_cast<const BiasDataType *
>(bias_iterator.ptr());
311 [&,
this](
const Coordinates &)
313 const auto in_ptr =
reinterpret_cast<const InputDataType *
>(input_iterator.ptr());
314 auto out_ptr =
reinterpret_cast<OutputDataType *
>(output_iterator.ptr());
318 std::tie(sum, sum_sq) = sum_qsymm16(in_ptr);
322 std::tie(mean, variance) = compute_mean_variance(sum, sum_sq, column_size);
324 int32_t stddev_invsqrt_mul{};
325 int32_t stddev_invsqrt_shift{};
327 stddev_invsqrt_shift);
329 normalize_qasymm16(in_ptr, out_ptr, weight_ptr, bias_ptr, mean, stddev_invsqrt_mul, stddev_invsqrt_shift);
331 input_iterator, output_iterator);
Class to describe a number of elements in each dimension.
uint8x8_t vadd(const uint8x8_t &a, const uint8x8_t &b)
constexpr int start() const
Return the start of the dimension.
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
Quantization information.
uint16x8_t vmovl(const uint8x8_t &a)
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
uint8x8_t vsub(const uint8x8_t &a, const uint8x8_t &b)
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
virtual size_t element_size() const =0
Element size in bytes calculated as data_size() * num_channels()
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(...)
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
uint8_t vgetlane(const uint8x8_t vector, const unsigned int lane)
uint32x2_t vmovn(const uint64x2_t &a)
static Status validate(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *weight, const ITensorInfo *bias)
Static function to check if given info will lead to a valid configuration of NEQLSTMLayerNormalizatio...
Interface for CPU tensor.
uint32x2_t vqmovn(const uint64x2_t &a)
uint8x16_t vloadq(const uint8_t *ptr)
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
@ QSYMM16
quantized, symmetric fixed-point 16-bit number
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor's metadata.
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
int32x4x2_t multiply_by_quantized_multiplier_2row(int32x4x2_t input, int32_t qmul, int32_t shift)
Multiply a neon vector using quantized multiplier and shift.
#define ARM_COMPUTE_ERROR_THROW_ON(status)
uint8x8_t vmul(const uint8x8_t &a, const uint8x8_t &b)
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...
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
bool auto_init_if_empty(ITensorInfo &info, const TensorShape &shape, int num_channels, DataType data_type, QuantizationInfo quantization_info=QuantizationInfo())
Auto initialize the tensor info (shape, number of channels and data type) if the current assignment i...
uint8x16_t vcombine(const uint8x8_t &a, const uint8x8_t &b)
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
virtual DataType data_type() const =0
Data type used for each element of the tensor.
T x() const
Alias to access the size of the first dimension.
UniformQuantizationInfo uniform() const
Return per layer quantization info.
int32_t multiply_by_quantized_multiplier(int32_t input, int32_t qmul, int32_t shift)
Compute the value multiplied by given quantized multiplier and shift.
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Describe one of the image's dimensions with a start, end and step.
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
void configure(const ITensor *input, ITensor *output, const ITensor *weight, const ITensor *bias)
Set the input and output tensors.
virtual QuantizationInfo quantization_info() const =0
Get the quantization settings (scale and offset) of the tensor.
const Window & window() const
The maximum window the kernel can be executed on.
Information about executing thread and CPU.
void get_invsqrt_quantized_multiplier_exp(int32_t input, int32_t reverse_shift, int32_t &output_inv_sqrt, int32_t &output_shift)
Compute quantized multiplier and shift for the inverse square root of input.
uint8x8_t vgetlow(const uint8x16_t val)
void vstore(uint8_t *ptr, uint8x8_t val)
Describe a multidimensional execution window.
Copyright (c) 2017-2024 Arm Limited.
@ S32
signed 32-bit number
Status calculate_quantized_multiplier(float multiplier, int32_t *quant_multiplier, int32_t *shift, bool ignore_epsilon=false)
Calculate quantized representation of multiplier.
Store the tensor's metadata.
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
uint16x4_t vpaddl(const uint8x8_t &a)
constexpr int end() const
Return the end of the dimension.
uint8x8_t vgethigh(const uint8x16_t val)
constexpr const Dimension & x() const
Alias to access the first dimension of the window.
virtual size_t total_size() const =0
Returns the total size of the tensor in bytes.
void run(const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
virtual ITensorInfo & set_quantization_info(const QuantizationInfo &quantization_info)=0
Set the quantization settings (scale and offset) of the tensor.
virtual size_t num_dimensions() const =0
The number of dimensions of the tensor (rank)
uint8x8_t vdup_n(uint8_t value, traits::vector_64_tag)