blob: 97382131149d6eaf0bdae7ae8bb611bf036c0a35 [file] [log] [blame]
Sang-Hoon Park0d008f72020-03-13 14:56:05 +00001/*
Sheri Zhangac6499a2021-02-10 15:32:38 +00002 * Copyright (c) 2020-2021 Arm Limited.
Sang-Hoon Park0d008f72020-03-13 14:56:05 +00003 *
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 */
Sang-Hoon Park0d008f72020-03-13 14:56:05 +000024#include "arm_compute/core/Types.h"
25#include "arm_compute/runtime/Tensor.h"
26#include "arm_compute/runtime/TensorAllocator.h"
Michalis Spyrouebcebf12020-10-21 00:04:14 +010027#include "src/core/NEON/kernels/NEQLSTMLayerNormalizationKernel.h"
Sang-Hoon Park0d008f72020-03-13 14:56:05 +000028#include "tests/NEON/Accessor.h"
Sheri Zhang45198c82020-04-14 22:29:36 +010029#include "tests/NEON/Helper.h"
Sang-Hoon Park0d008f72020-03-13 14:56:05 +000030#include "tests/PaddingCalculator.h"
31#include "tests/datasets/ShapeDatasets.h"
32#include "tests/framework/Asserts.h"
33#include "tests/framework/Macros.h"
34#include "tests/framework/datasets/Datasets.h"
35#include "tests/validation/Helpers.h"
36#include "tests/validation/Validation.h"
37#include "tests/validation/fixtures/QLSTMLayerNormalizationFixture.h"
38
39namespace arm_compute
40{
41namespace test
42{
43namespace validation
44{
45namespace
46{
47constexpr uint32_t vector_size_byte = 16;
48
49using test::datasets::ShapeDataset;
Sheri Zhang45198c82020-04-14 22:29:36 +010050using NEQLSTMLayerNormalization = NESynthetizeFunction<NEQLSTMLayerNormalizationKernel>;
51
Sang-Hoon Park0d008f72020-03-13 14:56:05 +000052template <uint32_t num_elements_per_iter, uint32_t num_batches, uint32_t num_iteration>
53class 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
59public:
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
71template <uint32_t num_elements_per_iter, uint32_t num_batches>
72class QLSTMLayerNormShapeDataSet<num_elements_per_iter, num_batches, 0> : public ShapeDataset
73{
74public:
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
85TEST_SUITE(NEON)
86TEST_SUITE(QLSTMLayerNormalization)
87
88static const TensorShape correct_input_shape{ TensorShape(15U, 2U) };
89static const TensorShape correct_weight_shape{ TensorShape(15U) };
90static const TensorShape correct_bias_shape{ TensorShape(15U) };
91static const TensorShape correct_output_shape{ correct_input_shape };
92static const DataType correct_input_dt{ DataType::QSYMM16 };
93static const DataType correct_weight_dt{ DataType::QSYMM16 };
94static const DataType correct_bias_dt{ DataType::S32 };
95static const DataType correct_output_dt{ correct_input_dt };
96static const uint32_t tensor_num_channel{ 1 };
97
98// *INDENT-OFF*
99// clang-format off
100
101DATA_TEST_CASE(Validate, framework::DatasetMode::ALL,
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{
Sheri Zhang45198c82020-04-14 22:29:36 +0100156 const Status s = NEQLSTMLayerNormalization::validate(&input_info, &output_info, &weight_info, &bias_info);
Sang-Hoon Park0d008f72020-03-13 14:56:05 +0000157 ARM_COMPUTE_EXPECT(!bool(s), framework::LogLevel::ERRORS);
158}
159
160// clang-format on
161// *INDENT-ON*
162
163template <typename T>
Sheri Zhang45198c82020-04-14 22:29:36 +0100164using NEQLSTMLayerNormalizationFixture = QLSTMLayerNormalizationValidationFixture<Tensor, Accessor, NEQLSTMLayerNormalization, T>;
Sang-Hoon Park0d008f72020-03-13 14:56:05 +0000165
166TEST_SUITE(Quantized)
167TEST_SUITE(QSYMM16)
168
169/** Tests will be targetting
Michele Di Giorgio33f41fa2021-03-09 14:09:08 +0000170 * - Comparison between optimized kernel and the exact same but scalar version of reference kernel
Sang-Hoon Park0d008f72020-03-13 14:56:05 +0000171 * - 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
Michele Di Giorgio33f41fa2021-03-09 14:09:08 +0000182 * reference kernel and the optimized kernel.
Sang-Hoon Park0d008f72020-03-13 14:56:05 +0000183 */
184
185constexpr 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
200FIXTURE_DATA_TEST_CASE(RandomValue1D, NEQLSTMLayerNormalizationFixture<int16_t>, framework::DatasetMode::ALL, QSYMM16_DATASET_1D)
201{
202 // Validate output
203 validate(Accessor(_target), _reference);
204}
205
206FIXTURE_DATA_TEST_CASE(RandomValue2D, NEQLSTMLayerNormalizationFixture<int16_t>, framework::DatasetMode::ALL, QSYMM16_DATASET_2D)
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
216TEST_SUITE_END() // QSYMM16
217TEST_SUITE_END() // Quantized
218TEST_SUITE_END() // QLSTMLayerNormalization
Sheri Zhangac6499a2021-02-10 15:32:38 +0000219TEST_SUITE_END() // Neon
Sang-Hoon Park0d008f72020-03-13 14:56:05 +0000220
221} // namespace validation
222} // namespace test
223} // namespace arm_compute