Giorgio Arena | 368e635 | 2018-08-20 15:06:07 +0100 | [diff] [blame] | 1 | /* |
Isabella Gottardi | 0a1090a | 2019-02-14 18:07:36 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2019 ARM Limited. |
Giorgio Arena | 368e635 | 2018-08-20 15:06:07 +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/NEFlattenLayerKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/CPP/Validate.h" |
| 27 | #include "arm_compute/core/Error.h" |
| 28 | #include "arm_compute/core/Helpers.h" |
| 29 | #include "arm_compute/core/ITensor.h" |
| 30 | #include "arm_compute/core/TensorInfo.h" |
| 31 | #include "arm_compute/core/Types.h" |
| 32 | #include "arm_compute/core/Validate.h" |
| 33 | |
| 34 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
| 35 | |
| 36 | #include <arm_neon.h> |
| 37 | |
| 38 | using namespace arm_compute; |
| 39 | using namespace misc::shape_calculator; |
| 40 | |
| 41 | namespace |
| 42 | { |
| 43 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output) |
| 44 | { |
| 45 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::S8, DataType::QASYMM8, |
| 46 | DataType::U16, DataType::S16, |
| 47 | DataType::U32, DataType::S32, |
| 48 | DataType::F16, DataType::F32); |
Georgios Pinitas | 8f5802f | 2019-02-22 11:08:32 +0000 | [diff] [blame] | 49 | // Note: ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input) is not needed here as this kernel doesn't use NEON FP16 instructions. |
Giorgio Arena | 368e635 | 2018-08-20 15:06:07 +0100 | [diff] [blame] | 50 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output); |
| 51 | |
| 52 | // Checks performed when output is configured |
| 53 | if(output->total_size() != 0) |
| 54 | { |
| 55 | const TensorInfo tensor_info_output = input->clone()->set_tensor_shape(compute_flatten_shape(input)); |
| 56 | |
| 57 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &tensor_info_output); |
| 58 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
Isabella Gottardi | 0a1090a | 2019-02-14 18:07:36 +0000 | [diff] [blame] | 59 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output); |
Giorgio Arena | 368e635 | 2018-08-20 15:06:07 +0100 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | return Status{}; |
| 63 | } |
| 64 | |
| 65 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output) |
| 66 | { |
| 67 | // Output tensor auto initialization if not yet initialized |
| 68 | auto_init_if_empty(*output, input->clone()->set_tensor_shape(compute_flatten_shape(input))); |
| 69 | |
| 70 | Window win = calculate_max_window(*input, Steps()); // Flatten does not need paddings |
| 71 | |
| 72 | output->set_valid_region(ValidRegion(Coordinates(), output->tensor_shape())); |
| 73 | |
| 74 | return std::make_pair(Status{}, win); |
| 75 | } |
| 76 | } // namespace |
| 77 | |
| 78 | NEFlattenLayerKernel::NEFlattenLayerKernel() |
| 79 | : _input(nullptr), _output(nullptr) |
| 80 | { |
| 81 | } |
| 82 | |
| 83 | void NEFlattenLayerKernel::configure(const ITensor *input, ITensor *output) |
| 84 | { |
| 85 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 86 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info())); |
| 87 | |
| 88 | _input = input; |
| 89 | _output = output; |
| 90 | |
| 91 | // Configure kernel window |
| 92 | auto win_config = validate_and_configure_window(input->info(), output->info()); |
| 93 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 94 | INEKernel::configure(win_config.second); |
| 95 | } |
| 96 | |
| 97 | Status NEFlattenLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output) |
| 98 | { |
| 99 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output)); |
| 100 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get()).first); |
| 101 | return Status{}; |
| 102 | } |
| 103 | |
| 104 | void NEFlattenLayerKernel::run(const Window &window, const ThreadInfo &info) |
| 105 | { |
| 106 | ARM_COMPUTE_UNUSED(info); |
| 107 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 108 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 109 | |
| 110 | const size_t in_width = _input->info()->dimension(0); |
| 111 | const size_t in_height = _input->info()->dimension(1); |
| 112 | const size_t out_step_x = in_width * _input->info()->element_size(); |
| 113 | const size_t out_step_y = out_step_x * in_height; |
| 114 | |
| 115 | Window in_window(window); |
| 116 | in_window.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 117 | |
| 118 | Window out_window; |
| 119 | out_window.use_tensor_dimensions(_output->info()->tensor_shape()); |
| 120 | out_window.set(Window::DimX, Window::Dimension(out_window.x().start(), out_window.x().end(), in_width)); |
| 121 | |
| 122 | Window in_slice = in_window.first_slice_window_3D(); |
| 123 | Window out_slice = out_window.first_slice_window_1D(); |
| 124 | |
| 125 | do |
| 126 | { |
| 127 | Iterator in(_input, in_slice); |
| 128 | Iterator out(_output, out_slice); |
| 129 | |
| 130 | uint8_t *out_ptr = out.ptr(); |
| 131 | |
| 132 | execute_window_loop(in_slice, [&](const Coordinates & id) |
| 133 | { |
| 134 | memcpy(out_ptr + id.y() * out_step_x + id.z() * out_step_y, in.ptr(), out_step_x); |
| 135 | }, |
| 136 | in); |
| 137 | } |
| 138 | while(in_window.slide_window_slice_3D(in_slice) && out_window.slide_window_slice_1D(out_slice)); |
| 139 | } |