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