blob: 6ee2e20d350aa11d0893b37f63b96b8627b06067 [file] [log] [blame]
SiCong Lif44bbc52022-08-29 18:25:51 +01001/*
2 * Copyright (c) 2022 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
25#include "arm_compute/core/CL/CLKernelLibrary.h"
26#include "arm_compute/core/TensorInfo.h"
27#include "arm_compute/dynamic_fusion/runtime/gpu/cl/ClWorkloadRuntime.h"
28#include "arm_compute/dynamic_fusion/sketch/OperatorAttributes.h"
29#include "arm_compute/dynamic_fusion/sketch/gpu/GpuWorkloadSketch.h"
30#include "arm_compute/dynamic_fusion/sketch/gpu/operators/GpuConv2d.h"
31#include "arm_compute/runtime/CL/CLScheduler.h"
32
33#include "src/gpu/cl/operators/ClAdd.h"
34#include "src/gpu/cl/operators/ClConv2d.h"
35
36#include "tests/CL/CLAccessor.h"
37#include "tests/framework/Asserts.h"
38#include "tests/framework/Macros.h"
39#include "tests/validation/CL/UNIT/dynamic_fusion/Utils.h"
40#include "tests/validation/Validation.h"
41#include "tests/validation/reference/ConvolutionLayer.h"
42#include "tests/validation/reference/ElementwiseOperations.h"
43#include "tests/validation/reference/Permute.h"
44
45#ifdef ARM_COMPUTE_ASSERTS_ENABLED
46#include "tests/SimpleTensorPrinter.h"
47#endif /* ARM_COMPUTE_ASSERTS_ENABLED */
48
49using namespace arm_compute::experimental::dynamic_fusion;
50using namespace arm_compute::test::validation::utils;
51
52namespace arm_compute
53{
54namespace test
55{
56namespace validation
57{
58TEST_SUITE(CL)
59TEST_SUITE(INTEGRATION)
60TEST_SUITE(DYNAMIC_FUSION)
61TEST_CASE(Conv2d, framework::DatasetMode::ALL)
62{
63 /* Computation:
64 * out = conv2d1x1(direct_conv)(input, weights, bias)
65 */
66 CLScheduler::get().default_reinit();
67
68 const auto data_type = DataType::F32;
69 const auto data_layout = DataLayout::NHWC;
70 const auto t_input_shape = TensorShape(384, 12, 12);
71 const auto t_weight_shape = TensorShape(384, 1, 1, 16);
72 const auto t_dst_shape = TensorShape(16, 12, 12);
73
74 // Create a new workload sketch
75 auto cl_compile_ctx = CLKernelLibrary::get().get_compile_context();
76 auto gpu_ctx = GpuWorkloadContext{ &cl_compile_ctx };
77 GpuWorkloadSketch sketch{ &gpu_ctx };
78
79 // Fuse conv2d
80 Conv2dAttributes conv2d_attr{};
81 auto input_info = sketch.create_tensor_info(t_input_shape, 1, data_type, data_layout);
82 auto weight_info = sketch.create_tensor_info(TensorInfo(t_weight_shape, 1, data_type, data_layout));
83 auto dst_info = sketch.create_tensor_info();
84 GpuConv2d::create_op(sketch, &input_info, &weight_info, nullptr, &dst_info, conv2d_attr);
85
86 // Configure runtime
87 ClWorkloadRuntime runtime;
88 runtime.configure(sketch);
89
90 // (Important) Allocate auxiliary tensor memory if there are any
91 // Instead of using ACL allocated memory, the user can choose to import memory into the tensors
92 for(auto &data : runtime.get_auxiliary_tensors())
93 {
94 CLTensor *tensor = data.first;
95 AuxMemoryInfo aux_mem_req = data.second;
96 tensor->allocator()->init(*data.first->info(), aux_mem_req.alignment);
97 tensor->allocator()->allocate(); // Use ACL allocated memory
98 // auto buf = cl::Buffer();
99 // tensor->allocator()->import_memory(buf); // Or, import external memory
100 }
101
102 // Construct user tensors
103 CLTensor t_input{};
104 CLTensor t_weight{};
105 CLTensor t_dst{};
106
107 // Initialize user tensors
108 t_input.allocator()->init(input_info);
109 t_weight.allocator()->init(weight_info);
110 t_dst.allocator()->init(dst_info);
111
112 // Allocate and fill user tensors
113 // Instead of using ACL allocator, the user can choose to import memory into the tensors
114 t_input.allocator()->allocate();
115 t_weight.allocator()->allocate();
116 t_dst.allocator()->allocate();
117 fill<float>(CLAccessor(t_input), 0, library.get());
118 fill<float>(CLAccessor(t_weight), 1, library.get());
119
120 // Run runtime
121 runtime.run({ &t_input, &t_weight, &t_dst });
122
123 // Create reference
124 SimpleTensor<float> ref_t_input{ t_input_shape, data_type, 1, QuantizationInfo(), DataLayout::NHWC };
125 SimpleTensor<float> ref_t_weight{ t_weight_shape, data_type, 1, QuantizationInfo(), DataLayout::NHWC };
126 SimpleTensor<float> ref_t_bias_placeholder{ t_dst_shape, data_type, 1, QuantizationInfo(), DataLayout::NHWC };
127
128 // Fill reference
129 fill<float>(ref_t_input, 0, library.get());
130 fill<float>(ref_t_weight, 1, library.get());
131
132 auto ref_t_input_nchw = reference::permute(ref_t_input, PermutationVector(1U, 2U, 0U));
133 auto ref_t_weight_nchw = reference::permute(ref_t_weight, PermutationVector(1U, 2U, 0U));
134 auto ref_t_bias_placeholder_nchw = reference::permute(ref_t_bias_placeholder, PermutationVector(1U, 2U, 0U));
135 auto t_dst_shape_nchw = t_dst_shape;
136 permute(t_dst_shape_nchw, PermutationVector(1U, 2U, 0U));
137
138 PadStrideInfo legacy_pad_stride(conv2d_attr.stride().x(), conv2d_attr.stride().y(), conv2d_attr.pad().left, conv2d_attr.pad().right, conv2d_attr.pad().top, conv2d_attr.pad().bottom,
139 DimensionRoundingType{});
140 auto ref_t_dst_nchw = reference::convolution_layer(ref_t_input_nchw, ref_t_weight_nchw, ref_t_bias_placeholder_nchw, t_dst_shape_nchw, legacy_pad_stride, conv2d_attr.dilation());
141 const auto ref_t_dst = reference::permute(ref_t_dst_nchw, PermutationVector(2U, 0U, 1U));
142
143 RelativeTolerance<float> tolerance_f32(0.001f); /**< Tolerance value for comparing reference's output against implementation's output for floating point data types */
144 validate(CLAccessor(t_dst), ref_t_dst_nchw, tolerance_f32);
145}
146TEST_SUITE(Invalid_Fusion_Should_Fail)
147TEST_CASE(Multiple_Complex_Ops_0, framework::DatasetMode::ALL)
148{
149 /* Computation:
150 * out = conv2d(conv2d(l0_input, l0_weight), l1_weight)
151 */
152 CLScheduler::get().default_reinit();
153
154 const auto data_type = DataType::F32;
155 const auto data_layout = DataLayout::NHWC;
156 const auto t_input_shape = TensorShape(384, 12, 12);
157 const auto t_weight_shape = TensorShape(384, 1, 1, 16);
SiCong Lif44bbc52022-08-29 18:25:51 +0100158 auto t_input_info = TensorInfo(t_input_shape, 1, data_type, data_layout);
159 auto t_weight_info = TensorInfo(t_weight_shape, 1, data_type, data_layout);
160 auto t_dst_info = TensorInfo();
161
162 Conv2dAttributes conv2d_attr{};
163
164 // Create a new workload sketch
165 auto cl_compile_ctx = CLKernelLibrary::get().get_compile_context();
166 auto gpu_ctx = GpuWorkloadContext{ &cl_compile_ctx };
167 GpuWorkloadSketch sketch{ &gpu_ctx };
168
169 // Create tensor infos
170 auto input_info = sketch.create_tensor_info(t_input_shape, 1, data_type, data_layout);
171 auto weight_info = sketch.create_tensor_info(TensorInfo(t_weight_shape, 1, data_type, data_layout));
172 auto dst_info = sketch.create_tensor_info();
173
174 // Fuse conv2d into the workload
175 {
176 // Validate operator
177 const auto success = GpuConv2d::validate_op(sketch, &input_info, &weight_info, nullptr, &dst_info, conv2d_attr);
178 ARM_COMPUTE_EXPECT(bool(success), framework::LogLevel::ERRORS);
179
180 GpuConv2d::create_op(sketch, &input_info, &weight_info, nullptr, &dst_info, conv2d_attr);
181 }
182
183 // Create tensor infos
184 auto weight_info_2 = sketch.create_tensor_info(t_weight_info);
185 auto dst_info_2 = sketch.create_tensor_info();
186
187 // Fuse conv2d into the workload
188 {
189 // Validate operator, should fail
190 const auto success = GpuConv2d::validate_op(sketch, &dst_info, &weight_info_2, nullptr, &dst_info_2, conv2d_attr);
191 ARM_COMPUTE_EXPECT(!bool(success), framework::LogLevel::ERRORS);
192 }
193}
194TEST_SUITE_END() // Invalid_Fusion_Should_Fail
195TEST_SUITE_END() // DYNAMIC_FUSION
196TEST_SUITE_END() // INTEGRATION
197TEST_SUITE_END() // CL
198} // namespace validation
199} // namespace test
200} // namespace arm_compute