Compute Library
 20.08
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 
34 #include "support/StringSupport.h"
35 
36 #include <climits>
37 
38 using namespace arm_compute;
40 
41 namespace
42 {
44 {
47  ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() < 3);
48 
49  if(output->tensor_shape().total_size() > 0)
50  {
52 
54 
56  }
57 
58  return Status{};
59 }
60 
61 std::tuple<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
62 {
64 
65  // Output auto initialization if not yet initialized
66  auto_init_if_empty(*output, output_shape, 1, input->data_type());
67 
68  const unsigned int num_elems_processed_per_iteration = 1;
69 
70  // Configure kernel window
73  AccessWindowStatic output_access(output, 0, 0, 2, output->dimension(1));
74 
75  bool window_changed = update_window_and_padding(win, input_access, output_access);
76 
77  output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
78 
79  Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
80  return std::make_tuple(err, win);
81 }
82 } // namespace
83 
85  : _input(nullptr), _output(nullptr)
86 {
87 }
88 
90 {
91  configure(CLKernelLibrary::get().get_compile_context(), input, output);
92 }
93 
94 void CLMinMaxLayerKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output)
95 {
98 
99  _input = input;
100  _output = output;
101 
102  std::set<std::string> build_opts;
103  build_opts.emplace("-DWIDTH=" + support::cpp11::to_string(input->info()->dimension(0)));
104  build_opts.emplace("-DHEIGHT=" + support::cpp11::to_string(input->info()->dimension(1)));
105  build_opts.emplace("-DDEPTH=" + support::cpp11::to_string(input->info()->dimension(2)));
106 
107  // Create kernel
108  _kernel = create_kernel(compile_context, "minmax_layer", build_opts);
109 
110  auto win_config = validate_and_configure_window(input->info(), output->info());
111 
112  ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config));
113 
114  ICLKernel::configure_internal(std::get<1>(win_config));
115 }
116 
118 {
120  ARM_COMPUTE_RETURN_ON_ERROR(std::get<0>(validate_and_configure_window(input->clone().get(), output->clone().get())));
121 
122  return Status{};
123 }
124 
125 void CLMinMaxLayerKernel::reset(cl::CommandQueue &queue)
126 {
127  _output->map(queue, true);
128 
129  Window window_output;
130  window_output.use_tensor_dimensions(_output->info()->tensor_shape());
131  window_output.set(Window::DimX, Window::Dimension(0, 1, 1));
132 
133  Iterator output(_output, window_output);
134 
135  // Reset output
136  execute_window_loop(window_output, [&](const Coordinates &)
137  {
138  auto *ptr = reinterpret_cast<float *>(output.ptr());
139  ptr[0] = std::numeric_limits<float>::max();
140  ptr[1] = std::numeric_limits<float>::min();
141  },
142  output);
143 
144  _output->unmap(queue);
145 }
146 
147 void CLMinMaxLayerKernel::run(const Window &window, cl::CommandQueue &queue)
148 {
151 
152  Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), 3);
153  Window slice = window_collapsed.first_slice_window_3D();
154  slice.set(Window::DimX, Window::Dimension(0, 1, 1));
155  slice.set(Window::DimY, Window::Dimension(0, 1, 1));
156  slice.set(Window::DimZ, Window::Dimension(0, 1, 1));
157 
158  do
159  {
160  Window output_slice = slice.shift_dimensions(2);
161 
162  unsigned int idx = 0;
163  // Set inputs
164  add_3D_tensor_argument(idx, _input, slice);
165  add_1D_tensor_argument(idx, _output, output_slice);
166  enqueue(queue, *this, slice, lws_hint());
167  }
168  while(window_collapsed.slide_window_slice_3D(slice));
169 }
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
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
void enqueue(cl::CommandQueue &queue, ICLKernel &kernel, const Window &window, const cl::NDRange &lws_hint=CLKernelLibrary::get().default_ndrange(), bool use_dummy_work_items=false)
Add the kernel to the command queue with the given window.
Definition: ICLKernel.cpp:39
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:545
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:263
#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.
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:792
1 channel, 1 F32 per channel
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
Store the tensor's metadata.
Definition: ITensorInfo.h:40
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
Describe one of the image's dimensions with a start, end and step.
Definition: Window.h:75
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
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps=Steps(), bool skip_border=false, BorderSize border_size=BorderSize())
Calculate the maximum window for a given tensor shape and border setting.
Definition: Helpers.cpp:28
void add_3D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
Add the passed 3D tensor's parameters to the object's kernel's arguments starting from the index idx.
Definition: ICLKernel.h:159
void use_tensor_dimensions(const TensorShape &shape, size_t first_dimension=Window::DimX)
Use the tensor's dimensions to fill the window dimensions.
Definition: Window.inl:276
Copyright (c) 2017-2020 Arm Limited.
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...
Definition: Helpers.inl:207
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.
void configure(const ICLTensor *input, ICLTensor *output)
Initialise the kernel'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: Helpers.h:437
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.
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(...)
Definition: Validate.h:288
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:171
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's metadata.
constexpr uint8_t * ptr() const
Return a pointer to the current pixel.
Definition: Helpers.inl:190
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:333
CLCompileContext class.
void reset(cl::CommandQueue &queue)
Resets global minimum and maximum.
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:163
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
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
Status validate_arguments(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, const GEMMLowpOutputStageInfo *output_stage)
unsigned int num_elems_processed_per_iteration
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:128
Container for valid region of a window.
Definition: Types.h:187
void add_1D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
Add the passed 1D tensor's parameters to the object's kernel's arguments starting from the index idx.
Definition: ICLKernel.h:111
Iterator updated by execute_window_loop for each window element.
Definition: Helpers.h:353
Window first_slice_window_3D() const
First 3D slice of the window.
Definition: Window.h:289
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205
Describe a multidimensional execution window.
Definition: Window.h:39
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:941
static Status validate(const ITensorInfo *input, const ITensorInfo *output)
Static function to check if given info will lead to a valid configuration of CLMinMaxLayerKernel.
SimpleTensor< T > slice(const SimpleTensor< T > &src, Coordinates starts, Coordinates ends)