Compute Library
 23.11
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 
27 
29 
30 #include <cstddef>
31 #include <cstdint>
32 
33 namespace arm_compute
34 {
35 CPPUpsampleKernel::CPPUpsampleKernel() : _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  window, [&](const Coordinates &) { memcpy(out.ptr(), in.ptr(), element_size); }, in, out);
122 }
123 } // namespace arm_compute
arm_compute::DataLayout::NCHW
@ NCHW
Num samples, channels, height, width.
arm_compute::Steps
Class to describe a number of elements in each dimension.
Definition: Steps.h:40
arm_compute::ITensorInfo::data_layout
virtual DataLayout data_layout() const =0
Get the data layout of the tensor.
arm_compute::UniformQuantizationInfo::offset
int32_t offset
Definition: QuantizationInfo.h:63
arm_compute::ITensorInfo::tensor_shape
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
Helpers.h
arm_compute::DataLayout
DataLayout
[DataLayout enum definition]
Definition: CoreTypes.h:110
arm_compute::calculate_max_window
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
Definition: WindowHelpers.cpp:29
arm_compute::PadStrideInfo::pad_right
unsigned int pad_right() const
Get the right padding.
Definition: CoreTypes.h:223
arm_compute::DataType::QASYMM8
@ QASYMM8
quantized, asymmetric fixed-point 8-bit number unsigned
arm_compute::CPPUpsampleKernel::CPPUpsampleKernel
CPPUpsampleKernel()
Default constructor.
Definition: CPPUpsampleKernel.cpp:35
ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:1079
arm_compute::ITensorInfo::element_size
virtual size_t element_size() const =0
Element size in bytes calculated as data_size() * num_channels()
arm_compute::Window::DimX
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
arm_compute::ITensor
Interface for CPU tensor.
Definition: ITensor.h:36
arm_compute::DataLayoutDimension::WIDTH
@ WIDTH
width
arm_compute::cpu::data_layout
constexpr auto data_layout
Definition: impl.h:36
arm_compute::ITensorInfo::dimension
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
ARM_COMPUTE_ERROR_ON_NULLPTR
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:159
arm_compute::ITensor::info
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor's metadata.
arm_compute::Iterator::ptr
constexpr uint8_t * ptr() const
Return a pointer to the current pixel.
Definition: Helpers.inl:147
arm_compute::CPPUpsampleKernel::run
void run(const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
Definition: CPPUpsampleKernel.cpp:63
arm_compute::execute_window_loop
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:74
arm_compute::DataLayoutDimension::HEIGHT
@ HEIGHT
height
arm_compute::Iterator
Iterator updated by execute_window_loop for each window element.
Definition: Helpers.h:46
arm_compute::ValidRegion
Container for valid region of a window.
Definition: Types.h:143
arm_compute::DataType::QASYMM8_SIGNED
@ QASYMM8_SIGNED
quantized, asymmetric fixed-point 8-bit number signed
WindowHelpers.h
ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:203
arm_compute::ITensorInfo::data_type
virtual DataType data_type() const =0
Data type used for each element of the tensor.
CPPUpsampleKernel.h
arm_compute::QuantizationInfo::uniform
UniformQuantizationInfo uniform() const
Return per layer quantization info.
Definition: QuantizationInfo.h:140
ARM_COMPUTE_UNUSED
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:151
arm_compute::Coordinates
Coordinates of an item.
Definition: Coordinates.h:37
arm_compute::Window::Dimension
Describe one of the image's dimensions with a start, end and step.
Definition: Window.h:79
arm_compute::PadStrideInfo
Definition: CoreTypes.h:139
arm_compute::Window::set
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:53
arm_compute::Window::DimY
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
arm_compute::ITensorInfo::quantization_info
virtual QuantizationInfo quantization_info() const =0
Get the quantization settings (scale and offset) of the tensor.
arm_compute::PadStrideInfo::pad_left
unsigned int pad_left() const
Get the left padding.
Definition: CoreTypes.h:218
arm_compute::IKernel::window
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
arm_compute::ThreadInfo
Information about executing thread and CPU.
Definition: CPPTypes.h:180
arm_compute::get_data_layout_dimension_index
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:201
arm_compute::PadStrideInfo::pad_bottom
unsigned int pad_bottom() const
Get the bottom padding.
Definition: CoreTypes.h:233
arm_compute::Window
Describe a multidimensional execution window.
Definition: Window.h:39
arm_compute
Copyright (c) 2017-2023 Arm Limited.
Definition: introduction.dox:24
arm_compute::ITensorInfo::set_valid_region
virtual void set_valid_region(const ValidRegion &valid_region)=0
Set the valid region of the tensor.
arm_compute::Dimensions::set_num_dimensions
void set_num_dimensions(size_t num_dimensions)
Set number of dimensions.
Definition: Dimensions.h:148
arm_compute::Window::DimZ
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
arm_compute::CPPUpsampleKernel::is_parallelisable
bool is_parallelisable() const override
Indicates whether or not the kernel is parallelisable.
Definition: CPPUpsampleKernel.cpp:39
arm_compute::test::validation::info
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
arm_compute::CPPUpsampleKernel::configure
void configure(const ITensor *input, ITensor *output, const PadStrideInfo &info)
Set the input and output of the kernel.
Definition: CPPUpsampleKernel.cpp:44
arm_compute::ITensorInfo::total_size
virtual size_t total_size() const =0
Returns the total size of the tensor in bytes.
arm_compute::PadStrideInfo::pad_top
unsigned int pad_top() const
Get the top padding.
Definition: CoreTypes.h:228
arm_compute::test::validation::input
auto input
Definition: LSTMLayerQuantized.cpp:486
arm_compute::ITensorInfo::num_dimensions
virtual size_t num_dimensions() const =0
The number of dimensions of the tensor (rank)
arm_compute::PadStrideInfo::stride
std::pair< unsigned int, unsigned int > stride() const
Get the stride.
Definition: CoreTypes.h:192
arm_compute::ITensor::buffer
virtual uint8_t * buffer() const =0
Interface to be implemented by the child class to return a pointer to CPU memory.