Georgios Pinitas | dea2d2d | 2018-12-19 16:23:17 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018-2019 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/runtime/NEON/functions/NEPadLayer.h" |
| 25 | |
| 26 | #include "arm_compute/runtime/NEON/NEScheduler.h" |
| 27 | |
| 28 | #include "arm_compute/core/ITensor.h" |
| 29 | #include "arm_compute/core/Types.h" |
| 30 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
| 31 | |
| 32 | #include "support/ToolchainSupport.h" |
| 33 | |
| 34 | namespace arm_compute |
| 35 | { |
| 36 | namespace |
| 37 | { |
| 38 | TensorInfo get_expected_output_tensorinfo(const ITensorInfo &input, const PaddingList &paddings) |
| 39 | { |
| 40 | const TensorShape expected_output_shape = arm_compute::misc::shape_calculator::compute_padded_shape(input.tensor_shape(), paddings); |
| 41 | const TensorInfo expected_output_info = input.clone()->set_tensor_shape(expected_output_shape); |
| 42 | return expected_output_info; |
| 43 | } |
| 44 | |
| 45 | Status validate_arguments(const ITensorInfo &input, ITensorInfo &output, const PaddingList &paddings) |
| 46 | { |
| 47 | const TensorInfo expected_output_info = get_expected_output_tensorinfo(input, paddings); |
| 48 | auto_init_if_empty(output, expected_output_info); |
| 49 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(&output, &expected_output_info); |
| 50 | |
| 51 | return Status{}; |
| 52 | } |
| 53 | |
| 54 | Coordinates get_subtensor_coords(const PaddingList &paddings) |
| 55 | { |
| 56 | Coordinates coords; |
| 57 | for(unsigned int i = 0; i < paddings.size(); ++i) |
| 58 | { |
| 59 | coords.set(i, paddings[i].first); |
| 60 | } |
| 61 | |
| 62 | return coords; |
| 63 | } |
| 64 | } // namespace |
| 65 | |
| 66 | NEPadLayer::NEPadLayer() |
| 67 | : _memset_kernel(), _copy_kernel(), _output_subtensor() |
| 68 | { |
| 69 | } |
| 70 | |
| 71 | void NEPadLayer::configure(ITensor *input, ITensor *output, const PaddingList &padding, PixelValue constant_value) |
| 72 | { |
| 73 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 74 | ARM_COMPUTE_THROW_ON_ERROR(NEPadLayer::validate(input->info(), output->info(), padding, constant_value)); |
| 75 | |
| 76 | // Auto-init |
| 77 | auto_init_if_empty(*output->info(), get_expected_output_tensorinfo(*input->info(), padding)); |
| 78 | |
| 79 | // Create SubTensor (Can use sub-tensor as the kernels to be executed do not require padding) |
| 80 | _output_subtensor = SubTensor(output, input->info()->tensor_shape(), get_subtensor_coords(padding), true); |
| 81 | |
| 82 | // Set the pages of the output to the specified value |
| 83 | _memset_kernel.configure(output, constant_value); |
| 84 | |
| 85 | // Copy the input to the output |
| 86 | _copy_kernel.configure(input, &_output_subtensor); |
| 87 | } |
| 88 | |
| 89 | Status NEPadLayer::validate(const ITensorInfo *input, const ITensorInfo *output, const PaddingList &padding, PixelValue constant_value) |
| 90 | { |
| 91 | ARM_COMPUTE_UNUSED(constant_value); |
| 92 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
| 93 | |
| 94 | auto output_clone = output->clone(); |
| 95 | |
| 96 | SubTensorInfo output_subtensor_info(output_clone.get(), input->tensor_shape(), get_subtensor_coords(padding), true); |
| 97 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(*input, *output_clone, padding)); |
| 98 | ARM_COMPUTE_RETURN_ON_ERROR(NECopyKernel::validate(input, &output_subtensor_info)); |
| 99 | |
| 100 | return Status{}; |
| 101 | } |
| 102 | |
| 103 | void NEPadLayer::run() |
| 104 | { |
| 105 | NEScheduler::get().schedule(&_memset_kernel, Window::DimY); |
| 106 | NEScheduler::get().schedule(&_copy_kernel, Window::DimY); |
| 107 | } |
| 108 | } // namespace arm_compute |