blob: 0e24de65846dcee4082a1d57b527c542c2cb8f0d [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
Georgios Pinitasafc630f2020-03-30 14:09:27 +010031#include "support/ToolchainSupport.h"
32
Sang-Hoon Park1a531fa2020-03-26 13:57:57 +000033namespace arm_compute
34{
35namespace test
36{
37namespace validation
38{
39namespace reference
40{
41SimpleTensor<float> qlstm_layer_normalization_float_compute(SimpleTensor<float> src, SimpleTensor<float> weight, SimpleTensor<float> bias)
42{
43 SimpleTensor<float> output = mean_std_normalization_layer(src);
Michele Di Giorgio9428a182020-03-30 14:10:20 +010044 output = pixel_wise_multiplication<float, float, float>(output, weight, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO, DataType::F32);
Sang-Hoon Park1a531fa2020-03-26 13:57:57 +000045 return arithmetic_operation(ArithmeticOperation::ADD, output, bias, DataType::F32, ConvertPolicy::SATURATE);
46}
47
48SimpleTensor<int16_t> qlstm_layer_normalization(const SimpleTensor<int16_t> &src, const SimpleTensor<int16_t> &weight, const SimpleTensor<int32_t> &bias)
49{
50 ARM_COMPUTE_ERROR_ON(src.shape().num_dimensions() > 2);
51
52 SimpleTensor<float> converted_src{ src.shape(), DataType::F32 };
53 SimpleTensor<float> converted_weight{ weight.shape(), DataType::F32 };
54 SimpleTensor<float> converted_bias{ bias.shape(), DataType::F32 };
55
56 const auto iq_info = src.quantization_info().uniform();
57 int output_multiplier{};
58 int output_shift{};
59 quantization::calculate_quantized_multiplier(iq_info.scale, &output_multiplier, &output_shift);
60
61 const float layer_norm_scale = output_multiplier * std::pow(2, static_cast<double>(output_shift - 31));
62 const float bias_scale = std::pow(2., -10) * layer_norm_scale;
63
64 for(int i = 0; i < src.num_elements(); i++)
65 {
66 converted_src[i] = static_cast<float>(src[i]);
67 }
68
69 for(int i = 0; i < bias.num_elements(); i++)
70 {
71 converted_bias[i] = static_cast<float>(bias[i]) * bias_scale;
72 }
73
74 for(int i = 0; i < weight.num_elements(); i++)
75 {
76 converted_weight[i] = weight[i] * layer_norm_scale;
77 }
78
79 SimpleTensor<float> output_float = qlstm_layer_normalization_float_compute(converted_src, converted_weight, converted_bias);
80 SimpleTensor<int16_t> output{ output_float.shape(), DataType::QSYMM16 };
81
82 for(int i = 0; i < output.num_elements(); i++)
83 {
Georgios Pinitasafc630f2020-03-30 14:09:27 +010084 const auto output_val_s32 = static_cast<int32_t>(support::cpp11::round(output_float[i] * std::pow(2, 12)));
Sang-Hoon Park1a531fa2020-03-26 13:57:57 +000085 output[i] = utility::clamp<int32_t, int16_t>(output_val_s32, std::numeric_limits<int16_t>::min());
86 }
87
88 return output;
89}
90} // namespace reference
91} // namespace validation
92} // namespace test
93} // namespace arm_compute