aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGeorgios Pinitas <georgios.pinitas@arm.com>2018-09-26 11:25:40 +0100
committerAnthony Barbier <anthony.barbier@arm.com>2018-11-02 16:55:45 +0000
commitc93691717a6e7ca67e32b4dedd233b8c63b6daf2 (patch)
treed3929606b525e89f60299b16f95eb4223d11d5a8
parente6dbde0128bf33b5d72a00c480bd92c290fd17b7 (diff)
downloadComputeLibrary-c93691717a6e7ca67e32b4dedd233b8c63b6daf2.tar.gz
COMPMID-1523: Fuse BN node with convolution.
Change-Id: I146936c9e98b343496a4b61cdbadf0eaa38e885a Reviewed-on: https://eu-gerrit-1.euhpc.arm.com/154008 Reviewed-by: Michele DiGiorgio <michele.digiorgio@arm.com> Reviewed-by: Giuseppe Rossini <giuseppe.rossini@arm.com> Tested-by: bsgcomp <bsgcomp@arm.com>
-rw-r--r--arm_compute/core/CL/CLKernels.h1
-rw-r--r--arm_compute/core/CL/kernels/CLFuseBatchNormalizationKernel.h101
-rw-r--r--arm_compute/runtime/CL/CLFunctions.h1
-rw-r--r--arm_compute/runtime/CL/functions/CLFuseBatchNormalization.h93
-rw-r--r--src/core/CL/CLKernelLibrary.cpp1
-rw-r--r--src/core/CL/cl_kernels/batchnormalization_layer.cl162
-rw-r--r--src/core/CL/kernels/CLFuseBatchNormalizationKernel.cpp221
-rw-r--r--src/runtime/CL/functions/CLFuseBatchNormalization.cpp59
-rw-r--r--tests/validation/CL/BatchNormalizationLayer.cpp47
-rw-r--r--tests/validation/fixtures/BatchNormalizationLayerFusionFixture.h186
-rw-r--r--tests/validation/reference/BatchNormalizationLayer.cpp1
11 files changed, 861 insertions, 12 deletions
diff --git a/arm_compute/core/CL/CLKernels.h b/arm_compute/core/CL/CLKernels.h
index f6759b9f1e..95b6f9b70f 100644
--- a/arm_compute/core/CL/CLKernels.h
+++ b/arm_compute/core/CL/CLKernels.h
@@ -65,6 +65,7 @@
#include "arm_compute/core/CL/kernels/CLFillBorderKernel.h"
#include "arm_compute/core/CL/kernels/CLFlattenLayerKernel.h"
#include "arm_compute/core/CL/kernels/CLFloorKernel.h"
+#include "arm_compute/core/CL/kernels/CLFuseBatchNormalizationKernel.h"
#include "arm_compute/core/CL/kernels/CLGEMMInterleave4x4Kernel.h"
#include "arm_compute/core/CL/kernels/CLGEMMLowpMatrixMultiplyKernel.h"
#include "arm_compute/core/CL/kernels/CLGEMMLowpOffsetContributionKernel.h"
diff --git a/arm_compute/core/CL/kernels/CLFuseBatchNormalizationKernel.h b/arm_compute/core/CL/kernels/CLFuseBatchNormalizationKernel.h
new file mode 100644
index 0000000000..05a57c171e
--- /dev/null
+++ b/arm_compute/core/CL/kernels/CLFuseBatchNormalizationKernel.h
@@ -0,0 +1,101 @@
+/*
+ * Copyright (c) 2018 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_CLFUSEBATCHNORMALIZATIONKERNEL_H__
+#define __ARM_COMPUTE_CLFUSEBATCHNORMALIZATIONKERNEL_H__
+
+#include "arm_compute/core/CL/ICLKernel.h"
+
+namespace arm_compute
+{
+// Forward declarations
+class ICLTensor;
+
+/** OpenCL kernel to fuse the batch normalization node to a preceding convolution node */
+class CLFuseBatchNormalizationKernel : public ICLKernel
+{
+public:
+ /** Default constructor */
+ CLFuseBatchNormalizationKernel();
+ /** Prevent instances of this class from being copied (As this class contains pointers) */
+ CLFuseBatchNormalizationKernel(const CLFuseBatchNormalizationKernel &) = delete;
+ /** Prevent instances of this class from being copied (As this class contains pointers) */
+ CLFuseBatchNormalizationKernel &operator=(const CLFuseBatchNormalizationKernel &) = delete;
+ /** Allow instances of this class to be moved */
+ CLFuseBatchNormalizationKernel(CLFuseBatchNormalizationKernel &&) = default;
+ /** Allow instances of this class to be moved */
+ CLFuseBatchNormalizationKernel &operator=(CLFuseBatchNormalizationKernel &&) = default;
+ /** Default destructor */
+ ~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. 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[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,
+ const ICLTensor *conv_bias = nullptr, const ICLTensor *bn_beta = nullptr, const ICLTensor *bn_gamma = nullptr,
+ 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.
+ *
+ * @return a status
+ */
+ static Status validate(const ITensorInfo *conv_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);
+
+ // 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;
+};
+} // namespace arm_compute
+#endif /*__ARM_COMPUTE_CLFUSEBATCHNORMALIZATIONKERNEL_H__ */
diff --git a/arm_compute/runtime/CL/CLFunctions.h b/arm_compute/runtime/CL/CLFunctions.h
index 6a614f7704..014e25bbb3 100644
--- a/arm_compute/runtime/CL/CLFunctions.h
+++ b/arm_compute/runtime/CL/CLFunctions.h
@@ -65,6 +65,7 @@
#include "arm_compute/runtime/CL/functions/CLFlattenLayer.h"
#include "arm_compute/runtime/CL/functions/CLFloor.h"
#include "arm_compute/runtime/CL/functions/CLFullyConnectedLayer.h"
+#include "arm_compute/runtime/CL/functions/CLFuseBatchNormalization.h"
#include "arm_compute/runtime/CL/functions/CLGEMM.h"
#include "arm_compute/runtime/CL/functions/CLGEMMConvolutionLayer.h"
#include "arm_compute/runtime/CL/functions/CLGEMMInterleave4x4.h"
diff --git a/arm_compute/runtime/CL/functions/CLFuseBatchNormalization.h b/arm_compute/runtime/CL/functions/CLFuseBatchNormalization.h
new file mode 100644
index 0000000000..777a80f8b9
--- /dev/null
+++ b/arm_compute/runtime/CL/functions/CLFuseBatchNormalization.h
@@ -0,0 +1,93 @@
+/*
+ * Copyright (c) 2018 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_CLFUSEBATCHNORMALIZATION_H__
+#define __ARM_COMPUTE_CLFUSEBATCHNORMALIZATION_H__
+
+#include "arm_compute/core/CL/kernels/CLFuseBatchNormalizationKernel.h"
+#include "arm_compute/core/Types.h"
+#include "arm_compute/runtime/IFunction.h"
+
+namespace arm_compute
+{
+// Forward declarations
+class ICLTensor;
+
+/** Basic function to fuse the batch normalization node to a preceding convolution node */
+class CLFuseBatchNormalization : public IFunction
+{
+public:
+ /** Default constructor */
+ CLFuseBatchNormalization();
+ /** Prevent instances of this class from being copied (As this class contains pointers) */
+ CLFuseBatchNormalization(const CLFuseBatchNormalization &) = delete;
+ /** Prevent instances of this class from being copied (As this class contains pointers) */
+ CLFuseBatchNormalization &operator=(const CLFuseBatchNormalization &) = delete;
+ /** Allow instances of this class to be moved */
+ CLFuseBatchNormalization(CLFuseBatchNormalization &&) = default;
+ /** Allow instances of this class to be moved */
+ CLFuseBatchNormalization &operator=(CLFuseBatchNormalization &&) = default;
+ /** Default destructor */
+ ~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. 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[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,
+ const ICLTensor *conv_bias = nullptr, const ICLTensor *bn_beta = nullptr, const ICLTensor *bn_gamma = nullptr,
+ 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.
+ *
+ * @return a status
+ */
+ static Status validate(const ITensorInfo *conv_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);
+
+ // Inherited methods overridden:
+ void run() override;
+
+private:
+ CLFuseBatchNormalizationKernel _fuse_bn_kernel;
+};
+} // namespace arm_compute
+#endif /*__ARM_COMPUTE_CLFUSEBATCHNORMALIZATION_H__ */
diff --git a/src/core/CL/CLKernelLibrary.cpp b/src/core/CL/CLKernelLibrary.cpp
index 957543c877..a2428ca99d 100644
--- a/src/core/CL/CLKernelLibrary.cpp
+++ b/src/core/CL/CLKernelLibrary.cpp
@@ -237,6 +237,7 @@ const std::map<std::string, std::string> CLKernelLibrary::_kernel_program_map =
{ "fill_image_borders_constant", "fill_border.cl" },
{ "fill_image_borders_replicate", "fill_border.cl" },
{ "finalize", "optical_flow_pyramid_lk.cl" },
+ { "fuse_batchnormalization_layer", "batchnormalization_layer.cl" },
{ "floor_layer", "floor.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 5352af3c5a..df141269bc 100644
--- a/src/core/CL/cl_kernels/batchnormalization_layer.cl
+++ b/src/core/CL/cl_kernels/batchnormalization_layer.cl
@@ -23,14 +23,14 @@
*/
#include "helpers.h"
-#if defined(VEC_SIZE) && defined(DATA_TYPE)
-
#define ADD_OP(a, b) ((a) + (b))
#define SUB_OP(a, b) ((a) - (b))
#define MUL_OP(a, b) ((a) * (b))
#define INVSQRT_OP(a) rsqrt((a))
#define SQCVT_SAT(a) (a)
+#if defined(VEC_SIZE) && defined(DATA_TYPE)
+
#if defined(FUSED_ACTIVATION)
#include "activation_layer.cl"
#define ACTIVATION_FUNC(x) ACTIVATION_OP(FUSED_ACTIVATION, x)
@@ -258,3 +258,161 @@ __kernel void batchnormalization_layer_nhwc(TENSOR3D_DECLARATION(input),
(res, 0, (__global DATA_TYPE *)out.ptr);
}
#endif /* defined(VEC_SIZE) && defined(DATA_TYPE) */
+
+#if defined(NUM_CHANNELS) && defined(DATA_TYPE) && defined(EPSILON)
+/** Fuse batchnorm parameters to convolution layer parameters
+ *
+ * @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
+ *
+ * @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
+ */
+__kernel void fuse_batchnormalization_layer(TENSOR4D_DECLARATION(conv_w),
+ VECTOR_DECLARATION(bn_mean),
+ VECTOR_DECLARATION(bn_var)
+#ifndef IN_PLACE_W
+ ,
+ TENSOR4D_DECLARATION(fused_w)
+#endif /* not 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 */
+ )
+{
+ 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);
+
+ // In-place ops
+#ifdef IN_PLACE_W
+ Tensor4D fused_w = conv_w;
+#else /* IN_PLACE_W */
+ Tensor4D fused_w = CONVERT_TO_TENSOR4D_STRUCT(fused_w, NUM_CHANNELS);
+#endif /* IN_PLACE */
+#ifdef IN_PLACE_B
+ Vector fused_b = conv_b;
+#else /* IN_PLACE_W */
+ Vector fused_b = CONVERT_TO_VECTOR_STRUCT_NO_STEP(fused_b);
+#endif /* IN_PLACE */
+
+ // Conditional ops
+#ifdef HAS_BIAS
+ Vector conv_b = CONVERT_TO_VECTOR_STRUCT_NO_STEP(conv_b);
+#endif /* USE_DEFAULT_BETA */
+#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 */
+
+ 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;
+
+#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)
+
+ // Store updated bias
+ *((__global DATA_TYPE *)(fused_b.ptr + current_slice * fused_b.stride_x)) = bn;
+}
+#endif /* defined(NUM_CHANNELS) && defined(DATA_TYPE) && defined(EPSILON) */
diff --git a/src/core/CL/kernels/CLFuseBatchNormalizationKernel.cpp b/src/core/CL/kernels/CLFuseBatchNormalizationKernel.cpp
new file mode 100644
index 0000000000..e14b8a3777
--- /dev/null
+++ b/src/core/CL/kernels/CLFuseBatchNormalizationKernel.cpp
@@ -0,0 +1,221 @@
+/*
+ * Copyright (c) 2018 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/core/CL/kernels/CLFuseBatchNormalizationKernel.h"
+
+#include "arm_compute/core/CL/CLHelpers.h"
+#include "arm_compute/core/CL/CLKernelLibrary.h"
+#include "arm_compute/core/CL/CLValidate.h"
+#include "arm_compute/core/CL/ICLTensor.h"
+#include "arm_compute/core/Helpers.h"
+#include "arm_compute/core/TensorInfo.h"
+#include "arm_compute/core/Utils.h"
+#include "arm_compute/core/Window.h"
+
+#include "support/ToolchainSupport.h"
+
+namespace arm_compute
+{
+namespace
+{
+Status validate_arguments(const ITensorInfo *conv_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)
+{
+ 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_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));
+
+ // Validate bias
+ if(conv_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);
+ }
+ // 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);
+ }
+ // 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);
+ }
+
+ // 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);
+ }
+ // 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);
+ }
+
+ return Status{};
+}
+} // 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(),
+ _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,
+ ICLTensor *fused_weights, ICLTensor *fused_bias,
+ const ICLTensor *conv_bias, const ICLTensor *bn_beta, const ICLTensor *bn_gamma,
+ float epsilon)
+{
+ ARM_COMPUTE_ERROR_ON_NULLPTR(conv_weights, bn_mean, bn_var);
+
+ _conv_weights = conv_weights;
+ _conv_bias = conv_bias;
+ _bn_mean = bn_mean;
+ _bn_var = bn_var;
+ _bn_beta = bn_beta;
+ _bn_gamma = bn_gamma;
+ _fused_weights = fused_weights;
+ _fused_bias = fused_bias;
+ _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);
+
+ // 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
+ ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(conv_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,
+ (bn_beta != nullptr) ? bn_beta->info() : nullptr,
+ (bn_gamma != nullptr) ? bn_gamma->info() : nullptr,
+ epsilon));
+
+ // Configure kernel window
+ const unsigned int num_elems_processed_per_iteration_x = 16 / conv_weights->info()->element_size();
+ 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("-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<int>(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");
+
+ // Create kernel
+ _kernel = static_cast<cl::Kernel>(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,
+ const ITensorInfo *fused_weights, const ITensorInfo *fused_bias,
+ const ITensorInfo *conv_bias, const ITensorInfo *bn_beta, const ITensorInfo *bn_gamma,
+ float epsilon)
+{
+ ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(conv_weights, bn_mean, bn_var, fused_weights, fused_bias, conv_bias, bn_beta, bn_gamma, epsilon));
+ return Status{};
+}
+
+void CLFuseBatchNormalizationKernel::run(const arm_compute::Window &window, cl::CommandQueue &queue)
+{
+ ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
+ 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));
+
+ // 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_4D_tensor_argument(idx, _fused_weights, slice);
+ }
+ if(!_run_in_place_bias)
+ {
+ add_1D_tensor_argument(idx, _fused_bias, vector_slice);
+ }
+ if(_conv_bias != nullptr)
+ {
+ add_1D_tensor_argument(idx, _conv_bias, vector_slice);
+ }
+ if(_bn_beta != nullptr)
+ {
+ add_1D_tensor_argument(idx, _bn_beta, vector_slice);
+ }
+ if(_bn_gamma != nullptr)
+ {
+ add_1D_tensor_argument(idx, _bn_gamma, vector_slice);
+ }
+ enqueue(queue, *this, slice, lws_hint());
+}
+} // namespace arm_compute
diff --git a/src/runtime/CL/functions/CLFuseBatchNormalization.cpp b/src/runtime/CL/functions/CLFuseBatchNormalization.cpp
new file mode 100644
index 0000000000..32e46787d3
--- /dev/null
+++ b/src/runtime/CL/functions/CLFuseBatchNormalization.cpp
@@ -0,0 +1,59 @@
+/*
+ * Copyright (c) 2018 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 "arm_compute/core/Error.h"
+#include "arm_compute/core/TensorInfo.h"
+#include "arm_compute/core/Types.h"
+#include "arm_compute/runtime/CL/CLScheduler.h"
+
+namespace arm_compute
+{
+CLFuseBatchNormalization::CLFuseBatchNormalization()
+ : _fuse_bn_kernel()
+{
+}
+
+void CLFuseBatchNormalization::configure(const ICLTensor *conv_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)
+{
+ _fuse_bn_kernel.configure(conv_weights, bn_mean, bn_var, fused_weights, fused_bias, conv_bias, bn_beta, bn_gamma, epsilon);
+}
+
+Status CLFuseBatchNormalization::validate(const ITensorInfo *conv_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)
+{
+ return CLFuseBatchNormalizationKernel::validate(conv_weights, bn_mean, bn_var, fused_weights, fused_bias, conv_bias, bn_beta, bn_gamma, epsilon);
+}
+
+void CLFuseBatchNormalization::run()
+{
+ CLScheduler::get().enqueue(_fuse_bn_kernel, true);
+}
+} // namespace arm_compute
diff --git a/tests/validation/CL/BatchNormalizationLayer.cpp b/tests/validation/CL/BatchNormalizationLayer.cpp
index 0d80ff7eb7..cbf3c7092d 100644
--- a/tests/validation/CL/BatchNormalizationLayer.cpp
+++ b/tests/validation/CL/BatchNormalizationLayer.cpp
@@ -25,16 +25,20 @@
#include "arm_compute/runtime/CL/CLTensor.h"
#include "arm_compute/runtime/CL/CLTensorAllocator.h"
#include "arm_compute/runtime/CL/functions/CLBatchNormalizationLayer.h"
+#include "arm_compute/runtime/CL/functions/CLConvolutionLayer.h"
+#include "arm_compute/runtime/CL/functions/CLFuseBatchNormalization.h"
#include "tests/CL/CLAccessor.h"
#include "tests/PaddingCalculator.h"
+#include "tests/datasets/LargeConvolutionLayerDataset.h"
#include "tests/datasets/RandomBatchNormalizationLayerDataset.h"
-#include "tests/datasets/ShapeDatasets.h"
+#include "tests/datasets/SmallConvolutionLayerDataset.h"
#include "tests/framework/Asserts.h"
#include "tests/framework/Macros.h"
#include "tests/framework/datasets/Datasets.h"
#include "tests/validation/Helpers.h"
#include "tests/validation/Validation.h"
#include "tests/validation/fixtures/BatchNormalizationLayerFixture.h"
+#include "tests/validation/fixtures/BatchNormalizationLayerFusionFixture.h"
namespace arm_compute
{
@@ -44,14 +48,20 @@ namespace validation
{
namespace
{
-constexpr AbsoluteTolerance<float> tolerance_f32(0.00001f); /**< Tolerance value for comparing reference's output against implementation's output for DataType::F32 */
-constexpr AbsoluteTolerance<float> tolerance_f16(0.01f); /**< Tolerance value for comparing reference's output against implementation's output for DataType::F16 */
+RelativeTolerance<float> rel_tolerance_f32(0.05f); /**< Tolerance value for comparing reference's output against implementation's output for DataType::F32 */
+constexpr AbsoluteTolerance<float> abs_tolerance_f32(0.00001f); /**< Tolerance value for comparing reference's output against implementation's output for DataType::F32 */
+constexpr AbsoluteTolerance<float> tolerance_f16(0.01f); /**< Tolerance value for comparing reference's output against implementation's output for DataType::F16 */
const auto act_infos = framework::dataset::make("ActivationInfo",
{
ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f),
ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 8.f, 2.f),
});
+
+const auto common_fusion_dataset = combine(combine(combine(framework::dataset::make("UseBias", { false, true }),
+ framework::dataset::make("UseBeta", { false, true })),
+ framework::dataset::make("UseGamma", { false, true })),
+ framework::dataset::make("Epsilon", { 0.001f }));
} // namespace
TEST_SUITE(CL)
@@ -150,9 +160,9 @@ FIXTURE_DATA_TEST_CASE(Random, CLBatchNormalizationLayerFixture<float>, framewor
framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })))
{
// Validate output
- validate(CLAccessor(_target), _reference, tolerance_f32, 0);
+ validate(CLAccessor(_target), _reference, abs_tolerance_f32, 0);
}
-TEST_SUITE_END()
+TEST_SUITE_END() //FP32
TEST_SUITE(FP16)
FIXTURE_DATA_TEST_CASE(Random, CLBatchNormalizationLayerFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(combine(datasets::RandomBatchNormalizationLayerDataset(),
@@ -165,11 +175,30 @@ FIXTURE_DATA_TEST_CASE(Random, CLBatchNormalizationLayerFixture<half>, framework
// Validate output
validate(CLAccessor(_target), _reference, tolerance_f16, 0);
}
-TEST_SUITE_END()
-TEST_SUITE_END()
+TEST_SUITE_END() // FP16
+TEST_SUITE_END() // Float
+
+TEST_SUITE_END() // BatchNormalizationLayer
+
+TEST_SUITE(BatchNormalizationLayerFusion)
+template <typename T>
+using CLBatchNormalizationLayerFusionFixture = BatchNormalizationLayerFusionValidationFixture<CLTensor, CLAccessor, CLConvolutionLayer, CLFuseBatchNormalization, T>;
+
+TEST_SUITE(Float)
+TEST_SUITE(FP32)
+FIXTURE_DATA_TEST_CASE(RunSmall, CLBatchNormalizationLayerFusionFixture<float>, framework::DatasetMode::PRECOMMIT,
+ combine(combine(combine(datasets::SmallConvolutionLayerDataset(), common_fusion_dataset),
+ framework::dataset::make("DataType", DataType::F32)),
+ framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })))
+{
+ // Validate output
+ validate(CLAccessor(_target), _reference, rel_tolerance_f32, 0.f, abs_tolerance_f32);
+}
+TEST_SUITE_END() // FP32
+TEST_SUITE_END() // Float
-TEST_SUITE_END()
-TEST_SUITE_END()
+TEST_SUITE_END() // BatchNormalizationLayerFusion
+TEST_SUITE_END() // CL
} // namespace validation
} // namespace test
} // namespace arm_compute
diff --git a/tests/validation/fixtures/BatchNormalizationLayerFusionFixture.h b/tests/validation/fixtures/BatchNormalizationLayerFusionFixture.h
new file mode 100644
index 0000000000..39c7d46114
--- /dev/null
+++ b/tests/validation/fixtures/BatchNormalizationLayerFusionFixture.h
@@ -0,0 +1,186 @@
+/*
+ * Copyright (c) 2018 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_BATCH_NORMALIZATION_LAYER_FUSION_FIXTURE
+#define ARM_COMPUTE_TEST_BATCH_NORMALIZATION_LAYER_FUSION_FIXTURE
+
+#include "arm_compute/core/TensorShape.h"
+#include "arm_compute/core/Types.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/BatchNormalizationLayer.h"
+#include "tests/validation/reference/ConvolutionLayer.h"
+
+namespace arm_compute
+{
+namespace test
+{
+namespace validation
+{
+template <typename TensorType, typename AccessorType, typename ConvolutionFunctionType, typename FusionFunctionType, typename T>
+class BatchNormalizationLayerFusionValidationFixture : public framework::Fixture
+{
+public:
+ template <typename...>
+ void setup(TensorShape src_shape, TensorShape w_shape, TensorShape b_shape, TensorShape dst_shape, PadStrideInfo info, Size2D dilation,
+ bool use_conv_b, bool use_beta, bool use_gamma, float epsilon, DataType dt, DataLayout data_layout)
+ {
+ ARM_COMPUTE_UNUSED(dilation);
+
+ _data_type = dt;
+ _data_layout = data_layout;
+ _use_conv_b = use_conv_b;
+ _use_beta = use_beta;
+ _use_gamma = use_gamma;
+
+ _target = compute_target(src_shape, w_shape, b_shape, dst_shape, info, epsilon);
+ _reference = compute_reference(src_shape, w_shape, b_shape, dst_shape, info, epsilon);
+ }
+
+protected:
+ template <typename U>
+ void fill(U &&src, U &&w_tensor, U &&b_tensor, U &&mean_tensor, U &&var_tensor, U &&beta_tensor, U &&gamma_tensor)
+ {
+ std::uniform_real_distribution<> distribution(-1.f, 1.f);
+ std::uniform_real_distribution<> distribution_gz(0, 1.f);
+
+ library->fill(src, distribution, 0);
+ library->fill(w_tensor, distribution, 1);
+ library->fill(mean_tensor, distribution, 2);
+ library->fill(var_tensor, distribution_gz, 3);
+ _use_conv_b ? library->fill(b_tensor, distribution, 4) : library->fill_tensor_value(b_tensor, 0.f);
+ _use_beta ? library->fill(beta_tensor, distribution, 5) : library->fill_tensor_value(beta_tensor, 0.f);
+ _use_gamma ? library->fill(gamma_tensor, distribution, 6) : library->fill_tensor_value(gamma_tensor, 1.f);
+ }
+
+ TensorType compute_target(TensorShape src_shape, TensorShape w_shape, TensorShape b_shape, TensorShape dst_shape, PadStrideInfo info, float epsilon)
+ {
+ if(_data_layout == DataLayout::NHWC)
+ {
+ permute(src_shape, PermutationVector(2U, 0U, 1U));
+ permute(w_shape, PermutationVector(2U, 0U, 1U));
+ permute(dst_shape, PermutationVector(2U, 0U, 1U));
+ }
+
+ // Create tensors
+ TensorType src = create_tensor<TensorType>(src_shape, _data_type, 1, QuantizationInfo(), _data_layout);
+ TensorType conv_w = create_tensor<TensorType>(w_shape, _data_type, 1, QuantizationInfo(), _data_layout);
+ TensorType conv_b = create_tensor<TensorType>(b_shape, _data_type, 1, QuantizationInfo(), _data_layout);
+ TensorType bn_mean = create_tensor<TensorType>(b_shape, _data_type, 1, QuantizationInfo(), _data_layout);
+ TensorType bn_var = create_tensor<TensorType>(b_shape, _data_type, 1, QuantizationInfo(), _data_layout);
+ TensorType bn_beta = create_tensor<TensorType>(b_shape, _data_type, 1, QuantizationInfo(), _data_layout);
+ TensorType bn_gamma = create_tensor<TensorType>(b_shape, _data_type, 1, QuantizationInfo(), _data_layout);
+ TensorType fused_w = create_tensor<TensorType>(w_shape, _data_type, 1, QuantizationInfo(), _data_layout);
+ TensorType fused_b = create_tensor<TensorType>(b_shape, _data_type, 1, QuantizationInfo(), _data_layout);
+ TensorType dst = create_tensor<TensorType>(dst_shape, _data_type, 1, QuantizationInfo(), _data_layout);
+
+ // Create and configure function
+ FusionFunctionType fuse_fn;
+ ConvolutionFunctionType conv_fn;
+ TensorType *conv_b_ptr = _use_conv_b ? &conv_b : nullptr;
+ TensorType *beta_ptr = _use_beta ? &bn_beta : nullptr;
+ TensorType *gamma_ptr = _use_gamma ? &bn_gamma : nullptr;
+ fuse_fn.configure(&conv_w, &bn_mean, &bn_var, &fused_w, &fused_b, conv_b_ptr, beta_ptr, gamma_ptr, epsilon);
+ conv_fn.configure(&src, &fused_w, &fused_b, &dst, info);
+
+ ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
+ ARM_COMPUTE_EXPECT(conv_w.info()->is_resizable(), framework::LogLevel::ERRORS);
+ ARM_COMPUTE_EXPECT(conv_b.info()->is_resizable(), framework::LogLevel::ERRORS);
+ ARM_COMPUTE_EXPECT(bn_mean.info()->is_resizable(), framework::LogLevel::ERRORS);
+ ARM_COMPUTE_EXPECT(bn_var.info()->is_resizable(), framework::LogLevel::ERRORS);
+ ARM_COMPUTE_EXPECT(bn_beta.info()->is_resizable(), framework::LogLevel::ERRORS);
+ ARM_COMPUTE_EXPECT(bn_gamma.info()->is_resizable(), framework::LogLevel::ERRORS);
+ ARM_COMPUTE_EXPECT(fused_w.info()->is_resizable(), framework::LogLevel::ERRORS);
+ ARM_COMPUTE_EXPECT(fused_b.info()->is_resizable(), framework::LogLevel::ERRORS);
+ ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
+
+ // Allocate tensors
+ src.allocator()->allocate();
+ conv_w.allocator()->allocate();
+ conv_b.allocator()->allocate();
+ bn_mean.allocator()->allocate();
+ bn_var.allocator()->allocate();
+ bn_beta.allocator()->allocate();
+ bn_gamma.allocator()->allocate();
+ fused_w.allocator()->allocate();
+ fused_b.allocator()->allocate();
+ dst.allocator()->allocate();
+
+ ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
+ ARM_COMPUTE_EXPECT(!conv_w.info()->is_resizable(), framework::LogLevel::ERRORS);
+ ARM_COMPUTE_EXPECT(!conv_b.info()->is_resizable(), framework::LogLevel::ERRORS);
+ ARM_COMPUTE_EXPECT(!bn_mean.info()->is_resizable(), framework::LogLevel::ERRORS);
+ ARM_COMPUTE_EXPECT(!bn_var.info()->is_resizable(), framework::LogLevel::ERRORS);
+ ARM_COMPUTE_EXPECT(!bn_beta.info()->is_resizable(), framework::LogLevel::ERRORS);
+ ARM_COMPUTE_EXPECT(!bn_gamma.info()->is_resizable(), framework::LogLevel::ERRORS);
+ ARM_COMPUTE_EXPECT(!fused_w.info()->is_resizable(), framework::LogLevel::ERRORS);
+ ARM_COMPUTE_EXPECT(!fused_b.info()->is_resizable(), framework::LogLevel::ERRORS);
+ ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
+
+ // Fill tensors
+ fill(AccessorType(src),
+ AccessorType(conv_w), AccessorType(conv_b),
+ AccessorType(bn_mean), AccessorType(bn_var), AccessorType(bn_beta), AccessorType(bn_gamma));
+
+ // Compute function
+ fuse_fn.run();
+ conv_fn.run();
+
+ return dst;
+ }
+
+ SimpleTensor<T> compute_reference(TensorShape src_shape, TensorShape w_shape, TensorShape b_shape, TensorShape dst_shape, PadStrideInfo info, float epsilon)
+ {
+ // Create reference
+ SimpleTensor<T> src{ src_shape, _data_type, 1 };
+ SimpleTensor<T> conv_w{ w_shape, _data_type, 1 };
+ SimpleTensor<T> conv_b{ b_shape, _data_type, 1 };
+ SimpleTensor<T> bn_var{ b_shape, _data_type, 1 };
+ SimpleTensor<T> bn_mean{ b_shape, _data_type, 1 };
+ SimpleTensor<T> bn_beta{ b_shape, _data_type, 1 };
+ SimpleTensor<T> bn_gamma{ b_shape, _data_type, 1 };
+
+ // Fill reference
+ fill(src, conv_w, conv_b, bn_mean, bn_var, bn_beta, bn_gamma);
+
+ // Calculate Conv + BN
+ auto conv_res = reference::convolution_layer(src, conv_w, conv_b, dst_shape, info);
+ return reference::batch_normalization_layer(conv_res, bn_mean, bn_var, bn_beta, bn_gamma, epsilon, ActivationLayerInfo());
+ }
+
+ TensorType _target{};
+ SimpleTensor<T> _reference{};
+ DataType _data_type{};
+ DataLayout _data_layout{};
+ bool _use_conv_b{};
+ bool _use_beta{};
+ bool _use_gamma{};
+};
+} // namespace validation
+} // namespace test
+} // namespace arm_compute
+#endif /* ARM_COMPUTE_TEST_BATCH_NORMALIZATION_LAYER_FUSION_FIXTURE */
diff --git a/tests/validation/reference/BatchNormalizationLayer.cpp b/tests/validation/reference/BatchNormalizationLayer.cpp
index 4ea3769c2c..37713c841d 100644
--- a/tests/validation/reference/BatchNormalizationLayer.cpp
+++ b/tests/validation/reference/BatchNormalizationLayer.cpp
@@ -77,7 +77,6 @@ template SimpleTensor<float> batch_normalization_layer(const SimpleTensor<float>
template SimpleTensor<half> batch_normalization_layer(const SimpleTensor<half> &src, const SimpleTensor<half> &mean, const SimpleTensor<half> &var,
const SimpleTensor<half> &beta,
const SimpleTensor<half> &gamma, float epsilon, ActivationLayerInfo act_info);
-
} // namespace reference
} // namespace validation
} // namespace test