Compute Library
 21.02
GCNormalizationLayerKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2020 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
25 
31 #include "arm_compute/core/Utils.h"
36 #include "support/StringSupport.h"
37 
38 #include <string>
39 
40 using namespace arm_compute;
41 
43  : _input(nullptr), _squared_input(nullptr), _output(nullptr), _border_size(0)
44 {
45 }
46 
48 {
49  return _border_size;
50 }
51 
53 {
56  ARM_COMPUTE_ERROR_ON_MSG(!(norm_info.norm_size() % 2), "Normalization size should be odd");
57  ARM_COMPUTE_ERROR_ON_MSG(norm_info.type() == NormType::IN_MAP_2D, "2D In-Map Normalization not implemented");
58 
59  // Set build options
60  std::set<std::string> build_opts;
61 
62  _input = input;
63  _squared_input = squared_input;
64  _output = output;
65 
66  const bool is_in_map = norm_info.is_in_map();
67  const unsigned int border_width = is_in_map ? std::min(norm_info.norm_size() / 2, 3U) : 0;
68  _border_size = BorderSize(0, border_width);
69 
70  // Set kernel static arguments
71  std::string func_name = ((norm_info.type() == NormType::IN_MAP_1D) ? "IN_MAP_1D" : "CROSS_MAP");
72  build_opts.emplace(("#define " + func_name));
73  build_opts.emplace(("#define COEFF " + float_to_string_with_full_precision(norm_info.scale_coeff())));
74  build_opts.emplace(("#define BETA " + float_to_string_with_full_precision(norm_info.beta())));
75  build_opts.emplace(("#define KAPPA " + float_to_string_with_full_precision(norm_info.kappa())));
76  build_opts.emplace(("#define RADIUS " + support::cpp11::to_string(norm_info.norm_size() / 2)));
77  build_opts.emplace(("#define LOCAL_SIZE_X " + support::cpp11::to_string(1)));
78  build_opts.emplace(("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1)));
79  build_opts.emplace(("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1)));
80 
81  // Create kernel
82  _kernel = static_cast<GCKernel>(GCKernelLibrary::get().create_kernel("normalization_layer", build_opts));
83 
84  // Configure kernel window
85  const unsigned int num_elems_processed_per_iteration = 1;
86  const unsigned int num_elems_read_per_iteration = num_elems_processed_per_iteration + 2 * (norm_info.norm_size() / 2);
87 
88  Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
89 
90  AccessWindowHorizontal input_access(input->info(), -_border_size.left, num_elems_read_per_iteration);
91  AccessWindowHorizontal squared_input_access(squared_input->info(), -_border_size.left, num_elems_read_per_iteration);
93 
94  update_window_and_padding(win, input_access, squared_input_access, output_access);
95 
96  output_access.set_valid_region(win, input->info()->valid_region());
97 
98  IGCKernel::configure(win);
99 }
100 
102 {
105 
106  _kernel.use();
107 
109 
110  do
111  {
112  unsigned int idx = 0;
113  unsigned int binding = 1;
114  add_3D_tensor_argument(idx, _input, binding++, slice);
115  add_3D_tensor_argument(idx, _squared_input, binding++, slice);
116  add_3D_tensor_argument(idx, _output, binding++, slice);
117 
118  _kernel.update_shader_params();
119 
120  enqueue(*this, slice);
121  }
122  while(window.slide_window_slice_3D(slice));
123 }
float scale_coeff() const
Return the scaling factor of the normalization function.
Definition: Types.h:1711
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
float kappa() const
Get the kappa value.
Definition: Types.h:1685
void add_3D_tensor_argument(unsigned int &idx, const IGCTensor *tensor, const unsigned int binding_point, const Window &window)
Add the passed 3D tensor&#39;s parameters to the object&#39;s kernel&#39;s arguments starting from the index idx...
Definition: IGCKernel.cpp:132
void enqueue(IGCKernel &kernel, const Window &window, const gles::NDRange &lws=gles::NDRange(1U, 1U, 1U))
Add the kernel to the command queue with the given window.
Definition: IGCKernel.cpp:41
bool is_in_map() const
Check if normalization is not cross map.
Definition: Types.h:1700
Container for 2D border size.
Definition: Types.h:273
uint32_t norm_size() const
Get the normalization size.
Definition: Types.h:1670
NormType type() const
Get the normalization type.
Definition: Types.h:1665
std::string to_string(T &&value)
Convert integer and float values to string.
void run(const Window &window) override
Enqueue the OpenGL ES shader to process the given window.
1 channel, 1 F32 per channel
Normalization Layer Information class.
Definition: Types.h:1647
Interface for GLES Compute tensor.
Definition: IGCTensor.h:35
GCKernel class.
Copyright (c) 2017-2021 Arm Limited.
virtual ValidRegion valid_region() const =0
Valid region of the tensor.
bool update_window_and_padding(Window &win, Ts &&... patterns)
Update window and padding size for each of the access patterns.
Definition: WindowHelpers.h:46
std::string float_to_string_with_full_precision(float val)
Create a string with the float in full precision.
Definition: Utils.h:1262
Normalization applied within the same map in 1D region.
#define ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:543
Manages all the GLES kernels compilation and caching, provides accessors for the GLES Context...
Class to describe a number of elements in each dimension.
Definition: Steps.h:40
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:456
Implementation of a row access pattern.
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
float beta() const
Get the beta value.
Definition: Types.h:1680
void configure(const IGCTensor *input, const IGCTensor *squared_input, IGCTensor *output, NormalizationLayerInfo norm_info)
Set the input and output tensors.
bool slide_window_slice_3D(Window &slice) const
Slide the passed 3D window slice.
Definition: Window.h:335
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:941
#define ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:790
static GCKernelLibrary & get()
Get the static instance of GCKernelLibrary.
BorderSize border_size() const override
The size of the border for that kernel.
GCKernel create_kernel(const std::string &shader_name, const StringSet &build_options_set={}) const
Creates a kernel from the kernel library.
unsigned int num_elems_processed_per_iteration
Window first_slice_window_3D() const
First 3D slice of the window.
Definition: Window.h:291
Describe a multidimensional execution window.
Definition: Window.h:39
Normalization applied within the same map in 2D region.
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205
SimpleTensor< T > slice(const SimpleTensor< T > &src, Coordinates starts, Coordinates ends)