blob: d29d67c8e66a9fd638b5e54d2acdd25fc936cc46 [file] [log] [blame]
Moritz Pflanzer572ade72017-07-21 17:36:33 +01001/*
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +01002 * Copyright (c) 2017-2018 ARM Limited.
Moritz Pflanzer572ade72017-07-21 17:36:33 +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_ACTIVATION_LAYER_FIXTURE
25#define ARM_COMPUTE_TEST_ACTIVATION_LAYER_FIXTURE
26
27#include "arm_compute/core/TensorShape.h"
28#include "arm_compute/core/Types.h"
Moritz Pflanzer572ade72017-07-21 17:36:33 +010029#include "tests/AssetsLibrary.h"
30#include "tests/Globals.h"
31#include "tests/IAccessor.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010032#include "tests/framework/Asserts.h"
33#include "tests/framework/Fixture.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010034#include "tests/validation/Helpers.h"
Georgios Pinitas5a7e7762017-12-01 16:27:29 +000035#include "tests/validation/reference/ActivationLayer.h"
Moritz Pflanzer572ade72017-07-21 17:36:33 +010036
37#include <random>
38
39namespace arm_compute
40{
41namespace test
42{
43namespace validation
44{
45template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Michel Iwaniec66cc12f2017-12-07 17:26:40 +000046class ActivationValidationGenericFixture : public framework::Fixture
Moritz Pflanzer572ade72017-07-21 17:36:33 +010047{
48public:
49 template <typename...>
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010050 void setup(TensorShape shape, bool in_place, ActivationLayerInfo::ActivationFunction function, float alpha_beta, DataType data_type, QuantizationInfo quantization_info)
Moritz Pflanzer572ade72017-07-21 17:36:33 +010051 {
Michel Iwaniec66cc12f2017-12-07 17:26:40 +000052 _quantization_info = quantization_info;
53 _data_type = data_type;
54 _function = function;
Moritz Pflanzer572ade72017-07-21 17:36:33 +010055
56 ActivationLayerInfo info(function, alpha_beta, alpha_beta);
57
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010058 _target = compute_target(shape, in_place, info, data_type, quantization_info);
59 _reference = compute_reference(shape, info, data_type, quantization_info);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010060 }
61
62protected:
63 template <typename U>
64 void fill(U &&tensor)
65 {
66 if(is_data_type_float(_data_type))
67 {
68 float min_bound = 0;
69 float max_bound = 0;
70 std::tie(min_bound, max_bound) = get_activation_layer_test_bounds<T>(_function, _data_type);
71 std::uniform_real_distribution<> distribution(min_bound, max_bound);
72 library->fill(tensor, distribution, 0);
73 }
Michel Iwaniec66cc12f2017-12-07 17:26:40 +000074 else if(is_data_type_quantized_asymmetric(tensor.data_type()))
75 {
76 library->fill_tensor_uniform(tensor, 0);
77 }
Moritz Pflanzer572ade72017-07-21 17:36:33 +010078 else
79 {
80 int min_bound = 0;
81 int max_bound = 0;
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010082 std::tie(min_bound, max_bound) = get_activation_layer_test_bounds<T>(_function, _data_type);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010083 std::uniform_int_distribution<> distribution(min_bound, max_bound);
84 library->fill(tensor, distribution, 0);
85 }
86 }
87
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010088 TensorType compute_target(const TensorShape &shape, bool in_place, ActivationLayerInfo info, DataType data_type, QuantizationInfo quantization_info)
Moritz Pflanzer572ade72017-07-21 17:36:33 +010089 {
90 // Create tensors
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010091 TensorType src = create_tensor<TensorType>(shape, data_type, 1, quantization_info);
92 TensorType dst = create_tensor<TensorType>(shape, data_type, 1, quantization_info);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010093
94 // Create and configure function
95 FunctionType act_layer;
96
97 TensorType *dst_ptr = in_place ? &src : &dst;
98
99 act_layer.configure(&src, dst_ptr, info);
100
101 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
102 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
103
104 // Allocate tensors
105 src.allocator()->allocate();
106 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
107
108 if(!in_place)
109 {
110 dst.allocator()->allocate();
111 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
112 }
113
114 // Fill tensors
115 fill(AccessorType(src));
116
117 // Compute function
118 act_layer.run();
119
120 if(in_place)
121 {
122 return src;
123 }
124 else
125 {
126 return dst;
127 }
128 }
129
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100130 SimpleTensor<T> compute_reference(const TensorShape &shape, ActivationLayerInfo info, DataType data_type, QuantizationInfo quantization_info)
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100131 {
132 // Create reference
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100133 SimpleTensor<T> src{ shape, data_type, 1, quantization_info };
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100134
135 // Fill reference
136 fill(src);
137
138 return reference::activation_layer<T>(src, info);
139 }
140
141 TensorType _target{};
142 SimpleTensor<T> _reference{};
Michel Iwaniec66cc12f2017-12-07 17:26:40 +0000143 QuantizationInfo _quantization_info{};
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100144 DataType _data_type{};
145 ActivationLayerInfo::ActivationFunction _function{};
146};
147
148template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Michel Iwaniec66cc12f2017-12-07 17:26:40 +0000149class ActivationValidationFixture : public ActivationValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100150{
151public:
152 template <typename...>
153 void setup(TensorShape shape, bool in_place, ActivationLayerInfo::ActivationFunction function, float alpha_beta, DataType data_type)
154 {
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100155 ActivationValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, in_place, function, alpha_beta, data_type, QuantizationInfo());
Michel Iwaniec66cc12f2017-12-07 17:26:40 +0000156 }
157};
158
159template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
160class ActivationValidationQuantizedFixture : public ActivationValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
161{
162public:
163 template <typename...>
164 void setup(TensorShape shape, bool in_place, ActivationLayerInfo::ActivationFunction function, float alpha_beta, DataType data_type, QuantizationInfo quantization_info)
165 {
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100166 ActivationValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, in_place, function, alpha_beta, data_type, quantization_info);
Michel Iwaniec66cc12f2017-12-07 17:26:40 +0000167 }
168};
169
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100170} // namespace validation
171} // namespace test
172} // namespace arm_compute
173#endif /* ARM_COMPUTE_TEST_ACTIVATION_LAYER_FIXTURE */