Compute Library
 22.11
CpuPool3dKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 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 "src/core/CPP/Validate.h"
33 
34 namespace arm_compute
35 {
36 namespace cpu
37 {
38 namespace kernels
39 {
40 namespace
41 {
42 using namespace misc::shape_calculator;
43 
44 static const std::vector<CpuPool3dKernel::Pooling3dKernel> available_kernels =
45 {
46  {
47  "neon_qu8_ndhwc_poolMxNxD",
48  [](const DataTypeISASelectorData & data) { return (data.dt == DataType::QASYMM8); },
50  },
51  {
52  "neon_qs8_ndhwc_poolMxNxD",
53  [](const DataTypeISASelectorData & data) { return (data.dt == DataType::QASYMM8_SIGNED); },
55  },
56  {
57  "neon_fp16_ndhwc_poolMxNxD",
58  [](const DataTypeISASelectorData & data) { return (data.dt == DataType::F16 && data.isa.fp16); },
60  },
61  {
62  "neon_fp32_ndhwc_poolMxNxD",
63  [](const DataTypeISASelectorData & data) { return (data.dt == DataType::F32); },
65  }
66 };
67 
68 Status validate_arguments(const ITensorInfo *src, const ITensorInfo *dst, const Pooling3dLayerInfo &pool_info)
69 {
71  ARM_COMPUTE_RETURN_ERROR_ON_MSG(src->data_layout() != DataLayout::NDHWC, "Only NDHWC layout supported");
74 
75  ARM_COMPUTE_RETURN_ERROR_ON_MSG((!is_data_type_float(src->data_type())) && (!pool_info.exclude_padding
76  && (pool_info.pool_type == PoolingType::AVG)),
77  "Exclude padding is unsupported for non-float types for Avg op");
78 
79  const auto data_layout = src->data_layout();
82  const int idx_depth = get_data_layout_dimension_index(data_layout, DataLayoutDimension::DEPTH);
83 
84  const bool is_global_pooling = pool_info.is_global_pooling;
85  const unsigned int pool_size_x = is_global_pooling ? src->dimension(idx_width) : pool_info.pool_size.width;
86  const unsigned int pool_size_y = is_global_pooling ? src->dimension(idx_height) : pool_info.pool_size.height;
87  const unsigned int pool_size_z = is_global_pooling ? src->dimension(idx_depth) : pool_info.pool_size.depth;
88 
89  const unsigned int stride_x = pool_info.stride.x();
90  const unsigned int stride_y = pool_info.stride.y();
91  const unsigned int stride_z = pool_info.stride.z();
92 
93  ARM_COMPUTE_RETURN_ERROR_ON((pool_size_x == 0) || (pool_size_y == 0) || (pool_size_z == 0));
94  ARM_COMPUTE_RETURN_ERROR_ON((stride_x == 0) || (stride_y == 0) || (stride_z == 0));
95 
96  int output_width = 0;
97  int output_height = 0;
98  int output_depth = 0;
99 
100  ARM_COMPUTE_RETURN_ERROR_ON_MSG(is_pool_3d_region_entirely_outside_input(pool_info), "Pooling region that is entirely outside input tensor is unsupported");
101 
102  std::tie(output_width, output_height, output_depth) = scaled_3d_dimensions_signed(src->tensor_shape()[idx_width], src->tensor_shape()[idx_height], src->tensor_shape()[idx_depth],
103  pool_size_x, pool_size_y, pool_size_z, pool_info);
104  ARM_COMPUTE_RETURN_ERROR_ON_MSG((output_width < 1 || output_height < 1 || output_depth < 1), "Calculated output dimension size is invalid");
105 
106  if(dst->total_size() != 0)
107  {
110  TensorInfo out_info(TensorInfo(compute_pool3d_shape(src->tensor_shape(), pool_info), 1, dst->data_type(), DataLayout::NDHWC));
112  }
113 
114  const auto *uk = CpuPool3dKernel::get_implementation(DataTypeISASelectorData{ src->data_type(), CPUInfo::get().get_isa() });
115  ARM_COMPUTE_RETURN_ERROR_ON(uk == nullptr || uk->ukernel == nullptr);
116 
117  return Status{};
118 }
119 } //namespace
120 
122 {
124 
125  // Perform validation step
126  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, dst, pool_info));
127 
128  // dst auto inizialitation if not yet initialized
129  auto_init_if_empty(*dst, src->clone()->set_tensor_shape(compute_pool3d_shape(src->tensor_shape(), pool_info)));
130 
131  // Get data layout
132  const auto data_layout = src->data_layout();
133  const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
134  const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
135  const int idx_depth = get_data_layout_dimension_index(data_layout, DataLayoutDimension::DEPTH);
136 
137  // Update pool size in case of global pooling
138  const bool is_global_pooling = pool_info.is_global_pooling;
139  const Size3D pool_size(
140  is_global_pooling ? src->dimension(idx_width) : pool_info.pool_size.width,
141  is_global_pooling ? src->dimension(idx_height) : pool_info.pool_size.height,
142  is_global_pooling ? src->dimension(idx_depth) : pool_info.pool_size.depth);
143 
145  ARM_COMPUTE_ERROR_ON(uk == nullptr);
146 
147  // Set instance variables
148  _pool_info = pool_info;
149  _run_method = uk->ukernel;
150  _name = std::string("CpuPool3dKernel").append("/").append(uk->name);
151 
152  // Configure kernel window
153  Window win = calculate_max_window(*dst, Steps());
154  ICpuKernel::configure(win);
155 }
156 
158 {
160 
161  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, dst, pool_info));
162 
163  return Status{};
164 }
165 
166 void CpuPool3dKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
167 {
168  ARM_COMPUTE_UNUSED(info);
171  ARM_COMPUTE_ERROR_ON(_run_method == nullptr);
172 
173  const ITensor *src = tensors.get_const_tensor(TensorType::ACL_SRC_0);
174  ITensor *dst = tensors.get_tensor(TensorType::ACL_DST_0);
175 
176  _run_method(src, dst, _pool_info, window);
177 }
178 
179 const char *CpuPool3dKernel::name() const
180 {
181  return _name.c_str();
182 }
183 
184 const std::vector<CpuPool3dKernel::Pooling3dKernel> &CpuPool3dKernel::get_available_kernels()
185 {
186  return available_kernels;
187 }
188 
189 } // namespace kernels
190 } // namespace cpu
191 } // namespace arm_compute
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
bool is_pool_3d_region_entirely_outside_input(const Pooling3dLayerInfo &info)
Check if the 3d pool region is entirely outside the input tensor.
Definition: Utils.cpp:249
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
static const auto * get_implementation(const SelectorType &selector, KernelSelectionType selection_type=KernelSelectionType::Supported)
Micro-kernel selector.
Definition: ICpuKernel.h:53
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(...)
Definition: Validate.h:490
#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.
const char * name() const override
Name of the kernel.
#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.
Class for specifying the size of a 3D shape or object.
Definition: Size3D.h:32
void neon_q8_pool3d(const ITensor *src0, ITensor *dst0, Pooling3dLayerInfo &, const Window &window)
Definition: qasymm8.cpp:29
1 channel, 1 F32 per channel
void neon_fp32_pool3d(const ITensor *src0, ITensor *dst0, Pooling3dLayerInfo &, const Window &window)
Definition: fp32.cpp:29
#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
#define REGISTER_QASYMM8_SIGNED_NEON(func_name)
Definition: Registrars.h:96
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
TensorShape compute_pool3d_shape(const TensorShape &src, Pooling3dLayerInfo pool3d_info)
Calculate the output pool3d shape of a tensor.
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(cond)
If the condition is true, an error is returned.
Definition: Error.h:296
Interface for CPU tensor.
Definition: ITensor.h:36
SimpleTensor< float > src
Definition: DFT.cpp:155
Copyright (c) 2017-2022 Arm Limited.
1 channel, 1 F16 per channel
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:159
static const std::vector< Pooling3dKernel > & get_available_kernels()
const ITensor * get_const_tensor(int id) const
Get constant tensor of a given id.
Definition: ITensorPack.cpp:54
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
#define REGISTER_QASYMM8_NEON(func_name)
Definition: Registrars.h:117
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
quantized, asymmetric fixed-point 8-bit number unsigned
void configure(const ITensorInfo *src, ITensorInfo *dst, const Pooling3dLayerInfo &pool_info)
Set the src, dst tensor and pooling info.
Class to describe a number of elements in each dimension.
Definition: Steps.h:40
size_t height
Height of the 3D shape or object.
Definition: Size3D.h:93
Pooling Layer Information struct.
Definition: Types.h:1295
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 std::unique_ptr< T > clone() const =0
Provide a clone of the current object of class T.
size_t width
Width of the 3D shape or object.
Definition: Size3D.h:92
void neon_q8_signed_pool3d(const ITensor *src0, ITensor *dst0, Pooling3dLayerInfo &, const Window &window)
Num samples, depth, height, width, channels.
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:915
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:64
size_t depth
Depth of the 3D shape or object.
Definition: Size3D.h:94
Information about executing thread and CPU.
Definition: CPPTypes.h:179
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(...)
Definition: Validate.h:439
static Status validate(const ITensorInfo *src, const ITensorInfo *dst, const Pooling3dLayerInfo &pool_info)
Static function to check if given info will lead to a valid configuration.
void run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
size_t get_data_layout_dimension_index(const DataLayout &data_layout, const DataLayoutDimension &data_layout_dimension)
Get the index of the given dimension.
Definition: Helpers.inl:193
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:541
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:788
std::tuple< int, int, int > scaled_3d_dimensions_signed(int width, int height, int depth, int kernel_width, int kernel_height, int kernel_depth, const Pooling3dLayerInfo &pool3d_info)
Returns calculated width, height and depth of output scaled tensor depending on dimensions rounding m...
Definition: Utils.cpp:492
#define ARM_COMPUTE_RETURN_ERROR_ON_MSG(cond, msg)
If the condition is true, an error is returned.
Definition: Error.h:244
Tensor packing service.
Definition: ITensorPack.h:39
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:157
quantized, asymmetric fixed-point 8-bit number signed
static CPUInfo & get()
Access the KernelLibrary singleton.
Definition: CPPTypes.cpp:40
Describe a multidimensional execution window.
Definition: Window.h:39
void neon_fp16_pool3d(const ITensor *src0, ITensor *dst0, Pooling3dLayerInfo &, const Window &window)
bool is_data_type_float(DataType dt)
Check if a given data type is of floating point type.
Definition: Utils.h:1010
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:201
cpuinfo::CpuIsaInfo get_isa() const
Gets the current cpu&#39;s ISA information.
Definition: CPPTypes.cpp:124
virtual DataLayout data_layout() const =0
Get the data layout of the tensor.