blob: 1967c553bdeea78ac4782169ab2336765bff2833 [file] [log] [blame]
morgolock37722d92020-04-09 14:17:48 +01001/*
2 * Copyright (c) 2020 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 */
24#include "arm_compute/core/NEON/kernels/NEMaxUnpoolingLayerKernel.h"
25
26#include "arm_compute/core/AccessWindowStatic.h"
27#include "arm_compute/core/CPP/Validate.h"
28#include "arm_compute/core/Error.h"
29#include "arm_compute/core/Helpers.h"
30#include "arm_compute/core/ITensor.h"
31#include "arm_compute/core/NEON/NEAsymm.h"
32#include "arm_compute/core/NEON/NEFixedPoint.h"
33#include "arm_compute/core/NEON/NEMath.h"
34#include "arm_compute/core/TensorInfo.h"
35#include "arm_compute/core/Utils.h"
36#include "arm_compute/core/Validate.h"
37#include "arm_compute/core/Window.h"
38#include "arm_compute/core/utils/misc/ShapeCalculator.h"
39
40#include "support/ToolchainSupport.h"
41
42namespace arm_compute
43{
44using namespace misc::shape_calculator;
45
46namespace
47{
48Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const PoolingLayerInfo &pool_info, const ITensorInfo *indices)
49{
50 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output, indices);
51 int pool_stride_x = 0;
52 int pool_stride_y = 0;
53 PoolingType pool_type = pool_info.pool_type;
54 const PadStrideInfo pad_stride_info = pool_info.pad_stride_info;
55 std::tie(pool_stride_x, pool_stride_y) = pad_stride_info.stride();
56 const int pool_size_x = pool_info.pool_size.width;
57 const int pool_size_y = pool_info.pool_size.height;
58 const Size2D pool_size(pool_size_x, pool_size_y);
59 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input);
60 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(indices, 1, DataType::U32);
61 ARM_COMPUTE_RETURN_ERROR_ON_MSG(pool_type != PoolingType::MAX, "Pooling indices only supported for MAX pooling method");
62 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32);
63 ARM_COMPUTE_RETURN_ERROR_ON_MSG((pool_size != Size2D(2, 2)), "Pooling indices only supported for pool size 2x2");
64 if(output->total_size() != 0)
65 {
66 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
67 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output);
68 }
69
70 return Status{};
71}
72} // namespace
73
74NEMaxUnpoolingLayerKernel::NEMaxUnpoolingLayerKernel()
75 : _func(nullptr), _input(nullptr), _output(nullptr), _indices(nullptr), _pool_info(), _data_layout(DataLayout::UNKNOWN), _num_elems_processed_per_iteration(0)
76{
77}
78
79void NEMaxUnpoolingLayerKernel::configure(const ITensor *input, const ITensor *indices, ITensor *output, const PoolingLayerInfo &pool_info)
80{
81 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
82 const Size2D pool_size(pool_info.pool_size.width, pool_info.pool_size.height);
83 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), pool_info, indices->info()));
84 _input = input;
85 _output = output;
86 _indices = indices;
87 _pool_info = pool_info;
88 _data_layout = input->info()->data_layout();
89 switch(input->info()->data_type())
90 {
91 case DataType::F32:
92 _func = &NEMaxUnpoolingLayerKernel::unpooling2<float>;
93 break;
94 case DataType::QASYMM8:
95 _func = &NEMaxUnpoolingLayerKernel::unpooling2<uint8_t>;
96 break;
97 case DataType::QASYMM8_SIGNED:
98 _func = &NEMaxUnpoolingLayerKernel::unpooling2<int8_t>;
99 break;
100#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
101 case DataType::F16:
102 _func = &NEMaxUnpoolingLayerKernel::unpooling2<float16_t>;
103 break;
104#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
105 default:
106 break;
107 }
108 const TensorShape output_shape = compute_unpool_shape(*input->info(), pool_info);
109 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape));
110 _num_elems_processed_per_iteration = 1;
111 auto window = calculate_max_window(*input->info(), Steps(_num_elems_processed_per_iteration));
112 INEKernel::configure(window);
113}
114template <typename T>
115void NEMaxUnpoolingLayerKernel::unpooling2(const Window &window)
116{
117 Iterator input(_input, window);
118 Iterator indices(_indices, window);
119 auto out_ptr = reinterpret_cast<T *>(_output->buffer());
120 const int out_stride_w = static_cast<int>(_output->info()->strides_in_bytes()[3]);
121 execute_window_loop(window, [&](const Coordinates & id)
122 {
123 auto vindices = reinterpret_cast<uint32_t *>(indices.ptr());
124 auto vinput = reinterpret_cast<T *>(input.ptr());
125 out_ptr[id[3] * out_stride_w / sizeof(T) + *vindices] = *vinput;
126 },
127 input, indices);
128}
129
130Status NEMaxUnpoolingLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *indices, const ITensorInfo *output, const PoolingLayerInfo &pool_info)
131{
132 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, indices, output);
133 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, pool_info, indices));
134 return Status{};
135}
136
137void NEMaxUnpoolingLayerKernel::run(const Window &window, const ThreadInfo &info)
138{
139 ARM_COMPUTE_UNUSED(info);
140 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
141 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
142 ARM_COMPUTE_ERROR_ON(_func == nullptr);
143 // Run function
144 (this->*_func)(window);
145}
146} // namespace arm_compute