1*c217d954SCole Faust /* 2*c217d954SCole Faust * Copyright (c) 2018-2021 Arm Limited. 3*c217d954SCole Faust * 4*c217d954SCole Faust * SPDX-License-Identifier: MIT 5*c217d954SCole Faust * 6*c217d954SCole Faust * Permission is hereby granted, free of charge, to any person obtaining a copy 7*c217d954SCole Faust * of this software and associated documentation files (the "Software"), to 8*c217d954SCole Faust * deal in the Software without restriction, including without limitation the 9*c217d954SCole Faust * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 10*c217d954SCole Faust * sell copies of the Software, and to permit persons to whom the Software is 11*c217d954SCole Faust * furnished to do so, subject to the following conditions: 12*c217d954SCole Faust * 13*c217d954SCole Faust * The above copyright notice and this permission notice shall be included in all 14*c217d954SCole Faust * copies or substantial portions of the Software. 15*c217d954SCole Faust * 16*c217d954SCole Faust * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17*c217d954SCole Faust * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18*c217d954SCole Faust * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19*c217d954SCole Faust * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20*c217d954SCole Faust * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21*c217d954SCole Faust * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22*c217d954SCole Faust * SOFTWARE. 23*c217d954SCole Faust */ 24*c217d954SCole Faust #ifndef ARM_COMPUTE_TEST_WINOGRAD_LAYER_FIXTURE 25*c217d954SCole Faust #define ARM_COMPUTE_TEST_WINOGRAD_LAYER_FIXTURE 26*c217d954SCole Faust 27*c217d954SCole Faust #include "arm_compute/core/TensorShape.h" 28*c217d954SCole Faust #include "arm_compute/core/Types.h" 29*c217d954SCole Faust #include "arm_compute/core/utils/misc/ShapeCalculator.h" 30*c217d954SCole Faust #include "tests/AssetsLibrary.h" 31*c217d954SCole Faust #include "tests/Globals.h" 32*c217d954SCole Faust #include "tests/IAccessor.h" 33*c217d954SCole Faust #include "tests/framework/Asserts.h" 34*c217d954SCole Faust #include "tests/framework/Fixture.h" 35*c217d954SCole Faust #include "tests/validation/Helpers.h" 36*c217d954SCole Faust #include "tests/validation/reference/ActivationLayer.h" 37*c217d954SCole Faust #include "tests/validation/reference/ConvolutionLayer.h" 38*c217d954SCole Faust #include "tests/validation/reference/GEMM.h" 39*c217d954SCole Faust #include "tests/validation/reference/Permute.h" 40*c217d954SCole Faust #include "tests/validation/reference/Utils.h" 41*c217d954SCole Faust #include "tests/validation/reference/Winograd.h" 42*c217d954SCole Faust #include "utils/Utils.h" 43*c217d954SCole Faust 44*c217d954SCole Faust #include <random> 45*c217d954SCole Faust 46*c217d954SCole Faust namespace arm_compute 47*c217d954SCole Faust { 48*c217d954SCole Faust namespace test 49*c217d954SCole Faust { 50*c217d954SCole Faust namespace validation 51*c217d954SCole Faust { 52*c217d954SCole Faust using namespace arm_compute::misc::shape_calculator; 53*c217d954SCole Faust 54*c217d954SCole Faust template <typename TensorType, typename AccessorType, typename FunctionType, typename T, typename T1 = T, bool use_bias = true, bool mixed_layout = false> 55*c217d954SCole Faust class WinogradConvolutionLayerFastMathValidationFixture : public framework::Fixture 56*c217d954SCole Faust { 57*c217d954SCole Faust public: 58*c217d954SCole Faust template <typename...> setup(TensorShape input_shape,TensorShape weights_shape,TensorShape bias_shape,TensorShape output_shape,PadStrideInfo info,Size2D dilation,DataType data_type,ActivationLayerInfo act_info,const DataLayout & data_layout)59*c217d954SCole Faust void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, PadStrideInfo info, Size2D dilation, 60*c217d954SCole Faust DataType data_type, ActivationLayerInfo act_info, const DataLayout &data_layout) 61*c217d954SCole Faust 62*c217d954SCole Faust { 63*c217d954SCole Faust ARM_COMPUTE_UNUSED(dilation); 64*c217d954SCole Faust _mixed_layout = mixed_layout; 65*c217d954SCole Faust _target = compute_target(input_shape, weights_shape, bias_shape, output_shape, info, data_type, act_info, data_layout); 66*c217d954SCole Faust _reference = compute_reference(input_shape, weights_shape, bias_shape, info, data_type, act_info); 67*c217d954SCole Faust } 68*c217d954SCole Faust 69*c217d954SCole Faust protected: mix_layout(FunctionType & layer,TensorType & src,TensorType & dst)70*c217d954SCole Faust void mix_layout(FunctionType &layer, TensorType &src, TensorType &dst) 71*c217d954SCole Faust { 72*c217d954SCole Faust const DataLayout data_layout = src.info()->data_layout(); 73*c217d954SCole Faust // Test Multi DataLayout graph cases, when the data layout changes after configure 74*c217d954SCole Faust src.info()->set_data_layout(data_layout == DataLayout::NCHW ? DataLayout::NHWC : DataLayout::NCHW); 75*c217d954SCole Faust dst.info()->set_data_layout(data_layout == DataLayout::NCHW ? DataLayout::NHWC : DataLayout::NCHW); 76*c217d954SCole Faust 77*c217d954SCole Faust // Compute Convolution function 78*c217d954SCole Faust layer.run(); 79*c217d954SCole Faust 80*c217d954SCole Faust // Reinstating original data layout for the test suite to properly check the values 81*c217d954SCole Faust src.info()->set_data_layout(data_layout); 82*c217d954SCole Faust dst.info()->set_data_layout(data_layout); 83*c217d954SCole Faust } 84*c217d954SCole Faust 85*c217d954SCole Faust template <typename U> fill(U && tensor,int i,float min,float max)86*c217d954SCole Faust void fill(U &&tensor, int i, float min, float max) 87*c217d954SCole Faust { 88*c217d954SCole Faust switch(tensor.data_type()) 89*c217d954SCole Faust { 90*c217d954SCole Faust case DataType::F16: 91*c217d954SCole Faust { 92*c217d954SCole Faust arm_compute::utils::uniform_real_distribution_16bit<half> distribution{ float(min), float(max) }; 93*c217d954SCole Faust library->fill(tensor, distribution, i); 94*c217d954SCole Faust break; 95*c217d954SCole Faust } 96*c217d954SCole Faust case DataType::F32: 97*c217d954SCole Faust { 98*c217d954SCole Faust std::uniform_real_distribution<float> distribution(min, max); 99*c217d954SCole Faust library->fill(tensor, distribution, i); 100*c217d954SCole Faust break; 101*c217d954SCole Faust } 102*c217d954SCole Faust default: 103*c217d954SCole Faust { 104*c217d954SCole Faust ARM_COMPUTE_ERROR("Not supported"); 105*c217d954SCole Faust } 106*c217d954SCole Faust } 107*c217d954SCole Faust } 108*c217d954SCole Faust compute_target(TensorShape input_shape,TensorShape weights_shape,TensorShape bias_shape,TensorShape output_shape,const PadStrideInfo & info,DataType data_type,ActivationLayerInfo act_info,const DataLayout data_layout)109*c217d954SCole Faust TensorType compute_target(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, const PadStrideInfo &info, 110*c217d954SCole Faust DataType data_type, ActivationLayerInfo act_info, const DataLayout data_layout) 111*c217d954SCole Faust { 112*c217d954SCole Faust if(data_layout == DataLayout::NHWC) 113*c217d954SCole Faust { 114*c217d954SCole Faust permute(input_shape, PermutationVector(2U, 0U, 1U)); 115*c217d954SCole Faust permute(weights_shape, PermutationVector(2U, 0U, 1U)); 116*c217d954SCole Faust permute(output_shape, PermutationVector(2U, 0U, 1U)); 117*c217d954SCole Faust } 118*c217d954SCole Faust 119*c217d954SCole Faust // Create tensors 120*c217d954SCole Faust TensorType src = create_tensor<TensorType>(input_shape, data_type, 1, QuantizationInfo(), data_layout); 121*c217d954SCole Faust TensorType weights = create_tensor<TensorType>(weights_shape, data_type, 1, QuantizationInfo(), data_layout); 122*c217d954SCole Faust TensorType bias = create_tensor<TensorType>(bias_shape, data_type, 1, QuantizationInfo(), data_layout); 123*c217d954SCole Faust TensorType dst = create_tensor<TensorType>(output_shape, data_type, 1, QuantizationInfo(), data_layout); 124*c217d954SCole Faust 125*c217d954SCole Faust // Create and configure function 126*c217d954SCole Faust FunctionType conv; 127*c217d954SCole Faust ARM_COMPUTE_EXPECT(static_cast<bool>(conv.validate(src.info(), weights.info(), (use_bias) ? bias.info() : nullptr, dst.info(), info, act_info, true /* Enable fast math */)), 128*c217d954SCole Faust framework::LogLevel::ERRORS); 129*c217d954SCole Faust conv.configure(&src, &weights, (use_bias) ? &bias : nullptr, &dst, info, act_info, true /* Enable fast math */); 130*c217d954SCole Faust 131*c217d954SCole Faust ARM_COMPUTE_ASSERT(src.info()->is_resizable()); 132*c217d954SCole Faust ARM_COMPUTE_ASSERT(weights.info()->is_resizable()); 133*c217d954SCole Faust ARM_COMPUTE_ASSERT(bias.info()->is_resizable()); 134*c217d954SCole Faust ARM_COMPUTE_ASSERT(dst.info()->is_resizable()); 135*c217d954SCole Faust 136*c217d954SCole Faust add_padding_x({ &src, &weights, &bias, &dst }, data_layout); 137*c217d954SCole Faust 138*c217d954SCole Faust // Allocate tensors 139*c217d954SCole Faust src.allocator()->allocate(); 140*c217d954SCole Faust weights.allocator()->allocate(); 141*c217d954SCole Faust dst.allocator()->allocate(); 142*c217d954SCole Faust bias.allocator()->allocate(); 143*c217d954SCole Faust 144*c217d954SCole Faust ARM_COMPUTE_ASSERT(!src.info()->is_resizable()); 145*c217d954SCole Faust ARM_COMPUTE_ASSERT(!weights.info()->is_resizable()); 146*c217d954SCole Faust ARM_COMPUTE_ASSERT(!bias.info()->is_resizable()); 147*c217d954SCole Faust ARM_COMPUTE_ASSERT(!dst.info()->is_resizable()); 148*c217d954SCole Faust 149*c217d954SCole Faust // Fill tensors 150*c217d954SCole Faust fill(AccessorType(src), 0, -0.5f, 0.5f); 151*c217d954SCole Faust fill(AccessorType(weights), 1, -0.5f, 0.5f); 152*c217d954SCole Faust fill(AccessorType(bias), 2, -0.5f, 0.5f); 153*c217d954SCole Faust 154*c217d954SCole Faust if(_mixed_layout) 155*c217d954SCole Faust { 156*c217d954SCole Faust mix_layout(conv, src, dst); 157*c217d954SCole Faust } 158*c217d954SCole Faust else 159*c217d954SCole Faust { 160*c217d954SCole Faust // Compute function 161*c217d954SCole Faust conv.run(); 162*c217d954SCole Faust } 163*c217d954SCole Faust return dst; 164*c217d954SCole Faust } 165*c217d954SCole Faust compute_reference(const TensorShape & input_shape,const TensorShape & weights_shape,const TensorShape & bias_shape,const PadStrideInfo & info,DataType data_type,ActivationLayerInfo act_info)166*c217d954SCole Faust SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const PadStrideInfo &info, 167*c217d954SCole Faust DataType data_type, ActivationLayerInfo act_info) 168*c217d954SCole Faust { 169*c217d954SCole Faust // Create reference 170*c217d954SCole Faust SimpleTensor<T> src_t{ input_shape, data_type, 1 }; 171*c217d954SCole Faust SimpleTensor<T> weights_t{ weights_shape, data_type, 1 }; 172*c217d954SCole Faust SimpleTensor<T> bias_t{ bias_shape, data_type, 1 }; 173*c217d954SCole Faust 174*c217d954SCole Faust // Fill reference 175*c217d954SCole Faust fill(src_t, 0, -0.5f, 0.5f); 176*c217d954SCole Faust SimpleTensor<T1> src_t1(copy_tensor<T1, T>(src_t)); 177*c217d954SCole Faust 178*c217d954SCole Faust fill(weights_t, 1, -0.5f, 0.5f); 179*c217d954SCole Faust SimpleTensor<T1> weights_t1(copy_tensor<T1, T>(weights_t)); 180*c217d954SCole Faust if(use_bias) 181*c217d954SCole Faust { 182*c217d954SCole Faust fill(bias_t, 2, -0.5f, 0.5f); 183*c217d954SCole Faust } 184*c217d954SCole Faust else 185*c217d954SCole Faust { 186*c217d954SCole Faust fill(bias_t, 2, 0.f, 0.f); 187*c217d954SCole Faust } 188*c217d954SCole Faust SimpleTensor<T1> bias_t1(copy_tensor<T1, T>(bias_t)); 189*c217d954SCole Faust 190*c217d954SCole Faust // Set output tile 191*c217d954SCole Faust Size2D output_tile(4U, 4U); 192*c217d954SCole Faust if(weights_shape[0] == 7 && weights_shape[1] == 1) 193*c217d954SCole Faust { 194*c217d954SCole Faust output_tile.width = 2; 195*c217d954SCole Faust output_tile.height = 1; 196*c217d954SCole Faust } 197*c217d954SCole Faust else if(weights_shape[0] == 1 && weights_shape[1] == 7) 198*c217d954SCole Faust { 199*c217d954SCole Faust output_tile.width = 1; 200*c217d954SCole Faust output_tile.height = 2; 201*c217d954SCole Faust } 202*c217d954SCole Faust else if(weights_shape[0] == 1) 203*c217d954SCole Faust { 204*c217d954SCole Faust output_tile.width = 1; 205*c217d954SCole Faust } 206*c217d954SCole Faust else if(weights_shape[1] == 1) 207*c217d954SCole Faust { 208*c217d954SCole Faust output_tile.height = 1; 209*c217d954SCole Faust } 210*c217d954SCole Faust 211*c217d954SCole Faust WinogradInfo winograd_info(output_tile, 212*c217d954SCole Faust Size2D(weights_shape[0], weights_shape[1]), 213*c217d954SCole Faust Size2D(input_shape[0], input_shape[1]), 214*c217d954SCole Faust info, 215*c217d954SCole Faust src_t1.data_layout()); 216*c217d954SCole Faust 217*c217d954SCole Faust // Compute tensor shapes for input, filter and output transforms 218*c217d954SCole Faust TensorShape input_transform_shape = compute_winograd_input_transform_shape(TensorInfo(input_shape, 1, data_type), winograd_info); 219*c217d954SCole Faust TensorShape filter_transform_shape = compute_winograd_filter_transform_shape(TensorInfo(weights_shape, 1, data_type), winograd_info); 220*c217d954SCole Faust TensorShape batched_gemm_shape = input_transform_shape; 221*c217d954SCole Faust batched_gemm_shape[0] = filter_transform_shape[0]; 222*c217d954SCole Faust TensorShape output_transform_shape = compute_winograd_output_transform_shape(TensorInfo(batched_gemm_shape, 1, data_type), winograd_info); 223*c217d954SCole Faust 224*c217d954SCole Faust // Dummy matrix C to perform matrix multiplication 225*c217d954SCole Faust SimpleTensor<T1> dummy_c{ batched_gemm_shape, data_type, 1 }; 226*c217d954SCole Faust 227*c217d954SCole Faust // Compute Winograd-based convolution 228*c217d954SCole Faust SimpleTensor<T1> input_transform_out = reference::winograd_input_transform<T1>(src_t1, input_transform_shape, winograd_info); 229*c217d954SCole Faust 230*c217d954SCole Faust SimpleTensor<T1> filter_transform_out = reference::winograd_filter_transform<T1>(weights_t1, filter_transform_shape, winograd_info); 231*c217d954SCole Faust SimpleTensor<T1> batched_gemm = reference::gemm<T1>(input_transform_out, filter_transform_out, dummy_c, 1.0f, 0.0f); 232*c217d954SCole Faust SimpleTensor<T1> conv_out = reference::winograd_output_transform<T1>(batched_gemm, bias_t1, output_transform_shape, winograd_info); 233*c217d954SCole Faust SimpleTensor<T> conv_out_t(std::move(copy_tensor<T, T1>(conv_out))); 234*c217d954SCole Faust return (act_info.enabled()) ? reference::activation_layer<T>(conv_out_t, act_info) : conv_out_t; 235*c217d954SCole Faust } 236*c217d954SCole Faust 237*c217d954SCole Faust TensorType _target{}; 238*c217d954SCole Faust SimpleTensor<T> _reference{}; 239*c217d954SCole Faust bool _mixed_layout{ false }; 240*c217d954SCole Faust }; 241*c217d954SCole Faust 242*c217d954SCole Faust template <typename TensorType, typename AccessorType, typename FunctionType, typename T, bool mixed_layout = false> 243*c217d954SCole Faust class WinogradInputTransformValidationFixture : public framework::Fixture 244*c217d954SCole Faust { 245*c217d954SCole Faust public: 246*c217d954SCole Faust template <typename...> setup(TensorShape input_shape,WinogradInfo winograd_info,DataLayout data_layout,DataType data_type)247*c217d954SCole Faust void setup(TensorShape input_shape, WinogradInfo winograd_info, DataLayout data_layout, DataType data_type) 248*c217d954SCole Faust { 249*c217d954SCole Faust TensorShape output_shape = compute_winograd_input_transform_shape(TensorInfo(input_shape, 1, data_type), winograd_info); 250*c217d954SCole Faust _mixed_layout = mixed_layout; 251*c217d954SCole Faust _target = compute_target(input_shape, output_shape, winograd_info, data_layout, data_type); 252*c217d954SCole Faust _reference = compute_reference(input_shape, output_shape, winograd_info, data_type); 253*c217d954SCole Faust } 254*c217d954SCole Faust 255*c217d954SCole Faust protected: mix_layout(FunctionType & layer,TensorType & src,TensorType & dst)256*c217d954SCole Faust void mix_layout(FunctionType &layer, TensorType &src, TensorType &dst) 257*c217d954SCole Faust { 258*c217d954SCole Faust const DataLayout data_layout_src = src.info()->data_layout(); 259*c217d954SCole Faust const DataLayout data_layout_dst = dst.info()->data_layout(); 260*c217d954SCole Faust 261*c217d954SCole Faust // Test Multi DataLayout graph cases, when the data layout changes after configure 262*c217d954SCole Faust src.info()->set_data_layout(data_layout_src == DataLayout::NCHW ? DataLayout::NHWC : DataLayout::NCHW); 263*c217d954SCole Faust dst.info()->set_data_layout(data_layout_dst == DataLayout::NCHW ? DataLayout::NHWC : DataLayout::NCHW); 264*c217d954SCole Faust 265*c217d954SCole Faust // Compute Convolution function 266*c217d954SCole Faust layer.run(); 267*c217d954SCole Faust 268*c217d954SCole Faust // Reinstating original data layout for the test suite to properly check the values 269*c217d954SCole Faust src.info()->set_data_layout(data_layout_src); 270*c217d954SCole Faust dst.info()->set_data_layout(data_layout_dst); 271*c217d954SCole Faust } 272*c217d954SCole Faust 273*c217d954SCole Faust template <typename U> fill(U && tensor,int i,float min,float max)274*c217d954SCole Faust void fill(U &&tensor, int i, float min, float max) 275*c217d954SCole Faust { 276*c217d954SCole Faust switch(tensor.data_type()) 277*c217d954SCole Faust { 278*c217d954SCole Faust case DataType::F16: 279*c217d954SCole Faust { 280*c217d954SCole Faust arm_compute::utils::uniform_real_distribution_16bit<half> distribution{ float(min), float(max) }; 281*c217d954SCole Faust library->fill(tensor, distribution, i); 282*c217d954SCole Faust break; 283*c217d954SCole Faust } 284*c217d954SCole Faust case DataType::F32: 285*c217d954SCole Faust { 286*c217d954SCole Faust std::uniform_real_distribution<float> distribution(min, max); 287*c217d954SCole Faust library->fill(tensor, distribution, i); 288*c217d954SCole Faust break; 289*c217d954SCole Faust } 290*c217d954SCole Faust default: 291*c217d954SCole Faust { 292*c217d954SCole Faust ARM_COMPUTE_ERROR("Not supported"); 293*c217d954SCole Faust } 294*c217d954SCole Faust } 295*c217d954SCole Faust } 296*c217d954SCole Faust compute_target(TensorShape input_shape,const TensorShape & output_shape,const WinogradInfo & winograd_info,DataLayout data_layout,DataType data_type)297*c217d954SCole Faust TensorType compute_target(TensorShape input_shape, const TensorShape &output_shape, const WinogradInfo &winograd_info, DataLayout data_layout, DataType data_type) 298*c217d954SCole Faust { 299*c217d954SCole Faust if(data_layout == DataLayout::NHWC) 300*c217d954SCole Faust { 301*c217d954SCole Faust permute(input_shape, PermutationVector(2U, 0U, 1U)); 302*c217d954SCole Faust } 303*c217d954SCole Faust 304*c217d954SCole Faust TensorType src = create_tensor<TensorType>(input_shape, data_type, 1, QuantizationInfo(), data_layout); 305*c217d954SCole Faust TensorType dst = create_tensor<TensorType>(output_shape, data_type, 1, QuantizationInfo()); 306*c217d954SCole Faust 307*c217d954SCole Faust // Create and configure function 308*c217d954SCole Faust FunctionType transf; 309*c217d954SCole Faust transf.configure(&src, &dst, winograd_info); 310*c217d954SCole Faust 311*c217d954SCole Faust ARM_COMPUTE_ASSERT(src.info()->is_resizable()); 312*c217d954SCole Faust ARM_COMPUTE_ASSERT(dst.info()->is_resizable()); 313*c217d954SCole Faust 314*c217d954SCole Faust add_padding_x({ &src, &dst }, data_layout); 315*c217d954SCole Faust 316*c217d954SCole Faust // Allocate tensors 317*c217d954SCole Faust src.allocator()->allocate(); 318*c217d954SCole Faust dst.allocator()->allocate(); 319*c217d954SCole Faust 320*c217d954SCole Faust ARM_COMPUTE_ASSERT(!src.info()->is_resizable()); 321*c217d954SCole Faust ARM_COMPUTE_ASSERT(!dst.info()->is_resizable()); 322*c217d954SCole Faust 323*c217d954SCole Faust // Fill tensors 324*c217d954SCole Faust fill(AccessorType(src), 0, -1.f, 1.f); 325*c217d954SCole Faust 326*c217d954SCole Faust if(_mixed_layout) 327*c217d954SCole Faust { 328*c217d954SCole Faust mix_layout(transf, src, dst); 329*c217d954SCole Faust } 330*c217d954SCole Faust else 331*c217d954SCole Faust { 332*c217d954SCole Faust // Compute Winograd input transform function 333*c217d954SCole Faust transf.run(); 334*c217d954SCole Faust } 335*c217d954SCole Faust return dst; 336*c217d954SCole Faust } 337*c217d954SCole Faust compute_reference(const TensorShape & input_shape,const TensorShape & output_shape,const WinogradInfo & winograd_info,DataType data_type)338*c217d954SCole Faust SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &output_shape, const WinogradInfo &winograd_info, DataType data_type) 339*c217d954SCole Faust { 340*c217d954SCole Faust // Create reference 341*c217d954SCole Faust SimpleTensor<T> src{ input_shape, data_type, 1, QuantizationInfo() }; 342*c217d954SCole Faust 343*c217d954SCole Faust // Fill reference 344*c217d954SCole Faust fill(src, 0, -1.f, 1.f); 345*c217d954SCole Faust 346*c217d954SCole Faust return reference::winograd_input_transform<T>(src, output_shape, winograd_info); 347*c217d954SCole Faust } 348*c217d954SCole Faust 349*c217d954SCole Faust bool _mixed_layout{ false }; 350*c217d954SCole Faust TensorType _target{}; 351*c217d954SCole Faust SimpleTensor<T> _reference{}; 352*c217d954SCole Faust }; 353*c217d954SCole Faust 354*c217d954SCole Faust template <typename TensorType, typename AccessorType, typename FunctionType, typename T, bool mixed_layout = false> 355*c217d954SCole Faust class WinogradFilterTransformValidationFixture : public framework::Fixture 356*c217d954SCole Faust { 357*c217d954SCole Faust public: 358*c217d954SCole Faust template <typename...> setup(TensorShape input_shape,Size2D output_tile,DataLayout data_layout,DataType data_type)359*c217d954SCole Faust void setup(TensorShape input_shape, Size2D output_tile, DataLayout data_layout, DataType data_type) 360*c217d954SCole Faust { 361*c217d954SCole Faust WinogradInfo winograd_info(output_tile, Size2D(input_shape[0], input_shape[1]), Size2D() /* Not needed */, PadStrideInfo() /* Not needed */, DataLayout::NCHW /* Not needed */); 362*c217d954SCole Faust TensorShape output_shape = compute_winograd_filter_transform_shape(TensorInfo(input_shape, 1, data_type), winograd_info); 363*c217d954SCole Faust 364*c217d954SCole Faust _mixed_layout = mixed_layout; 365*c217d954SCole Faust _target = compute_target(input_shape, output_shape, winograd_info, data_layout, data_type); 366*c217d954SCole Faust _reference = compute_reference(input_shape, output_shape, winograd_info, data_type); 367*c217d954SCole Faust } 368*c217d954SCole Faust 369*c217d954SCole Faust protected: mix_layout(FunctionType & layer,TensorType & src,TensorType & dst)370*c217d954SCole Faust void mix_layout(FunctionType &layer, TensorType &src, TensorType &dst) 371*c217d954SCole Faust { 372*c217d954SCole Faust const DataLayout data_layout_src = src.info()->data_layout(); 373*c217d954SCole Faust const DataLayout data_layout_dst = dst.info()->data_layout(); 374*c217d954SCole Faust 375*c217d954SCole Faust // Test Multi DataLayout graph cases, when the data layout changes after configure 376*c217d954SCole Faust src.info()->set_data_layout(data_layout_src == DataLayout::NCHW ? DataLayout::NHWC : DataLayout::NCHW); 377*c217d954SCole Faust dst.info()->set_data_layout(data_layout_dst == DataLayout::NCHW ? DataLayout::NHWC : DataLayout::NCHW); 378*c217d954SCole Faust 379*c217d954SCole Faust // Compute Convolution function 380*c217d954SCole Faust layer.run(); 381*c217d954SCole Faust 382*c217d954SCole Faust // Reinstating original data layout for the test suite to properly check the values 383*c217d954SCole Faust src.info()->set_data_layout(data_layout_src); 384*c217d954SCole Faust dst.info()->set_data_layout(data_layout_dst); 385*c217d954SCole Faust } 386*c217d954SCole Faust 387*c217d954SCole Faust template <typename U> fill(U && tensor,int i,float min,float max)388*c217d954SCole Faust void fill(U &&tensor, int i, float min, float max) 389*c217d954SCole Faust { 390*c217d954SCole Faust switch(tensor.data_type()) 391*c217d954SCole Faust { 392*c217d954SCole Faust case DataType::F16: 393*c217d954SCole Faust { 394*c217d954SCole Faust arm_compute::utils::uniform_real_distribution_16bit<half> distribution{ float(min), float(max) }; 395*c217d954SCole Faust library->fill(tensor, distribution, i); 396*c217d954SCole Faust break; 397*c217d954SCole Faust } 398*c217d954SCole Faust case DataType::F32: 399*c217d954SCole Faust { 400*c217d954SCole Faust std::uniform_real_distribution<float> distribution(min, max); 401*c217d954SCole Faust library->fill(tensor, distribution, i); 402*c217d954SCole Faust break; 403*c217d954SCole Faust } 404*c217d954SCole Faust default: 405*c217d954SCole Faust { 406*c217d954SCole Faust ARM_COMPUTE_ERROR("Not supported"); 407*c217d954SCole Faust } 408*c217d954SCole Faust } 409*c217d954SCole Faust } 410*c217d954SCole Faust compute_target(TensorShape input_shape,const TensorShape & output_shape,const WinogradInfo & winograd_info,DataLayout data_layout,DataType data_type)411*c217d954SCole Faust TensorType compute_target(TensorShape input_shape, const TensorShape &output_shape, const WinogradInfo &winograd_info, DataLayout data_layout, DataType data_type) 412*c217d954SCole Faust { 413*c217d954SCole Faust if(data_layout == DataLayout::NHWC) 414*c217d954SCole Faust { 415*c217d954SCole Faust permute(input_shape, PermutationVector(2U, 0U, 1U)); 416*c217d954SCole Faust } 417*c217d954SCole Faust 418*c217d954SCole Faust // Create tensors 419*c217d954SCole Faust TensorType src = create_tensor<TensorType>(input_shape, data_type, 1, QuantizationInfo(), data_layout); 420*c217d954SCole Faust TensorType dst = create_tensor<TensorType>(output_shape, data_type, 1, QuantizationInfo()); 421*c217d954SCole Faust 422*c217d954SCole Faust // Create and configure function 423*c217d954SCole Faust FunctionType filter_transform; 424*c217d954SCole Faust filter_transform.configure(&src, &dst, winograd_info); 425*c217d954SCole Faust 426*c217d954SCole Faust ARM_COMPUTE_ASSERT(src.info()->is_resizable()); 427*c217d954SCole Faust ARM_COMPUTE_ASSERT(dst.info()->is_resizable()); 428*c217d954SCole Faust 429*c217d954SCole Faust add_padding_x({ &src, &dst }, data_layout); 430*c217d954SCole Faust 431*c217d954SCole Faust // Allocate tensors 432*c217d954SCole Faust src.allocator()->allocate(); 433*c217d954SCole Faust dst.allocator()->allocate(); 434*c217d954SCole Faust 435*c217d954SCole Faust ARM_COMPUTE_ASSERT(!src.info()->is_resizable()); 436*c217d954SCole Faust ARM_COMPUTE_ASSERT(!dst.info()->is_resizable()); 437*c217d954SCole Faust 438*c217d954SCole Faust // Fill tensors 439*c217d954SCole Faust fill(AccessorType(src), 0, -1.f, 1.f); 440*c217d954SCole Faust 441*c217d954SCole Faust if(_mixed_layout) 442*c217d954SCole Faust { 443*c217d954SCole Faust mix_layout(filter_transform, src, dst); 444*c217d954SCole Faust } 445*c217d954SCole Faust else 446*c217d954SCole Faust { 447*c217d954SCole Faust // Compute Winograd filter transform function 448*c217d954SCole Faust filter_transform.run(); 449*c217d954SCole Faust } 450*c217d954SCole Faust return dst; 451*c217d954SCole Faust } 452*c217d954SCole Faust compute_reference(const TensorShape & input_shape,const TensorShape & output_shape,const WinogradInfo & winograd_info,DataType data_type)453*c217d954SCole Faust SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &output_shape, const WinogradInfo &winograd_info, DataType data_type) 454*c217d954SCole Faust { 455*c217d954SCole Faust // Create reference 456*c217d954SCole Faust SimpleTensor<T> src{ input_shape, data_type, 1, QuantizationInfo() }; 457*c217d954SCole Faust 458*c217d954SCole Faust // Fill reference 459*c217d954SCole Faust fill(src, 0, -1.f, 1.f); 460*c217d954SCole Faust 461*c217d954SCole Faust return reference::winograd_filter_transform<T>(src, output_shape, winograd_info); 462*c217d954SCole Faust } 463*c217d954SCole Faust 464*c217d954SCole Faust bool _mixed_layout{ false }; 465*c217d954SCole Faust TensorType _target{}; 466*c217d954SCole Faust SimpleTensor<T> _reference{}; 467*c217d954SCole Faust }; 468*c217d954SCole Faust 469*c217d954SCole Faust template <typename TensorType, typename AccessorType, typename FunctionType, typename T, bool mixed_layout = false> 470*c217d954SCole Faust class WinogradOutputTransformValidationFixture : public framework::Fixture 471*c217d954SCole Faust { 472*c217d954SCole Faust public: 473*c217d954SCole Faust template <typename...> 474*c217d954SCole Faust void setup(TensorShape input_shape, WinogradInfo winograd_info, DataType data_type, ActivationLayerInfo act_info = ActivationLayerInfo()) 475*c217d954SCole Faust { 476*c217d954SCole Faust _target = compute_target(input_shape, winograd_info, data_type, act_info); 477*c217d954SCole Faust _reference = compute_reference(input_shape, winograd_info, data_type, act_info); 478*c217d954SCole Faust } 479*c217d954SCole Faust 480*c217d954SCole Faust protected: mix_layout(FunctionType & layer,TensorType & src,TensorType & dst)481*c217d954SCole Faust void mix_layout(FunctionType &layer, TensorType &src, TensorType &dst) 482*c217d954SCole Faust { 483*c217d954SCole Faust const DataLayout data_layout_src = src.info()->data_layout(); 484*c217d954SCole Faust const DataLayout data_layout_dst = dst.info()->data_layout(); 485*c217d954SCole Faust 486*c217d954SCole Faust // Test Multi DataLayout graph cases, when the data layout changes after configure 487*c217d954SCole Faust src.info()->set_data_layout(data_layout_src == DataLayout::NCHW ? DataLayout::NHWC : DataLayout::NCHW); 488*c217d954SCole Faust dst.info()->set_data_layout(data_layout_dst == DataLayout::NCHW ? DataLayout::NHWC : DataLayout::NCHW); 489*c217d954SCole Faust 490*c217d954SCole Faust // Compute Convolution function 491*c217d954SCole Faust layer.run(); 492*c217d954SCole Faust 493*c217d954SCole Faust // Reinstating original data layout for the test suite to properly check the values 494*c217d954SCole Faust src.info()->set_data_layout(data_layout_src); 495*c217d954SCole Faust dst.info()->set_data_layout(data_layout_dst); 496*c217d954SCole Faust } 497*c217d954SCole Faust 498*c217d954SCole Faust template <typename U> fill(U && tensor,int i,float min,float max)499*c217d954SCole Faust void fill(U &&tensor, int i, float min, float max) 500*c217d954SCole Faust { 501*c217d954SCole Faust switch(tensor.data_type()) 502*c217d954SCole Faust { 503*c217d954SCole Faust case DataType::F16: 504*c217d954SCole Faust { 505*c217d954SCole Faust arm_compute::utils::uniform_real_distribution_16bit<half> distribution{ float(min), float(max) }; 506*c217d954SCole Faust library->fill(tensor, distribution, i); 507*c217d954SCole Faust break; 508*c217d954SCole Faust } 509*c217d954SCole Faust case DataType::F32: 510*c217d954SCole Faust { 511*c217d954SCole Faust std::uniform_real_distribution<float> distribution(min, max); 512*c217d954SCole Faust library->fill(tensor, distribution, i); 513*c217d954SCole Faust break; 514*c217d954SCole Faust } 515*c217d954SCole Faust default: 516*c217d954SCole Faust { 517*c217d954SCole Faust ARM_COMPUTE_ERROR("Not supported"); 518*c217d954SCole Faust } 519*c217d954SCole Faust } 520*c217d954SCole Faust } 521*c217d954SCole Faust compute_target(const TensorShape & input_shape,const WinogradInfo & winograd_info,DataType data_type,ActivationLayerInfo act_info)522*c217d954SCole Faust TensorType compute_target(const TensorShape &input_shape, const WinogradInfo &winograd_info, DataType data_type, ActivationLayerInfo act_info) 523*c217d954SCole Faust { 524*c217d954SCole Faust TensorShape output_shape = compute_winograd_output_transform_shape(TensorInfo(input_shape, 1, data_type), winograd_info); 525*c217d954SCole Faust 526*c217d954SCole Faust // Create tensors 527*c217d954SCole Faust TensorType src = create_tensor<TensorType>(input_shape, data_type); 528*c217d954SCole Faust TensorType bias = create_tensor<TensorType>(output_shape[get_data_layout_dimension_index(winograd_info.output_data_layout, DataLayoutDimension::CHANNEL)], data_type); 529*c217d954SCole Faust TensorType dst = create_tensor<TensorType>(output_shape, data_type, 1, QuantizationInfo(), winograd_info.output_data_layout); 530*c217d954SCole Faust 531*c217d954SCole Faust // Create and configure function 532*c217d954SCole Faust FunctionType output_transform; 533*c217d954SCole Faust output_transform.configure(&src, &bias, &dst, winograd_info, act_info); 534*c217d954SCole Faust 535*c217d954SCole Faust ARM_COMPUTE_ASSERT(src.info()->is_resizable()); 536*c217d954SCole Faust ARM_COMPUTE_ASSERT(bias.info()->is_resizable()); 537*c217d954SCole Faust ARM_COMPUTE_ASSERT(dst.info()->is_resizable()); 538*c217d954SCole Faust 539*c217d954SCole Faust add_padding_x({ &src, &bias, &dst }, winograd_info.output_data_layout); 540*c217d954SCole Faust 541*c217d954SCole Faust // Allocate tensors 542*c217d954SCole Faust src.allocator()->allocate(); 543*c217d954SCole Faust bias.allocator()->allocate(); 544*c217d954SCole Faust dst.allocator()->allocate(); 545*c217d954SCole Faust 546*c217d954SCole Faust ARM_COMPUTE_ASSERT(!src.info()->is_resizable()); 547*c217d954SCole Faust ARM_COMPUTE_ASSERT(!bias.info()->is_resizable()); 548*c217d954SCole Faust ARM_COMPUTE_ASSERT(!dst.info()->is_resizable()); 549*c217d954SCole Faust 550*c217d954SCole Faust // Fill tensors 551*c217d954SCole Faust fill(AccessorType(src), 0, -1.f, 1.f); 552*c217d954SCole Faust fill(AccessorType(bias), 1, -1.f, 1.f); 553*c217d954SCole Faust 554*c217d954SCole Faust if(_mixed_layout) 555*c217d954SCole Faust { 556*c217d954SCole Faust mix_layout(output_transform, src, dst); 557*c217d954SCole Faust } 558*c217d954SCole Faust else 559*c217d954SCole Faust { 560*c217d954SCole Faust // Compute Winograd output transform function 561*c217d954SCole Faust output_transform.run(); 562*c217d954SCole Faust } 563*c217d954SCole Faust return dst; 564*c217d954SCole Faust } 565*c217d954SCole Faust compute_reference(const TensorShape & input_shape,WinogradInfo winograd_info,DataType data_type,ActivationLayerInfo act_info)566*c217d954SCole Faust SimpleTensor<T> compute_reference(const TensorShape &input_shape, WinogradInfo winograd_info, DataType data_type, ActivationLayerInfo act_info) 567*c217d954SCole Faust { 568*c217d954SCole Faust winograd_info.output_data_layout = DataLayout::NCHW; 569*c217d954SCole Faust TensorShape output_shape = compute_winograd_output_transform_shape(TensorInfo(input_shape, 1, data_type), winograd_info); 570*c217d954SCole Faust 571*c217d954SCole Faust // Create reference 572*c217d954SCole Faust SimpleTensor<T> src{ input_shape, data_type }; 573*c217d954SCole Faust SimpleTensor<T> bias{ TensorShape(input_shape[0]), data_type }; 574*c217d954SCole Faust 575*c217d954SCole Faust // Fill reference 576*c217d954SCole Faust fill(src, 0, -1.f, 1.f); 577*c217d954SCole Faust fill(bias, 1, -1.f, 1.f); 578*c217d954SCole Faust 579*c217d954SCole Faust const SimpleTensor<T> winograd_output = reference::winograd_output_transform<T>(src, bias, output_shape, winograd_info); 580*c217d954SCole Faust 581*c217d954SCole Faust return (act_info.enabled()) ? reference::activation_layer<T>(winograd_output, act_info) : winograd_output; 582*c217d954SCole Faust } 583*c217d954SCole Faust 584*c217d954SCole Faust bool _mixed_layout{ false }; 585*c217d954SCole Faust TensorType _target{}; 586*c217d954SCole Faust SimpleTensor<T> _reference{}; 587*c217d954SCole Faust }; 588*c217d954SCole Faust } // namespace validation 589*c217d954SCole Faust } // namespace test 590*c217d954SCole Faust } // namespace arm_compute 591*c217d954SCole Faust #endif /* ARM_COMPUTE_TEST_WINOGRAD_LAYER_FIXTURE */