blob: 0b3e76d677f03d3e34b448e61475c7f2683c0282 [file] [log] [blame]
Giorgio Arenac6aa49b2018-08-07 11:53:30 +01001/*
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +01002 * Copyright (c) 2018-2021 Arm Limited.
Giorgio Arenac6aa49b2018-08-07 11:53:30 +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_WEIGHTS_RESHAPE_FIXTURE
25#define ARM_COMPUTE_TEST_WEIGHTS_RESHAPE_FIXTURE
26
27#include "arm_compute/core/Helpers.h"
28#include "arm_compute/core/TensorShape.h"
29#include "arm_compute/core/Types.h"
30#include "arm_compute/core/utils/misc/ShapeCalculator.h"
31#include "arm_compute/runtime/Tensor.h"
32#include "tests/AssetsLibrary.h"
33#include "tests/Globals.h"
34#include "tests/IAccessor.h"
35#include "tests/framework/Asserts.h"
36#include "tests/framework/Fixture.h"
37#include "tests/validation/reference/WeightsReshape.h"
38
39namespace arm_compute
40{
41namespace test
42{
43namespace validation
44{
45using namespace arm_compute::misc::shape_calculator;
46
47template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
48class WeightsReshapeValidationFixture : public framework::Fixture
49{
50public:
51 template <typename...>
52 void setup(TensorShape input_shape, DataType data_type, bool has_bias, unsigned int num_groups)
53 {
54 const TensorShape output_shape = compute_weights_reshaped_shape(TensorInfo(input_shape, 1, data_type), has_bias, num_groups);
55
56 _target = compute_target(input_shape, output_shape, has_bias, num_groups, data_type);
57 _reference = compute_reference(input_shape, output_shape, has_bias, num_groups, data_type);
58 }
59
60protected:
61 template <typename U>
62 void fill(U &&tensor, const int seed)
63 {
64 library->fill_tensor_uniform(tensor, seed);
65 }
66
67 TensorType compute_target(const TensorShape &input_shape, const TensorShape &output_shape, const bool has_bias, const unsigned int num_groups, DataType data_type)
68 {
69 // Create tensors
70 TensorType src = create_tensor<TensorType>(input_shape, data_type);
71 TensorType bias = create_tensor<TensorType>(TensorShape(input_shape[3]), data_type);
72 TensorType dst = create_tensor<TensorType>(output_shape, data_type);
73
74 // Create and configure function
75 FunctionType weights_reshape_func;
76 weights_reshape_func.configure(&src, (has_bias ? &bias : nullptr), &dst, num_groups);
77
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +010078 ARM_COMPUTE_ASSERT(src.info()->is_resizable());
79 ARM_COMPUTE_ASSERT(dst.info()->is_resizable());
Giorgio Arenac6aa49b2018-08-07 11:53:30 +010080
81 // Allocate tensors
82 src.allocator()->allocate();
83 dst.allocator()->allocate();
84
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +010085 ARM_COMPUTE_ASSERT(!src.info()->is_resizable());
86 ARM_COMPUTE_ASSERT(!dst.info()->is_resizable());
Giorgio Arenac6aa49b2018-08-07 11:53:30 +010087
88 // Fill tensors
89 fill(AccessorType(src), 0);
90
91 if(has_bias)
92 {
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +010093 ARM_COMPUTE_ASSERT(bias.info()->is_resizable());
Giorgio Arenac6aa49b2018-08-07 11:53:30 +010094
95 bias.allocator()->allocate();
96
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +010097 ARM_COMPUTE_ASSERT(!bias.info()->is_resizable());
Giorgio Arenac6aa49b2018-08-07 11:53:30 +010098
99 fill(AccessorType(bias), 1);
100 }
101
102 // Compute function
103 weights_reshape_func.run();
104
105 return dst;
106 }
107
108 SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &output_shape, const bool has_bias, const unsigned int num_groups, DataType data_type)
109 {
110 // Create reference
111 SimpleTensor<T> src{ input_shape, data_type };
112 SimpleTensor<T> bias{ TensorShape(has_bias ? input_shape[3] : 0), data_type };
113
114 // Fill reference
115 fill(src, 0);
116 if(has_bias)
117 {
118 fill(bias, 1);
119 }
120
121 return reference::weights_reshape(src, bias, output_shape, num_groups);
122 }
123
124 TensorType _target{};
125 SimpleTensor<T> _reference{};
126};
127} // namespace validation
128} // namespace test
129} // namespace arm_compute
130#endif /* ARM_COMPUTE_TEST_WEIGHTS_RESHAPE_FIXTURE */