aboutsummaryrefslogtreecommitdiff
path: root/src
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 /src
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>
Diffstat (limited to 'src')
-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
4 files changed, 441 insertions, 2 deletions
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