blob: e39ee748005bb6128bf1963ead8974a4b25e88f4 [file] [log] [blame]
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +01001/*
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +01002 * Copyright (c) 2017-2018 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"
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...>
giuros01efbf6c82018-09-03 09:53:53 +010050 void setup(TensorShape shape, DataType data_type, QuantizationInfo quantization_info, float beta, size_t axis)
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010051 {
Chunosovf450caa2017-11-08 16:09:35 +070052 _quantization_info = quantization_info;
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010053
giuros01efbf6c82018-09-03 09:53:53 +010054 _target = compute_target(shape, data_type, quantization_info, beta, axis);
55 _reference = compute_reference(shape, data_type, quantization_info, beta, axis);
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010056 }
57
58protected:
59 template <typename U>
60 void fill(U &&tensor)
61 {
Chunosovf450caa2017-11-08 16:09:35 +070062 if(!is_data_type_quantized(tensor.data_type()))
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010063 {
64 std::uniform_real_distribution<> distribution(-1000.f, 1000.f);
65 library->fill(tensor, distribution, 0);
66 }
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010067 else // data type is quantized_asymmetric
Chunosovf450caa2017-11-08 16:09:35 +070068 {
69 std::uniform_int_distribution<> distribution(0, 100);
70 library->fill(tensor, distribution, 0);
71 }
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010072 }
73
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010074 TensorType compute_target(const TensorShape &shape, DataType data_type,
giuros01efbf6c82018-09-03 09:53:53 +010075 QuantizationInfo quantization_info, float beta, size_t axis)
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010076 {
77 // Create tensors
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010078 TensorType src = create_tensor<TensorType>(shape, data_type, 1, quantization_info);
79 TensorType dst = create_tensor<TensorType>(shape, data_type, 1, QuantizationInfo(1.f / 256, 0));
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010080
81 // Create and configure function
82 FunctionType smx_layer;
giuros01efbf6c82018-09-03 09:53:53 +010083 smx_layer.configure(&src, &dst, beta, axis);
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010084
85 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
86 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
87
88 // Allocate tensors
89 src.allocator()->allocate();
90 dst.allocator()->allocate();
91
92 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
93 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
94
95 // Fill tensors
96 fill(AccessorType(src));
97
98 // Compute function
99 smx_layer.run();
100
101 return dst;
102 }
103
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100104 SimpleTensor<T> compute_reference(const TensorShape &shape, DataType data_type,
giuros01efbf6c82018-09-03 09:53:53 +0100105 QuantizationInfo quantization_info, float beta, size_t axis)
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100106 {
107 // Create reference
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100108 SimpleTensor<T> src{ shape, data_type, 1, quantization_info };
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100109
110 // Fill reference
111 fill(src);
112
giuros01efbf6c82018-09-03 09:53:53 +0100113 return reference::softmax_layer<T>(src, beta, axis);
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100114 }
115
Chunosovf450caa2017-11-08 16:09:35 +0700116 TensorType _target{};
117 SimpleTensor<T> _reference{};
Chunosovf450caa2017-11-08 16:09:35 +0700118 QuantizationInfo _quantization_info{};
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100119};
120
121template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Chunosovf450caa2017-11-08 16:09:35 +0700122class SoftmaxValidationFixture : public SoftmaxValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100123{
124public:
125 template <typename...>
giuros01efbf6c82018-09-03 09:53:53 +0100126 void setup(TensorShape shape, DataType data_type, float beta, size_t axis)
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100127 {
Pablo Palmiera2b89ca2017-10-05 15:01:34 +0100128 SoftmaxValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape,
129 data_type,
Pablo Palmiera2b89ca2017-10-05 15:01:34 +0100130 QuantizationInfo(),
giuros01efbf6c82018-09-03 09:53:53 +0100131 beta,
132 axis);
Chunosovf450caa2017-11-08 16:09:35 +0700133 }
134};
135
136template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Chunosovf450caa2017-11-08 16:09:35 +0700137class SoftmaxValidationQuantizedFixture : public SoftmaxValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
138{
139public:
140 template <typename...>
giuros01efbf6c82018-09-03 09:53:53 +0100141 void setup(TensorShape shape, DataType data_type, QuantizationInfo quantization_info, float beta, size_t axis)
Chunosovf450caa2017-11-08 16:09:35 +0700142 {
Pablo Palmiera2b89ca2017-10-05 15:01:34 +0100143 SoftmaxValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape,
144 data_type,
Pablo Palmiera2b89ca2017-10-05 15:01:34 +0100145 quantization_info,
giuros01efbf6c82018-09-03 09:53:53 +0100146 beta,
147 axis);
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100148 }
149};
150} // namespace validation
151} // namespace test
152} // namespace arm_compute
153#endif /* ARM_COMPUTE_TEST_SOFTMAX_LAYER_FIXTURE */