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/NEDepthwiseWeightsReshapeKernel.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 | |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 40 | namespace |
| 41 | { |
| 42 | template <typename T> |
| 43 | void weights_reshape(const ITensor *input, const ITensor *bias, ITensor *output, const Window &window) |
| 44 | { |
| 45 | const int input_w = input->info()->dimension(0); |
| 46 | const int output_stride_x = output->info()->strides_in_bytes().x(); |
| 47 | const int output_stride_y = output->info()->strides_in_bytes().y(); |
| 48 | |
| 49 | Window window_in(window); |
| 50 | // The first three dimensions of the input are increased by the inner loops |
| 51 | window_in.set(Window::DimX, Window::Dimension(0, input->info()->dimension(0), input->info()->dimension(0))); |
| 52 | window_in.set(Window::DimY, Window::Dimension(0, input->info()->dimension(1), 1)); |
| 53 | window_in.set(Window::DimZ, Window::Dimension(0, input->info()->dimension(2), 1)); |
| 54 | |
| 55 | // Setup output window |
| 56 | Window window_out; |
| 57 | window_out.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 58 | window_out.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 59 | |
| 60 | Iterator in(input, window_in); |
| 61 | Iterator out(output, window_out); |
| 62 | |
| 63 | execute_window_loop(window_in, [&](const Coordinates & id) |
| 64 | { |
| 65 | auto input_ptr = reinterpret_cast<T *>(in.ptr()); |
| 66 | auto output_ptr = reinterpret_cast<T *>(out.ptr() + id.y() * input_w * output_stride_x + id.z() * output_stride_y); |
| 67 | |
| 68 | for(int i = 0; i < input_w; ++i, ++input_ptr) |
| 69 | { |
| 70 | *(output_ptr + i) = *input_ptr; |
| 71 | } |
| 72 | |
| 73 | if(bias != nullptr) |
| 74 | { |
| 75 | *(output_ptr + input_w) = *(reinterpret_cast<T *>(bias->ptr_to_element(Coordinates(id.z())))); |
| 76 | } |
| 77 | }, |
| 78 | in, out); |
| 79 | } |
| 80 | } // namespace |
| 81 | |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 82 | NEDepthwiseWeightsReshapeKernel::NEDepthwiseWeightsReshapeKernel() |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 83 | : _func(nullptr), _input(nullptr), _output(nullptr), _biases(nullptr) |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 84 | { |
| 85 | } |
| 86 | |
| 87 | void NEDepthwiseWeightsReshapeKernel::configure(const ITensor *input, ITensor *output, const ITensor *biases) |
| 88 | { |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 89 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32); |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 90 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 91 | ARM_COMPUTE_ERROR_ON(is_data_type_quantized_asymmetric(input->info()->data_type()) && (biases != nullptr)); |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 92 | ARM_COMPUTE_ERROR_ON(input->info()->dimension(2) != output->info()->dimension(1)); |
| 93 | ARM_COMPUTE_ERROR_ON(output->info()->dimension(0) != (input->info()->dimension(0) * input->info()->dimension(1) + ((biases != nullptr) ? 1 : 0))); |
| 94 | |
| 95 | if(biases != nullptr) |
| 96 | { |
| 97 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases); |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 98 | ARM_COMPUTE_ERROR_ON(biases->info()->dimension(0) != input->info()->dimension(2)); |
| 99 | ARM_COMPUTE_ERROR_ON(biases->info()->num_dimensions() > 1); |
| 100 | } |
| 101 | |
| 102 | _input = input; |
| 103 | _output = output; |
| 104 | _biases = biases; |
| 105 | |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 106 | switch(_input->info()->element_size()) |
| 107 | { |
| 108 | case 4: |
| 109 | { |
| 110 | _func = &weights_reshape<uint32_t>; |
| 111 | break; |
| 112 | } |
| 113 | case 2: |
| 114 | { |
| 115 | _func = &weights_reshape<uint16_t>; |
| 116 | break; |
| 117 | } |
| 118 | case 1: |
| 119 | { |
| 120 | _func = &weights_reshape<uint8_t>; |
| 121 | break; |
| 122 | } |
| 123 | default: |
| 124 | { |
| 125 | ARM_COMPUTE_ERROR_ON("Element size not supported"); |
| 126 | break; |
| 127 | } |
| 128 | } |
| 129 | |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 130 | // Configure kernel window |
| 131 | Window win = calculate_max_window(*input->info(), Steps()); |
| 132 | // The NEDepthwiseWeightsReshapeKernel 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 | |
| 138 | void NEDepthwiseWeightsReshapeKernel::run(const Window &window, const ThreadInfo &info) |
| 139 | { |
| 140 | ARM_COMPUTE_UNUSED(info); |
| 141 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 142 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 143 | |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 144 | if(_func != nullptr) |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 145 | { |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 146 | (*_func)(_input, _biases, _output, window); |
| 147 | } |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 148 | } |