1*c217d954SCole Faust /*
2*c217d954SCole Faust * Copyright (c) 2020-2021 Arm Limited.
3*c217d954SCole Faust *
4*c217d954SCole Faust * SPDX-License-Identifier: MIT
5*c217d954SCole Faust *
6*c217d954SCole Faust * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust *
13*c217d954SCole Faust * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust * copies or substantial portions of the Software.
15*c217d954SCole Faust *
16*c217d954SCole Faust * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust * SOFTWARE.
23*c217d954SCole Faust */
24*c217d954SCole Faust #include "arm_compute/core/Types.h"
25*c217d954SCole Faust #include "arm_compute/runtime/Tensor.h"
26*c217d954SCole Faust #include "arm_compute/runtime/TensorAllocator.h"
27*c217d954SCole Faust #include "src/core/NEON/kernels/NEQLSTMLayerNormalizationKernel.h"
28*c217d954SCole Faust #include "tests/NEON/Accessor.h"
29*c217d954SCole Faust #include "tests/NEON/Helper.h"
30*c217d954SCole Faust #include "tests/PaddingCalculator.h"
31*c217d954SCole Faust #include "tests/datasets/ShapeDatasets.h"
32*c217d954SCole Faust #include "tests/framework/Asserts.h"
33*c217d954SCole Faust #include "tests/framework/Macros.h"
34*c217d954SCole Faust #include "tests/framework/datasets/Datasets.h"
35*c217d954SCole Faust #include "tests/validation/Helpers.h"
36*c217d954SCole Faust #include "tests/validation/Validation.h"
37*c217d954SCole Faust #include "tests/validation/fixtures/QLSTMLayerNormalizationFixture.h"
38*c217d954SCole Faust
39*c217d954SCole Faust namespace arm_compute
40*c217d954SCole Faust {
41*c217d954SCole Faust namespace test
42*c217d954SCole Faust {
43*c217d954SCole Faust namespace validation
44*c217d954SCole Faust {
45*c217d954SCole Faust namespace
46*c217d954SCole Faust {
47*c217d954SCole Faust constexpr uint32_t vector_size_byte = 16;
48*c217d954SCole Faust
49*c217d954SCole Faust using test::datasets::ShapeDataset;
50*c217d954SCole Faust using NEQLSTMLayerNormalization = NESynthetizeFunction<NEQLSTMLayerNormalizationKernel>;
51*c217d954SCole Faust
52*c217d954SCole Faust template <uint32_t num_elements_per_iter, uint32_t num_batches, uint32_t num_iteration>
53*c217d954SCole Faust class QLSTMLayerNormShapeDataSet : public ShapeDataset
54*c217d954SCole Faust {
55*c217d954SCole Faust static constexpr auto boundary_minus_one = num_elements_per_iter * num_iteration - 1;
56*c217d954SCole Faust static constexpr auto boundary = num_elements_per_iter * num_iteration;
57*c217d954SCole Faust static constexpr auto boundary_plus_one = num_elements_per_iter * num_iteration + 1;
58*c217d954SCole Faust
59*c217d954SCole Faust public:
QLSTMLayerNormShapeDataSet(std::string name)60*c217d954SCole Faust QLSTMLayerNormShapeDataSet(std::string name)
61*c217d954SCole Faust : ShapeDataset(name,
62*c217d954SCole Faust {
63*c217d954SCole Faust TensorShape{ boundary_minus_one, num_batches },
64*c217d954SCole Faust TensorShape{ boundary, num_batches },
65*c217d954SCole Faust TensorShape{ boundary_plus_one, num_batches }
66*c217d954SCole Faust })
67*c217d954SCole Faust {
68*c217d954SCole Faust }
69*c217d954SCole Faust };
70*c217d954SCole Faust
71*c217d954SCole Faust template <uint32_t num_elements_per_iter, uint32_t num_batches>
72*c217d954SCole Faust class QLSTMLayerNormShapeDataSet<num_elements_per_iter, num_batches, 0> : public ShapeDataset
73*c217d954SCole Faust {
74*c217d954SCole Faust public:
QLSTMLayerNormShapeDataSet(std::string name)75*c217d954SCole Faust QLSTMLayerNormShapeDataSet(std::string name)
76*c217d954SCole Faust : ShapeDataset(name,
77*c217d954SCole Faust {
78*c217d954SCole Faust TensorShape{ 1, num_batches },
79*c217d954SCole Faust TensorShape{ 2, num_batches }
80*c217d954SCole Faust })
81*c217d954SCole Faust {
82*c217d954SCole Faust }
83*c217d954SCole Faust };
84*c217d954SCole Faust } // namespace
85*c217d954SCole Faust TEST_SUITE(NEON)
TEST_SUITE(QLSTMLayerNormalization) const86*c217d954SCole Faust TEST_SUITE(QLSTMLayerNormalization)
87*c217d954SCole Faust
88*c217d954SCole Faust static const TensorShape correct_input_shape{ TensorShape(15U, 2U) };
89*c217d954SCole Faust static const TensorShape correct_weight_shape{ TensorShape(15U) };
90*c217d954SCole Faust static const TensorShape correct_bias_shape{ TensorShape(15U) };
91*c217d954SCole Faust static const TensorShape correct_output_shape{ correct_input_shape };
92*c217d954SCole Faust static const DataType correct_input_dt{ DataType::QSYMM16 };
93*c217d954SCole Faust static const DataType correct_weight_dt{ DataType::QSYMM16 };
94*c217d954SCole Faust static const DataType correct_bias_dt{ DataType::S32 };
95*c217d954SCole Faust static const DataType correct_output_dt{ correct_input_dt };
96*c217d954SCole Faust static const uint32_t tensor_num_channel{ 1 };
97*c217d954SCole Faust
98*c217d954SCole Faust // *INDENT-OFF*
99*c217d954SCole Faust // clang-format off
100*c217d954SCole Faust
101*c217d954SCole Faust DATA_TEST_CASE(Validate, framework::DatasetMode::ALL,
102*c217d954SCole Faust zip(zip(zip(
103*c217d954SCole Faust framework::dataset::make("InputInfo", {
104*c217d954SCole Faust TensorInfo(correct_input_shape, tensor_num_channel, DataType::F16), // input supports only QSYMM16
105*c217d954SCole Faust TensorInfo(correct_input_shape, tensor_num_channel, correct_input_dt), // weight supports only QSYMM16
106*c217d954SCole Faust TensorInfo(correct_input_shape, tensor_num_channel, correct_input_dt), // bias supports only S32
107*c217d954SCole Faust TensorInfo(TensorShape(15U, 2U, 2U), tensor_num_channel, correct_input_dt), // input supports only up to 2D
108*c217d954SCole Faust TensorInfo(correct_input_shape, tensor_num_channel, correct_input_dt), // weight supports only up to 1D
109*c217d954SCole Faust TensorInfo(correct_input_shape, tensor_num_channel, correct_input_dt), // bias supports only up to 1D
110*c217d954SCole Faust TensorInfo(correct_input_shape, tensor_num_channel, correct_input_dt), // input_shape[0] != weight_shape[0] should fail
111*c217d954SCole Faust TensorInfo(correct_input_shape, tensor_num_channel, correct_input_dt), // weight_shape[0] != bias_shape[0] should fail
112*c217d954SCole Faust TensorInfo(correct_input_shape, tensor_num_channel, correct_input_dt), // output shape mismatches with input shape
113*c217d954SCole Faust TensorInfo(correct_input_shape, tensor_num_channel, correct_input_dt), // output data type mismatches with input data type
114*c217d954SCole Faust }),
115*c217d954SCole Faust framework::dataset::make("WeightInfo", {
116*c217d954SCole Faust TensorInfo(correct_weight_shape, tensor_num_channel, correct_weight_dt),
117*c217d954SCole Faust TensorInfo(correct_weight_shape, tensor_num_channel, DataType::F16),
118*c217d954SCole Faust TensorInfo(correct_weight_shape, tensor_num_channel, correct_weight_dt),
119*c217d954SCole Faust TensorInfo(correct_weight_shape, tensor_num_channel, correct_weight_dt),
120*c217d954SCole Faust TensorInfo(TensorShape(15U, 2U), tensor_num_channel, correct_weight_dt),
121*c217d954SCole Faust TensorInfo(correct_weight_shape, tensor_num_channel, correct_weight_dt),
122*c217d954SCole Faust TensorInfo(TensorShape(14U), tensor_num_channel, correct_weight_dt),
123*c217d954SCole Faust TensorInfo(correct_weight_shape, tensor_num_channel, correct_weight_dt),
124*c217d954SCole Faust TensorInfo(correct_weight_shape, tensor_num_channel, correct_weight_dt),
125*c217d954SCole Faust TensorInfo(correct_weight_shape, tensor_num_channel, correct_weight_dt),
126*c217d954SCole Faust })
127*c217d954SCole Faust ),
128*c217d954SCole Faust framework::dataset::make("BiasInfo", {
129*c217d954SCole Faust TensorInfo(correct_bias_shape, tensor_num_channel, correct_bias_dt),
130*c217d954SCole Faust TensorInfo(correct_bias_shape, tensor_num_channel, correct_bias_dt),
131*c217d954SCole Faust TensorInfo(correct_bias_shape, tensor_num_channel, DataType::QSYMM16),
132*c217d954SCole Faust TensorInfo(correct_bias_shape, tensor_num_channel, correct_bias_dt),
133*c217d954SCole Faust TensorInfo(correct_bias_shape, tensor_num_channel, correct_bias_dt),
134*c217d954SCole Faust TensorInfo(TensorShape(15U, 2U), tensor_num_channel, correct_bias_dt),
135*c217d954SCole Faust TensorInfo(correct_bias_shape, tensor_num_channel, correct_bias_dt),
136*c217d954SCole Faust TensorInfo(TensorShape(14U), tensor_num_channel, correct_bias_dt),
137*c217d954SCole Faust TensorInfo(correct_bias_shape, tensor_num_channel, correct_bias_dt),
138*c217d954SCole Faust TensorInfo(correct_bias_shape, tensor_num_channel, correct_bias_dt),
139*c217d954SCole Faust })
140*c217d954SCole Faust ),
141*c217d954SCole Faust framework::dataset::make("OutputInfo", {
142*c217d954SCole Faust TensorInfo(correct_output_shape, tensor_num_channel, correct_output_dt),
143*c217d954SCole Faust TensorInfo(correct_output_shape, tensor_num_channel, correct_output_dt),
144*c217d954SCole Faust TensorInfo(correct_output_shape, tensor_num_channel, correct_output_dt),
145*c217d954SCole Faust TensorInfo(correct_output_shape, tensor_num_channel, correct_output_dt),
146*c217d954SCole Faust TensorInfo(correct_output_shape, tensor_num_channel, correct_output_dt),
147*c217d954SCole Faust TensorInfo(correct_output_shape, tensor_num_channel, correct_output_dt),
148*c217d954SCole Faust TensorInfo(correct_output_shape, tensor_num_channel, correct_output_dt),
149*c217d954SCole Faust TensorInfo(correct_output_shape, tensor_num_channel, correct_output_dt),
150*c217d954SCole Faust TensorInfo(TensorShape(15, 3), tensor_num_channel, correct_output_dt),
151*c217d954SCole Faust TensorInfo(correct_output_shape, tensor_num_channel, DataType::S32),
152*c217d954SCole Faust })
153*c217d954SCole Faust ),
154*c217d954SCole Faust input_info, weight_info, bias_info, output_info)
155*c217d954SCole Faust {
156*c217d954SCole Faust const Status s = NEQLSTMLayerNormalization::validate(&input_info, &output_info, &weight_info, &bias_info);
157*c217d954SCole Faust ARM_COMPUTE_EXPECT(!bool(s), framework::LogLevel::ERRORS);
158*c217d954SCole Faust }
159*c217d954SCole Faust
160*c217d954SCole Faust // clang-format on
161*c217d954SCole Faust // *INDENT-ON*
162*c217d954SCole Faust
163*c217d954SCole Faust template <typename T>
164*c217d954SCole Faust using NEQLSTMLayerNormalizationFixture = QLSTMLayerNormalizationValidationFixture<Tensor, Accessor, NEQLSTMLayerNormalization, T>;
165*c217d954SCole Faust
166*c217d954SCole Faust TEST_SUITE(Quantized)
167*c217d954SCole Faust TEST_SUITE(QSYMM16)
168*c217d954SCole Faust
169*c217d954SCole Faust /** Tests will be targetting
170*c217d954SCole Faust * - Comparison between optimized kernel and the exact same but scalar version of reference kernel
171*c217d954SCole Faust * - Input shapes of 1D and 2D with the first dimension covers boundary values of 128-bit vector size (0~3 iterations)
172*c217d954SCole Faust * - Weight and bias 1D shape that have same size as that of input shapes
173*c217d954SCole Faust * - Quantization scale is greater and smaller than one.
174*c217d954SCole Faust * - Input values will be noted in fixture.
175*c217d954SCole Faust *
176*c217d954SCole Faust * What we can't test
177*c217d954SCole Faust * - Since reference kernel uses the exact the same algorithm in the same quantized domain
178*c217d954SCole Faust * it is hard to fully test whether the algorithm accomplishes what it is supposed to.
179*c217d954SCole Faust * - The algorithm has been sensitive to quantization scale but it is hard to fully test
180*c217d954SCole Faust * the sensitivity due to aforementioned reason.
181*c217d954SCole Faust * - Again, it is hard to fully test corner values due to the exact same algorithm of the
182*c217d954SCole Faust * reference kernel and the optimized kernel.
183*c217d954SCole Faust */
184*c217d954SCole Faust
185*c217d954SCole Faust constexpr uint32_t qsymm16_per_vector = vector_size_byte / sizeof(int16_t);
186*c217d954SCole Faust
187*c217d954SCole Faust #define QSYMM16_DATASET_ITER(num_input_batch, num_iter) \
188*c217d954SCole Faust combine(combine(zip(zip(QLSTMLayerNormShapeDataSet<qsymm16_per_vector, num_input_batch, num_iter>("InputShape"), \
189*c217d954SCole Faust QLSTMLayerNormShapeDataSet<qsymm16_per_vector, 1, num_iter>("WeightShape")), \
190*c217d954SCole Faust QLSTMLayerNormShapeDataSet<qsymm16_per_vector, 1, num_iter>("BiasShape")), \
191*c217d954SCole Faust framework::dataset::make("DataType", DataType::QSYMM16)), \
192*c217d954SCole Faust framework::dataset::make("WeightQuantizationInfo", { QuantizationInfo(1. / 8192), QuantizationInfo(8192) }))
193*c217d954SCole Faust
194*c217d954SCole Faust #define QSYMM16_DATASET_1D \
195*c217d954SCole Faust concat(concat(QSYMM16_DATASET_ITER(1, 0), QSYMM16_DATASET_ITER(1, 1)), QSYMM16_DATASET_ITER(1, 2))
196*c217d954SCole Faust
197*c217d954SCole Faust #define QSYMM16_DATASET_2D \
198*c217d954SCole Faust concat(concat(QSYMM16_DATASET_ITER(3, 0), QSYMM16_DATASET_ITER(3, 1)), QSYMM16_DATASET_ITER(3, 2))
199*c217d954SCole Faust
FIXTURE_DATA_TEST_CASE(RandomValue1D,NEQLSTMLayerNormalizationFixture<int16_t>,framework::DatasetMode::ALL,QSYMM16_DATASET_1D)200*c217d954SCole Faust FIXTURE_DATA_TEST_CASE(RandomValue1D, NEQLSTMLayerNormalizationFixture<int16_t>, framework::DatasetMode::ALL, QSYMM16_DATASET_1D)
201*c217d954SCole Faust {
202*c217d954SCole Faust // Validate output
203*c217d954SCole Faust validate(Accessor(_target), _reference);
204*c217d954SCole Faust }
205*c217d954SCole Faust
FIXTURE_DATA_TEST_CASE(RandomValue2D,NEQLSTMLayerNormalizationFixture<int16_t>,framework::DatasetMode::ALL,QSYMM16_DATASET_2D)206*c217d954SCole Faust FIXTURE_DATA_TEST_CASE(RandomValue2D, NEQLSTMLayerNormalizationFixture<int16_t>, framework::DatasetMode::ALL, QSYMM16_DATASET_2D)
207*c217d954SCole Faust {
208*c217d954SCole Faust // Validate output
209*c217d954SCole Faust validate(Accessor(_target), _reference);
210*c217d954SCole Faust }
211*c217d954SCole Faust
212*c217d954SCole Faust #undef QSYMM16_DATASET_ITER
213*c217d954SCole Faust #undef QSYMM16_DATASET_2D
214*c217d954SCole Faust #undef QSYMM16_DATASET_1D
215*c217d954SCole Faust
216*c217d954SCole Faust TEST_SUITE_END() // QSYMM16
217*c217d954SCole Faust TEST_SUITE_END() // Quantized
218*c217d954SCole Faust TEST_SUITE_END() // QLSTMLayerNormalization
219*c217d954SCole Faust TEST_SUITE_END() // Neon
220*c217d954SCole Faust
221*c217d954SCole Faust } // namespace validation
222*c217d954SCole Faust } // namespace test
223*c217d954SCole Faust } // namespace arm_compute
224