Compute Library
 22.08
CpuConvertQuantizedSignednessKernel.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 
26 #include "arm_compute/core/Error.h"
35 
36 namespace arm_compute
37 {
38 namespace cpu
39 {
40 namespace kernels
41 {
42 namespace
43 {
44 Status validate_arguments(const ITensorInfo *src, const ITensorInfo *dst)
45 {
48 
49  // Validate output if initialized
50  if(dst->total_size() != 0)
51  {
53  ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(src->tensor_shape(), dst->tensor_shape());
54  }
55 
56  return Status{};
57 }
58 
59 std::pair<Status, Window> validate_and_configure_window(const ITensorInfo *src, ITensorInfo *dst)
60 {
61  // Output auto inizialitation if not yet initialized
62  {
63  const bool is_input_signed = src->data_type() == DataType::QASYMM8_SIGNED;
64  const DataType dt = is_input_signed ? DataType::QASYMM8 : DataType::QASYMM8_SIGNED;
65  const UniformQuantizationInfo qinfo = src->quantization_info().uniform();
66  const int offset_correction = is_input_signed ? -128 : 128;
67  const QuantizationInfo corrected_qinfo = QuantizationInfo(qinfo.scale, qinfo.offset + offset_correction);
68 
69  auto_init_if_empty(*dst, src->clone()->set_data_type(dt).set_quantization_info(corrected_qinfo));
70  }
71 
72  return std::make_pair(Status{}, calculate_max_window(*dst));
73 }
74 } // namespace
75 
77 {
80 
81  std::pair<Status, Window> win_config = validate_and_configure_window(src, dst);
82  ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
83  ICpuKernel::configure(win_config.second);
84 }
85 
87 {
89  return Status{};
90 }
91 
93 {
94  auto src = tensors.get_const_tensor(TensorType::ACL_SRC);
95  auto dst = tensors.get_tensor(TensorType::ACL_DST);
96  ARM_COMPUTE_UNUSED(info);
99 
100  Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
101  win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
102 
103  Iterator input(src, win_collapsed);
104  Iterator output(dst, win_collapsed);
105 
106  const int window_step_x = 16;
107  const auto window_start_x = static_cast<int>(window.x().start());
108  const auto window_end_x = static_cast<int>(window.x().end());
109 
110  const uint8_t mask = 128;
111  const auto vmask = wrapper::vdup_n(mask, wrapper::traits::vector_128_tag{});
112 
113  execute_window_loop(win_collapsed, [&](const Coordinates &)
114  {
115  const auto input_ptr = reinterpret_cast<const uint8_t *>(input.ptr());
116  const auto output_ptr = reinterpret_cast<uint8_t *>(output.ptr());
117 
118  // Compute S elements per iteration
119  int x = window_start_x;
120  for(; x <= (window_end_x - window_step_x); x += window_step_x)
121  {
122  const auto vin = wrapper::vloadq(input_ptr + x);
123  wrapper::vstore(output_ptr + x, wrapper::veor(vin, vmask));
124  }
125 
126  // Compute left-over elements
127  for(; x < window_end_x; ++x)
128  {
129  const uint8_t in = *(reinterpret_cast<const uint8_t *>(input_ptr + x));
130  *(output_ptr + x) = in ^ mask;
131  }
132  },
133  input, output);
134 }
135 
137 {
138  return "CpuConvertQuantizedSignednessKernel";
139 }
140 } // namespace kernels
141 } // namespace cpu
142 } // namespace arm_compute
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
static Status validate(const ITensorInfo *src, const ITensorInfo *dst)
Static function to check if given info will lead to a valid configuration.
uint8x16_t vloadq(const uint8_t *ptr)
Definition: load.h:58
#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
Describe one of the image&#39;s dimensions with a start, end and step.
Definition: Window.h:79
Status class.
Definition: Error.h:52
Status validate_arguments(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *dst, const PadStrideInfo &conv_info)
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(...)
Definition: Validate.h:284
SimpleTensor< float > src
Definition: DFT.cpp:155
Copyright (c) 2017-2022 Arm Limited.
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:159
const ITensor * get_const_tensor(int id) const
Get constant tensor of a given id.
Definition: ITensorPack.cpp:54
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
Window collapse_if_possible(const Window &full_window, size_t first, size_t last, bool *has_collapsed=nullptr) const
Collapse the dimensions between first and last if possible.
Definition: Window.inl:68
quantized, asymmetric fixed-point 8-bit number unsigned
Coordinates of an item.
Definition: Coordinates.h:37
uint8x8_t veor(const uint8x8_t &a, const uint8x8_t &b)
Definition: eor.h:39
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...
constexpr uint8_t * ptr() const
Return a pointer to the current pixel.
Definition: Helpers.inl:139
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:49
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:915
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
std::pair< Status, Window > validate_and_configure_window(ITensorInfo *src, ITensorInfo *dst)
ITensor * get_tensor(int id)
Get tensor of a given id from the pac.
Definition: ITensorPack.cpp:64
Information about executing thread and CPU.
Definition: CPPTypes.h:179
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
const QuantizationInfo qinfo
Definition: Im2Col.cpp:155
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:788
void run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
void vstore(uint8_t *ptr, uint8x8_t val)
Definition: store.h:39
Tensor packing service.
Definition: ITensorPack.h:39
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:157
uint8x8_t vdup_n(uint8_t value, traits::vector_64_tag)
Definition: dup_n.h:41
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
quantized, asymmetric fixed-point 8-bit number signed
Includes all wrapper headers at once.
constexpr int end() const
Return the end of the dimension.
Definition: Window.h:102
Iterator updated by execute_window_loop for each window element.
Definition: Helpers.h:46
DataType
Available data types.
Definition: Types.h:79
constexpr int start() const
Return the start of the dimension.
Definition: Window.h:97
Describe a multidimensional execution window.
Definition: Window.h:39
void configure(const ITensorInfo *src, ITensorInfo *dst)
Initialize the kernel input and output info.
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:201
constexpr const Dimension & x() const
Alias to access the first dimension of the window.
Definition: Window.h:159