Gunes Bayir | e87fa66 | 2023-09-07 12:20:33 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2023 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 | |
| 25 | #include "src/gpu/cl/kernels/helpers/MatMulKernelHelpers.h" |
| 26 | |
| 27 | #include "arm_compute/core/Coordinates.h" |
| 28 | #include "arm_compute/core/utils/helpers/AdjustVecSize.h" |
| 29 | #include "arm_compute/core/utils/math/Math.h" |
| 30 | |
| 31 | #include "src/core/helpers/WindowHelpers.h" |
| 32 | |
| 33 | namespace arm_compute |
| 34 | { |
| 35 | namespace opencl |
| 36 | { |
| 37 | namespace kernels |
| 38 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 39 | Status validate_matmul_input_shapes(const TensorShape &lhs_shape, |
| 40 | const TensorShape &rhs_shape, |
| 41 | const MatMulKernelInfo &matmul_kernel_info) |
Gunes Bayir | e87fa66 | 2023-09-07 12:20:33 +0100 | [diff] [blame] | 42 | { |
| 43 | const size_t lhs_k = matmul_kernel_info.adj_lhs ? lhs_shape.y() : lhs_shape.x(); |
| 44 | const size_t rhs_k = matmul_kernel_info.adj_rhs ? rhs_shape.x() : rhs_shape.y(); |
| 45 | |
| 46 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(lhs_k != rhs_k, "K dimension in Lhs and Rhs matrices must match."); |
| 47 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(lhs_shape.total_size() == 0, "Lhs tensor can't be empty"); |
| 48 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(rhs_shape.total_size() == 0, "Rhs tensor can't be empty"); |
| 49 | |
| 50 | constexpr size_t batch_dim_start = 2; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 51 | for (size_t i = batch_dim_start; i < Coordinates::num_max_dimensions; ++i) |
Gunes Bayir | e87fa66 | 2023-09-07 12:20:33 +0100 | [diff] [blame] | 52 | { |
| 53 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(lhs_shape[i] != rhs_shape[i], "Batch dimension broadcasting is not supported"); |
| 54 | } |
| 55 | |
| 56 | return Status{}; |
| 57 | } |
| 58 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 59 | std::pair<Status, Window> validate_and_configure_window_for_mmul_kernels(const ITensorInfo *lhs, |
| 60 | const ITensorInfo *rhs, |
| 61 | const ITensorInfo *dst, |
| 62 | const MatMulKernelInfo &matmul_kernel_info, |
| 63 | int mmul_m0, |
| 64 | int mmul_n0) |
Gunes Bayir | e87fa66 | 2023-09-07 12:20:33 +0100 | [diff] [blame] | 65 | { |
| 66 | ARM_COMPUTE_UNUSED(lhs, rhs); |
| 67 | |
| 68 | const Window win = calculate_max_window(*dst, Steps(1, 1)); |
| 69 | |
| 70 | // Collapse along the Z direction |
| 71 | // This collapse needs to be here in order to tune the Z dimension of LWS |
| 72 | Window collapsed = win.collapse(win, Window::DimZ); |
| 73 | |
| 74 | // Reconfigure window size, one arm_matrix_multiply call needs 16 threads to finish. |
| 75 | Window::Dimension x_dimension = collapsed.x(); |
| 76 | Window::Dimension y_dimension = collapsed.y(); |
| 77 | |
| 78 | const int m = dst->dimension(1); |
| 79 | const int n = dst->dimension(0); |
| 80 | |
| 81 | const int m0 = std::min(matmul_kernel_info.m0, m); |
| 82 | const int n0 = adjust_vec_size(matmul_kernel_info.n0, n); |
| 83 | |
| 84 | // Make M and N multiple of M0 and N0 respectively |
| 85 | const unsigned int ceil_to_multiple_n_n0 = ceil_to_multiple(n, n0); |
| 86 | const unsigned int ceil_to_multiple_m_m0 = ceil_to_multiple(m, m0); |
| 87 | |
| 88 | // Divide M and N by M0 and N0 respectively |
| 89 | const unsigned int n_div_n0 = ceil_to_multiple_n_n0 / n0; |
| 90 | const unsigned int m_div_m0 = ceil_to_multiple_m_m0 / m0; |
| 91 | |
| 92 | // Make n_div_n0 and m_div_m0 multiple of mmul_n0 and mmul_m0 respectively |
| 93 | const unsigned int ceil_to_multiple_n_div_n0_mmul_n0 = ceil_to_multiple(n_div_n0, mmul_n0); |
| 94 | const unsigned int ceil_to_multiple_m_div_m0_mmul_m0 = ceil_to_multiple(m_div_m0, mmul_m0); |
| 95 | |
| 96 | // Ensure x_dimension is multiple of MMUL block size (mmul_m0 * mmul_n0) |
| 97 | x_dimension.set_end(ceil_to_multiple_n_div_n0_mmul_n0 * mmul_m0); |
| 98 | y_dimension.set_end(ceil_to_multiple_m_div_m0_mmul_m0 / mmul_m0); |
| 99 | |
| 100 | collapsed.set(Window::DimX, x_dimension); |
| 101 | collapsed.set(Window::DimY, y_dimension); |
| 102 | |
| 103 | return std::make_pair(Status{}, collapsed); |
| 104 | } |
| 105 | |
| 106 | } // namespace kernels |
| 107 | } // namespace opencl |
| 108 | } // namespace arm_compute |