Manuel Bottini | 29599d0 | 2021-07-06 15:01:35 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017-2021 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 | */ |
Georgios Pinitas | 7891a73 | 2021-08-20 21:39:25 +0100 | [diff] [blame] | 24 | #include "src/cpu/kernels/CpuWeightsReshapeKernel.h" |
Manuel Bottini | 29599d0 | 2021-07-06 15:01:35 +0100 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/Helpers.h" |
| 27 | #include "arm_compute/core/Validate.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 28 | |
Manuel Bottini | 29599d0 | 2021-07-06 15:01:35 +0100 | [diff] [blame] | 29 | #include "src/core/helpers/AutoConfiguration.h" |
| 30 | #include "src/core/helpers/WindowHelpers.h" |
| 31 | |
| 32 | namespace arm_compute |
| 33 | { |
| 34 | namespace cpu |
| 35 | { |
| 36 | namespace kernels |
| 37 | { |
| 38 | namespace |
| 39 | { |
| 40 | TensorShape get_output_shape(const ITensorInfo *src, bool has_bias) |
| 41 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 42 | TensorShape output_shape{src->tensor_shape()}; |
Manuel Bottini | 29599d0 | 2021-07-06 15:01:35 +0100 | [diff] [blame] | 43 | |
| 44 | output_shape.collapse(3); |
| 45 | const size_t tmp_dim = output_shape[0]; |
| 46 | output_shape.set(0, output_shape[1]); |
| 47 | output_shape.set(1, tmp_dim + (has_bias ? 1 : 0)); |
| 48 | |
| 49 | return output_shape; |
| 50 | } |
| 51 | |
| 52 | Status validate_arguments(const ITensorInfo *src, const ITensorInfo *biases, const ITensorInfo *dst) |
| 53 | { |
| 54 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst); |
| 55 | //Note: ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(src) is not needed here as this kernel doesn't use CPU FP16 instructions. |
| 56 | ARM_COMPUTE_RETURN_ERROR_ON(src->data_type() == DataType::UNKNOWN); |
| 57 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 58 | if (biases != nullptr) |
Manuel Bottini | 29599d0 | 2021-07-06 15:01:35 +0100 | [diff] [blame] | 59 | { |
| 60 | ARM_COMPUTE_RETURN_ERROR_ON(is_data_type_quantized_asymmetric(src->data_type())); |
| 61 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, biases); |
| 62 | ARM_COMPUTE_RETURN_ERROR_ON((src->num_dimensions() == 4) && (biases->num_dimensions() != 1)); |
| 63 | ARM_COMPUTE_RETURN_ERROR_ON((src->num_dimensions() == 5) && (biases->num_dimensions() != 2)); |
| 64 | ARM_COMPUTE_RETURN_ERROR_ON((src->num_dimensions() == 4) && (biases->dimension(0) != src->tensor_shape()[3])); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 65 | ARM_COMPUTE_RETURN_ERROR_ON((src->num_dimensions() == 5) && (biases->dimension(0) != src->tensor_shape()[3] || |
| 66 | biases->dimension(1) != src->tensor_shape()[4])); |
Manuel Bottini | 29599d0 | 2021-07-06 15:01:35 +0100 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | // Checks performed when output is configured |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 70 | if (dst->total_size() != 0) |
Manuel Bottini | 29599d0 | 2021-07-06 15:01:35 +0100 | [diff] [blame] | 71 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 72 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(dst->tensor_shape(), |
| 73 | get_output_shape(src, biases != nullptr)); |
Manuel Bottini | 29599d0 | 2021-07-06 15:01:35 +0100 | [diff] [blame] | 74 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst); |
| 75 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(src, dst); |
| 76 | } |
| 77 | |
| 78 | return Status{}; |
| 79 | } |
| 80 | } // namespace |
| 81 | |
| 82 | void CpuWeightsReshapeKernel::configure(const ITensorInfo *src, const ITensorInfo *biases, ITensorInfo *dst) |
| 83 | { |
| 84 | ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst); |
| 85 | |
| 86 | // Output tensor auto inizialitation if not yet initialized |
| 87 | auto_init_if_empty(*dst, src->clone()->set_tensor_shape(get_output_shape(src, (biases != nullptr)))); |
| 88 | |
| 89 | // Perform validation step |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 90 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, biases, dst)); |
Manuel Bottini | 29599d0 | 2021-07-06 15:01:35 +0100 | [diff] [blame] | 91 | |
| 92 | // Configure kernel |
| 93 | Window window = calculate_max_window(*src, Steps()); |
| 94 | window.set(Window::DimX, Window::Dimension(0, src->dimension(0), src->dimension(0))); |
| 95 | window.set(Window::DimY, Window::Dimension(0, src->dimension(1), src->dimension(1))); |
| 96 | window.set(Window::DimZ, Window::Dimension(0, src->dimension(2), src->dimension(2))); |
| 97 | ICpuKernel::configure(window); |
| 98 | } |
| 99 | |
| 100 | Status CpuWeightsReshapeKernel::validate(const ITensorInfo *src, const ITensorInfo *biases, const ITensorInfo *dst) |
| 101 | { |
| 102 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, biases, dst)); |
| 103 | return Status{}; |
| 104 | } |
| 105 | |
| 106 | void CpuWeightsReshapeKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info) |
| 107 | { |
| 108 | ARM_COMPUTE_UNUSED(info); |
| 109 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 110 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window); |
| 111 | |
| 112 | auto src = tensors.get_const_tensor(TensorType::ACL_SRC); |
| 113 | auto biases = tensors.get_const_tensor(TensorType::ACL_BIAS); |
| 114 | auto dst = tensors.get_tensor(TensorType::ACL_DST); |
| 115 | |
| 116 | const unsigned int kernel_size_x = src->info()->dimension(0); |
| 117 | const unsigned int kernel_size_y = src->info()->dimension(1); |
| 118 | const unsigned int kernel_depth = src->info()->dimension(2); |
| 119 | const unsigned int input_stride_x = src->info()->strides_in_bytes().x(); |
| 120 | const unsigned int input_stride_y = src->info()->strides_in_bytes().y(); |
| 121 | const unsigned int input_stride_z = src->info()->strides_in_bytes().z(); |
| 122 | const unsigned int output_stride_y = dst->info()->strides_in_bytes().y(); |
| 123 | |
| 124 | // Create iterators |
| 125 | Iterator in(src, window); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 126 | execute_window_loop( |
| 127 | window, |
| 128 | [&](const Coordinates &id) |
Manuel Bottini | 29599d0 | 2021-07-06 15:01:35 +0100 | [diff] [blame] | 129 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 130 | // Get column index |
| 131 | const int kernel_idx = id[3]; |
| 132 | const int kernel_idz = id[4]; |
| 133 | |
| 134 | // Setup pointers |
| 135 | const uint8_t *tmp_input_ptr = in.ptr(); |
| 136 | uint8_t *tmp_output_ptr = dst->ptr_to_element(Coordinates(kernel_idx, 0, kernel_idz)); |
| 137 | const uint8_t *curr_input_row_ptr = tmp_input_ptr; |
| 138 | const uint8_t *curr_input_depth_ptr = tmp_input_ptr; |
| 139 | |
| 140 | // Linearize volume |
| 141 | for (unsigned int d = 0; d < kernel_depth; ++d) |
Manuel Bottini | 29599d0 | 2021-07-06 15:01:35 +0100 | [diff] [blame] | 142 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 143 | for (unsigned int j = 0; j < kernel_size_y; ++j) |
Manuel Bottini | 29599d0 | 2021-07-06 15:01:35 +0100 | [diff] [blame] | 144 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 145 | for (unsigned int i = 0; i < kernel_size_x; ++i) |
| 146 | { |
| 147 | std::memcpy(tmp_output_ptr, tmp_input_ptr, src->info()->element_size()); |
| 148 | tmp_input_ptr += input_stride_x; |
| 149 | tmp_output_ptr += output_stride_y; |
| 150 | } |
| 151 | curr_input_row_ptr += input_stride_y; |
| 152 | tmp_input_ptr = curr_input_row_ptr; |
Manuel Bottini | 29599d0 | 2021-07-06 15:01:35 +0100 | [diff] [blame] | 153 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 154 | curr_input_depth_ptr += input_stride_z; |
| 155 | curr_input_row_ptr = curr_input_depth_ptr; |
| 156 | tmp_input_ptr = curr_input_depth_ptr; |
Manuel Bottini | 29599d0 | 2021-07-06 15:01:35 +0100 | [diff] [blame] | 157 | } |
Manuel Bottini | 29599d0 | 2021-07-06 15:01:35 +0100 | [diff] [blame] | 158 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 159 | // Add bias |
| 160 | if (biases != nullptr) |
| 161 | { |
| 162 | std::memcpy(tmp_output_ptr, biases->ptr_to_element(Coordinates(kernel_idx, kernel_idz)), |
| 163 | src->info()->element_size()); |
| 164 | } |
| 165 | }, |
| 166 | in); |
Manuel Bottini | 29599d0 | 2021-07-06 15:01:35 +0100 | [diff] [blame] | 167 | } |
| 168 | const char *CpuWeightsReshapeKernel::name() const |
| 169 | { |
| 170 | return "CpuWeightsReshapeKernel"; |
| 171 | } |
| 172 | } // namespace kernels |
| 173 | } // namespace cpu |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 174 | } // namespace arm_compute |