Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +0000 | [diff] [blame] | 1 | /* |
Viet-Hoa Do | fdf56fb | 2024-01-18 16:10:46 +0000 | [diff] [blame] | 2 | * Copyright (c) 2023-2024 Arm Limited. |
Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +0000 | [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 | */ |
Adnan AlSinan | 2e6d659 | 2023-08-21 13:54:27 +0100 | [diff] [blame] | 24 | #ifndef ACL_TESTS_VALIDATION_FIXTURES_DYNAMIC_FUSION_GPU_CL_POOL2DFIXTURE_H |
| 25 | #define ACL_TESTS_VALIDATION_FIXTURES_DYNAMIC_FUSION_GPU_CL_POOL2DFIXTURE_H |
Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +0000 | [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 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +0000 | [diff] [blame] | 31 | #include "arm_compute/dynamic_fusion/runtime/gpu/cl/ClWorkloadRuntime.h" |
| 32 | #include "arm_compute/dynamic_fusion/sketch/attributes/Pool2dAttributes.h" |
| 33 | #include "arm_compute/dynamic_fusion/sketch/gpu/GpuWorkloadSketch.h" |
Adnan AlSinan | 2e6d659 | 2023-08-21 13:54:27 +0100 | [diff] [blame] | 34 | #include "arm_compute/dynamic_fusion/sketch/gpu/operators/GpuOutput.h" |
Viet-Hoa Do | fdf56fb | 2024-01-18 16:10:46 +0000 | [diff] [blame] | 35 | #include "arm_compute/dynamic_fusion/sketch/gpu/operators/GpuPool2d.h" |
Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +0000 | [diff] [blame] | 36 | |
Viet-Hoa Do | fdf56fb | 2024-01-18 16:10:46 +0000 | [diff] [blame] | 37 | #include "src/dynamic_fusion/utils/Utils.h" |
Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +0000 | [diff] [blame] | 38 | #include "tests/CL/CLAccessor.h" |
| 39 | #include "tests/framework/Fixture.h" |
| 40 | #include "tests/validation/reference/PoolingLayer.h" |
| 41 | |
| 42 | using namespace arm_compute::experimental::dynamic_fusion; |
| 43 | |
| 44 | namespace arm_compute |
| 45 | { |
| 46 | namespace test |
| 47 | { |
| 48 | namespace validation |
| 49 | { |
| 50 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 51 | class DynamicFusionGpuPool2dValidationGenericFixture : public framework::Fixture |
| 52 | { |
| 53 | public: |
Gunes Bayir | 2b9fa59 | 2024-01-17 16:07:03 +0000 | [diff] [blame^] | 54 | void setup(TensorShape input_shape, const Pool2dAttributes &pool_attr, DataType data_type) |
Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +0000 | [diff] [blame] | 55 | { |
Gunes Bayir | 2b9fa59 | 2024-01-17 16:07:03 +0000 | [diff] [blame^] | 56 | _target = compute_target(input_shape, pool_attr, data_type); |
| 57 | _reference = compute_reference( |
| 58 | input_shape, convert_pool_attr_to_pool_info(pool_attr, true /* mixed_precision */), data_type); |
Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | protected: |
| 62 | template <typename U> |
| 63 | void fill(U &&tensor, int i) |
| 64 | { |
Viet-Hoa Do | fdf56fb | 2024-01-18 16:10:46 +0000 | [diff] [blame] | 65 | switch (tensor.data_type()) |
Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +0000 | [diff] [blame] | 66 | { |
| 67 | case DataType::F16: |
| 68 | { |
Viet-Hoa Do | fdf56fb | 2024-01-18 16:10:46 +0000 | [diff] [blame] | 69 | arm_compute::utils::uniform_real_distribution_16bit<half> distribution{-1.0f, 1.0f}; |
Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +0000 | [diff] [blame] | 70 | library->fill(tensor, distribution, i); |
| 71 | break; |
| 72 | } |
| 73 | case DataType::F32: |
| 74 | { |
| 75 | std::uniform_real_distribution<float> distribution(-1.0f, 1.0f); |
| 76 | library->fill(tensor, distribution, i); |
| 77 | break; |
| 78 | } |
| 79 | default: |
| 80 | library->fill_tensor_uniform(tensor, i); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | // Given input is in nchw format |
Gunes Bayir | 2b9fa59 | 2024-01-17 16:07:03 +0000 | [diff] [blame^] | 85 | TensorType compute_target(TensorShape input_shape, const Pool2dAttributes &pool_attr, const DataType data_type) |
Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +0000 | [diff] [blame] | 86 | { |
| 87 | CLScheduler::get().default_reinit(); |
| 88 | |
| 89 | // Change shape due to NHWC data layout, test shapes are NCHW |
| 90 | permute(input_shape, PermutationVector(2U, 0U, 1U)); |
| 91 | |
| 92 | // Create a new workload sketch |
| 93 | auto cl_compile_ctx = CLKernelLibrary::get().get_compile_context(); |
Viet-Hoa Do | fdf56fb | 2024-01-18 16:10:46 +0000 | [diff] [blame] | 94 | auto context = GpuWorkloadContext{&cl_compile_ctx}; |
| 95 | GpuWorkloadSketch sketch{&context}; |
Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +0000 | [diff] [blame] | 96 | |
| 97 | // Create sketch tensors |
Viet-Hoa Do | 3fcf3dc | 2023-05-17 15:17:48 +0100 | [diff] [blame] | 98 | auto input_info = context.create_tensor_info(TensorInfo(input_shape, 1, data_type, DataLayout::NHWC)); |
| 99 | auto dst_info = context.create_tensor_info(); |
Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +0000 | [diff] [blame] | 100 | |
| 101 | // Create Pool2dSettings |
Gunes Bayir | 2b9fa59 | 2024-01-17 16:07:03 +0000 | [diff] [blame^] | 102 | GpuPool2dSettings pool_settings = GpuPool2dSettings(); |
Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +0000 | [diff] [blame] | 103 | |
Viet-Hoa Do | fdf56fb | 2024-01-18 16:10:46 +0000 | [diff] [blame] | 104 | ITensorInfo *ans_info = FunctionType::create_op(sketch, input_info, pool_attr, pool_settings); |
| 105 | GpuOutput::create_op(sketch, ans_info, dst_info); |
Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +0000 | [diff] [blame] | 106 | |
| 107 | // Configure runtime |
| 108 | ClWorkloadRuntime runtime; |
| 109 | runtime.configure(sketch); |
| 110 | // (Important) Allocate auxiliary tensor memory if there are any |
Viet-Hoa Do | fdf56fb | 2024-01-18 16:10:46 +0000 | [diff] [blame] | 111 | for (auto &data : runtime.get_auxiliary_tensors()) |
Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +0000 | [diff] [blame] | 112 | { |
Ramy Elgammal | 002e653 | 2023-01-11 18:48:04 +0000 | [diff] [blame] | 113 | CLTensor *tensor = std::get<0>(data); |
| 114 | TensorInfo info = std::get<1>(data); |
| 115 | AuxMemoryInfo aux_mem_req = std::get<2>(data); |
| 116 | tensor->allocator()->init(info, aux_mem_req.alignment); |
Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +0000 | [diff] [blame] | 117 | tensor->allocator()->allocate(); // Use ACL allocated memory |
| 118 | } |
| 119 | // Construct user tensors |
| 120 | TensorType t_input{}; |
| 121 | TensorType t_dst{}; |
| 122 | |
| 123 | // Initialize user tensors |
Viet-Hoa Do | fdf56fb | 2024-01-18 16:10:46 +0000 | [diff] [blame] | 124 | t_input.allocator()->init(*input_info); |
| 125 | t_dst.allocator()->init(*dst_info); |
Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +0000 | [diff] [blame] | 126 | |
| 127 | // Allocate and fill user tensors |
| 128 | t_input.allocator()->allocate(); |
| 129 | t_dst.allocator()->allocate(); |
| 130 | |
| 131 | fill(AccessorType(t_input), 0); |
| 132 | |
| 133 | // Run runtime |
Viet-Hoa Do | fdf56fb | 2024-01-18 16:10:46 +0000 | [diff] [blame] | 134 | runtime.run({&t_input, &t_dst}); |
Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +0000 | [diff] [blame] | 135 | return t_dst; |
| 136 | } |
| 137 | |
| 138 | SimpleTensor<T> compute_reference(TensorShape shape, PoolingLayerInfo pool_info, DataType data_type) |
| 139 | { |
| 140 | // Create reference |
| 141 | SimpleTensor<T> src(shape, data_type, 1, QuantizationInfo()); |
| 142 | // Fill reference |
| 143 | fill(src, 0); |
| 144 | return reference::pooling_layer<T>(src, pool_info, QuantizationInfo(), nullptr, DataLayout::NCHW); |
| 145 | } |
| 146 | |
Ramy Elgammal | 002e653 | 2023-01-11 18:48:04 +0000 | [diff] [blame] | 147 | TensorType _target{}; |
| 148 | SimpleTensor<T> _reference{}; |
Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +0000 | [diff] [blame] | 149 | }; |
| 150 | |
| 151 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
Viet-Hoa Do | fdf56fb | 2024-01-18 16:10:46 +0000 | [diff] [blame] | 152 | class DynamicFusionGpuPool2dValidationFixture |
| 153 | : public DynamicFusionGpuPool2dValidationGenericFixture<TensorType, AccessorType, FunctionType, T> |
Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +0000 | [diff] [blame] | 154 | { |
| 155 | public: |
Viet-Hoa Do | fdf56fb | 2024-01-18 16:10:46 +0000 | [diff] [blame] | 156 | void setup(TensorShape input_shape, |
| 157 | PoolingType pool_type, |
| 158 | Size2D pool_size, |
| 159 | Padding2D pad, |
| 160 | Size2D stride, |
| 161 | bool exclude_padding, |
| 162 | DataType data_type) |
Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +0000 | [diff] [blame] | 163 | { |
Viet-Hoa Do | fdf56fb | 2024-01-18 16:10:46 +0000 | [diff] [blame] | 164 | DynamicFusionGpuPool2dValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup( |
| 165 | input_shape, |
| 166 | Pool2dAttributes().pool_type(pool_type).pool_size(pool_size).pad(pad).stride(stride).exclude_padding( |
| 167 | exclude_padding), |
Gunes Bayir | 2b9fa59 | 2024-01-17 16:07:03 +0000 | [diff] [blame^] | 168 | data_type); |
Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +0000 | [diff] [blame] | 169 | } |
| 170 | }; |
| 171 | |
| 172 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
Viet-Hoa Do | fdf56fb | 2024-01-18 16:10:46 +0000 | [diff] [blame] | 173 | class DynamicFusionGpuPool2dSpecialValidationFixture |
| 174 | : public DynamicFusionGpuPool2dValidationGenericFixture<TensorType, AccessorType, FunctionType, T> |
Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +0000 | [diff] [blame] | 175 | { |
| 176 | public: |
Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +0000 | [diff] [blame] | 177 | void setup(TensorShape input_shape, Pool2dAttributes pool_attr, DataType data_type) |
| 178 | { |
Viet-Hoa Do | fdf56fb | 2024-01-18 16:10:46 +0000 | [diff] [blame] | 179 | DynamicFusionGpuPool2dValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup( |
Gunes Bayir | 2b9fa59 | 2024-01-17 16:07:03 +0000 | [diff] [blame^] | 180 | input_shape, pool_attr, data_type); |
Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +0000 | [diff] [blame] | 181 | } |
| 182 | }; |
| 183 | |
| 184 | } // namespace validation |
| 185 | } // namespace test |
| 186 | } // namespace arm_compute |
| 187 | |
Adnan AlSinan | 2e6d659 | 2023-08-21 13:54:27 +0100 | [diff] [blame] | 188 | #endif // ACL_TESTS_VALIDATION_FIXTURES_DYNAMIC_FUSION_GPU_CL_POOL2DFIXTURE_H |