From 2732cca12bac29e1515cee1db5005c73893c61b4 Mon Sep 17 00:00:00 2001 From: Manuel Bottini Date: Tue, 28 May 2019 11:44:41 +0100 Subject: COMPMID-2244: Extend CLFuseBatchNormalization to support DepthwiseConvolution weights Change-Id: I7d1907f35cc4899379073759be2f7cce24e51e9d Signed-off-by: Manuel Bottini Reviewed-on: https://review.mlplatform.org/c/1327 Reviewed-by: Gian Marco Iodice Comments-Addressed: Arm Jenkins Tested-by: Arm Jenkins --- .../CL/kernels/CLFuseBatchNormalizationKernel.h | 76 ++++++++--------- arm_compute/core/Types.h | 7 ++ .../CL/functions/CLFuseBatchNormalization.h | 54 ++++++------ src/core/CL/CLKernelLibrary.cpp | 2 +- src/core/CL/cl_kernels/batchnormalization_layer.cl | 58 ++++++++----- .../CL/kernels/CLFuseBatchNormalizationKernel.cpp | 95 ++++++++++++---------- .../CL/functions/CLFuseBatchNormalization.cpp | 18 ++-- tests/validation/CL/FuseBatchNormalization.cpp | 73 +++++++++++++++++ .../fixtures/FuseBatchNormalizationFixture.h | 5 +- 9 files changed, 251 insertions(+), 137 deletions(-) diff --git a/arm_compute/core/CL/kernels/CLFuseBatchNormalizationKernel.h b/arm_compute/core/CL/kernels/CLFuseBatchNormalizationKernel.h index a5b98bb27d..d2df0897d4 100644 --- a/arm_compute/core/CL/kernels/CLFuseBatchNormalizationKernel.h +++ b/arm_compute/core/CL/kernels/CLFuseBatchNormalizationKernel.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018 ARM Limited. + * Copyright (c) 2018-2019 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -49,57 +49,59 @@ public: ~CLFuseBatchNormalizationKernel() = default; /** Set the source, destination of the kernel * - * @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. 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 + * @param[in] input_weights Input weights tensor for convolution or depthwise convolution layer. Data type supported: F16/F32. Data layout supported: NCHW, NHWC + * @param[in] bn_mean Batch normalization layer mean tensor. Same as @p input_weights + * @param[in] bn_var Batch normalization layer variance tensor. Same as @p input_weights + * @param[out] fused_weights Output fused weights tensor. It can be a nullptr in case of in-place computation. Same as @p input_weights + * @param[out] fused_bias Output fused bias tensor. It can be a nullptr in case of in-place computation and input_bias != nullptr. Same as @p input_weights + * @param[in] input_bias (Optional) Input bias tensor for convolution or depthwise convolution layer. It can be a nullptr in case the bias tensor is not required. Same as @p input_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 input_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 + * @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 input_weights * @note if nullptr, bn_gamma is set to 1.0 * @param[in] epsilon (Optional) Batch normalization layer epsilon parameter. Defaults to 0.001f. + * @param[in] fbn_type (Optional) Fused batch normalization type. Defaults to CONVOLUTION. */ - void configure(const ICLTensor *conv_weights, const ICLTensor *bn_mean, const ICLTensor *bn_var, ICLTensor *fused_weights, ICLTensor *fused_bias, - const ICLTensor *conv_bias = nullptr, const ICLTensor *bn_beta = nullptr, const ICLTensor *bn_gamma = nullptr, - float epsilon = 0.001f); + void configure(const ICLTensor *input_weights, const ICLTensor *bn_mean, const ICLTensor *bn_var, ICLTensor *fused_weights, ICLTensor *fused_bias, + const ICLTensor *input_bias = nullptr, const ICLTensor *bn_beta = nullptr, const ICLTensor *bn_gamma = nullptr, + float epsilon = 0.001f, FuseBatchNormalizationType fbn_type = FuseBatchNormalizationType::CONVOLUTION); /** Static function to check if given info will lead to a valid configuration of @ref CLFuseBatchNormalizationKernel * - * @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. + * @param[in] input_weights Input weights tensor info for convolution or depthwise convolution layer. Data type supported: F16/F32. Data layout supported: NCHW, NHWC + * @param[in] bn_mean Batch normalization layer mean tensor info. Same as @p input_weights + * @param[in] bn_var Batch normalization layer variance tensor info. Same as @p input_weights + * @param[in] fused_weights Output fused weights tensor info. It can be a nullptr in case of in-place computation. Same as @p input_weights + * @param[in] fused_bias Output fused bias tensor info. It can be a nullptr in case of in-place computation and input_bias != nullptr. Same as @p input_weights + * @param[in] input_bias (Optional) Input bias tensor info for convolution or depthwise convolution layer. It can be a nullptr in case the bias tensor is not required. Same as @p input_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 input_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 input_weights + * @note if nullptr, bn_gamma is set to 1.0 + * @param[in] epsilon (Optional) Batch normalization layer epsilon parameter. Defaults to 0.001f. + * @param[in] fbn_type (Optional) Fused batch normalization type. Defaults to CONVOLUTION. * * @return a status */ - static Status validate(const ITensorInfo *conv_weights, const ITensorInfo *bn_mean, const ITensorInfo *bn_var, + static Status validate(const ITensorInfo *input_weights, const ITensorInfo *bn_mean, const ITensorInfo *bn_var, const ITensorInfo *fused_weights, const ITensorInfo *fused_bias, - const ITensorInfo *conv_bias = nullptr, const ITensorInfo *bn_beta = nullptr, const ITensorInfo *bn_gamma = nullptr, - float epsilon = 0.001f); + const ITensorInfo *input_bias = nullptr, const ITensorInfo *bn_beta = nullptr, const ITensorInfo *bn_gamma = nullptr, + float epsilon = 0.001f, FuseBatchNormalizationType fbn_type = FuseBatchNormalizationType::CONVOLUTION); // Inherited methods overridden: void run(const Window &window, cl::CommandQueue &queue) override; private: - const ICLTensor *_conv_weights; - const ICLTensor *_conv_bias; - const ICLTensor *_bn_mean; - const ICLTensor *_bn_var; - const ICLTensor *_bn_gamma; - const ICLTensor *_bn_beta; - ICLTensor *_fused_weights; - ICLTensor *_fused_bias; - float _epsilon; - bool _run_in_place_weights; - bool _run_in_place_bias; + const ICLTensor *_input_weights; + const ICLTensor *_input_bias; + const ICLTensor *_bn_mean; + const ICLTensor *_bn_var; + const ICLTensor *_bn_gamma; + const ICLTensor *_bn_beta; + ICLTensor *_fused_weights; + ICLTensor *_fused_bias; + float _epsilon; + bool _run_in_place_weights; + bool _run_in_place_bias; }; } // namespace arm_compute #endif /*__ARM_COMPUTE_CLFUSEBATCHNORMALIZATIONKERNEL_H__ */ diff --git a/arm_compute/core/Types.h b/arm_compute/core/Types.h index d49315d591..1a49624113 100644 --- a/arm_compute/core/Types.h +++ b/arm_compute/core/Types.h @@ -142,6 +142,13 @@ enum class DeconvolutionMethod DIRECT, /**< Direct deconvolution */ }; +/** Available FuseBatchNormalizationType*/ +enum class FuseBatchNormalizationType +{ + CONVOLUTION, /**< For Convolution weights */ + DEPTHWISECONVOLUTION /**< For Depthwise Convolution weights*/ +}; + /** Padding mode to use for PadLayer */ enum class PaddingMode { diff --git a/arm_compute/runtime/CL/functions/CLFuseBatchNormalization.h b/arm_compute/runtime/CL/functions/CLFuseBatchNormalization.h index 4e7f1cba74..50385d438d 100644 --- a/arm_compute/runtime/CL/functions/CLFuseBatchNormalization.h +++ b/arm_compute/runtime/CL/functions/CLFuseBatchNormalization.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018 ARM Limited. + * Copyright (c) 2018-2019 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -51,41 +51,43 @@ public: ~CLFuseBatchNormalization() = default; /** Set the input and output tensors. * - * @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. 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 + * @param[in] input_weights Input weights tensor for convolution or depthwise convolution layer. Data type supported: F16/F32. Data layout supported: NCHW, NHWC + * @param[in] bn_mean Batch normalization layer mean tensor. Same as @p input_weights + * @param[in] bn_var Batch normalization layer variance tensor. Same as @p input_weights + * @param[out] fused_weights Output fused weights tensor. It can be a nullptr in case of in-place computation. Same as @p input_weights + * @param[out] fused_bias Output fused bias tensor. It can be a nullptr in case of in-place computation and input_bias != nullptr. Same as @p input_weights + * @param[in] input_bias (Optional) Input bias tensor for convolution or depthwise convolution layer. It can be a nullptr in case the bias tensor is not required. Same as @p input_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 input_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 + * @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 input_weights * @note if nullptr, bn_gamma is set to 1.0 * @param[in] epsilon (Optional) Batch normalization layer epsilon parameter. Defaults to 0.001f. + * @param[in] fbn_type (Optional) Fused batch normalization type. Defaults to Convolution. */ - void configure(const ICLTensor *conv_weights, const ICLTensor *bn_mean, const ICLTensor *bn_var, ICLTensor *fused_weights, ICLTensor *fused_bias, - const ICLTensor *conv_bias = nullptr, const ICLTensor *bn_beta = nullptr, const ICLTensor *bn_gamma = nullptr, - float epsilon = 0.001f); + void configure(const ICLTensor *input_weights, const ICLTensor *bn_mean, const ICLTensor *bn_var, ICLTensor *fused_weights, ICLTensor *fused_bias, + const ICLTensor *input_bias = nullptr, const ICLTensor *bn_beta = nullptr, const ICLTensor *bn_gamma = nullptr, + float epsilon = 0.001f, FuseBatchNormalizationType fbn_type = FuseBatchNormalizationType::CONVOLUTION); /** Static function to check if given info will lead to a valid configuration of @ref CLFuseBatchNormalization * - * @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. + * @param[in] input_weights Input weights tensor info for convolution or depthwise convolution layer. Data type supported: F16/F32. Data layout supported: NCHW, NHWC + * @param[in] bn_mean Batch normalization layer mean tensor info. Same as @p input_weights + * @param[in] bn_var Batch normalization layer variance tensor info. Same as @p input_weights + * @param[in] fused_weights Output fused weights tensor info. It can be a nullptr in case of in-place computation. Same as @p input_weights + * @param[in] fused_bias Output fused bias tensor info. It can be a nullptr in case of in-place computation and input_bias != nullptr. Same as @p input_weights + * @param[in] input_bias (Optional) Input bias tensor info for convolution or depthwise convolution layer. It can be a nullptr in case the bias tensor is not required. Same as @p input_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 input_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 input_weights + * @note if nullptr, bn_gamma is set to 1.0 + * @param[in] epsilon (Optional) Batch normalization layer epsilon parameter. Defaults to 0.001f. + * @param[in] fbn_type (Optional) Fused batch normalization type. Defaults to Convolution. * * @return a status */ - static Status validate(const ITensorInfo *conv_weights, const ITensorInfo *bn_mean, const ITensorInfo *bn_var, + static Status validate(const ITensorInfo *input_weights, const ITensorInfo *bn_mean, const ITensorInfo *bn_var, const ITensorInfo *fused_weights, const ITensorInfo *fused_bias, - const ITensorInfo *conv_bias = nullptr, const ITensorInfo *bn_beta = nullptr, const ITensorInfo *bn_gamma = nullptr, - float epsilon = 0.001f); + const ITensorInfo *input_bias = nullptr, const ITensorInfo *bn_beta = nullptr, const ITensorInfo *bn_gamma = nullptr, + float epsilon = 0.001f, FuseBatchNormalizationType fbn_type = FuseBatchNormalizationType::CONVOLUTION); // Inherited methods overridden: void run() override; diff --git a/src/core/CL/CLKernelLibrary.cpp b/src/core/CL/CLKernelLibrary.cpp index 51acd9f339..253da40077 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_conv_layer", "batchnormalization_layer.cl" }, + { "fuse_batchnormalization_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 a5321315d3..918caff212 100644 --- a/src/core/CL/cl_kernels/batchnormalization_layer.cl +++ b/src/core/CL/cl_kernels/batchnormalization_layer.cl @@ -259,12 +259,14 @@ __kernel void batchnormalization_layer_nhwc(TENSOR3D_DECLARATION(input), } #endif /* defined(VEC_SIZE) && defined(DATA_TYPE) && defined(DATA_TYPE)*/ -#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 +#if defined(DATA_TYPE) && defined(EPSILON) +/** OpenCL kernel to fuse the weights of convolution or depthwise convolution layer with batch normalization when the data layout is either NCHW or NHWC * * @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 The third dimension of the input tensor should be passed at compile time when weights belong to a convolution layer using -DDIM2=size. e.g. -DDIM2=16. + * For depthwise convolution weight do not pass DIM2 + * @note Data layout NHWC should be passed at compile time with -DNHWC. For data layout NCHW it is not required to pass any parameter * @note Batch normalization epsilon parameter should be passed at compile time using -DEPSILON=value. e.g. -DEPSILON=0.001f * * @param[in] w_ptr Pointer to the weights tensor. Supported data types: F16/F32 @@ -312,35 +314,45 @@ __kernel void batchnormalization_layer_nhwc(TENSOR3D_DECLARATION(input), * @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_conv_layer(TENSOR3D_DECLARATION(w), +__kernel void fuse_batchnormalization_layer(TENSOR3D_DECLARATION(w), #if defined(BIAS) - VECTOR_DECLARATION(b), + VECTOR_DECLARATION(b), #endif // defined(BIAS) - VECTOR_DECLARATION(mean), - VECTOR_DECLARATION(var) + VECTOR_DECLARATION(mean), + VECTOR_DECLARATION(var) #ifndef IN_PLACE_W - , - TENSOR3D_DECLARATION(w_fused) + , + TENSOR3D_DECLARATION(w_fused) #endif // ifndef IN_PLACE_W #ifndef IN_PLACE_B - , - VECTOR_DECLARATION(b_fused) + , + VECTOR_DECLARATION(b_fused) #endif // ifndef IN_PLACE_B #if defined(BETA) - , - VECTOR_DECLARATION(beta) + , + VECTOR_DECLARATION(beta) #endif // defined(BETA) #if defined(GAMMA) - , - VECTOR_DECLARATION(gamma) + , + VECTOR_DECLARATION(gamma) #endif // defined(GAMMA) - ) + ) { - int x = get_global_id(0); - int y = get_global_id(1); - int z = get_global_id(2); + int x = get_global_id(0); + int y = get_global_id(1); + int z = get_global_id(2); + +#if defined(DIM2) int c0 = z % DIM2; int c1 = z / DIM2; +#else // ! defined(DIM2) + int c0 = 0; +#if defined(NHWC) + int c1 = x; +#else // defined(NHWC) + int c1 = z; +#endif // defined(NHWC) +#endif // defined(DIM2) int w_offset = x * sizeof(DATA_TYPE) + y * w_stride_y + z * w_stride_z; int v_offset = c1 * sizeof(DATA_TYPE); @@ -368,11 +380,15 @@ __kernel void fuse_batchnormalization_conv_layer(TENSOR3D_DECLARATION(w), #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; + *((__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 !defined(DIM2) && defined(NHWC) + if(z == 0 && y == 0) +#else !defined(DIM2) && defined(NHWC) if(x == 0 && y == 0 && c0 == 0) +#endif // !defined(DIM2) && defined(NHWC) { #if defined(BIAS) b_old = *((__global DATA_TYPE *)(b_ptr + v_offset + b_offset_first_element_in_bytes)); @@ -400,4 +416,4 @@ __kernel void fuse_batchnormalization_conv_layer(TENSOR3D_DECLARATION(w), #endif // defined(BIAS) } } -#endif // defined(DIM2) && defined(DATA_TYPE) && defined(EPSILON) \ No newline at end of file +#endif // 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 16ad7d970e..bf827bf2c2 100644 --- a/src/core/CL/kernels/CLFuseBatchNormalizationKernel.cpp +++ b/src/core/CL/kernels/CLFuseBatchNormalizationKernel.cpp @@ -38,50 +38,60 @@ namespace arm_compute { namespace { -Status validate_arguments(const ITensorInfo *conv_weights, const ITensorInfo *bn_mean, const ITensorInfo *bn_var, +Status validate_arguments(const ITensorInfo *input_weights, const ITensorInfo *bn_mean, const ITensorInfo *bn_var, const ITensorInfo *fused_weights, const ITensorInfo *fused_bias, - const ITensorInfo *conv_bias, const ITensorInfo *bn_beta, const ITensorInfo *bn_gamma, - float epsilon) + const ITensorInfo *input_bias, const ITensorInfo *bn_beta, const ITensorInfo *bn_gamma, + float epsilon, FuseBatchNormalizationType fbn_type) { ARM_COMPUTE_UNUSED(epsilon); - ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(conv_weights); - ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(conv_weights, 1, DataType::F16, DataType::F32); + ARM_COMPUTE_ERROR_ON_NULLPTR(input_weights, bn_mean, bn_var); + ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input_weights); + ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input_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); - 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_MISMATCHING_DATA_TYPES(input_weights, bn_mean, bn_var); + ARM_COMPUTE_RETURN_ERROR_ON(input_bias == nullptr && fused_bias == nullptr); ARM_COMPUTE_RETURN_ERROR_ON(bn_mean->num_dimensions() > 1); + if(fbn_type == FuseBatchNormalizationType::CONVOLUTION) + { + ARM_COMPUTE_RETURN_ERROR_ON(input_weights->dimension(3) != bn_mean->dimension(0)); + } + else + { + const size_t channel_idx = get_data_layout_dimension_index(input_weights->data_layout(), DataLayoutDimension::CHANNEL); + ARM_COMPUTE_RETURN_ERROR_ON(input_weights->dimension(channel_idx) != bn_mean->dimension(0)); + } + // Validate bias - if(conv_bias != nullptr) + if(input_bias != nullptr) { - ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(bn_mean, conv_bias); - ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(conv_weights, conv_bias); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(bn_mean, input_bias); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_weights, input_bias); } // Validate beta if(bn_beta != nullptr) { ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(bn_mean, bn_beta); - ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(conv_weights, bn_beta); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_weights, bn_beta); } // Validate gamma if(bn_gamma != nullptr) { ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(bn_mean, bn_gamma); - ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(conv_weights, bn_gamma); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_weights, bn_gamma); } // Validate output weights if(fused_weights != nullptr && fused_weights->total_size() != 0) { - ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(conv_weights, fused_weights); - ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(conv_weights, fused_weights); - ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(conv_weights, fused_weights); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input_weights, fused_weights); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input_weights, fused_weights); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_weights, fused_weights); } // Validate output bias if(fused_bias != nullptr && fused_bias->total_size() != 0) { ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(bn_mean, fused_bias); - ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(conv_weights, fused_bias); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_weights, fused_bias); } return Status{}; @@ -89,20 +99,20 @@ Status validate_arguments(const ITensorInfo *conv_weights, const ITensorInfo *bn } // namespace CLFuseBatchNormalizationKernel::CLFuseBatchNormalizationKernel() - : _conv_weights(nullptr), _conv_bias(nullptr), _bn_mean(nullptr), _bn_var(nullptr), _bn_gamma(nullptr), _bn_beta(nullptr), _fused_weights(nullptr), _fused_bias(nullptr), _epsilon(), + : _input_weights(nullptr), _input_bias(nullptr), _bn_mean(nullptr), _bn_var(nullptr), _bn_gamma(nullptr), _bn_beta(nullptr), _fused_weights(nullptr), _fused_bias(nullptr), _epsilon(), _run_in_place_weights(false), _run_in_place_bias(false) { } -void CLFuseBatchNormalizationKernel::configure(const ICLTensor *conv_weights, const ICLTensor *bn_mean, const ICLTensor *bn_var, +void CLFuseBatchNormalizationKernel::configure(const ICLTensor *input_weights, const ICLTensor *bn_mean, const ICLTensor *bn_var, ICLTensor *fused_weights, ICLTensor *fused_bias, - const ICLTensor *conv_bias, const ICLTensor *bn_beta, const ICLTensor *bn_gamma, - float epsilon) + const ICLTensor *input_bias, const ICLTensor *bn_beta, const ICLTensor *bn_gamma, + float epsilon, FuseBatchNormalizationType fbn_type) { - ARM_COMPUTE_ERROR_ON_NULLPTR(conv_weights, bn_mean, bn_var); + ARM_COMPUTE_ERROR_ON_NULLPTR(input_weights, bn_mean, bn_var); - _conv_weights = conv_weights; - _conv_bias = conv_bias; + _input_weights = input_weights; + _input_bias = input_bias; _bn_mean = bn_mean; _bn_var = bn_var; _bn_beta = bn_beta; @@ -111,14 +121,14 @@ void CLFuseBatchNormalizationKernel::configure(const ICLTensor *conv_weights, co _fused_bias = fused_bias; _epsilon = epsilon; - _run_in_place_weights = (fused_weights == nullptr) || (fused_weights == conv_weights); - _run_in_place_bias = (conv_bias != nullptr && fused_bias == nullptr) || (conv_bias != nullptr && fused_bias == conv_bias); + _run_in_place_weights = (fused_weights == nullptr) || (fused_weights == input_weights); + _run_in_place_bias = (input_bias != nullptr && fused_bias == nullptr) || (input_bias != nullptr && fused_bias == input_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()); + auto_init_if_empty(*_fused_weights->info(), *_input_weights->info()->clone()); } if(_fused_bias != nullptr) { @@ -127,39 +137,40 @@ void CLFuseBatchNormalizationKernel::configure(const ICLTensor *conv_weights, co } // Validate arguments - ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(conv_weights->info(), bn_mean->info(), bn_var->info(), + ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input_weights->info(), bn_mean->info(), bn_var->info(), (fused_weights != nullptr) ? fused_weights->info() : nullptr, (fused_bias != nullptr) ? fused_bias->info() : nullptr, - (conv_bias != nullptr) ? conv_bias->info() : nullptr, + (input_bias != nullptr) ? input_bias->info() : nullptr, (bn_beta != nullptr) ? bn_beta->info() : nullptr, (bn_gamma != nullptr) ? bn_gamma->info() : nullptr, - epsilon)); + epsilon, fbn_type)); // Configure kernel window - Window win = calculate_max_window(*conv_weights->info()); + Window win = calculate_max_window(*input_weights->info()); 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("-DDIM2=" + support::cpp11::to_string(conv_weights->info()->dimension(2))); + build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input_weights->info()->data_type())); + build_opts.add_option_if(fbn_type == FuseBatchNormalizationType::CONVOLUTION, "-DDIM2=" + support::cpp11::to_string(input_weights->info()->dimension(2))); build_opts.add_option("-DEPSILON=" + float_to_string_with_full_precision(epsilon)); + build_opts.add_option_if(_input_weights->info()->data_layout() == DataLayout::NHWC, "-DNHWC"); 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, "-DBIAS"); + build_opts.add_option_if(input_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_conv_layer", build_opts.options())); + _kernel = static_cast(CLKernelLibrary::get().create_kernel("fuse_batchnormalization_layer", build_opts.options())); } -Status CLFuseBatchNormalizationKernel::validate(const ITensorInfo *conv_weights, const ITensorInfo *bn_mean, const ITensorInfo *bn_var, +Status CLFuseBatchNormalizationKernel::validate(const ITensorInfo *input_weights, const ITensorInfo *bn_mean, const ITensorInfo *bn_var, const ITensorInfo *fused_weights, const ITensorInfo *fused_bias, - const ITensorInfo *conv_bias, const ITensorInfo *bn_beta, const ITensorInfo *bn_gamma, - float epsilon) + const ITensorInfo *input_bias, const ITensorInfo *bn_beta, const ITensorInfo *bn_gamma, + float epsilon, FuseBatchNormalizationType fbn_type) { - ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(conv_weights, bn_mean, bn_var, fused_weights, fused_bias, conv_bias, bn_beta, bn_gamma, epsilon)); + ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input_weights, bn_mean, bn_var, fused_weights, fused_bias, input_bias, bn_beta, bn_gamma, epsilon, fbn_type)); return Status{}; } @@ -175,10 +186,10 @@ void CLFuseBatchNormalizationKernel::run(const arm_compute::Window &window, cl:: // Add kernel arguments unsigned int idx = 0; - add_3D_tensor_argument(idx, _conv_weights, slice_3d); - if(_conv_bias != nullptr) + add_3D_tensor_argument(idx, _input_weights, slice_3d); + if(_input_bias != nullptr) { - add_1D_tensor_argument(idx, _conv_bias, slice_1d); + add_1D_tensor_argument(idx, _input_bias, slice_1d); } add_1D_tensor_argument(idx, _bn_mean, slice_1d); add_1D_tensor_argument(idx, _bn_var, slice_1d); diff --git a/src/runtime/CL/functions/CLFuseBatchNormalization.cpp b/src/runtime/CL/functions/CLFuseBatchNormalization.cpp index 32e46787d3..72dd27e3cc 100644 --- a/src/runtime/CL/functions/CLFuseBatchNormalization.cpp +++ b/src/runtime/CL/functions/CLFuseBatchNormalization.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018 ARM Limited. + * Copyright (c) 2018-2019 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -36,20 +36,20 @@ CLFuseBatchNormalization::CLFuseBatchNormalization() { } -void CLFuseBatchNormalization::configure(const ICLTensor *conv_weights, const ICLTensor *bn_mean, const ICLTensor *bn_var, +void CLFuseBatchNormalization::configure(const ICLTensor *input_weights, const ICLTensor *bn_mean, const ICLTensor *bn_var, ICLTensor *fused_weights, ICLTensor *fused_bias, - const ICLTensor *conv_bias, const ICLTensor *bn_beta, const ICLTensor *bn_gamma, - float epsilon) + const ICLTensor *input_bias, const ICLTensor *bn_beta, const ICLTensor *bn_gamma, + float epsilon, FuseBatchNormalizationType fbn_type) { - _fuse_bn_kernel.configure(conv_weights, bn_mean, bn_var, fused_weights, fused_bias, conv_bias, bn_beta, bn_gamma, epsilon); + _fuse_bn_kernel.configure(input_weights, bn_mean, bn_var, fused_weights, fused_bias, input_bias, bn_beta, bn_gamma, epsilon, fbn_type); } -Status CLFuseBatchNormalization::validate(const ITensorInfo *conv_weights, const ITensorInfo *bn_mean, const ITensorInfo *bn_var, +Status CLFuseBatchNormalization::validate(const ITensorInfo *input_weights, const ITensorInfo *bn_mean, const ITensorInfo *bn_var, const ITensorInfo *fused_weights, const ITensorInfo *fused_bias, - const ITensorInfo *conv_bias, const ITensorInfo *bn_beta, const ITensorInfo *bn_gamma, - float epsilon) + const ITensorInfo *input_bias, const ITensorInfo *bn_beta, const ITensorInfo *bn_gamma, + float epsilon, FuseBatchNormalizationType fbn_type) { - return CLFuseBatchNormalizationKernel::validate(conv_weights, bn_mean, bn_var, fused_weights, fused_bias, conv_bias, bn_beta, bn_gamma, epsilon); + return CLFuseBatchNormalizationKernel::validate(input_weights, bn_mean, bn_var, fused_weights, fused_bias, input_bias, bn_beta, bn_gamma, epsilon, fbn_type); } void CLFuseBatchNormalization::run() diff --git a/tests/validation/CL/FuseBatchNormalization.cpp b/tests/validation/CL/FuseBatchNormalization.cpp index 92d63c0c3d..35414b765a 100644 --- a/tests/validation/CL/FuseBatchNormalization.cpp +++ b/tests/validation/CL/FuseBatchNormalization.cpp @@ -44,6 +44,8 @@ AbsoluteTolerance absolute_tolerance_f16(0.2f); template using CLFuseBatchNormalizationConvFixture = FuseBatchNormalizationFixture; +template +using CLFuseBatchNormalizationDWCFixture = FuseBatchNormalizationFixture; // *INDENT-OFF* // clang-format off @@ -140,6 +142,77 @@ FIXTURE_DATA_TEST_CASE(RunLarge, CLFuseBatchNormalizationConvFixture, fram TEST_SUITE_END() // FP16 TEST_SUITE_END() // Float TEST_SUITE_END() // Convolution + +TEST_SUITE(DepthwiseConvolution) +TEST_SUITE(Float) +TEST_SUITE(FP32) +FIXTURE_DATA_TEST_CASE(RunSmall, CLFuseBatchNormalizationDWCFixture, framework::DatasetMode::PRECOMMIT, + combine(combine(combine(combine(combine(combine( + datasets::Small3DShapes(), + 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, CLFuseBatchNormalizationDWCFixture, framework::DatasetMode::NIGHTLY, + combine(combine(combine(combine(combine(combine( + datasets::Large3DShapes(), + 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, CLFuseBatchNormalizationDWCFixture, framework::DatasetMode::PRECOMMIT, + combine(combine(combine(combine(combine(combine( + datasets::Small3DShapes(), + 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, CLFuseBatchNormalizationDWCFixture, framework::DatasetMode::NIGHTLY, + combine(combine(combine(combine(combine(combine( + datasets::Large3DShapes(), + 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() // DepthwiseConvolution + TEST_SUITE_END() // FuseBatchNormalization TEST_SUITE_END() // CL } // namespace validation diff --git a/tests/validation/fixtures/FuseBatchNormalizationFixture.h b/tests/validation/fixtures/FuseBatchNormalizationFixture.h index 864d627ed7..2e76792b3e 100644 --- a/tests/validation/fixtures/FuseBatchNormalizationFixture.h +++ b/tests/validation/fixtures/FuseBatchNormalizationFixture.h @@ -90,9 +90,12 @@ protected: auto w_fused_to_use = in_place_w ? nullptr : &w_fused; auto b_fused_to_use = in_place_b ? nullptr : &b_fused; + const FuseBatchNormalizationType fuse_bn_type = dims_weights == 3 ? + FuseBatchNormalizationType::DEPTHWISECONVOLUTION : + FuseBatchNormalizationType::CONVOLUTION; // 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); + 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); ARM_COMPUTE_EXPECT(w.info()->is_resizable(), framework::LogLevel::ERRORS); ARM_COMPUTE_EXPECT(b.info()->is_resizable(), framework::LogLevel::ERRORS); -- cgit v1.2.1