Elffuss BraidHub

kikocis/kleidiai public

main · 5.8 KiBRawPermalink
1//
2// SPDX-FileCopyrightText: Copyright 2024-2026 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#include <stdint.h>
10
11#include "kai/kai_common.h"
12
13#ifdef __cplusplus
14extern "C" {
15#endif
16
17/// Gets the row stride in bytes to the packed RHS matrix.
18///
19/// @param[in] k The number of columns in the RHS matrix (not packed).
20/// @param[in] nr The number of columns written by the matmul micro-kernel.
21/// @param[in] kr The number of columns loaded in the single innermost loop of the matmul micro-kernel.
22/// @param[in] bl The block length, which defines the number of K values stored in a single block. It must be a multiple
23/// of 32.
24///
25/// @return Row stride in bytes to the packed RHS matrix.
26size_t kai_get_rhs_packed_stride_rhs_pack_nxk_qsi4c32pscalef16_qsu4c32s16s0(size_t k, size_t nr, size_t kr, size_t bl);
27
28/// Gets the offset in bytes for the RHS matrix (not packed), which holds
29/// the int4 values in a N x K matrix, where N is number of rows and K is the number of columns.
30///
31/// Two int4 K values are stored in one byte. These values are stored in blocks, where each block
32/// has it own scale factor. The scale factor is expected to be a f16 value and stored at the beginning of each block.
33/// The first byte in the block holds the K-index + 0 and K-index + 16 values.
34/// The K-index + 0 value is stored in the lower order part of the byte (low nibble) while
35/// the K-index + 16 value is stored in the higher order part (high nibble).
36/// For example, if the block length is 32, the values are stored in the following order:
37/// |byte(s16, s0),byte(s17, s1),byte(s18, s2),...,byte(s31, s15),float16(scale)|
38///
39/// @param[in] n_idx Row index in the RHS matrix (not packed).
40/// @param[in] rhs_stride The number of bytes in in each row of the RHS matrix (not packed)
41///
42/// @return the offset in bytes to the RHS matrix (not packed)
43size_t kai_get_rhs_offset_rhs_pack_nxk_qsi4c32pscalef16_qsu4c32s16s0(
44 size_t n_idx, //
45 size_t rhs_stride); //
46
47/// Gets the offset in bytes for the packed RHS matrix.
48///
49/// @param[in] n_idx Row index in the RHS matrix (not packed).
50/// @param[in] k The common dimension between the LHS and RHS matrix (K)
51/// @param[in] nr The number of columns written by the matmul micro-kernel
52/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.
53/// @param[in] bl The block length, which defines the number of K values stored in a single block. It must be a
54/// multiple of 32.
55///
56/// @return the offset in bytes to the packed RHS matrix
57size_t kai_get_rhs_packed_offset_rhs_pack_nxk_qsi4c32pscalef16_qsu4c32s16s0(
58 size_t n_idx, //
59 size_t k, //
60 size_t nr, //
61 size_t kr, //
62 size_t bl); //
63
64/// Gets the size in bytes for the quantized and packed RHS matrix.
65///
66/// @param[in] n The number of rows in the RHS matrix (not packed)
67/// @param[in] k The number of columns in the RHS matrix (not packed).
68/// @param[in] nr The number of columns written by the matmul micro-kernel
69/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.
70/// @param[in] bl The block length, which defines the number of K values stored in a single block. It must be a multiple
71/// of 32.
72///
73/// @return the packed RHS matrix size in bytes
74size_t kai_get_rhs_packed_size_rhs_pack_nxk_qsi4c32pscalef16_qsu4c32s16s0(
75 size_t n, //
76 size_t k, //
77 size_t nr, //
78 size_t kr, //
79 size_t bl); //
80
81/// Runs the RHS packing micro-kernel.
82///
83/// The int4 values are stored in a N x K matrix, where N is number of rows and K is the number of columns.
84/// Two int4 values are stored in one byte. The lower order part of the byte (low) holds
85/// the first nibble (K-index + 0). The higher order of the byte holds the second nibble (K-index + 1).
86///
87/// @param[in] num_groups The number of groups. It must be 1.
88/// @param[in] n The number of columns of the output matrix (N).
89/// @param[in] k The common dimension between the LHS and RHS matrix (K).
90/// @param[in] nr The number of columns written by the matmul micro-kernel.
91/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.
92/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.
93/// However, kr must be multiple of sr.
94/// @param[in] bl The block length, which defines the number of
95/// K values stored in a single block. It must be a multiple of 32.
96/// @param[in] rhs The RHS matrix containing the 4-bit values.
97/// Size in bytes is expected to be greater than or equal to n * k * (sizeof(uint8_t) / 2).
98/// @param[in] bias The biases.
99/// @param[out] rhs_packed The packed RHS matrix.
100/// @param[in] extra_bytes Extra bytes to append to the end of each row of the packed RHS matrix.
101/// @param[in] params Parameters for the micro-kernel.
102void kai_run_rhs_pack_nxk_qsi4c32pscalef16_qsu4c32s16s0(
103 size_t num_groups, //
104 size_t n, //
105 size_t k, //
106 size_t nr, //
107 size_t kr, //
108 size_t sr, //
109 size_t bl, //
110 const uint8_t* rhs, //
111 const float* bias, //
112 void* rhs_packed, //
113 size_t extra_bytes, //
114 const struct kai_rhs_pack_qs4cxs1s0_param* params); //
115
116#ifdef __cplusplus
117}
118#endif