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_MULFIXTURE 25 #define TESTS_VALIDATION_FIXTURES_DYNAMIC_FUSION_OPERATORS_MULFIXTURE 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/dynamic_fusion/runtime/gpu/cl/ClWorkloadRuntime.h" 31 #include "arm_compute/dynamic_fusion/sketch/gpu/GpuWorkloadSketch.h" 32 #include "arm_compute/dynamic_fusion/sketch/gpu/operators/GpuOutput.h" 33 34 #include "tests/Globals.h" 35 #include "tests/framework/Fixture.h" 36 #include "tests/framework/Macros.h" 37 #include "tests/validation/reference/PixelWiseMultiplication.h" 38 39 using namespace arm_compute::experimental::dynamic_fusion; 40 41 namespace arm_compute 42 { 43 namespace test 44 { 45 namespace validation 46 { 47 /* We use a separate test fixture for Multiplication op instead of reusing ElementwiseBinaryFixture to avoid exposing 48 * the internal enum ElementwiseOp to the public utils/TypePrinters.h as required by the data test case macros 49 * to print the test data. 50 */ 51 template <typename TensorType, typename AccessorType, typename FunctionType, typename T> 52 class DynamicFusionMulValidationFixture : public framework::Fixture 53 { 54 public: 55 template <typename...> 56 void setup(const TensorShape &shape0, const TensorShape &shape1, const TensorShape &shape2, DataType data_type, bool is_inplace, bool fuse_two_ops = false) 57 { 58 _data_type = data_type; 59 _is_inplace = is_inplace; 60 _fuse = fuse_two_ops; 61 ARM_COMPUTE_ERROR_ON_MSG(_fuse && shape2.total_size() == 0, "No shape2 provided for fusion of two ops."); 62 ARM_COMPUTE_ERROR_ON_MSG(_fuse && _is_inplace, "In place for fusing case not supported yet."); 63 _target = compute_target(shape0, shape1, shape2); 64 _reference = compute_reference(shape0, shape1, shape2); 65 } 66 67 protected: 68 template <typename U> fill(U && tensor,int i)69 void fill(U &&tensor, int i) 70 { 71 library->fill_tensor_uniform(tensor, i); 72 } 73 compute_target(const TensorShape & shape0,const TensorShape & shape1,const TensorShape & shape2)74 TensorType compute_target(const TensorShape &shape0, const TensorShape &shape1, const TensorShape &shape2) 75 { 76 // Create a new workload sketch 77 auto cl_compile_ctx = CLKernelLibrary::get().get_compile_context(); 78 auto gpu_ctx = GpuWorkloadContext{ &cl_compile_ctx }; 79 GpuWorkloadSketch sketch{ &gpu_ctx }; 80 81 // Fuse first multiplication op 82 TensorInfo lhs_info = sketch.create_tensor_info(TensorInfo(shape0, 1, _data_type)); 83 TensorInfo rhs_info = sketch.create_tensor_info(TensorInfo(shape1, 1, _data_type)); 84 TensorInfo dst_info = sketch.create_tensor_info(); 85 86 TensorInfo rhs_info_fuse; 87 88 ITensorInfo *ans_info = FunctionType::create_op(sketch, &lhs_info, &rhs_info); 89 90 if(_fuse) 91 { 92 rhs_info_fuse = sketch.create_tensor_info(TensorInfo(shape2, 1, _data_type)); 93 ITensorInfo *ans2_info = FunctionType::create_op(sketch, ans_info, &rhs_info_fuse); 94 GpuOutput::create_op(sketch, ans2_info, &dst_info); 95 } 96 else 97 { 98 GpuOutput::create_op(sketch, ans_info, &dst_info); 99 } 100 101 // Configure runtime 102 ClWorkloadRuntime runtime; 103 runtime.configure(sketch); 104 105 // (Important) Allocate auxiliary tensor memory if there are any 106 for(auto &data : runtime.get_auxiliary_tensors()) 107 { 108 CLTensor *tensor = std::get<0>(data); 109 TensorInfo info = std::get<1>(data); 110 AuxMemoryInfo aux_mem_req = std::get<2>(data); 111 tensor->allocator()->init(info, aux_mem_req.alignment); 112 tensor->allocator()->allocate(); // Use ACL allocated memory 113 } 114 115 // Construct user tensors 116 TensorType t_lhs{}; 117 TensorType t_rhs{}; 118 TensorType t_rhs_fuse{}; 119 TensorType t_dst{}; 120 121 // Initialize user tensors 122 t_lhs.allocator()->init(lhs_info); 123 t_rhs.allocator()->init(rhs_info); 124 t_dst.allocator()->init(dst_info); 125 if(_fuse) 126 { 127 t_rhs_fuse.allocator()->init(rhs_info_fuse); 128 } 129 130 // Allocate and fill user tensors 131 // Instead of using ACL allocator, the user can choose to import memory into the tensors 132 t_lhs.allocator()->allocate(); 133 t_rhs.allocator()->allocate(); 134 t_dst.allocator()->allocate(); 135 if(_fuse) 136 { 137 t_rhs_fuse.allocator()->allocate(); 138 } 139 140 fill(AccessorType(t_lhs), 0); 141 fill(AccessorType(t_rhs), 1); 142 if(_fuse) 143 { 144 fill(AccessorType(t_rhs_fuse), 2); 145 } 146 147 // Run runtime 148 if(_fuse) 149 { 150 runtime.run({ &t_lhs, &t_rhs, &t_rhs_fuse, &t_dst }); 151 } 152 else 153 { 154 runtime.run({ &t_lhs, &t_rhs, &t_dst }); 155 } 156 157 return t_dst; 158 } 159 compute_reference(const TensorShape & shape0,const TensorShape & shape1,const TensorShape & shape2)160 SimpleTensor<T> compute_reference(const TensorShape &shape0, const TensorShape &shape1, const TensorShape &shape2) 161 { 162 // Create reference 163 SimpleTensor<T> ref_lhs{ shape0, _data_type, 1, QuantizationInfo() }; 164 SimpleTensor<T> ref_rhs{ shape1, _data_type, 1, QuantizationInfo() }; 165 SimpleTensor<T> ref_rhs_fuse{ shape2, _data_type, 1, QuantizationInfo() }; 166 167 // Fill reference 168 fill(ref_lhs, 0); 169 fill(ref_rhs, 1); 170 SimpleTensor<T> ref_dst = reference::pixel_wise_multiplication<T, T, T>(ref_lhs, 171 ref_rhs, 172 1.f, 173 ConvertPolicy::SATURATE, 174 RoundingPolicy::TO_NEAREST_UP, 175 _data_type, 176 QuantizationInfo()); 177 if(_fuse) 178 { 179 fill(ref_rhs_fuse, 2); 180 SimpleTensor<T> ref_dst_fuse = reference::pixel_wise_multiplication<T, T, T>(ref_dst, 181 ref_rhs_fuse, 182 1.f, 183 ConvertPolicy::SATURATE, 184 RoundingPolicy::TO_NEAREST_UP, 185 _data_type, 186 QuantizationInfo()); 187 return ref_dst_fuse; 188 } 189 return ref_dst; 190 } 191 192 TensorType _target{}; 193 SimpleTensor<T> _reference{}; 194 DataType _data_type{}; 195 bool _is_inplace{ false }; 196 bool _fuse{ false }; 197 }; 198 199 template <typename TensorType, typename AccessorType, typename FunctionType, typename T> 200 class DynamicFusionMulOneOpValidationFixture : public DynamicFusionMulValidationFixture<TensorType, AccessorType, FunctionType, T> 201 { 202 public: 203 template <typename...> setup(const TensorShape & shape0,DataType data_type,bool is_inplace)204 void setup(const TensorShape &shape0, DataType data_type, bool is_inplace) 205 { 206 DynamicFusionMulValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape0, shape0, TensorShape(), data_type, is_inplace); 207 } 208 }; 209 210 template <typename TensorType, typename AccessorType, typename FunctionType, typename T> 211 class DynamicFusionMulBroadcastValidationFixture : public DynamicFusionMulValidationFixture<TensorType, AccessorType, FunctionType, T> 212 { 213 public: 214 template <typename...> setup(const TensorShape & shape0,const TensorShape & shape1,DataType data_type,bool is_inplace)215 void setup(const TensorShape &shape0, const TensorShape &shape1, DataType data_type, bool is_inplace) 216 { 217 DynamicFusionMulValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape0, shape1, TensorShape(), data_type, is_inplace); 218 } 219 }; 220 221 template <typename TensorType, typename AccessorType, typename FunctionType, typename T> 222 class DynamicFusionMulTwoOpsValidationFixture : public DynamicFusionMulValidationFixture<TensorType, AccessorType, FunctionType, T> 223 { 224 public: 225 template <typename...> setup(const TensorShape & shape0,const TensorShape & shape1,const TensorShape & shape2,DataType data_type,bool is_inplace,bool fuse_two_ops)226 void setup(const TensorShape &shape0, const TensorShape &shape1, const TensorShape &shape2, DataType data_type, bool is_inplace, bool fuse_two_ops) 227 { 228 DynamicFusionMulValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape0, shape1, shape2, data_type, is_inplace, fuse_two_ops); 229 } 230 }; 231 232 } // namespace validation 233 } // namespace test 234 } // namespace arm_compute 235 #endif /* TESTS_VALIDATION_FIXTURES_DYNAMIC_FUSION_OPERATORS_MULFIXTURE */ 236