blob: 82daf34f13693bd68d5c625469e04d9f276ab598 [file] [log] [blame]
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +01001/*
Sang-Hoon Parkd24affe2019-10-08 18:07:23 +01002 * Copyright (c) 2017-2019 ARM Limited.
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +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 */
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"
Sang-Hoon Parkd24affe2019-10-08 18:07:23 +010035#include "tests/validation/reference/LogSoftmaxLayer.h"
Georgios Pinitas5a7e7762017-12-01 16:27:29 +000036#include "tests/validation/reference/SoftmaxLayer.h"
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010037
38#include <random>
39
40namespace arm_compute
41{
42namespace test
43{
44namespace validation
45{
Sang-Hoon Parkd24affe2019-10-08 18:07:23 +010046template <typename TensorType, typename AccessorType, typename FunctionType, typename T, bool IS_LOG = false>
Chunosovf450caa2017-11-08 16:09:35 +070047class SoftmaxValidationGenericFixture : public framework::Fixture
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010048{
49public:
50 template <typename...>
giuros01efbf6c82018-09-03 09:53:53 +010051 void setup(TensorShape shape, DataType data_type, QuantizationInfo quantization_info, float beta, size_t axis)
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010052 {
Chunosovf450caa2017-11-08 16:09:35 +070053 _quantization_info = quantization_info;
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010054
giuros01efbf6c82018-09-03 09:53:53 +010055 _target = compute_target(shape, data_type, quantization_info, beta, axis);
56 _reference = compute_reference(shape, data_type, quantization_info, beta, axis);
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 }
Sang-Hoon Park0779fec2019-11-13 17:08:12 +000068 else // data type is quantized_asymmetric (signed or unsigned)
Chunosovf450caa2017-11-08 16:09:35 +070069 {
70 std::uniform_int_distribution<> distribution(0, 100);
71 library->fill(tensor, distribution, 0);
72 }
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010073 }
74
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010075 TensorType compute_target(const TensorShape &shape, DataType data_type,
giuros01efbf6c82018-09-03 09:53:53 +010076 QuantizationInfo quantization_info, float beta, size_t axis)
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010077 {
78 // Create tensors
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010079 TensorType src = create_tensor<TensorType>(shape, data_type, 1, quantization_info);
Sang-Hoon Park0779fec2019-11-13 17:08:12 +000080 TensorType dst = create_tensor<TensorType>(shape, data_type, 1, get_softmax_output_quantization_info(data_type, IS_LOG));
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010081
82 // Create and configure function
83 FunctionType smx_layer;
giuros01efbf6c82018-09-03 09:53:53 +010084 smx_layer.configure(&src, &dst, beta, axis);
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010085
86 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
87 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
88
89 // Allocate tensors
90 src.allocator()->allocate();
91 dst.allocator()->allocate();
92
93 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
94 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
95
96 // Fill tensors
97 fill(AccessorType(src));
98
99 // Compute function
100 smx_layer.run();
101
102 return dst;
103 }
104
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100105 SimpleTensor<T> compute_reference(const TensorShape &shape, DataType data_type,
giuros01efbf6c82018-09-03 09:53:53 +0100106 QuantizationInfo quantization_info, float beta, size_t axis)
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100107 {
108 // Create reference
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100109 SimpleTensor<T> src{ shape, data_type, 1, quantization_info };
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100110
111 // Fill reference
112 fill(src);
113
Sang-Hoon Parkd24affe2019-10-08 18:07:23 +0100114 if(IS_LOG)
115 {
116 return reference::log_softmax_layer<T>(src, beta, axis);
117 }
118 else
119 {
120 return reference::softmax_layer<T>(src, beta, axis);
121 }
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100122 }
123
Chunosovf450caa2017-11-08 16:09:35 +0700124 TensorType _target{};
125 SimpleTensor<T> _reference{};
Chunosovf450caa2017-11-08 16:09:35 +0700126 QuantizationInfo _quantization_info{};
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100127};
128
Sang-Hoon Parkd24affe2019-10-08 18:07:23 +0100129template <typename TensorType, typename AccessorType, typename FunctionType, typename T, bool IS_LOG = false>
130class SoftmaxValidationFixture : public SoftmaxValidationGenericFixture<TensorType, AccessorType, FunctionType, T, IS_LOG>
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100131{
132public:
133 template <typename...>
giuros01efbf6c82018-09-03 09:53:53 +0100134 void setup(TensorShape shape, DataType data_type, float beta, size_t axis)
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100135 {
Sang-Hoon Parkd24affe2019-10-08 18:07:23 +0100136 SoftmaxValidationGenericFixture<TensorType, AccessorType, FunctionType, T, IS_LOG>::setup(shape,
137 data_type,
138 QuantizationInfo(),
139 beta,
140 axis);
Chunosovf450caa2017-11-08 16:09:35 +0700141 }
142};
143
Sang-Hoon Parkd24affe2019-10-08 18:07:23 +0100144template <typename TensorType, typename AccessorType, typename FunctionType, typename T, bool IS_LOG = false>
145class SoftmaxValidationQuantizedFixture : public SoftmaxValidationGenericFixture<TensorType, AccessorType, FunctionType, T, IS_LOG>
Chunosovf450caa2017-11-08 16:09:35 +0700146{
147public:
148 template <typename...>
giuros01efbf6c82018-09-03 09:53:53 +0100149 void setup(TensorShape shape, DataType data_type, QuantizationInfo quantization_info, float beta, size_t axis)
Chunosovf450caa2017-11-08 16:09:35 +0700150 {
Sang-Hoon Parkd24affe2019-10-08 18:07:23 +0100151 SoftmaxValidationGenericFixture<TensorType, AccessorType, FunctionType, T, IS_LOG>::setup(shape,
152 data_type,
153 quantization_info,
154 beta,
155 axis);
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100156 }
157};
158} // namespace validation
159} // namespace test
160} // namespace arm_compute
161#endif /* ARM_COMPUTE_TEST_SOFTMAX_LAYER_FIXTURE */