• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 TESTS_VALIDATION_FIXTURES_DYNAMIC_FUSION_OPERATORS_RESHAPEFIXTURE
25 #define TESTS_VALIDATION_FIXTURES_DYNAMIC_FUSION_OPERATORS_RESHAPEFIXTURE
26 
27 #include "arm_compute/core/TensorShape.h"
28 #include "arm_compute/core/Types.h"
29 #include "arm_compute/dynamic_fusion/runtime/gpu/cl/ClWorkloadRuntime.h"
30 #include "arm_compute/dynamic_fusion/sketch/attributes/ReshapeAttributes.h"
31 #include "arm_compute/dynamic_fusion/sketch/gpu/GpuWorkloadContext.h"
32 #include "arm_compute/dynamic_fusion/sketch/gpu/GpuWorkloadSketch.h"
33 #include "arm_compute/dynamic_fusion/sketch/gpu/operators/GpuOutput.h"
34 #include "arm_compute/dynamic_fusion/sketch/gpu/operators/GpuReshape.h"
35 
36 #include "tests/Globals.h"
37 #include "tests/framework/Asserts.h"
38 #include "tests/framework/Fixture.h"
39 #include "tests/validation/reference/ReshapeLayer.h"
40 
41 using namespace arm_compute::experimental::dynamic_fusion;
42 
43 namespace arm_compute
44 {
45 namespace test
46 {
47 namespace validation
48 {
49 template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
50 class DynamicFusionGpuReshapeLayerValidationFixture : public framework::Fixture
51 {
52 public:
53     template <typename...>
setup(TensorShape input_shape,TensorShape output_shape,DataType data_type)54     void setup(TensorShape input_shape, TensorShape output_shape, DataType data_type)
55     {
56         _target    = compute_target(input_shape, output_shape, data_type);
57         _reference = compute_reference(input_shape, output_shape, data_type);
58     }
59 
60 protected:
61     template <typename U>
fill(U && tensor,int i)62     void fill(U &&tensor, int i)
63     {
64         library->fill_tensor_uniform(tensor, i);
65     }
66 
compute_target(TensorShape & input_shape,TensorShape & output_shape,DataType data_type)67     TensorType compute_target(TensorShape &input_shape, TensorShape &output_shape, DataType data_type)
68     {
69         // Check if indeed the input shape can be reshape to the output one
70         ARM_COMPUTE_ASSERT(input_shape.total_size() == output_shape.total_size());
71 
72         // Create a new workload sketch
73         auto              cl_compile_ctx = CLKernelLibrary::get().get_compile_context();
74         auto              gpu_ctx        = GpuWorkloadContext{ &cl_compile_ctx };
75         GpuWorkloadSketch sketch{ &gpu_ctx };
76 
77         // Create sketch tensors
78         TensorInfo        src_info = sketch.create_tensor_info(TensorInfo(input_shape, 1, data_type));
79         TensorInfo        dst_info = sketch.create_tensor_info(TensorInfo(output_shape, 1, data_type));
80         ReshapeAttributes attributes;
81         attributes.shape(output_shape);
82 
83         ITensorInfo *ans_info = FunctionType::create_op(sketch, &src_info, attributes);
84         GpuOutput::create_op(sketch, ans_info, &dst_info);
85 
86         // Configure runtime
87         ClWorkloadRuntime runtime;
88         runtime.configure(sketch);
89 
90         // (Important) Allocate auxiliary tensor memory if there are any
91         for(auto &data : runtime.get_auxiliary_tensors())
92         {
93             CLTensor     *tensor      = std::get<0>(data);
94             TensorInfo    info        = std::get<1>(data);
95             AuxMemoryInfo aux_mem_req = std::get<2>(data);
96             tensor->allocator()->init(info, aux_mem_req.alignment);
97             tensor->allocator()->allocate(); // Use ACL allocated memory
98         }
99 
100         // Construct user tensors
101         TensorType t_src{};
102         TensorType t_dst{};
103         // Initialize user tensors
104         t_src.allocator()->init(src_info);
105         t_dst.allocator()->init(dst_info);
106 
107         // Allocate and fill user tensors
108         t_src.allocator()->allocate();
109         t_dst.allocator()->allocate();
110 
111         fill(AccessorType(t_src), 0);
112 
113         // Run runtime
114         runtime.run({ &t_src, &t_dst });
115 
116         return t_dst;
117     }
118 
compute_reference(const TensorShape & input_shape,const TensorShape & output_shape,DataType data_type)119     SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &output_shape, DataType data_type)
120     {
121         // Create reference
122         SimpleTensor<T> src{ input_shape, data_type };
123 
124         // Fill reference
125         fill(src, 0);
126 
127         return reference::reshape_layer<T>(src, output_shape);
128     }
129 
130     TensorType      _target{};
131     SimpleTensor<T> _reference{};
132 };
133 /** [ReshapeLayer fixture] **/
134 } // namespace validation
135 } // namespace test
136 } // namespace arm_compute
137 #endif /* TESTS_VALIDATION_FIXTURES_DYNAMIC_FUSION_OPERATORS_RESHAPEFIXTURE */
138