Ramy Elgammal | 73f19af | 2022-10-23 11:44:49 +0100 | [diff] [blame] | 1 | /* |
Gunes Bayir | 3a1e125 | 2023-01-03 21:26:09 +0000 | [diff] [blame^] | 2 | * Copyright (c) 2022-2023 Arm Limited. |
Ramy Elgammal | 73f19af | 2022-10-23 11:44:49 +0100 | [diff] [blame] | 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 | */ |
Ramy Elgammal | 404462a | 2022-11-08 02:14:46 +0000 | [diff] [blame] | 24 | #ifndef TESTS_VALIDATION_FIXTURES_DYNAMIC_FUSION_GPU_CL_DIRECTCONV2DFIXTURE |
| 25 | #define TESTS_VALIDATION_FIXTURES_DYNAMIC_FUSION_GPU_CL_DIRECTCONV2DFIXTURE |
Ramy Elgammal | 73f19af | 2022-10-23 11:44:49 +0100 | [diff] [blame] | 26 | |
| 27 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
| 28 | #include "arm_compute/core/TensorInfo.h" |
| 29 | #include "arm_compute/core/Types.h" |
| 30 | |
Ramy Elgammal | 73f19af | 2022-10-23 11:44:49 +0100 | [diff] [blame] | 31 | #include "arm_compute/dynamic_fusion/runtime/gpu/cl/ClWorkloadRuntime.h" |
| 32 | #include "arm_compute/dynamic_fusion/sketch/OperatorAttributes.h" |
| 33 | #include "arm_compute/dynamic_fusion/sketch/gpu/GpuWorkloadSketch.h" |
| 34 | #include "arm_compute/dynamic_fusion/sketch/gpu/operators/GpuConv2d.h" |
Viet-Hoa Do | b84e253 | 2022-12-13 13:09:10 +0000 | [diff] [blame] | 35 | #include "arm_compute/dynamic_fusion/sketch/gpu/operators/GpuOutput.h" |
Ramy Elgammal | 73f19af | 2022-10-23 11:44:49 +0100 | [diff] [blame] | 36 | |
Ramy Elgammal | 73f19af | 2022-10-23 11:44:49 +0100 | [diff] [blame] | 37 | #include "tests/CL/CLAccessor.h" |
Ramy Elgammal | 73f19af | 2022-10-23 11:44:49 +0100 | [diff] [blame] | 38 | #include "tests/framework/Fixture.h" |
| 39 | #include "tests/framework/Macros.h" |
Ramy Elgammal | 73f19af | 2022-10-23 11:44:49 +0100 | [diff] [blame] | 40 | #include "tests/validation/Validation.h" |
| 41 | #include "tests/validation/reference/ConvolutionLayer.h" |
Ramy Elgammal | 73f19af | 2022-10-23 11:44:49 +0100 | [diff] [blame] | 42 | #include "tests/validation/reference/Permute.h" |
| 43 | |
| 44 | using namespace arm_compute::experimental::dynamic_fusion; |
| 45 | |
| 46 | namespace arm_compute |
| 47 | { |
| 48 | namespace test |
| 49 | { |
| 50 | namespace validation |
| 51 | { |
| 52 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 53 | class DynamicFusionGpuConv2dValidationGenericFixture : public framework::Fixture |
| 54 | { |
| 55 | public: |
| 56 | using TBias = typename std::conditional < std::is_same<typename std::decay<T>::type, uint8_t>::value |
| 57 | || std::is_same<typename std::decay<T>::type, int8_t>::value, |
| 58 | int32_t, T >::type; // If T: uint8_t or int8_t then TBias: int32_t, otherwise TBias: T |
| 59 | |
| 60 | template <typename...> |
| 61 | void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, const PadStrideInfo &info, const Size2D &dilation, DataType data_type, |
| 62 | DataLayout data_layout, QuantizationInfo quantization_info, QuantizationInfo weight_quantization_info) |
| 63 | { |
| 64 | ARM_COMPUTE_ERROR_ON(data_layout != DataLayout::NHWC); // Dynamic fusion conv2d only supports NHWC layout |
| 65 | const Conv2dAttributes conv2d_attr = convert_pad_stride_info_to_conv_attr(info, dilation); |
| 66 | _data_type = data_type; |
| 67 | _data_layout = data_layout; |
| 68 | _is_quantized = is_data_type_quantized_asymmetric(data_type); |
| 69 | _quantization_info = quantization_info; |
| 70 | _weight_quantization_info = weight_quantization_info; |
| 71 | _bias_data_type = _is_quantized ? DataType::S32 : data_type; |
| 72 | _target = compute_target(input_shape, weights_shape, bias_shape, conv2d_attr); |
| 73 | _reference = compute_reference(input_shape, weights_shape, bias_shape, output_shape, conv2d_attr); |
| 74 | } |
| 75 | |
| 76 | protected: |
| 77 | template <typename U> |
| 78 | void fill(U &&tensor, int i) |
| 79 | { |
| 80 | switch(tensor.data_type()) |
| 81 | { |
| 82 | case DataType::F16: |
| 83 | { |
| 84 | arm_compute::utils::uniform_real_distribution_16bit<half> distribution{ -1.0f, 1.0f }; |
| 85 | library->fill(tensor, distribution, i); |
| 86 | break; |
| 87 | } |
| 88 | case DataType::F32: |
| 89 | { |
| 90 | std::uniform_real_distribution<float> distribution(-1.0f, 1.0f); |
| 91 | library->fill(tensor, distribution, i); |
| 92 | break; |
| 93 | } |
| 94 | default: |
| 95 | library->fill_tensor_uniform(tensor, i); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // Given input is in nchw format |
| 100 | TensorType compute_target(TensorShape input_shape, TensorShape weights_shape, const TensorShape &bias_shape, Conv2dAttributes conv2d_attr) |
| 101 | { |
| 102 | ARM_COMPUTE_ERROR_ON(_data_layout != DataLayout::NHWC); |
| 103 | permute(input_shape, PermutationVector(2U, 0U, 1U)); |
| 104 | permute(weights_shape, PermutationVector(2U, 0U, 1U)); |
| 105 | CLScheduler::get().default_reinit(); |
| 106 | |
| 107 | // Create a new workload sketch |
| 108 | auto cl_compile_ctx = CLKernelLibrary::get().get_compile_context(); |
| 109 | auto gpu_ctx = GpuWorkloadContext{ &cl_compile_ctx }; |
| 110 | GpuWorkloadSketch sketch{ &gpu_ctx }; |
| 111 | |
| 112 | // Create sketch tensors |
Gunes Bayir | 3a1e125 | 2023-01-03 21:26:09 +0000 | [diff] [blame^] | 113 | TensorInfo input_info = sketch.create_tensor_info(TensorInfo(input_shape, 1, _data_type, _data_layout)); |
| 114 | TensorInfo weight_info = sketch.create_tensor_info(TensorInfo(weights_shape, 1, _data_type, _data_layout)); |
| 115 | TensorInfo bias_info = sketch.create_tensor_info(TensorInfo(bias_shape, 1, _data_type, _data_layout)); |
| 116 | TensorInfo dst_info = sketch.create_tensor_info(); |
Viet-Hoa Do | b84e253 | 2022-12-13 13:09:10 +0000 | [diff] [blame] | 117 | |
Gunes Bayir | 3a1e125 | 2023-01-03 21:26:09 +0000 | [diff] [blame^] | 118 | ITensorInfo *ans_info = FunctionType::create_op(sketch, &input_info, &weight_info, &bias_info, conv2d_attr); |
| 119 | GpuOutput::create_op(sketch, ans_info, &dst_info); |
Ramy Elgammal | 73f19af | 2022-10-23 11:44:49 +0100 | [diff] [blame] | 120 | |
| 121 | // Configure runtime |
| 122 | ClWorkloadRuntime runtime; |
| 123 | runtime.configure(sketch); |
| 124 | // (Important) Allocate auxiliary tensor memory if there are any |
| 125 | for(auto &data : runtime.get_auxiliary_tensors()) |
| 126 | { |
| 127 | auto tensor = data.first; |
| 128 | const auto aux_mem_req = data.second; |
| 129 | tensor->allocator()->init(*data.first->info(), aux_mem_req.alignment); |
| 130 | tensor->allocator()->allocate(); // Use ACL allocated memory |
| 131 | } |
| 132 | // Construct user tensors |
Ramy Elgammal | 404462a | 2022-11-08 02:14:46 +0000 | [diff] [blame] | 133 | TensorType t_input{}; |
| 134 | TensorType t_weight{}; |
| 135 | TensorType t_bias{}; |
| 136 | TensorType t_dst{}; |
Ramy Elgammal | 73f19af | 2022-10-23 11:44:49 +0100 | [diff] [blame] | 137 | |
| 138 | // Initialize user tensors |
| 139 | t_input.allocator()->init(input_info); |
| 140 | t_weight.allocator()->init(weight_info); |
| 141 | t_bias.allocator()->init(bias_info); |
| 142 | t_dst.allocator()->init(dst_info); |
| 143 | |
| 144 | // Allocate and fill user tensors |
| 145 | t_input.allocator()->allocate(); |
| 146 | t_weight.allocator()->allocate(); |
| 147 | t_bias.allocator()->allocate(); |
| 148 | t_dst.allocator()->allocate(); |
Ramy Elgammal | 404462a | 2022-11-08 02:14:46 +0000 | [diff] [blame] | 149 | |
| 150 | fill(AccessorType(t_input), 0); |
| 151 | fill(AccessorType(t_weight), 1); |
| 152 | fill(AccessorType(t_bias), 2); |
Ramy Elgammal | 73f19af | 2022-10-23 11:44:49 +0100 | [diff] [blame] | 153 | |
| 154 | // Run runtime |
| 155 | runtime.run({ &t_input, &t_weight, &t_bias, &t_dst }); |
| 156 | return t_dst; |
| 157 | } |
| 158 | |
| 159 | SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, |
| 160 | const TensorShape &output_shape, Conv2dAttributes conv2d_attr) |
| 161 | { |
| 162 | // Create reference |
| 163 | SimpleTensor<T> src{ input_shape, _data_type, 1, _quantization_info }; |
| 164 | SimpleTensor<T> weight{ weights_shape, _data_type, 1, _weight_quantization_info }; |
| 165 | SimpleTensor<TBias> bias{ bias_shape, _data_type, 1, _quantization_info }; |
| 166 | |
| 167 | fill(src, 0); |
| 168 | fill(weight, 1); |
| 169 | fill(bias, 2); |
| 170 | |
| 171 | auto src_nchw = src; |
| 172 | auto weights_nchw = weight; |
| 173 | auto bias_nchw = bias; |
| 174 | auto output_shape_nchw = output_shape; |
| 175 | |
| 176 | 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, |
| 177 | DimensionRoundingType{}); |
| 178 | auto dst_nchw = reference::convolution_layer(src_nchw, weights_nchw, bias_nchw, output_shape_nchw, legacy_pad_stride, conv2d_attr.dilation()); |
| 179 | return dst_nchw; |
| 180 | } |
| 181 | |
| 182 | TensorType _target{}; |
| 183 | SimpleTensor<T> _reference{}; |
| 184 | DataType _data_type{}; |
Ramy Elgammal | 73f19af | 2022-10-23 11:44:49 +0100 | [diff] [blame] | 185 | DataType _bias_data_type{}; |
Ramy Elgammal | 73f19af | 2022-10-23 11:44:49 +0100 | [diff] [blame] | 186 | DataLayout _data_layout{}; |
| 187 | QuantizationInfo _quantization_info{}; |
| 188 | QuantizationInfo _weight_quantization_info{}; |
| 189 | bool _is_quantized = false; |
Ramy Elgammal | 73f19af | 2022-10-23 11:44:49 +0100 | [diff] [blame] | 190 | }; |
| 191 | |
| 192 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 193 | class DynamicFusionGpuConv2dValidationFixture : public DynamicFusionGpuConv2dValidationGenericFixture<TensorType, AccessorType, FunctionType, T> |
| 194 | { |
| 195 | public: |
| 196 | template <typename...> |
| 197 | void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape output_shape, TensorShape bias_shape, |
| 198 | const PadStrideInfo &info, const Size2D &dialation, DataType data_type, DataLayout data_layout, QuantizationInfo quantization_info) |
| 199 | { |
| 200 | DynamicFusionGpuConv2dValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(input_shape, weights_shape, output_shape, bias_shape, info, dialation, |
Ramy Elgammal | 404462a | 2022-11-08 02:14:46 +0000 | [diff] [blame] | 201 | data_type, data_layout, quantization_info, quantization_info); |
Ramy Elgammal | 73f19af | 2022-10-23 11:44:49 +0100 | [diff] [blame] | 202 | } |
| 203 | }; |
| 204 | } // namespace validation |
| 205 | } // namespace test |
| 206 | } // namespace arm_compute |
Ramy Elgammal | 404462a | 2022-11-08 02:14:46 +0000 | [diff] [blame] | 207 | #endif /* TESTS_VALIDATION_FIXTURES_DYNAMIC_FUSION_GPU_CL_DIRECTCONV2DFIXTURE */ |