Compute Library
 22.11
NEGenerateProposalsLayerKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2022 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"
35 #include <arm_neon.h>
36 
37 namespace arm_compute
38 {
39 namespace
40 {
41 struct ComputeAllAnchorsData
42 {
44 };
45 
46 using ComputeAllAnchorsSelectorPtr = std::add_pointer<bool(const ComputeAllAnchorsData &data)>::type;
48 
49 struct ComputeAllAnchorsKernel
50 {
51  const char *name;
52  const ComputeAllAnchorsSelectorPtr is_selected;
53  ComputeAllAnchorsUKernelPtr ukernel;
54 };
55 
56 static const ComputeAllAnchorsKernel available_kernels[] =
57 {
58 #if defined(ARM_COMPUTE_ENABLE_NEON)
59  {
60  "neon_qu16_computeallanchors",
61  [](const ComputeAllAnchorsData & data) { return data.dt == DataType::QSYMM16; },
63  },
64 #endif //defined(ARM_COMPUTE_ENABLE_NEON)
65 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
66  {
67  "neon_fp16_computeallanchors",
68  [](const ComputeAllAnchorsData & data) { return data.dt == DataType::F16; },
70  },
71 #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
72  {
73  "neon_fp32_computeallanchors",
74  [](const ComputeAllAnchorsData & data) { return data.dt == DataType::F32; },
76  },
77 };
78 
79 /** Micro-kernel selector
80  *
81  * @param[in] data Selection data passed to help pick the appropriate micro-kernel
82  *
83  * @return A matching micro-kernel else nullptr
84  */
85 const ComputeAllAnchorsKernel *get_implementation(const ComputeAllAnchorsData &data)
86 {
87  for(const auto &uk : available_kernels)
88  {
89  if(uk.is_selected(data))
90  {
91  return &uk;
92  }
93  }
94  return nullptr;
95 }
96 
97 Status validate_arguments(const ITensorInfo *anchors, const ITensorInfo *all_anchors, const ComputeAnchorsInfo &info)
98 {
99  ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(anchors, all_anchors);
101  ARM_COMPUTE_RETURN_ERROR_ON(anchors->dimension(0) != info.values_per_roi());
103  ARM_COMPUTE_RETURN_ERROR_ON(anchors->num_dimensions() > 2);
104  if(all_anchors->total_size() > 0)
105  {
106  const size_t feature_height = info.feat_height();
107  const size_t feature_width = info.feat_width();
108  const size_t num_anchors = anchors->dimension(1);
110  ARM_COMPUTE_RETURN_ERROR_ON(all_anchors->num_dimensions() > 2);
111  ARM_COMPUTE_RETURN_ERROR_ON(all_anchors->dimension(0) != info.values_per_roi());
112  ARM_COMPUTE_RETURN_ERROR_ON(all_anchors->dimension(1) != feature_height * feature_width * num_anchors);
113 
114  if(is_data_type_quantized(anchors->data_type()))
115  {
117  }
118  }
119  return Status{};
120 }
121 
122 } // namespace
123 
125  : _anchors(nullptr), _all_anchors(nullptr), _anchors_info(0.f, 0.f, 0.f)
126 {
127 }
128 
130 {
131  ARM_COMPUTE_ERROR_ON_NULLPTR(anchors, all_anchors);
132  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(anchors->info(), all_anchors->info(), info));
133 
134  // Metadata
135  const size_t num_anchors = anchors->info()->dimension(1);
136  const DataType data_type = anchors->info()->data_type();
137  const float width = info.feat_width();
138  const float height = info.feat_height();
139 
140  // Initialize the output if empty
141  const TensorShape output_shape(info.values_per_roi(), width * height * num_anchors);
142  auto_init_if_empty(*all_anchors->info(), TensorInfo(output_shape, 1, data_type, anchors->info()->quantization_info()));
143 
144  // Set instance variables
145  _anchors = anchors;
146  _all_anchors = all_anchors;
147  _anchors_info = info;
148 
149  Window win = calculate_max_window(*all_anchors->info(), Steps(info.values_per_roi()));
150 
151  INEKernel::configure(win);
152 }
153 
155 {
156  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(anchors, all_anchors, info));
157  return Status{};
158 }
159 
161 {
162  ARM_COMPUTE_UNUSED(info);
165 
166  const auto *uk = get_implementation(ComputeAllAnchorsData{ _anchors->info()->data_type() });
167  ARM_COMPUTE_ERROR_ON(uk == nullptr || uk->ukernel == nullptr);
168 
169  uk->ukernel(_anchors, _all_anchors, _anchors_info, window);
170 }
171 } // namespace arm_compute
bool is_data_type_quantized(DataType dt)
Check if a given data type is of quantized type.
Definition: Utils.h:1030
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
quantized, symmetric fixed-point 16-bit number
#define ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(tensor)
Definition: Validate.h:115
#define REGISTER_FP16_NEON(func_name)
Definition: Registrars.h:48
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:1559
const char * name
#define REGISTER_FP32_NEON(func_name)
Definition: Registrars.h:74
#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
#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
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...
void neon_qu16_computeallanchors(const ITensor *anchors, ITensor *all_anchors, ComputeAnchorsInfo anchors_info, const Window &window)
Definition: qsymm16.cpp:29
Status class.
Definition: Error.h:52
void configure(const ITensor *anchors, ITensor *all_anchors, const ComputeAnchorsInfo &info)
Set the input and output tensors.
Status validate_arguments(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *dst, const PadStrideInfo &conv_info)
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:296
decltype(strategy::transforms) typedef type
Interface for CPU tensor.
Definition: ITensor.h:36
Copyright (c) 2017-2022 Arm Limited.
1 channel, 1 F16 per channel
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:159
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
void neon_fp16_computeallanchors(const ITensor *anchors, ITensor *all_anchors, ComputeAnchorsInfo anchors_info, const Window &window)
Class to describe a number of elements in each dimension.
Definition: Steps.h:40
#define REGISTER_QSYMM16_NEON(func_name)
Definition: Registrars.h:139
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.
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:179
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:541
ComputeAnchors information class.
Definition: Types.h:1522
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:157
Store the tensor&#39;s metadata.
Definition: TensorInfo.h:43
ComputeAllAnchorsUKernelPtr ukernel
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(t,...)
Definition: Validate.h:690
DataType
Available data types.
Definition: Types.h:79
Describe a multidimensional execution window.
Definition: Window.h:39
void neon_fp32_computeallanchors(const ITensor *anchors, ITensor *all_anchors, ComputeAnchorsInfo anchors_info, const Window &window)
Definition: fp32.cpp:29
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:201
const ComputeAllAnchorsSelectorPtr is_selected