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_BATCH_NORMALIZATION_LAYER_FUSION_FIXTURE 25*c217d954SCole Faust #define ARM_COMPUTE_TEST_BATCH_NORMALIZATION_LAYER_FUSION_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 "tests/AssetsLibrary.h" 30*c217d954SCole Faust #include "tests/Globals.h" 31*c217d954SCole Faust #include "tests/IAccessor.h" 32*c217d954SCole Faust #include "tests/framework/Asserts.h" 33*c217d954SCole Faust #include "tests/framework/Fixture.h" 34*c217d954SCole Faust #include "tests/validation/Helpers.h" 35*c217d954SCole Faust #include "tests/validation/reference/BatchNormalizationLayer.h" 36*c217d954SCole Faust #include "tests/validation/reference/ConvolutionLayer.h" 37*c217d954SCole Faust 38*c217d954SCole Faust namespace arm_compute 39*c217d954SCole Faust { 40*c217d954SCole Faust namespace test 41*c217d954SCole Faust { 42*c217d954SCole Faust namespace validation 43*c217d954SCole Faust { 44*c217d954SCole Faust template <typename TensorType, typename AccessorType, typename ConvolutionFunctionType, typename FusionFunctionType, typename T> 45*c217d954SCole Faust class BatchNormalizationLayerFusionValidationFixture : public framework::Fixture 46*c217d954SCole Faust { 47*c217d954SCole Faust public: 48*c217d954SCole Faust template <typename...> setup(TensorShape src_shape,TensorShape w_shape,TensorShape b_shape,TensorShape dst_shape,PadStrideInfo info,Size2D dilation,bool use_conv_b,bool use_beta,bool use_gamma,float epsilon,DataType dt,DataLayout data_layout)49*c217d954SCole Faust void setup(TensorShape src_shape, TensorShape w_shape, TensorShape b_shape, TensorShape dst_shape, PadStrideInfo info, Size2D dilation, 50*c217d954SCole Faust bool use_conv_b, bool use_beta, bool use_gamma, float epsilon, DataType dt, DataLayout data_layout) 51*c217d954SCole Faust { 52*c217d954SCole Faust ARM_COMPUTE_UNUSED(dilation); 53*c217d954SCole Faust 54*c217d954SCole Faust _data_type = dt; 55*c217d954SCole Faust _data_layout = data_layout; 56*c217d954SCole Faust _use_conv_b = use_conv_b; 57*c217d954SCole Faust _use_beta = use_beta; 58*c217d954SCole Faust _use_gamma = use_gamma; 59*c217d954SCole Faust 60*c217d954SCole Faust _target = compute_target(src_shape, w_shape, b_shape, dst_shape, info, epsilon); 61*c217d954SCole Faust _reference = compute_reference(src_shape, w_shape, b_shape, dst_shape, info, epsilon); 62*c217d954SCole Faust } 63*c217d954SCole Faust 64*c217d954SCole Faust protected: 65*c217d954SCole Faust template <typename U> fill(U && src,U && w_tensor,U && b_tensor,U && mean_tensor,U && var_tensor,U && beta_tensor,U && gamma_tensor)66*c217d954SCole Faust void fill(U &&src, U &&w_tensor, U &&b_tensor, U &&mean_tensor, U &&var_tensor, U &&beta_tensor, U &&gamma_tensor) 67*c217d954SCole Faust { 68*c217d954SCole Faust static_assert(std::is_floating_point<T>::value || std::is_same<T, half>::value, "Only floating point data types supported."); 69*c217d954SCole Faust using DistributionType = typename std::conditional<std::is_same<T, half>::value, arm_compute::utils::uniform_real_distribution_16bit<T>, std::uniform_real_distribution<T>>::type; 70*c217d954SCole Faust 71*c217d954SCole Faust DistributionType distribution{ T(-1.f), T(1.f) }; 72*c217d954SCole Faust DistributionType distribution_gz{ T(0.f), T(1.f) }; 73*c217d954SCole Faust 74*c217d954SCole Faust library->fill(src, distribution, 0); 75*c217d954SCole Faust library->fill(w_tensor, distribution, 1); 76*c217d954SCole Faust library->fill(mean_tensor, distribution, 2); 77*c217d954SCole Faust library->fill(var_tensor, distribution_gz, 3); 78*c217d954SCole Faust _use_conv_b ? library->fill(b_tensor, distribution, 4) : library->fill_tensor_value(b_tensor, T(0.f)); 79*c217d954SCole Faust _use_beta ? library->fill(beta_tensor, distribution, 5) : library->fill_tensor_value(beta_tensor, T(0.f)); 80*c217d954SCole Faust _use_gamma ? library->fill(gamma_tensor, distribution, 6) : library->fill_tensor_value(gamma_tensor, T(1.f)); 81*c217d954SCole Faust } 82*c217d954SCole Faust compute_target(TensorShape src_shape,TensorShape w_shape,TensorShape b_shape,TensorShape dst_shape,PadStrideInfo info,float epsilon)83*c217d954SCole Faust TensorType compute_target(TensorShape src_shape, TensorShape w_shape, TensorShape b_shape, TensorShape dst_shape, PadStrideInfo info, float epsilon) 84*c217d954SCole Faust { 85*c217d954SCole Faust if(_data_layout == DataLayout::NHWC) 86*c217d954SCole Faust { 87*c217d954SCole Faust permute(src_shape, PermutationVector(2U, 0U, 1U)); 88*c217d954SCole Faust permute(w_shape, PermutationVector(2U, 0U, 1U)); 89*c217d954SCole Faust permute(dst_shape, PermutationVector(2U, 0U, 1U)); 90*c217d954SCole Faust } 91*c217d954SCole Faust 92*c217d954SCole Faust // Create tensors 93*c217d954SCole Faust TensorType src = create_tensor<TensorType>(src_shape, _data_type, 1, QuantizationInfo(), _data_layout); 94*c217d954SCole Faust TensorType conv_w = create_tensor<TensorType>(w_shape, _data_type, 1, QuantizationInfo(), _data_layout); 95*c217d954SCole Faust TensorType conv_b = create_tensor<TensorType>(b_shape, _data_type, 1, QuantizationInfo(), _data_layout); 96*c217d954SCole Faust TensorType bn_mean = create_tensor<TensorType>(b_shape, _data_type, 1, QuantizationInfo(), _data_layout); 97*c217d954SCole Faust TensorType bn_var = create_tensor<TensorType>(b_shape, _data_type, 1, QuantizationInfo(), _data_layout); 98*c217d954SCole Faust TensorType bn_beta = create_tensor<TensorType>(b_shape, _data_type, 1, QuantizationInfo(), _data_layout); 99*c217d954SCole Faust TensorType bn_gamma = create_tensor<TensorType>(b_shape, _data_type, 1, QuantizationInfo(), _data_layout); 100*c217d954SCole Faust TensorType fused_w = create_tensor<TensorType>(w_shape, _data_type, 1, QuantizationInfo(), _data_layout); 101*c217d954SCole Faust TensorType fused_b = create_tensor<TensorType>(b_shape, _data_type, 1, QuantizationInfo(), _data_layout); 102*c217d954SCole Faust TensorType dst = create_tensor<TensorType>(dst_shape, _data_type, 1, QuantizationInfo(), _data_layout); 103*c217d954SCole Faust 104*c217d954SCole Faust // Create and configure function 105*c217d954SCole Faust FusionFunctionType fuse_fn; 106*c217d954SCole Faust ConvolutionFunctionType conv_fn; 107*c217d954SCole Faust TensorType *conv_b_ptr = _use_conv_b ? &conv_b : nullptr; 108*c217d954SCole Faust TensorType *beta_ptr = _use_beta ? &bn_beta : nullptr; 109*c217d954SCole Faust TensorType *gamma_ptr = _use_gamma ? &bn_gamma : nullptr; 110*c217d954SCole Faust fuse_fn.configure(&conv_w, &bn_mean, &bn_var, &fused_w, &fused_b, conv_b_ptr, beta_ptr, gamma_ptr, epsilon); 111*c217d954SCole Faust conv_fn.configure(&src, &fused_w, &fused_b, &dst, info); 112*c217d954SCole Faust 113*c217d954SCole Faust ARM_COMPUTE_ASSERT(src.info()->is_resizable()); 114*c217d954SCole Faust ARM_COMPUTE_ASSERT(conv_w.info()->is_resizable()); 115*c217d954SCole Faust ARM_COMPUTE_ASSERT(conv_b.info()->is_resizable()); 116*c217d954SCole Faust ARM_COMPUTE_ASSERT(bn_mean.info()->is_resizable()); 117*c217d954SCole Faust ARM_COMPUTE_ASSERT(bn_var.info()->is_resizable()); 118*c217d954SCole Faust ARM_COMPUTE_ASSERT(bn_beta.info()->is_resizable()); 119*c217d954SCole Faust ARM_COMPUTE_ASSERT(bn_gamma.info()->is_resizable()); 120*c217d954SCole Faust ARM_COMPUTE_ASSERT(fused_w.info()->is_resizable()); 121*c217d954SCole Faust ARM_COMPUTE_ASSERT(fused_b.info()->is_resizable()); 122*c217d954SCole Faust ARM_COMPUTE_ASSERT(dst.info()->is_resizable()); 123*c217d954SCole Faust 124*c217d954SCole Faust // Allocate tensors 125*c217d954SCole Faust src.allocator()->allocate(); 126*c217d954SCole Faust conv_w.allocator()->allocate(); 127*c217d954SCole Faust conv_b.allocator()->allocate(); 128*c217d954SCole Faust bn_mean.allocator()->allocate(); 129*c217d954SCole Faust bn_var.allocator()->allocate(); 130*c217d954SCole Faust bn_beta.allocator()->allocate(); 131*c217d954SCole Faust bn_gamma.allocator()->allocate(); 132*c217d954SCole Faust fused_w.allocator()->allocate(); 133*c217d954SCole Faust fused_b.allocator()->allocate(); 134*c217d954SCole Faust dst.allocator()->allocate(); 135*c217d954SCole Faust 136*c217d954SCole Faust ARM_COMPUTE_ASSERT(!src.info()->is_resizable()); 137*c217d954SCole Faust ARM_COMPUTE_ASSERT(!conv_w.info()->is_resizable()); 138*c217d954SCole Faust ARM_COMPUTE_ASSERT(!conv_b.info()->is_resizable()); 139*c217d954SCole Faust ARM_COMPUTE_ASSERT(!bn_mean.info()->is_resizable()); 140*c217d954SCole Faust ARM_COMPUTE_ASSERT(!bn_var.info()->is_resizable()); 141*c217d954SCole Faust ARM_COMPUTE_ASSERT(!bn_beta.info()->is_resizable()); 142*c217d954SCole Faust ARM_COMPUTE_ASSERT(!bn_gamma.info()->is_resizable()); 143*c217d954SCole Faust ARM_COMPUTE_ASSERT(!fused_w.info()->is_resizable()); 144*c217d954SCole Faust ARM_COMPUTE_ASSERT(!fused_b.info()->is_resizable()); 145*c217d954SCole Faust ARM_COMPUTE_ASSERT(!dst.info()->is_resizable()); 146*c217d954SCole Faust 147*c217d954SCole Faust // Fill tensors 148*c217d954SCole Faust fill(AccessorType(src), 149*c217d954SCole Faust AccessorType(conv_w), AccessorType(conv_b), 150*c217d954SCole Faust AccessorType(bn_mean), AccessorType(bn_var), AccessorType(bn_beta), AccessorType(bn_gamma)); 151*c217d954SCole Faust 152*c217d954SCole Faust // Compute function 153*c217d954SCole Faust fuse_fn.run(); 154*c217d954SCole Faust conv_fn.run(); 155*c217d954SCole Faust 156*c217d954SCole Faust return dst; 157*c217d954SCole Faust } 158*c217d954SCole Faust compute_reference(TensorShape src_shape,TensorShape w_shape,TensorShape b_shape,TensorShape dst_shape,PadStrideInfo info,float epsilon)159*c217d954SCole Faust SimpleTensor<T> compute_reference(TensorShape src_shape, TensorShape w_shape, TensorShape b_shape, TensorShape dst_shape, PadStrideInfo info, float epsilon) 160*c217d954SCole Faust { 161*c217d954SCole Faust // Create reference 162*c217d954SCole Faust SimpleTensor<T> src{ src_shape, _data_type, 1 }; 163*c217d954SCole Faust SimpleTensor<T> conv_w{ w_shape, _data_type, 1 }; 164*c217d954SCole Faust SimpleTensor<T> conv_b{ b_shape, _data_type, 1 }; 165*c217d954SCole Faust SimpleTensor<T> bn_var{ b_shape, _data_type, 1 }; 166*c217d954SCole Faust SimpleTensor<T> bn_mean{ b_shape, _data_type, 1 }; 167*c217d954SCole Faust SimpleTensor<T> bn_beta{ b_shape, _data_type, 1 }; 168*c217d954SCole Faust SimpleTensor<T> bn_gamma{ b_shape, _data_type, 1 }; 169*c217d954SCole Faust 170*c217d954SCole Faust // Fill reference 171*c217d954SCole Faust fill(src, conv_w, conv_b, bn_mean, bn_var, bn_beta, bn_gamma); 172*c217d954SCole Faust 173*c217d954SCole Faust // Calculate Conv + BN 174*c217d954SCole Faust auto conv_res = reference::convolution_layer(src, conv_w, conv_b, dst_shape, info); 175*c217d954SCole Faust return reference::batch_normalization_layer(conv_res, bn_mean, bn_var, bn_beta, bn_gamma, epsilon, ActivationLayerInfo()); 176*c217d954SCole Faust } 177*c217d954SCole Faust 178*c217d954SCole Faust TensorType _target{}; 179*c217d954SCole Faust SimpleTensor<T> _reference{}; 180*c217d954SCole Faust DataType _data_type{}; 181*c217d954SCole Faust DataLayout _data_layout{}; 182*c217d954SCole Faust bool _use_conv_b{}; 183*c217d954SCole Faust bool _use_beta{}; 184*c217d954SCole Faust bool _use_gamma{}; 185*c217d954SCole Faust }; 186*c217d954SCole Faust } // namespace validation 187*c217d954SCole Faust } // namespace test 188*c217d954SCole Faust } // namespace arm_compute 189*c217d954SCole Faust #endif /* ARM_COMPUTE_TEST_BATCH_NORMALIZATION_LAYER_FUSION_FIXTURE */ 190