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