blob: 6764a81617ea90f55b55516aa5193edea426e65b [file] [log] [blame]
Sang-Hoon Park1a531fa2020-03-26 13:57:57 +00001/*
2 * Copyright (c) 2020 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
25#include "QLSTMLayerNormalization.h"
26#include "ArithmeticOperations.h"
27#include "MeanStdDevNormalizationLayer.h"
28#include "PixelWiseMultiplication.h"
29#include "src/core/utils/quantization/AsymmHelpers.cpp"
30
31namespace arm_compute
32{
33namespace test
34{
35namespace validation
36{
37namespace reference
38{
39SimpleTensor<float> qlstm_layer_normalization_float_compute(SimpleTensor<float> src, SimpleTensor<float> weight, SimpleTensor<float> bias)
40{
41 SimpleTensor<float> output = mean_std_normalization_layer(src);
42 output = pixel_wise_multiplication(output, weight, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO);
43 return arithmetic_operation(ArithmeticOperation::ADD, output, bias, DataType::F32, ConvertPolicy::SATURATE);
44}
45
46SimpleTensor<int16_t> qlstm_layer_normalization(const SimpleTensor<int16_t> &src, const SimpleTensor<int16_t> &weight, const SimpleTensor<int32_t> &bias)
47{
48 ARM_COMPUTE_ERROR_ON(src.shape().num_dimensions() > 2);
49
50 SimpleTensor<float> converted_src{ src.shape(), DataType::F32 };
51 SimpleTensor<float> converted_weight{ weight.shape(), DataType::F32 };
52 SimpleTensor<float> converted_bias{ bias.shape(), DataType::F32 };
53
54 const auto iq_info = src.quantization_info().uniform();
55 int output_multiplier{};
56 int output_shift{};
57 quantization::calculate_quantized_multiplier(iq_info.scale, &output_multiplier, &output_shift);
58
59 const float layer_norm_scale = output_multiplier * std::pow(2, static_cast<double>(output_shift - 31));
60 const float bias_scale = std::pow(2., -10) * layer_norm_scale;
61
62 for(int i = 0; i < src.num_elements(); i++)
63 {
64 converted_src[i] = static_cast<float>(src[i]);
65 }
66
67 for(int i = 0; i < bias.num_elements(); i++)
68 {
69 converted_bias[i] = static_cast<float>(bias[i]) * bias_scale;
70 }
71
72 for(int i = 0; i < weight.num_elements(); i++)
73 {
74 converted_weight[i] = weight[i] * layer_norm_scale;
75 }
76
77 SimpleTensor<float> output_float = qlstm_layer_normalization_float_compute(converted_src, converted_weight, converted_bias);
78 SimpleTensor<int16_t> output{ output_float.shape(), DataType::QSYMM16 };
79
80 for(int i = 0; i < output.num_elements(); i++)
81 {
82 const auto output_val_s32 = static_cast<int32_t>(std::round(output_float[i] * std::pow(2, 12)));
83 output[i] = utility::clamp<int32_t, int16_t>(output_val_s32, std::numeric_limits<int16_t>::min());
84 }
85
86 return output;
87}
88} // namespace reference
89} // namespace validation
90} // namespace test
91} // namespace arm_compute