Elffuss BraidHub

kikocis/kleidiai public

main · 5.1 KiBRawPermalink
1//
2// SPDX-FileCopyrightText: Copyright 2024 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#define kai_rhs_pack_nxk_qsi4cxp_qs4cxs1s0_params kai_rhs_pack_qs4cxs1s0_param
18
19/// Get the n step value.
20/// The micro-kernel can process any N values. However, the starting N index to
21/// be processed must be a multiple of n step.
22///
23/// @param[in] nr The number of columns written by the matmul micro-kernel
24///
25/// @return the n step value
26size_t kai_get_n_step_rhs_pack_nxk_qsi4cxp_qs4cxs1s0(size_t nr);
27
28/// Gets the offset in bytes for the RHS matrix (not packed).
29///
30/// @note The int4 values are stored in a N x K matrix. Two int4 values are stored in one byte.
31/// The lower order part of the byte (low) holds the first nibble (K-index + 0).
32/// The higher order of the byte holds the second nibble (K-index + 1).
33///
34/// @param[in] n_idx Row index in the RHS matrix (not packed). It must be a multiple of n_step.
35/// @param[in] rhs_stride The number of bytes in in each row of the RHS matrix (not packed)
36///
37/// @return the offset in bytes to the RHS matrix (not packed)
38size_t kai_get_rhs_offset_rhs_pack_nxk_qsi4cxp_qs4cxs1s0(size_t n_idx, size_t rhs_stride);
39
40/// Get the row stride in bytes to the packed RHS matrix
41///
42/// @param[in] k In the RHS matrix (not packed), K is the number of columns.
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///
47/// @return the stride in bytes to the packed RHS matrix
48size_t kai_get_rhs_packed_stride_rhs_pack_nxk_qsi4cxp_qs4cxs1s0(size_t k, size_t nr, size_t kr, size_t sr);
49
50/// Gets the offset in bytes for the packed RHS matrix, which contains the packed 4-bit quantized symmetric per-channel
51/// (qsu4cx) values.
52///
53/// @param[in] n_idx Row index in the RHS matrix (not packed). It must be a multiple of n_step.
54/// @param[in] k In the RHS matrix (not packed), K is the number of columns.
55/// @param[in] nr The number of columns written by the matmul micro-kernel
56/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.
57/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.
58///
59/// @return the offset in bytes to the packed RHS matrix
60size_t kai_get_rhs_packed_offset_rhs_pack_nxk_qsi4cxp_qs4cxs1s0(
61 size_t n_idx, size_t k, size_t nr, size_t kr, size_t sr);
62
63/// @brief Gets the size in bytes for the packed RHS matrix
64///
65/// @param[in] n The number of rows in the RHS matrix (not packed)
66/// @param[in] k The number of columns in the RHS matrix (not packed).
67/// @param[in] nr The number of columns written by the matmul micro-kernel
68/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel.
69/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.
70///
71/// @return the packed RHS matrix size in bytes
72size_t kai_get_rhs_packed_size_rhs_pack_nxk_qsi4cxp_qs4cxs1s0(size_t n, size_t k, size_t nr, size_t kr, size_t sr);
73
74/// Run the micro-kernel to pack the RHS matrix.
75///
76/// @note The int4 values are stored in a N x K matrix. Two int4 values are stored in one byte.
77/// The lower order part of the byte (low) holds the first nibble (K-index + 0).
78/// The higher order of the byte holds the second nibble (K-index + 1).
79///
80/// @param[in] num_groups The number of groups. It must be 1.
81/// @param[in] n The number of rows.
82/// @param[in] k The common dimension between the LHS and RHS matrix (K). It must be an even value.
83/// @param[in] nr The number of N rows to interleave on the same output output row.
84/// @param[in] kr The number of K values loaded in the single inner most loop of the matmul micro-kernel.
85/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr.
86/// However, kr must be multiple of sr.
87/// @param[in] rhs The RHS matrix containing the 4-bit values.
88/// Size in bytes is expected to be greater than or equal to n * k * (sizeof(uint8_t) / 2).
89/// @param[in] bias The biases.
90/// @param[in] scale The scale for each output channel.
91/// @param[out] rhs_packed The packed RHS matrix.
92/// @param[in] extra_bytes Extra bytes to append to the end of each row of the packed RHS matrix.
93/// @param[in] params Parameters for the micro-kernel.
94void kai_run_rhs_pack_nxk_qsi4cxp_qs4cxs1s0(
95 size_t num_groups, //
96 size_t n, //
97 size_t k, //
98 size_t nr, //
99 size_t kr, //
100 size_t sr, //
101 const uint8_t* rhs, //
102 const float* bias, //
103 const float* scale, //
104 void* rhs_packed, //
105 size_t extra_bytes, //
106 const struct kai_rhs_pack_nxk_qsi4cxp_qs4cxs1s0_params* params);
107
108#ifdef __cplusplus
109}
110#endif