Manuel Bottini | 5b7d537 | 2019-05-17 14:04:22 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 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/core/NEON/kernels/NESpaceToDepthLayerKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/Helpers.h" |
| 27 | #include "arm_compute/core/ITensor.h" |
| 28 | #include "arm_compute/core/NEON/wrapper/wrapper.h" |
| 29 | #include "arm_compute/core/Types.h" |
| 30 | #include "arm_compute/core/Validate.h" |
| 31 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
| 32 | #include <arm_neon.h> |
| 33 | #include <cstdint> |
| 34 | |
| 35 | using namespace arm_compute::misc::shape_calculator; |
| 36 | |
| 37 | namespace arm_compute |
| 38 | { |
| 39 | namespace |
| 40 | { |
| 41 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, int32_t block_shape) |
| 42 | { |
| 43 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
Georgios Pinitas | 3384356 | 2019-12-10 13:33:18 +0000 | [diff] [blame] | 44 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN); |
Manuel Bottini | 5b7d537 | 2019-05-17 14:04:22 +0100 | [diff] [blame] | 45 | ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 4); |
| 46 | |
| 47 | ARM_COMPUTE_RETURN_ERROR_ON(block_shape < 1); |
| 48 | |
| 49 | // Validate output if initialized |
| 50 | if(output->total_size() != 0) |
| 51 | { |
| 52 | const DataLayout data_layout = input->data_layout(); |
| 53 | const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 54 | const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 55 | const int idx_channel = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL); |
| 56 | const int idx_batch = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES); |
| 57 | ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape()[idx_width] % block_shape != 0); |
| 58 | ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape()[idx_height] % block_shape != 0); |
| 59 | ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape()[idx_batch] != output->tensor_shape()[idx_batch]); |
| 60 | ARM_COMPUTE_RETURN_ERROR_ON(output->tensor_shape()[idx_channel] % (block_shape * block_shape) != 0); |
| 61 | ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape().total_size() != output->tensor_shape().total_size()); |
| 62 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 63 | } |
| 64 | |
| 65 | return Status{}; |
| 66 | } |
| 67 | } // namespace |
| 68 | |
| 69 | NESpaceToDepthLayerKernel::NESpaceToDepthLayerKernel() |
| 70 | : _input(nullptr), _output(nullptr), _block_shape() |
| 71 | { |
| 72 | } |
| 73 | |
| 74 | void NESpaceToDepthLayerKernel::configure(const ITensor *input, ITensor *output, int32_t block_shape) |
| 75 | { |
| 76 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 77 | |
| 78 | TensorShape output_shape = misc::shape_calculator::compute_space_to_depth_shape(input->info(), block_shape); |
| 79 | auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type()); |
| 80 | |
| 81 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), block_shape)); |
| 82 | |
| 83 | _input = input; |
| 84 | _block_shape = block_shape; |
| 85 | _output = output; |
| 86 | |
| 87 | // Configure kernel window |
| 88 | Window win = calculate_max_window(*output->info(), Steps()); |
| 89 | INEKernel::configure(win); |
| 90 | } |
| 91 | |
| 92 | Status NESpaceToDepthLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, int32_t block_shape) |
| 93 | { |
| 94 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, block_shape)); |
| 95 | return Status{}; |
| 96 | } |
| 97 | |
| 98 | void NESpaceToDepthLayerKernel::run(const Window &window, const ThreadInfo &info) |
| 99 | { |
| 100 | ARM_COMPUTE_UNUSED(info); |
| 101 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 102 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICPPKernel::window(), window); |
| 103 | |
| 104 | const DataLayout data_layout = _input->info()->data_layout(); |
| 105 | const int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL); |
| 106 | const int element_size = _input->info()->element_size(); |
| 107 | |
| 108 | const size_t channel_size = _input->info()->dimension(channel_idx); |
| 109 | |
| 110 | Window slice_out = window.first_slice_window_3D(); |
| 111 | |
| 112 | int batch_id = 0; |
| 113 | |
| 114 | // Main loop for NCHW and NHWC |
| 115 | if(_output->info()->data_layout() == DataLayout::NCHW) |
| 116 | { |
| 117 | do |
| 118 | { |
| 119 | Iterator out(_output, slice_out); |
| 120 | execute_window_loop(slice_out, [&](const Coordinates & id) |
| 121 | { |
| 122 | const size_t channel_id = id.z(); |
| 123 | const size_t in_x = id.x() * _block_shape + (channel_id / channel_size) % _block_shape; |
| 124 | const size_t in_y = id.y() * _block_shape + (channel_id / channel_size) / _block_shape; |
| 125 | const int z = channel_id % channel_size; |
| 126 | Coordinates input_coords{ in_x, in_y, z, batch_id }; |
| 127 | memcpy(out.ptr(), _input->ptr_to_element(input_coords), element_size); |
| 128 | }, |
| 129 | out); |
| 130 | ++batch_id; |
| 131 | } |
| 132 | while(window.slide_window_slice_3D(slice_out)); |
| 133 | } |
| 134 | else |
| 135 | { |
| 136 | do |
| 137 | { |
| 138 | Iterator out(_output, slice_out); |
| 139 | execute_window_loop(slice_out, [&](const Coordinates & id) |
| 140 | { |
| 141 | const size_t channel_id = id.x(); |
| 142 | const size_t in_x = id.y() * _block_shape + (channel_id / channel_size) % _block_shape; |
| 143 | const size_t in_y = id.z() * _block_shape + (channel_id / channel_size) / _block_shape; |
| 144 | const int z = channel_id % channel_size; |
| 145 | Coordinates input_coords{ z, in_x, in_y, batch_id }; |
| 146 | memcpy(out.ptr(), _input->ptr_to_element(input_coords), element_size); |
| 147 | }, |
| 148 | out); |
| 149 | ++batch_id; |
| 150 | } |
| 151 | while(window.slide_window_slice_3D(slice_out)); |
| 152 | } |
| 153 | } |
| 154 | } // namespace arm_compute |