blob: 384e63bf494fcd9b4eeaa29e3df7bc557e35c1ab [file] [log] [blame]
Moritz Pflanzer572ade72017-07-21 17:36:33 +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_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"
34#include "tests/validation/CPP/ActivationLayer.h"
35#include "tests/validation/Helpers.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>
46class ActivationValidationFixedPointFixture : public framework::Fixture
47{
48public:
49 template <typename...>
50 void setup(TensorShape shape, bool in_place, ActivationLayerInfo::ActivationFunction function, float alpha_beta, DataType data_type, int fractional_bits)
51 {
52 _fractional_bits = fractional_bits;
53 _data_type = data_type;
54 _function = function;
55
56 ActivationLayerInfo info(function, alpha_beta, alpha_beta);
57
58 _target = compute_target(shape, in_place, info, data_type, fractional_bits);
59 _reference = compute_reference(shape, info, data_type, fractional_bits);
60 }
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 }
74 else
75 {
76 int min_bound = 0;
77 int max_bound = 0;
78 std::tie(min_bound, max_bound) = get_activation_layer_test_bounds<T>(_function, _data_type, _fractional_bits);
79 std::uniform_int_distribution<> distribution(min_bound, max_bound);
80 library->fill(tensor, distribution, 0);
81 }
82 }
83
84 TensorType compute_target(const TensorShape &shape, bool in_place, ActivationLayerInfo info, DataType data_type, int fixed_point_position = 0)
85 {
86 // Create tensors
87 TensorType src = create_tensor<TensorType>(shape, data_type, 1, fixed_point_position);
88 TensorType dst = create_tensor<TensorType>(shape, data_type, 1, fixed_point_position);
89
90 // Create and configure function
91 FunctionType act_layer;
92
93 TensorType *dst_ptr = in_place ? &src : &dst;
94
95 act_layer.configure(&src, dst_ptr, info);
96
97 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
98 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
99
100 // Allocate tensors
101 src.allocator()->allocate();
102 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
103
104 if(!in_place)
105 {
106 dst.allocator()->allocate();
107 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
108 }
109
110 // Fill tensors
111 fill(AccessorType(src));
112
113 // Compute function
114 act_layer.run();
115
116 if(in_place)
117 {
118 return src;
119 }
120 else
121 {
122 return dst;
123 }
124 }
125
126 SimpleTensor<T> compute_reference(const TensorShape &shape, ActivationLayerInfo info, DataType data_type, int fixed_point_position = 0)
127 {
128 // Create reference
129 SimpleTensor<T> src{ shape, data_type, 1, fixed_point_position };
130
131 // Fill reference
132 fill(src);
133
134 return reference::activation_layer<T>(src, info);
135 }
136
137 TensorType _target{};
138 SimpleTensor<T> _reference{};
139 int _fractional_bits{};
140 DataType _data_type{};
141 ActivationLayerInfo::ActivationFunction _function{};
142};
143
144template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
145class ActivationValidationFixture : public ActivationValidationFixedPointFixture<TensorType, AccessorType, FunctionType, T>
146{
147public:
148 template <typename...>
149 void setup(TensorShape shape, bool in_place, ActivationLayerInfo::ActivationFunction function, float alpha_beta, DataType data_type)
150 {
151 ActivationValidationFixedPointFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, in_place, function, alpha_beta, data_type, 0);
152 }
153};
154} // namespace validation
155} // namespace test
156} // namespace arm_compute
157#endif /* ARM_COMPUTE_TEST_ACTIVATION_LAYER_FIXTURE */