Compute Library
 21.02
CpuConcatenateDepthKernel.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 
26 #include "arm_compute/core/Error.h"
31 #include "arm_compute/core/Utils.h"
34 #include "src/core/NEON/NEAsymm.h"
39 
40 #include <cstdint>
41 
42 namespace arm_compute
43 {
44 namespace cpu
45 {
46 namespace kernels
47 {
48 namespace
49 {
50 template <typename T>
51 void depth_concat(const ITensor *src, ITensor *dst, unsigned int depth_offset, const Window &window)
52 {
53  // Offset source
54  uint8_t *src_ptr = src->buffer() + src->info()->offset_first_element_in_bytes();
55 
56  // Offset destination
57  uint8_t *dst_ptr = dst->buffer() + dst->info()->offset_first_element_in_bytes() + depth_offset * dst->info()->strides_in_bytes()[2];
58 
59  const auto window_start_x = static_cast<int>(window.x().start());
60  const auto window_end_x = static_cast<int>(window.x().end());
61  const int window_step_x = 16 / dst->info()->element_size();
62 
63  Window win{ window };
64  win.set(Window::DimX, Window::Dimension(0, 1, 1));
65  win.set(Window::DimZ, Window::Dimension(0, src->info()->tensor_shape().z(), 1));
66 
67  Iterator src_it(src, win);
68  Iterator dst_it(dst, win);
69 
70  const DataType dt = src->info()->data_type();
71  const UniformQuantizationInfo src_qinfo = src->info()->quantization_info().uniform();
72  const UniformQuantizationInfo dst_qinfo = dst->info()->quantization_info().uniform();
73  if(dt == DataType::QASYMM8 && src_qinfo != dst_qinfo)
74  {
75  execute_window_loop(win, [&](const Coordinates &)
76  {
77  const auto in_ptr = reinterpret_cast<const uint8_t *>(src_ptr + src_it.offset());
78  const auto out_ptr = reinterpret_cast<uint8_t *>(dst_ptr + dst_it.offset());
79  int x = window_start_x;
80  for(; x <= (window_end_x - window_step_x); x += window_step_x)
81  {
82  wrapper::vstore(out_ptr + x, vquantize(vdequantize(wrapper::vloadq(in_ptr + x), src_qinfo), dst_qinfo));
83  }
84 
85  // Compute left-over elements
86  for(; x < window_end_x; ++x)
87  {
88  *(out_ptr + x) = quantize_qasymm8(dequantize_qasymm8(*(in_ptr + x), src_qinfo), dst_qinfo);
89  }
90  },
91  src_it, dst_it);
92  }
93  else if(dt == DataType::QASYMM8_SIGNED && src_qinfo != dst_qinfo)
94  {
95  execute_window_loop(win, [&](const Coordinates &)
96  {
97  const auto in_ptr = reinterpret_cast<const int8_t *>(src_ptr + src_it.offset());
98  const auto out_ptr = reinterpret_cast<int8_t *>(dst_ptr + dst_it.offset());
99  int x = window_start_x;
100  for(; x <= (window_end_x - window_step_x); x += window_step_x)
101  {
102  wrapper::vstore(out_ptr + x, vquantize_signed(vdequantize(wrapper::vloadq(in_ptr + x), src_qinfo), dst_qinfo));
103  }
104 
105  // Compute left-over elements
106  for(; x < window_end_x; ++x)
107  {
108  *(out_ptr + x) = quantize_qasymm8_signed(dequantize_qasymm8_signed(*(in_ptr + x), src_qinfo), dst_qinfo);
109  }
110  },
111  src_it, dst_it);
112  }
113  else
114  {
115  execute_window_loop(win, [&](const Coordinates &)
116  {
117  const auto in_ptr = reinterpret_cast<const T *>(src_ptr + src_it.offset());
118  const auto out_ptr = reinterpret_cast<T *>(dst_ptr + dst_it.offset());
119  int x = window_start_x;
120  for(; x <= (window_end_x - window_step_x); x += window_step_x)
121  {
122  wrapper::vstore(out_ptr + x, wrapper::vloadq(in_ptr + x));
123  }
124  // Compute left-over elements
125  for(; x < window_end_x; ++x)
126  {
127  *(out_ptr + x) = *(in_ptr + x);
128  }
129  },
130  src_it, dst_it);
131  }
132 }
133 
134 Status validate_arguments(const ITensorInfo *input, unsigned int depth_offset, const ITensorInfo *output)
135 {
137  //Note: ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input) is not needed here as this kernel doesn't use Neon FP16 instructions.
140 
141  ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(Window::DimX) != output->dimension(Window::DimX));
142  ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(Window::DimY) != output->dimension(Window::DimY));
143  ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(2) + depth_offset > output->dimension(2));
145 
146  return Status{};
147 }
148 } // namespace
149 
151  : _func(nullptr), _depth_offset(0)
152 {
153 }
154 
155 void CpuConcatenateDepthKernel::configure(const ITensorInfo *src, unsigned int depth_offset, ITensorInfo *dst)
156 {
158  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, depth_offset, dst));
159 
160  _func = nullptr;
161  _depth_offset = depth_offset;
162 
163  switch(src->data_type())
164  {
165  case DataType::QASYMM8:
166  _func = &depth_concat<uint8_t>;
167  break;
169  _func = &depth_concat<int8_t>;
170  break;
171  case DataType::F16:
172  _func = &depth_concat<uint16_t>;
173  break;
174  case DataType::F32:
175  _func = &depth_concat<uint32_t>;
176  break;
177  default:
178  ARM_COMPUTE_ERROR("Unsupported data type.");
179  }
180 
181  // Configure kernel window
182  Window win = calculate_max_window(*dst, Steps());
183  Coordinates coord;
184  coord.set_num_dimensions(dst->num_dimensions());
185 
186  dst->set_valid_region(ValidRegion(coord, dst->tensor_shape()));
187  ICpuKernel::configure(win);
188 }
189 
191  unsigned int depth_offset,
192  const arm_compute::ITensorInfo *dst)
193 {
194  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, depth_offset, dst));
195  return Status{};
196 }
197 
199 {
200  ARM_COMPUTE_UNUSED(info);
203  ARM_COMPUTE_ERROR_ON(_func == nullptr);
204 
205  (*_func)(tensors.get_const_tensor(TensorType::ACL_SRC),
207  _depth_offset,
208  window);
209 }
210 
212 {
213  return "CpuConcatenateDepthKernel";
214 }
215 } // namespace kernels
216 } // namespace cpu
217 } // namespace arm_compute
virtual size_t num_dimensions() const =0
The number of dimensions of the tensor (rank)
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
void run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
float dequantize_qasymm8(uint8_t value, const INFO_TYPE &qinfo)
Dequantize a value given an unsigned 8-bit asymmetric quantization scheme.
const char * name() const override
Name of the kernel.
uint8_t quantize_qasymm8(float value, const INFO_TYPE &qinfo, RoundingPolicy rounding_policy=RoundingPolicy::TO_NEAREST_UP)
Quantize a value given an unsigned 8-bit asymmetric quantization scheme.
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
float32x4x2_t vdequantize(const uint8x8_t &qv, const UniformQuantizationInfo &qi)
Dequantize a neon vector holding 8 quantized values.
Definition: NEAsymm.h:415
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
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
Status class.
Definition: Error.h:52
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:296
SimpleTensor< float > src
Definition: DFT.cpp:155
Copyright (c) 2017-2021 Arm Limited.
virtual void set_valid_region(const ValidRegion &valid_region)=0
Set the valid region of the tensor.
1 channel, 1 F16 per channel
DataType dt
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:163
void configure(const ITensorInfo *src, unsigned int depth_offset, ITensorInfo *dst)
Configure kernel for a given list of arguments.
const ITensor * get_const_tensor(int id) const
Get constant tensor of a given id.
Definition: ITensorPack.cpp:40
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
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
int8_t quantize_qasymm8_signed(float value, const INFO_TYPE &qinfo, RoundingPolicy rounding_policy=RoundingPolicy::TO_NEAREST_UP)
Quantize a value given a signed 8-bit asymmetric quantization scheme.
quantized, asymmetric fixed-point 8-bit number unsigned
Class to describe a number of elements in each dimension.
Definition: Steps.h:40
Coordinates of an item.
Definition: Coordinates.h:37
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:941
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)
ITensor * get_tensor(int id)
Get tensor of a given id from the pac.
Definition: ITensorPack.cpp:50
Information about executing thread and CPU.
Definition: CPPTypes.h:235
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(...)
Definition: Validate.h:443
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
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:792
Status validate_arguments(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, const GEMMLowpOutputStageInfo *output_stage)
uint8x8_t vquantize(const float32x4x2_t &qv, const UniformQuantizationInfo &qi)
Quantize a neon vector holding 8 floating point values.
Definition: NEAsymm.h:602
void vstore(uint8_t *ptr, uint8x8_t val)
Definition: store.h:39
float dequantize_qasymm8_signed(int8_t value, const INFO_TYPE &qinfo)
Dequantize a value given a signed 8-bit asymmetric quantization scheme.
Tensor packing service.
Definition: ITensorPack.h:37
#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
void set_num_dimensions(size_t num_dimensions)
Set number of dimensions.
Definition: Dimensions.h:149
quantized, asymmetric fixed-point 8-bit number signed
Includes all wrapper headers at once.
int8x8_t vquantize_signed(const float32x4x2_t &qv, const UniformQuantizationInfo &qi)
Quantize a neon vector holding 8 floating point values.
Definition: NEAsymm.h:630
static Status validate(const ITensorInfo *src, unsigned int depth_offset, const ITensorInfo *dst)
Static function to check if given info will lead to a valid configuration of CpuConcatenateDepthKerne...
Container for valid region of a window.
Definition: Types.h:188
DataType
Available data types.
Definition: Types.h:77
Describe a multidimensional execution window.
Definition: Window.h:39
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205