Compute Library
 21.02
NEGEMMInterleave4x4Kernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-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 
26 #include "arm_compute/core/Error.h"
29 #include "arm_compute/core/Types.h"
36 
37 #include <arm_neon.h>
38 #include <cstddef>
39 #include <cstdint>
40 #include <tuple>
41 
42 using namespace arm_compute;
44 
45 namespace
46 {
48 {
50  //Note: ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input) is not needed here as this kernel doesn't use Neon FP16 instructions.
53 
54  if(output->total_size() != 0)
55  {
57  output_shape.set(0, input->dimension(0) * 4);
58  output_shape.set(1, std::ceil(input->dimension(1) / 4.0f));
62  }
63 
64  return Status{};
65 }
66 } // namespace
67 
69  : _func(nullptr)
70 {
71 }
72 
74 {
75  ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
76 
77  // Output auto inizialitation if not yet initialized
78  auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(compute_interleaved_shape(*input->info())));
79 
80  // Perform validate step
82 
83  _input = input;
84  _output = output;
85 
86  switch(input->info()->element_size())
87  {
88  case 1:
89  _func = &NEGEMMInterleave4x4Kernel::gemm_interleave4x4<uint8_t>;
90  break;
91  case 2:
92  _func = &NEGEMMInterleave4x4Kernel::gemm_interleave4x4<uint16_t>;
93  break;
94  case 4:
95  _func = &NEGEMMInterleave4x4Kernel::gemm_interleave4x4<uint32_t>;
96  break;
97  default:
98  ARM_COMPUTE_ERROR_ON("Element size not supported");
99  break;
100  }
101 
102  Window win = calculate_max_window(*input->info(), Steps(1, 4));
103 
104  Coordinates coord;
105  coord.set_num_dimensions(output->info()->num_dimensions());
106  output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape()));
107 
108  INEKernel::configure(win);
109 }
110 
112 {
114 
115  return Status{};
116 }
117 
118 template <typename ScalarType>
119 void NEGEMMInterleave4x4Kernel::gemm_interleave4x4(const ITensor *input, ITensor *output, const Window &window)
120 {
121  const size_t window_start_x = window.x().start();
122  const size_t window_end_x = window.x().end();
123 
124  const size_t in_height = input->info()->dimension(1);
125  const size_t in_stride = input->info()->strides_in_bytes()[1];
126 
127  const size_t partial_y = in_height % 4;
128 
129  // Set window for the input tensor
130  Window win = window;
131  win.set(Window::DimX, Window::Dimension(0, 1, 1));
132 
133  // Set window for the output tensor
134  Window win_out(window);
135  win_out.set(Window::DimX, Window::Dimension(0, 1, 1));
136  win_out.scale(Window::DimY, 0.25f);
137 
138  Iterator in(input, win);
139  Iterator out(output, win_out);
140 
141  execute_window_loop(win, [&](const Coordinates & id)
142  {
143  if(id.y() + 4 <= static_cast<int>(in_height))
144  {
145  for(size_t x = window_start_x; x < window_end_x; ++x)
146  {
147  const ScalarType data[4] =
148  {
149  *(reinterpret_cast<const ScalarType *>(in.ptr() + 0 * in_stride) + x),
150  *(reinterpret_cast<const ScalarType *>(in.ptr() + 1 * in_stride) + x),
151  *(reinterpret_cast<const ScalarType *>(in.ptr() + 2 * in_stride) + x),
152  *(reinterpret_cast<const ScalarType *>(in.ptr() + 3 * in_stride) + x),
153  };
154  std::memcpy(out.ptr() + x * 4 * sizeof(ScalarType), data, 4 * sizeof(ScalarType));
155  }
156  }
157  else
158  {
159  for(size_t x = window_start_x; x < window_end_x; ++x)
160  {
161  ScalarType data[4] = { 0, 0, 0, 0 };
162 
163  for(size_t y = 0; y < partial_y; ++y)
164  {
165  data[y] = *(reinterpret_cast<const ScalarType *>(in.ptr() + y * in_stride) + x);
166  }
167 
168  std::memcpy(out.ptr() + x * 4 * sizeof(ScalarType), data, 4 * sizeof(ScalarType));
169  }
170  }
171  },
172  in, out);
173 }
174 
176 {
177  ARM_COMPUTE_UNUSED(info);
180  ARM_COMPUTE_ERROR_ON(_func == nullptr);
181  /*
182  * This kernel puts the values in a 4x4 block of Matrix A on the same row (Interleaved values)
183  * |a00 a01 a02 a03|
184  * |a10 a11 a12 a13|
185  * |a20 a21 a22 a23| = | a00 a10 a20 a30 || a01 a11 a21 a31 || a02 a12 a22 a32 || a03 a13 a23 a33 |
186  * |a30 a31 a32 a33|
187  *
188  * After this operation, the output matrix will have the following shape: [ height * 4, ceil(width / 4.0f) ]
189  */
190  (this->*_func)(_input, _output, window);
191 }
virtual size_t num_dimensions() const =0
The number of dimensions of the tensor (rank)
void scale(size_t dimension, float scale_value)
Scale the values of a given dimension by the given scale_value.
Definition: Window.inl:155
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
Shape of a tensor.
Definition: TensorShape.h:39
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(...)
Definition: Validate.h:610
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:204
virtual DataType data_type() const =0
Data type used for each element of the tensor.
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
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
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(...)
Definition: Validate.h:288
TensorShape compute_interleaved_shape(const ITensorInfo &a, int mult_interleave4x4_height=1, bool reinterpret_input_as_3d=false)
Calculate the interleaved shape of an input tensor.
Copyright (c) 2017-2021 Arm Limited.
virtual void set_valid_region(const ValidRegion &valid_region)=0
Set the valid region of the tensor.
#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
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
Class to describe a number of elements in each dimension.
Definition: Steps.h:40
static Status validate(const ITensorInfo *input, const ITensorInfo *output)
Static function to check if given info will lead to a valid configuration of NEGEMMInterleave4x4Kerne...
Coordinates of an item.
Definition: Coordinates.h:37
void configure(const ITensor *input, ITensor *output)
Initialise the kernel&#39;s input and output.
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
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
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:941
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:235
virtual size_t total_size() const =0
Returns the total size of the tensor in bytes.
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:545
Status validate_arguments(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, const GEMMLowpOutputStageInfo *output_stage)
#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
void run(const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
void set_num_dimensions(size_t num_dimensions)
Set number of dimensions.
Definition: Dimensions.h:149
virtual const Strides & strides_in_bytes() const =0
The strides in bytes for accessing each dimension of the tensor.
Container for valid region of a window.
Definition: Types.h:188
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
TensorShape & set(size_t dimension, size_t value, bool apply_dim_correction=true, bool increase_dim_unit=true)
Accessor to set the value of one of the dimensions.
Definition: TensorShape.h:79
#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