Compute Library
 21.02
CLMinMaxLayerKernel.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 
35 #include "support/StringSupport.h"
36 
37 #include <climits>
38 
39 using namespace arm_compute;
41 
42 namespace
43 {
45 {
49 
50  if(output->tensor_shape().total_size() > 0)
51  {
53 
55 
57  }
58 
59  return Status{};
60 }
61 
62 std::tuple<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
63 {
65 
66  // Output auto initialization if not yet initialized
67  auto_init_if_empty(*output, output_shape, 1, input->data_type());
68 
69  const unsigned int num_elems_processed_per_iteration = 1;
70 
71  // Configure kernel window
72  Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
73  AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
74  AccessWindowStatic output_access(output, 0, 0, 2, output->dimension(1));
75 
76  bool window_changed = update_window_and_padding(win, input_access, output_access);
77 
78  output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
79 
80  Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
81  return std::make_tuple(err, win);
82 }
83 } // namespace
84 
86  : _input(nullptr), _output(nullptr)
87 {
88 }
89 
91 {
92  configure(CLKernelLibrary::get().get_compile_context(), input, output);
93 }
94 
95 void CLMinMaxLayerKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output)
96 {
97  ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
99 
100  _input = input;
101  _output = output;
102 
103  std::set<std::string> build_opts;
104  build_opts.emplace("-DWIDTH=" + support::cpp11::to_string(input->info()->dimension(0)));
105  build_opts.emplace("-DHEIGHT=" + support::cpp11::to_string(input->info()->dimension(1)));
106  build_opts.emplace("-DDEPTH=" + support::cpp11::to_string(input->info()->dimension(2)));
107 
108  // Create kernel
109  _kernel = create_kernel(compile_context, "minmax_layer", build_opts);
110 
111  auto win_config = validate_and_configure_window(input->info(), output->info());
112 
113  ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config));
114 
115  ICLKernel::configure_internal(std::get<1>(win_config));
116 }
117 
119 {
121  ARM_COMPUTE_RETURN_ON_ERROR(std::get<0>(validate_and_configure_window(input->clone().get(), output->clone().get())));
122 
123  return Status{};
124 }
125 
126 void CLMinMaxLayerKernel::reset(cl::CommandQueue &queue)
127 {
128  _output->map(queue, true);
129 
130  Window window_output;
131  window_output.use_tensor_dimensions(_output->info()->tensor_shape());
132  window_output.set(Window::DimX, Window::Dimension(0, 1, 1));
133 
134  Iterator output(_output, window_output);
135 
136  // Reset output
137  execute_window_loop(window_output, [&](const Coordinates &)
138  {
139  auto *ptr = reinterpret_cast<float *>(output.ptr());
140  ptr[0] = std::numeric_limits<float>::max();
141  ptr[1] = std::numeric_limits<float>::min();
142  },
143  output);
144 
145  _output->unmap(queue);
146 }
147 
148 void CLMinMaxLayerKernel::run(const Window &window, cl::CommandQueue &queue)
149 {
152 
153  Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), 3);
154  Window slice = window_collapsed.first_slice_window_3D();
155  slice.set(Window::DimX, Window::Dimension(0, 1, 1));
156  slice.set(Window::DimY, Window::Dimension(0, 1, 1));
157  slice.set(Window::DimZ, Window::Dimension(0, 1, 1));
158 
159  do
160  {
161  Window output_slice = slice.shift_dimensions(2);
162 
163  unsigned int idx = 0;
164  // Set inputs
165  add_3D_tensor_argument(idx, _input, slice);
166  add_1D_tensor_argument(idx, _output, output_slice);
167  enqueue(queue, *this, slice, lws_hint());
168  }
169  while(window_collapsed.slide_window_slice_3D(slice));
170 }
virtual size_t num_dimensions() const =0
The number of dimensions of the tensor (rank)
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
void map(cl::CommandQueue &q, bool blocking=true)
Enqueue a map operation of the allocated buffer on the given queue.
Definition: ICLTensor.cpp:35
Shape of a tensor.
Definition: TensorShape.h:39
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
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
void run(const Window &window, cl::CommandQueue &queue) override
Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue...
cl::NDRange lws_hint() const
Return the Local-Workgroup-Size hint.
Definition: ICLKernel.h:276
#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.
virtual DataType data_type() const =0
Data type used for each element of the tensor.
1 channel, 1 F32 per channel
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(cond)
If the condition is true, an error is returned.
Definition: Error.h:296
#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
void use_tensor_dimensions(const TensorShape &shape, size_t first_dimension=Window::DimX)
Use the tensor&#39;s dimensions to fill the window dimensions.
Definition: Window.inl:276
Copyright (c) 2017-2021 Arm Limited.
TensorShape compute_min_max_shape(const ITensorInfo *input)
Calculate the min/max shape output shape of a tensor.
Implementation of a static rectangular access pattern.
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:163
void configure(const ICLTensor *input, ICLTensor *output)
Initialise the kernel&#39;s input and output.
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
CLMinMaxLayerKernel()
Default constructor.
Window collapse_if_possible(const Window &full_window, size_t first, size_t last, bool *has_collapsed=nullptr) const
Collapse the dimensions between first and last if possible.
Definition: Window.inl:68
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
void unmap(cl::CommandQueue &q)
Enqueue an unmap operation of the allocated and mapped buffer on the given queue. ...
Definition: ICLTensor.cpp:40
Class to describe a number of elements in each dimension.
Definition: Steps.h:40
Coordinates of an item.
Definition: Coordinates.h:37
Implementation of a row access pattern.
size_t total_size() const
Collapses all dimensions to a single linear total size.
Definition: TensorShape.h:172
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.
constexpr uint8_t * ptr() const
Return a pointer to the current pixel.
Definition: Helpers.inl:139
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:49
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.
void reset(cl::CommandQueue &queue)
Resets global minimum and maximum.
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
Interface for OpenCL tensor.
Definition: ICLTensor.h:42
#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_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
void execute_window_loop(const Window &w, L &&lambda_function, Ts &&... iterators)
Iterate through the passed window, automatically adjusting the iterators and calling the lambda_funct...
Definition: Helpers.inl:77
Container for valid region of a window.
Definition: Types.h:188
void add_1D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
Add the passed 1D tensor&#39;s parameters to the object&#39;s kernel&#39;s arguments starting from the index idx...
Definition: ICLKernel.h:124
Iterator updated by execute_window_loop for each window element.
Definition: Helpers.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
Window shift_dimensions(unsigned int shift_value) const
Shift down all the dimensions of a window.
Definition: Window.inl:101
static Status validate(const ITensorInfo *input, const ITensorInfo *output)
Static function to check if given info will lead to a valid configuration of CLMinMaxLayerKernel.
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205
SimpleTensor< T > slice(const SimpleTensor< T > &src, Coordinates starts, Coordinates ends)