1*c217d954SCole Faust /* 2*c217d954SCole Faust * Copyright (c) 2019-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_FUSEBATCHNORMALIZATION_FIXTURE 25*c217d954SCole Faust #define ARM_COMPUTE_TEST_FUSEBATCHNORMALIZATION_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/FuseBatchNormalization.h" 36*c217d954SCole Faust 37*c217d954SCole Faust #include <tuple> 38*c217d954SCole Faust #include <utility> 39*c217d954SCole Faust 40*c217d954SCole Faust namespace arm_compute 41*c217d954SCole Faust { 42*c217d954SCole Faust namespace test 43*c217d954SCole Faust { 44*c217d954SCole Faust namespace validation 45*c217d954SCole Faust { 46*c217d954SCole Faust template <typename TensorType, typename AccessorType, typename FunctionType, int dims_weights, typename T> 47*c217d954SCole Faust class FuseBatchNormalizationFixture : public framework::Fixture 48*c217d954SCole Faust { 49*c217d954SCole Faust public: 50*c217d954SCole Faust template <typename...> setup(TensorShape shape_w,DataType data_type,DataLayout data_layout,bool in_place,bool with_bias,bool with_gamma,bool with_beta)51*c217d954SCole Faust void setup(TensorShape shape_w, DataType data_type, DataLayout data_layout, bool in_place, bool with_bias, bool with_gamma, bool with_beta) 52*c217d954SCole Faust { 53*c217d954SCole Faust std::tie(_target_w, _target_b) = compute_target(shape_w, data_type, data_layout, in_place, with_bias, with_gamma, with_beta); 54*c217d954SCole Faust std::tie(_reference_w, _reference_b) = compute_reference(shape_w, data_type, with_bias, with_gamma, with_beta); 55*c217d954SCole Faust } 56*c217d954SCole Faust 57*c217d954SCole Faust protected: 58*c217d954SCole Faust template <typename U> fill(U && tensor,int i,float min,float max)59*c217d954SCole Faust void fill(U &&tensor, int i, float min, float max) 60*c217d954SCole Faust { 61*c217d954SCole Faust library->fill_tensor_uniform(tensor, i, min, max); 62*c217d954SCole Faust } 63*c217d954SCole Faust compute_target(TensorShape shape_w,DataType data_type,DataLayout data_layout,bool in_place,bool with_bias,bool with_gamma,bool with_beta)64*c217d954SCole Faust std::pair<TensorType, TensorType> compute_target(TensorShape shape_w, DataType data_type, DataLayout data_layout, bool in_place, bool with_bias, bool with_gamma, bool with_beta) 65*c217d954SCole Faust { 66*c217d954SCole Faust const TensorShape shape_v(shape_w[dims_weights - 1]); 67*c217d954SCole Faust 68*c217d954SCole Faust if(data_layout == DataLayout::NHWC) 69*c217d954SCole Faust { 70*c217d954SCole Faust permute(shape_w, PermutationVector(2U, 0U, 1U)); 71*c217d954SCole Faust } 72*c217d954SCole Faust 73*c217d954SCole Faust const bool in_place_w = in_place; 74*c217d954SCole Faust const bool in_place_b = with_bias ? in_place : false; 75*c217d954SCole Faust 76*c217d954SCole Faust // Create tensors 77*c217d954SCole Faust TensorType w = create_tensor<TensorType>(shape_w, data_type, 1, QuantizationInfo(), data_layout); 78*c217d954SCole Faust TensorType b = create_tensor<TensorType>(shape_v, data_type); 79*c217d954SCole Faust TensorType mean = create_tensor<TensorType>(shape_v, data_type); 80*c217d954SCole Faust TensorType var = create_tensor<TensorType>(shape_v, data_type); 81*c217d954SCole Faust TensorType w_fused = create_tensor<TensorType>(shape_w, data_type, 1, QuantizationInfo(), data_layout); 82*c217d954SCole Faust TensorType b_fused = create_tensor<TensorType>(shape_v, data_type); 83*c217d954SCole Faust TensorType beta = create_tensor<TensorType>(shape_v, data_type); 84*c217d954SCole Faust TensorType gamma = create_tensor<TensorType>(shape_v, data_type); 85*c217d954SCole Faust 86*c217d954SCole Faust auto b_to_use = with_bias ? &b : nullptr; 87*c217d954SCole Faust auto gamma_to_use = with_gamma ? &gamma : nullptr; 88*c217d954SCole Faust auto beta_to_use = with_beta ? &beta : nullptr; 89*c217d954SCole Faust auto w_fused_to_use = in_place_w ? nullptr : &w_fused; 90*c217d954SCole Faust auto b_fused_to_use = in_place_b ? nullptr : &b_fused; 91*c217d954SCole Faust 92*c217d954SCole Faust const FuseBatchNormalizationType fuse_bn_type = dims_weights == 3 ? 93*c217d954SCole Faust FuseBatchNormalizationType::DEPTHWISECONVOLUTION : 94*c217d954SCole Faust FuseBatchNormalizationType::CONVOLUTION; 95*c217d954SCole Faust // Create and configure function 96*c217d954SCole Faust FunctionType fuse_batch_normalization; 97*c217d954SCole Faust fuse_batch_normalization.configure(&w, &mean, &var, w_fused_to_use, b_fused_to_use, b_to_use, beta_to_use, gamma_to_use, _epsilon, fuse_bn_type); 98*c217d954SCole Faust 99*c217d954SCole Faust ARM_COMPUTE_ASSERT(w.info()->is_resizable()); 100*c217d954SCole Faust ARM_COMPUTE_ASSERT(b.info()->is_resizable()); 101*c217d954SCole Faust ARM_COMPUTE_ASSERT(mean.info()->is_resizable()); 102*c217d954SCole Faust ARM_COMPUTE_ASSERT(var.info()->is_resizable()); 103*c217d954SCole Faust ARM_COMPUTE_ASSERT(w_fused.info()->is_resizable()); 104*c217d954SCole Faust ARM_COMPUTE_ASSERT(b_fused.info()->is_resizable()); 105*c217d954SCole Faust ARM_COMPUTE_ASSERT(beta.info()->is_resizable()); 106*c217d954SCole Faust ARM_COMPUTE_ASSERT(gamma.info()->is_resizable()); 107*c217d954SCole Faust 108*c217d954SCole Faust // Allocate tensors 109*c217d954SCole Faust w.allocator()->allocate(); 110*c217d954SCole Faust b.allocator()->allocate(); 111*c217d954SCole Faust mean.allocator()->allocate(); 112*c217d954SCole Faust var.allocator()->allocate(); 113*c217d954SCole Faust w_fused.allocator()->allocate(); 114*c217d954SCole Faust b_fused.allocator()->allocate(); 115*c217d954SCole Faust beta.allocator()->allocate(); 116*c217d954SCole Faust gamma.allocator()->allocate(); 117*c217d954SCole Faust 118*c217d954SCole Faust ARM_COMPUTE_ASSERT(!w.info()->is_resizable()); 119*c217d954SCole Faust ARM_COMPUTE_ASSERT(!b.info()->is_resizable()); 120*c217d954SCole Faust ARM_COMPUTE_ASSERT(!mean.info()->is_resizable()); 121*c217d954SCole Faust ARM_COMPUTE_ASSERT(!var.info()->is_resizable()); 122*c217d954SCole Faust ARM_COMPUTE_ASSERT(!w_fused.info()->is_resizable()); 123*c217d954SCole Faust ARM_COMPUTE_ASSERT(!b_fused.info()->is_resizable()); 124*c217d954SCole Faust ARM_COMPUTE_ASSERT(!beta.info()->is_resizable()); 125*c217d954SCole Faust ARM_COMPUTE_ASSERT(!gamma.info()->is_resizable()); 126*c217d954SCole Faust 127*c217d954SCole Faust // Fill tensors 128*c217d954SCole Faust fill(AccessorType(w), 0U, -1.0f, 1.0f); 129*c217d954SCole Faust fill(AccessorType(b), 1U, -1.0f, 1.0f); 130*c217d954SCole Faust fill(AccessorType(mean), 2U, -1.0f, 1.0f); 131*c217d954SCole Faust fill(AccessorType(var), 3U, 0.0f, 1.0f); 132*c217d954SCole Faust fill(AccessorType(beta), 4U, -1.0f, 1.0f); 133*c217d954SCole Faust fill(AccessorType(gamma), 5U, -1.0f, 1.0f); 134*c217d954SCole Faust 135*c217d954SCole Faust // Compute function 136*c217d954SCole Faust fuse_batch_normalization.run(); 137*c217d954SCole Faust 138*c217d954SCole Faust return std::make_pair(std::move(in_place_w ? w : w_fused), std::move(in_place_b ? b : b_fused)); 139*c217d954SCole Faust } 140*c217d954SCole Faust compute_reference(TensorShape shape_w,DataType data_type,bool with_bias,bool with_gamma,bool with_beta)141*c217d954SCole Faust std::pair<SimpleTensor<T>, SimpleTensor<T>> compute_reference(TensorShape shape_w, DataType data_type, bool with_bias, bool with_gamma, bool with_beta) 142*c217d954SCole Faust { 143*c217d954SCole Faust const TensorShape shape_v(shape_w[dims_weights - 1]); 144*c217d954SCole Faust 145*c217d954SCole Faust SimpleTensor<T> w{ shape_w, data_type }; 146*c217d954SCole Faust SimpleTensor<T> b{ shape_v, data_type }; 147*c217d954SCole Faust SimpleTensor<T> mean{ shape_v, data_type }; 148*c217d954SCole Faust SimpleTensor<T> var{ shape_v, data_type }; 149*c217d954SCole Faust SimpleTensor<T> w_fused{ shape_w, data_type }; 150*c217d954SCole Faust SimpleTensor<T> b_fused{ shape_v, data_type }; 151*c217d954SCole Faust SimpleTensor<T> beta{ shape_v, data_type }; 152*c217d954SCole Faust SimpleTensor<T> gamma{ shape_v, data_type }; 153*c217d954SCole Faust 154*c217d954SCole Faust // Fill reference tensor 155*c217d954SCole Faust fill(w, 0U, -1.0f, 1.0f); 156*c217d954SCole Faust fill(b, 1U, -1.0f, 1.0f); 157*c217d954SCole Faust fill(mean, 2U, -1.0f, 1.0f); 158*c217d954SCole Faust fill(var, 3U, 0.0f, 1.0f); 159*c217d954SCole Faust fill(beta, 4U, -1.0f, 1.0f); 160*c217d954SCole Faust fill(gamma, 5U, -1.0f, 1.0f); 161*c217d954SCole Faust 162*c217d954SCole Faust if(!with_bias) 163*c217d954SCole Faust { 164*c217d954SCole Faust // Fill with zeros 165*c217d954SCole Faust fill(b, 0U, 0.0f, 0.0f); 166*c217d954SCole Faust } 167*c217d954SCole Faust 168*c217d954SCole Faust if(!with_gamma) 169*c217d954SCole Faust { 170*c217d954SCole Faust // Fill with ones 171*c217d954SCole Faust fill(gamma, 0U, 1.0f, 1.0f); 172*c217d954SCole Faust } 173*c217d954SCole Faust 174*c217d954SCole Faust if(!with_beta) 175*c217d954SCole Faust { 176*c217d954SCole Faust // Fill with zeros 177*c217d954SCole Faust fill(beta, 0U, 0.0f, 0.0f); 178*c217d954SCole Faust } 179*c217d954SCole Faust 180*c217d954SCole Faust switch(dims_weights) 181*c217d954SCole Faust { 182*c217d954SCole Faust case 3: 183*c217d954SCole Faust // Weights for depth wise convolution layer 184*c217d954SCole Faust reference::fuse_batch_normalization_dwc_layer(w, mean, var, w_fused, b_fused, b, beta, gamma, _epsilon); 185*c217d954SCole Faust break; 186*c217d954SCole Faust case 4: 187*c217d954SCole Faust // Weights for convolution layer 188*c217d954SCole Faust reference::fuse_batch_normalization_conv_layer(w, mean, var, w_fused, b_fused, b, beta, gamma, _epsilon); 189*c217d954SCole Faust break; 190*c217d954SCole Faust default: 191*c217d954SCole Faust ARM_COMPUTE_ERROR("Not supported number of dimensions for the input weights tensor"); 192*c217d954SCole Faust } 193*c217d954SCole Faust 194*c217d954SCole Faust return std::make_pair(std::move(w_fused), std::move(b_fused)); 195*c217d954SCole Faust } 196*c217d954SCole Faust 197*c217d954SCole Faust const float _epsilon{ 0.0001f }; 198*c217d954SCole Faust TensorType _target_w{}; 199*c217d954SCole Faust TensorType _target_b{}; 200*c217d954SCole Faust SimpleTensor<T> _reference_w{}; 201*c217d954SCole Faust SimpleTensor<T> _reference_b{}; 202*c217d954SCole Faust }; 203*c217d954SCole Faust } // namespace validation 204*c217d954SCole Faust } // namespace test 205*c217d954SCole Faust } // namespace arm_compute 206*c217d954SCole Faust #endif /* ARM_COMPUTE_TEST_FUSEBATCHNORMALIZATION_FIXTURE */ 207