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