Compute Library
 22.11
impl.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 namespace arm_compute
26 {
27 class ITensor;
28 class Window;
29 namespace cpu
30 {
31 template <typename T>
32 void compute_all_anchors(const ITensor *anchors, ITensor *all_anchors, ComputeAnchorsInfo anchors_info, const Window &window)
33 {
34  Iterator all_anchors_it(all_anchors, window);
35  Iterator anchors_it(all_anchors, window);
36 
37  const size_t num_anchors = anchors->info()->dimension(1);
38  const T stride = 1.f / anchors_info.spatial_scale();
39  const size_t feat_width = anchors_info.feat_width();
40 
41  execute_window_loop(window, [&](const Coordinates & id)
42  {
43  const size_t anchor_offset = id.y() % num_anchors;
44 
45  const auto out_anchor_ptr = reinterpret_cast<T *>(all_anchors_it.ptr());
46  const auto anchor_ptr = reinterpret_cast<T *>(anchors->ptr_to_element(Coordinates(0, anchor_offset)));
47 
48  const size_t shift_idy = id.y() / num_anchors;
49  const T shiftx = (shift_idy % feat_width) * stride;
50  const T shifty = (shift_idy / feat_width) * stride;
51 
52  *out_anchor_ptr = *anchor_ptr + shiftx;
53  *(out_anchor_ptr + 1) = *(1 + anchor_ptr) + shifty;
54  *(out_anchor_ptr + 2) = *(2 + anchor_ptr) + shiftx;
55  *(out_anchor_ptr + 3) = *(3 + anchor_ptr) + shifty;
56  },
57  all_anchors_it);
58 }
59 
60 template void compute_all_anchors<float>(const ITensor *anchors, ITensor *all_anchors, ComputeAnchorsInfo anchors_info, const Window &window);
61 #if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) && defined(ENABLE_FP16_KERNELS)
62 template void compute_all_anchors<float16_t>(const ITensor *anchors, ITensor *all_anchors, ComputeAnchorsInfo anchors_info, const Window &window);
63 #endif //defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) && defined(ENABLE_FP16_KERNELS)
64 
65 void compute_all_anchors_qasymm16(const ITensor *anchors, ITensor *all_anchors, ComputeAnchorsInfo anchors_info, const Window &window)
66 {
67  Iterator all_anchors_it(all_anchors, window);
68  Iterator anchors_it(all_anchors, window);
69 
70  const size_t num_anchors = anchors->info()->dimension(1);
71  const float stride = 1.f / anchors_info.spatial_scale();
72  const size_t feat_width = anchors_info.feat_width();
73 
75 
76  execute_window_loop(window, [&](const Coordinates & id)
77  {
78  const size_t anchor_offset = id.y() % num_anchors;
79 
80  const auto out_anchor_ptr = reinterpret_cast<int16_t *>(all_anchors_it.ptr());
81  const auto anchor_ptr = reinterpret_cast<int16_t *>(anchors->ptr_to_element(Coordinates(0, anchor_offset)));
82 
83  const size_t shift_idy = id.y() / num_anchors;
84  const float shiftx = (shift_idy % feat_width) * stride;
85  const float shifty = (shift_idy / feat_width) * stride;
86 
87  const float new_anchor_x1 = dequantize_qsymm16(*anchor_ptr, qinfo.scale) + shiftx;
88  const float new_anchor_y1 = dequantize_qsymm16(*(1 + anchor_ptr), qinfo.scale) + shifty;
89  const float new_anchor_x2 = dequantize_qsymm16(*(2 + anchor_ptr), qinfo.scale) + shiftx;
90  const float new_anchor_y2 = dequantize_qsymm16(*(3 + anchor_ptr), qinfo.scale) + shifty;
91 
92  *out_anchor_ptr = quantize_qsymm16(new_anchor_x1, qinfo.scale);
93  *(out_anchor_ptr + 1) = quantize_qsymm16(new_anchor_y1, qinfo.scale);
94  *(out_anchor_ptr + 2) = quantize_qsymm16(new_anchor_x2, qinfo.scale);
95  *(out_anchor_ptr + 3) = quantize_qsymm16(new_anchor_y2, qinfo.scale);
96  },
97  all_anchors_it);
98 }
99 } // namespace cpu
100 } // namespace arm_compute
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.
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
Quantization info when assuming per layer quantization.
Interface for CPU tensor.
Definition: ITensor.h:36
float spatial_scale() const
Definition: Types.h:1553
Copyright (c) 2017-2022 Arm Limited.
float dequantize_qsymm16(int16_t value, const UniformQuantizationInfo &qinfo)
Dequantize a value given a 16-bit symmetric quantization scheme.
Coordinates of an item.
Definition: Coordinates.h:37
UniformQuantizationInfo uniform() const
Return per layer quantization info.
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
void compute_all_anchors(const ITensor *anchors, ITensor *all_anchors, ComputeAnchorsInfo anchors_info, const Window &window)
Definition: impl.cpp:32
virtual QuantizationInfo quantization_info() const =0
Get the quantization settings (scale and offset) of the tensor.
const QuantizationInfo qinfo
Definition: Im2Col.cpp:155
ComputeAnchors information class.
Definition: Types.h:1522
constexpr const Dimension & y() const
Alias to access the second dimension of the window.
Definition: Window.h:168
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
template void compute_all_anchors< float >(const ITensor *anchors, ITensor *all_anchors, ComputeAnchorsInfo anchors_info, const Window &window)
void compute_all_anchors_qasymm16(const ITensor *anchors, ITensor *all_anchors, ComputeAnchorsInfo anchors_info, const Window &window)
Definition: impl.cpp:65
Iterator updated by execute_window_loop for each window element.
Definition: Helpers.h:46
Describe a multidimensional execution window.
Definition: Window.h:39