Compute Library
 21.02
NEReverseKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2021 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 
32 
33 namespace arm_compute
34 {
35 namespace
36 {
37 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *axis)
38 {
39  ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output, axis);
40  //Note: ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input) is not needed here as this kernel doesn't use Neon FP16 instructions.
41  ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
43  ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis->num_dimensions() > 1, "Axis must be a 1D tensor");
44  ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis->dimension(0) > 4, "Only up to 4 dimensions can be reversed");
45 
46  // Checks performed when output is configured
47  if(output->total_size() != 0)
48  {
52  }
53 
54  return Status{};
55 }
56 } // namespace
57 
59  : _input(nullptr), _output(nullptr), _axis(nullptr)
60 {
61 }
62 
63 void NEReverseKernel::configure(const ITensor *input, ITensor *output, const ITensor *axis)
64 {
65  ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, axis);
66 
67  _input = input;
68  _output = output;
69  _axis = axis;
70 
71  // Output tensor auto initialization if not yet initialized
72  auto_init_if_empty(*output->info(), *input->info()->clone());
73 
74  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), axis->info()));
75 
76  // Configure kernel window
77  INEKernel::configure(calculate_max_window(*output->info()));
78 }
79 
80 Status NEReverseKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *axis)
81 {
82  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, axis));
83 
84  return Status{};
85 }
86 
87 template <typename T>
88 void run_reverse(const Window &window, const ITensor *input, const ITensor *axis, ITensor *output)
89 {
90  int axis_bit = 0;
91  for(unsigned int i = 0; i < axis->info()->dimension(0); ++i)
92  {
93  const int axis_i = *(reinterpret_cast<const int *>(axis->buffer()) + i);
94  axis_bit |= 1 << axis_i;
95  }
96 
97  // Check if we need a left-over loop for the y dimension
98  const int window_step_x = 16 / input->info()->element_size();
99  const int window_start_x = window.x().start();
100  const int window_end_x = window.x().end();
101 
102  Window win(window);
103  win.set(Window::DimX, Window::Dimension(0, 1, 1));
104 
105  Iterator input_it(input, win);
106  execute_window_loop(win, [&](const Coordinates & id)
107  {
108  int x = window_start_x;
109  for(; x <= (window_end_x - window_step_x); x += window_step_x)
110  {
111  auto in = wrapper::vloadq(reinterpret_cast<T *>(input_it.ptr()) + x);
112 
113  // Reverse 0 axis
114  if(axis_bit & 0x1)
115  {
116  in = wrapper::vrev64(in);
118  }
119 
120  const int offset_x = (axis_bit & 0x1) ? output->info()->dimension(0) - x - window_step_x : x;
121  const int offset_y = (axis_bit & 0x2) ? output->info()->dimension(1) - id.y() - 1 : id.y();
122  const int offset_z = (axis_bit & 0x4) ? output->info()->dimension(2) - id.z() - 1 : id.z();
123  const int offset_w = (axis_bit & 0x8) ? output->info()->dimension(3) - id[3] - 1 : id[3];
124 
125  auto out_ptr = reinterpret_cast<T *>(output->ptr_to_element(Coordinates(offset_x, offset_y, offset_z, offset_w)));
126  wrapper::vstore(out_ptr, in);
127  }
128 
129  // Compute left-over elements
130  for(; x < window_end_x; ++x)
131  {
132  const auto in = *(reinterpret_cast<T *>(input_it.ptr()) + x);
133 
134  const int offset_x = (axis_bit & 0x1) ? output->info()->dimension(0) - x - 1 : x;
135  const int offset_y = (axis_bit & 0x2) ? output->info()->dimension(1) - id.y() - 1 : id.y();
136  const int offset_z = (axis_bit & 0x4) ? output->info()->dimension(2) - id.z() - 1 : id.z();
137  const int offset_w = (axis_bit & 0x8) ? output->info()->dimension(3) - id[3] - 1 : id[3];
138 
139  *reinterpret_cast<T *>(output->ptr_to_element(Coordinates(offset_x, offset_y, offset_z, offset_w))) = in;
140  }
141  },
142  input_it);
143 }
144 
146 {
147  ARM_COMPUTE_UNUSED(info);
150 
151  switch(_input->info()->element_size())
152  {
153  case 4:
154  run_reverse<uint32_t>(window, _input, _axis, _output);
155  break;
156  case 2:
157  run_reverse<uint16_t>(window, _input, _axis, _output);
158  break;
159  case 1:
160  run_reverse<uint8_t>(window, _input, _axis, _output);
161  break;
162  default:
163  ARM_COMPUTE_ERROR("Element size not supported");
164  }
165 }
166 } // namespace arm_compute
void run(const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
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
uint8_t * ptr_to_element(const Coordinates &id) const
Return a pointer to the element at the passed coordinates.
Definition: ITensor.h:63
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
static Status validate(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *axis)
Static function to check if given info will lead to a valid configuration of NEReverseKernel.
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(...)
Definition: Validate.h:610
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
uint8x16_t vloadq(const uint8_t *ptr)
Definition: load.h:58
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:204
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
Describe one of the image&#39;s dimensions with a start, end and step.
Definition: Window.h:77
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
Interface for Neon tensor.
Definition: ITensor.h:36
NEReverseKernel()
Default constructor.
void configure(const ITensor *input, ITensor *output, const ITensor *axis)
Initialise the kernel&#39;s inputs and output.
Copyright (c) 2017-2021 Arm Limited.
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:163
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
1 channel, 1 U32 per channel
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. ...
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...
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&#39;s metadata.
constexpr uint8_t * ptr() const
Return a pointer to the current pixel.
Definition: Helpers.inl:139
uint8x8_t vgetlow(const uint8x16_t val)
Definition: getlow.h:39
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
uint8x16_t vcombine(const uint8x8_t &a, const uint8x8_t &b)
Definition: combine.h:39
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:941
uint8x8_t vgethigh(const uint8x16_t val)
Definition: gethigh.h:39
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
Information about executing thread and CPU.
Definition: CPPTypes.h:235
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(...)
Definition: Validate.h:443
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:545
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:792
Status validate_arguments(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, const GEMMLowpOutputStageInfo *output_stage)
uint8x8_t vrev64(const uint8x8_t &a)
Definition: rev64.h:39
void vstore(uint8_t *ptr, uint8x8_t val)
Definition: store.h:39
#define ARM_COMPUTE_RETURN_ERROR_ON_MSG(cond, msg)
If the condition is true, an error is returned.
Definition: Error.h:244
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
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
Includes all wrapper headers at once.
void run_reverse(const Window &window, const ITensor *input, const ITensor *axis, ITensor *output)
constexpr int end() const
Return the end of the dimension.
Definition: Window.h:99
Iterator updated by execute_window_loop for each window element.
Definition: Helpers.h:46
constexpr int start() const
Return the start of the dimension.
Definition: Window.h:94
Describe a multidimensional execution window.
Definition: Window.h:39
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205
constexpr const Dimension & x() const
Alias to access the first dimension of the window.
Definition: Window.h:145