blob: 7ad14e1b40bad9737156ee703ed1d10363557e51 [file] [log] [blame]
Giorgio Arena657bdb32018-04-26 18:52:01 +01001/*
Matthew Bentham945b8da2023-07-12 11:54:59 +00002 * Copyright (c) 2018-2023 Arm Limited.
Giorgio Arena657bdb32018-04-26 18:52:01 +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_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:
Giorgio Arena657bdb32018-04-26 18:52:01 +010046 void setup(TensorShape input_shape, unsigned int weights_w, DataLayout training_data_layout, DataType data_type)
47 {
48 const unsigned int height = input_shape.x() * input_shape.y() * input_shape.z();
49 const TensorShape weights_shape(weights_w, height);
50
51 _target = compute_target(input_shape, weights_shape, training_data_layout, data_type);
52 _reference = compute_reference(input_shape, weights_shape, training_data_layout, data_type);
53 }
54
55protected:
56 template <typename U>
57 void fill(U &&tensor, int i)
58 {
59 switch(tensor.data_type())
60 {
61 case DataType::QASYMM8:
62 {
Pablo Tello29cab362022-03-10 17:05:34 +000063 std::uniform_int_distribution<uint32_t> distribution(0, 10);
Giorgio Arena657bdb32018-04-26 18:52:01 +010064 library->fill(tensor, distribution, i);
65 break;
66 }
Giorgio Arena657bdb32018-04-26 18:52:01 +010067 case DataType::F16:
68 {
Giorgio Arenaa8e2aeb2021-01-06 11:34:57 +000069 arm_compute::utils::uniform_real_distribution_16bit<half> distribution{ -1.0f, 1.0f };
Giorgio Arena6aeb2172020-12-15 15:45:43 +000070 library->fill(tensor, distribution, i);
71 break;
72 }
73 case DataType::F32:
74 {
75 std::uniform_real_distribution<float> distribution(-1.0f, 1.0f);
Giorgio Arena657bdb32018-04-26 18:52:01 +010076 library->fill(tensor, distribution, i);
77 break;
78 }
79 default:
80 library->fill_tensor_uniform(tensor, i);
81 }
82 }
83
84 TensorType compute_target(const TensorShape &input_shape, const TensorShape &weights_shape, const DataLayout training_data_layout, const DataType data_type)
85 {
86 // Create tensors
87 TensorType src = create_tensor<TensorType>(weights_shape, data_type);
88 TensorType dst = create_tensor<TensorType>(weights_shape, data_type);
89
90 // Create and configure function
91 FunctionType convert_weights;
92
93 convert_weights.configure(&src, &dst, input_shape, training_data_layout);
94
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +010095 ARM_COMPUTE_ASSERT(src.info()->is_resizable());
96 ARM_COMPUTE_ASSERT(dst.info()->is_resizable());
Giorgio Arena657bdb32018-04-26 18:52:01 +010097
98 // Allocate tensors
99 src.allocator()->allocate();
100 dst.allocator()->allocate();
101
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +0100102 ARM_COMPUTE_ASSERT(!src.info()->is_resizable());
103 ARM_COMPUTE_ASSERT(!dst.info()->is_resizable());
Giorgio Arena657bdb32018-04-26 18:52:01 +0100104
105 // Fill tensors
106 fill(AccessorType(src), 0);
107
108 // Compute function
109 convert_weights.run();
110
111 return dst;
112 }
113
114 SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &weights_shape, const DataLayout training_data_layout, const DataType data_type)
115 {
116 // Create reference
117 SimpleTensor<T> src{ weights_shape, data_type };
118
119 // Fill reference
120 fill(src, 0);
121
122 return reference::convert_fully_connected_weights(src, input_shape, training_data_layout);
123 }
124
125 TensorType _target{};
126 SimpleTensor<T> _reference{};
127};
128} // namespace validation
129} // namespace test
130} // namespace arm_compute
131#endif /* ARM_COMPUTE_TEST_CONVERT_FULLY_CONNECTED_WEIGHTS_FIXTURE */