blob: 0fcef5c31f6c8063d474680fb5b0101e94fe63cf [file] [log] [blame]
Giorgio Arena657bdb32018-04-26 18:52:01 +01001/*
2 * Copyright (c) 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_CONVERT_FULLY_CONNECTED_WEIGHTS_FIXTURE
25#define ARM_COMPUTE_TEST_CONVERT_FULLY_CONNECTED_WEIGHTS_FIXTURE
26
27#include "arm_compute/core/TensorShape.h"
28#include "arm_compute/core/Types.h"
29#include "tests/AssetsLibrary.h"
30#include "tests/Globals.h"
31#include "tests/IAccessor.h"
32#include "tests/framework/Asserts.h"
33#include "tests/framework/Fixture.h"
34#include "tests/validation/reference/ConvertFullyConnectedWeights.h"
35
36namespace arm_compute
37{
38namespace test
39{
40namespace validation
41{
42template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
43class ConvertFullyConnectedWeightsValidationFixture : public framework::Fixture
44{
45public:
46 template <typename...>
47 void setup(TensorShape input_shape, unsigned int weights_w, DataLayout training_data_layout, DataType data_type)
48 {
49 const unsigned int height = input_shape.x() * input_shape.y() * input_shape.z();
50 const TensorShape weights_shape(weights_w, height);
51
52 _target = compute_target(input_shape, weights_shape, training_data_layout, data_type);
53 _reference = compute_reference(input_shape, weights_shape, training_data_layout, data_type);
54 }
55
56protected:
57 template <typename U>
58 void fill(U &&tensor, int i)
59 {
60 switch(tensor.data_type())
61 {
62 case DataType::QASYMM8:
63 {
64 std::uniform_int_distribution<uint8_t> distribution(0, 10);
65 library->fill(tensor, distribution, i);
66 break;
67 }
68 case DataType::F32:
69 case DataType::F16:
70 {
71 std::uniform_real_distribution<> distribution(-1.0f, 1.0f);
72 library->fill(tensor, distribution, i);
73 break;
74 }
75 default:
76 library->fill_tensor_uniform(tensor, i);
77 }
78 }
79
80 TensorType compute_target(const TensorShape &input_shape, const TensorShape &weights_shape, const DataLayout training_data_layout, const DataType data_type)
81 {
82 // Create tensors
83 TensorType src = create_tensor<TensorType>(weights_shape, data_type);
84 TensorType dst = create_tensor<TensorType>(weights_shape, data_type);
85
86 // Create and configure function
87 FunctionType convert_weights;
88
89 convert_weights.configure(&src, &dst, input_shape, training_data_layout);
90
91 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
92 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
93
94 // Allocate tensors
95 src.allocator()->allocate();
96 dst.allocator()->allocate();
97
98 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
99 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
100
101 // Fill tensors
102 fill(AccessorType(src), 0);
103
104 // Compute function
105 convert_weights.run();
106
107 return dst;
108 }
109
110 SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &weights_shape, const DataLayout training_data_layout, const DataType data_type)
111 {
112 // Create reference
113 SimpleTensor<T> src{ weights_shape, data_type };
114
115 // Fill reference
116 fill(src, 0);
117
118 return reference::convert_fully_connected_weights(src, input_shape, training_data_layout);
119 }
120
121 TensorType _target{};
122 SimpleTensor<T> _reference{};
123};
124} // namespace validation
125} // namespace test
126} // namespace arm_compute
127#endif /* ARM_COMPUTE_TEST_CONVERT_FULLY_CONNECTED_WEIGHTS_FIXTURE */