Compute Library
 19.08
GCNormalizePlanarYUVLayerKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2018 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"
34 
36 
37 using namespace arm_compute;
38 
39 namespace
40 {
41 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *mean, const ITensorInfo *std)
42 {
46 
49  ARM_COMPUTE_RETURN_ERROR_ON_MSG(mean->num_dimensions() > 1, "mean and std must be vectors");
50 
51  const unsigned int channel_idx = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::CHANNEL);
52  ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(channel_idx) != mean->dimension(0));
53 
54  // Checks performed when output is configured
55  if(output->total_size() != 0)
56  {
59  }
60 
61  return Status{};
62 }
63 
64 std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, ITensorInfo *mean, ITensorInfo *std)
65 {
66  // Output tensor auto initialization if not yet initialized
67  auto_init_if_empty(*output, *input->clone());
68 
69  const unsigned int num_elems_processed_per_iteration = 4;
70 
71  // Configure kernel window
72  Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
73 
74  AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
75  AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
76  const int mean_padding = ceil_to_multiple(mean->dimension(0), num_elems_processed_per_iteration) - mean->dimension(0);
77  const int std_padding = ceil_to_multiple(std->dimension(0), num_elems_processed_per_iteration) - std->dimension(0);
78  AccessWindowStatic mean_access(mean, 0, 0, mean->dimension(0) + mean_padding, mean->dimension(1));
79  AccessWindowStatic std_access(std, 0, 0, std->dimension(0) + std_padding, std->dimension(1));
80 
81  const bool window_changed = update_window_and_padding(win, input_access, output_access, mean_access, std_access);
82  output_access.set_valid_region(win, input->valid_region());
83 
84  Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
85  return std::make_pair(err, win);
86 }
87 } // namespace
88 
90  : _input(nullptr), _output(nullptr), _mean(nullptr), _std(nullptr)
91 {
92 }
93 
94 void GCNormalizePlanarYUVLayerKernel::configure(const IGCTensor *input, IGCTensor *output, const IGCTensor *mean, const IGCTensor *std)
95 {
96  ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, mean, std);
97 
98  // Output tensor auto initialization if not yet initialized
99  auto_init_if_empty(*output->info(), *input->info()->clone());
100 
101  // Perform validation step
102  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), mean->info(), std->info()));
103 
104  _input = input;
105  _output = output;
106  _mean = mean;
107  _std = std;
108 
109  // Set build options
110  std::set<std::string> build_opts;
111  build_opts.emplace(("#define LOCAL_SIZE_X " + support::cpp11::to_string(1)));
112  build_opts.emplace(("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1)));
113  build_opts.emplace(("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1)));
114 
115  // Create kernel
116  _kernel = static_cast<GCKernel>(GCKernelLibrary::get().create_kernel("normalize_planar_yuv_layer", build_opts));
117 
118  // Configure kernel window
119  auto win_config = validate_and_configure_window(input->info(), output->info(), mean->info(), std->info());
120  ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config));
121 
122  IGCKernel::configure(std::get<1>(win_config));
123 }
124 
126 {
127  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, mean, std));
128  ARM_COMPUTE_RETURN_ON_ERROR(std::get<0>(validate_and_configure_window(input->clone().get(), output->clone().get(), mean->clone().get(), std->clone().get())));
129  return Status{};
130 }
131 
133 {
136 
137  _kernel.use();
138 
139  _output->set_needs_shifting(true);
140 
142 
143  Window slice_in;
144  //slice_in.use_tensor_dimensions(_mean->info()->tensor_shape());
145  slice_in = window.first_slice_window_1D();
146  slice_in.set(Window::DimX, Window::Dimension(0, 0, 0));
147 
148  unsigned int idx = 2 * num_arguments_per_3D_tensor();
149  add_1D_tensor_argument(idx, _mean, 3, slice_in);
150  add_1D_tensor_argument(idx, _std, 4, slice_in);
151 
152  slice_in = window.first_slice_window_3D();
153 
154  slice.shift(Window::DimX, -(_output->info()->padding()).left);
155 
156  do
157  {
158  idx = 0;
159  add_3D_tensor_argument(idx, _input, 1, slice_in);
160  add_3D_tensor_argument(idx, _output, 2, slice);
161 
162  _kernel.update_shader_params();
163 
164  enqueue(*this, slice);
165  }
167 }
virtual size_t num_dimensions() const =0
The number of dimensions of the tensor (rank)
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
void add_3D_tensor_argument(unsigned int &idx, const IGCTensor *tensor, const unsigned int binding_point, const Window &window)
Add the passed 3D tensor's parameters to the object's kernel's arguments starting from the index idx.
Definition: IGCKernel.cpp:132
void configure(const IGCTensor *input, IGCTensor *output, const IGCTensor *mean, const IGCTensor *std)
Set the input and output tensors.
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
std::pair< Status, Window > validate_and_configure_window(ITensorInfo *input, ITensorInfo *weights, ITensorInfo *biases, ITensorInfo *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier, const Size2D &dilation)
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:193
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:791
Store the tensor's metadata.
Definition: ITensorInfo.h:40
Interface for GLES Compute tensor.
Definition: IGCTensor.h:35
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:327
Describe one of the image's dimensions with a start, end and step.
Definition: Window.h:75
unsigned int num_arguments_per_3D_tensor() const
Returns the number of arguments enqueued per 3D tensor object.
Definition: IGCKernel.cpp:147
Status class.
Definition: Error.h:52
void run(const Window &window) override
Enqueue the OpenGL ES shader to process the given window.
#define ARM_COMPUTE_CREATE_ERROR(error_code,...)
Creates an error with a given message.
Definition: Error.h:167
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:244
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
Copyright (c) 2017-2018 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:201
1 channel, 1 F16 per channel
virtual ValidRegion valid_region() const =0
Valid region of the tensor.
static Status validate(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *mean, const ITensorInfo *std)
Static function to check if given info will lead to a valid configuration of GCNormalizePlanarYUVLaye...
Implementation of a static rectangular access pattern.
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
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(...)
Definition: Validate.h:443
auto ceil_to_multiple(S value, T divisor) -> decltype(((value+divisor - 1)/divisor) *divisor)
Computes the smallest number larger or equal to value that is a multiple of divisor.
Definition: Utils.h:66
Class to describe a number of elements in each dimension.
Definition: Steps.h:40
void set_needs_shifting(bool needs_shifting)
Set the flag indicating whether or not a tensor needs shifting.
Definition: IGCTensor.cpp:61
Implementation of a row access pattern.
#define ARM_COMPUTE_RETURN_ERROR_ON_MSG(cond,...)
If the condition is true, an error is returned.
Definition: Error.h:214
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.
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:48
virtual PaddingSize padding() const =0
Padding of tensor.
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:319
Num samples, channels, height, width.
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:163
static GCKernelLibrary & get()
Get the static instance of GCKernelLibrary.
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
virtual size_t total_size() const =0
Returns the total size of the tensor in bytes.
size_t get_data_layout_dimension_index(const DataLayout data_layout, const DataLayoutDimension data_layout_dimension)
Get the index of the given dimension.
Definition: Helpers.inl:326
Window first_slice_window_3D() const
First 3D slice of the window.
Definition: Window.h:275
#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:940
void add_1D_tensor_argument(unsigned int &idx, const IGCTensor *tensor, const unsigned int binding_point, const Window &window)
Add the passed 1D tensor's parameters to the object's kernel's arguments starting from the index idx.
Definition: IGCKernel.cpp:122
Window first_slice_window_1D() const
First 1D slice of the window.
Definition: Window.h:259
SimpleTensor< T > slice(const SimpleTensor< T > &src, Coordinates starts, Coordinates ends)
virtual DataLayout data_layout() const =0
Get the data layout of the tensor.