blob: ab9819e56f0e64a97faf5ffc8d66312233c3a282 [file] [log] [blame]
Sanghoon Leef47bfb92018-01-23 15:16:47 +00001/*
2 * Copyright (c) 2017-2018 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_LOCALLY_CONNECTED_FIXTURE
25#define ARM_COMPUTE_TEST_LOCALLY_CONNECTED_FIXTURE
26
27#include "arm_compute/core/TensorShape.h"
28#include "arm_compute/core/Types.h"
29#include "arm_compute/runtime/NEON/NEScheduler.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/Helpers.h"
36#include "tests/validation/reference/LocallyConnected.h"
37#include "tests/validation/reference/Utils.h"
38
39#include <random>
40
41namespace arm_compute
42{
43class NELocallyConnected;
44
45namespace test
46{
47namespace validation
48{
49template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
50class LocallyConnectedValidationFixture : public framework::Fixture
51{
52public:
53 using TBias = typename std::conditional<std::is_same<typename std::decay<T>::type, uint8_t>::value, int32_t, T>::type;
54
55public:
56 template <typename...>
57 void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, PadStrideInfo info, DataType data_type)
58 {
59 _data_type = data_type;
60 _bias_data_type = data_type;
61
62 _target = compute_target(input_shape, weights_shape, bias_shape, output_shape, info);
63 _reference = compute_reference(input_shape, weights_shape, bias_shape, output_shape, info);
64 }
65
66protected:
67 template <typename U>
68 void fill(U &&tensor, int i)
69 {
70 std::uniform_real_distribution<> distribution(-1.0f, 1.0f);
71 library->fill(tensor, distribution, i);
72 }
73
74 TensorType compute_target(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape, const PadStrideInfo &info)
75 {
76 TensorShape reshaped_weights_shape(weights_shape);
77
78 // Create tensors
79 TensorType src = create_tensor<TensorType>(input_shape, _data_type);
80 TensorType weights = create_tensor<TensorType>(reshaped_weights_shape, _data_type);
81 TensorType bias = create_tensor<TensorType>(bias_shape, _bias_data_type);
82 TensorType dst = create_tensor<TensorType>(output_shape, _data_type);
83
84 // Create and configure function
85 FunctionType locally_connected;
86 locally_connected.configure(&src, &weights, &bias, &dst, info);
87
88 // Allocate tensors
89 src.allocator()->allocate();
90 weights.allocator()->allocate();
91 bias.allocator()->allocate();
92 dst.allocator()->allocate();
93
94 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
95 ARM_COMPUTE_EXPECT(!weights.info()->is_resizable(), framework::LogLevel::ERRORS);
96 ARM_COMPUTE_EXPECT(!bias.info()->is_resizable(), framework::LogLevel::ERRORS);
97 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
98
99 // Fill tensors
100 fill(AccessorType(src), 0);
101 fill(AccessorType(weights), 1);
102 fill(AccessorType(bias), 2);
103
104 locally_connected.run();
105
106 return dst;
107 }
108
109 SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape, const PadStrideInfo &info)
110 {
111 // Create reference
112 SimpleTensor<T> src(input_shape, _data_type);
113 SimpleTensor<T> weights(weights_shape, _data_type);
114 SimpleTensor<TBias> bias(bias_shape, _bias_data_type);
115
116 // Fill reference
117 fill(src, 0);
118 fill(weights, 1);
119 fill(bias, 2);
120
121 return reference::locally_connected<T>(src, weights, bias, output_shape, info);
122 }
123
124 TensorType _target{};
125 SimpleTensor<T> _reference{};
126 DataType _data_type{};
127 DataType _bias_data_type{};
128};
129
130} // namespace validation
131} // namespace test
132} // namespace arm_compute
133#endif /* ARM_COMPUTE_TEST_LOCALLY_CONNECTED_FIXTURE */