blob: 9836502cd2aade97a056faf20bd367662082799e [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"
35#include "tests/validation/CPP/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...>
Chunosovf450caa2017-11-08 16:09:35 +070050 void setup(TensorShape shape, DataType data_type, int fractional_bits, QuantizationInfo quantization_info)
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
Chunosovf450caa2017-11-08 16:09:35 +070055 _target = compute_target(shape, data_type, fractional_bits, quantization_info);
56 _reference = compute_reference(shape, data_type, fractional_bits, quantization_info);
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
Chunosovf450caa2017-11-08 16:09:35 +070081 TensorType compute_target(const TensorShape &shape, DataType data_type, int fixed_point_position, QuantizationInfo quantization_info)
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010082 {
83 // Create tensors
Chunosovf450caa2017-11-08 16:09:35 +070084 TensorType src = create_tensor<TensorType>(shape, data_type, 1, fixed_point_position, quantization_info);
85 TensorType dst = create_tensor<TensorType>(shape, data_type, 1, fixed_point_position, QuantizationInfo(1.f / 256, 0));
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010086
87 // Create and configure function
88 FunctionType smx_layer;
89 smx_layer.configure(&src, &dst);
90
91 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
92 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
93
94 // Allocate tensors
95 src.allocator()->allocate();
96 dst.allocator()->allocate();
97
98 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
99 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
100
101 // Fill tensors
102 fill(AccessorType(src));
103
104 // Compute function
105 smx_layer.run();
106
107 return dst;
108 }
109
Chunosovf450caa2017-11-08 16:09:35 +0700110 SimpleTensor<T> compute_reference(const TensorShape &shape, DataType data_type, int fixed_point_position, QuantizationInfo quantization_info)
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100111 {
112 // Create reference
Chunosovf450caa2017-11-08 16:09:35 +0700113 SimpleTensor<T> src{ shape, data_type, 1, fixed_point_position, quantization_info };
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100114
115 // Fill reference
116 fill(src);
117
118 return reference::softmax_layer<T>(src);
119 }
120
Chunosovf450caa2017-11-08 16:09:35 +0700121 TensorType _target{};
122 SimpleTensor<T> _reference{};
123 int _fractional_bits{};
124 QuantizationInfo _quantization_info{};
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100125};
126
127template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Chunosovf450caa2017-11-08 16:09:35 +0700128class SoftmaxValidationFixture : public SoftmaxValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100129{
130public:
131 template <typename...>
132 void setup(TensorShape shape, DataType data_type)
133 {
Chunosovf450caa2017-11-08 16:09:35 +0700134 SoftmaxValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, 0, QuantizationInfo());
135 }
136};
137
138template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
139class SoftmaxValidationFixedPointFixture : public SoftmaxValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
140{
141public:
142 template <typename...>
143 void setup(TensorShape shape, DataType data_type, int fixed_point_position)
144 {
145 SoftmaxValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, fixed_point_position, QuantizationInfo());
146 }
147};
148
149template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
150class SoftmaxValidationQuantizedFixture : public SoftmaxValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
151{
152public:
153 template <typename...>
154 void setup(TensorShape shape, DataType data_type, QuantizationInfo quantization_info)
155 {
156 SoftmaxValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, 0, quantization_info);
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100157 }
158};
159} // namespace validation
160} // namespace test
161} // namespace arm_compute
162#endif /* ARM_COMPUTE_TEST_SOFTMAX_LAYER_FIXTURE */