diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b5d8567c650d7e3dc6bce5cb210fc5e30fd60ae..3c9634682f1d2dceca2e102095bab97861bbc62f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,8 @@ KleidiAI follows the [Semantic Versioning](https://semver.org/) specification fo - Matrix multiplication (1xN) Micro-kernels of QSI8D32 LHS and QAI4C32 RHS with F32 output, optimized for FEAT_DotProd. - Matrix multiplication (MxN) Micro-kernels of QSI8D32 LHS and QAI4C32 RHS with F16 output, optimized for FEAT_I8MM. - Matrix multiplication (1xN) Micro-kernels of QSI8D32 LHS and QAI4C32 RHS with F16 output, optimized for FEAT_DotProd. + - Matrix multiplication (MxN) Micro-kernels of QSI8DX LHS and QAI4CX RHS with F32 output, optimized for FEAT_I8MM. + - Matrix multiplication (1xN) Micro-kernels of QSI8DX LHS and QAI4CX RHS with F32 output, optimized for FEAT_DotProd. ## v1.6.0 diff --git a/CMakeLists.txt b/CMakeLists.txt index 8e0c713024da474aadb962f705def01a39e46ffa..9acfeac1fb1dc0db10ccf99b35fd3e037866d104 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -137,13 +137,15 @@ set(KLEIDIAI_FILES_NEON kai/ukernels/matmul/matmul_clamp_f32_f32_f32p/kai_matmul_clamp_f32_f32_f32p8x1biasf32_6x8x4_neon_mla.c kai/ukernels/matmul/matmul_clamp_f32_f32_f32p/kai_matmul_clamp_f32_f32_f32p8x1biasf32_6x8x4_neon_mla_asm.S kai/ukernels/matmul/pack/kai_lhs_quant_pack_qsi8d32p_f32_neon.c + kai/ukernels/matmul/pack/kai_lhs_quant_pack_qsi8d32pscalef32_f32_neon.c + kai/ukernels/matmul/pack/kai_lhs_quant_pack_qsi8dxpscalef32_f32_neon.c kai/ukernels/matmul/pack/kai_rhs_pack_kxn_f32p8x1biasf32_f32_f32_neon.c kai/ukernels/matmul/pack/kai_rhs_pack_kxn_qsi8cxp_qsi8cx_neon.c kai/ukernels/matmul/pack/kai_rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon.c kai/ukernels/matmul/pack/kai_rhs_pack_nxk_qsi4cxps1s0_qsu4cxs1s0_neon.c kai/ukernels/matmul/pack/kai_rhs_pack_nxk_qsi8cxp_qsi8cx_neon.c kai/ukernels/matmul/pack/kai_rhs_pack_nxk_qai4c32p_qau4c32s0s1_f32_f32_f32_neon.c - kai/ukernels/matmul/pack/kai_lhs_quant_pack_qsi8d32pscalef32_f32_neon.c + kai/ukernels/matmul/pack/kai_rhs_pack_nxk_qai4cxp_qau4cxs1s0_f32_f32_f32_neon.c ) set(KLEIDIAI_FILES_NEON_DOTPROD_ASM @@ -163,6 +165,8 @@ set(KLEIDIAI_FILES_NEON_DOTPROD_ASM kai/ukernels/matmul/matmul_clamp_f32_qai8dxp_qsi4c32p/kai_matmul_clamp_f32_qai8dxp4x4_qsi4c32p8x4_4x8_neon_dotprod_asm.S kai/ukernels/matmul/matmul_clamp_f32_qsi8d32p_qai4c32p/kai_matmul_clamp_f32_qsi8d32p1x8_qai4c32p4x8_1x4_neon_dotprod_asm.S kai/ukernels/matmul/matmul_clamp_f32_qsi8d32p_qai4c32p/kai_matmul_clamp_f32_qsi8d32p1x8_qai4c32p4x8_1x4_neon_dotprod.c + kai/ukernels/matmul/matmul_clamp_f32_qsi8dxp_qai4cxp/kai_matmul_clamp_f32_qsi8dxp1x8_qai4cxp4x8_1x4_neon_dotprod_asm.S + kai/ukernels/matmul/matmul_clamp_f32_qsi8dxp_qai4cxp/kai_matmul_clamp_f32_qsi8dxp1x8_qai4cxp4x8_1x4_neon_dotprod.c ) set(KLEIDIAI_FILES_NEON_DOTPROD @@ -191,6 +195,8 @@ set(KLEIDIAI_FILES_NEON_I8MM_ASM kai/ukernels/matmul/matmul_clamp_f32_qai8dxp_qsi4c32p/kai_matmul_clamp_f32_qai8dxp4x8_qsi4c32p8x8_4x8x32_neon_i8mm_asm.S kai/ukernels/matmul/matmul_clamp_f32_qsi8d32p_qai4c32p/kai_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm_asm.S kai/ukernels/matmul/matmul_clamp_f32_qsi8d32p_qai4c32p/kai_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm.c + kai/ukernels/matmul/matmul_clamp_f32_qsi8dxp_qai4cxp/kai_matmul_clamp_f32_qsi8dxp4x8_qai4cxp4x8_8x4_neon_i8mm_asm.S + kai/ukernels/matmul/matmul_clamp_f32_qsi8dxp_qai4cxp/kai_matmul_clamp_f32_qsi8dxp4x8_qai4cxp4x8_8x4_neon_i8mm.c ) set(KLEIDIAI_FILES_NEON_I8MM @@ -369,6 +375,7 @@ if(KLEIDIAI_BUILD_TESTS) test/tests/matmul_clamp_f32_qsi8d32p_qai4c32p_test.cpp test/tests/matmul_clamp_f16_qsi8d32p_qai4c32p_test.cpp test/tests/matmul_clamp_qai8_qai8p_qsi8cxp_test.cpp + test/tests/matmul_clamp_f32_qsi8dxp_qai4cxp_test.cpp test/tests/matmul_test.cpp ) diff --git a/kai/ukernels/matmul/BUILD.bazel b/kai/ukernels/matmul/BUILD.bazel index 79bfac8b50910ed212b0d13f1a9b3378e82b0394..ba1dc5bd245be89c7513cdc83d89cbab7d040af1 100644 --- a/kai/ukernels/matmul/BUILD.bazel +++ b/kai/ukernels/matmul/BUILD.bazel @@ -30,10 +30,12 @@ SCALAR_KERNELS = [ NEON_KERNELS = [ "pack/kai_lhs_quant_pack_qsi8d32p_f32_neon", "pack/kai_lhs_quant_pack_qsi8d32pscalef32_f32_neon", + "pack/kai_lhs_quant_pack_qsi8dxpscalef32_f32_neon", "pack/kai_rhs_pack_kxn_f32p8x1biasf32_f32_f32_neon", "pack/kai_rhs_pack_kxn_qsi4c32p_qsu4c32s1s0", "pack/kai_rhs_pack_kxn_qsi8cxp_qsi8cx_neon", "pack/kai_rhs_pack_nxk_qai4c32p_qau4c32s0s1_f32_f32_f32_neon", + "pack/kai_rhs_pack_nxk_qai4cxp_qau4cxs1s0_f32_f32_f32_neon", "pack/kai_rhs_pack_nxk_qsi4c32p_qsu4c32s1s0", "pack/kai_rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon", "pack/kai_rhs_pack_nxk_qsi4c32pscalef16_qsu4c32s16s0", @@ -105,6 +107,7 @@ DOTPROD_KERNELS_ASM = [ "matmul_clamp_f32_qai8dxp_qsi4c32p/kai_matmul_clamp_f32_qai8dxp4x4_qsi4c32p4x4_16x4_neon_dotprod", "matmul_clamp_f32_qai8dxp_qsi4c32p/kai_matmul_clamp_f32_qai8dxp4x4_qsi4c32p8x4_4x8_neon_dotprod", "matmul_clamp_f32_qsi8d32p_qai4c32p/kai_matmul_clamp_f32_qsi8d32p1x8_qai4c32p4x8_1x4_neon_dotprod", + "matmul_clamp_f32_qsi8dxp_qai4cxp/kai_matmul_clamp_f32_qsi8dxp1x8_qai4cxp4x8_1x4_neon_dotprod", ] # buildifier: keep sorted @@ -125,6 +128,7 @@ I8MM_KERNELS_ASM = [ "matmul_clamp_f32_qai8dxp_qsi4c32p/kai_matmul_clamp_f32_qai8dxp4x8_qsi4c32p8x8_4x8_neon_i8mm", "matmul_clamp_f32_qai8dxp_qsi4c32p/kai_matmul_clamp_f32_qai8dxp4x8_qsi4c32p8x8_4x8x32_neon_i8mm", "matmul_clamp_f32_qsi8d32p_qai4c32p/kai_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm", + "matmul_clamp_f32_qsi8dxp_qai4cxp/kai_matmul_clamp_f32_qsi8dxp4x8_qai4cxp4x8_8x4_neon_i8mm", ] # buildifier: keep sorted diff --git a/kai/ukernels/matmul/matmul_clamp_f32_qsi8dxp_qai4cxp/kai_matmul_clamp_f32_qsi8dxp1x8_qai4cxp4x8_1x4_neon_dotprod.c b/kai/ukernels/matmul/matmul_clamp_f32_qsi8dxp_qai4cxp/kai_matmul_clamp_f32_qsi8dxp1x8_qai4cxp4x8_1x4_neon_dotprod.c new file mode 100644 index 0000000000000000000000000000000000000000..a0fa31ef395dc9e7f89a5deb3c5d61543f760e60 --- /dev/null +++ b/kai/ukernels/matmul/matmul_clamp_f32_qsi8dxp_qai4cxp/kai_matmul_clamp_f32_qsi8dxp1x8_qai4cxp4x8_1x4_neon_dotprod.c @@ -0,0 +1,162 @@ +// +// SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates +// +// SPDX-License-Identifier: Apache-2.0 +// +#if !defined(__aarch64__) && !defined(__ARM_FEATURE_DOTPROD) && !defined(_M_ARM64) +#error "Dotprod extension required to compile this micro-kernel" +#else // Architectural features check. + +#include "kai_matmul_clamp_f32_qsi8dxp1x8_qai4cxp4x8_1x4_neon_dotprod.h" + +#include + +#include "kai/kai_common.h" + +typedef struct { + float* dst; + const void* lhs_packed; + const void* rhs_packed; + const float* clamp_vals; + size_t dst_stride_row; + size_t m; + size_t n; + size_t num_blocks; +} KernelArgs; + +void kai_kernel_matmul_clamp_f32_qsi8dxp1x8_qai4cxp4x8_1x4_neon_dotprod(KernelArgs* args_ptr); + +// Compute args +static const size_t kai_m_step = 1; +static const size_t kai_n_step = 4; +// Packing args +static const size_t kai_mr = 1; +static const size_t kai_nr = 4; +static const size_t kai_kr = 16; +static const size_t kai_sr = 2; +// LHS format args (num. bytes per value, multiplier, zero_point (if asymmetric)) +static const size_t kai_num_bytes_qvalue_lhs = 1; +static const size_t kai_num_bytes_multiplier_lhs = 4; +static const size_t kai_num_bytes_sum_lhs = 4; +// RHS format args (num. bytes per value, multiplier, zero_point (if asymmetric), and reduction sum (if LHS is +// asymmetric)) +static const size_t kai_num_bytes_recip_qvalue_rhs = 2; +static const size_t kai_num_bytes_multiplier_rhs = 4; +static const size_t kai_num_bytes_offset_rhs = 4; + +// DST format args +static const size_t kai_num_bytes_dst_value = 4; +// Extra args +static const size_t kai_num_bytes_bias = 4; +static const size_t kai_k_multiple_of = 32; + +static const size_t kai_bl = 32; + +inline static size_t kai_get_k_roundedup(size_t k) { + return kai_roundup(k, kai_k_multiple_of); +} + +inline static size_t kai_get_lhs_packed_stride(size_t k) { + const size_t k_internal = kai_get_k_roundedup(k); + size_t lhs_packed_stride = + kai_mr * ((k_internal * kai_num_bytes_qvalue_lhs) + kai_num_bytes_multiplier_lhs + kai_num_bytes_sum_lhs); + + return lhs_packed_stride; +} + +inline static size_t kai_get_rhs_packed_stride(size_t k) { + const size_t k_internal = kai_get_k_roundedup(k); + size_t rhs_packed_stride = kai_nr * (k_internal / kai_num_bytes_recip_qvalue_rhs); + + rhs_packed_stride += kai_nr * kai_num_bytes_multiplier_rhs; + // Since the bias is packed with the RHS matrix, the stride is adjusted with the number of bytes of the bias + rhs_packed_stride += kai_nr * kai_num_bytes_bias; + rhs_packed_stride += kai_nr * kai_num_bytes_offset_rhs; + + return rhs_packed_stride; +} + +size_t kai_get_m_step_matmul_clamp_f32_qsi8dxp1x8_qai4cxp4x8_1x4_neon_dotprod(void) { + return kai_m_step; +} + +size_t kai_get_n_step_matmul_clamp_f32_qsi8dxp1x8_qai4cxp4x8_1x4_neon_dotprod(void) { + return kai_n_step; +} + +size_t kai_get_mr_matmul_clamp_f32_qsi8dxp1x8_qai4cxp4x8_1x4_neon_dotprod(void) { + return kai_mr; +} + +size_t kai_get_nr_matmul_clamp_f32_qsi8dxp1x8_qai4cxp4x8_1x4_neon_dotprod(void) { + return kai_nr; +} + +size_t kai_get_kr_matmul_clamp_f32_qsi8dxp1x8_qai4cxp4x8_1x4_neon_dotprod(void) { + return kai_kr; +} + +size_t kai_get_sr_matmul_clamp_f32_qsi8dxp1x8_qai4cxp4x8_1x4_neon_dotprod(void) { + return kai_sr; +} + +size_t kai_get_lhs_packed_offset_matmul_clamp_f32_qsi8dxp1x8_qai4cxp4x8_1x4_neon_dotprod(size_t m_idx, size_t k) { + KAI_ASSUME((m_idx % kai_m_step) == 0); + + return (m_idx / kai_mr) * kai_get_lhs_packed_stride(k); +} + +size_t kai_get_rhs_packed_offset_matmul_clamp_f32_qsi8dxp1x8_qai4cxp4x8_1x4_neon_dotprod(size_t n_idx, size_t k) { + KAI_ASSUME((n_idx % kai_n_step) == 0); + + return (n_idx / kai_nr) * kai_get_rhs_packed_stride(k); +} + +size_t kai_get_dst_offset_matmul_clamp_f32_qsi8dxp1x8_qai4cxp4x8_1x4_neon_dotprod( + size_t m_idx, size_t n_idx, size_t dst_stride) { + KAI_ASSUME((m_idx % kai_m_step) == 0); + KAI_ASSUME((n_idx % kai_n_step) == 0); + + return (n_idx * kai_num_bytes_dst_value) + m_idx * dst_stride; +} + +size_t kai_get_dst_size_matmul_clamp_f32_qsi8dxp1x8_qai4cxp4x8_1x4_neon_dotprod(size_t m, size_t n) { + return m * n * kai_num_bytes_dst_value; +} + +void kai_run_matmul_clamp_f32_qsi8dxp1x8_qai4cxp4x8_1x4_neon_dotprod( + size_t m, // + size_t n, // + size_t k, // + const void* restrict lhs_packed, // + const void* restrict rhs_packed, // + float* restrict dst, // NOLINT(readability-non-const-parameter) + size_t dst_stride_row, // + size_t dst_stride_col, // + float scalar_min, // + float scalar_max) { + KAI_ASSUME(dst_stride_col == sizeof(float)); + KAI_ASSUME(m == 1); + if (m == 0) { + return; + } + + const size_t k_internal = kai_get_k_roundedup(k); + const size_t num_blocks = k_internal / kai_bl; + const float clamp_vals[2] = {scalar_min, scalar_max}; + + KernelArgs args; + + args.dst = dst; + args.lhs_packed = lhs_packed; + args.rhs_packed = rhs_packed; + args.clamp_vals = clamp_vals; + args.dst_stride_row = dst_stride_row; + args.m = m; + args.n = n; + args.num_blocks = num_blocks; + + kai_kernel_matmul_clamp_f32_qsi8dxp1x8_qai4cxp4x8_1x4_neon_dotprod(&args); +} + +#endif // Architectural features check. diff --git a/kai/ukernels/matmul/matmul_clamp_f32_qsi8dxp_qai4cxp/kai_matmul_clamp_f32_qsi8dxp1x8_qai4cxp4x8_1x4_neon_dotprod.h b/kai/ukernels/matmul/matmul_clamp_f32_qsi8dxp_qai4cxp/kai_matmul_clamp_f32_qsi8dxp1x8_qai4cxp4x8_1x4_neon_dotprod.h new file mode 100644 index 0000000000000000000000000000000000000000..e0b8581300c40f5329ae88dc0cd00e76eb0fd06f --- /dev/null +++ b/kai/ukernels/matmul/matmul_clamp_f32_qsi8dxp_qai4cxp/kai_matmul_clamp_f32_qsi8dxp1x8_qai4cxp4x8_1x4_neon_dotprod.h @@ -0,0 +1,138 @@ +// +// SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates +// +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +/// Micro-kernel dependencies +/// +/// -# @ref kai_lhs_quant_pack_qsi8dxp_f32 to dynamically quantize and pack the LHS matrix in a single step. +/// -# @ref kai_rhs_pack_nxk_qai4cxp_qau4cxs1s0_f32_f32_f32_neon to pack the RHS NxK matrix. + +/// -------------------------------------------------- + +/// Gets the m step value. +/// The micro-kernel can process any M values. However, the starting M index to +/// be processed must be a multiple of m step. +/// +/// @return the m step value +size_t kai_get_m_step_matmul_clamp_f32_qsi8dxp1x8_qai4cxp4x8_1x4_neon_dotprod(void); + +/// Gets the n step value. +/// The micro-kernel can process any N values. However, the starting N index to +/// be processed must be a multiple of n step. +/// +/// @return the n step +size_t kai_get_n_step_matmul_clamp_f32_qsi8dxp1x8_qai4cxp4x8_1x4_neon_dotprod(void); + +/// Gets the mr value, which must be used to pack the LHS matrix +/// +/// @return the mr value +size_t kai_get_mr_matmul_clamp_f32_qsi8dxp1x8_qai4cxp4x8_1x4_neon_dotprod(void); + +/// Gets the nr value, which must be used to pack the RHS matrix. +/// +/// @return the nr value +size_t kai_get_nr_matmul_clamp_f32_qsi8dxp1x8_qai4cxp4x8_1x4_neon_dotprod(void); + +/// Gets the kr value, which must be used to pack the LHS and RHS matrices +/// +/// @return the kr value +size_t kai_get_kr_matmul_clamp_f32_qsi8dxp1x8_qai4cxp4x8_1x4_neon_dotprod(void); + +/// Gets the sr value, which must be used to pack the LHS and RHS matrices +/// +/// @return the sr value +size_t kai_get_sr_matmul_clamp_f32_qsi8dxp1x8_qai4cxp4x8_1x4_neon_dotprod(void); + +/// Gets the offset in bytes for the packed LHS matrix, +/// which contains the packed Quantized Symmetric Signed 8-bit with per-row quantization (qsi8dx) values. +/// +/// This function should be called before passing the pointer to the packed LHS matrix to the micro-kernel. +/// +/// @param[in] m_idx Row index in the LHS matrix (not packed). It must be 1. +/// @param[in] k Total number of columns in the LHS matrix (not packed). +/// +/// @return the offset in bytes to the packed LHS matrix +size_t kai_get_lhs_packed_offset_matmul_clamp_f32_qsi8dxp1x8_qai4cxp4x8_1x4_neon_dotprod( + size_t m_idx, // + size_t k); // + +/// Gets the offset in bytes for the packed RHS matrix, +/// which contains the packed Quantized Asymmetric Signed 4-bit with per-channel quantization (qai4cx) values. +/// +/// @param[in] n_idx Col index in the RHS matrix (not packed). It must be a multiple of n_step. +/// @param[in] k The common dimension between the LHS and RHS matrix (K). +/// +/// @return the offset in bytes to the packed RHS matrix +size_t kai_get_rhs_packed_offset_matmul_clamp_f32_qsi8dxp1x8_qai4cxp4x8_1x4_neon_dotprod( + size_t n_idx, // + size_t k); // + +/// Gets the offset in bytes for the DST matrix +/// +/// @param[in] m_idx Row index in the DST matrix. It must be 1. +/// @param[in] n_idx Column index in the DST matrix. It must be multiple of n_step. +/// @param[in] dst_stride The number of bytes in in each row of the DST matrix +/// +/// @return the DST offset in bytes +size_t kai_get_dst_offset_matmul_clamp_f32_qsi8dxp1x8_qai4cxp4x8_1x4_neon_dotprod( + size_t m_idx, // + size_t n_idx, // + size_t dst_stride); // + +/// Gets the size in bytes for the destination (DST) matrix. +/// +/// @param[in] m Number of rows in the destination (DST) matrix. +/// @param[in] n Number of columns in the destination (DST) matrix. +/// +/// @return the destination (DST) matrix size in bytes +size_t kai_get_dst_size_matmul_clamp_f32_qsi8dxp1x8_qai4cxp4x8_1x4_neon_dotprod( + size_t m, // + size_t n); // + +/// Runs the matrix multiplication (matmul) micro-kernel followed by a clamp (min-max) operation. +/// +/// LHS matrix: Quantized Symmetric Signed 8-bit with per-row quantization (qsi8dx) and packed. +/// RHS matrix: Quantized Asymmetric Signed 4-bit with per-channel quantization (qai4cx) and packed. +/// Output tile: (rows x cols) = m_step x n_step. +/// +/// Note: Please refer to the get functions for m_step and n_step for the exact values. +/// +/// Features used: dotprod +/// +/// @param[in] m The number of output rows written. It must be 1. +/// @param[in] n The number of output columns written. +/// @param[in] k The number of channels. The common dimension between the LHS and RHS matrix. +/// @param[in] lhs_packed The LHS packed matrix. The micro-kernel to pack the native LHS matrix is reported at the +/// top of this file. +/// @param[in] rhs_packed The RHS packed matrix. The micro-kernel to pack the native RHS matrix is reported at the +/// top of this file. +/// @param[out] dst The DST matrix. +/// @param[in] dst_stride_row Stride in bytes between two rows of the DST matrix. +/// @param[in] dst_stride_col Stride in bytes between two columns of the DST matrix. It must be sizeof(float) bytes. +/// @param[in] scalar_min Min value used to clamp the final result. +/// @param[in] scalar_max Max value used to clamp the final result. +void kai_run_matmul_clamp_f32_qsi8dxp1x8_qai4cxp4x8_1x4_neon_dotprod( + size_t m, // + size_t n, // + size_t k, // + const void* lhs_packed, // + const void* rhs_packed, // + float* dst, // + size_t dst_stride_row, // + size_t dst_stride_col, // + float scalar_min, // + float scalar_max); // + +#ifdef __cplusplus +} +#endif // __cplusplus diff --git a/kai/ukernels/matmul/matmul_clamp_f32_qsi8dxp_qai4cxp/kai_matmul_clamp_f32_qsi8dxp1x8_qai4cxp4x8_1x4_neon_dotprod_asm.S b/kai/ukernels/matmul/matmul_clamp_f32_qsi8dxp_qai4cxp/kai_matmul_clamp_f32_qsi8dxp1x8_qai4cxp4x8_1x4_neon_dotprod_asm.S new file mode 100644 index 0000000000000000000000000000000000000000..2f192212afcdb1fb7ac6afd64498fe0419484cc5 --- /dev/null +++ b/kai/ukernels/matmul/matmul_clamp_f32_qsi8dxp_qai4cxp/kai_matmul_clamp_f32_qsi8dxp1x8_qai4cxp4x8_1x4_neon_dotprod_asm.S @@ -0,0 +1,148 @@ +// +// SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates +// +// SPDX-License-Identifier: Apache-2.0 +// + +#if defined(_MSC_VER) + #define KAI_ASM_GLOBAL(name) GLOBAL name + #define KAI_ASM_FUNCTION_TYPE(name) + #define KAI_ASM_FUNCTION_LABEL(name) name PROC + #define KAI_ASM_FUNCTION_END(name) ENDP + + #define KAI_ASM_CODE(name) AREA name, CODE, READONLY + #define KAI_ASM_ALIGN + #define KAI_ASM_LABEL(name) name + #define KAI_ASM_INST(hex) DCD hex + #define KAI_ASM_END END +#else + #if defined(__APPLE__) + #define KAI_ASM_GLOBAL(name) .globl _##name + #define KAI_ASM_FUNCTION_TYPE(name) + #define KAI_ASM_FUNCTION_LABEL(name) _##name: + #define KAI_ASM_FUNCTION_END(name) + #else + #define KAI_ASM_GLOBAL(name) .global name + #define KAI_ASM_FUNCTION_TYPE(name) .type name, %function + #define KAI_ASM_FUNCTION_LABEL(name) name: + #define KAI_ASM_FUNCTION_END(name) .size name, .-name + #endif + + #define KAI_ASM_CODE(name) .text + #define KAI_ASM_ALIGN .p2align 4,,11 + #define KAI_ASM_LABEL(name) name: + #define KAI_ASM_INST(hex) .inst hex + #define KAI_ASM_END +#endif + + KAI_ASM_CODE(matmul_clamp_f32_qsi8dxp1x8_qai4cxp4x8_1x4_neon_dotprod) + KAI_ASM_ALIGN + + KAI_ASM_GLOBAL(kai_kernel_matmul_clamp_f32_qsi8dxp1x8_qai4cxp4x8_1x4_neon_dotprod) + +KAI_ASM_FUNCTION_TYPE(kai_kernel_matmul_clamp_f32_qsi8dxp1x8_qai4cxp4x8_1x4_neon_dotprod) +KAI_ASM_FUNCTION_LABEL(kai_kernel_matmul_clamp_f32_qsi8dxp1x8_qai4cxp4x8_1x4_neon_dotprod) + stp x20, x21, [sp, -80]! + stp x22, x23, [sp, 16] + stp x24, x25, [sp, 32] + stp x26, x27, [sp, 48] + str x28, [sp, 64] + mov x13, #0x20 + movi v30.16b, #0xf0 + mov x21, #0x8 + ldr x12, [x0, #0x38] + ldr x20, [x0, #0x28] + ldr x11, [x0, #0x8] + ldr x10, [x0, #0x10] + ldr x9, [x0, #0x30] + ldr x28, [x0, #0x0] + ldr x27, [x0, #0x20] + madd x13, x12, x13, x21 + ldr x26, [x0, #0x18] + mov x25, x20 +KAI_ASM_LABEL(label_1) // Row loop + mov x24, x10 + mov x23, x9 + add x22, x28, x27 +KAI_ASM_LABEL(label_2) // Column loop + mov x21, x11 + movi v29.4s, #0x0 + movi v28.4s, #0x0 + mov x20, x12 +KAI_ASM_LABEL(label_3) // Sub block loop + ldr q27, [x24, #0x0] + ldr q26, [x24, #0x10] + subs x20, x20, #0x1 + ld1r { v25.2d }, [x21], #0x8 + ldr q24, [x24, #0x20] + ldr q23, [x24, #0x30] + add x24, x24, #0x40 + ld1r { v22.2d }, [x21], #0x8 + ld1r { v21.2d }, [x21], #0x8 + shl v20.16b, v27.16b, #0x4 + shl v19.16b, v26.16b, #0x4 + ld1r { v18.2d }, [x21], #0x8 + shl v17.16b, v24.16b, #0x4 + and v27.16b, v27.16b, v30.16b + shl v16.16b, v23.16b, #0x4 + and v26.16b, v26.16b, v30.16b + KAI_ASM_INST(0x4e99969d) // sdot v29.4s, v20.16b, v25.16b + KAI_ASM_INST(0x4e99967c) // sdot v28.4s, v19.16b, v25.16b + and v24.16b, v24.16b, v30.16b + and v23.16b, v23.16b, v30.16b + KAI_ASM_INST(0x4e96963d) // sdot v29.4s, v17.16b, v22.16b + KAI_ASM_INST(0x4e96961c) // sdot v28.4s, v16.16b, v22.16b + KAI_ASM_INST(0x4e95977d) // sdot v29.4s, v27.16b, v21.16b + KAI_ASM_INST(0x4e95975c) // sdot v28.4s, v26.16b, v21.16b + KAI_ASM_INST(0x4e92971d) // sdot v29.4s, v24.16b, v18.16b + KAI_ASM_INST(0x4e9296fc) // sdot v28.4s, v23.16b, v18.16b + bgt label_3 + ld1r { v22.4s }, [x21] + ldr q16, [x24, #0x0] + add x21, x21, #0x4 + addp v29.4s, v29.4s, v28.4s + ld1r { v21.4s }, [x21] + ldr q20, [x24, #0x10] + add x20, x26, #0x4 + cmp x23, #0x4 + ldr q19, [x24, #0x20] + ld1r { v18.4s }, [x26] + add x24, x24, #0x30 + ld1r { v17.4s }, [x20] + fmul v16.4s, v16.4s, v22.s[0] + scvtf v29.4s, v29.4s + fmul v20.4s, v20.4s, v21.4s + fmla v16.4s, v29.4s, v20.4s + fadd v16.4s, v16.4s, v19.4s + fmax v16.4s, v16.4s, v18.4s + fmin v16.4s, v16.4s, v17.4s + blt label_4 + str q16, [x28, #0x0] + b label_7 +KAI_ASM_LABEL(label_4) // Partial output + mov x20, x28 + tbz x23, #1, label_5 + st1 { v16.d }[0], [x20], #0x8 + tbz x23, #0, label_6 + st1 { v16.s }[2], [x20] + b label_6 +KAI_ASM_LABEL(label_5) // Output block 0: partial_1_0 + st1 { v16.s }[0], [x20] +KAI_ASM_LABEL(label_6) // Output block 0: Done +KAI_ASM_LABEL(label_7) // Stores done + subs x23, x23, #0x4 + add x28, x28, #0x10 + bgt label_2 + subs x25, x25, #0x1 + add x11, x11, x13 + mov x28, x22 + bgt label_1 + ldp x22, x23, [sp, 16] + ldp x24, x25, [sp, 32] + ldp x26, x27, [sp, 48] + ldr x28, [sp, 64] + ldp x20, x21, [sp], 80 + ret + KAI_ASM_FUNCTION_END(kai_kernel_matmul_clamp_f32_qsi8dxp1x8_qai4cxp4x8_1x4_neon_dotprod) + + KAI_ASM_END diff --git a/kai/ukernels/matmul/matmul_clamp_f32_qsi8dxp_qai4cxp/kai_matmul_clamp_f32_qsi8dxp4x8_qai4cxp4x8_8x4_neon_i8mm.c b/kai/ukernels/matmul/matmul_clamp_f32_qsi8dxp_qai4cxp/kai_matmul_clamp_f32_qsi8dxp4x8_qai4cxp4x8_8x4_neon_i8mm.c new file mode 100644 index 0000000000000000000000000000000000000000..f80f2cb7b88ae17adbafe377819e5c1b71104c15 --- /dev/null +++ b/kai/ukernels/matmul/matmul_clamp_f32_qsi8dxp_qai4cxp/kai_matmul_clamp_f32_qsi8dxp4x8_qai4cxp4x8_8x4_neon_i8mm.c @@ -0,0 +1,161 @@ +// +// SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates +// +// SPDX-License-Identifier: Apache-2.0 +// +#if !defined(__aarch64__) && !defined(__ARM_FEATURE_MATMUL_INT8) && !defined(_M_ARM64) +#error "I8mm extension required to compile this micro-kernel" +#else // Architectural features check. + +#include "kai_matmul_clamp_f32_qsi8dxp4x8_qai4cxp4x8_8x4_neon_i8mm.h" + +#include + +#include "kai/kai_common.h" + +typedef struct { + float* dst; + const void* lhs_packed; + const void* rhs_packed; + const float* clamp_vals; + size_t dst_stride_row; + size_t m; + size_t n; + size_t num_blocks; +} KernelArgs; + +void kai_kernel_matmul_clamp_f32_qsi8dxp4x8_qai4cxp4x8_8x4_neon_i8mm(KernelArgs* args_ptr); + +// Compute args +static const size_t kai_m_step = 8; +static const size_t kai_n_step = 4; +// Packing args +static const size_t kai_mr = 4; +static const size_t kai_nr = 4; +static const size_t kai_kr = 16; +static const size_t kai_sr = 2; +// LHS format args (num. bytes per value, multiplier, zero_point (if asymmetric)) +static const size_t kai_num_bytes_qvalue_lhs = 1; +static const size_t kai_num_bytes_multiplier_lhs = 4; +static const size_t kai_num_bytes_sum_lhs = 4; +// RHS format args (num. bytes per value, multiplier, zero_point (if asymmetric), and reduction sum (if LHS is +// asymmetric)) +static const size_t kai_num_bytes_recip_qvalue_rhs = 2; +static const size_t kai_num_bytes_multiplier_rhs = 4; +static const size_t kai_num_bytes_offset_rhs = 4; + +// DST format args +static const size_t kai_num_bytes_dst_value = 4; +// Extra args +static const size_t kai_num_bytes_bias = 4; +static const size_t kai_k_multiple_of = 32; + +static const size_t kai_bl = 32; + +inline static size_t kai_get_k_roundedup(size_t k) { + return kai_roundup(k, kai_k_multiple_of); +} + +inline static size_t kai_get_lhs_packed_stride(size_t k) { + const size_t k_internal = kai_get_k_roundedup(k); + size_t lhs_packed_stride = + kai_mr * ((k_internal * kai_num_bytes_qvalue_lhs) + kai_num_bytes_multiplier_lhs + kai_num_bytes_sum_lhs); + + return lhs_packed_stride; +} + +inline static size_t kai_get_rhs_packed_stride(size_t k) { + const size_t k_internal = kai_get_k_roundedup(k); + size_t rhs_packed_stride = kai_nr * (k_internal / kai_num_bytes_recip_qvalue_rhs); + + rhs_packed_stride += kai_nr * kai_num_bytes_multiplier_rhs; + // Since the bias is packed with the RHS matrix, the stride is adjusted with the number of bytes of the bias + rhs_packed_stride += kai_nr * kai_num_bytes_bias; + rhs_packed_stride += kai_nr * kai_num_bytes_offset_rhs; + + return rhs_packed_stride; +} + +size_t kai_get_m_step_matmul_clamp_f32_qsi8dxp4x8_qai4cxp4x8_8x4_neon_i8mm(void) { + return kai_m_step; +} + +size_t kai_get_n_step_matmul_clamp_f32_qsi8dxp4x8_qai4cxp4x8_8x4_neon_i8mm(void) { + return kai_n_step; +} + +size_t kai_get_mr_matmul_clamp_f32_qsi8dxp4x8_qai4cxp4x8_8x4_neon_i8mm(void) { + return kai_mr; +} + +size_t kai_get_nr_matmul_clamp_f32_qsi8dxp4x8_qai4cxp4x8_8x4_neon_i8mm(void) { + return kai_nr; +} + +size_t kai_get_kr_matmul_clamp_f32_qsi8dxp4x8_qai4cxp4x8_8x4_neon_i8mm(void) { + return kai_kr; +} + +size_t kai_get_sr_matmul_clamp_f32_qsi8dxp4x8_qai4cxp4x8_8x4_neon_i8mm(void) { + return kai_sr; +} + +size_t kai_get_lhs_packed_offset_matmul_clamp_f32_qsi8dxp4x8_qai4cxp4x8_8x4_neon_i8mm(size_t m_idx, size_t k) { + KAI_ASSUME((m_idx % kai_m_step) == 0); + + return (m_idx / kai_mr) * kai_get_lhs_packed_stride(k); +} + +size_t kai_get_rhs_packed_offset_matmul_clamp_f32_qsi8dxp4x8_qai4cxp4x8_8x4_neon_i8mm(size_t n_idx, size_t k) { + KAI_ASSUME((n_idx % kai_n_step) == 0); + + return (n_idx / kai_nr) * kai_get_rhs_packed_stride(k); +} + +size_t kai_get_dst_offset_matmul_clamp_f32_qsi8dxp4x8_qai4cxp4x8_8x4_neon_i8mm( + size_t m_idx, size_t n_idx, size_t dst_stride) { + KAI_ASSUME((m_idx % kai_m_step) == 0); + KAI_ASSUME((n_idx % kai_n_step) == 0); + + return (n_idx * kai_num_bytes_dst_value) + m_idx * dst_stride; +} + +size_t kai_get_dst_size_matmul_clamp_f32_qsi8dxp4x8_qai4cxp4x8_8x4_neon_i8mm(size_t m, size_t n) { + return m * n * kai_num_bytes_dst_value; +} + +void kai_run_matmul_clamp_f32_qsi8dxp4x8_qai4cxp4x8_8x4_neon_i8mm( + size_t m, // + size_t n, // + size_t k, // + const void* restrict lhs_packed, // + const void* restrict rhs_packed, // + float* restrict dst, // NOLINT(readability-non-const-parameter) + size_t dst_stride_row, // + size_t dst_stride_col, // + float scalar_min, // + float scalar_max) { + KAI_ASSUME(dst_stride_col == sizeof(float)); + if (m == 0) { + return; + } + + const size_t k_internal = kai_get_k_roundedup(k); + const size_t num_blocks = k_internal / kai_bl; + const float clamp_vals[2] = {scalar_min, scalar_max}; + + KernelArgs args; + + args.dst = dst; + args.lhs_packed = lhs_packed; + args.rhs_packed = rhs_packed; + args.clamp_vals = clamp_vals; + args.dst_stride_row = dst_stride_row; + args.m = m; + args.n = n; + args.num_blocks = num_blocks; + + kai_kernel_matmul_clamp_f32_qsi8dxp4x8_qai4cxp4x8_8x4_neon_i8mm(&args); +} + +#endif // Architectural features check. diff --git a/kai/ukernels/matmul/matmul_clamp_f32_qsi8dxp_qai4cxp/kai_matmul_clamp_f32_qsi8dxp4x8_qai4cxp4x8_8x4_neon_i8mm.h b/kai/ukernels/matmul/matmul_clamp_f32_qsi8dxp_qai4cxp/kai_matmul_clamp_f32_qsi8dxp4x8_qai4cxp4x8_8x4_neon_i8mm.h new file mode 100644 index 0000000000000000000000000000000000000000..04e32914b4cb4ef58885081426584d181943a96b --- /dev/null +++ b/kai/ukernels/matmul/matmul_clamp_f32_qsi8dxp_qai4cxp/kai_matmul_clamp_f32_qsi8dxp4x8_qai4cxp4x8_8x4_neon_i8mm.h @@ -0,0 +1,138 @@ +// +// SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates +// +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +/// Micro-kernel dependencies +/// +/// -# @ref kai_lhs_quant_pack_qsi8dxp_f32 to dynamically quantize and pack the LHS matrix in a single step. +/// -# @ref kai_rhs_pack_nxk_qai4cxp_qau4cxs1s0_f32_f32_f32_neon to pack the RHS NxK matrix. + +/// -------------------------------------------------- + +/// Gets the m step value. +/// The micro-kernel can process any M values. However, the starting M index to +/// be processed must be a multiple of m step. +/// +/// @return the m step value +size_t kai_get_m_step_matmul_clamp_f32_qsi8dxp4x8_qai4cxp4x8_8x4_neon_i8mm(void); + +/// Gets the n step value. +/// The micro-kernel can process any N values. However, the starting N index to +/// be processed must be a multiple of n step. +/// +/// @return the n step +size_t kai_get_n_step_matmul_clamp_f32_qsi8dxp4x8_qai4cxp4x8_8x4_neon_i8mm(void); + +/// Gets the mr value, which must be used to pack the LHS matrix +/// +/// @return the mr value +size_t kai_get_mr_matmul_clamp_f32_qsi8dxp4x8_qai4cxp4x8_8x4_neon_i8mm(void); + +/// Gets the nr value, which must be used to pack the RHS matrix. +/// +/// @return the nr value +size_t kai_get_nr_matmul_clamp_f32_qsi8dxp4x8_qai4cxp4x8_8x4_neon_i8mm(void); + +/// Gets the kr value, which must be used to pack the LHS and RHS matrices +/// +/// @return the kr value +size_t kai_get_kr_matmul_clamp_f32_qsi8dxp4x8_qai4cxp4x8_8x4_neon_i8mm(void); + +/// Gets the sr value, which must be used to pack the LHS and RHS matrices +/// +/// @return the sr value +size_t kai_get_sr_matmul_clamp_f32_qsi8dxp4x8_qai4cxp4x8_8x4_neon_i8mm(void); + +/// Gets the offset in bytes for the packed LHS matrix, +/// which contains the packed Quantized Symmetric Signed 8-bit with per-row quantization (qsi8dx) values. +/// +/// This function should be called before passing the pointer to the packed LHS matrix to the micro-kernel. +/// +/// @param[in] m_idx Row index in the LHS matrix (not packed). It must be a multiple of m_step. +/// @param[in] k Total number of columns in the LHS matrix (not packed). +/// +/// @return the offset in bytes to the packed LHS matrix +size_t kai_get_lhs_packed_offset_matmul_clamp_f32_qsi8dxp4x8_qai4cxp4x8_8x4_neon_i8mm( + size_t m_idx, // + size_t k); // + +/// Gets the offset in bytes for the packed RHS matrix, +/// which contains the packed Quantized Asymmetric Signed 4-bit with per-channel quantization (qai4cx) values. +/// +/// @param[in] n_idx Col index in the RHS matrix (not packed). It must be a multiple of n_step. +/// @param[in] k The common dimension between the LHS and RHS matrix (K). +/// +/// @return the offset in bytes to the packed RHS matrix +size_t kai_get_rhs_packed_offset_matmul_clamp_f32_qsi8dxp4x8_qai4cxp4x8_8x4_neon_i8mm( + size_t n_idx, // + size_t k); // + +/// Gets the offset in bytes for the DST matrix +/// +/// @param[in] m_idx Row index in the DST matrix. It must be a multiple of m_step. +/// @param[in] n_idx Column index in the DST matrix. It must be multiple of n_step. +/// @param[in] dst_stride The number of bytes in in each row of the DST matrix +/// +/// @return the DST offset in bytes +size_t kai_get_dst_offset_matmul_clamp_f32_qsi8dxp4x8_qai4cxp4x8_8x4_neon_i8mm( + size_t m_idx, // + size_t n_idx, // + size_t dst_stride); // + +/// Gets the size in bytes for the destination (DST) matrix. +/// +/// @param[in] m Number of rows in the destination (DST) matrix. +/// @param[in] n Number of columns in the destination (DST) matrix. +/// +/// @return the destination (DST) matrix size in bytes +size_t kai_get_dst_size_matmul_clamp_f32_qsi8dxp4x8_qai4cxp4x8_8x4_neon_i8mm( + size_t m, // + size_t n); // + +/// Runs the matrix multiplication (matmul) micro-kernel followed by a clamp (min-max) operation. +/// +/// LHS matrix: Quantized Symmetric Signed 8-bit with per-row quantization (qsi8dx) and packed. +/// RHS matrix: Quantized Asymmetric Signed 4-bit with per-channel quantization (qai4cx) and packed. +/// Output tile: (rows x cols) = m_step x n_step. +/// +/// Note: Please refer to the get functions for m_step and n_step for the exact values. +/// +/// Features used: i8mm +/// +/// @param[in] m The number of output rows written. +/// @param[in] n The number of output columns written. +/// @param[in] k The number of channels. The common dimension between the LHS and RHS matrix. +/// @param[in] lhs_packed The LHS packed matrix. The micro-kernel to pack the native LHS matrix is reported at the +/// top of this file. +/// @param[in] rhs_packed The RHS packed matrix. The micro-kernel to pack the native RHS matrix is reported at the +/// top of this file. +/// @param[out] dst The DST matrix. +/// @param[in] dst_stride_row Stride in bytes between two rows of the DST matrix. +/// @param[in] dst_stride_col Stride in bytes between two columns of the DST matrix. It must be sizeof(float) bytes. +/// @param[in] scalar_min Min value used to clamp the final result. +/// @param[in] scalar_max Max value used to clamp the final result. +void kai_run_matmul_clamp_f32_qsi8dxp4x8_qai4cxp4x8_8x4_neon_i8mm( + size_t m, // + size_t n, // + size_t k, // + const void* lhs_packed, // + const void* rhs_packed, // + float* dst, // + size_t dst_stride_row, // + size_t dst_stride_col, // + float scalar_min, // + float scalar_max); // + +#ifdef __cplusplus +} +#endif // __cplusplus diff --git a/kai/ukernels/matmul/matmul_clamp_f32_qsi8dxp_qai4cxp/kai_matmul_clamp_f32_qsi8dxp4x8_qai4cxp4x8_8x4_neon_i8mm_asm.S b/kai/ukernels/matmul/matmul_clamp_f32_qsi8dxp_qai4cxp/kai_matmul_clamp_f32_qsi8dxp4x8_qai4cxp4x8_8x4_neon_i8mm_asm.S new file mode 100644 index 0000000000000000000000000000000000000000..67d5d80adf5ad49a86b246814ce68ad4181b5afb --- /dev/null +++ b/kai/ukernels/matmul/matmul_clamp_f32_qsi8dxp_qai4cxp/kai_matmul_clamp_f32_qsi8dxp4x8_qai4cxp4x8_8x4_neon_i8mm_asm.S @@ -0,0 +1,455 @@ +// +// SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates +// +// SPDX-License-Identifier: Apache-2.0 +// + +#if defined(_MSC_VER) + #define KAI_ASM_GLOBAL(name) GLOBAL name + #define KAI_ASM_FUNCTION_TYPE(name) + #define KAI_ASM_FUNCTION_LABEL(name) name PROC + #define KAI_ASM_FUNCTION_END(name) ENDP + + #define KAI_ASM_CODE(name) AREA name, CODE, READONLY + #define KAI_ASM_ALIGN + #define KAI_ASM_LABEL(name) name + #define KAI_ASM_INST(hex) DCD hex + #define KAI_ASM_END END +#else + #if defined(__APPLE__) + #define KAI_ASM_GLOBAL(name) .globl _##name + #define KAI_ASM_FUNCTION_TYPE(name) + #define KAI_ASM_FUNCTION_LABEL(name) _##name: + #define KAI_ASM_FUNCTION_END(name) + #else + #define KAI_ASM_GLOBAL(name) .global name + #define KAI_ASM_FUNCTION_TYPE(name) .type name, %function + #define KAI_ASM_FUNCTION_LABEL(name) name: + #define KAI_ASM_FUNCTION_END(name) .size name, .-name + #endif + + #define KAI_ASM_CODE(name) .text + #define KAI_ASM_ALIGN .p2align 4,,11 + #define KAI_ASM_LABEL(name) name: + #define KAI_ASM_INST(hex) .inst hex + #define KAI_ASM_END +#endif + + KAI_ASM_CODE(matmul_clamp_f32_qsi8dxp4x8_qai4cxp4x8_8x4_neon_i8mm) + KAI_ASM_ALIGN + + KAI_ASM_GLOBAL(kai_kernel_matmul_clamp_f32_qsi8dxp4x8_qai4cxp4x8_8x4_neon_i8mm) + +KAI_ASM_FUNCTION_TYPE(kai_kernel_matmul_clamp_f32_qsi8dxp4x8_qai4cxp4x8_8x4_neon_i8mm) +KAI_ASM_FUNCTION_LABEL(kai_kernel_matmul_clamp_f32_qsi8dxp4x8_qai4cxp4x8_8x4_neon_i8mm) + stp x20, x21, [sp, -112]! + stp x22, x23, [sp, 16] + stp x24, x25, [sp, 32] + stp x26, x27, [sp, 48] + str x28, [sp, 64] + stp d10, d11, [sp, 72] + stp d8, d9, [sp, 88] + mov x7, #0x80 + movi v11.16b, #0xf0 + mov x21, #0x20 + ldr x20, [x0, #0x28] + ldr x8, [x0, #0x38] + ldr x17, [x0, #0x8] + ldr x16, [x0, #0x10] + ldr x15, [x0, #0x30] + ldr x14, [x0, #0x0] + mov x13, x20 + ldr x12, [x0, #0x20] + madd x7, x8, x7, x21 + ldr x11, [x0, #0x18] + cmp x13, #0x8 + blt label_10 +KAI_ASM_LABEL(label_1) // Row loop + mov x10, x16 + mov x9, x15 + add x28, x14, x12, LSL #3 +KAI_ASM_LABEL(label_2) // Column loop + mov x22, x17 + movi v10.4s, #0x0 + movi v9.4s, #0x0 + mov x21, x8 + movi v8.4s, #0x0 + movi v7.4s, #0x0 + movi v6.4s, #0x0 + movi v5.4s, #0x0 + add x20, x22, x7 + movi v4.4s, #0x0 + movi v3.4s, #0x0 +KAI_ASM_LABEL(label_3) // Sub block loop + ldr q2, [x10, #0x0] + ldr q1, [x10, #0x10] + subs x21, x21, #0x1 + ldr q20, [x22, #0x0] + ldr q19, [x22, #0x10] + ldr q18, [x20, #0x0] + ldr q0, [x20, #0x10] + ldr q31, [x10, #0x20] + ldr q30, [x10, #0x30] + shl v17.16b, v2.16b, #0x4 + shl v16.16b, v1.16b, #0x4 + ldr q29, [x22, #0x20] + ldr q28, [x22, #0x30] + and v2.16b, v2.16b, v11.16b + and v1.16b, v1.16b, v11.16b + ldr q27, [x20, #0x20] + ldr q26, [x20, #0x30] + add x10, x10, #0x40 + ldr q25, [x22, #0x40] + ldr q24, [x22, #0x50] + KAI_ASM_INST(0x4e91a68a) // smmla v10.4s, v20.16b, v17.16b + KAI_ASM_INST(0x4e90a689) // smmla v9.4s, v20.16b, v16.16b + ldr q23, [x20, #0x40] + ldr q22, [x20, #0x50] + KAI_ASM_INST(0x4e91a668) // smmla v8.4s, v19.16b, v17.16b + KAI_ASM_INST(0x4e90a667) // smmla v7.4s, v19.16b, v16.16b + ldr q21, [x22, #0x60] + ldr q20, [x22, #0x70] + KAI_ASM_INST(0x4e91a646) // smmla v6.4s, v18.16b, v17.16b + KAI_ASM_INST(0x4e90a645) // smmla v5.4s, v18.16b, v16.16b + ldr q19, [x20, #0x60] + ldr q18, [x20, #0x70] + KAI_ASM_INST(0x4e91a404) // smmla v4.4s, v0.16b, v17.16b + KAI_ASM_INST(0x4e90a403) // smmla v3.4s, v0.16b, v16.16b + shl v17.16b, v31.16b, #0x4 + shl v16.16b, v30.16b, #0x4 + add x22, x22, #0x80 + add x20, x20, #0x80 + and v31.16b, v31.16b, v11.16b + and v30.16b, v30.16b, v11.16b + KAI_ASM_INST(0x4e91a7aa) // smmla v10.4s, v29.16b, v17.16b + KAI_ASM_INST(0x4e90a7a9) // smmla v9.4s, v29.16b, v16.16b + KAI_ASM_INST(0x4e91a788) // smmla v8.4s, v28.16b, v17.16b + KAI_ASM_INST(0x4e90a787) // smmla v7.4s, v28.16b, v16.16b + KAI_ASM_INST(0x4e91a766) // smmla v6.4s, v27.16b, v17.16b + KAI_ASM_INST(0x4e90a765) // smmla v5.4s, v27.16b, v16.16b + KAI_ASM_INST(0x4e91a744) // smmla v4.4s, v26.16b, v17.16b + KAI_ASM_INST(0x4e90a743) // smmla v3.4s, v26.16b, v16.16b + KAI_ASM_INST(0x4e82a72a) // smmla v10.4s, v25.16b, v2.16b + KAI_ASM_INST(0x4e81a729) // smmla v9.4s, v25.16b, v1.16b + KAI_ASM_INST(0x4e82a708) // smmla v8.4s, v24.16b, v2.16b + KAI_ASM_INST(0x4e81a707) // smmla v7.4s, v24.16b, v1.16b + KAI_ASM_INST(0x4e82a6e6) // smmla v6.4s, v23.16b, v2.16b + KAI_ASM_INST(0x4e81a6e5) // smmla v5.4s, v23.16b, v1.16b + KAI_ASM_INST(0x4e82a6c4) // smmla v4.4s, v22.16b, v2.16b + KAI_ASM_INST(0x4e81a6c3) // smmla v3.4s, v22.16b, v1.16b + KAI_ASM_INST(0x4e9fa6aa) // smmla v10.4s, v21.16b, v31.16b + KAI_ASM_INST(0x4e9ea6a9) // smmla v9.4s, v21.16b, v30.16b + KAI_ASM_INST(0x4e9fa688) // smmla v8.4s, v20.16b, v31.16b + KAI_ASM_INST(0x4e9ea687) // smmla v7.4s, v20.16b, v30.16b + KAI_ASM_INST(0x4e9fa666) // smmla v6.4s, v19.16b, v31.16b + KAI_ASM_INST(0x4e9ea665) // smmla v5.4s, v19.16b, v30.16b + KAI_ASM_INST(0x4e9fa644) // smmla v4.4s, v18.16b, v31.16b + KAI_ASM_INST(0x4e9ea643) // smmla v3.4s, v18.16b, v30.16b + bgt label_3 + ld1 { v17.4s }, [x22] + ldr q25, [x10, #0x0] + add x22, x22, #0x10 + uzp1 v23.2d, v10.2d, v9.2d + ldr q24, [x10, #0x10] + ldr q16, [x22, #0x0] + uzp2 v22.2d, v10.2d, v9.2d + uzp1 v21.2d, v8.2d, v7.2d + uzp2 v20.2d, v8.2d, v7.2d + add x10, x10, #0x20 + fmul v10.4s, v25.4s, v17.s[0] + fmul v9.4s, v25.4s, v17.s[1] + fmul v8.4s, v25.4s, v17.s[2] + fmul v7.4s, v25.4s, v17.s[3] + fmul v19.4s, v24.4s, v16.s[0] + scvtf v23.4s, v23.4s + fmul v18.4s, v24.4s, v16.s[1] + scvtf v22.4s, v22.4s + fmul v17.4s, v24.4s, v16.s[2] + scvtf v21.4s, v21.4s + fmul v16.4s, v24.4s, v16.s[3] + scvtf v20.4s, v20.4s + fmla v10.4s, v23.4s, v19.4s + fmla v9.4s, v22.4s, v18.4s + fmla v8.4s, v21.4s, v17.4s + fmla v7.4s, v20.4s, v16.4s + ld1 { v17.4s }, [x20] + add x20, x20, #0x10 + uzp1 v23.2d, v6.2d, v5.2d + uzp2 v22.2d, v6.2d, v5.2d + ldr q16, [x20, #0x0] + uzp1 v21.2d, v4.2d, v3.2d + uzp2 v20.2d, v4.2d, v3.2d + fmul v6.4s, v25.4s, v17.s[0] + fmul v5.4s, v25.4s, v17.s[1] + fmul v4.4s, v25.4s, v17.s[2] + fmul v3.4s, v25.4s, v17.s[3] + fmul v19.4s, v24.4s, v16.s[0] + scvtf v23.4s, v23.4s + fmul v18.4s, v24.4s, v16.s[1] + scvtf v22.4s, v22.4s + fmul v17.4s, v24.4s, v16.s[2] + scvtf v21.4s, v21.4s + fmul v16.4s, v24.4s, v16.s[3] + scvtf v20.4s, v20.4s + fmla v6.4s, v23.4s, v19.4s + fmla v5.4s, v22.4s, v18.4s + fmla v4.4s, v21.4s, v17.4s + fmla v3.4s, v20.4s, v16.4s + ldr q18, [x10, #0x0] + ld1r { v17.4s }, [x11] + add x20, x11, #0x4 + cmp x9, #0x4 + ld1r { v16.4s }, [x20] + add x10, x10, #0x10 + fadd v10.4s, v10.4s, v18.4s + fadd v9.4s, v9.4s, v18.4s + fadd v8.4s, v8.4s, v18.4s + fadd v7.4s, v7.4s, v18.4s + fadd v6.4s, v6.4s, v18.4s + fadd v5.4s, v5.4s, v18.4s + fadd v4.4s, v4.4s, v18.4s + fadd v3.4s, v3.4s, v18.4s + fmax v10.4s, v10.4s, v17.4s + fmax v9.4s, v9.4s, v17.4s + fmax v8.4s, v8.4s, v17.4s + fmax v7.4s, v7.4s, v17.4s + fmax v6.4s, v6.4s, v17.4s + fmax v5.4s, v5.4s, v17.4s + fmax v4.4s, v4.4s, v17.4s + fmax v3.4s, v3.4s, v17.4s + fmin v10.4s, v10.4s, v16.4s + fmin v9.4s, v9.4s, v16.4s + fmin v8.4s, v8.4s, v16.4s + fmin v7.4s, v7.4s, v16.4s + fmin v6.4s, v6.4s, v16.4s + fmin v5.4s, v5.4s, v16.4s + fmin v4.4s, v4.4s, v16.4s + fmin v3.4s, v3.4s, v16.4s + blt label_6 + mov x20, x14 + str q10, [x20, #0x0] + add x20, x20, x12 + str q9, [x20, #0x0] + add x20, x20, x12 + str q8, [x20, #0x0] + add x20, x20, x12 + str q7, [x20, #0x0] + add x20, x20, x12 + str q6, [x20, #0x0] + add x20, x20, x12 + str q5, [x20, #0x0] + add x20, x20, x12 + str q4, [x20, #0x0] + add x20, x20, x12 + str q3, [x20, #0x0] + b label_9 +KAI_ASM_LABEL(label_6) // Partial output + mov x27, x14 + add x26, x27, x12, LSL #2 + add x25, x26, x12, LSL #1 + add x24, x26, x12 + add x23, x25, x12 + add x22, x27, x12, LSL #1 + add x21, x27, x12 + add x20, x22, x12 + tbz x9, #1, label_7 + st1 { v3.d }[0], [x23], #0x8 + st1 { v4.d }[0], [x25], #0x8 + st1 { v5.d }[0], [x24], #0x8 + st1 { v6.d }[0], [x26], #0x8 + st1 { v7.d }[0], [x20], #0x8 + st1 { v8.d }[0], [x22], #0x8 + st1 { v9.d }[0], [x21], #0x8 + st1 { v10.d }[0], [x27], #0x8 + tbz x9, #0, label_8 + st1 { v3.s }[2], [x23] + st1 { v4.s }[2], [x25] + st1 { v5.s }[2], [x24] + st1 { v6.s }[2], [x26] + st1 { v7.s }[2], [x20] + st1 { v8.s }[2], [x22] + st1 { v9.s }[2], [x21] + st1 { v10.s }[2], [x27] + b label_8 +KAI_ASM_LABEL(label_7) // Output block 0: partial_1_0 + st1 { v3.s }[0], [x23] + st1 { v4.s }[0], [x25] + st1 { v5.s }[0], [x24] + st1 { v6.s }[0], [x26] + st1 { v7.s }[0], [x20] + st1 { v8.s }[0], [x22] + st1 { v9.s }[0], [x21] + st1 { v10.s }[0], [x27] +KAI_ASM_LABEL(label_8) // Output block 0: Done +KAI_ASM_LABEL(label_9) // Output stage exit + subs x9, x9, #0x4 + add x14, x14, #0x10 + bgt label_2 + mov x20, #0x2 + sub x13, x13, #0x8 + cmp x13, #0x8 + mov x14, x28 + madd x17, x20, x7, x17 + bge label_1 +KAI_ASM_LABEL(label_10) // Row loop skip + cbz x13, label_19 +KAI_ASM_LABEL(label_11) // Row tail: Row loop + mov x26, x16 + mov x25, x15 + add x24, x14, x12, LSL #2 +KAI_ASM_LABEL(label_12) // Row tail: Column loop + mov x22, x17 + movi v10.4s, #0x0 + movi v9.4s, #0x0 + mov x20, x8 + movi v8.4s, #0x0 + movi v7.4s, #0x0 +KAI_ASM_LABEL(label_13) // Row tail: Sub block loop + ldr q31, [x26, #0x0] + ldr q30, [x26, #0x10] + subs x20, x20, #0x1 + ldr q29, [x22, #0x0] + ldr q28, [x22, #0x10] + ldr q27, [x26, #0x20] + ldr q26, [x26, #0x30] + add x26, x26, #0x40 + ldr q25, [x22, #0x20] + ldr q24, [x22, #0x30] + shl v23.16b, v31.16b, #0x4 + shl v22.16b, v30.16b, #0x4 + ldr q21, [x22, #0x40] + ldr q20, [x22, #0x50] + and v31.16b, v31.16b, v11.16b + and v30.16b, v30.16b, v11.16b + ldr q19, [x22, #0x60] + ldr q18, [x22, #0x70] + shl v17.16b, v27.16b, #0x4 + shl v16.16b, v26.16b, #0x4 + KAI_ASM_INST(0x4e97a7aa) // smmla v10.4s, v29.16b, v23.16b + KAI_ASM_INST(0x4e96a7a9) // smmla v9.4s, v29.16b, v22.16b + and v27.16b, v27.16b, v11.16b + add x22, x22, #0x80 + KAI_ASM_INST(0x4e97a788) // smmla v8.4s, v28.16b, v23.16b + KAI_ASM_INST(0x4e96a787) // smmla v7.4s, v28.16b, v22.16b + and v26.16b, v26.16b, v11.16b + KAI_ASM_INST(0x4e91a72a) // smmla v10.4s, v25.16b, v17.16b + KAI_ASM_INST(0x4e90a729) // smmla v9.4s, v25.16b, v16.16b + KAI_ASM_INST(0x4e91a708) // smmla v8.4s, v24.16b, v17.16b + KAI_ASM_INST(0x4e90a707) // smmla v7.4s, v24.16b, v16.16b + KAI_ASM_INST(0x4e9fa6aa) // smmla v10.4s, v21.16b, v31.16b + KAI_ASM_INST(0x4e9ea6a9) // smmla v9.4s, v21.16b, v30.16b + KAI_ASM_INST(0x4e9fa688) // smmla v8.4s, v20.16b, v31.16b + KAI_ASM_INST(0x4e9ea687) // smmla v7.4s, v20.16b, v30.16b + KAI_ASM_INST(0x4e9ba66a) // smmla v10.4s, v19.16b, v27.16b + KAI_ASM_INST(0x4e9aa669) // smmla v9.4s, v19.16b, v26.16b + KAI_ASM_INST(0x4e9ba648) // smmla v8.4s, v18.16b, v27.16b + KAI_ASM_INST(0x4e9aa647) // smmla v7.4s, v18.16b, v26.16b + bgt label_13 + ld1 { v18.4s }, [x22] + ldr q17, [x26, #0x0] + add x22, x22, #0x10 + uzp1 v24.2d, v10.2d, v9.2d + ldr q23, [x26, #0x10] + ldr q16, [x22, #0x0] + uzp2 v22.2d, v10.2d, v9.2d + uzp1 v21.2d, v8.2d, v7.2d + uzp2 v20.2d, v8.2d, v7.2d + add x26, x26, #0x20 + fmul v10.4s, v17.4s, v18.s[0] + fmul v9.4s, v17.4s, v18.s[1] + fmul v8.4s, v17.4s, v18.s[2] + fmul v7.4s, v17.4s, v18.s[3] + fmul v19.4s, v23.4s, v16.s[0] + scvtf v24.4s, v24.4s + fmul v18.4s, v23.4s, v16.s[1] + scvtf v22.4s, v22.4s + fmul v17.4s, v23.4s, v16.s[2] + scvtf v21.4s, v21.4s + fmul v16.4s, v23.4s, v16.s[3] + scvtf v20.4s, v20.4s + fmla v10.4s, v24.4s, v19.4s + fmla v9.4s, v22.4s, v18.4s + fmla v8.4s, v21.4s, v17.4s + fmla v7.4s, v20.4s, v16.4s + ldr q18, [x26, #0x0] + ld1r { v17.4s }, [x11] + add x20, x11, #0x4 + cmp x25, #0x4 + ld1r { v16.4s }, [x20] + add x26, x26, #0x10 + fadd v10.4s, v10.4s, v18.4s + fadd v9.4s, v9.4s, v18.4s + fadd v8.4s, v8.4s, v18.4s + fadd v7.4s, v7.4s, v18.4s + fmax v10.4s, v10.4s, v17.4s + fmax v9.4s, v9.4s, v17.4s + fmax v8.4s, v8.4s, v17.4s + fmax v7.4s, v7.4s, v17.4s + fmin v10.4s, v10.4s, v16.4s + fmin v9.4s, v9.4s, v16.4s + fmin v8.4s, v8.4s, v16.4s + fmin v7.4s, v7.4s, v16.4s + blt label_15 + mov x20, x14 + cmp x13, #0x1 + str q10, [x20, #0x0] + add x20, x20, x12 + ble label_18 + cmp x13, #0x2 + str q9, [x20, #0x0] + add x20, x20, x12 + ble label_18 + cmp x13, #0x3 + str q8, [x20, #0x0] + add x20, x20, x12 + ble label_18 + str q7, [x20, #0x0] + b label_18 +KAI_ASM_LABEL(label_15) // Row tail: Partial output + mov x23, x14 + cmp x13, #0x1 + add x22, x23, x12 + csel x22, x22, x23, GT + cmp x13, #0x2 + add x21, x23, x12, LSL #1 + csel x21, x21, x22, GT + cmp x13, #0x3 + add x20, x21, x12 + csel x20, x20, x21, GT + tbz x25, #1, label_16 + st1 { v7.d }[0], [x20], #0x8 + st1 { v8.d }[0], [x21], #0x8 + st1 { v9.d }[0], [x22], #0x8 + st1 { v10.d }[0], [x23], #0x8 + tbz x25, #0, label_17 + st1 { v7.s }[2], [x20] + st1 { v8.s }[2], [x21] + st1 { v9.s }[2], [x22] + st1 { v10.s }[2], [x23] + b label_17 +KAI_ASM_LABEL(label_16) // Row tail: Output block 0: partial_1_0 + st1 { v7.s }[0], [x20] + st1 { v8.s }[0], [x21] + st1 { v9.s }[0], [x22] + st1 { v10.s }[0], [x23] +KAI_ASM_LABEL(label_17) // Row tail: Output block 0: Done +KAI_ASM_LABEL(label_18) // Row tail: Output stage exit + subs x25, x25, #0x4 + add x14, x14, #0x10 + bgt label_12 + subs x13, x13, #0x4 + add x17, x17, x7 + mov x14, x24 + bgt label_11 +KAI_ASM_LABEL(label_19) // Row tail: Row loop skip + ldp x22, x23, [sp, 16] + ldp x24, x25, [sp, 32] + ldp x26, x27, [sp, 48] + ldr x28, [sp, 64] + ldp d10, d11, [sp, 72] + ldp d8, d9, [sp, 88] + ldp x20, x21, [sp], 112 + ret + KAI_ASM_FUNCTION_END(kai_kernel_matmul_clamp_f32_qsi8dxp4x8_qai4cxp4x8_8x4_neon_i8mm) + + KAI_ASM_END diff --git a/kai/ukernels/matmul/matmul_clamp_f32_qsi8dxp_qai4cxp/kai_matmul_clamp_f32_qsi8dxp_qai4cxp_interface.h b/kai/ukernels/matmul/matmul_clamp_f32_qsi8dxp_qai4cxp/kai_matmul_clamp_f32_qsi8dxp_qai4cxp_interface.h new file mode 100644 index 0000000000000000000000000000000000000000..1fd19e263f7397e5d0709d6b8e3725df1faeb55d --- /dev/null +++ b/kai/ukernels/matmul/matmul_clamp_f32_qsi8dxp_qai4cxp/kai_matmul_clamp_f32_qsi8dxp_qai4cxp_interface.h @@ -0,0 +1,52 @@ +// +// SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates +// +// SPDX-License-Identifier: Apache-2.0 +// +#pragma once + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +// All micro-kernels variants of the same type share the same interfaces +// In this case, the micro-kernel type is: matmul_clamp_f32_qsi8dxp_qai4cxp + +/// Micro-kernel helper functions ("get" methods) +typedef size_t (*kai_matmul_clamp_f32_qsi8dxp_qai4cxp_get_m_step_func_t)(void); +typedef size_t (*kai_matmul_clamp_f32_qsi8dxp_qai4cxp_get_n_step_func_t)(void); +typedef size_t (*kai_matmul_clamp_f32_qsi8dxp_qai4cxp_get_mr_func_t)(void); +typedef size_t (*kai_matmul_clamp_f32_qsi8dxp_qai4cxp_get_nr_func_t)(void); +typedef size_t (*kai_matmul_clamp_f32_qsi8dxp_qai4cxp_get_kr_func_t)(void); +typedef size_t (*kai_matmul_clamp_f32_qsi8dxp_qai4cxp_get_sr_func_t)(void); +typedef size_t (*kai_matmul_clamp_f32_qsi8dxp_qai4cxp_get_lhs_packed_offset_func_t)(size_t m_idx, size_t k); +typedef size_t (*kai_matmul_clamp_f32_qsi8dxp_qai4cxp_get_rhs_packed_offset_func_t)(size_t n_idx, size_t k); +typedef size_t (*kai_matmul_clamp_f32_qsi8dxp_qai4cxp_get_dst_offset_func_t)( + size_t m_idx, size_t n_idx, size_t dst_stride); +typedef size_t (*kai_matmul_clamp_f32_qsi8dxp_qai4cxp_get_dst_size_func_t)(size_t m, size_t n); + +/// Micro-kernel core function ("run" method) +typedef void (*kai_matmul_clamp_f32_qsi8dxp_qai4cxp_run_matmul_func_t)( + size_t m, size_t n, size_t k, const void* lhs_p, const void* rhs_p, float* dst, size_t dst_stride_row, + size_t dst_stride_col, float scalar_min, float scalar_max); + +/// Micro-kernel interface +struct kai_matmul_clamp_f32_qsi8dxp_qai4cxp_ukernel { + kai_matmul_clamp_f32_qsi8dxp_qai4cxp_get_m_step_func_t get_m_step; + kai_matmul_clamp_f32_qsi8dxp_qai4cxp_get_n_step_func_t get_n_step; + kai_matmul_clamp_f32_qsi8dxp_qai4cxp_get_mr_func_t get_mr; + kai_matmul_clamp_f32_qsi8dxp_qai4cxp_get_nr_func_t get_nr; + kai_matmul_clamp_f32_qsi8dxp_qai4cxp_get_kr_func_t get_kr; + kai_matmul_clamp_f32_qsi8dxp_qai4cxp_get_sr_func_t get_sr; + kai_matmul_clamp_f32_qsi8dxp_qai4cxp_get_lhs_packed_offset_func_t get_lhs_packed_offset; + kai_matmul_clamp_f32_qsi8dxp_qai4cxp_get_rhs_packed_offset_func_t get_rhs_packed_offset; + kai_matmul_clamp_f32_qsi8dxp_qai4cxp_get_dst_offset_func_t get_dst_offset; + kai_matmul_clamp_f32_qsi8dxp_qai4cxp_get_dst_size_func_t get_dst_size; + kai_matmul_clamp_f32_qsi8dxp_qai4cxp_run_matmul_func_t run_matmul; +}; + +#ifdef __cplusplus +} +#endif diff --git a/kai/ukernels/matmul/pack/kai_lhs_quant_pack_qsi8dxpscalef32_f32_neon.c b/kai/ukernels/matmul/pack/kai_lhs_quant_pack_qsi8dxpscalef32_f32_neon.c new file mode 100644 index 0000000000000000000000000000000000000000..f6d28e96cacbb50d1f3ab119e1834e3af5e58d61 --- /dev/null +++ b/kai/ukernels/matmul/pack/kai_lhs_quant_pack_qsi8dxpscalef32_f32_neon.c @@ -0,0 +1,188 @@ +// +// SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates +// +// SPDX-License-Identifier: Apache-2.0 +// + +#if !defined(__aarch64__) +#error This file must be compiled for AArch64. +#else // Architectural features check. + +#include "kai_lhs_quant_pack_qsi8dxpscalef32_f32_neon.h" + +#include +#include +#include +#include + +#include "kai/kai_common.h" + +static const size_t kai_num_bytes_sum = sizeof(float); +static const size_t kai_num_bytes_multiplier = sizeof(float); + +inline static size_t kai_k_roundedup(size_t k, size_t kr, size_t sr) { + // Since we pack a float and int32 value at the end of the row, + // we must make sure that k is a multiple of 4 for memory alignment. + size_t kr_sr_roundedup4 = kai_roundup(kr * sr, 4); + return kai_roundup(k, kr_sr_roundedup4); +} + +inline static size_t kai_get_lhs_packed_stride(size_t k, size_t mr, size_t kr, size_t sr) { + const size_t k_internal = kai_k_roundedup(k, kr, sr); + + KAI_ASSERT((k_internal % 2) == 0); + + return mr * (k_internal * sizeof(int8_t) + kai_num_bytes_multiplier + kai_num_bytes_sum); +} + +size_t kai_get_m_step_lhs_quant_pack_qsi8dxpscalef32_f32_neon(size_t mr) { + KAI_UNUSED(mr); + return 1; +} + +size_t kai_get_lhs_offset_lhs_quant_pack_qsi8dxpscalef32_f32_neon(size_t m_idx, size_t lhs_stride) { + return m_idx * lhs_stride; +} + +size_t kai_get_lhs_packed_offset_lhs_quant_pack_qsi8dxpscalef32_f32_neon( + size_t m_idx, size_t k, size_t mr, size_t kr, size_t sr) { + // It always points to the beginning of the row + return (m_idx / mr) * kai_get_lhs_packed_stride(k, mr, kr, sr); +} + +size_t kai_get_lhs_packed_size_lhs_quant_pack_qsi8dxpscalef32_f32_neon( + size_t m, size_t k, size_t mr, size_t kr, size_t sr) { + const size_t num_rows = kai_roundup(m, mr) / mr; + + return (num_rows * kai_get_lhs_packed_stride(k, mr, kr, sr)); +} + +void kai_run_lhs_quant_pack_qsi8dxpscalef32_f32_neon( + size_t m, size_t k, size_t mr, size_t kr, size_t sr, size_t m_idx_start, const float* restrict lhs, + size_t lhs_stride, void* restrict lhs_packed) { + KAI_ASSERT((kr % sr) == 0); + KAI_ASSUME((kr / sr) % 8 == 0); + + if (m == 0) { + return; + } + + const size_t num_rows = m; + + const float* src_ptr = lhs; + + const size_t lhs_packed_stride = kai_get_lhs_packed_stride(k, mr, kr, sr); + const size_t k_internal = kai_k_roundedup(k, kr, sr); + const int32_t k_block_len = (int32_t)(kr / sr); + + const int32_t num_blocks_k = (int32_t)(k / k_block_len); + const int32_t num_blocks_k_internal = (int32_t)(k_internal / k_block_len); + + for (size_t row_idx = 0; row_idx < num_rows; ++row_idx) { + float absmax = -FLT_MAX; + // Find absmax for each channel + int32_t k_idx = 0; + float32x4_t vabsmax = vdupq_n_f32(-FLT_MAX); + for (; k_idx <= ((int32_t)k - 8); k_idx += 8) { + const float32x4_t src0_0 = vld1q_f32(src_ptr + 0 + (size_t)k_idx); + const float32x4_t src0_1 = vld1q_f32(src_ptr + 4 + (size_t)k_idx); + // Calculate the max + float32x4_t vabsmax0 = vmaxq_f32(vabsmax, vabsq_f32(src0_0)); + float32x4_t vabsmax1 = vmaxq_f32(vabsmax, vabsq_f32(src0_1)); + vabsmax = vmaxq_f32(vabsmax0, vabsmax1); + } + + // Get the absmax + absmax = vmaxvq_f32(vabsmax); + + for (; k_idx < (int32_t)k; ++k_idx) { + const float src0_0 = *(src_ptr + (size_t)k_idx); + absmax = KAI_MAX(src0_0, absmax); + } + + // Maximum/minimum int8 values + const float qmax = (float)INT8_MAX; + + // Get the scale and reciprocal to quantize + const float scale0 = absmax == 0.0F ? 0.0F : qmax / absmax; + const float recip_scale0 = scale0 ? 1.0F / scale0 : 0.0F; + + const size_t dst_x = ((row_idx + m_idx_start) % mr); + + uint8_t* dst_ptr = (uint8_t*)lhs_packed + (dst_x * k_block_len * sizeof(int8_t)); + + // Quantize the channels + int32_t block_idx = 0; + int32_t qsum = 0; + for (; block_idx < num_blocks_k; ++block_idx) { + // Clamp at the last valid k-index + const int32_t k_idx_start = block_idx * k_block_len; + + const float32x4_t src_0 = vld1q_f32(src_ptr + k_idx_start); + const float32x4_t src_1 = vld1q_f32(src_ptr + k_idx_start + 4); + + // Scale the values + const float32x4_t v0_f32 = vmulq_n_f32(src_0, scale0); + const float32x4_t v1_f32 = vmulq_n_f32(src_1, scale0); + const int32x4_t v0_s32 = vcvtnq_s32_f32(v0_f32); + const int32x4_t v1_s32 = vcvtnq_s32_f32(v1_f32); + + const int16x4_t v0_s16 = vqmovn_s32(v0_s32); + const int16x4_t v1_s16 = vqmovn_s32(v1_s32); + int16x8_t v_s16 = vcombine_s16(v0_s16, v1_s16); + + const int8x8_t v0_s8 = vqmovn_s16(v_s16); + vst1_s8((int8_t*)(dst_ptr), v0_s8); + dst_ptr += 8 * sizeof(int8_t); + dst_ptr += (mr - 1) * k_block_len * sizeof(int8_t); + + // Update the sum + qsum += vaddlv_s8(v0_s8); + } + for (; block_idx < num_blocks_k_internal; ++block_idx) { + // left over k + for (size_t k_block_idx = 0; k_block_idx < (size_t)k_block_len; ++k_block_idx) { + // Clamp at the last valid k-index + const size_t k_idx_start = KAI_MIN((size_t)(block_idx * k_block_len) + k_block_idx, k - 1); + + const float src0_0 = *(src_ptr + k_idx_start); + + // Scale the values + int32_t v0_s32 = (int32_t)(roundf(src0_0 * scale0)); + + v0_s32 = KAI_MAX(v0_s32, INT8_MIN); + v0_s32 = KAI_MIN(v0_s32, INT8_MAX); + + *((int8_t*)(dst_ptr)) = (int8_t)v0_s32; + if ((size_t)(block_idx * k_block_len) + k_block_idx <= k - 1) { + qsum += v0_s32; + } + dst_ptr += sizeof(int8_t); + } + dst_ptr += (mr - 1) * k_block_len * sizeof(int8_t); + } + + dst_ptr = (uint8_t*)lhs_packed + mr * (k_internal * sizeof(int8_t)); + + dst_ptr += dst_x * kai_num_bytes_sum; + + // LHS offset at the beginning of the row + *((float*)(dst_ptr)) = ((float)qsum) * recip_scale0; + + // Assuming the same sizeof() for kai_num_bytes_sum and kai_num_bytes_multiplier + KAI_ASSERT(kai_num_bytes_sum == kai_num_bytes_multiplier); + + dst_ptr += mr * kai_num_bytes_sum; + + // Store the scale quantization params + *((float*)(dst_ptr)) = recip_scale0; + + src_ptr += (lhs_stride / sizeof(float)); + + // Move to the next row if we have interleaved all Mr rows + if ((((row_idx + 1) + m_idx_start) % mr) == 0) { + lhs_packed = (void*)((uint8_t*)lhs_packed + lhs_packed_stride); + } + } +} +#endif // Architectural features check. diff --git a/kai/ukernels/matmul/pack/kai_lhs_quant_pack_qsi8dxpscalef32_f32_neon.h b/kai/ukernels/matmul/pack/kai_lhs_quant_pack_qsi8dxpscalef32_f32_neon.h new file mode 100644 index 0000000000000000000000000000000000000000..9bafa5538398e17941f18a6bdb1d5cefc87c4fb7 --- /dev/null +++ b/kai/ukernels/matmul/pack/kai_lhs_quant_pack_qsi8dxpscalef32_f32_neon.h @@ -0,0 +1,78 @@ +// +// SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates +// +// SPDX-License-Identifier: Apache-2.0 +// +#pragma once + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/// Gets the m step value. +/// The micro-kernel can process any M values. However, the starting M index to +/// be processed must be a multiple of m step. +/// +/// @param[in] mr The number of M rows to interleave on the same output row. +/// +/// @return the m step value +size_t kai_get_m_step_lhs_quant_pack_qsi8dxpscalef32_f32_neon(size_t mr); + +/// Gets the offset in bytes for the LHS matrix (not packed) +/// +/// This function should be called before passing the pointer to the LHS matrix to the micro-kernel. +/// +/// @param[in] m_idx Row index in the LHS matrix (not packed). +/// @param[in] lhs_stride The number of bytes in in each row of the LHS matrix (not packed) +/// +/// @return the offset in bytes to the LHS matrix +size_t kai_get_lhs_offset_lhs_quant_pack_qsi8dxpscalef32_f32_neon(size_t m_idx, size_t lhs_stride); + +/// Gets the offset in bytes for the packed LHS matrix, +/// which contains the packed 8-bit quantized symmetric per-row (qsi8dx) values. +/// +/// This function should be called before passing the pointer to the packed LHS matrix to the micro-kernel. +/// +/// @param[in] m_idx Row index in the LHS matrix (not packed). It must be a multiple of mr. +/// @param[in] k Total number of columns in the LHS matrix (not packed). +/// @param[in] mr The number of M rows to interleave on the same output row. +/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel. +/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr. +/// +/// @return the offset in bytes to the packed LHS matrix +size_t kai_get_lhs_packed_offset_lhs_quant_pack_qsi8dxpscalef32_f32_neon( + size_t m_idx, size_t k, size_t mr, size_t kr, size_t sr); + +/// Gets the size in bytes for the quantized and packed LHS matrix +/// +/// @param[in] m Total number of rows in the LHS matrix (not packed). +/// @param[in] k Total number of columns in the LHS matrix (not packed). +/// @param[in] mr The number of M rows to interleave on the same output row. +/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel. +/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr. +/// +/// @return the packed LHS matrix size in bytes +size_t kai_get_lhs_packed_size_lhs_quant_pack_qsi8dxpscalef32_f32_neon( + size_t m, size_t k, size_t mr, size_t kr, size_t sr); + +/// Run the micro-kernel to quantize and pack the LHS matrix. +/// +/// @param[in] m The number of output rows written. +/// @param[in] k The number of channels. The common dimension of LHS & RHS. It must be multiple of 8. +/// @param[in] mr The number of M rows to interleave on the same output row. +/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel. +/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr. +/// However, kr must be multiple of sr. +/// @param[in] m_idx_start The starting M index. +/// @param[in] lhs LHS of the vector-by-matrix. +/// @param[in] lhs_stride Stride in bytes between two rows of LHS. +/// @param[out] lhs_packed The quantized and packed LHS matrix. +void kai_run_lhs_quant_pack_qsi8dxpscalef32_f32_neon( + size_t m, size_t k, size_t mr, size_t kr, size_t sr, size_t m_idx_start, const float* lhs, size_t lhs_stride, + void* lhs_packed); + +#ifdef __cplusplus +} +#endif diff --git a/kai/ukernels/matmul/pack/kai_rhs_pack_nxk_qai4cxp_qau4cxs1s0_f32_f32_f32_neon.c b/kai/ukernels/matmul/pack/kai_rhs_pack_nxk_qai4cxp_qau4cxs1s0_f32_f32_f32_neon.c new file mode 100644 index 0000000000000000000000000000000000000000..c255e6b5357a187bd9265e352ffcdafd77227483 --- /dev/null +++ b/kai/ukernels/matmul/pack/kai_rhs_pack_nxk_qai4cxp_qau4cxs1s0_f32_f32_f32_neon.c @@ -0,0 +1,140 @@ +// +// SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates +// +// SPDX-License-Identifier: Apache-2.0 +// + +#if !defined(__aarch64__) +#error This file must be compiled for AArch64. +#else // Architectural features check. +#include "kai_rhs_pack_nxk_qai4cxp_qau4cxs1s0_f32_f32_f32_neon.h" + +#include +#include + +#include "kai/kai_common.h" +static const size_t kai_num_bytes_offset_rhs = sizeof(float); +static const size_t kai_num_bytes_multiplier_rhs = sizeof(float); +static const size_t kai_num_bytes_bias = sizeof(float); + +inline static size_t kai_k_roundedup(size_t k, size_t kr, size_t sr) { + // Since we pack a float and int32 value at the end of the row, + // we must make sure that k is a multiple of 4 for alignment + size_t kr_sr_roundedup4 = kai_roundup(kr * sr, 4); + return kai_roundup(k, kr_sr_roundedup4); +} + +size_t kai_get_rhs_offset_rhs_pack_nxk_qai4cxp_qau4cxs1s0_f32_f32_f32_neon(size_t n_idx, size_t rhs_stride) { + return n_idx * rhs_stride; +} + +size_t kai_get_rhs_packed_stride_rhs_pack_nxk_qai4cxp_qau4cxs1s0_f32_f32_f32_neon( + size_t k, size_t nr, size_t kr, size_t sr) { + const size_t k_internal = kai_k_roundedup(k, kr, sr); + KAI_ASSERT((k_internal % 2) == 0); + + return nr * ((k_internal / 2) + kai_num_bytes_multiplier_rhs + kai_num_bytes_offset_rhs + kai_num_bytes_bias); +} + +size_t kai_get_rhs_packed_offset_rhs_pack_nxk_qai4cxp_qau4cxs1s0_f32_f32_f32_neon( + size_t n_idx, size_t k, size_t nr, size_t kr, size_t sr) { + KAI_ASSERT((n_idx % nr) == 0); + + return (n_idx / nr) * kai_get_rhs_packed_stride_rhs_pack_nxk_qai4cxp_qau4cxs1s0_f32_f32_f32_neon(k, nr, kr, sr); +} + +size_t kai_get_rhs_packed_size_rhs_pack_nxk_qai4cxp_qau4cxs1s0_f32_f32_f32_neon( + size_t n, size_t k, size_t nr, size_t kr, size_t sr) { + const size_t num_rows = kai_roundup(n, nr) / nr; + + return num_rows * kai_get_rhs_packed_stride_rhs_pack_nxk_qai4cxp_qau4cxs1s0_f32_f32_f32_neon(k, nr, kr, sr); +} + +void kai_run_rhs_pack_nxk_qai4cxp_qau4cxs1s0_f32_f32_f32_neon( + size_t num_groups, size_t n, size_t k, size_t nr, size_t kr, size_t sr, const uint8_t* rhs, const void* zero, + const void* bias, const void* scale, void* rhs_packed, size_t extra_bytes, + const struct kai_rhs_pack_nxk_qai4cxp_params* params) { + KAI_ASSERT(num_groups == 1); + KAI_ASSERT(extra_bytes == 0); + KAI_ASSERT((kr % sr) == 0); + KAI_ASSERT(rhs != NULL); + KAI_ASSERT(scale != NULL); + KAI_ASSERT(rhs_packed != NULL); + KAI_ASSERT(params != NULL); + + KAI_ASSERT(params->rhs_zero_point == 8); + KAI_ASSERT(params->lhs_zero_point == 1); + + const int32_t rhs_zero_point = params->rhs_zero_point; + const size_t rhs_stride = kai_roundup(k, 2) / 2; + const size_t rhs_packed_stride = + kai_get_rhs_packed_stride_rhs_pack_nxk_qai4cxp_qau4cxs1s0_f32_f32_f32_neon(k, nr, kr, sr); + + const size_t k_internal = kai_k_roundedup(k, kr, sr); + const size_t dst_num_rows = kai_roundup(n, nr) / nr; + const size_t dst_num_bytes_per_row = nr * (k_internal / 2); + const size_t block_length_in_bytes = kr / sr; + const size_t k_interleaved_v = 16U; + for (size_t dst_row_idx = 0; dst_row_idx < dst_num_rows; ++dst_row_idx) { + uint8_t* dst_row = (uint8_t*)rhs_packed + dst_row_idx * rhs_packed_stride; + for (size_t dst_byte_idx = 0; dst_byte_idx < dst_num_bytes_per_row; ++dst_byte_idx) { + const size_t block_idx = dst_byte_idx / block_length_in_bytes; + const size_t block_byte_idx = dst_byte_idx % block_length_in_bytes; + const size_t super_block_idx = block_idx / nr; + const size_t nr_idx = block_idx % nr; + const size_t k_adjustment = + ((block_byte_idx + super_block_idx * block_length_in_bytes) / k_interleaved_v) * k_interleaved_v; + const size_t k0_idx = block_byte_idx + super_block_idx * block_length_in_bytes + k_adjustment; + const size_t k1_idx = k0_idx + k_interleaved_v; + const size_t n0_idx = dst_row_idx * nr + nr_idx; + // Clamp the index to avoid out-of-bound reads + const size_t n0_valid_idx = KAI_MIN(n0_idx, n - 1); + const size_t src_addr_byte0 = (k0_idx / 2) + n0_valid_idx * rhs_stride; + const size_t src_addr_byte1 = (k1_idx / 2) + n0_valid_idx * rhs_stride; + uint8_t byte0 = rhs_zero_point | rhs_zero_point << 4; + uint8_t byte1 = rhs_zero_point | rhs_zero_point << 4; + if (k0_idx < k) { + byte0 = rhs[src_addr_byte0]; + } + if (k1_idx < k) { + byte1 = rhs[src_addr_byte1]; + } + const size_t shift_right_x0 = (k0_idx % 2) * 4; + const size_t shift_right_x1 = (k1_idx % 2) * 4; + const uint8_t src_x0_lo = (byte0 >> shift_right_x0) & 0x0F; + const uint8_t src_x0_hi = (byte1 >> shift_right_x1) & 0x0F; + const uint8_t dst_qs0 = src_x0_lo | (src_x0_hi << 4); + *dst_row = dst_qs0 ^ 0x88; + dst_row += sizeof(uint8_t); + } + // Adjust the zero point + if (zero == NULL) { + memset(dst_row, 0, nr * kai_num_bytes_offset_rhs); + dst_row += nr * sizeof(float); + } else { + for (size_t i = 0; i < nr; ++i) { + const size_t src_row_idx = KAI_MIN(dst_row_idx * nr + i, n - 1); + *((float*)(dst_row)) = *((const float*)zero + src_row_idx); + dst_row += sizeof(float); + } + } + // Adjust the scales + for (size_t i = 0; i < nr; ++i) { + // Clamp the row index to avoid out-of-bound reads + const size_t src_row_idx = KAI_MIN(dst_row_idx * nr + i, n - 1); + *((float*)(dst_row)) = *((const float*)scale + src_row_idx) * 0.0625F; + dst_row += sizeof(float); + } + // Set the bias + if (bias == NULL) { + memset(dst_row, 0, nr * kai_num_bytes_bias); + } else { + for (size_t i = 0; i < nr; ++i) { + // Clamp the row index to avoid out-of-bound reads + const size_t src_row_idx = KAI_MIN(dst_row_idx * nr + i, n - 1); + ((float*)dst_row)[i] = *((const float*)bias + src_row_idx); + } + } + } +} +#endif diff --git a/kai/ukernels/matmul/pack/kai_rhs_pack_nxk_qai4cxp_qau4cxs1s0_f32_f32_f32_neon.h b/kai/ukernels/matmul/pack/kai_rhs_pack_nxk_qai4cxp_qau4cxs1s0_f32_f32_f32_neon.h new file mode 100644 index 0000000000000000000000000000000000000000..92c77a72553fe7fb73086dd73e4eb88f65c5cd21 --- /dev/null +++ b/kai/ukernels/matmul/pack/kai_rhs_pack_nxk_qai4cxp_qau4cxs1s0_f32_f32_f32_neon.h @@ -0,0 +1,106 @@ +// +// SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates +// +// SPDX-License-Identifier: Apache-2.0 +// +#pragma once + +#include + +#include "kai/kai_common.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef kai_rhs_pack_nxk_qai4cxp_params +#define kai_rhs_pack_nxk_qai4cxp_params kai_rhs_pack_qs4cxs1s0_param +#endif + +/// Gets the offset in bytes for the RHS matrix (not packed). +/// +/// @note The int4 values are stored in a N x K matrix. Two int4 values are stored in one byte. +/// The lower order part of the byte (low) holds the first nibble (K-index + 0). +/// The higher order of the byte holds the second nibble (K-index + 1). +/// +/// @param[in] n_idx Row index in the RHS matrix (not packed). It must be a multiple of n_step. +/// @param[in] rhs_stride The number of bytes in in each row of the RHS matrix (not packed) +/// +/// @return the offset in bytes to the RHS matrix (not packed) +size_t kai_get_rhs_offset_rhs_pack_nxk_qai4cxp_qau4cxs1s0_f32_f32_f32_neon(size_t n_idx, size_t rhs_stride); + +/// Get the row stride in bytes to the packed RHS matrix +/// +/// @param[in] k In the RHS matrix (not packed), K is the number of columns. +/// @param[in] nr The number of columns written by the matmul micro-kernel. +/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel. +/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr. +/// +/// @return the stride in bytes to the packed RHS matrix +size_t kai_get_rhs_packed_stride_rhs_pack_nxk_qai4cxp_qau4cxs1s0_f32_f32_f32_neon( + size_t k, size_t nr, size_t kr, size_t sr); + +/// Gets the offset in bytes for the packed RHS matrix, which contains the packed 4-bit quantized asymmetric per-channel +/// (qai4cx) values. +/// +/// @param[in] n_idx Row index in the RHS matrix (not packed). It must be a multiple of n_step. +/// @param[in] k In the RHS matrix (not packed), K is the number of columns. +/// @param[in] nr The number of columns written by the matmul micro-kernel +/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel. +/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr. +/// +/// @return the offset in bytes to the packed RHS matrix +size_t kai_get_rhs_packed_offset_rhs_pack_nxk_qai4cxp_qau4cxs1s0_f32_f32_f32_neon( + size_t n_idx, size_t k, size_t nr, size_t kr, size_t sr); + +/// @brief Gets the size in bytes for the packed RHS matrix +/// +/// @param[in] n The number of rows in the RHS matrix (not packed) +/// @param[in] k The number of columns in the RHS matrix (not packed). +/// @param[in] nr The number of columns written by the matmul micro-kernel +/// @param[in] kr The number of columns loaded in the single inner most loop of the matmul micro-kernel. +/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr. +/// +/// @return the packed RHS matrix size in bytes +size_t kai_get_rhs_packed_size_rhs_pack_nxk_qai4cxp_qau4cxs1s0_f32_f32_f32_neon( + size_t n, size_t k, size_t nr, size_t kr, size_t sr); + +/// Run the micro-kernel to pack the RHS matrix. +/// +/// @note The int4 values are stored in a N x K matrix. Two int4 values are stored in one byte. +/// The lower order part of the byte (low) holds the first nibble (K-index + 0). +/// The higher order of the byte holds the second nibble (K-index + 1). +/// +/// @param[in] num_groups The number of groups. It must be 1. +/// @param[in] n The number of rows. +/// @param[in] k The common dimension between the LHS and RHS matrix (K). It must be an even value. +/// @param[in] nr The number of N rows to interleave on the same output row. +/// @param[in] kr The number of K values loaded in the single inner most loop of the matmul micro-kernel. +/// @param[in] sr The number of kr splits. It can be 1 (no splits) up to kr. +/// However, kr must be multiple of sr. +/// @param[in] rhs The RHS matrix containing the 4-bit values. +/// Size in bytes is expected to be greater than or equal to n * k * (sizeof(uint8_t) / 2). +/// @param[in] zero The zero point. +/// @param[in] bias The biases. +/// @param[in] scale The scale for each output channel. +/// @param[out] rhs_packed The packed RHS matrix. +/// @param[in] extra_bytes Extra bytes to append to the end of each row of the packed RHS matrix. +/// @param[in] params Parameters for the micro-kernel. +void kai_run_rhs_pack_nxk_qai4cxp_qau4cxs1s0_f32_f32_f32_neon( + size_t num_groups, // + size_t n, // + size_t k, // + size_t nr, // + size_t kr, // + size_t sr, // + const uint8_t* rhs, // + const void* zero, // + const void* bias, // + const void* scale, // + void* rhs_packed, // + size_t extra_bytes, // + const struct kai_rhs_pack_nxk_qai4cxp_params* params); + +#ifdef __cplusplus +} +#endif diff --git a/test/tests/matmul_clamp_f32_qsi8dxp_qai4cxp_test.cpp b/test/tests/matmul_clamp_f32_qsi8dxp_qai4cxp_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..253afd4fb6cd6da208e1c5e70b46baf514c6d5df --- /dev/null +++ b/test/tests/matmul_clamp_f32_qsi8dxp_qai4cxp_test.cpp @@ -0,0 +1,230 @@ +// +// SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates +// +// SPDX-License-Identifier: Apache-2.0 +// + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "kai/ukernels/matmul/matmul_clamp_f32_qsi8dxp_qai4cxp/kai_matmul_clamp_f32_qsi8dxp1x8_qai4cxp4x8_1x4_neon_dotprod.h" +#include "kai/ukernels/matmul/matmul_clamp_f32_qsi8dxp_qai4cxp/kai_matmul_clamp_f32_qsi8dxp4x8_qai4cxp4x8_8x4_neon_i8mm.h" +#include "kai/ukernels/matmul/matmul_clamp_f32_qsi8dxp_qai4cxp/kai_matmul_clamp_f32_qsi8dxp_qai4cxp_interface.h" +#include "kai/ukernels/matmul/pack/kai_lhs_quant_pack_qsi8dxpscalef32_f32_neon.h" +#include "kai/ukernels/matmul/pack/kai_rhs_pack_nxk_qai4cxp_qau4cxs1s0_f32_f32_f32_neon.h" +#include "test/common/compare.hpp" +#include "test/common/cpu_info.hpp" +#include "test/common/data_format.hpp" +#include "test/common/float16.hpp" +#include "test/common/int4.hpp" +#include "test/common/matrix_portion.hpp" +#include "test/common/memory.hpp" +#include "test/common/round.hpp" +#include "test/common/test_suite.hpp" +#include "test/reference/cast.hpp" +#include "test/reference/clamp.hpp" +#include "test/reference/fill.hpp" +#include "test/reference/matmul.hpp" +#include "test/reference/pack.hpp" +#include "test/reference/pad.hpp" +#include "test/reference/quantize.hpp" + +namespace kai::test { + +static const std::array, 2> + variants_kai_matmul_clamp_f32_qsi8dxp_qai4cxp = { + {{UKERNEL_MATMUL_VARIANT(clamp_f32_qsi8dxp1x8_qai4cxp4x8_1x4_neon_dotprod), + "kai_matmul_clamp_f32_qsi8dxp1x8_qai4cxp4x8_1x4_neon_dotprod", cpu_has_dotprod}, + {UKERNEL_MATMUL_VARIANT(clamp_f32_qsi8dxp4x8_qai4cxp4x8_8x4_neon_i8mm), + "kai_matmul_clamp_f32_qsi8dxp4x8_qai4cxp4x8_8x4_neon_i8mm", cpu_has_i8mm}}}; + +class MatMulTest_f32_qsi8dxp_qai4cxp : public ::testing::TestWithParam {}; + +TEST_P(MatMulTest_f32_qsi8dxp_qai4cxp, EndToEnd) { + const auto& [variant_index, matmul_shape, portion, has_bias] = GetParam(); + const auto& ukernel_variant = variants_kai_matmul_clamp_f32_qsi8dxp_qai4cxp.at(variant_index); + + if (ukernel_variant.fn_is_supported && !ukernel_variant.fn_is_supported()) { + GTEST_SKIP() << "CPU features are not supported by current CPU"; + } + + const std::uint32_t seed = 0; + + const size_t M = matmul_shape.m; + const size_t N = matmul_shape.n; + const size_t K = matmul_shape.k; + + const auto mr = ukernel_variant.interface.get_mr(); + const auto nr = ukernel_variant.interface.get_nr(); + const auto kr = ukernel_variant.interface.get_kr(); + const auto sr = ukernel_variant.interface.get_sr(); + + if (mr == 1 && M > 1) { + GTEST_SKIP() << "Kernel does not support M != 1"; + } + + auto m_step = ukernel_variant.interface.get_m_step(); + ASSERT_TRUE(m_step % mr == 0); + + auto n_step = ukernel_variant.interface.get_n_step(); + ASSERT_TRUE(n_step % nr == 0); + + const auto rect = portion.compute_portion(M, N, m_step, n_step); + if (rect.height() == 0 || rect.width() == 0) { + GTEST_SKIP() << "Empty dimension of matrix(" << rect.width() << "," << rect.height() << ")"; + } + + // Generates input data. + const auto ref_lhs = fill_random(M * K, seed + 0); + const auto ref_rhs = fill_random(N * K, seed + 1); + std::vector ref_biases; + + if (has_bias) { + ref_biases = fill_random(N, seed + 2); + } + // Runs the reference implementation. + // * Quantizes the LHS matrix using 8-bit symmetric quantization. + // * Quantizes the RHS matrix using 8-bit asymmetric quantization. + // * Performs GEMM. + const auto [ref_lhs_qvalues, ref_lhs_scales] = + quantize_symmetric_per_block_dynamic(ref_lhs.data(), M, K, K); + const auto [ref_rhs_qai4, ref_rhs_scales, ref_rhs_zero_points] = + quantize_asymmetric_per_block_dynamic(ref_rhs.data(), N, K, K); + + const auto ref_dst_no_clamp = + matmul_nt_t_quantized( + M, N, K, ref_lhs_qvalues.data(), ref_lhs_scales.data(), nullptr, 1, K, ref_rhs_qai4.data(), + ref_rhs_scales.data(), ref_rhs_zero_points.data(), 1, K, has_bias ? ref_biases.data() : nullptr, nullptr, + nullptr, 1); + + // Clamps the reference output. + const auto clamp_ratio = 0.8F; + const auto [clamp_min, clamp_max] = find_clamp_range(ref_dst_no_clamp.data(), M * N, clamp_ratio); + const auto ref_dst = clamp(ref_dst_no_clamp.data(), M * N, clamp_min, clamp_max); + + // Runs the LHS packing micro-kernel. + const auto lhs_start_row = rect.start_row(); + const auto imp_packed_lhs_size = kai_get_lhs_packed_size_lhs_quant_pack_qsi8dxpscalef32_f32_neon(M, K, mr, kr, sr); + std::vector imp_packed_lhs(imp_packed_lhs_size); + + auto lhs_stride = K * sizeof(float); + auto lhs_offset = kai_get_lhs_offset_lhs_quant_pack_qsi8dxpscalef32_f32_neon(lhs_start_row, lhs_stride); + auto lhs_packed_offset = + kai_get_lhs_packed_offset_lhs_quant_pack_qsi8dxpscalef32_f32_neon(lhs_start_row, K, mr, kr, sr); + auto lhs_matmul_offset = ukernel_variant.interface.get_lhs_packed_offset(lhs_start_row, K); + + ASSERT_EQ(lhs_packed_offset, lhs_matmul_offset); + + kai_run_lhs_quant_pack_qsi8dxpscalef32_f32_neon( + rect.height() /* m */, K, mr, kr, sr, 0, reinterpret_cast(ref_lhs.data() + lhs_offset), + lhs_stride, imp_packed_lhs.data() + lhs_packed_offset); + + // Prepare the offsets as the RHS packing kernel expects the scaled zero-points in float. + const size_t ref_zp_size = N; + const size_t ref_zp_size_in_bytes = ref_zp_size * sizeof(float); + std::vector ref_rhs_zp_f32(ref_zp_size_in_bytes); + for (size_t i = 0; i < ref_zp_size; ++i) { + reinterpret_cast(ref_rhs_zp_f32.data())[i] = + -reinterpret_cast(ref_rhs_zero_points.data())[i] * + reinterpret_cast(ref_rhs_scales.data())[i]; + } + + // Cast to unsigned int + const auto ref_rhs_qau4 = cast_qsu4_qsi4(ref_rhs_qai4.data(), N * K); + const auto ref_rhs_qau4_padded = pad_row( + ref_rhs_qau4.data(), N, K, K, round_up_multiple(K, 2), round_up_division(N * round_up_multiple(K, 2), 2)); + const auto imp_packed_rhs_size = + kai_get_rhs_packed_size_rhs_pack_nxk_qai4cxp_qau4cxs1s0_f32_f32_f32_neon(N, K, nr, kr, sr); + std::vector imp_packed_rhs(imp_packed_rhs_size); + const auto rhs_start_row = rect.start_col(); + auto rhs_packed_offset = + kai_get_rhs_packed_offset_rhs_pack_nxk_qai4cxp_qau4cxs1s0_f32_f32_f32_neon(rhs_start_row, K, nr, kr, sr); + auto rhs_matmul_offset = ukernel_variant.interface.get_rhs_packed_offset(rhs_start_row, K); + ASSERT_EQ(rhs_packed_offset, rhs_matmul_offset); + + // Runs the RHS packing micro-kernel. + kai_rhs_pack_nxk_qai4cxp_params params{}; + params.lhs_zero_point = 1; + params.rhs_zero_point = 8; + + kai_run_rhs_pack_nxk_qai4cxp_qau4cxs1s0_f32_f32_f32_neon( + 1, N, K, nr, kr, sr, ref_rhs_qau4_padded.data(), ref_rhs_zp_f32.data(), has_bias ? ref_biases.data() : nullptr, + ref_rhs_scales.data(), imp_packed_rhs.data(), 0, ¶ms); + + const auto dst_stride_row = N * sizeof(float); + const auto dst_stride_col = sizeof(float); + const auto dst_offset = + ukernel_variant.interface.get_dst_offset(rect.start_row(), rect.start_col(), dst_stride_row); + const auto ref_dst_offset = rect.start_row() * dst_stride_row + rect.start_col() * dst_stride_col; + ASSERT_EQ(dst_offset, ref_dst_offset); + + // Runs the GEMM micro-kernel. + const auto imp_dst_size = ukernel_variant.interface.get_dst_size(M, N); + ASSERT_EQ(imp_dst_size, ref_dst.size()); + std::vector imp_dst(imp_dst_size); + ukernel_variant.interface.run_matmul( + rect.height(), rect.width(), K, imp_packed_lhs.data() + lhs_matmul_offset, + imp_packed_rhs.data() + rhs_matmul_offset, reinterpret_cast(imp_dst.data() + dst_offset), + dst_stride_row, dst_stride_col, clamp_min, clamp_max); + + // Compares the output of the micro-kernels against the output of the reference implementation for the portion + // tested. + DefaultMismatchHandler handler(0, 0.02, 0, 0.05); + DataFormat dst_format = DataFormat(DataType::FP32); + const auto success = compare(imp_dst.data(), ref_dst.data(), dst_format, M, N, rect, handler); + ASSERT_TRUE(success); +} + +INSTANTIATE_TEST_SUITE_P( + MatMul, MatMulTest_f32_qsi8dxp_qai4cxp, + testing::Combine( + testing::Range(0, variants_kai_matmul_clamp_f32_qsi8dxp_qai4cxp.size()), + testing::Values( + MatMulShape{1, 2, 32}, // + MatMulShape{1, 3, 32}, // + MatMulShape{1, 4, 32}, // + MatMulShape{1, 5, 32}, // + MatMulShape{3, 3, 32}, // + MatMulShape{4, 4, 32}, // + MatMulShape{51, 15, 25}, // + MatMulShape{15, 35, 31}, // + MatMulShape{8, 32, 64}, // + MatMulShape{15, 32, 33}, // + MatMulShape{77, 99, 65}), + testing::Values( + MatrixPortion(0, 0, 1, 1), // Full matrix. + MatrixPortion(0, 0, 1, 0.25), // Leftmost portion. + MatrixPortion(0, 0.75, 1, 1), // Rightmost portion. + MatrixPortion(0, 0.5, 1, 0.8), // Somewhere Middle + MatrixPortion(0.75, 0.75, 1, 1), // Bottom-right corner. + MatrixPortion(0.75, 0, 1, 1), // Partial rows + MatrixPortion(0.4, 0.5, 0.6, 0.8) // Somewhere Middle + ), + testing::Bool()), + [](const auto& info) { + const auto variant_idx = std::get<0>(info.param); + const std::string name{variants_kai_matmul_clamp_f32_qsi8dxp_qai4cxp.at(variant_idx).name}; + const auto shape = std::get(info.param); + const auto portion = std::get<2>(info.param); + const auto has_bias = std::get<3>(info.param); + + std::stringstream sstream; + sstream << name << "__M_" << shape.m << "__N_" << shape.n << "__K_" << shape.k // + << "__PortionStartRow_" << static_cast(portion.start_row() * 1000) // + << "__PortionStartCol_" << static_cast(portion.start_col() * 1000) // + << "__PortionHeight_" << static_cast(portion.height() * 1000) // + << "__PortionWidth_" << static_cast(portion.width() * 1000) // + << (has_bias ? "__Bias" : ""); + return sstream.str(); + }); + +} // namespace kai::test