Georgios Pinitas | 5ee66ea | 2017-09-07 17:29:16 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 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/NEReshapeLayerKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/AccessWindowStatic.h" |
| 27 | #include "arm_compute/core/Error.h" |
| 28 | #include "arm_compute/core/Helpers.h" |
| 29 | #include "arm_compute/core/IAccessWindow.h" |
| 30 | #include "arm_compute/core/ITensor.h" |
| 31 | #include "arm_compute/core/NEON/INEKernel.h" |
| 32 | #include "arm_compute/core/TensorInfo.h" |
| 33 | #include "arm_compute/core/Validate.h" |
| 34 | |
| 35 | #include <arm_neon.h> |
| 36 | #include <cstdint> |
| 37 | |
| 38 | using namespace arm_compute; |
| 39 | |
| 40 | namespace |
| 41 | { |
| 42 | template <typename T> |
| 43 | inline void reshape_tensor(const Window &window, const ITensor *input, ITensor *output) |
| 44 | { |
| 45 | const TensorShape &input_shape = input->info()->tensor_shape(); |
| 46 | const TensorShape &output_shape = output->info()->tensor_shape(); |
| 47 | Coordinates output_coord{}; |
| 48 | |
| 49 | window.collapse_if_possible(window, 3); |
| 50 | Iterator in(input, window); |
| 51 | |
| 52 | execute_window_loop(window, [&](const Coordinates & id) |
| 53 | { |
| 54 | output_coord = index2coords(output_shape, coords2index(input_shape, id)); |
| 55 | *reinterpret_cast<T *>(output->ptr_to_element(output_coord)) = *reinterpret_cast<T *>(in.ptr()); |
| 56 | }, |
| 57 | in); |
| 58 | } |
| 59 | } // namespace |
| 60 | |
| 61 | void NEReshapeLayerKernel::configure(const ITensor *input, ITensor *output) |
| 62 | { |
Les Bell | a5e6d67 | 2017-11-29 10:48:23 +0000 | [diff] [blame] | 63 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::S8, DataType::QASYMM8, DataType::QS8, DataType::U16, DataType::S16, DataType::QS16, |
Georgios Pinitas | 5ee66ea | 2017-09-07 17:29:16 +0100 | [diff] [blame] | 64 | DataType::U32, DataType::S32, DataType::F16, DataType::F32); |
| 65 | ARM_COMPUTE_ERROR_ON_NULLPTR(output); |
| 66 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 67 | ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input, output); |
| 68 | ARM_COMPUTE_ERROR_ON(input->info()->tensor_shape().total_size() != output->info()->tensor_shape().total_size()); |
| 69 | |
| 70 | _input = input; |
| 71 | _output = output; |
| 72 | |
| 73 | constexpr unsigned int num_elems_processed_per_iteration = 1; |
| 74 | |
| 75 | // Configure kernel window |
| 76 | Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration)); |
| 77 | |
| 78 | AccessWindowHorizontal input_access(input->info(), 0, num_elems_processed_per_iteration); |
| 79 | AccessWindowStatic output_access(output->info(), 0, 0, output->info()->tensor_shape().x(), output->info()->tensor_shape().y()); |
| 80 | update_window_and_padding(win, input_access, output_access); |
| 81 | |
| 82 | output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape())); |
| 83 | |
| 84 | INEKernel::configure(win); |
| 85 | } |
| 86 | |
| 87 | void NEReshapeLayerKernel::run(const Window &window, const ThreadInfo &info) |
| 88 | { |
| 89 | ARM_COMPUTE_UNUSED(info); |
| 90 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 91 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INESimpleKernel::window(), window); |
| 92 | |
| 93 | switch(_input->info()->data_type()) |
| 94 | { |
| 95 | case DataType::U8: |
| 96 | case DataType::S8: |
Les Bell | a5e6d67 | 2017-11-29 10:48:23 +0000 | [diff] [blame] | 97 | case DataType::QASYMM8: |
Georgios Pinitas | 5ee66ea | 2017-09-07 17:29:16 +0100 | [diff] [blame] | 98 | case DataType::QS8: |
| 99 | reshape_tensor<uint8_t>(window, _input, _output); |
| 100 | break; |
| 101 | case DataType::U16: |
| 102 | case DataType::S16: |
| 103 | case DataType::QS16: |
| 104 | case DataType::F16: |
| 105 | reshape_tensor<uint16_t>(window, _input, _output); |
| 106 | break; |
| 107 | case DataType::U32: |
| 108 | case DataType::S32: |
| 109 | case DataType::F32: |
| 110 | reshape_tensor<uint32_t>(window, _input, _output); |
| 111 | break; |
| 112 | default: |
| 113 | ARM_COMPUTE_ERROR("Unsupported data type!"); |
| 114 | } |
| 115 | } |