Compute Library
 19.11
CLMinMaxLayerKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2019 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 
35 #include <climits>
36 
37 using namespace arm_compute;
39 
40 namespace
41 {
42 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output)
43 {
46  ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() < 3);
47 
48  if(output->tensor_shape().total_size() > 0)
49  {
51 
53 
55  }
56 
57  return Status{};
58 }
59 
60 std::tuple<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
61 {
63 
64  // Output auto initialization if not yet initialized
65  auto_init_if_empty(*output, output_shape, 1, input->data_type());
66 
67  const unsigned int num_elems_processed_per_iteration = 1;
68 
69  // Configure kernel window
70  Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
71  AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
72  AccessWindowStatic output_access(output, 0, 0, 2, output->dimension(1));
73 
74  bool window_changed = update_window_and_padding(win, input_access, output_access);
75 
76  output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
77 
78  Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
79  return std::make_tuple(err, win);
80 }
81 } // namespace
82 
84  : _input(nullptr), _output(nullptr)
85 {
86 }
87 
89 {
91  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info()));
92 
93  _input = input;
94  _output = output;
95 
96  std::set<std::string> build_opts;
97  build_opts.emplace("-DWIDTH=" + support::cpp11::to_string(input->info()->dimension(0)));
98  build_opts.emplace("-DHEIGHT=" + support::cpp11::to_string(input->info()->dimension(1)));
99  build_opts.emplace("-DDEPTH=" + support::cpp11::to_string(input->info()->dimension(2)));
100 
101  // Create kernel
102  _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("minmax_layer", build_opts));
103 
104  auto win_config = validate_and_configure_window(input->info(), output->info());
105 
106  ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config));
107 
108  ICLKernel::configure_internal(std::get<1>(win_config));
109 }
110 
112 {
113  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output));
114  ARM_COMPUTE_RETURN_ON_ERROR(std::get<0>(validate_and_configure_window(input->clone().get(), output->clone().get())));
115 
116  return Status{};
117 }
118 
119 void CLMinMaxLayerKernel::reset(cl::CommandQueue &queue)
120 {
121  _output->map(queue, true);
122 
123  Window window_output;
124  window_output.use_tensor_dimensions(_output->info()->tensor_shape());
125  window_output.set(Window::DimX, Window::Dimension(0, 1, 1));
126 
127  Iterator output(_output, window_output);
128 
129  // Reset output
130  execute_window_loop(window_output, [&](const Coordinates &)
131  {
132  auto *ptr = reinterpret_cast<float *>(output.ptr());
133  ptr[0] = std::numeric_limits<float>::max();
134  ptr[1] = std::numeric_limits<float>::min();
135  },
136  output);
137 
138  _output->unmap(queue);
139 }
140 
141 void CLMinMaxLayerKernel::run(const Window &window, cl::CommandQueue &queue)
142 {
145 
146  Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), 3);
147  Window slice = window_collapsed.first_slice_window_3D();
148  slice.set(Window::DimX, Window::Dimension(0, 1, 1));
149  slice.set(Window::DimY, Window::Dimension(0, 1, 1));
150  slice.set(Window::DimZ, Window::Dimension(0, 1, 1));
151 
152  do
153  {
154  Window output_slice = slice.shift_dimensions(2);
155 
156  unsigned int idx = 0;
157  // Set inputs
158  add_3D_tensor_argument(idx, _input, slice);
159  add_1D_tensor_argument(idx, _output, output_slice);
160  enqueue(queue, *this, slice, lws_hint());
161  }
162  while(window_collapsed.slide_window_slice_3D(slice));
163 }
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:247
#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:158
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:264
Copyright (c) 2017-2019 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:202
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.
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:402
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:185
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:49
std::unique_ptr< Kernel > create_kernel()
Helper function to create and return a unique_ptr pointed to a CL/GLES kernel object.
Definition: Helpers.h:86
bool slide_window_slice_3D(Window &slice) const
Slide the passed 3D window slice.
Definition: Window.h:333
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
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:123
Container for valid region of a window.
Definition: Types.h:183
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:110
Iterator updated by execute_window_loop for each window element.
Definition: Helpers.h:318
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)