Compute Library
 21.11
NEGenerateProposalsLayerKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-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 
28 #include "arm_compute/core/Utils.h"
30 #include "src/core/CPP/Validate.h"
33 
34 #include <arm_neon.h>
35 
36 namespace arm_compute
37 {
38 namespace
39 {
40 Status validate_arguments(const ITensorInfo *anchors, const ITensorInfo *all_anchors, const ComputeAnchorsInfo &info)
41 {
42  ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(anchors, all_anchors);
44  ARM_COMPUTE_RETURN_ERROR_ON(anchors->dimension(0) != info.values_per_roi());
46  ARM_COMPUTE_RETURN_ERROR_ON(anchors->num_dimensions() > 2);
47  if(all_anchors->total_size() > 0)
48  {
49  const size_t feature_height = info.feat_height();
50  const size_t feature_width = info.feat_width();
51  const size_t num_anchors = anchors->dimension(1);
53  ARM_COMPUTE_RETURN_ERROR_ON(all_anchors->num_dimensions() > 2);
54  ARM_COMPUTE_RETURN_ERROR_ON(all_anchors->dimension(0) != info.values_per_roi());
55  ARM_COMPUTE_RETURN_ERROR_ON(all_anchors->dimension(1) != feature_height * feature_width * num_anchors);
56 
57  if(is_data_type_quantized(anchors->data_type()))
58  {
60  }
61  }
62  return Status{};
63 }
64 
65 } // namespace
66 
68  : _anchors(nullptr), _all_anchors(nullptr), _anchors_info(0.f, 0.f, 0.f)
69 {
70 }
71 
72 void NEComputeAllAnchorsKernel::configure(const ITensor *anchors, ITensor *all_anchors, const ComputeAnchorsInfo &info)
73 {
74  ARM_COMPUTE_ERROR_ON_NULLPTR(anchors, all_anchors);
75  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(anchors->info(), all_anchors->info(), info));
76 
77  // Metadata
78  const size_t num_anchors = anchors->info()->dimension(1);
79  const DataType data_type = anchors->info()->data_type();
80  const float width = info.feat_width();
81  const float height = info.feat_height();
82 
83  // Initialize the output if empty
84  const TensorShape output_shape(info.values_per_roi(), width * height * num_anchors);
85  auto_init_if_empty(*all_anchors->info(), TensorInfo(output_shape, 1, data_type, anchors->info()->quantization_info()));
86 
87  // Set instance variables
88  _anchors = anchors;
89  _all_anchors = all_anchors;
90  _anchors_info = info;
91 
92  Window win = calculate_max_window(*all_anchors->info(), Steps(info.values_per_roi()));
93 
94  INEKernel::configure(win);
95 }
96 
98 {
99  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(anchors, all_anchors, info));
100  return Status{};
101 }
102 
103 template <>
104 void NEComputeAllAnchorsKernel::internal_run<int16_t>(const Window &window)
105 {
106  Iterator all_anchors_it(_all_anchors, window);
107  Iterator anchors_it(_all_anchors, window);
108 
109  const size_t num_anchors = _anchors->info()->dimension(1);
110  const float stride = 1.f / _anchors_info.spatial_scale();
111  const size_t feat_width = _anchors_info.feat_width();
112 
114 
115  execute_window_loop(window, [&](const Coordinates & id)
116  {
117  const size_t anchor_offset = id.y() % num_anchors;
118 
119  const auto out_anchor_ptr = reinterpret_cast<int16_t *>(all_anchors_it.ptr());
120  const auto anchor_ptr = reinterpret_cast<int16_t *>(_anchors->ptr_to_element(Coordinates(0, anchor_offset)));
121 
122  const size_t shift_idy = id.y() / num_anchors;
123  const float shiftx = (shift_idy % feat_width) * stride;
124  const float shifty = (shift_idy / feat_width) * stride;
125 
126  const float new_anchor_x1 = dequantize_qsymm16(*anchor_ptr, qinfo.scale) + shiftx;
127  const float new_anchor_y1 = dequantize_qsymm16(*(1 + anchor_ptr), qinfo.scale) + shifty;
128  const float new_anchor_x2 = dequantize_qsymm16(*(2 + anchor_ptr), qinfo.scale) + shiftx;
129  const float new_anchor_y2 = dequantize_qsymm16(*(3 + anchor_ptr), qinfo.scale) + shifty;
130 
131  *out_anchor_ptr = quantize_qsymm16(new_anchor_x1, qinfo.scale);
132  *(out_anchor_ptr + 1) = quantize_qsymm16(new_anchor_y1, qinfo.scale);
133  *(out_anchor_ptr + 2) = quantize_qsymm16(new_anchor_x2, qinfo.scale);
134  *(out_anchor_ptr + 3) = quantize_qsymm16(new_anchor_y2, qinfo.scale);
135  },
136  all_anchors_it);
137 }
138 
139 template <typename T>
140 void NEComputeAllAnchorsKernel::internal_run(const Window &window)
141 {
142  Iterator all_anchors_it(_all_anchors, window);
143  Iterator anchors_it(_all_anchors, window);
144 
145  const size_t num_anchors = _anchors->info()->dimension(1);
146  const T stride = 1.f / _anchors_info.spatial_scale();
147  const size_t feat_width = _anchors_info.feat_width();
148 
149  execute_window_loop(window, [&](const Coordinates & id)
150  {
151  const size_t anchor_offset = id.y() % num_anchors;
152 
153  const auto out_anchor_ptr = reinterpret_cast<T *>(all_anchors_it.ptr());
154  const auto anchor_ptr = reinterpret_cast<T *>(_anchors->ptr_to_element(Coordinates(0, anchor_offset)));
155 
156  const size_t shift_idy = id.y() / num_anchors;
157  const T shiftx = (shift_idy % feat_width) * stride;
158  const T shifty = (shift_idy / feat_width) * stride;
159 
160  *out_anchor_ptr = *anchor_ptr + shiftx;
161  *(out_anchor_ptr + 1) = *(1 + anchor_ptr) + shifty;
162  *(out_anchor_ptr + 2) = *(2 + anchor_ptr) + shiftx;
163  *(out_anchor_ptr + 3) = *(3 + anchor_ptr) + shifty;
164  },
165  all_anchors_it);
166 }
167 
168 void NEComputeAllAnchorsKernel::run(const Window &window, const ThreadInfo &info)
169 {
170  ARM_COMPUTE_UNUSED(info);
173 
174  switch(_anchors->info()->data_type())
175  {
176  case DataType::QSYMM16:
177  {
178  internal_run<int16_t>(window);
179  break;
180  }
181 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
182  case DataType::F16:
183  {
184  internal_run<float16_t>(window);
185  break;
186  }
187 #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
188  case DataType::F32:
189  {
190  internal_run<float>(window);
191  break;
192  }
193  default:
194  {
195  ARM_COMPUTE_ERROR("Data type not supported");
196  }
197  }
198 }
199 } // namespace arm_compute
bool is_data_type_quantized(DataType dt)
Check if a given data type is of quantized type.
Definition: Utils.h:981
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
int16_t quantize_qsymm16(float value, const UniformQuantizationInfo &qinfo, RoundingPolicy rounding_policy=RoundingPolicy::TO_NEAREST_UP)
Quantize a value given a 16-bit symmetric quantization scheme.
Shape of a tensor.
Definition: TensorShape.h:39
quantized, symmetric fixed-point 16-bit number
#define ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(tensor)
Definition: Validate.h:115
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:606
size_t values_per_roi() const
Definition: Types.h:1429
#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
virtual DataType data_type() const =0
Data type used for each element of the tensor.
1 channel, 1 F32 per channel
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
Quantization info when assuming per layer quantization.
static Status validate(const ITensorInfo *anchors, const ITensorInfo *all_anchors, const ComputeAnchorsInfo &info)
Static function to check if given info will lead to a valid configuration of NEComputeAllAnchorsKerne...
Status class.
Definition: Error.h:52
void configure(const ITensor *anchors, ITensor *all_anchors, const ComputeAnchorsInfo &info)
Set the input and output tensors.
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:296
Interface for CPU tensor.
Definition: ITensor.h:36
float spatial_scale() const
Definition: Types.h:1423
Copyright (c) 2017-2021 Arm Limited.
1 channel, 1 F16 per channel
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:159
const DataType data_type
Definition: Im2Col.cpp:150
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
float dequantize_qsymm16(int16_t value, const UniformQuantizationInfo &qinfo)
Dequantize a value given a 16-bit symmetric quantization scheme.
Class to describe a number of elements in each dimension.
Definition: Steps.h:40
Coordinates of an item.
Definition: Coordinates.h:37
UniformQuantizationInfo uniform() const
Return per layer quantization info.
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 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 QuantizationInfo quantization_info() const =0
Get the quantization settings (scale and offset) of the tensor.
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:915
void run(const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
Information about executing thread and CPU.
Definition: CPPTypes.h:158
const QuantizationInfo qinfo
Definition: Im2Col.cpp:155
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:541
ComputeAnchors information class.
Definition: Types.h:1392
constexpr const Dimension & y() const
Alias to access the second dimension of the window.
Definition: Window.h:154
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:157
Store the tensor&#39;s metadata.
Definition: TensorInfo.h:43
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
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(t,...)
Definition: Validate.h:690
Iterator updated by execute_window_loop for each window element.
Definition: Helpers.h:46
DataType
Available data types.
Definition: Types.h:79
Describe a multidimensional execution window.
Definition: Window.h:39
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:201