Compute Library
 21.02
CLL2NormalizeLayerKernel.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"
33 #include "src/core/CL/CLValidate.h"
36 
37 #include "support/StringSupport.h"
38 
39 namespace arm_compute
40 {
41 namespace
42 {
43 constexpr int max_input_tensor_dim = 3;
44 
45 constexpr unsigned int num_elems_processed_per_iteration = 16;
46 
47 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output, int axis, float epsilon)
48 {
49  ARM_COMPUTE_UNUSED(epsilon);
50 
51  const uint32_t actual_axis = wrap_around(axis, max_input_tensor_dim);
52  ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, sum, output);
56  ARM_COMPUTE_RETURN_ERROR_ON_MSG(actual_axis > 2, "Actual axis greater than 2 is not supported");
57  ARM_COMPUTE_RETURN_ERROR_ON_MSG(actual_axis >= TensorShape::num_max_dimensions, "Actual normalization axis greater than max number of dimensions");
58 
59  // Reduce shape on axis
60  TensorShape sum_shape = input->tensor_shape();
61  sum_shape.set(actual_axis, 1);
62  ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(sum->tensor_shape(), sum_shape);
63 
64  if(output->total_size() != 0)
65  {
69  ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(input->tensor_shape(), output->tensor_shape());
70  }
71 
72  return Status{};
73 }
74 
75 std::tuple<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
76 {
77  Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
78 
79  // Output tensor auto initialization if not yet initialized
80  auto_init_if_empty(*output, input->tensor_shape(), 1, input->data_type());
81 
82  AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
83  AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
84 
85  bool window_changed = update_window_and_padding(win, input_access, output_access);
86  output_access.set_valid_region(win, input->valid_region());
87 
88  Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
89 
90  return std::make_tuple(err, win);
91 }
92 } // namespace
93 
95  : _input(nullptr), _sum(nullptr), _output(nullptr), _actual_axis(0), _epsilon(1e-12)
96 {
97 }
98 
99 void CLL2NormalizeLayerKernel::configure(const ICLTensor *input, const ICLTensor *sum, ICLTensor *output, int axis, float epsilon)
100 {
101  configure(CLKernelLibrary::get().get_compile_context(), input, sum, output, axis, epsilon);
102 }
103 
104 void CLL2NormalizeLayerKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *sum, ICLTensor *output, int axis, float epsilon)
105 {
106  ARM_COMPUTE_ERROR_ON_NULLPTR(input, sum, output);
107  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), sum->info(), output->info(), axis, epsilon));
108 
109  _input = input;
110  _sum = sum;
111  _output = output;
112  _actual_axis = wrap_around(axis, max_input_tensor_dim);
113  _epsilon = epsilon;
114 
115  // Set build options
116  std::set<std::string> build_opts;
117  build_opts.emplace(("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())));
118  build_opts.emplace(("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration)));
119 
120  // Create kernel
121  std::string kernel_name;
122  unsigned int idx = 0;
123  switch(_actual_axis)
124  {
125  case 0:
126  kernel_name = "x";
127  idx = num_arguments_per_2D_tensor() * 3;
128  break;
129  case 1:
130  kernel_name = "y";
131  idx = num_arguments_per_2D_tensor() * 3;
132  break;
133  case 2:
134  kernel_name = "z";
135  idx = num_arguments_per_3D_tensor() * 3;
136  break;
137  default:
138  ARM_COMPUTE_ERROR("Axis not supported");
139  }
140  _kernel = create_kernel(compile_context, "l2_normalize_" + kernel_name, build_opts);
141 
142  // Set epsilon argument
143  if(input->info()->data_type() == DataType::F32)
144  {
145  _kernel.setArg<cl_float>(idx, _epsilon);
146  }
147  else
148  {
149  _kernel.setArg<cl_half>(idx, _epsilon);
150  }
151 
152  // Configure kernel window
153  auto win_config = validate_and_configure_window(_input->info(), _output->info());
154  ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config));
155 
156  ICLKernel::configure_internal(std::get<1>(win_config));
157 }
158 
159 Status CLL2NormalizeLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output, int axis, float epsilon)
160 {
161  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, sum, output, axis, epsilon));
162  ARM_COMPUTE_RETURN_ON_ERROR(std::get<0>(validate_and_configure_window(input->clone().get(), output->clone().get())));
163 
164  return Status{};
165 }
166 
167 void CLL2NormalizeLayerKernel::run(const Window &window, cl::CommandQueue &queue)
168 {
171 
172  Window window_sum(window);
173 
174  switch(_actual_axis)
175  {
176  case 0:
177  {
178  window_sum.set(Window::DimX, Window::Dimension(0, 0, 0));
179  Window in_slice = window.first_slice_window_2D();
180  Window sum_slice = window_sum.first_slice_window_2D();
181  do
182  {
183  unsigned int idx = 0;
184  add_2D_tensor_argument(idx, _input, in_slice);
185  add_2D_tensor_argument(idx, _sum, sum_slice);
186  add_2D_tensor_argument(idx, _output, in_slice);
187  enqueue(queue, *this, in_slice, lws_hint());
188  }
189  while(window.slide_window_slice_2D(in_slice) && window.slide_window_slice_2D(sum_slice));
190  }
191  break;
192  case 1:
193  {
194  window_sum.set(Window::DimY, Window::Dimension(0, 0, 0));
195  Window in_slice = window.first_slice_window_2D();
196  Window sum_slice = window_sum.first_slice_window_2D();
197  do
198  {
199  unsigned int idx = 0;
200  add_2D_tensor_argument(idx, _input, in_slice);
201  add_2D_tensor_argument(idx, _sum, sum_slice);
202  add_2D_tensor_argument(idx, _output, in_slice);
203  enqueue(queue, *this, in_slice, lws_hint());
204  }
205  while(window.slide_window_slice_2D(in_slice) && window.slide_window_slice_2D(sum_slice));
206  }
207  break;
208  case 2:
209  {
210  window_sum.set(Window::DimZ, Window::Dimension(0, 0, 0));
211  Window in_slice = window.first_slice_window_3D();
212  Window sum_slice = window_sum.first_slice_window_3D();
213  do
214  {
215  unsigned int idx = 0;
216  add_3D_tensor_argument(idx, _input, in_slice);
217  add_3D_tensor_argument(idx, _sum, sum_slice);
218  add_3D_tensor_argument(idx, _output, in_slice);
219  enqueue(queue, *this, in_slice, lws_hint());
220  }
221  while(window.slide_window_slice_3D(in_slice) && window.slide_window_slice_3D(sum_slice));
222  }
223  break;
224  default:
225  ARM_COMPUTE_ERROR("Not supported");
226  }
227 }
228 } // namespace arm_compute
Window first_slice_window_2D() const
First 2D slice of the window.
Definition: Window.h:283
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
#define ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(tensor)
Definition: CLValidate.h:35
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
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
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(...)
Definition: Validate.h:494
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
cl::NDRange lws_hint() const
Return the Local-Workgroup-Size hint.
Definition: ICLKernel.h:276
DATA_TYPE sum(__global const DATA_TYPE *input)
Calculate sum of a vector.
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:204
std::string to_string(T &&value)
Convert integer and float values to string.
void run(const Window &window, cl::CommandQueue &queue) override
Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue...
virtual DataType data_type() const =0
Data type used for each element of the tensor.
1 channel, 1 F32 per channel
void configure(const ICLTensor *input, const ICLTensor *sum, ICLTensor *output, int axis, float epsilon)
Set the input and output tensors.
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
Describe one of the image&#39;s dimensions with a start, end and step.
Definition: Window.h:77
Status class.
Definition: Error.h:52
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(...)
Definition: Validate.h:288
void add_3D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, 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: ICLKernel.h:172
bool slide_window_slice_2D(Window &slice) const
Slide the passed 2D window slice.
Definition: Window.h:323
Copyright (c) 2017-2021 Arm Limited.
1 channel, 1 F16 per channel
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:163
T wrap_around(T x, T m)
Wrap-around a number within the range 0 <= x < m.
Definition: Helpers.h:231
cl::Kernel create_kernel(const CLCompileContext &ctx, const std::string &kernel_name, const std::set< std::string > &build_opts=std::set< std::string >())
Creates an opencl kernel using a compile context.
Definition: CLHelpers.cpp:403
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
bool update_window_and_padding(Window &win, Ts &&... patterns)
Update window and padding size for each of the access patterns.
Definition: WindowHelpers.h:46
static constexpr unsigned int num_arguments_per_3D_tensor()
Returns the number of arguments enqueued per 3D tensor object.
Definition: ICLKernel.h:214
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
std::string kernel_name
std::string get_cl_type_from_data_type(const DataType &dt)
Translates a tensor data type to the appropriate OpenCL type.
Definition: CLHelpers.cpp:37
bool auto_init_if_empty(ITensorInfo &info, const TensorShape &shape, int num_channels, DataType data_type, QuantizationInfo quantization_info=QuantizationInfo())
Auto initialize the tensor info (shape, number of channels and data type) if the current assignment i...
virtual std::unique_ptr< T > clone() const =0
Provide a clone of the current object of class T.
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:49
static constexpr unsigned int num_arguments_per_2D_tensor()
Returns the number of arguments enqueued per 2D tensor object.
Definition: ICLKernel.h:206
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
CLCompileContext class.
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
void add_2D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
Add the passed 2D tensor&#39;s parameters to the object&#39;s kernel&#39;s arguments starting from the index idx...
Definition: ICLKernel.h:148
Interface for OpenCL tensor.
Definition: ICLTensor.h:42
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(...)
Definition: Validate.h:443
#define ARM_COMPUTE_CREATE_ERROR(error_code, msg)
Creates an error with a given message.
Definition: Error.h:159
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
Manages all the OpenCL kernels compilation and caching, provides accessors for the OpenCL Context...
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:545
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:792
Status validate_arguments(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, const GEMMLowpOutputStageInfo *output_stage)
unsigned int num_elems_processed_per_iteration
#define ARM_COMPUTE_RETURN_ERROR_ON_MSG(cond, msg)
If the condition is true, an error is returned.
Definition: Error.h:244
static Status validate(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output, int axis, float epsilon)
Static function to check if given info will lead to a valid configuration of CLL2NormalizeLayerKernel...
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
static constexpr size_t num_max_dimensions
Number of dimensions the tensor has.
Definition: Dimensions.h:46
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
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205