blob: c2ab2e2ef6c7ae879547da1e7301352b31314024 [file] [log] [blame]
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +01001/*
2 * Copyright (c) 2017 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#ifndef ARM_COMPUTE_TEST_SOFTMAX_LAYER_FIXTURE
25#define ARM_COMPUTE_TEST_SOFTMAX_LAYER_FIXTURE
26
27#include "arm_compute/core/TensorShape.h"
28#include "arm_compute/core/Types.h"
29#include "arm_compute/runtime/Tensor.h"
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010030#include "tests/AssetsLibrary.h"
31#include "tests/Globals.h"
32#include "tests/IAccessor.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010033#include "tests/framework/Asserts.h"
34#include "tests/framework/Fixture.h"
Georgios Pinitas5a7e7762017-12-01 16:27:29 +000035#include "tests/validation/reference/SoftmaxLayer.h"
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010036
37#include <random>
38
39namespace arm_compute
40{
41namespace test
42{
43namespace validation
44{
45template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Chunosovf450caa2017-11-08 16:09:35 +070046class SoftmaxValidationGenericFixture : public framework::Fixture
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010047{
48public:
49 template <typename...>
Pablo Palmiera2b89ca2017-10-05 15:01:34 +010050 void setup(TensorShape shape, DataType data_type, int fractional_bits, QuantizationInfo quantization_info, float beta)
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010051 {
Chunosovf450caa2017-11-08 16:09:35 +070052 _fractional_bits = fractional_bits;
53 _quantization_info = quantization_info;
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010054
Pablo Palmiera2b89ca2017-10-05 15:01:34 +010055 _target = compute_target(shape, data_type, fractional_bits, quantization_info, beta);
56 _reference = compute_reference(shape, data_type, fractional_bits, quantization_info, beta);
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010057 }
58
59protected:
60 template <typename U>
61 void fill(U &&tensor)
62 {
Chunosovf450caa2017-11-08 16:09:35 +070063 if(!is_data_type_quantized(tensor.data_type()))
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010064 {
65 std::uniform_real_distribution<> distribution(-1000.f, 1000.f);
66 library->fill(tensor, distribution, 0);
67 }
Chunosovf450caa2017-11-08 16:09:35 +070068 else if(is_data_type_quantized_asymmetric(tensor.data_type()))
69 {
70 std::uniform_int_distribution<> distribution(0, 100);
71 library->fill(tensor, distribution, 0);
72 }
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010073 else
74 {
75 const int one_fixed = 1 << _fractional_bits;
76 std::uniform_int_distribution<> distribution(-one_fixed, one_fixed);
77 library->fill(tensor, distribution, 0);
78 }
79 }
80
Pablo Palmiera2b89ca2017-10-05 15:01:34 +010081 TensorType compute_target(const TensorShape &shape, DataType data_type, int fixed_point_position,
82 QuantizationInfo quantization_info, float beta)
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010083 {
84 // Create tensors
Chunosovf450caa2017-11-08 16:09:35 +070085 TensorType src = create_tensor<TensorType>(shape, data_type, 1, fixed_point_position, quantization_info);
86 TensorType dst = create_tensor<TensorType>(shape, data_type, 1, fixed_point_position, QuantizationInfo(1.f / 256, 0));
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010087
88 // Create and configure function
89 FunctionType smx_layer;
Pablo Palmiera2b89ca2017-10-05 15:01:34 +010090 smx_layer.configure(&src, &dst, beta);
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010091
92 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
93 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
94
95 // Allocate tensors
96 src.allocator()->allocate();
97 dst.allocator()->allocate();
98
99 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
100 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
101
102 // Fill tensors
103 fill(AccessorType(src));
104
105 // Compute function
106 smx_layer.run();
107
108 return dst;
109 }
110
Pablo Palmiera2b89ca2017-10-05 15:01:34 +0100111 SimpleTensor<T> compute_reference(const TensorShape &shape, DataType data_type, int fixed_point_position,
112 QuantizationInfo quantization_info, float beta)
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100113 {
114 // Create reference
Chunosovf450caa2017-11-08 16:09:35 +0700115 SimpleTensor<T> src{ shape, data_type, 1, fixed_point_position, quantization_info };
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100116
117 // Fill reference
118 fill(src);
119
Pablo Palmiera2b89ca2017-10-05 15:01:34 +0100120 return reference::softmax_layer<T>(src, beta);
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100121 }
122
Chunosovf450caa2017-11-08 16:09:35 +0700123 TensorType _target{};
124 SimpleTensor<T> _reference{};
125 int _fractional_bits{};
126 QuantizationInfo _quantization_info{};
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100127};
128
129template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Chunosovf450caa2017-11-08 16:09:35 +0700130class SoftmaxValidationFixture : public SoftmaxValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100131{
132public:
133 template <typename...>
Pablo Palmiera2b89ca2017-10-05 15:01:34 +0100134 void setup(TensorShape shape, DataType data_type, float beta)
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100135 {
Pablo Palmiera2b89ca2017-10-05 15:01:34 +0100136 SoftmaxValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape,
137 data_type,
138 0,
139 QuantizationInfo(),
140 beta);
Chunosovf450caa2017-11-08 16:09:35 +0700141 }
142};
143
144template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
145class SoftmaxValidationFixedPointFixture : public SoftmaxValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
146{
147public:
148 template <typename...>
149 void setup(TensorShape shape, DataType data_type, int fixed_point_position)
150 {
Pablo Palmiera2b89ca2017-10-05 15:01:34 +0100151 SoftmaxValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape,
152 data_type,
153 fixed_point_position,
154 QuantizationInfo(),
155 1.0f);
Chunosovf450caa2017-11-08 16:09:35 +0700156 }
157};
158
159template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
160class SoftmaxValidationQuantizedFixture : public SoftmaxValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
161{
162public:
163 template <typename...>
Pablo Palmiera2b89ca2017-10-05 15:01:34 +0100164 void setup(TensorShape shape, DataType data_type, QuantizationInfo quantization_info, float beta)
Chunosovf450caa2017-11-08 16:09:35 +0700165 {
Pablo Palmiera2b89ca2017-10-05 15:01:34 +0100166 SoftmaxValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape,
167 data_type,
168 0,
169 quantization_info,
170 beta);
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100171 }
172};
173} // namespace validation
174} // namespace test
175} // namespace arm_compute
176#endif /* ARM_COMPUTE_TEST_SOFTMAX_LAYER_FIXTURE */