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