1 /* 2 * Copyright (c) 2017-2020 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 ARM_COMPUTE_TEST_PIXEL_WISE_MULTIPLICATION_FIXTURE 25 #define ARM_COMPUTE_TEST_PIXEL_WISE_MULTIPLICATION_FIXTURE 26 27 #include "arm_compute/core/TensorShape.h" 28 #include "arm_compute/core/Types.h" 29 #include "tests/AssetsLibrary.h" 30 #include "tests/Globals.h" 31 #include "tests/IAccessor.h" 32 #include "tests/framework/Asserts.h" 33 #include "tests/framework/Fixture.h" 34 #include "tests/validation/reference/ActivationLayer.h" 35 #include "tests/validation/reference/PixelWiseMultiplication.h" 36 37 namespace arm_compute 38 { 39 namespace test 40 { 41 namespace validation 42 { 43 template <typename TensorType, typename AccessorType, typename FunctionType, typename T1, typename T2, typename T3 = T2> 44 class PixelWiseMultiplicationGenericValidationFixture : public framework::Fixture 45 { 46 public: 47 template <typename...> setup(const TensorShape & shape0,const TensorShape & shape1,DataType dt_in1,DataType dt_in2,DataType dt_out,float scale,ConvertPolicy convert_policy,RoundingPolicy rounding_policy,QuantizationInfo qinfo0,QuantizationInfo qinfo1,QuantizationInfo qinfo_out,ActivationLayerInfo act_info,bool is_inplace)48 void setup(const TensorShape &shape0, 49 const TensorShape &shape1, 50 DataType dt_in1, 51 DataType dt_in2, 52 DataType dt_out, 53 float scale, 54 ConvertPolicy convert_policy, 55 RoundingPolicy rounding_policy, 56 QuantizationInfo qinfo0, 57 QuantizationInfo qinfo1, 58 QuantizationInfo qinfo_out, 59 ActivationLayerInfo act_info, 60 bool is_inplace) 61 { 62 _is_inplace = is_inplace; 63 _target = compute_target(shape0, shape1, dt_in1, dt_in2, dt_out, scale, convert_policy, rounding_policy, qinfo0, qinfo1, qinfo_out, act_info); 64 _reference = compute_reference(shape0, shape1, dt_in1, dt_in2, dt_out, scale, convert_policy, rounding_policy, qinfo0, qinfo1, qinfo_out, act_info); 65 } 66 67 protected: 68 template <typename U> fill(U && tensor,unsigned int seed_offset)69 void fill(U &&tensor, unsigned int seed_offset) 70 { 71 library->fill_tensor_uniform(tensor, seed_offset); 72 } 73 compute_target(const TensorShape & shape0,const TensorShape & shape1,DataType dt_in1,DataType dt_in2,DataType dt_out,float scale,ConvertPolicy convert_policy,RoundingPolicy rounding_policy,QuantizationInfo qinfo0,QuantizationInfo qinfo1,QuantizationInfo qinfo_out,ActivationLayerInfo act_info)74 TensorType compute_target(const TensorShape &shape0, const TensorShape &shape1, DataType dt_in1, DataType dt_in2, DataType dt_out, 75 float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy, 76 QuantizationInfo qinfo0, QuantizationInfo qinfo1, QuantizationInfo qinfo_out, ActivationLayerInfo act_info) 77 { 78 // Create tensors 79 TensorType src1 = create_tensor<TensorType>(shape0, dt_in1, 1, qinfo0); 80 TensorType src2 = create_tensor<TensorType>(shape1, dt_in2, 1, qinfo1); 81 TensorType dst = create_tensor<TensorType>(TensorShape::broadcast_shape(shape0, shape1), dt_out, 1, qinfo_out); 82 83 auto allocate_tensor = [](TensorType & t) 84 { 85 ARM_COMPUTE_EXPECT(t.info()->is_resizable(), framework::LogLevel::ERRORS); 86 t.allocator()->allocate(); 87 ARM_COMPUTE_EXPECT(!t.info()->is_resizable(), framework::LogLevel::ERRORS); 88 }; 89 90 // Create and configure function 91 FunctionType multiply; 92 multiply.configure(&src1, &src2, (_is_inplace ? &src1 : &dst), scale, convert_policy, rounding_policy, act_info); 93 94 allocate_tensor(src1); 95 allocate_tensor(src2); 96 97 if(!_is_inplace) 98 { 99 allocate_tensor(dst); 100 } 101 102 // Fill tensors 103 fill(AccessorType(src1), 0); 104 fill(AccessorType(src2), 1); 105 106 // Compute function 107 multiply.run(); 108 109 if(_is_inplace) 110 { 111 return src1; 112 } 113 114 return dst; 115 } 116 compute_reference(const TensorShape & shape0,const TensorShape & shape1,DataType dt_in1,DataType dt_in2,DataType dt_out,float scale,ConvertPolicy convert_policy,RoundingPolicy rounding_policy,QuantizationInfo qinfo0,QuantizationInfo qinfo1,QuantizationInfo qinfo_out,ActivationLayerInfo act_info)117 SimpleTensor<T3> compute_reference(const TensorShape &shape0, const TensorShape &shape1, DataType dt_in1, DataType dt_in2, DataType dt_out, 118 float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy, 119 QuantizationInfo qinfo0, QuantizationInfo qinfo1, QuantizationInfo qinfo_out, ActivationLayerInfo act_info) 120 { 121 // Create reference 122 SimpleTensor<T1> src1{ shape0, dt_in1, 1, qinfo0 }; 123 SimpleTensor<T2> src2{ shape1, dt_in2, 1, qinfo1 }; 124 125 // current in-place implementation only supports same metadata of input and output tensors. 126 // By ignoring output quantization information here, we can make test cases implementation much simpler. 127 QuantizationInfo output_qinfo = _is_inplace ? qinfo0 : qinfo_out; 128 129 // Fill reference 130 fill(src1, 0); 131 fill(src2, 1); 132 133 auto result = reference::pixel_wise_multiplication<T1, T2, T3>(src1, src2, scale, convert_policy, rounding_policy, dt_out, output_qinfo); 134 return act_info.enabled() ? reference::activation_layer(result, act_info, output_qinfo) : result; 135 } 136 137 TensorType _target{}; 138 SimpleTensor<T3> _reference{}; 139 bool _is_inplace{ false }; 140 }; 141 142 template <typename TensorType, typename AccessorType, typename FunctionType, typename T1, typename T2, typename T3 = T2> 143 class PixelWiseMultiplicationValidationFixture : public PixelWiseMultiplicationGenericValidationFixture<TensorType, AccessorType, FunctionType, T1, T2, T3> 144 { 145 public: 146 template <typename...> setup(const TensorShape & shape,DataType dt_in1,DataType dt_in2,DataType dt_out,float scale,ConvertPolicy convert_policy,RoundingPolicy rounding_policy,bool is_inplace)147 void setup(const TensorShape &shape, DataType dt_in1, DataType dt_in2, DataType dt_out, float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy, bool is_inplace) 148 { 149 PixelWiseMultiplicationGenericValidationFixture<TensorType, AccessorType, FunctionType, T1, T2, T3>::setup(shape, shape, dt_in1, dt_in2, dt_out, scale, convert_policy, rounding_policy, 150 QuantizationInfo(), QuantizationInfo(), QuantizationInfo(), ActivationLayerInfo(), is_inplace); 151 } 152 }; 153 154 template <typename TensorType, typename AccessorType, typename FunctionType, typename T1, typename T2, typename T3 = T2> 155 class PixelWiseMultiplicationBroadcastValidationFixture : public PixelWiseMultiplicationGenericValidationFixture<TensorType, AccessorType, FunctionType, T1, T2, T3> 156 { 157 public: 158 template <typename...> setup(const TensorShape & shape0,const TensorShape & shape1,DataType dt_in1,DataType dt_in2,DataType dt_out,float scale,ConvertPolicy convert_policy,RoundingPolicy rounding_policy,bool is_inplace)159 void setup(const TensorShape &shape0, const TensorShape &shape1, DataType dt_in1, DataType dt_in2, DataType dt_out, float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy, 160 bool is_inplace) 161 { 162 PixelWiseMultiplicationGenericValidationFixture<TensorType, AccessorType, FunctionType, T1, T2, T3>::setup(shape0, shape1, dt_in1, dt_in2, dt_out, scale, convert_policy, rounding_policy, 163 QuantizationInfo(), QuantizationInfo(), QuantizationInfo(), ActivationLayerInfo(), is_inplace); 164 } 165 }; 166 167 template <typename TensorType, typename AccessorType, typename FunctionType, typename T1, typename T2> 168 class PixelWiseMultiplicationValidationFloatFixture : public PixelWiseMultiplicationGenericValidationFixture<TensorType, AccessorType, FunctionType, T1, T2> 169 { 170 public: 171 template <typename...> setup(const TensorShape & shape,DataType dt_in1,DataType dt_in2,float scale,ConvertPolicy convert_policy,RoundingPolicy rounding_policy,ActivationLayerInfo act_info,bool is_inplace)172 void setup(const TensorShape &shape, DataType dt_in1, DataType dt_in2, float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy, ActivationLayerInfo act_info, bool is_inplace) 173 { 174 PixelWiseMultiplicationGenericValidationFixture<TensorType, AccessorType, FunctionType, T1, T2>::setup(shape, shape, dt_in1, dt_in2, dt_in2, scale, convert_policy, rounding_policy, 175 QuantizationInfo(), QuantizationInfo(), QuantizationInfo(), act_info, is_inplace); 176 } 177 }; 178 179 template <typename TensorType, typename AccessorType, typename FunctionType, typename T1, typename T2> 180 class PixelWiseMultiplicationBroadcastValidationFloatFixture : public PixelWiseMultiplicationGenericValidationFixture<TensorType, AccessorType, FunctionType, T1, T2> 181 { 182 public: 183 template <typename...> setup(const TensorShape & shape0,const TensorShape & shape1,DataType dt_in1,DataType dt_in2,float scale,ConvertPolicy convert_policy,RoundingPolicy rounding_policy,ActivationLayerInfo act_info,bool is_inplace)184 void setup(const TensorShape &shape0, const TensorShape &shape1, DataType dt_in1, DataType dt_in2, float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy, 185 ActivationLayerInfo act_info, bool is_inplace) 186 { 187 PixelWiseMultiplicationGenericValidationFixture<TensorType, AccessorType, FunctionType, T1, T2>::setup(shape0, shape1, dt_in1, dt_in2, dt_in2, scale, convert_policy, rounding_policy, 188 QuantizationInfo(), QuantizationInfo(), QuantizationInfo(), act_info, is_inplace); 189 } 190 }; 191 192 template <typename TensorType, typename AccessorType, typename FunctionType, typename T1, typename T2, typename T3 = T2> 193 class PixelWiseMultiplicationValidationQuantizedFixture : public PixelWiseMultiplicationGenericValidationFixture<TensorType, AccessorType, FunctionType, T1, T2, T3> 194 { 195 public: 196 template <typename...> setup(const TensorShape & shape,DataType dt_in1,DataType dt_in2,DataType dt_out,float scale,ConvertPolicy convert_policy,RoundingPolicy rounding_policy,QuantizationInfo qinfo0,QuantizationInfo qinfo1,QuantizationInfo qinfo_out,bool is_inplace)197 void setup(const TensorShape &shape, DataType dt_in1, DataType dt_in2, DataType dt_out, float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy, 198 QuantizationInfo qinfo0, QuantizationInfo qinfo1, QuantizationInfo qinfo_out, bool is_inplace) 199 { 200 PixelWiseMultiplicationGenericValidationFixture<TensorType, AccessorType, FunctionType, T1, T2, T3>::setup(shape, shape, dt_in1, dt_in2, dt_out, scale, convert_policy, rounding_policy, 201 qinfo0, qinfo1, qinfo_out, ActivationLayerInfo(), is_inplace); 202 } 203 }; 204 205 template <typename TensorType, typename AccessorType, typename FunctionType, typename T1, typename T2, typename T3 = T2> 206 class PixelWiseMultiplicationBroadcastValidationQuantizedFixture : public PixelWiseMultiplicationGenericValidationFixture<TensorType, AccessorType, FunctionType, T1, T2, T3> 207 { 208 public: 209 template <typename...> setup(const TensorShape & shape0,const TensorShape & shape1,DataType dt_in1,DataType dt_in2,DataType dt_out,float scale,ConvertPolicy convert_policy,RoundingPolicy rounding_policy,QuantizationInfo qinfo0,QuantizationInfo qinfo1,QuantizationInfo qinfo_out,bool is_inplace)210 void setup(const TensorShape &shape0, const TensorShape &shape1, DataType dt_in1, DataType dt_in2, DataType dt_out, float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy, 211 QuantizationInfo qinfo0, QuantizationInfo qinfo1, QuantizationInfo qinfo_out, bool is_inplace) 212 { 213 PixelWiseMultiplicationGenericValidationFixture<TensorType, AccessorType, FunctionType, T1, T2, T3>::setup(shape0, shape1, dt_in1, dt_in2, dt_out, scale, convert_policy, rounding_policy, 214 qinfo0, qinfo1, qinfo_out, ActivationLayerInfo(), is_inplace); 215 } 216 }; 217 } // namespace validation 218 } // namespace test 219 } // namespace arm_compute 220 #endif /* ARM_COMPUTE_TEST_PIXEL_WISE_MULTIPLICATION_FIXTURE */ 221