Compute Library
 21.05
CPPUpsampleKernel.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 
28 
29 #include <cstddef>
30 #include <cstdint>
31 
32 namespace arm_compute
33 {
35  : _input(nullptr), _output(nullptr), _info()
36 {
37 }
38 
40 {
41  return false;
42 }
43 
45 {
47 
48  _input = input;
49  _output = output;
50  _info = info;
51 
52  // Configure kernel window
53  Window win = calculate_max_window(*input->info(), Steps());
54 
55  // The CPPUpsampleKernel doesn't need padding so update_window_and_padding() can be skipped
56  Coordinates coord;
57  coord.set_num_dimensions(output->info()->num_dimensions());
58  output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape()));
59 
60  ICPPKernel::configure(win);
61 }
62 
63 void CPPUpsampleKernel::run(const Window &window, const ThreadInfo &info)
64 {
68 
69  const DataLayout data_layout = _input->info()->data_layout();
72 
73  // Initialize _scaled_output buffer
74  const int width_scaled = _output->info()->dimension(idx_w);
75  const int height_scaled = _output->info()->dimension(idx_h);
76  const int stride_width = _info.stride().first;
77  const int stride_height = _info.stride().second;
78  const int start_width = _info.pad_left();
79  const int start_height = _info.pad_top();
80  const int end_width = width_scaled - _info.pad_right();
81  const int end_height = height_scaled - _info.pad_bottom();
82  const size_t element_size = _input->info()->element_size();
83 
84  // The fill value is normally 0, but for quantized types '0' corresponds to the offset
85  switch(_output->info()->data_type())
86  {
87  case DataType::QASYMM8:
88  {
89  const uint8_t fill_value = _output->info()->quantization_info().uniform().offset;
90  std::fill_n(_output->buffer(), _output->info()->total_size(), fill_value);
91  }
92  break;
94  {
95  const int8_t fill_value = _output->info()->quantization_info().uniform().offset;
96  std::fill_n(_output->buffer(), _output->info()->total_size(), fill_value);
97  }
98  break;
99  default:
100  std::fill_n(_output->buffer(), _output->info()->total_size(), 0);
101  }
102 
103  // Create window
104  Window window_out(window);
106  {
107  window_out.set(Window::DimX, Window::Dimension(start_width, end_width, stride_width));
108  window_out.set(Window::DimY, Window::Dimension(start_height, end_height, stride_height));
109  }
110  else
111  {
112  window_out.set(Window::DimY, Window::Dimension(start_width, end_width, stride_width));
113  window_out.set(Window::DimZ, Window::Dimension(start_height, end_height, stride_height));
114  }
115 
116  // Create iterators
117  Iterator in(_input, window);
118  Iterator out(_output, window_out);
119 
121  {
122  memcpy(out.ptr(), in.ptr(), element_size);
123  },
124  in, out);
125 }
126 } // namespace arm_compute
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
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
virtual DataType data_type() const =0
Data type used for each element of the tensor.
const DataLayout data_layout
Definition: Im2Col.cpp:151
Describe one of the image's dimensions with a start, end and step.
Definition: Window.h:77
unsigned int pad_top() const
Get the top padding.
Definition: Types.h:734
Interface for CPU tensor.
Definition: ITensor.h:36
CPPUpsampleKernel()
Default constructor.
Copyright (c) 2017-2021 Arm Limited.
virtual void set_valid_region(const ValidRegion &valid_region)=0
Set the valid region of the tensor.
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
quantized, asymmetric fixed-point 8-bit number unsigned
Class to describe a number of elements in each dimension.
Definition: Steps.h:40
Coordinates of an item.
Definition: Coordinates.h:37
virtual uint8_t * buffer() const =0
Interface to be implemented by the child class to return a pointer to CPU memory.
std::pair< unsigned int, unsigned int > stride() const
Get the stride.
Definition: Types.h:698
UniformQuantizationInfo uniform() const
Return per layer quantization info.
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor's metadata.
unsigned int pad_right() const
Get the right padding.
Definition: Types.h:729
constexpr uint8_t * ptr() const
Return a pointer to the current pixel.
Definition: Helpers.inl:139
Padding and stride information class.
Definition: Types.h:650
virtual size_t element_size() const =0
Element size in bytes calculated as data_size() * num_channels()
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:49
virtual QuantizationInfo quantization_info() const =0
Get the quantization settings (scale and offset) of the tensor.
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:915
bool is_parallelisable() const override
Indicates whether or not the kernel is parallelisable.
Num samples, channels, height, width.
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
Information about executing thread and CPU.
Definition: CPPTypes.h:252
virtual size_t total_size() const =0
Returns the total size of the tensor in bytes.
void configure(const ITensor *input, ITensor *output, const PadStrideInfo &info)
Set the input and output of the kernel.
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:157
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
void set_num_dimensions(size_t num_dimensions)
Set number of dimensions.
Definition: Dimensions.h:149
quantized, asymmetric fixed-point 8-bit number signed
Container for valid region of a window.
Definition: Types.h:182
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:193
unsigned int pad_bottom() const
Get the bottom padding.
Definition: Types.h:739
Iterator updated by execute_window_loop for each window element.
Definition: Helpers.h:46
unsigned int pad_left() const
Get the left padding.
Definition: Types.h:724
DataLayout
[DataLayout enum definition]
Definition: Types.h:114
Describe a multidimensional execution window.
Definition: Window.h:39
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:201
void run(const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
virtual DataLayout data_layout() const =0
Get the data layout of the tensor.