Compute Library
 20.08
NEGEMMTranspose1xWKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-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 
32 
33 #include <arm_neon.h>
34 
35 namespace arm_compute
36 {
37 namespace
38 {
39 TensorShape get_output_shape(const ITensorInfo *input)
40 {
41  TensorShape output_shape{ input->tensor_shape() };
42  const size_t transpose_w = 16 / input->element_size();
43  output_shape.set(0, input->dimension(1) * transpose_w);
44  output_shape.set(1, static_cast<size_t>(std::ceil((input->dimension(0) / static_cast<float>(transpose_w)))));
45  return output_shape;
46 }
47 
48 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output)
49 {
52  //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  {
56  ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), get_output_shape(input));
59  }
60 
61  return Status{};
62 }
63 
64 std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
65 {
66  const unsigned int num_elems_processed_per_iteration = 16 / input->element_size();
67 
68  // Configure kernel window
70 
71  AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
72 
73  // Configure window in case of configured output
74  if(output->total_size() != 0)
75  {
76  AccessWindowStatic output_access(output, 0, 0, output->dimension(0), output->dimension(1));
77  output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
78  }
79 
80  const bool window_changed = update_window_and_padding(win, input_access);
81 
82  Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
83  return std::make_pair(err, win);
84 }
85 } // namespace
86 
88 {
90 
91  // Output tensor auto inizialitation if not yet initialized
92  auto_init_if_empty(*output->info(), get_output_shape(input->info()), 1, input->info()->data_type());
93 
94  // Perform validate step
96 
97  _input = input;
98  _output = output;
99 
100  // Configure kernel window
101  auto win_config = validate_and_configure_window(input->info(), output->info());
102  ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
103  INEKernel::configure(win_config.second);
104 }
105 
107 {
109  ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get()).first);
110 
111  return Status{};
112 }
113 
115 {
119 
120  /*
121  * Following an example of how the transposition1xW works when the input data type is F32
122  *
123  * |a00 a01 a02 a03|
124  * |a10 a11 a12 a13|
125  * |a20 a21 a22 a23| = | a00 a01 a02 a03 || a10 a11 a12 a13 || a20 a21 a22 a23 || a30 a31 a32 a33 |
126  * |a30 a31 a32 a33|
127  *
128  * The output matrix will have the following shape: [ height * W, ceil(width / W) ], where W = (16 / element size of the tensor)
129  */
130 
131  // Set window for output tensor. Set to 0 the X and Y dimensions in order to allow multi-threading implementation and future batched matrix multiplications
132  Window win_out(window);
133  win_out.set(Window::DimX, Window::Dimension(0, 0, 0));
134  win_out.set(Window::DimY, Window::Dimension(0, 0, 0));
135 
136  Iterator in(_input, window);
137  Iterator out(_output, win_out);
138 
139  switch(_input->info()->element_size())
140  {
141  case 1:
142  {
143  const size_t out_stride = _output->info()->strides_in_bytes()[1];
144  execute_window_loop(window, [&](const Coordinates & id)
145  {
146  // Output address = base addr + (y * 16) + (x / 16 ) * stride
147  const uint8_t *in_ptr = in.ptr();
148  uint8_t *const out_ptr = out.ptr() + (id.y() << 4) + (id.x() >> 4) * out_stride;
149  vst1q_u8(out_ptr, vld1q_u8(in_ptr));
150  },
151  in, out);
152  break;
153  }
154  case 2:
155  {
156  const size_t out_stride = _output->info()->strides_in_bytes()[1] / sizeof(int16_t);
157  execute_window_loop(window, [&](const Coordinates & id)
158  {
159  // Output address = base addr + (y * 8) + (x / 8 ) * stride
160  const auto in_ptr = reinterpret_cast<const uint16_t *>(in.ptr());
161  const auto out_ptr = reinterpret_cast<uint16_t *>(out.ptr()) + (id.y() << 3) + (id.x() >> 3) * out_stride;
162  vst1q_u16(out_ptr, vld1q_u16(in_ptr));
163  },
164  in, out);
165  break;
166  }
167  case 4:
168  {
169  const size_t out_stride = _output->info()->strides_in_bytes()[1] / sizeof(float);
170  execute_window_loop(window, [&](const Coordinates & id)
171  {
172  // Output address = base addr + (y * 4) + (x / 4 ) * stride
173  const auto in_ptr = reinterpret_cast<const uint32_t *>(in.ptr());
174  const auto out_ptr = reinterpret_cast<uint32_t *>(out.ptr()) + (id.y() << 2) + (id.x() >> 2) * out_stride;
175  vst1q_u32(out_ptr, vld1q_u32(in_ptr));
176  },
177  in, out);
178  break;
179  }
180  default:
181  {
182  ARM_COMPUTE_ERROR("Element size not supported");
183  break;
184  }
185  }
186 }
187 } // namespace arm_compute
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
void run(const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:545
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:204
Store the tensor's metadata.
Definition: ITensorInfo.h:40
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
Describe one of the image's dimensions with a start, end and step.
Definition: Window.h:75
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
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
static Status validate(const ITensorInfo *input, const ITensorInfo *output)
Static function to check if given info will lead to a valid configuration of NEGEMMTranspose1xWKernel...
Copyright (c) 2017-2020 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:207
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:437
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(...)
Definition: Validate.h:288
Coordinates of an item.
Definition: Coordinates.h:37
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.
constexpr uint8_t * ptr() const
Return a pointer to the current pixel.
Definition: Helpers.inl:190
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:49
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(...)
Definition: Validate.h:610
void configure(const ITensor *input, ITensor *output)
Initialise the kernel's input and output.
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:163
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
Information about executing thread and CPU.
Definition: CPPTypes.h:235
#define ARM_COMPUTE_CREATE_ERROR(error_code, msg)
Creates an error with a given message.
Definition: Error.h:159
Status validate_arguments(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, const GEMMLowpOutputStageInfo *output_stage)
unsigned int num_elems_processed_per_iteration
TensorShape & set(size_t dimension, size_t value, bool apply_dim_correction=true)
Accessor to set the value of one of the dimensions.
Definition: TensorShape.h:78
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:128
Iterator updated by execute_window_loop for each window element.
Definition: Helpers.h:353
#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:941