xref: /aosp_15_r20/external/ComputeLibrary/tests/validation/fixtures/SpaceToDepthFixture.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2019-2021 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_SPACE_TO_DEPTH_LAYER_FIXTURE
25 #define ARM_COMPUTE_TEST_SPACE_TO_DEPTH_LAYER_FIXTURE
26 
27 #include "arm_compute/core/utils/misc/ShapeCalculator.h"
28 #include "tests/Globals.h"
29 #include "tests/framework/Asserts.h"
30 #include "tests/framework/Fixture.h"
31 #include "tests/validation/reference/SpaceToDepth.h"
32 
33 namespace arm_compute
34 {
35 namespace test
36 {
37 namespace validation
38 {
39 template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
40 class SpaceToDepthLayerValidationFixture : public framework::Fixture
41 {
42 public:
43     template <typename...>
setup(TensorShape input_shape,TensorShape output_shape,const int block_shape,DataType data_type,DataLayout data_layout)44     void setup(TensorShape input_shape, TensorShape output_shape, const int block_shape, DataType data_type, DataLayout data_layout)
45     {
46         _target    = compute_target(input_shape, output_shape, block_shape, data_type, data_layout);
47         _reference = compute_reference(input_shape, output_shape, block_shape, data_type);
48     }
49 
50 protected:
51     template <typename U>
fill(U && tensor,int i)52     void fill(U &&tensor, int i)
53     {
54         static_assert(std::is_floating_point<T>::value || std::is_same<T, half>::value, "Only floating point data types supported.");
55         using DistributionType = typename std::conditional<std::is_same<T, half>::value, arm_compute::utils::uniform_real_distribution_16bit<T>, std::uniform_real_distribution<T>>::type;
56 
57         DistributionType distribution{ T(-1.0f), T(1.0f) };
58         library->fill(tensor, distribution, i);
59     }
compute_target(TensorShape input_shape,TensorShape output_shape,const int block_shape,DataType data_type,DataLayout data_layout)60     TensorType compute_target(TensorShape input_shape, TensorShape output_shape, const int block_shape,
61                               DataType data_type, DataLayout data_layout)
62     {
63         if(data_layout == DataLayout::NHWC)
64         {
65             permute(input_shape, PermutationVector(2U, 0U, 1U));
66             permute(output_shape, PermutationVector(2U, 0U, 1U));
67         }
68 
69         // Create tensors
70         TensorType input  = create_tensor<TensorType>(input_shape, data_type, 1, QuantizationInfo(), data_layout);
71         TensorType output = create_tensor<TensorType>(output_shape, data_type, 1, QuantizationInfo(), data_layout);
72 
73         auto calc_out_shape = misc::shape_calculator::compute_space_to_depth_shape(input.info(), block_shape);
74         ARM_COMPUTE_ASSERT(output_shape[0] == calc_out_shape[0]);
75         ARM_COMPUTE_ASSERT(output_shape[1] == calc_out_shape[1]);
76         ARM_COMPUTE_ASSERT(output_shape[2] == calc_out_shape[2]);
77         ARM_COMPUTE_ASSERT(output_shape[3] == calc_out_shape[3]);
78 
79         // Create and configure function
80         FunctionType space_to_depth;
81         space_to_depth.configure(&input, &output, block_shape);
82 
83         ARM_COMPUTE_ASSERT(input.info()->is_resizable());
84         ARM_COMPUTE_ASSERT(output.info()->is_resizable());
85 
86         // Allocate tensors
87         input.allocator()->allocate();
88         output.allocator()->allocate();
89 
90         ARM_COMPUTE_ASSERT(!input.info()->is_resizable());
91         ARM_COMPUTE_ASSERT(!output.info()->is_resizable());
92 
93         // Fill tensors
94         fill(AccessorType(input), 0);
95         // Compute function
96         space_to_depth.run();
97 
98         return output;
99     }
100 
compute_reference(const TensorShape & input_shape,const TensorShape & output_shape,const int block_shape,DataType data_type)101     SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &output_shape,
102                                       const int block_shape, DataType data_type)
103     {
104         // Create reference
105         SimpleTensor<T> input{ input_shape, data_type };
106 
107         // Fill reference
108         fill(input, 0);
109 
110         // Compute reference
111         return reference::space_to_depth(input, output_shape, block_shape);
112     }
113 
114     TensorType      _target{};
115     SimpleTensor<T> _reference{};
116 };
117 } // namespace validation
118 } // namespace test
119 } // namespace arm_compute
120 #endif /* ARM_COMPUTE_TEST_SPACE_TO_DEPTH_LAYER_FIXTURE */
121