Debian Codegraph @

docs/imatmul/README.md

raw
1 <!--
2 SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates <[email protected]>
3
4 SPDX-License-Identifier: Apache-2.0
5 -->
6
7 # How to run the indirect matmul micro-kernels
8
9 The goal of this document is to give an overview of the steps needed to run an
10 indirect `matmul`, denoted `imatmul`, micro-kernel. The example is using the following
11 micro-kernels.
12
13 - `imatmul_clamp_f16_f16p2vlx2_f16p2vlx2_2vlx2vl_sme2_mopa`
14 - `lhs_imatmul_pack_x16p2vlx2_x16p_sme`
15 - `rhs_imatmul_pack_kxn_x16p2vlx2b_x16_x16_sme`
16
17 ## Prerequisites
18
19 To be able to use the micro-kernels in this example you need to have the
20 following includes:
21
22 ```cpp
23 #include "kai_imatmul_clamp_f16_f16p2vlx2_f16p2vlx2_2vlx2vl_sme2_mopa.h"
24 #include "kai_lhs_imatmul_pack_x16p2vlx2_x16p_sme.h"
25 #include "kai_rhs_imatmul_pack_kxn_x16p2vlx2b_x16_x16_sme.h"
26 ```
27
28 ## Background
29
30 The difference between direct `matmul` and the `imatmul` is mainly in the view
31 of the K dimension of the matrix multiplication. The `matmul` doesn't pose any
32 restrictions on the K dimension, whereas the `imatmul` needs the K dimension to
33 be evenly divided into chunks. This is reflected in the API as `k_chunk_count`
34 being the number of chunks, and `k_chunk_length` being the length, in elements,
35 of each chunk.
36
37 Additionally, the left hand side operand is a table of chunk pointers rather
38 than a table of values. This is the indirection part of the `imatmul`.
39
40 ## Use case
41
42 The benefit of using the `imatmul` micro-kernels is that they allow much more
43 efficient representations of convolutions by filters with shapes larger than
44 1×1. For a walk through of this, please refer to the fp16 example of the
45 `imatmul` micro-kernel.
46
47 ## Packing
48
49 The major difference between the normal `matmul`, and the `imatmul` flow is the
50 left hand side packing.
51
52 ### Left hand side packing
53
54 The rows of the left hand side matrix are split into chunks, where each chunk is
55 `k_chunk_length` number of values, and on each row there are `k_chunk_count`
56 number of chunks. This will be referred to by an indirection table, a table of
57 pointers, where each row will refer to a column of `m_step` chunks, as
58 illustrated by the below example.
59
60 The left hand side packing micro-kernel operates on an indirection table, a table of
61 pointers to chunks, where each chunk contains `k_chunk_length` number of values.
62 The layout of this table is a bit special, to allow linear memory access of the
63 table entries. The memory layout is a row major table, where each row has M-step
64 number pointers. This is illustrated in the figure below.
65
66 ![indirection table](imgs/lhs_igemm.png)
67
68 The indirection table can use two types of pointers, chunk pointers and padding
69 pointers. The difference between these two pointers is that the function
70 argument `lhs_ptr_offset` will not be added to padding pointers. The reason for
71 this distinction is that padding chunks can live outside of the left hand side
72 matrix, and you can use the base address of the left hand side matrix as
73 `lhs_ptr_offset` with `lhs_ptrs` being a table of indices rather than pointers.
74
75 A simple flow of invoking the left hand side packing could look like the
76 following.
77
78 The following values are used to describe the input data.
79
80 ```cpp
81 /* Values used for the LHS packing */
82 size_t m, k_chunk_count, k_chunk_length;
83 float16_t *lhs_ptr; // matrix of m * (k_chunk_count * k_chunk_length) values
84 ```
85
86 Using the symbols above you can populate the indirection table using something
87 like the below code.
88
89 ```cpp
90 /* Indirection table setup */
91 const size_t m_step = kai_get_m_step_lhs_imatmul_pack_x16p2vlx2_x16p_sme();
92 const size_t itable_rows = k_chunk_count * round_up_division(m, m_step);
93 const size_t itable_cols = m_step;
94
95 /* Allocate the indirection table */
96 float16_t **const itable = new float16_t *[itable_rows * itable_cols];
97
98 /* Populate the indirection table */
99 size_t chunk = 0;
100 for (size_t itable_block = 0; itable_block < itable_rows; itable_block += k_chunk_count) {
101 for (size_t block_col = 0; block_col < itable_cols; block_col += 1) {
102 for (size_t block_row = 0; block_row < k_chunk_count; block_row += 1) {
103 /* Note that this will set values for all entries, even unused entries */
104 const size_t idx = (itable_block + block_row) * itable_cols + block_col;
105 itable[idx] = lhs_ptr + k_chunk_length * chunk++;
106 }
107 }
108 }
109 ```
110
111 Using the indirection table above, you can then invoke the left hand side
112 packing micro-kernel using.
113
114 ```cpp
115 const size_t lhs_packed_size = kai_get_lhs_packed_size_lhs_imatmul_pack_x16p2vlx2_x16p_sme(m, k_chunk_count, k_chunk_length);
116 std::byte *lhs_packed = new std::byte[lhs_packed_size];
117 kai_run_lhs_imatmul_pack_x16p2vlx2_x16p_sme(m, k_chunk_count, k_chunk_length, itable, 0, nullptr, lhs_packed);
118 ```
119
120 ### Right hand side packing
121
122 The right hand side packing for `imatmul` is very similar to the normal right
123 hand side packing. The difference is that the resulting layout will be suitable
124 for the layout used by the left hand side packing. Similar to the left hand side
125 packing the right hand side packing also takes `k_chunk_count` and
126 `k_chunk_length` arguments.
127
128 Same as for left hand side, set up values describing the input data.
129
130 ```cpp
131 size_t n, k_chunk_count, k_chunk_length;
132 float16_t *rhs; // Matrix of (k_chunk_count * k_chunk_length) * n values
133 float16_t *bias; // vector of n values
134 ```
135
136 Then allocate output buffer, and invoke the right hand side packing
137 micro-kernel.
138
139 ```C++
140 const size_t rhs_packed_size = kai_get_rhs_packed_size_rhs_imatmul_pack_kxn_x16p2vlx2b_x16_x16_sme(n, k_chunk_count, k_chunk_length);
141 std::byte *rhs_packed = new std::byte[rhs_packed_size];
142 kai_run_rhs_imatmul_pack_kxn_x16p2vlx2b_x16_x16_sme(n, k_chunk_count, k_chunk_length, n * sizeof(float16_t), bias, rhs_packed);
143 ```
144
145 ## `imatmul`
146
147 Once the input data has been pack, as per description above the next step is
148 simply to invoke the `imatmul` micro-kernel.
149
150 Similarly to the invocations above, you need some parameters representing your
151 input. You need to allocate memory for output, and you need to invoke the
152 `imatmul` micro-kernel.
153
154 ```cpp
155 size_t m, n, k_chunk_count, k_chunk_length;
156
157 const size_t dst_size = kai_get_dst_size_imatmul_clamp_f16_f16p2vlx2_f16p2vlx2_2vlx2vl_sme2_mopa(m, n);
158 float16_t* dst = new float16_t[dst_size / sizeof(float16_t)];
159
160 kai_run_imatmul_clamp_f16_f16p2vlx2_f16p2vlx2_2vlx2vl_sme2_mopa(m, n, k_chunk_count, k_chunk_length,
161 lhs_packed, rhs_packed, dst,
162 m * sizeof(float16_t), -1.0f, 1.0f);
163 ```