From 761c8d02ff875877db7aa7c850cf8d128592e822 Mon Sep 17 00:00:00 2001 From: Gian Marco Iodice Date: Mon, 10 Jun 2019 14:46:49 +0100 Subject: COMPMID-2398: Add test for CLFuseBatchNormalizationLayer Change-Id: I786df628ce15fc33fc42c9437fe82972e02e3b16 Signed-off-by: Gian Marco Iodice Reviewed-on: https://review.mlplatform.org/c/1317 Comments-Addressed: Arm Jenkins Reviewed-by: Michele Di Giorgio Tested-by: Arm Jenkins --- .../CL/kernels/CLFuseBatchNormalizationKernel.h | 32 ++- .../CL/functions/CLFuseBatchNormalization.h | 32 ++- src/core/CL/CLKernelLibrary.cpp | 2 +- src/core/CL/cl_kernels/batchnormalization_layer.cl | 282 ++++++++++----------- .../CL/kernels/CLFuseBatchNormalizationKernel.cpp | 66 ++--- tests/validation/CL/FuseBatchNormalization.cpp | 147 +++++++++++ .../fixtures/FuseBatchNormalizationFixture.h | 204 +++++++++++++++ .../reference/FuseBatchNormalization.cpp | 111 ++++++++ .../validation/reference/FuseBatchNormalization.h | 51 ++++ 9 files changed, 707 insertions(+), 220 deletions(-) create mode 100644 tests/validation/CL/FuseBatchNormalization.cpp create mode 100644 tests/validation/fixtures/FuseBatchNormalizationFixture.h create mode 100644 tests/validation/reference/FuseBatchNormalization.cpp create mode 100644 tests/validation/reference/FuseBatchNormalization.h diff --git a/arm_compute/core/CL/kernels/CLFuseBatchNormalizationKernel.h b/arm_compute/core/CL/kernels/CLFuseBatchNormalizationKernel.h index 05a57c171e..a5b98bb27d 100644 --- a/arm_compute/core/CL/kernels/CLFuseBatchNormalizationKernel.h +++ b/arm_compute/core/CL/kernels/CLFuseBatchNormalizationKernel.h @@ -52,11 +52,13 @@ public: * @param[in] conv_weights Convolution layer weights tensor. Data type supported: F16/F32 * @param[in] bn_mean Batch normalization layer mean tensor. Same as @p conv_weights * @param[in] bn_var Batch normalization layer variance tensor. Same as @p conv_weights - * @param[out] fused_weights Output fused weights tensor. Same as @p conv_weights - * @param[out] fused_bias Output fused bias tensor. Same as @p conv_weights - * @param[in] conv_bias (Optional) Convolution layer bias tensor. Same as @p conv_weights - * @param[in] bn_beta (Optional) Batch normalization layer beta tensor. Same as @p conv_weights - * @param[in] bn_gamma (Optional) Batch normalization layer gamma tensor. Same as @p conv_weights + * @param[out] fused_weights Output fused weights tensor. It can be a nullptr in case of in-place computation. Same as @p conv_weights + * @param[out] fused_bias Output fused bias tensor. It can be a nullptr in case of in-place computation and conv_bias != nullptr. Same as @p conv_weights + * @param[in] conv_bias (Optional) Convolution layer bias tensor. It can be a nullptr in case the bias tensor is not required. Same as @p conv_weights + * @param[in] bn_beta (Optional) Batch normalization layer beta tensor. It can be a nullptr in case the beta tensor is not required. Same as @p conv_weights + * @note if nullptr, bn_beta is set to 0.0 + * @param[in] bn_gamma (Optional) Batch normalization layer gamma tensor. It can be a nullptr in case the gamma tensor is not required. Same as @p conv_weights + * @note if nullptr, bn_gamma is set to 1.0 * @param[in] epsilon (Optional) Batch normalization layer epsilon parameter. Defaults to 0.001f. */ void configure(const ICLTensor *conv_weights, const ICLTensor *bn_mean, const ICLTensor *bn_var, ICLTensor *fused_weights, ICLTensor *fused_bias, @@ -64,15 +66,17 @@ public: float epsilon = 0.001f); /** Static function to check if given info will lead to a valid configuration of @ref CLFuseBatchNormalizationKernel * - * @param[in] conv_weights Convolution layer weights tensor. Data type supported: F16/F32 - * @param[in] bn_mean Batch normalization layer mean tensor. Same as @p conv_weights - * @param[in] bn_var Batch normalization layer variance tensor. Same as @p conv_weights - * @param[in] fused_weights Output fused weights tensor. Same as @p conv_weights - * @param[in] fused_bias Output fused bias tensor. Same as @p conv_weights - * @param[in] conv_bias (Optional) Convolution layer bias tensor. Same as @p conv_weights - * @param[in] bn_beta (Optional) Batch normalization layer beta tensor. Same as @p conv_weights - * @param[in] bn_gamma (Optional) Batch normalization layer gamma tensor. Same as @p conv_weights - * @param[in] epsilon (Optional) Batch normalization layer epsilon parameter. Defaults to 0.001f. + * @param[in] conv_weights Convolution layer weights tensor info. Data type supported: F16/F32 + * @param[in] bn_mean Batch normalization layer mean tensor info. Same as @p conv_weights + * @param[in] bn_var Batch normalization layer variance tensor info. Same as @p conv_weights + * @param[out] fused_weights Output fused weights tensor info. It can be a nullptr in case of in-place computation. Same as @p conv_weights + * @param[out] fused_bias Output fused bias tensor info. It can be a nullptr in case of in-place computation and conv_bias != nullptr. Same as @p conv_weights + * @param[in] conv_bias (Optional) Convolution layer bias tensor info. It can be a nullptr in case the bias tensor is not required. Same as @p conv_weights + * @param[in] bn_beta (Optional) Batch normalization layer beta tensor info. It can be a nullptr in case the beta tensor is not required. Same as @p conv_weights + * @note if nullptr, bn_beta is set to 0.0 + * @param[in] bn_gamma (Optional) Batch normalization layer gamma tensor info. It can be a nullptr in case the gamma tensor is not required. Same as @p conv_weights + * @note if nullptr, bn_gamma is set to 1.0 + * @param[in] epsilon (Optional) Batch normalization layer epsilon parameter. Defaults to 0.001f. * * @return a status */ diff --git a/arm_compute/runtime/CL/functions/CLFuseBatchNormalization.h b/arm_compute/runtime/CL/functions/CLFuseBatchNormalization.h index 777a80f8b9..4e7f1cba74 100644 --- a/arm_compute/runtime/CL/functions/CLFuseBatchNormalization.h +++ b/arm_compute/runtime/CL/functions/CLFuseBatchNormalization.h @@ -54,11 +54,13 @@ public: * @param[in] conv_weights Convolution layer weights tensor. Data type supported: F16/F32 * @param[in] bn_mean Batch normalization layer mean tensor. Same as @p conv_weights * @param[in] bn_var Batch normalization layer variance tensor. Same as @p conv_weights - * @param[out] fused_weights Output fused weights tensor. Same as @p conv_weights - * @param[out] fused_bias Output fused bias tensor. Same as @p conv_weights - * @param[in] conv_bias (Optional) Convolution layer bias tensor. Same as @p conv_weights - * @param[in] bn_beta (Optional) Batch normalization layer beta tensor. Same as @p conv_weights - * @param[in] bn_gamma (Optional) Batch normalization layer gamma tensor. Same as @p conv_weights + * @param[out] fused_weights Output fused weights tensor. It can be a nullptr in case of in-place computation. Same as @p conv_weights + * @param[out] fused_bias Output fused bias tensor. It can be a nullptr in case of in-place computation and conv_bias != nullptr. Same as @p conv_weights + * @param[in] conv_bias (Optional) Convolution layer bias tensor. It can be a nullptr in case the bias tensor is not required. Same as @p conv_weights + * @param[in] bn_beta (Optional) Batch normalization layer beta tensor. It can be a nullptr in case the beta tensor is not required. Same as @p conv_weights + * @note if nullptr, bn_beta is set to 0.0 + * @param[in] bn_gamma (Optional) Batch normalization layer gamma tensor. It can be a nullptr in case the gamma tensor is not required. Same as @p conv_weights + * @note if nullptr, bn_gamma is set to 1.0 * @param[in] epsilon (Optional) Batch normalization layer epsilon parameter. Defaults to 0.001f. */ void configure(const ICLTensor *conv_weights, const ICLTensor *bn_mean, const ICLTensor *bn_var, ICLTensor *fused_weights, ICLTensor *fused_bias, @@ -66,15 +68,17 @@ public: float epsilon = 0.001f); /** Static function to check if given info will lead to a valid configuration of @ref CLFuseBatchNormalization * - * @param[in] conv_weights Convolution layer weights tensor. Data type supported: F16/F32 - * @param[in] bn_mean Batch normalization layer mean tensor. Same as @p conv_weights - * @param[in] bn_var Batch normalization layer variance tensor. Same as @p conv_weights - * @param[in] fused_weights Output fused weights tensor. Same as @p conv_weights - * @param[in] fused_bias Output fused bias tensor. Same as @p conv_weights - * @param[in] conv_bias (Optional) Convolution layer bias tensor. Same as @p conv_weights - * @param[in] bn_beta (Optional) Batch normalization layer beta tensor. Same as @p conv_weights - * @param[in] bn_gamma (Optional) Batch normalization layer gamma tensor. Same as @p conv_weights - * @param[in] epsilon (Optional) Batch normalization layer epsilon parameter. Defaults to 0.001f. + * @param[in] conv_weights Convolution layer weights tensor info. Data type supported: F16/F32 + * @param[in] bn_mean Batch normalization layer mean tensor info. Same as @p conv_weights + * @param[in] bn_var Batch normalization layer variance tensor info. Same as @p conv_weights + * @param[out] fused_weights Output fused weights tensor info. It can be a nullptr in case of in-place computation. Same as @p conv_weights + * @param[out] fused_bias Output fused bias tensor info. It can be a nullptr in case of in-place computation and conv_bias != nullptr. Same as @p conv_weights + * @param[in] conv_bias (Optional) Convolution layer bias tensor info. It can be a nullptr in case the bias tensor is not required. Same as @p conv_weights + * @param[in] bn_beta (Optional) Batch normalization layer beta tensor info. It can be a nullptr in case the beta tensor is not required. Same as @p conv_weights + * @note if nullptr, bn_beta is set to 0.0 + * @param[in] bn_gamma (Optional) Batch normalization layer gamma tensor info. It can be a nullptr in case the gamma tensor is not required. Same as @p conv_weights + * @note if nullptr, bn_gamma is set to 1.0 + * @param[in] epsilon (Optional) Batch normalization layer epsilon parameter. Defaults to 0.001f. * * @return a status */ diff --git a/src/core/CL/CLKernelLibrary.cpp b/src/core/CL/CLKernelLibrary.cpp index 904575a53c..b734fd291c 100644 --- a/src/core/CL/CLKernelLibrary.cpp +++ b/src/core/CL/CLKernelLibrary.cpp @@ -298,7 +298,7 @@ const std::map CLKernelLibrary::_kernel_program_map = { "finalize", "optical_flow_pyramid_lk.cl" }, { "flatten", "flatten.cl" }, { "floor_layer", "floor.cl" }, - { "fuse_batchnormalization_layer", "batchnormalization_layer.cl" }, + { "fuse_batchnormalization_conv_layer", "batchnormalization_layer.cl" }, { "gather", "gather.cl" }, { "gaussian1x5_sub_x", "gaussian_pyramid.cl" }, { "gaussian5x1_sub_y", "gaussian_pyramid.cl" }, diff --git a/src/core/CL/cl_kernels/batchnormalization_layer.cl b/src/core/CL/cl_kernels/batchnormalization_layer.cl index 66d371c02f..a5321315d3 100644 --- a/src/core/CL/cl_kernels/batchnormalization_layer.cl +++ b/src/core/CL/cl_kernels/batchnormalization_layer.cl @@ -259,161 +259,145 @@ __kernel void batchnormalization_layer_nhwc(TENSOR3D_DECLARATION(input), } #endif /* defined(VEC_SIZE) && defined(DATA_TYPE) && defined(DATA_TYPE)*/ -#if defined(NUM_CHANNELS) && defined(DATA_TYPE) && defined(EPSILON) -/** Fuse batchnorm parameters to convolution layer parameters +#if defined(DIM2) && defined(DATA_TYPE) && defined(EPSILON) +/** OpenCL kernel to fuse the weights of convolution layer with batch normalization when the data layout is either NCHW or NHWC * - * @attention Data type should be passed using the -DDATA_TYPE compile flag, e.g. -DDATA_TYPE=float - * @attention Input tensor depth should be given as a preprocessor argument using -DNUM_CHANNELS=size. e.g. -DNUM_CHANNELS=16 - * @attention Batch normalization epsilon parameter should be given as a preprocessor argument with -DEPSILON=value. e.g. -DEPSILON=0.001f + * @note The input weights tensor is assumed 4D with the OFMs in the fourth dimension + * @note Data type should be passed at compile time using the -DDATA_TYPE, e.g. -DDATA_TYPE=float + * @note The third dimension of the input tensor should be passed at compile time using -DNUM_CHANNELS=size. e.g. -DNUM_CHANNELS=16 + * @note Batch normalization epsilon parameter should be passed at compile time using -DEPSILON=value. e.g. -DEPSILON=0.001f * - * @param[in] conv_w_ptr Pointer to the source tensor. Supported data types: F16/F32 - * @param[in] conv_w_stride_x Stride of the source tensor in X dimension (in bytes) - * @param[in] conv_w_step_x input_stride_x * number of elements along X processed per workitem(in bytes) - * @param[in] conv_w_stride_y Stride of the source tensor in Y dimension (in bytes) - * @param[in] conv_w_step_y input_stride_y * number of elements along Y processed per workitem(in bytes) - * @param[in] conv_w_stride_z Stride of the source tensor in Z dimension (in bytes) - * @param[in] conv_w_step_z input_stride_z * number of elements along Z processed per workitem(in bytes) - * @param[in] conv_w_stride_w Stride of the source tensor in W dimension (in bytes) - * @param[in] conv_w_step_w input_stride_w * number of elements along W processed per workitem(in bytes) - * @param[in] conv_w_offset_first_element_in_bytes The offset of the first element in the source tensor - * @param[in] bn_mean_ptr Pointer to the mean source tensor. Supported data types: same as @p input_ptr - * @param[in] bn_mean_stride_x Stride of the mean source tensor in X dimension (in bytes) - * @param[in] bn_mean_step_x bn_mean_stride_x * number of elements along X processed per workitem(in bytes) - * @param[in] bn_mean_offset_first_element_in_bytes The offset of the first element in the mean source tensor - * @param[in] bn_var_ptr Pointer to the var tensor. Supported data types: same as @p input_ptr - * @param[in] bn_var_stride_x Stride of the var tensor in X dimension (in bytes) - * @param[in] bn_var_step_x bn_var_stride_x * number of elements along X processed per workitem(in bytes) - * @param[in] bn_var_offset_first_element_in_bytes The offset of the first element in the var source tensor - * @param[out] fused_w_ptr Pointer to the destination weights tensors. Supported data types: same as @p input_ptr - * @param[in] fused_w_stride_x Stride of the destination tensor in X dimension (in bytes) - * @param[in] fused_w_step_x fused_w_stride_x * number of elements along X processed per workitem(in bytes) - * @param[in] fused_w_stride_y Stride of the destination tensor in Y dimension (in bytes) - * @param[in] fused_w_step_y fused_w_stride_y * number of elements along Y processed per workitem(in bytes) - * @param[in] fused_w_stride_z Stride of the destination tensor in Z dimension (in bytes) - * @param[in] fused_w_step_z fused_w_stride_z * number of elements along Z processed per workitem(in bytes) - * @param[in] fused_w_stride_w Stride of the destination tensor in W dimension (in bytes) - * @param[in] fused_w_step_w fused_w_stride_w * number of elements along W processed per workitem(in bytes) - * @param[in] fused_w_offset_first_element_in_bytes The offset of the first element in the destination tensor - * @param[in] fused_b_ptr Pointer to the destination bias tensor. Supported data types: same as @p input_ptr - * @param[in] fused_b_stride_x Stride of the bias source tensor in X dimension (in bytes) - * @param[in] fused_b_step_x fused_b_stride_x * number of elements along X processed per workitem(in bytes) - * @param[in] fused_b_offset_first_element_in_bytes The offset of the first element in the destination tensor - * @param[in] conv_b_ptr Pointer to the source bias tensor. Supported data types: same as @p input_ptr - * @param[in] conv_b_stride_x Stride of the beta source tensor in X dimension (in bytes) - * @param[in] conv_b_step_x conv_b_beta_stride_x * number of elements along X processed per workitem(in bytes) - * @param[in] conv_b_offset_first_element_in_bytes The offset of the first element in the source bias tensor - * @param[in] bn_beta_ptr Pointer to the beta source tensor. Supported data types: same as @p input_ptr - * @param[in] bn_beta_stride_x Stride of the beta source tensor in X dimension (in bytes) - * @param[in] bn_beta_step_x bn_beta_stride_x * number of elements along X processed per workitem(in bytes) - * @param[in] bn_beta_offset_first_element_in_bytes The offset of the first element in the beta source tensor - * @param[in] bn_gamma_ptr Pointer to the gamma source tensor. Supported data types: same as @p input_ptr - * @param[in] bn_gamma_stride_x Stride of the gamma source tensor in X dimension (in bytes) - * @param[in] bn_gamma_step_x bn_gamma_stride_x * number of elements along X processed per workitem(in bytes) - * @param[in] bn_gamma_offset_first_element_in_bytes The offset of the first element in the gamma source tensor - * @param[in] epsilon Epsilon parameter in the batch normalization equation + * @param[in] w_ptr Pointer to the weights tensor. Supported data types: F16/F32 + * @param[in] w_stride_x Stride of the weights tensor in X dimension (in bytes) + * @param[in] w_step_x w_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] w_stride_y Stride of the weights tensor in Y dimension (in bytes) + * @param[in] w_step_y w_stride_y * number of elements along Y processed per workitem(in bytes) + * @param[in] w_stride_z Stride of the weights tensor in Z dimension (in bytes) + * @param[in] w_step_z w_stride_z * number of elements along Z processed per workitem(in bytes) + * @param[in] w_offset_first_element_in_bytes The offset of the first element in the weights tensor + * @param[in] b_ptr (Optional) Pointer to the bias tensor. Supported data types: same as @p w_ptr + * @param[in] b_stride_x (Optional) Stride of the bias tensor in X dimension (in bytes) + * @param[in] b_step_x (Optional) b_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] b_stride_y (Optional) Stride of the bias tensor in Y dimension (in bytes) + * @param[in] b_step_y (Optional) b_stride_y * number of elements along Y processed per workitem(in bytes) + * @param[in] b_stride_z (Optional) Stride of the bias tensor in Z dimension (in bytes) + * @param[in] b_step_z (Optional) b_stride_z * number of elements along Z processed per workitem(in bytes) + * @param[in] b_offset_first_element_in_bytes (Optional) The offset of the first element in the bias tensor + * @param[in] mean_ptr Pointer to the mean source tensor. Supported data types: same as @p w_ptr + * @param[in] mean_stride_x Stride of the mean source tensor in X dimension (in bytes) + * @param[in] mean_step_x mean_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] mean_offset_first_element_in_bytes The offset of the first element in the mean source tensor + * @param[in] var_ptr Pointer to the var tensor. Supported data types: same as @p w_ptr + * @param[in] var_stride_x Stride of the var tensor in X dimension (in bytes) + * @param[in] var_step_x var_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] var_offset_first_element_in_bytes The offset of the first element in the var source tensor + * @param[out] w_fused_ptr (Optional) Pointer to the destination weights tensors. Supported data types: same as @p w_ptr + * @param[in] w_fused_stride_x (Optional) Stride of the destination weights tensor in X dimension (in bytes) + * @param[in] w_fused_step_x (Optional) w_fused_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] w_fused_stride_y (Optional) Stride of the destination weights tensor in Y dimension (in bytes) + * @param[in] w_fused_step_y (Optional) w_fused_stride_y * number of elements along Y processed per workitem(in bytes) + * @param[in] w_fused_stride_z (Optional) Stride of the destination weights tensor in Z dimension (in bytes) + * @param[in] w_fused_step_z (Optional) w_fused_stride_z * number of elements along Z processed per workitem(in bytes) + * @param[in] w_fused_offset_first_element_in_bytes (Optional) The offset of the first element in the destination weights tensor + * @param[in] b_fused_ptr (Optional) Pointer to the destination bias tensor. Supported data types: same as @p w_ptr + * @param[in] b_fused_stride_x (Optional) Stride of the destination bias tensor in X dimension (in bytes) + * @param[in] b_fused_step_x (Optional) b_fused_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] b_fused_offset_first_element_in_bytes (Optional) The offset of the first element in the destination bias tensor + * @param[in] beta_ptr (Optional) Pointer to the beta source tensor. Supported data types: same as @p w_ptr + * @param[in] beta_stride_x (Optional) Stride of the beta source tensor in X dimension (in bytes) + * @param[in] beta_step_x (Optional) beta_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] beta_offset_first_element_in_bytes (Optional) The offset of the first element in the beta source tensor + * @param[in] gamma_ptr (Optional) Pointer to the gamma source tensor. Supported data types: same as @p w_ptr + * @param[in] gamma_stride_x (Optional) Stride of the gamma source tensor in X dimension (in bytes) + * @param[in] gamma_step_x (Optional) gamma_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] gamma_offset_first_element_in_bytes (Optional) The offset of the first element in the gamma source tensor */ -__kernel void fuse_batchnormalization_layer(TENSOR4D_DECLARATION(conv_w), - VECTOR_DECLARATION(bn_mean), - VECTOR_DECLARATION(bn_var) +__kernel void fuse_batchnormalization_conv_layer(TENSOR3D_DECLARATION(w), +#if defined(BIAS) + VECTOR_DECLARATION(b), +#endif // defined(BIAS) + VECTOR_DECLARATION(mean), + VECTOR_DECLARATION(var) #ifndef IN_PLACE_W - , - TENSOR4D_DECLARATION(fused_w) -#endif /* not IN_PLACE_W */ + , + TENSOR3D_DECLARATION(w_fused) +#endif // ifndef IN_PLACE_W #ifndef IN_PLACE_B - , - VECTOR_DECLARATION(fused_b) -#endif /* not IN_PLACE_B */ -#ifdef HAS_BIAS - , - VECTOR_DECLARATION(conv_b) -#endif /* HAS_BIAS */ -#ifndef USE_DEFAULT_BETA - , - VECTOR_DECLARATION(bn_beta) -#endif /* USE_DEFAULT_BETA */ -#ifndef USE_DEFAULT_GAMMA - , - VECTOR_DECLARATION(bn_gamma) -#endif /* USE_DEFAULT_GAMMA */ - ) + , + VECTOR_DECLARATION(b_fused) +#endif // ifndef IN_PLACE_B +#if defined(BETA) + , + VECTOR_DECLARATION(beta) +#endif // defined(BETA) +#if defined(GAMMA) + , + VECTOR_DECLARATION(gamma) +#endif // defined(GAMMA) + ) { - Tensor4D conv_w = CONVERT_TO_TENSOR4D_STRUCT(conv_w, NUM_CHANNELS); - Vector bn_mean = CONVERT_TO_VECTOR_STRUCT_NO_STEP(bn_mean); - Vector bn_var = CONVERT_TO_VECTOR_STRUCT_NO_STEP(bn_var); - - // Conditional ops -#ifdef HAS_BIAS - Vector conv_b = CONVERT_TO_VECTOR_STRUCT_NO_STEP(conv_b); -#endif /* HAS_BIAS */ -#ifndef USE_DEFAULT_BETA - Vector bn_beta = CONVERT_TO_VECTOR_STRUCT_NO_STEP(bn_beta); -#endif /* USE_DEFAULT_BETA */ -#ifndef USE_DEFAULT_GAMMA - Vector bn_gamma = CONVERT_TO_VECTOR_STRUCT_NO_STEP(bn_gamma); -#endif /* USE_DEFAULT_GAMMA */ - - // In-place ops -#ifdef IN_PLACE_W - Tensor4D fused_w = conv_w; - uint fused_w_stride_x = conv_w_stride_x; -#else /* IN_PLACE_W */ - Tensor4D fused_w = CONVERT_TO_TENSOR4D_STRUCT(fused_w, NUM_CHANNELS); -#endif /* IN_PLACE_W */ -#ifdef IN_PLACE_B - Vector fused_b = conv_b; -#else /* IN_PLACE_B */ - Vector fused_b = CONVERT_TO_VECTOR_STRUCT_NO_STEP(fused_b); -#endif /* IN_PLACE_B */ - - const int current_slice = get_global_id(2) / NUM_CHANNELS; - -#if defined(VEC_SIZE) && defined(LAST_ACCESSED_X) - // Check if access on width gets out of bounds - // If it does shift access vector to access elements within bounds - const int xi = (int)(get_global_id(0) * VEC_SIZE); - conv_w.ptr -= max(xi - (int)LAST_ACCESSED_X, 0) * conv_w_stride_x; - fused_w.ptr -= max(xi - (int)LAST_ACCESSED_X, 0) * fused_w_stride_x; - - // Load W - VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) - wn = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)conv_w.ptr); -#else // !defined(VEC_SIZE) || !defined(LAST_ACCESSED_X) - DATA_TYPE wn = *((__global DATA_TYPE *)(conv_w.ptr)); -#endif // defined(VEC_SIZE) && defined(LAST_ACCESSED_X) - - // rvar = 1 / sqrt(var + epsilon) - const DATA_TYPE var = *((__global DATA_TYPE *)(bn_var.ptr + current_slice * bn_var.stride_x)); - const DATA_TYPE rvar = INVSQRT_OP(ADD_OP(var, SQCVT_SAT((float)EPSILON))); - wn *= rvar; - - // Load b - const DATA_TYPE mean = *((__global DATA_TYPE *)(bn_mean.ptr + current_slice * bn_mean.stride_x)); - DATA_TYPE bn = 0; -#ifdef HAS_BIAS - bn = *((__global DATA_TYPE *)(conv_b.ptr + current_slice * conv_b.stride_x)); -#endif /* HAS_BIAS */ - bn = (bn - mean) * rvar; + int x = get_global_id(0); + int y = get_global_id(1); + int z = get_global_id(2); + int c0 = z % DIM2; + int c1 = z / DIM2; + + int w_offset = x * sizeof(DATA_TYPE) + y * w_stride_y + z * w_stride_z; + int v_offset = c1 * sizeof(DATA_TYPE); + + DATA_TYPE w_old = 0.0f; + DATA_TYPE b_old = 0.0f; + DATA_TYPE w_new = 0.0f; + DATA_TYPE b_new = 0.0f; + DATA_TYPE gamma = 1.0f; + DATA_TYPE mean = 0.0f; + DATA_TYPE var = 1.0f; + DATA_TYPE beta = 0.0f; + + w_old = *((__global DATA_TYPE *)(w_ptr + w_offset + w_offset_first_element_in_bytes)); + var = *((__global DATA_TYPE *)(var_ptr + v_offset + var_offset_first_element_in_bytes)); + mean = *((__global DATA_TYPE *)(mean_ptr + v_offset + mean_offset_first_element_in_bytes)); + +#if defined(GAMMA) + gamma = *((__global DATA_TYPE *)(gamma_ptr + v_offset + gamma_offset_first_element_in_bytes)); +#endif // defined(GAMMA) + + // Compute new weight + w_new = (gamma * w_old) / (sqrt(var + EPSILON)); + +#if defined(IN_PLACE_W) + *((__global DATA_TYPE *)(w_ptr + w_offset + w_offset_first_element_in_bytes)) = w_new; +#else // defined(IN_PLACE_W) + *((__global DATA_TYPE *)(w_fused_ptr + w_offset + w_fused_offset_first_element_in_bytes)) = w_new; +#endif // defined(IN_PLACE_W) + + // Compute bias + if(x == 0 && y == 0 && c0 == 0) + { +#if defined(BIAS) + b_old = *((__global DATA_TYPE *)(b_ptr + v_offset + b_offset_first_element_in_bytes)); +#endif // defined(BIAS) +#if defined(BETA) + beta = *((__global DATA_TYPE *)(beta_ptr + v_offset + beta_offset_first_element_in_bytes)); +#endif // defined(BETA) + + b_new = ((gamma * (b_old - mean)) / (sqrt(var + EPSILON))) + beta; + +#if defined(BIAS) + +#if defined(IN_PLACE_B) + *((__global DATA_TYPE *)(b_ptr + v_offset + b_offset_first_element_in_bytes)) = b_new; +#else // defined(IN_PLACE_B) + *((__global DATA_TYPE *)(b_fused_ptr + v_offset + b_fused_offset_first_element_in_bytes)) = b_new; +#endif // defined(IN_PLACE_B) + +#else // defined(BIAS) -#ifndef USE_DEFAULT_GAMMA - const DATA_TYPE gamma_scalar = *((__global DATA_TYPE *)(bn_gamma.ptr + current_slice * bn_gamma.stride_x)); - wn *= gamma_scalar; - bn *= gamma_scalar; -#endif /* USE_DEFAULT_GAMMA */ - -#ifndef USE_DEFAULT_BETA - const DATA_TYPE beta_scalar = *((__global DATA_TYPE *)(bn_beta.ptr + current_slice * bn_beta.stride_x)); - bn += beta_scalar; -#endif /* USE_DEFAULT_BETA */ - -#if defined(VEC_SIZE) && defined(LAST_ACCESSED_X) - // Store updated weights - VSTORE(VEC_SIZE) - (wn, 0, (__global DATA_TYPE *)fused_w.ptr); -#else // !defined(VEC_SIZE) || !defined(LAST_ACCESSED_X) - *((__global DATA_TYPE *)(fused_w.ptr)) = wn; -#endif // defined(VEC_SIZE) && defined(LAST_ACCESSED_X) +#ifndef IN_PLACE_B + *((__global DATA_TYPE *)(b_fused_ptr + v_offset + b_fused_offset_first_element_in_bytes)) = b_new; +#endif // ifndef IN_PLACE_B - // Store updated bias - *((__global DATA_TYPE *)(fused_b.ptr + current_slice * fused_b.stride_x)) = bn; +#endif // defined(BIAS) + } } -#endif /* defined(NUM_CHANNELS) && defined(DATA_TYPE) && defined(EPSILON) */ +#endif // defined(DIM2) && defined(DATA_TYPE) && defined(EPSILON) \ No newline at end of file diff --git a/src/core/CL/kernels/CLFuseBatchNormalizationKernel.cpp b/src/core/CL/kernels/CLFuseBatchNormalizationKernel.cpp index 150d9b6d1a..16ad7d970e 100644 --- a/src/core/CL/kernels/CLFuseBatchNormalizationKernel.cpp +++ b/src/core/CL/kernels/CLFuseBatchNormalizationKernel.cpp @@ -48,9 +48,9 @@ Status validate_arguments(const ITensorInfo *conv_weights, const ITensorInfo *bn ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(conv_weights, 1, DataType::F16, DataType::F32); ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(bn_mean, bn_var); ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(conv_weights, bn_mean, bn_var); - - unsigned int kernels_idx = get_data_layout_dimension_index(conv_weights->data_layout(), DataLayoutDimension::BATCHES); - ARM_COMPUTE_RETURN_ERROR_ON(conv_weights->dimension(kernels_idx) != bn_mean->dimension(0)); + ARM_COMPUTE_RETURN_ERROR_ON(conv_bias == nullptr && fused_bias == nullptr); + ARM_COMPUTE_RETURN_ERROR_ON(conv_weights->dimension(3) != bn_mean->dimension(0)); + ARM_COMPUTE_RETURN_ERROR_ON(bn_mean->num_dimensions() > 1); // Validate bias if(conv_bias != nullptr) @@ -70,7 +70,6 @@ Status validate_arguments(const ITensorInfo *conv_weights, const ITensorInfo *bn ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(bn_mean, bn_gamma); ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(conv_weights, bn_gamma); } - // Validate output weights if(fused_weights != nullptr && fused_weights->total_size() != 0) { @@ -113,20 +112,18 @@ void CLFuseBatchNormalizationKernel::configure(const ICLTensor *conv_weights, co _epsilon = epsilon; _run_in_place_weights = (fused_weights == nullptr) || (fused_weights == conv_weights); - _run_in_place_bias = (fused_bias == nullptr) || (conv_bias != nullptr && fused_bias == conv_bias); + _run_in_place_bias = (conv_bias != nullptr && fused_bias == nullptr) || (conv_bias != nullptr && fused_bias == conv_bias); // Auto initialize outputs if(_fused_weights != nullptr) { // Output tensor auto initialization if not yet initialized auto_init_if_empty(*_fused_weights->info(), *_conv_weights->info()->clone()); - fused_weights->info()->set_valid_region(conv_weights->info()->valid_region()); } if(_fused_bias != nullptr) { // Output tensor auto initialization if not yet initialized auto_init_if_empty(*_fused_bias->info(), *_bn_mean->info()->clone()); - _fused_bias->info()->set_valid_region(bn_mean->info()->valid_region()); } // Validate arguments @@ -139,35 +136,22 @@ void CLFuseBatchNormalizationKernel::configure(const ICLTensor *conv_weights, co epsilon)); // Configure kernel window - const unsigned int num_elems_processed_per_iteration_x = 4; - const int output_width_x = conv_weights->info()->tensor_shape().x(); - const bool multi_access_x = (output_width_x / num_elems_processed_per_iteration_x > 0); - Window win = calculate_max_window(*conv_weights->info()); - if(multi_access_x) - { - win.set(Window::DimX, Window::Dimension(win.x().start(), - ceil_to_multiple(win.x().end(), num_elems_processed_per_iteration_x), - num_elems_processed_per_iteration_x)); - } ICLKernel::configure_internal(win); // Set build options CLBuildOptions build_opts; build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(conv_weights->info()->data_type())); - build_opts.add_option("-DSELECT_DATA_TYPE=" + get_cl_select_type_from_data_type(conv_weights->info()->data_type())); - build_opts.add_option("-DNUM_CHANNELS=" + support::cpp11::to_string(conv_weights->info()->dimension(2))); + build_opts.add_option("-DDIM2=" + support::cpp11::to_string(conv_weights->info()->dimension(2))); build_opts.add_option("-DEPSILON=" + float_to_string_with_full_precision(epsilon)); - build_opts.add_option_if(multi_access_x, "-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration_x)); - build_opts.add_option_if(multi_access_x, "-DLAST_ACCESSED_X=" + support::cpp11::to_string(std::max(output_width_x - num_elems_processed_per_iteration_x, 0))); build_opts.add_option_if(_run_in_place_weights, "-DIN_PLACE_W"); build_opts.add_option_if(_run_in_place_bias, "-DIN_PLACE_B"); - build_opts.add_option_if(conv_bias != nullptr, "-DHAS_BIAS"); - build_opts.add_option_if(bn_beta == nullptr, "-DUSE_DEFAULT_BETA"); - build_opts.add_option_if(bn_gamma == nullptr, "-DUSE_DEFAULT_GAMMA"); + build_opts.add_option_if(conv_bias != nullptr, "-DBIAS"); + build_opts.add_option_if(bn_beta != nullptr, "-DBETA"); + build_opts.add_option_if(bn_gamma != nullptr, "-DGAMMA"); // Create kernel - _kernel = static_cast(CLKernelLibrary::get().create_kernel("fuse_batchnormalization_layer", build_opts.options())); + _kernel = static_cast(CLKernelLibrary::get().create_kernel("fuse_batchnormalization_conv_layer", build_opts.options())); } Status CLFuseBatchNormalizationKernel::validate(const ITensorInfo *conv_weights, const ITensorInfo *bn_mean, const ITensorInfo *bn_var, @@ -185,37 +169,35 @@ void CLFuseBatchNormalizationKernel::run(const arm_compute::Window &window, cl:: ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); // Create window slice - Window collapsed_window = window.collapse_if_possible(window, Window::DimZ); - Window slice = collapsed_window.first_slice_window_4D(); - - Window vector_slice = window.first_slice_window_1D(); - vector_slice.set(Window::DimX, Window::Dimension(0, 0, 0)); + Window collapsed_window = window.collapse(window, Window::DimZ); + Window slice_1d = window.first_slice_window_1D(); + Window slice_3d = collapsed_window.first_slice_window_3D(); // Add kernel arguments unsigned int idx = 0; - add_4D_tensor_argument(idx, _conv_weights, slice); - add_1D_tensor_argument(idx, _bn_mean, vector_slice); - add_1D_tensor_argument(idx, _bn_var, vector_slice); - if(!_run_in_place_weights) + add_3D_tensor_argument(idx, _conv_weights, slice_3d); + if(_conv_bias != nullptr) { - add_4D_tensor_argument(idx, _fused_weights, slice); + add_1D_tensor_argument(idx, _conv_bias, slice_1d); } - if(!_run_in_place_bias) + add_1D_tensor_argument(idx, _bn_mean, slice_1d); + add_1D_tensor_argument(idx, _bn_var, slice_1d); + if(!_run_in_place_weights) { - add_1D_tensor_argument(idx, _fused_bias, vector_slice); + add_3D_tensor_argument(idx, _fused_weights, slice_3d); } - if(_conv_bias != nullptr) + if(!_run_in_place_bias) { - add_1D_tensor_argument(idx, _conv_bias, vector_slice); + add_1D_tensor_argument(idx, _fused_bias, slice_1d); } if(_bn_beta != nullptr) { - add_1D_tensor_argument(idx, _bn_beta, vector_slice); + add_1D_tensor_argument(idx, _bn_beta, slice_1d); } if(_bn_gamma != nullptr) { - add_1D_tensor_argument(idx, _bn_gamma, vector_slice); + add_1D_tensor_argument(idx, _bn_gamma, slice_1d); } - enqueue(queue, *this, slice); + enqueue(queue, *this, slice_3d); } } // namespace arm_compute diff --git a/tests/validation/CL/FuseBatchNormalization.cpp b/tests/validation/CL/FuseBatchNormalization.cpp new file mode 100644 index 0000000000..92d63c0c3d --- /dev/null +++ b/tests/validation/CL/FuseBatchNormalization.cpp @@ -0,0 +1,147 @@ +/* + * Copyright (c) 2019 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "arm_compute/runtime/CL/functions/CLFuseBatchNormalization.h" +#include "tests/CL/CLAccessor.h" +#include "tests/Globals.h" +#include "tests/datasets/ShapeDatasets.h" +#include "tests/framework/Macros.h" +#include "tests/framework/datasets/Datasets.h" +#include "tests/validation/Validation.h" +#include "tests/validation/fixtures/FuseBatchNormalizationFixture.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace +{ +AbsoluteTolerance absolute_tolerance_f32(0.001f); +AbsoluteTolerance absolute_tolerance_f16(0.2f); +} // namespace + +template +using CLFuseBatchNormalizationConvFixture = FuseBatchNormalizationFixture; + +// *INDENT-OFF* +// clang-format off + +/** Shapes to test - Precommit */ +const auto shape_conv_values_precommit = concat(datasets::Small4DShapes(), datasets::Small3DShapes()); + +/** Shapes to test - Nightly */ +const auto shape_conv_values_nightly = concat(datasets::Large4DShapes(), datasets::Large3DShapes()); + +/** Data layout to test */ +const auto data_layout_values = framework::dataset::make("DataLayout", { DataLayout::NHWC, DataLayout::NCHW }); + +/** In-place flags to test */ +const auto in_place_values = framework::dataset::make("InPlace", { true, false }); + +/** With bias flags to test */ +const auto with_bias_values = framework::dataset::make("WithBias", { true, false }); + +/** With gamma flags to test */ +const auto with_gamma_values = framework::dataset::make("WithGamma", { true, false }); + +/** With beta flags to test */ +const auto with_beta_values = framework::dataset::make("WithBeta", { true, false }); + +TEST_SUITE(CL) +TEST_SUITE(FuseBatchNormalization) +TEST_SUITE(Convolution) +TEST_SUITE(Float) +TEST_SUITE(FP32) +FIXTURE_DATA_TEST_CASE(RunSmall, CLFuseBatchNormalizationConvFixture, framework::DatasetMode::PRECOMMIT, + combine(combine(combine(combine(combine(combine( + shape_conv_values_precommit, + framework::dataset::make("DataType", { DataType::F32 })), + data_layout_values), + in_place_values), + with_bias_values), + with_gamma_values), + with_beta_values)) +{ + // Validate outputs + validate(CLAccessor(_target_w), _reference_w, absolute_tolerance_f32); + validate(CLAccessor(_target_b), _reference_b, absolute_tolerance_f32); +} + +FIXTURE_DATA_TEST_CASE(RunLarge, CLFuseBatchNormalizationConvFixture, framework::DatasetMode::NIGHTLY, + combine(combine(combine(combine(combine(combine( + shape_conv_values_nightly, + framework::dataset::make("DataType", { DataType::F32 })), + data_layout_values), + in_place_values), + with_bias_values), + with_gamma_values), + with_beta_values)) +{ + // Validate outputs + validate(CLAccessor(_target_w), _reference_w, absolute_tolerance_f32); + validate(CLAccessor(_target_b), _reference_b, absolute_tolerance_f32); +} + +TEST_SUITE_END() // FP32 + +TEST_SUITE(FP16) +FIXTURE_DATA_TEST_CASE(RunSmall, CLFuseBatchNormalizationConvFixture, framework::DatasetMode::PRECOMMIT, + combine(combine(combine(combine(combine(combine( + shape_conv_values_precommit, + framework::dataset::make("DataType", { DataType::F16 })), + data_layout_values), + in_place_values), + with_bias_values), + with_gamma_values), + with_beta_values)) +{ + // Validate outputs + validate(CLAccessor(_target_w), _reference_w, absolute_tolerance_f16); + validate(CLAccessor(_target_b), _reference_b, absolute_tolerance_f16); +} + +FIXTURE_DATA_TEST_CASE(RunLarge, CLFuseBatchNormalizationConvFixture, framework::DatasetMode::NIGHTLY, + combine(combine(combine(combine(combine(combine( + shape_conv_values_nightly, + framework::dataset::make("DataType", { DataType::F16 })), + data_layout_values), + in_place_values), + with_bias_values), + with_gamma_values), + with_beta_values)) +{ + // Validate outputs + validate(CLAccessor(_target_w), _reference_w, absolute_tolerance_f16); + validate(CLAccessor(_target_b), _reference_b, absolute_tolerance_f16); +} + +TEST_SUITE_END() // FP16 +TEST_SUITE_END() // Float +TEST_SUITE_END() // Convolution +TEST_SUITE_END() // FuseBatchNormalization +TEST_SUITE_END() // CL +} // namespace validation +} // namespace test +} // namespace arm_compute \ No newline at end of file diff --git a/tests/validation/fixtures/FuseBatchNormalizationFixture.h b/tests/validation/fixtures/FuseBatchNormalizationFixture.h new file mode 100644 index 0000000000..864d627ed7 --- /dev/null +++ b/tests/validation/fixtures/FuseBatchNormalizationFixture.h @@ -0,0 +1,204 @@ +/* + * Copyright (c) 2019 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef ARM_COMPUTE_TEST_FUSEBATCHNORMALIZATION_FIXTURE +#define ARM_COMPUTE_TEST_FUSEBATCHNORMALIZATION_FIXTURE + +#include "arm_compute/core/TensorShape.h" +#include "arm_compute/core/Types.h" +#include "arm_compute/runtime/CL/functions/CLFuseBatchNormalization.h" +#include "tests/AssetsLibrary.h" +#include "tests/Globals.h" +#include "tests/IAccessor.h" +#include "tests/framework/Asserts.h" +#include "tests/framework/Fixture.h" +#include "tests/validation/Helpers.h" +#include "tests/validation/reference/FuseBatchNormalization.h" + +#include +#include + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +template +class FuseBatchNormalizationFixture : public framework::Fixture +{ +public: + template + void setup(TensorShape shape_w, DataType data_type, DataLayout data_layout, bool in_place, bool with_bias, bool with_gamma, bool with_beta) + { + std::tie(_target_w, _target_b) = compute_target(shape_w, data_type, data_layout, in_place, with_bias, with_gamma, with_beta); + std::tie(_reference_w, _reference_b) = compute_reference(shape_w, data_type, data_layout, with_bias, with_gamma, with_beta); + } + +protected: + template + void fill(U &&tensor, int i, float min, float max) + { + library->fill_tensor_uniform(tensor, i, min, max); + } + + std::pair compute_target(TensorShape shape_w, DataType data_type, DataLayout data_layout, bool in_place, bool with_bias, bool with_gamma, bool with_beta) + { + const TensorShape shape_v(shape_w[dims_weights - 1]); + + if(data_layout == DataLayout::NHWC) + { + permute(shape_w, PermutationVector(2U, 0U, 1U)); + } + + const bool in_place_w = in_place; + const bool in_place_b = with_bias ? in_place : false; + + // Create tensors + TensorType w = create_tensor(shape_w, data_type, 1, QuantizationInfo(), data_layout); + TensorType b = create_tensor(shape_v, data_type); + TensorType mean = create_tensor(shape_v, data_type); + TensorType var = create_tensor(shape_v, data_type); + TensorType w_fused = create_tensor(shape_w, data_type, 1, QuantizationInfo(), data_layout); + TensorType b_fused = create_tensor(shape_v, data_type); + TensorType beta = create_tensor(shape_v, data_type); + TensorType gamma = create_tensor(shape_v, data_type); + + auto b_to_use = with_bias ? &b : nullptr; + auto gamma_to_use = with_gamma ? &gamma : nullptr; + auto beta_to_use = with_beta ? &beta : nullptr; + auto w_fused_to_use = in_place_w ? nullptr : &w_fused; + auto b_fused_to_use = in_place_b ? nullptr : &b_fused; + + // Create and configure function + FunctionType fuse_batch_normalization; + 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); + + ARM_COMPUTE_EXPECT(w.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(b.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(mean.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(var.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(w_fused.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(b_fused.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(beta.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(gamma.info()->is_resizable(), framework::LogLevel::ERRORS); + + // Allocate tensors + w.allocator()->allocate(); + b.allocator()->allocate(); + mean.allocator()->allocate(); + var.allocator()->allocate(); + w_fused.allocator()->allocate(); + b_fused.allocator()->allocate(); + beta.allocator()->allocate(); + gamma.allocator()->allocate(); + + ARM_COMPUTE_EXPECT(!w.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!b.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!mean.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!var.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!w_fused.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!b_fused.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!beta.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!gamma.info()->is_resizable(), framework::LogLevel::ERRORS); + + // Fill tensors + fill(AccessorType(w), 0U, -1.0f, 1.0f); + fill(AccessorType(b), 1U, -1.0f, 1.0f); + fill(AccessorType(mean), 2U, -1.0f, 1.0f); + fill(AccessorType(var), 3U, 0.0f, 1.0f); + fill(AccessorType(beta), 4U, -1.0f, 1.0f); + fill(AccessorType(gamma), 5U, -1.0f, 1.0f); + + // Compute function + fuse_batch_normalization.run(); + + return std::make_pair(std::move(in_place_w ? w : w_fused), std::move(in_place_b ? b : b_fused)); + } + + std::pair, SimpleTensor> compute_reference(TensorShape shape_w, DataType data_type, DataLayout data_layout, bool with_bias, bool with_gamma, bool with_beta) + { + const TensorShape shape_v(shape_w[dims_weights - 1]); + + SimpleTensor w{ shape_w, data_type }; + SimpleTensor b{ shape_v, data_type }; + SimpleTensor mean{ shape_v, data_type }; + SimpleTensor var{ shape_v, data_type }; + SimpleTensor w_fused{ shape_w, data_type }; + SimpleTensor b_fused{ shape_v, data_type }; + SimpleTensor beta{ shape_v, data_type }; + SimpleTensor gamma{ shape_v, data_type }; + + // Fill reference tensor + fill(w, 0U, -1.0f, 1.0f); + fill(b, 1U, -1.0f, 1.0f); + fill(mean, 2U, -1.0f, 1.0f); + fill(var, 3U, 0.0f, 1.0f); + fill(beta, 4U, -1.0f, 1.0f); + fill(gamma, 5U, -1.0f, 1.0f); + + if(!with_bias) + { + // Fill with zeros + fill(b, 0U, 0.0f, 0.0f); + } + + if(!with_gamma) + { + // Fill with ones + fill(gamma, 0U, 1.0f, 1.0f); + } + + if(!with_beta) + { + // Fill with zeros + fill(beta, 0U, 0.0f, 0.0f); + } + + switch(dims_weights) + { + case 3: + // Weights for depth wise convolution layer + reference::fuse_batch_normalization_dwc_layer(w, mean, var, w_fused, b_fused, b, beta, gamma, _epsilon); + break; + case 4: + // Weights for convolution layer + reference::fuse_batch_normalization_conv_layer(w, mean, var, w_fused, b_fused, b, beta, gamma, _epsilon); + break; + default: + ARM_COMPUTE_ERROR("Not supported number of dimensions for the input weights tensor"); + } + + return std::make_pair(std::move(w_fused), std::move(b_fused)); + } + + const float _epsilon{ 0.0001f }; + TensorType _target_w{}; + TensorType _target_b{}; + SimpleTensor _reference_w{}; + SimpleTensor _reference_b{}; +}; +} // namespace validation +} // namespace test +} // namespace arm_compute +#endif /* ARM_COMPUTE_TEST_FUSEBATCHNORMALIZATION_FIXTURE */ diff --git a/tests/validation/reference/FuseBatchNormalization.cpp b/tests/validation/reference/FuseBatchNormalization.cpp new file mode 100644 index 0000000000..df12b25912 --- /dev/null +++ b/tests/validation/reference/FuseBatchNormalization.cpp @@ -0,0 +1,111 @@ +/* + * Copyright (c) 2019 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "FuseBatchNormalization.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace reference +{ +template +void fuse_batch_normalization_dwc_layer(const SimpleTensor &w, const SimpleTensor &mean, const SimpleTensor &var, SimpleTensor &w_fused, SimpleTensor &b_fused, const SimpleTensor &b, + const SimpleTensor &beta, const SimpleTensor &gamma, float epsilon) +{ + const auto *w_data = w.data(); + const auto *b_data = b.data(); + + auto *w_fused_data = w_fused.data(); + auto *b_fused_data = b_fused.data(); + + const unsigned int width = w.shape()[0]; + const unsigned int height = w.shape()[1]; + const unsigned int dim2 = w.shape()[2]; + + for(unsigned int b = 0; b < dim2; ++b) + { + const auto mean_val = mean.data()[b]; + const auto var_val = var.data()[b]; + const auto beta_val = beta.data()[b]; + const auto gamma_val = gamma.data()[b]; + + for(unsigned int i = 0; i < width * height; ++i) + { + unsigned int index = i + b * width * height; + + w_fused_data[index] = (gamma_val * (w_data[index])) / sqrt(var_val + epsilon); + } + + b_fused_data[b] = (b_data[b] - mean_val) / sqrt(var_val + epsilon) * gamma_val + beta_val; + } +} + +template +void fuse_batch_normalization_conv_layer(const SimpleTensor &w, const SimpleTensor &mean, const SimpleTensor &var, SimpleTensor &w_fused, SimpleTensor &b_fused, + const SimpleTensor &b, + const SimpleTensor &beta, const SimpleTensor &gamma, float epsilon) +{ + const auto *w_data = w.data(); + const auto *b_data = b.data(); + + auto *w_fused_data = w_fused.data(); + auto *b_fused_data = b_fused.data(); + + const unsigned int width = w.shape()[0]; + const unsigned int height = w.shape()[1]; + const unsigned int dim2 = w.shape()[2]; + const unsigned int dim3 = w.shape()[3]; + + for(unsigned int b = 0; b < dim3; ++b) + { + const auto mean_val = mean.data()[b]; + const auto var_val = var.data()[b]; + const auto beta_val = beta.data()[b]; + const auto gamma_val = gamma.data()[b]; + + for(unsigned int i = 0; i < width * height * dim2; ++i) + { + unsigned int index = i + b * width * height * dim2; + + w_fused_data[index] = (gamma_val * (w_data[index])) / sqrt(var_val + epsilon); + } + + b_fused_data[b] = (b_data[b] - mean_val) / sqrt(var_val + epsilon) * gamma_val + beta_val; + } +} + +template void fuse_batch_normalization_dwc_layer(const SimpleTensor &w, const SimpleTensor &mean, const SimpleTensor &var, SimpleTensor &w_fused, + SimpleTensor &b_fused, const SimpleTensor &b, const SimpleTensor &beta, const SimpleTensor &gamma, float epsilon); +template void fuse_batch_normalization_dwc_layer(const SimpleTensor &w, const SimpleTensor &mean, const SimpleTensor &var, SimpleTensor &w_fused, SimpleTensor &b_fused, + const SimpleTensor &b, const SimpleTensor &beta, const SimpleTensor &gamma, float epsilon); +template void fuse_batch_normalization_conv_layer(const SimpleTensor &w, const SimpleTensor &mean, const SimpleTensor &var, SimpleTensor &w_fused, + SimpleTensor &b_fused, const SimpleTensor &b, const SimpleTensor &beta, const SimpleTensor &gamma, float epsilon); +template void fuse_batch_normalization_conv_layer(const SimpleTensor &w, const SimpleTensor &mean, const SimpleTensor &var, SimpleTensor &w_fused, SimpleTensor &b_fused, + const SimpleTensor &b, const SimpleTensor &beta, const SimpleTensor &gamma, float epsilon); +} // namespace reference +} // namespace validation +} // namespace test +} // namespace arm_compute diff --git a/tests/validation/reference/FuseBatchNormalization.h b/tests/validation/reference/FuseBatchNormalization.h new file mode 100644 index 0000000000..1575fc0acc --- /dev/null +++ b/tests/validation/reference/FuseBatchNormalization.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2019 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef __ARM_COMPUTE_TEST_FUSEBATCHNORMALIZATION_H__ +#define __ARM_COMPUTE_TEST_FUSEBATCHNORMALIZATION_H__ + +#include "tests/SimpleTensor.h" +#include "tests/validation/Helpers.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace reference +{ +template +void fuse_batch_normalization_dwc_layer(const SimpleTensor &w, const SimpleTensor &mean, const SimpleTensor &var, SimpleTensor &w_fused, SimpleTensor &b_fused, const SimpleTensor &b, + const SimpleTensor &beta, const SimpleTensor &gamma, float epsilon); + +template +void fuse_batch_normalization_conv_layer(const SimpleTensor &w, const SimpleTensor &mean, const SimpleTensor &var, SimpleTensor &w_fused, SimpleTensor &b_fused, + const SimpleTensor &b, + const SimpleTensor &beta, const SimpleTensor &gamma, float epsilon); + +} // namespace reference +} // namespace validation +} // namespace test +} // namespace arm_compute +#endif // __ARM_COMPUTE_TEST_FUSEBATCHNORMALIZATION_H__ -- cgit v1.2.1