xref: /aosp_15_r20/external/ComputeLibrary/tests/validation/fixtures/SoftmaxLayerFixture.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2017-2021 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"
30 #include "tests/AssetsLibrary.h"
31 #include "tests/Globals.h"
32 #include "tests/IAccessor.h"
33 #include "tests/framework/Asserts.h"
34 #include "tests/framework/Fixture.h"
35 #include "tests/validation/reference/SoftmaxLayer.h"
36 
37 #include <random>
38 
39 namespace arm_compute
40 {
41 namespace test
42 {
43 namespace validation
44 {
45 template <typename TensorType, typename AccessorType, typename FunctionType, typename T, bool IS_LOG = false>
46 class SoftmaxValidationGenericFixture : public framework::Fixture
47 {
48 public:
49     template <typename...>
setup(TensorShape shape,DataType data_type,QuantizationInfo quantization_info,float beta,size_t axis)50     void setup(TensorShape shape, DataType data_type, QuantizationInfo quantization_info, float beta, size_t axis)
51     {
52         _quantization_info = quantization_info;
53 
54         _reference = compute_reference(shape, data_type, quantization_info, beta, axis);
55         _target    = compute_target(shape, data_type, quantization_info, beta, axis);
56     }
57 
58 protected:
59     template <typename U>
fill(U && tensor)60     void fill(U &&tensor)
61     {
62         if(tensor.data_type() == DataType::F32)
63         {
64             std::uniform_real_distribution<float> distribution(-10.0f, 10.0f);
65             library->fill(tensor, distribution, 0);
66         }
67         else if(tensor.data_type() == DataType::F16)
68         {
69             arm_compute::utils::uniform_real_distribution_16bit<half> distribution{ -10.0f, 10.0f };
70             library->fill(tensor, distribution, 0);
71         }
72         else if(!is_data_type_quantized(tensor.data_type()))
73         {
74             std::uniform_int_distribution<> distribution(0, 100);
75             library->fill(tensor, distribution, 0);
76         }
77         else
78         {
79             library->fill_tensor_uniform(tensor, 0);
80         }
81     }
82 
compute_target(const TensorShape & shape,DataType data_type,QuantizationInfo quantization_info,float beta,int32_t axis)83     TensorType compute_target(const TensorShape &shape, DataType data_type,
84                               QuantizationInfo quantization_info, float beta, int32_t axis)
85     {
86         // Create tensors
87         TensorType src = create_tensor<TensorType>(shape, data_type, 1, quantization_info);
88         TensorType dst = create_tensor<TensorType>(shape, data_type, 1, get_softmax_output_quantization_info(data_type, IS_LOG));
89 
90         // Create and configure function
91         FunctionType smx_layer;
92         smx_layer.configure(&src, &dst, beta, axis);
93 
94         ARM_COMPUTE_ASSERT(src.info()->is_resizable());
95         ARM_COMPUTE_ASSERT(dst.info()->is_resizable());
96 
97         // Allocate tensors
98         src.allocator()->allocate();
99         dst.allocator()->allocate();
100 
101         ARM_COMPUTE_ASSERT(!src.info()->is_resizable());
102         ARM_COMPUTE_ASSERT(!dst.info()->is_resizable());
103 
104         // Fill tensors
105         fill(AccessorType(src));
106 
107         // Compute function
108         smx_layer.run();
109 
110         return dst;
111     }
112 
compute_reference(const TensorShape & shape,DataType data_type,QuantizationInfo quantization_info,float beta,int32_t axis)113     SimpleTensor<T> compute_reference(const TensorShape &shape, DataType data_type,
114                                       QuantizationInfo quantization_info, float beta, int32_t axis)
115     {
116         // Create reference
117         SimpleTensor<T> src{ shape, data_type, 1, quantization_info };
118 
119         // Fill reference
120         fill(src);
121 
122         return reference::softmax_layer<T>(src, beta, axis, IS_LOG);
123     }
124 
125     TensorType       _target{};
126     SimpleTensor<T>  _reference{};
127     QuantizationInfo _quantization_info{};
128 };
129 
130 template <typename TensorType, typename AccessorType, typename FunctionType, typename T, bool IS_LOG = false>
131 class SoftmaxValidationFixture : public SoftmaxValidationGenericFixture<TensorType, AccessorType, FunctionType, T, IS_LOG>
132 {
133 public:
134     template <typename...>
setup(TensorShape shape,DataType data_type,float beta,size_t axis)135     void setup(TensorShape shape, DataType data_type, float beta, size_t axis)
136     {
137         SoftmaxValidationGenericFixture<TensorType, AccessorType, FunctionType, T, IS_LOG>::setup(shape,
138                                                                                                   data_type,
139                                                                                                   QuantizationInfo(),
140                                                                                                   beta,
141                                                                                                   axis);
142     }
143 };
144 
145 template <typename TensorType, typename AccessorType, typename FunctionType, typename T, bool IS_LOG = false>
146 class SoftmaxValidationQuantizedFixture : public SoftmaxValidationGenericFixture<TensorType, AccessorType, FunctionType, T, IS_LOG>
147 {
148 public:
149     template <typename...>
setup(TensorShape shape,DataType data_type,QuantizationInfo quantization_info,float beta,size_t axis)150     void setup(TensorShape shape, DataType data_type, QuantizationInfo quantization_info, float beta, size_t axis)
151     {
152         SoftmaxValidationGenericFixture<TensorType, AccessorType, FunctionType, T, IS_LOG>::setup(shape,
153                                                                                                   data_type,
154                                                                                                   quantization_info,
155                                                                                                   beta,
156                                                                                                   axis);
157     }
158 };
159 
160 } // namespace validation
161 } // namespace test
162 } // namespace arm_compute
163 #endif /* ARM_COMPUTE_TEST_SOFTMAX_LAYER_FIXTURE */
164