Michele Di Giorgio | 93b75e0 | 2021-06-21 12:00:43 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016-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/CpuGemmTranspose1xWKernel.h" |
Michele Di Giorgio | 93b75e0 | 2021-06-21 12:00:43 +0100 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/ITensor.h" |
| 27 | #include "arm_compute/core/Validate.h" |
| 28 | #include "arm_compute/core/Window.h" |
| 29 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
| 30 | #include "src/core/helpers/AutoConfiguration.h" |
| 31 | #include "src/core/helpers/WindowHelpers.h" |
| 32 | |
| 33 | #include <arm_neon.h> |
| 34 | |
| 35 | namespace arm_compute |
| 36 | { |
| 37 | namespace cpu |
| 38 | { |
| 39 | namespace kernels |
| 40 | { |
| 41 | using namespace arm_compute::misc::shape_calculator; |
| 42 | |
| 43 | void CpuGemmTranspose1xWKernel::configure(const ITensorInfo *src, ITensorInfo *dst) |
| 44 | { |
| 45 | ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst); |
| 46 | |
| 47 | // Output tensor auto inizialitation if not yet initialized |
| 48 | auto_init_if_empty(*dst, src->clone()->set_tensor_shape(compute_transpose1xW_with_element_size_shape(*src))); |
| 49 | |
| 50 | // Perform validate step |
| 51 | ARM_COMPUTE_ERROR_THROW_ON(CpuGemmTranspose1xWKernel::validate(src, dst)); |
| 52 | |
| 53 | const size_t vector_size = 16 / src->element_size(); |
| 54 | |
| 55 | // Configure kernel window |
| 56 | Window win = calculate_max_window(*src, Steps(vector_size)); |
| 57 | ICPPKernel::configure(win); |
| 58 | } |
| 59 | |
| 60 | Status CpuGemmTranspose1xWKernel::validate(const ITensorInfo *src, const ITensorInfo *dst) |
| 61 | { |
| 62 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src); |
| 63 | ARM_COMPUTE_RETURN_ERROR_ON(src->data_type() == DataType::UNKNOWN); |
| 64 | //Note: ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(src) is not needed here as this kernel doesn't use CPU FP16 instructions. |
| 65 | |
| 66 | if(dst->total_size() != 0) |
| 67 | { |
| 68 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(dst->tensor_shape(), compute_transpose1xW_with_element_size_shape(*src)); |
| 69 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst); |
| 70 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(src, dst); |
| 71 | } |
| 72 | |
| 73 | return Status{}; |
| 74 | } |
| 75 | |
| 76 | void CpuGemmTranspose1xWKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info) |
| 77 | { |
| 78 | ARM_COMPUTE_UNUSED(info); |
| 79 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 80 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 81 | ARM_COMPUTE_ERROR_ON(tensors.empty()); |
| 82 | |
| 83 | /* |
| 84 | * Following an example of how the transposition1xW works when the src data type is F32 |
| 85 | * |
| 86 | * |a00 a01 a02 a03| |
| 87 | * |a10 a11 a12 a13| |
| 88 | * |a20 a21 a22 a23| = | a00 a01 a02 a03 || a10 a11 a12 a13 || a20 a21 a22 a23 || a30 a31 a32 a33 | |
| 89 | * |a30 a31 a32 a33| |
| 90 | * |
| 91 | * The dst matrix will have the following shape: [ height * W, ceil(width / W) ], where W = (16 / element size of the tensor) |
| 92 | */ |
| 93 | |
| 94 | // Set window for dst tensor. Set to 0 the X and Y dimensions in order to allow multi-threading implementation and future batched matrix multiplications |
| 95 | Window win_out(window); |
| 96 | win_out.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 97 | win_out.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 98 | |
| 99 | const ITensor *src = tensors.get_const_tensor(TensorType::ACL_SRC); |
| 100 | ITensor *dst = tensors.get_tensor(TensorType::ACL_DST); |
| 101 | |
| 102 | Iterator in(src, window); |
| 103 | Iterator out(dst, win_out); |
| 104 | |
| 105 | const size_t in_width = src->info()->dimension(0); |
| 106 | const size_t element_size = src->info()->element_size(); |
| 107 | const size_t out_stride = dst->info()->strides_in_bytes()[1]; |
| 108 | const size_t vector_size = 16 / element_size; |
| 109 | |
| 110 | execute_window_loop(window, [&](const Coordinates & id) |
| 111 | { |
| 112 | const uint8_t *in_ptr = in.ptr(); |
| 113 | uint8_t *const out_ptr = out.ptr() + (id.y() * vector_size) * element_size + (id.x() / vector_size) * out_stride; |
| 114 | |
| 115 | for(size_t k = 0; k < vector_size; ++k) |
| 116 | { |
| 117 | // If the src width is not multiple of W, we fill the reference with 0s |
| 118 | if((id.x() + k) >= in_width) |
| 119 | { |
| 120 | std::memset(out_ptr + k * element_size, 0, element_size); |
| 121 | } |
| 122 | else |
| 123 | { |
| 124 | std::memcpy(out_ptr + k * element_size, in_ptr + k * element_size, element_size); |
| 125 | } |
| 126 | } |
| 127 | }, |
| 128 | in, out); |
| 129 | } |
| 130 | |
| 131 | const char *CpuGemmTranspose1xWKernel::name() const |
| 132 | { |
| 133 | return "CpuGemmTranspose1xWKernel"; |
| 134 | } |
| 135 | } // namespace kernels |
| 136 | } // namespace cpu |
| 137 | } // namespace arm_compute |