Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 1 | /* |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +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/NEON/kernels/NEDepthwiseVectorToTensorKernel.h" |
| 25 | |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/CPP/Validate.h" |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 27 | #include "arm_compute/core/Coordinates.h" |
| 28 | #include "arm_compute/core/Error.h" |
| 29 | #include "arm_compute/core/Helpers.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/TensorShape.h" |
| 34 | #include "arm_compute/core/Types.h" |
| 35 | #include "arm_compute/core/Validate.h" |
| 36 | #include "arm_compute/core/Window.h" |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 37 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 38 | |
| 39 | using namespace arm_compute; |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 40 | using namespace arm_compute::misc::shape_calculator; |
| 41 | |
| 42 | namespace |
| 43 | { |
| 44 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, size_t conv_w, size_t conv_h) |
| 45 | { |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 46 | ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input); |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 47 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::S32, DataType::F16, DataType::F32); |
| 48 | |
| 49 | if(output->total_size() != 0) |
| 50 | { |
| 51 | TensorShape output_shape = compute_vector_to_tensor_output_shape(input->tensor_shape(), conv_w, conv_h, output->data_layout()); |
| 52 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), output_shape); |
| 53 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 54 | } |
| 55 | |
| 56 | return Status{}; |
| 57 | } |
| 58 | } // namespace |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 59 | |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 60 | template <typename T> |
| 61 | void NEDepthwiseVectorToTensorKernel::vector_to_tensor(const Window &window) |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 62 | { |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 63 | // const int input_w = _input->info()->dimension(0); |
| 64 | const int output_stride_x = _output->info()->strides_in_bytes().x(); |
| 65 | const int output_stride_y = _output->info()->strides_in_bytes().y(); |
| 66 | const int output_stride_z = _output->info()->strides_in_bytes().z(); |
| 67 | |
| 68 | // Setup output window |
| 69 | Window window_out(window); |
| 70 | window_out.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 71 | window_out.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 72 | window_out.set(Window::DimZ, Window::Dimension(0, 0, 0)); |
| 73 | |
| 74 | Iterator in(_input, window); |
| 75 | Iterator out(_output, window_out); |
| 76 | |
| 77 | const int patch_size = _conv_dims.first * _conv_dims.second; |
| 78 | |
| 79 | execute_window_loop(window, [&](const Coordinates & id) |
| 80 | { |
| 81 | const int z = id.x() / patch_size; |
| 82 | const int index2D = id.x() - z * patch_size; |
| 83 | |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 84 | auto input_ptr = reinterpret_cast<T *>(in.ptr()); |
| 85 | auto output_ptr = reinterpret_cast<T *>(out.ptr() + index2D % _conv_dims.first * output_stride_x + index2D / _conv_dims.first * output_stride_y + z * output_stride_z); |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 86 | |
| 87 | *output_ptr = *input_ptr; |
| 88 | }, |
| 89 | in, out); |
| 90 | } |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 91 | |
| 92 | NEDepthwiseVectorToTensorKernel::NEDepthwiseVectorToTensorKernel() |
| 93 | : _func(nullptr), _input(nullptr), _output(nullptr), _conv_dims() |
| 94 | { |
| 95 | } |
| 96 | |
| 97 | void NEDepthwiseVectorToTensorKernel::configure(const ITensor *input, ITensor *output, size_t conv_w, size_t conv_h) |
| 98 | { |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 99 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 100 | |
| 101 | // Output auto inizialitation if not yet initialized |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 102 | TensorShape output_shape = compute_vector_to_tensor_output_shape(input->info()->tensor_shape(), conv_w, conv_h, output->info()->data_layout()); |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 103 | auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape)); |
| 104 | |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 105 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), conv_w, conv_h)); |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 106 | |
| 107 | _input = input; |
| 108 | _output = output; |
| 109 | _conv_dims = std::pair<size_t, size_t>(conv_w, conv_h); |
| 110 | |
| 111 | // Set appropriate function to run |
| 112 | switch(input->info()->data_type()) |
| 113 | { |
| 114 | case DataType::QASYMM8: |
| 115 | _func = &NEDepthwiseVectorToTensorKernel::vector_to_tensor<uint8_t>; |
| 116 | break; |
| 117 | case DataType::S32: |
| 118 | _func = &NEDepthwiseVectorToTensorKernel::vector_to_tensor<int32_t>; |
| 119 | break; |
| 120 | case DataType::F16: |
| 121 | _func = &NEDepthwiseVectorToTensorKernel::vector_to_tensor<half>; |
| 122 | break; |
| 123 | case DataType::F32: |
| 124 | _func = &NEDepthwiseVectorToTensorKernel::vector_to_tensor<float>; |
| 125 | break; |
| 126 | default: |
| 127 | ARM_COMPUTE_ERROR("Unsupported data type"); |
| 128 | } |
| 129 | |
| 130 | // Configure kernel window |
| 131 | Window win = calculate_max_window(*input->info(), Steps()); |
| 132 | // The NEDepthwisevectorToTensorKernel doesn't need padding so update_window_and_padding() can be skipped |
| 133 | output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape())); |
| 134 | |
| 135 | INEKernel::configure(win); |
| 136 | } |
| 137 | |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 138 | Status NEDepthwiseVectorToTensorKernel::validate(const ITensorInfo *input, const ITensorInfo *output, size_t conv_w, size_t conv_h) |
| 139 | { |
| 140 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
| 141 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, conv_w, conv_h)); |
| 142 | return Status{}; |
| 143 | } |
| 144 | |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 145 | void NEDepthwiseVectorToTensorKernel::run(const Window &window, const ThreadInfo &info) |
| 146 | { |
| 147 | ARM_COMPUTE_UNUSED(info); |
| 148 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 149 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 150 | |
| 151 | if(_func != nullptr) |
| 152 | { |
| 153 | (this->*_func)(window); |
| 154 | } |
| 155 | } |