Compute Library
 21.02
NEWeightsReshapeKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-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 
30 
31 namespace arm_compute
32 {
33 namespace
34 {
35 TensorShape get_output_shape(const ITensorInfo *input, bool has_bias)
36 {
37  TensorShape output_shape{ input->tensor_shape() };
38 
40  const size_t tmp_dim = output_shape[0];
42  output_shape.set(1, tmp_dim + (has_bias ? 1 : 0));
43 
44  return output_shape;
45 }
46 
47 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *biases, const ITensorInfo *output)
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.
51  ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
52 
53  if(biases != nullptr)
54  {
57  ARM_COMPUTE_RETURN_ERROR_ON((input->num_dimensions() == 4) && (biases->num_dimensions() != 1));
58  ARM_COMPUTE_RETURN_ERROR_ON((input->num_dimensions() == 5) && (biases->num_dimensions() != 2));
59  ARM_COMPUTE_RETURN_ERROR_ON((input->num_dimensions() == 4) && (biases->dimension(0) != input->tensor_shape()[3]));
60  ARM_COMPUTE_RETURN_ERROR_ON((input->num_dimensions() == 5) && (biases->dimension(0) != input->tensor_shape()[3] || biases->dimension(1) != input->tensor_shape()[4]));
61  }
62 
63  // Checks performed when output is configured
64  if(output->total_size() != 0)
65  {
66  ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), get_output_shape(input, biases != nullptr));
69  }
70 
71  return Status{};
72 }
73 
74 std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
75 {
76  Window window = calculate_max_window(*input, Steps());
77  window.set(Window::DimX, Window::Dimension(0, input->dimension(0), input->dimension(0)));
78  window.set(Window::DimY, Window::Dimension(0, input->dimension(1), input->dimension(1)));
79  window.set(Window::DimZ, Window::Dimension(0, input->dimension(2), input->dimension(2)));
80 
81  // The NEConvolutionLayerWeightsReshapeKernel doesn't need padding so update_window_and_padding() can be skipped
82  output->set_valid_region(ValidRegion(Coordinates(), output->tensor_shape()));
83 
84  return std::make_pair(Status{}, window);
85 }
86 } // namespace
87 
89  : _input(nullptr), _bias(nullptr), _output(nullptr)
90 {
91 }
92 
93 void NEWeightsReshapeKernel::configure(const ITensor *input, const ITensor *bias, ITensor *output)
94 {
95  ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
96 
97  // Output tensor auto inizialitation if not yet initialized
98  auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(get_output_shape(input->info(), (bias != nullptr))));
99 
100  // Perform validation step
102  (bias != nullptr) ? bias->info() : nullptr,
103  output->info()));
104 
105  _input = input;
106  _bias = bias;
107  _output = output;
108 
109  // Configure kernel
110  auto win_config = validate_and_configure_window(input->info(), output->info());
111  ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
112  INEKernel::configure(win_config.second);
113 }
114 
115 Status NEWeightsReshapeKernel::validate(const ITensorInfo *input, const ITensorInfo *biases, const ITensorInfo *output)
116 {
117  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, biases, output));
118  ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get()).first);
119 
120  return Status{};
121 }
122 
124 {
125  ARM_COMPUTE_UNUSED(info);
128 
129  const unsigned int kernel_size_x = _input->info()->dimension(0);
130  const unsigned int kernel_size_y = _input->info()->dimension(1);
131  const unsigned int kernel_depth = _input->info()->dimension(2);
132  const unsigned int input_stride_x = _input->info()->strides_in_bytes().x();
133  const unsigned int input_stride_y = _input->info()->strides_in_bytes().y();
134  const unsigned int input_stride_z = _input->info()->strides_in_bytes().z();
135  const unsigned int output_stride_y = _output->info()->strides_in_bytes().y();
136 
137  // Create iterators
138  Iterator in(_input, window);
139  execute_window_loop(window, [&](const Coordinates & id)
140  {
141  // Get column index
142  const int kernel_idx = id[3];
143  const int kernel_idz = id[4];
144 
145  // Setup pointers
146  const uint8_t *tmp_input_ptr = in.ptr();
147  uint8_t *tmp_output_ptr = _output->ptr_to_element(Coordinates(kernel_idx, 0, kernel_idz));
148  const uint8_t *curr_input_row_ptr = tmp_input_ptr;
149  const uint8_t *curr_input_depth_ptr = tmp_input_ptr;
150 
151  // Linearize volume
152  for(unsigned int d = 0; d < kernel_depth; ++d)
153  {
154  for(unsigned int j = 0; j < kernel_size_y; ++j)
155  {
156  for(unsigned int i = 0; i < kernel_size_x; ++i)
157  {
158  std::memcpy(tmp_output_ptr, tmp_input_ptr, _input->info()->element_size());
159  tmp_input_ptr += input_stride_x;
160  tmp_output_ptr += output_stride_y;
161  }
162  curr_input_row_ptr += input_stride_y;
163  tmp_input_ptr = curr_input_row_ptr;
164  }
165  curr_input_depth_ptr += input_stride_z;
166  curr_input_row_ptr = curr_input_depth_ptr;
167  tmp_input_ptr = curr_input_depth_ptr;
168  }
169 
170  // Add bias
171  if(_bias != nullptr)
172  {
173  std::memcpy(tmp_output_ptr, _bias->ptr_to_element(Coordinates(kernel_idx, kernel_idz)), _input->info()->element_size());
174  }
175  },
176  in);
177 }
178 } // namespace arm_compute
void configure(const ITensor *input, const ITensor *bias, ITensor *output)
Set the input and output of the kernel.
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.
#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
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
Status class.
Definition: Error.h:52
void run(const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
#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
Copyright (c) 2017-2021 Arm Limited.
static Status validate(const ITensorInfo *input, const ITensorInfo *biases, const ITensorInfo *output)
Static function to check if given info will lead to a valid configuration of NEWeightsReshapeKernel.
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:163
T x() const
Alias to access the size of the first dimension.
Definition: Dimensions.h:87
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
T z() const
Alias to access the size of the third dimension.
Definition: Dimensions.h:97
Coordinates of an item.
Definition: Coordinates.h:37
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()
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:941
bool is_data_type_quantized_asymmetric(DataType dt)
Check if a given data type is of asymmetric quantized type.
Definition: Utils.h:1190
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
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
#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
T y() const
Alias to access the size of the second dimension.
Definition: Dimensions.h:92
virtual const Strides & strides_in_bytes() const =0
The strides in bytes for accessing each dimension of the tensor.
Iterator updated by execute_window_loop for each window element.
Definition: Helpers.h:46
Describe a multidimensional execution window.
Definition: Window.h:39
void collapse(size_t n, size_t first=0)
Collapse the first n dimensions.
Definition: TensorShape.h:133
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