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