Elffuss BraidHub

kikocis/kleidiai public

main · 7.4 KiBRawPermalink
1//
2// SPDX-FileCopyrightText: Copyright 2024-2025 Arm Limited and/or its affiliates <open-source-office@arm.com>
3//
4// SPDX-License-Identifier: Apache-2.0
5//
6#pragma once
7
8#include <stddef.h>
9
10#include "kai/kai_common.h"
11
12#ifdef __cplusplus
13extern "C" {
14#endif
15
16/// Get the n step value.
17/// The micro-kernel can process any N values. However, the starting N index to
18/// be processed must be a multiple of n step.
19///
20/// @param[in] nr The number of columns written by the matmul micro-kernel
21///
22/// @return the n step value
23size_t kai_get_n_step_rhs_pack_nxk_qsi4c32p_qsu4c32s1s0(size_t nr);
24
25/// Gets the offset in bytes for the RHS matrix (not packed), which holds
26/// the int4 values in a N x K matrix, where N is number of rows and K is the number of columns.
27///
28/// Two int4 values are stored in one byte.
29/// The lower order part of the byte (low) holds the first nibble (K-index + 0).
30/// The higher order of the byte holds the second nibble (K-index + 1).
31///
32/// @param[in] n_idx Row index in the RHS matrix (not packed).
33/// @param[in] rhs_stride The number of bytes in in each row of the RHS matrix (not packed)
34///
35/// @return the offset in bytes to the RHS matrix (not packed)
36size_t kai_get_rhs_offset_rhs_pack_nxk_qsi4c32p_qsu4c32s1s0(
37 size_t n_idx, //
38 size_t rhs_stride); //
39
40/// Get the row stride in bytes to the packed RHS matrix
41///
42/// @param[in] k The number of columns in the RHS matrix (not packed).
43/// @param[in] nr The number of columns written by the matmul micro-kernel.
44/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.
45/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.
46/// @param[in] bl The block length, which defines the number of K values stored in a single block. It must be a
47/// multiple of 32.
48/// @param[in] scale_dt Block scale data type
49///
50/// @return the stride in bytes to the packed RHS matrix
51size_t kai_get_rhs_packed_stride_rhs_pack_nxk_qsi4c32p_qsu4c32s1s0(
52 size_t k, //
53 size_t nr, //
54 size_t kr, //
55 size_t sr, //
56 size_t bl, //
57 enum kai_datatype scale_dt); //
58
59/// Gets the offset in bytes for the packed RHS matrix.
60///
61/// @param[in] n_idx Row index in the RHS matrix (not packed).
62/// @param[in] k The number of columns in the RHS matrix (not packed).
63/// @param[in] nr The number of columns written by the matmul micro-kernel
64/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.
65/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.
66/// @param[in] bl The block length, which defines the number of K values stored in a single block. It must be a
67/// multiple of 32.
68/// @param[in] scale_dt Block scale data type
69///
70/// @return the offset in bytes to the packed RHS matrix
71size_t kai_get_rhs_packed_offset_rhs_pack_nxk_qsi4c32p_qsu4c32s1s0(
72 size_t n_idx, //
73 size_t k, //
74 size_t nr, //
75 size_t kr, //
76 size_t sr, //
77 size_t bl, //
78 enum kai_datatype scale_dt); //
79
80/// Gets the size in bytes for the quantized and packed RHS matrix.
81///
82/// @param[in] n The number of rows in the RHS matrix (not packed)
83/// @param[in] k The number of columns in the RHS matrix (not packed).
84/// @param[in] nr The number of columns written by the matmul micro-kernel
85/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.
86/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.
87/// @param[in] bl The block length, which defines the number of K values stored in a single block. It must be a multiple
88/// of 32.
89/// @param[in] scale_dt Block scale data type
90///
91/// @return the packed RHS matrix size in bytes
92size_t kai_get_rhs_packed_size_rhs_pack_nxk_qsi4c32p_qsu4c32s1s0(
93 size_t n, //
94 size_t k, //
95 size_t nr, //
96 size_t kr, //
97 size_t sr, //
98 size_t bl, //
99 enum kai_datatype scale_dt); //
100
101/// Runs the RHS packing micro-kernel.
102///
103/// The int4 values are stored in a N x K matrix, where N is number of rows and K is the number of columns.
104/// Two int4 values are stored in one byte. The lower order part of the byte (low) holds
105/// the first nibble (K-index + 0). The higher order of the byte holds the second nibble (K-index + 1).
106///
107/// @param[in] num_groups The number of groups. It must be 1.
108/// @param[in] n The number of rows in the RHS matrix (not packed).
109/// @param[in] k The number of columns in the RHS matrix (not packed).
110/// @param[in] nr The number of columns written by the matmul micro-kernel. It must be a multiple of 4.
111/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.
112/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.
113/// However, kr must be multiple of sr.
114/// @param[in] bl The block length, which defines the number of
115/// K values stored in a single block. It must be a multiple of 32.
116/// @param[in] rhs The RHS matrix containing the 4-bit values.
117/// Size in bytes is expected to be greater than or equal to n * k * (sizeof(uint8_t) / 2).
118/// @param[in] rhs_stride The number of bytes per row in bytes of the RHS matrix
119/// @param[in] bias The biases.
120/// @param[in] scale The per-block quantization scales.
121/// The scale data type must be provided with the params object.
122/// Supported scale data types are FP32, FP16 and BF16.
123/// @param[in] scale_stride The number of bytes per row in bytes of the scale matrix
124/// @param[out] rhs_packed The packed RHS matrix.
125/// @param[in] extra_bytes Extra bytes to append to the end of each row of the packed RHS matrix.
126/// @param[in] params Parameters for the micro-kernel.
127void kai_run_rhs_pack_nxk_qsi4c32p_qsu4c32s1s0(
128 size_t num_groups, //
129 size_t n, //
130 size_t k, //
131 size_t nr, //
132 size_t kr, //
133 size_t sr, //
134 size_t bl, //
135 const uint8_t* rhs, //
136 size_t rhs_stride, //
137 const float* bias, //
138 const void* scale, //
139 size_t scale_stride, //
140 void* rhs_packed, //
141 size_t extra_bytes, //
142 const struct kai_rhs_pack_nxk_qsi4c32p_qsu4c32s1s0_params* params); //
143
144#ifdef __cplusplus
145}
146#endif