xref: /aosp_15_r20/external/ComputeLibrary/tests/validation/CL/QLSTMLayerNormalization.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1*c217d954SCole Faust /*
2*c217d954SCole Faust  * Copyright (c) 2020 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 "src/core/CL/kernels/CLQLSTMLayerNormalizationKernel.h"
25*c217d954SCole Faust #include "tests/CL/CLAccessor.h"
26*c217d954SCole Faust #include "tests/CL/Helper.h"
27*c217d954SCole Faust #include "tests/PaddingCalculator.h"
28*c217d954SCole Faust #include "tests/datasets/ShapeDatasets.h"
29*c217d954SCole Faust #include "tests/framework/Asserts.h"
30*c217d954SCole Faust #include "tests/framework/Macros.h"
31*c217d954SCole Faust #include "tests/framework/datasets/Datasets.h"
32*c217d954SCole Faust #include "tests/validation/Helpers.h"
33*c217d954SCole Faust #include "tests/validation/Validation.h"
34*c217d954SCole Faust #include "tests/validation/fixtures/QLSTMLayerNormalizationFixture.h"
35*c217d954SCole Faust 
36*c217d954SCole Faust namespace arm_compute
37*c217d954SCole Faust {
38*c217d954SCole Faust namespace test
39*c217d954SCole Faust {
40*c217d954SCole Faust namespace validation
41*c217d954SCole Faust {
42*c217d954SCole Faust namespace
43*c217d954SCole Faust {
44*c217d954SCole Faust constexpr AbsoluteTolerance<int16_t> tolerance_s16(0); /**< Tolerance value for comparing reference's output against implementation's output for QSYMM16 data types */
45*c217d954SCole Faust constexpr uint32_t                   vector_size_byte = 16;
46*c217d954SCole Faust 
47*c217d954SCole Faust using test::datasets::ShapeDataset;
48*c217d954SCole Faust using CLQLSTMLayerNormalization = CLSynthetizeFunction<CLQLSTMLayerNormalizationKernel>;
49*c217d954SCole Faust template <uint32_t num_elements_per_iter, uint32_t num_batches, uint32_t num_iteration>
50*c217d954SCole Faust class QLSTMLayerNormShapeDataSet : public ShapeDataset
51*c217d954SCole Faust {
52*c217d954SCole Faust     static constexpr auto boundary_minus_one = num_elements_per_iter * num_iteration - 1;
53*c217d954SCole Faust     static constexpr auto boundary           = num_elements_per_iter * num_iteration;
54*c217d954SCole Faust     static constexpr auto boundary_plus_one  = num_elements_per_iter * num_iteration + 1;
55*c217d954SCole Faust 
56*c217d954SCole Faust public:
QLSTMLayerNormShapeDataSet(std::string name)57*c217d954SCole Faust     QLSTMLayerNormShapeDataSet(std::string name)
58*c217d954SCole Faust         : ShapeDataset(name,
59*c217d954SCole Faust     {
60*c217d954SCole Faust         TensorShape{ boundary_minus_one, num_batches },
61*c217d954SCole Faust                      TensorShape{ boundary, num_batches },
62*c217d954SCole Faust                      TensorShape{ boundary_plus_one, num_batches }
63*c217d954SCole Faust     })
64*c217d954SCole Faust     {
65*c217d954SCole Faust     }
66*c217d954SCole Faust };
67*c217d954SCole Faust 
68*c217d954SCole Faust template <uint32_t num_elements_per_iter, uint32_t num_batches>
69*c217d954SCole Faust class QLSTMLayerNormShapeDataSet<num_elements_per_iter, num_batches, 0> : public ShapeDataset
70*c217d954SCole Faust {
71*c217d954SCole Faust public:
QLSTMLayerNormShapeDataSet(std::string name)72*c217d954SCole Faust     QLSTMLayerNormShapeDataSet(std::string name)
73*c217d954SCole Faust         : ShapeDataset(name,
74*c217d954SCole Faust     {
75*c217d954SCole Faust         TensorShape{ 1, num_batches },
76*c217d954SCole Faust                      TensorShape{ 2, num_batches }
77*c217d954SCole Faust     })
78*c217d954SCole Faust     {
79*c217d954SCole Faust     }
80*c217d954SCole Faust };
81*c217d954SCole Faust } // namespace
82*c217d954SCole Faust TEST_SUITE(CL)
TEST_SUITE(QLSTMLayerNormalization) const83*c217d954SCole Faust TEST_SUITE(QLSTMLayerNormalization)
84*c217d954SCole Faust 
85*c217d954SCole Faust static const TensorShape correct_input_shape{ TensorShape(15U, 2U) };
86*c217d954SCole Faust static const TensorShape correct_weight_shape{ TensorShape(15U) };
87*c217d954SCole Faust static const TensorShape correct_bias_shape{ TensorShape(15U) };
88*c217d954SCole Faust static const DataType    correct_input_dt{ DataType::QSYMM16 };
89*c217d954SCole Faust static const DataType    correct_weight_dt{ DataType::QSYMM16 };
90*c217d954SCole Faust static const DataType    correct_bias_dt{ DataType::S32 };
91*c217d954SCole Faust static const uint32_t    tensor_num_channel{ 1 };
92*c217d954SCole Faust 
93*c217d954SCole Faust // *INDENT-OFF*
94*c217d954SCole Faust // clang-format off
95*c217d954SCole Faust 
96*c217d954SCole Faust DATA_TEST_CASE(Validate, framework::DatasetMode::ALL,
97*c217d954SCole Faust     zip(zip(
98*c217d954SCole Faust         framework::dataset::make("InputInfo", {
99*c217d954SCole Faust             TensorInfo(correct_input_shape, tensor_num_channel, DataType::F16), // input supports only QSYMM16
100*c217d954SCole Faust             TensorInfo(correct_input_shape, tensor_num_channel, correct_input_dt), // weight supports only QSYMM16
101*c217d954SCole Faust             TensorInfo(correct_input_shape, tensor_num_channel, correct_input_dt), // bias supports only S32
102*c217d954SCole Faust             TensorInfo(TensorShape(15U, 2U, 2U), tensor_num_channel, correct_input_dt), // input supports only up to 2D
103*c217d954SCole Faust             TensorInfo(correct_input_shape, tensor_num_channel, correct_input_dt), // weight supports only up to 1D
104*c217d954SCole Faust             TensorInfo(correct_input_shape, tensor_num_channel, correct_input_dt), // bias supports only up to 1D
105*c217d954SCole Faust             TensorInfo(correct_input_shape, tensor_num_channel, correct_input_dt), // input_shape[0] != weight_shape[0] should fail
106*c217d954SCole Faust             TensorInfo(correct_input_shape, tensor_num_channel, correct_input_dt), // weight_shape[0] != bias_shape[0] should fail
107*c217d954SCole Faust         }),
108*c217d954SCole Faust         framework::dataset::make("WeightInfo", {
109*c217d954SCole Faust             TensorInfo(correct_weight_shape, tensor_num_channel, correct_weight_dt),
110*c217d954SCole Faust             TensorInfo(correct_weight_shape, tensor_num_channel, DataType::F16),
111*c217d954SCole Faust             TensorInfo(correct_weight_shape, tensor_num_channel, correct_weight_dt),
112*c217d954SCole Faust             TensorInfo(correct_weight_shape, tensor_num_channel, correct_weight_dt),
113*c217d954SCole Faust             TensorInfo(TensorShape(15U, 2U), tensor_num_channel, correct_weight_dt),
114*c217d954SCole Faust             TensorInfo(correct_weight_shape, tensor_num_channel, correct_weight_dt),
115*c217d954SCole Faust             TensorInfo(TensorShape(14U), tensor_num_channel, correct_weight_dt),
116*c217d954SCole Faust             TensorInfo(correct_weight_shape, tensor_num_channel, correct_weight_dt),
117*c217d954SCole Faust         })
118*c217d954SCole Faust     ),
119*c217d954SCole Faust         framework::dataset::make("BiasInfo", {
120*c217d954SCole Faust             TensorInfo(correct_bias_shape, tensor_num_channel, correct_bias_dt),
121*c217d954SCole Faust             TensorInfo(correct_bias_shape, tensor_num_channel, correct_bias_dt),
122*c217d954SCole Faust             TensorInfo(correct_bias_shape, tensor_num_channel, DataType::QSYMM16),
123*c217d954SCole Faust             TensorInfo(correct_bias_shape, tensor_num_channel, correct_bias_dt),
124*c217d954SCole Faust             TensorInfo(correct_bias_shape, tensor_num_channel, correct_bias_dt),
125*c217d954SCole Faust             TensorInfo(TensorShape(15U, 2U), tensor_num_channel, correct_bias_dt),
126*c217d954SCole Faust             TensorInfo(correct_bias_shape, tensor_num_channel, correct_bias_dt),
127*c217d954SCole Faust             TensorInfo(TensorShape(14U), tensor_num_channel, correct_bias_dt),
128*c217d954SCole Faust         })
129*c217d954SCole Faust     ), input_info, weight_info, bias_info)
130*c217d954SCole Faust {
131*c217d954SCole Faust     TensorInfo dummy_output{};
132*c217d954SCole Faust     const Status s = CLQLSTMLayerNormalization::validate(&input_info, &dummy_output, &weight_info, &bias_info);
133*c217d954SCole Faust     ARM_COMPUTE_EXPECT(!bool(s), framework::LogLevel::ERRORS);
134*c217d954SCole Faust }
135*c217d954SCole Faust 
136*c217d954SCole Faust // clang-format on
137*c217d954SCole Faust // *INDENT-ON*
138*c217d954SCole Faust 
139*c217d954SCole Faust template <typename T>
140*c217d954SCole Faust using CLQLSTMLayerNormalizationFixture = QLSTMLayerNormalizationValidationFixture<CLTensor, CLAccessor, CLQLSTMLayerNormalization, T>;
141*c217d954SCole Faust 
142*c217d954SCole Faust TEST_SUITE(Quantized)
143*c217d954SCole Faust TEST_SUITE(QSYMM16)
144*c217d954SCole Faust 
145*c217d954SCole Faust /** Tests will be targetting
146*c217d954SCole Faust  * - Comparison between OpenCL kernel and the exact same but scalar version of reference kernel
147*c217d954SCole Faust  * - Input shapes of 1D and 2D with the first dimension covers boundary values of 128-bit vector size (0~3 iterations)
148*c217d954SCole Faust  * - Weight and bias 1D shape that have same size as that of input shapes
149*c217d954SCole Faust  * - Quantization scale is greater and smaller than one.
150*c217d954SCole Faust  * - Input values will be noted in fixture.
151*c217d954SCole Faust  *
152*c217d954SCole Faust  * What we can't test
153*c217d954SCole Faust  * - Since reference kernel uses the exact the same algorithm in the same quantized domain
154*c217d954SCole Faust  *   it is hard to fully test whether the algorithm accomplishes what it is supposed to.
155*c217d954SCole Faust  * - The algorithm has been sensitive to quantization scale but it is hard to fully test
156*c217d954SCole Faust  *   the sensitivity due to aforementioned reason.
157*c217d954SCole Faust  * - Again, it is hard to fully test corner values due to the exact same algorithm of the
158*c217d954SCole Faust  *   reference kernel and the OpenCL kernel.
159*c217d954SCole Faust  */
160*c217d954SCole Faust 
161*c217d954SCole Faust constexpr uint32_t qsymm16_per_vector = vector_size_byte / sizeof(int16_t);
162*c217d954SCole Faust 
163*c217d954SCole Faust #define QSYMM16_DATASET_ITER(num_input_batch, num_iter)                                                              \
164*c217d954SCole Faust     combine(combine(zip(zip(QLSTMLayerNormShapeDataSet<qsymm16_per_vector, num_input_batch, num_iter>("InputShape"), \
165*c217d954SCole Faust                             QLSTMLayerNormShapeDataSet<qsymm16_per_vector, 1, num_iter>("WeightShape")),             \
166*c217d954SCole Faust                         QLSTMLayerNormShapeDataSet<qsymm16_per_vector, 1, num_iter>("BiasShape")),                   \
167*c217d954SCole Faust                     framework::dataset::make("DataType", DataType::QSYMM16)),                                        \
168*c217d954SCole Faust             framework::dataset::make("InputQuantizationInfo", { QuantizationInfo(1. / 8192), QuantizationInfo(8192) }))
169*c217d954SCole Faust 
170*c217d954SCole Faust #define QSYMM16_DATASET_1D \
171*c217d954SCole Faust     concat(concat(QSYMM16_DATASET_ITER(1, 0), QSYMM16_DATASET_ITER(1, 1)), QSYMM16_DATASET_ITER(1, 2))
172*c217d954SCole Faust 
173*c217d954SCole Faust #define QSYMM16_DATASET_2D \
174*c217d954SCole Faust     concat(concat(QSYMM16_DATASET_ITER(3, 0), QSYMM16_DATASET_ITER(3, 1)), QSYMM16_DATASET_ITER(3, 2))
175*c217d954SCole Faust 
FIXTURE_DATA_TEST_CASE(RandomValue1D,CLQLSTMLayerNormalizationFixture<int16_t>,framework::DatasetMode::ALL,QSYMM16_DATASET_1D)176*c217d954SCole Faust FIXTURE_DATA_TEST_CASE(RandomValue1D, CLQLSTMLayerNormalizationFixture<int16_t>, framework::DatasetMode::ALL, QSYMM16_DATASET_1D)
177*c217d954SCole Faust {
178*c217d954SCole Faust     // Validate output
179*c217d954SCole Faust     validate(CLAccessor(_target), _reference, tolerance_s16);
180*c217d954SCole Faust }
181*c217d954SCole Faust 
FIXTURE_DATA_TEST_CASE(RandomValue2D,CLQLSTMLayerNormalizationFixture<int16_t>,framework::DatasetMode::ALL,QSYMM16_DATASET_2D)182*c217d954SCole Faust FIXTURE_DATA_TEST_CASE(RandomValue2D, CLQLSTMLayerNormalizationFixture<int16_t>, framework::DatasetMode::ALL, QSYMM16_DATASET_2D)
183*c217d954SCole Faust {
184*c217d954SCole Faust     // Validate output
185*c217d954SCole Faust     validate(CLAccessor(_target), _reference, tolerance_s16);
186*c217d954SCole Faust }
187*c217d954SCole Faust 
188*c217d954SCole Faust #undef QSYMM16_DATASET_ITER
189*c217d954SCole Faust #undef QSYMM16_DATASET_2D
190*c217d954SCole Faust #undef QSYMM16_DATASET_1D
191*c217d954SCole Faust 
192*c217d954SCole Faust TEST_SUITE_END() // QSYMM16
193*c217d954SCole Faust TEST_SUITE_END() // Quantized
194*c217d954SCole Faust TEST_SUITE_END() // QLSTMLayerNormalization
195*c217d954SCole Faust TEST_SUITE_END() // CL
196*c217d954SCole Faust 
197*c217d954SCole Faust } // namespace validation
198*c217d954SCole Faust } // namespace test
199*c217d954SCole Faust } // namespace arm_compute
200