Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [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/NEDepthwiseVectorToTensorKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/AccessWindowTranspose.h" |
| 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" |
| 37 | |
| 38 | using namespace arm_compute; |
| 39 | |
| 40 | NEDepthwiseVectorToTensorKernel::NEDepthwiseVectorToTensorKernel() |
| 41 | : _input(nullptr), _output(nullptr), _conv_dims() |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | void NEDepthwiseVectorToTensorKernel::configure(const ITensor *input, ITensor *output, size_t conv_w, size_t conv_h) |
| 46 | { |
| 47 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32); |
| 48 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 49 | ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input, output); |
| 50 | |
| 51 | _input = input; |
| 52 | _output = output; |
| 53 | _conv_dims = std::pair<size_t, size_t>(conv_w, conv_h); |
| 54 | |
| 55 | // Configure kernel window |
| 56 | Window win = calculate_max_window(*input->info(), Steps()); |
| 57 | // The NEDepthwisevectorToTensorKernel doesn't need padding so update_window_and_padding() can be skipped |
| 58 | output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape())); |
| 59 | |
| 60 | INEKernel::configure(win); |
| 61 | } |
| 62 | |
| 63 | void NEDepthwiseVectorToTensorKernel::run(const Window &window, const ThreadInfo &info) |
| 64 | { |
| 65 | ARM_COMPUTE_UNUSED(info); |
| 66 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 67 | |
| 68 | // const int input_w = _input->info()->dimension(0); |
| 69 | const int output_stride_x = _output->info()->strides_in_bytes().x(); |
| 70 | const int output_stride_y = _output->info()->strides_in_bytes().y(); |
| 71 | const int output_stride_z = _output->info()->strides_in_bytes().z(); |
| 72 | |
| 73 | // Setup output window |
| 74 | Window window_out(window); |
| 75 | window_out.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 76 | window_out.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 77 | window_out.set(Window::DimZ, Window::Dimension(0, 0, 0)); |
| 78 | |
| 79 | Iterator in(_input, window); |
| 80 | Iterator out(_output, window_out); |
| 81 | |
| 82 | const int patch_size = _conv_dims.first * _conv_dims.second; |
| 83 | |
| 84 | execute_window_loop(window, [&](const Coordinates & id) |
| 85 | { |
| 86 | const int z = id.x() / patch_size; |
| 87 | const int index2D = id.x() - z * patch_size; |
| 88 | |
| 89 | auto input_ptr = reinterpret_cast<float *>(in.ptr()); |
| 90 | auto output_ptr = reinterpret_cast<float *>(out.ptr() + index2D % _conv_dims.first * output_stride_x + index2D / _conv_dims.first * output_stride_y + z * output_stride_z); |
| 91 | |
| 92 | *output_ptr = *input_ptr; |
| 93 | }, |
| 94 | in, out); |
| 95 | } |