Michalis Spyrou | 33a6990 | 2018-02-23 15:01:52 +0000 | [diff] [blame] | 1 | /* |
Usama Arif | 2899e00 | 2019-04-16 14:32:25 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 ARM Limited. |
Michalis Spyrou | 33a6990 | 2018-02-23 15:01:52 +0000 | [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/CPP/kernels/CPPUpsampleKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/Error.h" |
| 27 | #include "arm_compute/core/Helpers.h" |
| 28 | #include "arm_compute/core/ITensor.h" |
| 29 | #include "arm_compute/core/TensorInfo.h" |
| 30 | #include "arm_compute/core/Types.h" |
| 31 | #include "arm_compute/core/Validate.h" |
| 32 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
| 33 | |
| 34 | #include <cstddef> |
| 35 | #include <cstdint> |
| 36 | |
| 37 | using namespace arm_compute; |
| 38 | |
| 39 | CPPUpsampleKernel::CPPUpsampleKernel() |
| 40 | : _input(nullptr), _output(nullptr), _info(), _inner_border() |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | bool CPPUpsampleKernel::is_parallelisable() const |
| 45 | { |
| 46 | return false; |
| 47 | } |
| 48 | |
| 49 | void CPPUpsampleKernel::configure(const ITensor *input, ITensor *output, const PadStrideInfo &info, unsigned int inner_border_right, unsigned int inner_border_top) |
| 50 | { |
| 51 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 52 | |
| 53 | _input = input; |
| 54 | _output = output; |
| 55 | _info = info; |
| 56 | _inner_border = std::make_pair(inner_border_right, inner_border_top); |
| 57 | |
| 58 | // Configure kernel window |
| 59 | Window win = calculate_max_window(*input->info(), Steps()); |
| 60 | |
| 61 | // The CPPUpsampleKernel doesn't need padding so update_window_and_padding() can be skipped |
| 62 | Coordinates coord; |
| 63 | coord.set_num_dimensions(output->info()->num_dimensions()); |
| 64 | output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape())); |
| 65 | |
| 66 | ICPPKernel::configure(win); |
| 67 | } |
| 68 | |
| 69 | void CPPUpsampleKernel::run(const Window &window, const ThreadInfo &info) |
| 70 | { |
| 71 | ARM_COMPUTE_UNUSED(info); |
| 72 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 73 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICPPKernel::window(), window); |
| 74 | |
| 75 | // Initialize _scaled_output buffer |
Usama Arif | 2899e00 | 2019-04-16 14:32:25 +0100 | [diff] [blame] | 76 | const int width_scaled = _output->info()->dimension(0); |
| 77 | const int height_scaled = _output->info()->dimension(1); |
| 78 | const int stride_x = _info.stride().first; |
| 79 | const int stride_y = _info.stride().second; |
| 80 | const int start_x = _info.pad().first; |
| 81 | const int start_y = _inner_border.second + _info.pad().second; |
| 82 | const int end_y = height_scaled - _info.pad().second; |
| 83 | const int end_x = width_scaled - _inner_border.first - _info.pad().first; |
| 84 | const size_t element_size = _input->info()->element_size(); |
Michalis Spyrou | 33a6990 | 2018-02-23 15:01:52 +0000 | [diff] [blame] | 85 | |
| 86 | std::fill_n(_output->buffer(), _output->info()->total_size(), 0); |
| 87 | |
| 88 | // Create window |
| 89 | Window window_out(window); |
| 90 | window_out.set(Window::DimX, Window::Dimension(start_x, end_x, stride_x)); |
| 91 | window_out.set(Window::DimY, Window::Dimension(start_y, end_y, stride_y)); |
| 92 | |
| 93 | // Create iterators |
| 94 | Iterator in(_input, window); |
| 95 | Iterator out(_output, window_out); |
| 96 | |
| 97 | execute_window_loop(window, [&](const Coordinates & id) |
| 98 | { |
Usama Arif | 2899e00 | 2019-04-16 14:32:25 +0100 | [diff] [blame] | 99 | memcpy(out.ptr(), in.ptr(), element_size); |
Michalis Spyrou | 33a6990 | 2018-02-23 15:01:52 +0000 | [diff] [blame] | 100 | }, |
| 101 | in, out); |
| 102 | } |