Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016, 2017 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/NEDeconvolutionLayerUpsampleKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/AccessWindowStatic.h" |
| 27 | #include "arm_compute/core/Coordinates.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/TensorInfo.h" |
| 32 | #include "arm_compute/core/Validate.h" |
| 33 | #include "arm_compute/core/Window.h" |
| 34 | |
| 35 | #include <arm_neon.h> |
| 36 | #include <cstddef> |
| 37 | #include <cstdint> |
| 38 | |
| 39 | using namespace arm_compute; |
| 40 | |
| 41 | NEDeconvolutionLayerUpsampleKernel::NEDeconvolutionLayerUpsampleKernel() |
| 42 | : _offsets(nullptr), _input(nullptr), _output(nullptr) |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | BorderSize NEDeconvolutionLayerUpsampleKernel::border_size() const |
| 47 | { |
| 48 | return BorderSize(1); |
| 49 | } |
| 50 | |
| 51 | void NEDeconvolutionLayerUpsampleKernel::configure(const ITensor *input, const ITensor *offsets, ITensor *output) |
| 52 | { |
| 53 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32); |
| 54 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::F32); |
| 55 | ARM_COMPUTE_ERROR_ON(output->info()->dimension(0) == 0); |
| 56 | ARM_COMPUTE_ERROR_ON(output->info()->dimension(1) == 0); |
| 57 | |
| 58 | for(size_t i = 2; i < Coordinates::num_max_dimensions; ++i) |
| 59 | { |
| 60 | ARM_COMPUTE_ERROR_ON(input->info()->dimension(i) != output->info()->dimension(i)); |
| 61 | } |
| 62 | |
| 63 | _input = input; |
| 64 | _output = output; |
| 65 | _offsets = offsets; |
| 66 | |
| 67 | constexpr unsigned int num_elems_processed_per_iteration = 16; |
| 68 | const int border_offset = border_size().left; |
| 69 | |
| 70 | // Configure kernel window |
| 71 | Window win = calculate_max_window(*output->info(), Steps(num_elems_processed_per_iteration)); |
| 72 | |
| 73 | AccessWindowRectangle input_access(input->info(), -border_offset, -border_offset, input->info()->dimension(0) + border_offset, input->info()->dimension(1) + border_offset); |
| 74 | AccessWindowHorizontal offsets_access(offsets->info(), 0, num_elems_processed_per_iteration); |
| 75 | AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration); |
| 76 | |
| 77 | update_window_and_padding(win, input_access, offsets_access, output_access); |
| 78 | |
| 79 | output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape())); |
| 80 | |
| 81 | INEKernel::configure(win); |
| 82 | } |
| 83 | |
| 84 | void NEDeconvolutionLayerUpsampleKernel::scale_nearest(const Window &window) |
| 85 | { |
| 86 | const size_t input_stride = _input->info()->strides_in_bytes()[1]; |
| 87 | |
| 88 | // Compute the ratio between source height and destination height |
| 89 | const auto hr = static_cast<float>(_input->info()->dimension(1)) / static_cast<float>(_output->info()->dimension(1)); |
| 90 | |
| 91 | // Don't increment in X and Y direction for the input tensor |
| 92 | // A pointer to the start of this plane is needed as base for the precomputed offsets |
| 93 | Window win_in(window); |
| 94 | win_in.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 95 | win_in.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 96 | |
| 97 | Window win_off; |
| 98 | win_off.set(Window::DimX, window[Window::DimX]); |
| 99 | win_off.set(Window::DimY, window[Window::DimY]); |
| 100 | |
| 101 | for(size_t d = Window::DimZ; d < _offsets->info()->num_dimensions(); ++d) |
| 102 | { |
| 103 | win_off.set(d, Window::Dimension(0, 0, 0)); |
| 104 | } |
| 105 | |
| 106 | Iterator in(_input, win_in); |
| 107 | Iterator out(_output, window); |
| 108 | Iterator offsets(_offsets, win_off); |
| 109 | |
| 110 | switch(_input->info()->data_type()) |
| 111 | { |
| 112 | case DataType::F32: |
| 113 | { |
| 114 | float32x4x4_t tmp = |
| 115 | { |
| 116 | { |
| 117 | vdupq_n_f32(0), |
| 118 | vdupq_n_f32(0) |
| 119 | } |
| 120 | }; |
| 121 | execute_window_loop(window, [&](const Coordinates & id) |
| 122 | { |
| 123 | const auto offsets_ptr = reinterpret_cast<const int32_t *>(offsets.ptr()); |
| 124 | |
| 125 | const size_t in_yi = (id.y() + 0.5f) * hr; |
| 126 | const size_t offset_row = in_yi * input_stride; |
| 127 | |
| 128 | tmp.val[0] = vsetq_lane_f32(*reinterpret_cast<const float *>(in.ptr() + offsets_ptr[0] + offset_row), tmp.val[0], 0); |
| 129 | tmp.val[0] = vsetq_lane_f32(*reinterpret_cast<const float *>(in.ptr() + offsets_ptr[4] + offset_row), tmp.val[0], 1); |
| 130 | tmp.val[0] = vsetq_lane_f32(*reinterpret_cast<const float *>(in.ptr() + offsets_ptr[8] + offset_row), tmp.val[0], 2); |
| 131 | tmp.val[0] = vsetq_lane_f32(*reinterpret_cast<const float *>(in.ptr() + offsets_ptr[12] + offset_row), tmp.val[0], 3); |
| 132 | |
| 133 | tmp.val[1] = vsetq_lane_f32(*reinterpret_cast<const float *>(in.ptr() + offsets_ptr[1] + offset_row), tmp.val[1], 0); |
| 134 | tmp.val[1] = vsetq_lane_f32(*reinterpret_cast<const float *>(in.ptr() + offsets_ptr[5] + offset_row), tmp.val[1], 1); |
| 135 | tmp.val[1] = vsetq_lane_f32(*reinterpret_cast<const float *>(in.ptr() + offsets_ptr[9] + offset_row), tmp.val[1], 2); |
| 136 | tmp.val[1] = vsetq_lane_f32(*reinterpret_cast<const float *>(in.ptr() + offsets_ptr[13] + offset_row), tmp.val[1], 3); |
| 137 | |
| 138 | tmp.val[2] = vsetq_lane_f32(*reinterpret_cast<const float *>(in.ptr() + offsets_ptr[2] + offset_row), tmp.val[2], 0); |
| 139 | tmp.val[2] = vsetq_lane_f32(*reinterpret_cast<const float *>(in.ptr() + offsets_ptr[6] + offset_row), tmp.val[2], 1); |
| 140 | tmp.val[2] = vsetq_lane_f32(*reinterpret_cast<const float *>(in.ptr() + offsets_ptr[10] + offset_row), tmp.val[2], 2); |
| 141 | tmp.val[2] = vsetq_lane_f32(*reinterpret_cast<const float *>(in.ptr() + offsets_ptr[14] + offset_row), tmp.val[2], 3); |
| 142 | |
| 143 | tmp.val[3] = vsetq_lane_f32(*reinterpret_cast<const float *>(in.ptr() + offsets_ptr[3] + offset_row), tmp.val[3], 0); |
| 144 | tmp.val[3] = vsetq_lane_f32(*reinterpret_cast<const float *>(in.ptr() + offsets_ptr[7] + offset_row), tmp.val[3], 1); |
| 145 | tmp.val[3] = vsetq_lane_f32(*reinterpret_cast<const float *>(in.ptr() + offsets_ptr[11] + offset_row), tmp.val[3], 2); |
| 146 | tmp.val[3] = vsetq_lane_f32(*reinterpret_cast<const float *>(in.ptr() + offsets_ptr[15] + offset_row), tmp.val[3], 3); |
| 147 | |
| 148 | vst4q_f32(reinterpret_cast<float *>(out.ptr()), tmp); |
| 149 | }, |
| 150 | in, offsets, out); |
| 151 | break; |
| 152 | } |
| 153 | default: |
| 154 | ARM_COMPUTE_ERROR("Not supported"); |
| 155 | break; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | void NEDeconvolutionLayerUpsampleKernel::run(const Window &window, const ThreadInfo &info) |
| 160 | { |
| 161 | ARM_COMPUTE_UNUSED(info); |
| 162 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 163 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 164 | scale_nearest(window); |
| 165 | } |