Compute Library
 22.11
QLSTMLayerNormalization.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020-2021 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 #include "arm_compute/core/Types.h"
28 #include "tests/NEON/Accessor.h"
29 #include "tests/NEON/Helper.h"
31 #include "tests/datasets/ShapeDatasets.h"
33 #include "tests/framework/Macros.h"
37 #include "tests/validation/fixtures/QLSTMLayerNormalizationFixture.h"
38 
39 namespace arm_compute
40 {
41 namespace test
42 {
43 namespace validation
44 {
45 namespace
46 {
47 constexpr uint32_t vector_size_byte = 16;
48 
49 using test::datasets::ShapeDataset;
50 using NEQLSTMLayerNormalization = NESynthetizeFunction<NEQLSTMLayerNormalizationKernel>;
51 
52 template <uint32_t num_elements_per_iter, uint32_t num_batches, uint32_t num_iteration>
53 class QLSTMLayerNormShapeDataSet : public ShapeDataset
54 {
55  static constexpr auto boundary_minus_one = num_elements_per_iter * num_iteration - 1;
56  static constexpr auto boundary = num_elements_per_iter * num_iteration;
57  static constexpr auto boundary_plus_one = num_elements_per_iter * num_iteration + 1;
58 
59 public:
60  QLSTMLayerNormShapeDataSet(std::string name)
61  : ShapeDataset(name,
62  {
63  TensorShape{ boundary_minus_one, num_batches },
64  TensorShape{ boundary, num_batches },
65  TensorShape{ boundary_plus_one, num_batches }
66  })
67  {
68  }
69 };
70 
71 template <uint32_t num_elements_per_iter, uint32_t num_batches>
72 class QLSTMLayerNormShapeDataSet<num_elements_per_iter, num_batches, 0> : public ShapeDataset
73 {
74 public:
75  QLSTMLayerNormShapeDataSet(std::string name)
76  : ShapeDataset(name,
77  {
78  TensorShape{ 1, num_batches },
79  TensorShape{ 2, num_batches }
80  })
81  {
82  }
83 };
84 } // namespace
85 TEST_SUITE(NEON)
86 TEST_SUITE(QLSTMLayerNormalization)
87 
88 static const TensorShape correct_input_shape{ TensorShape(15U, 2U) };
89 static const TensorShape correct_weight_shape{ TensorShape(15U) };
90 static const TensorShape correct_bias_shape{ TensorShape(15U) };
91 static const TensorShape correct_output_shape{ correct_input_shape };
92 static const DataType correct_input_dt{ DataType::QSYMM16 };
93 static const DataType correct_weight_dt{ DataType::QSYMM16 };
94 static const DataType correct_bias_dt{ DataType::S32 };
95 static const DataType correct_output_dt{ correct_input_dt };
96 static const uint32_t tensor_num_channel{ 1 };
97 
98 // *INDENT-OFF*
99 // clang-format off
100 
102  zip(zip(zip(
103  framework::dataset::make("InputInfo", {
104  TensorInfo(correct_input_shape, tensor_num_channel, DataType::F16), // input supports only QSYMM16
105  TensorInfo(correct_input_shape, tensor_num_channel, correct_input_dt), // weight supports only QSYMM16
106  TensorInfo(correct_input_shape, tensor_num_channel, correct_input_dt), // bias supports only S32
107  TensorInfo(TensorShape(15U, 2U, 2U), tensor_num_channel, correct_input_dt), // input supports only up to 2D
108  TensorInfo(correct_input_shape, tensor_num_channel, correct_input_dt), // weight supports only up to 1D
109  TensorInfo(correct_input_shape, tensor_num_channel, correct_input_dt), // bias supports only up to 1D
110  TensorInfo(correct_input_shape, tensor_num_channel, correct_input_dt), // input_shape[0] != weight_shape[0] should fail
111  TensorInfo(correct_input_shape, tensor_num_channel, correct_input_dt), // weight_shape[0] != bias_shape[0] should fail
112  TensorInfo(correct_input_shape, tensor_num_channel, correct_input_dt), // output shape mismatches with input shape
113  TensorInfo(correct_input_shape, tensor_num_channel, correct_input_dt), // output data type mismatches with input data type
114  }),
115  framework::dataset::make("WeightInfo", {
116  TensorInfo(correct_weight_shape, tensor_num_channel, correct_weight_dt),
117  TensorInfo(correct_weight_shape, tensor_num_channel, DataType::F16),
118  TensorInfo(correct_weight_shape, tensor_num_channel, correct_weight_dt),
119  TensorInfo(correct_weight_shape, tensor_num_channel, correct_weight_dt),
120  TensorInfo(TensorShape(15U, 2U), tensor_num_channel, correct_weight_dt),
121  TensorInfo(correct_weight_shape, tensor_num_channel, correct_weight_dt),
122  TensorInfo(TensorShape(14U), tensor_num_channel, correct_weight_dt),
123  TensorInfo(correct_weight_shape, tensor_num_channel, correct_weight_dt),
124  TensorInfo(correct_weight_shape, tensor_num_channel, correct_weight_dt),
125  TensorInfo(correct_weight_shape, tensor_num_channel, correct_weight_dt),
126  })
127  ),
128  framework::dataset::make("BiasInfo", {
129  TensorInfo(correct_bias_shape, tensor_num_channel, correct_bias_dt),
130  TensorInfo(correct_bias_shape, tensor_num_channel, correct_bias_dt),
131  TensorInfo(correct_bias_shape, tensor_num_channel, DataType::QSYMM16),
132  TensorInfo(correct_bias_shape, tensor_num_channel, correct_bias_dt),
133  TensorInfo(correct_bias_shape, tensor_num_channel, correct_bias_dt),
134  TensorInfo(TensorShape(15U, 2U), tensor_num_channel, correct_bias_dt),
135  TensorInfo(correct_bias_shape, tensor_num_channel, correct_bias_dt),
136  TensorInfo(TensorShape(14U), tensor_num_channel, correct_bias_dt),
137  TensorInfo(correct_bias_shape, tensor_num_channel, correct_bias_dt),
138  TensorInfo(correct_bias_shape, tensor_num_channel, correct_bias_dt),
139  })
140  ),
141  framework::dataset::make("OutputInfo", {
142  TensorInfo(correct_output_shape, tensor_num_channel, correct_output_dt),
143  TensorInfo(correct_output_shape, tensor_num_channel, correct_output_dt),
144  TensorInfo(correct_output_shape, tensor_num_channel, correct_output_dt),
145  TensorInfo(correct_output_shape, tensor_num_channel, correct_output_dt),
146  TensorInfo(correct_output_shape, tensor_num_channel, correct_output_dt),
147  TensorInfo(correct_output_shape, tensor_num_channel, correct_output_dt),
148  TensorInfo(correct_output_shape, tensor_num_channel, correct_output_dt),
149  TensorInfo(correct_output_shape, tensor_num_channel, correct_output_dt),
150  TensorInfo(TensorShape(15, 3), tensor_num_channel, correct_output_dt),
151  TensorInfo(correct_output_shape, tensor_num_channel, DataType::S32),
152  })
153  ),
154  input_info, weight_info, bias_info, output_info)
155 {
156  const Status s = NEQLSTMLayerNormalization::validate(&input_info, &output_info, &weight_info, &bias_info);
158 }
159 
160 // clang-format on
161 // *INDENT-ON*
162 
163 template <typename T>
164 using NEQLSTMLayerNormalizationFixture = QLSTMLayerNormalizationValidationFixture<Tensor, Accessor, NEQLSTMLayerNormalization, T>;
165 
166 TEST_SUITE(Quantized)
167 TEST_SUITE(QSYMM16)
168 
169 /** Tests will be targetting
170  * - Comparison between optimized kernel and the exact same but scalar version of reference kernel
171  * - Input shapes of 1D and 2D with the first dimension covers boundary values of 128-bit vector size (0~3 iterations)
172  * - Weight and bias 1D shape that have same size as that of input shapes
173  * - Quantization scale is greater and smaller than one.
174  * - Input values will be noted in fixture.
175  *
176  * What we can't test
177  * - Since reference kernel uses the exact the same algorithm in the same quantized domain
178  * it is hard to fully test whether the algorithm accomplishes what it is supposed to.
179  * - The algorithm has been sensitive to quantization scale but it is hard to fully test
180  * the sensitivity due to aforementioned reason.
181  * - Again, it is hard to fully test corner values due to the exact same algorithm of the
182  * reference kernel and the optimized kernel.
183  */
184 
185 constexpr uint32_t qsymm16_per_vector = vector_size_byte / sizeof(int16_t);
186 
187 #define QSYMM16_DATASET_ITER(num_input_batch, num_iter) \
188  combine(combine(zip(zip(QLSTMLayerNormShapeDataSet<qsymm16_per_vector, num_input_batch, num_iter>("InputShape"), \
189  QLSTMLayerNormShapeDataSet<qsymm16_per_vector, 1, num_iter>("WeightShape")), \
190  QLSTMLayerNormShapeDataSet<qsymm16_per_vector, 1, num_iter>("BiasShape")), \
191  framework::dataset::make("DataType", DataType::QSYMM16)), \
192  framework::dataset::make("WeightQuantizationInfo", { QuantizationInfo(1. / 8192), QuantizationInfo(8192) }))
193 
194 #define QSYMM16_DATASET_1D \
195  concat(concat(QSYMM16_DATASET_ITER(1, 0), QSYMM16_DATASET_ITER(1, 1)), QSYMM16_DATASET_ITER(1, 2))
196 
197 #define QSYMM16_DATASET_2D \
198  concat(concat(QSYMM16_DATASET_ITER(3, 0), QSYMM16_DATASET_ITER(3, 1)), QSYMM16_DATASET_ITER(3, 2))
199 
201 {
202  // Validate output
203  validate(Accessor(_target), _reference);
204 }
205 
207 {
208  // Validate output
209  validate(Accessor(_target), _reference);
210 }
211 
212 #undef QSYMM16_DATASET_ITER
213 #undef QSYMM16_DATASET_2D
214 #undef QSYMM16_DATASET_1D
215 
216 TEST_SUITE_END() // QSYMM16
217 TEST_SUITE_END() // Quantized
218 TEST_SUITE_END() // QLSTMLayerNormalization
219 TEST_SUITE_END() // Neon
220 
221 } // namespace validation
222 } // namespace test
223 } // namespace arm_compute
constexpr uint32_t qsymm16_per_vector
Tests will be targetting.
QLSTMLayerNormalizationValidationFixture< Tensor, Accessor, NEQLSTMLayerNormalization, T > NEQLSTMLayerNormalizationFixture
Status validate(const OperatorGraph &op_graph)
Return the validity of op_graph, usually after performing an operation (e.g.
Shape of a tensor.
Definition: TensorShape.h:39
quantized, symmetric fixed-point 16-bit number
ARM_COMPUTE_EXPECT(has_error==expected, framework::LogLevel::ERRORS)
std::enable_if< is_container< T >::value, ContainerDataset< T > >::type make(std::string name, T &&values)
Helper function to create a ContainerDataset.
Status class.
Definition: Error.h:52
Copyright (c) 2017-2022 Arm Limited.
1 channel, 1 F16 per channel
1 channel, 1 S32 per channel
#define QSYMM16_DATASET_1D
DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(framework::dataset::make("InputInfo", { TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QASYMM8), TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QASYMM8), TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16), }), framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F16), TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QASYMM8), TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QASYMM8), TensorInfo(TensorShape(30U, 11U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16, QuantizationInfo(1.f/32768.f, 0)), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16, QuantizationInfo(1.f/32768.f, 0)), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16, QuantizationInfo(1.f/32768.f, 0)), })), framework::dataset::make("ActivationInfo", { ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::SQRT), })), framework::dataset::make("Expected", { false, true, true, true, false, false, true, true, false })), input_info, output_info, act_info, expected)
Accessor implementation for Tensor objects.
Definition: Accessor.h:35
TEST_SUITE_END() FIXTURE_DATA_TEST_CASE(RunSmall
[CLActivationLayer Test snippet]
validate(CLAccessor(output_state), expected_output)
const char * name
FIXTURE_DATA_TEST_CASE(RunSmall, CLAbsLayerFixture< half >, framework::DatasetMode::PRECOMMIT, combine(datasets::SmallShapes(), framework::dataset::make("DataType", DataType::F16)))
Definition: AbsLayer.cpp:50
#define QSYMM16_DATASET_2D
Store the tensor&#39;s metadata.
Definition: TensorInfo.h:43
zip(zip(framework::dataset::make("Weights", { TensorInfo(TensorShape(32U, 13U, 2U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U, 1U), 1, DataType::F32), }), framework::dataset::make("MVBGInfo",{ TensorInfo(TensorShape(2U), 1, DataType::F32), TensorInfo(TensorShape(2U), 1, DataType::F16), TensorInfo(TensorShape(5U), 1, DataType::F32), })), framework::dataset::make("Expected", { true, false, false}))
TEST_SUITE(QASYMM8_to_F32) FIXTURE_DATA_TEST_CASE(RunSmall
DataType
Available data types.
Definition: Types.h:79