blob: 68bd8b689d81c999c29fc34e6255db266b9eda63 [file] [log] [blame]
Giorgio Arenac6aa49b2018-08-07 11:53:30 +01001/*
Matthew Bentham945b8da2023-07-12 11:54:59 +00002 * Copyright (c) 2018-2021, 2023 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>
Manuel Bottinid87aded2021-07-16 10:23:31 +010048class WeightsReshapeOpValidationFixture : public framework::Fixture
Giorgio Arenac6aa49b2018-08-07 11:53:30 +010049{
50public:
Giorgio Arenac6aa49b2018-08-07 11:53:30 +010051 void setup(TensorShape input_shape, DataType data_type, bool has_bias, unsigned int num_groups)
52 {
53 const TensorShape output_shape = compute_weights_reshaped_shape(TensorInfo(input_shape, 1, data_type), has_bias, num_groups);
54
55 _target = compute_target(input_shape, output_shape, has_bias, num_groups, data_type);
56 _reference = compute_reference(input_shape, output_shape, has_bias, num_groups, data_type);
57 }
58
59protected:
60 template <typename U>
61 void fill(U &&tensor, const int seed)
62 {
63 library->fill_tensor_uniform(tensor, seed);
64 }
65
66 TensorType compute_target(const TensorShape &input_shape, const TensorShape &output_shape, const bool has_bias, const unsigned int num_groups, DataType data_type)
67 {
68 // Create tensors
69 TensorType src = create_tensor<TensorType>(input_shape, data_type);
70 TensorType bias = create_tensor<TensorType>(TensorShape(input_shape[3]), data_type);
71 TensorType dst = create_tensor<TensorType>(output_shape, data_type);
72
73 // Create and configure function
74 FunctionType weights_reshape_func;
Manuel Bottinid87aded2021-07-16 10:23:31 +010075 weights_reshape_func.configure(src.info(), (has_bias ? bias.info() : nullptr), dst.info(), num_groups);
Giorgio Arenac6aa49b2018-08-07 11:53:30 +010076
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +010077 ARM_COMPUTE_ASSERT(src.info()->is_resizable());
78 ARM_COMPUTE_ASSERT(dst.info()->is_resizable());
Giorgio Arenac6aa49b2018-08-07 11:53:30 +010079
80 // Allocate tensors
81 src.allocator()->allocate();
82 dst.allocator()->allocate();
83
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +010084 ARM_COMPUTE_ASSERT(!src.info()->is_resizable());
85 ARM_COMPUTE_ASSERT(!dst.info()->is_resizable());
Giorgio Arenac6aa49b2018-08-07 11:53:30 +010086
87 // Fill tensors
88 fill(AccessorType(src), 0);
89
90 if(has_bias)
91 {
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +010092 ARM_COMPUTE_ASSERT(bias.info()->is_resizable());
Giorgio Arenac6aa49b2018-08-07 11:53:30 +010093
94 bias.allocator()->allocate();
95
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +010096 ARM_COMPUTE_ASSERT(!bias.info()->is_resizable());
Giorgio Arenac6aa49b2018-08-07 11:53:30 +010097
98 fill(AccessorType(bias), 1);
99 }
100
Manuel Bottinid87aded2021-07-16 10:23:31 +0100101 arm_compute::ITensorPack pack =
102 {
103 { arm_compute::TensorType::ACL_SRC, &src },
104 { arm_compute::TensorType::ACL_DST, &dst }
105 };
106
107 if(has_bias)
108 {
109 pack.add_const_tensor(arm_compute::TensorType::ACL_BIAS, &bias);
110 }
Giorgio Arenac6aa49b2018-08-07 11:53:30 +0100111 // Compute function
Manuel Bottinid87aded2021-07-16 10:23:31 +0100112 weights_reshape_func.run(pack);
Giorgio Arenac6aa49b2018-08-07 11:53:30 +0100113
114 return dst;
115 }
116
117 SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &output_shape, const bool has_bias, const unsigned int num_groups, DataType data_type)
118 {
119 // Create reference
120 SimpleTensor<T> src{ input_shape, data_type };
121 SimpleTensor<T> bias{ TensorShape(has_bias ? input_shape[3] : 0), data_type };
122
123 // Fill reference
124 fill(src, 0);
125 if(has_bias)
126 {
127 fill(bias, 1);
128 }
129
130 return reference::weights_reshape(src, bias, output_shape, num_groups);
131 }
132
133 TensorType _target{};
134 SimpleTensor<T> _reference{};
135};
136} // namespace validation
137} // namespace test
138} // namespace arm_compute
139#endif /* ARM_COMPUTE_TEST_WEIGHTS_RESHAPE_FIXTURE */