diff --git a/CHANGELOG.md b/CHANGELOG.md index e4e1b6d25d44bfc774a7ae4dccbb1fc5b7e652f7..070054f05496b7fa6b78fb1fbc39fdd66737cf88 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,8 @@ KleidiAI follows the [Semantic Versioning](https://semver.org/) specification fo - Optimizations for FEAT_DotProd. - New 1x8 block size variant of matrix multiplication of QAI8DXP LHS and QSI4C32P RHS with F32 output. - Optimizations for FEAT_DotProd. + - New 4x8 block size variant of matrix multiplication of QAI8DXP LHS and QSI4C32P RHS with F32 output. + - Optimizations for FEAT_I8MM. - New 1x8 block size variant of matrix multiplication of QAI8DXP 1x8 LHS and QSI4C32P 8x8 RHS with F32 output. - Optimizations for FEAT_DotProd. - New SME2 micro-kernels: diff --git a/CMakeLists.txt b/CMakeLists.txt index 7fc3539462d9d33a4ec4a91bc20dcba6001a90db..c4ff1c8aac333d29b75e3b9648b7bcedb90a3682 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -161,7 +161,9 @@ set(KLEIDIAI_FILES_NEON_I8MM_ASM kai/ukernels/matmul/matmul_clamp_f32_qai8dxp_qsi4c32p/kai_matmul_clamp_f32_qai8dxp4x8_qsi4c32p4x8_8x4x32_neon_i8mm_asm.S 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_qai8dxp_qsi4c32p/kai_matmul_clamp_f32_qai8dxp4x8_qsi4c32p4x8_16x4x32_neon_i8mm_asm.S + kai/ukernels/matmul/matmul_clamp_f32_qai8dxp_qsi4c32p/kai_matmul_clamp_f32_qai8dxp4x8_qsi4c32p8x8_4x8_neon_i8mm_asm.S kai/ukernels/matmul/matmul_clamp_f32_qai8dxp_qsi4c32p/kai_matmul_clamp_f32_qai8dxp4x8_qsi4c32p4x8_16x4x32_neon_i8mm.c + kai/ukernels/matmul/matmul_clamp_f32_qai8dxp_qsi4c32p/kai_matmul_clamp_f32_qai8dxp4x8_qsi4c32p8x8_4x8_neon_i8mm.c kai/ukernels/matmul/matmul_clamp_f32_qai8dxp_qsi4c32p/kai_matmul_clamp_f32_qai8dxp4x8_qsi4c32p4x8_8x4x32_neon_i8mm.c kai/ukernels/matmul/matmul_clamp_f32_qai8dxp_qsi4c32p/kai_matmul_clamp_f32_qai8dxp4x8_qsi4c32p8x8_4x8x32_neon_i8mm.c ) diff --git a/kai/ukernels/matmul/BUILD.bazel b/kai/ukernels/matmul/BUILD.bazel index 64ead662686bb540735a3477e1072b6551c7b917..74214896393d65149c6d09a5b303ad91bdba0cdc 100644 --- a/kai/ukernels/matmul/BUILD.bazel +++ b/kai/ukernels/matmul/BUILD.bazel @@ -103,6 +103,7 @@ DOTPROD_KERNELS_ASM = [ I8MM_KERNELS = [ "matmul_clamp_f32_qai8dxp_qsi4c32p/kai_matmul_clamp_f32_qai8dxp4x8_qsi4c32p4x8_16x4x32_neon_i8mm", "matmul_clamp_f32_qai8dxp_qsi4c32p/kai_matmul_clamp_f32_qai8dxp4x8_qsi4c32p4x8_8x4x32_neon_i8mm", + "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_qai8dxp_qsi4cxp/kai_matmul_clamp_f32_qai8dxp4x8_qsi4cxp4x8_4x4x32_neon_i8mm", "matmul_clamp_f32_qai8dxp_qsi4cxp/kai_matmul_clamp_f32_qai8dxp4x8_qsi4cxp4x8_8x4x32_neon_i8mm", @@ -116,6 +117,7 @@ I8MM_KERNELS = [ I8MM_KERNELS_ASM = [ "matmul_clamp_f32_qai8dxp_qsi4c32p/kai_matmul_clamp_f32_qai8dxp4x8_qsi4c32p4x8_8x4x32_neon_i8mm", "matmul_clamp_f32_qai8dxp_qsi4c32p/kai_matmul_clamp_f32_qai8dxp4x8_qsi4c32p4x8_16x4x32_neon_i8mm", + "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", ] diff --git a/kai/ukernels/matmul/matmul_clamp_f32_qai8dxp_qsi4c32p/kai_matmul_clamp_f32_qai8dxp4x8_qsi4c32p8x8_4x8_neon_i8mm.c b/kai/ukernels/matmul/matmul_clamp_f32_qai8dxp_qsi4c32p/kai_matmul_clamp_f32_qai8dxp4x8_qsi4c32p8x8_4x8_neon_i8mm.c new file mode 100644 index 0000000000000000000000000000000000000000..17a45ab960099204cc665f04cf6dfa3b5b0ceed2 --- /dev/null +++ b/kai/ukernels/matmul/matmul_clamp_f32_qai8dxp_qsi4c32p/kai_matmul_clamp_f32_qai8dxp4x8_qsi4c32p8x8_4x8_neon_i8mm.c @@ -0,0 +1,185 @@ +// +// 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_qai8dxp4x8_qsi4c32p8x8_4x8_neon_i8mm.h" + +#include +#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; + size_t num_subblocks; +} KernelArgs; + +void kai_kernel_matmul_clamp_f32_qai8dxp4x8_qsi4c32p8x8_4x8_neon_i8mm(KernelArgs* args_ptr); + +// Compute args +static const size_t kai_m_step = 4; +static const size_t kai_n_step = 8; +// Packing args +static const size_t kai_mr = 4; +static const size_t kai_nr = 8; +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_zp_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 = 2; +static const size_t kai_num_bytes_rsum_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_num_bytes_per_block_rhs(size_t bl) { + KAI_ASSUME((bl % kai_bl) == 0); + size_t num_bytes_per_block_rhs = (bl / kai_num_bytes_recip_qvalue_rhs) + kai_num_bytes_multiplier_rhs; + return num_bytes_per_block_rhs; +} + +inline static size_t kai_get_num_blocks_per_row(size_t k, size_t bl) { + KAI_ASSUME((bl % kai_bl) == 0); + + return kai_roundup(k, bl) / bl; +} + +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); + // Since the LHS matrix is asymmetric with per-row quantization, we must include the + // the number of bytes to hold the zero point value + lhs_packed_stride += kai_mr * kai_num_bytes_zp_lhs; + + return lhs_packed_stride; +} + +inline static size_t kai_get_rhs_packed_stride(size_t k, size_t bl) { + KAI_ASSUME((bl % kai_bl) == 0); + + const size_t num_blocks_per_row = kai_get_num_blocks_per_row(k, bl); + const size_t num_bytes_per_block = kai_get_num_bytes_per_block_rhs(bl); + + size_t rhs_packed_stride = kai_nr * (num_bytes_per_block * num_blocks_per_row); + // Since the LHS matrix is quantized asymmetric with per-row quantization, we also include + // the number of bytes for the reduction sum + rhs_packed_stride += kai_nr * kai_num_bytes_rsum_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; + + return rhs_packed_stride; +} + +size_t kai_get_m_step_matmul_clamp_f32_qai8dxp4x8_qsi4c32p8x8_4x8_neon_i8mm(void) { + return kai_m_step; +} + +size_t kai_get_n_step_matmul_clamp_f32_qai8dxp4x8_qsi4c32p8x8_4x8_neon_i8mm(void) { + return kai_n_step; +} + +size_t kai_get_mr_matmul_clamp_f32_qai8dxp4x8_qsi4c32p8x8_4x8_neon_i8mm(void) { + return kai_mr; +} + +size_t kai_get_nr_matmul_clamp_f32_qai8dxp4x8_qsi4c32p8x8_4x8_neon_i8mm(void) { + return kai_nr; +} + +size_t kai_get_kr_matmul_clamp_f32_qai8dxp4x8_qsi4c32p8x8_4x8_neon_i8mm(void) { + return kai_kr; +} + +size_t kai_get_sr_matmul_clamp_f32_qai8dxp4x8_qsi4c32p8x8_4x8_neon_i8mm(void) { + return kai_sr; +} + +size_t kai_get_lhs_packed_offset_matmul_clamp_f32_qai8dxp4x8_qsi4c32p8x8_4x8_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_qai8dxp4x8_qsi4c32p8x8_4x8_neon_i8mm( + size_t n_idx, size_t k, size_t bl) { + KAI_ASSUME((k % bl) == 0); + KAI_ASSUME((n_idx % kai_n_step) == 0); + + return (n_idx / kai_nr) * kai_get_rhs_packed_stride(k, bl); +} + +size_t kai_get_dst_offset_matmul_clamp_f32_qai8dxp4x8_qsi4c32p8x8_4x8_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_qai8dxp4x8_qsi4c32p8x8_4x8_neon_i8mm(size_t m, size_t n) { + return m * n * kai_num_bytes_dst_value; +} + +void kai_run_matmul_clamp_f32_qai8dxp4x8_qsi4c32p8x8_4x8_neon_i8mm( + size_t m, // + size_t n, // + size_t k, // + size_t bl, // + 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((k % bl) == 0); + KAI_ASSUME((bl % kai_bl) == 0); + + if (m == 0) { + return; + } + const size_t num_subblocks = bl / kai_bl; + const size_t num_blocks = kai_get_num_blocks_per_row(k, 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; + args.num_subblocks = num_subblocks; + + kai_kernel_matmul_clamp_f32_qai8dxp4x8_qsi4c32p8x8_4x8_neon_i8mm(&args); +} + +#endif // Architectural features check. diff --git a/kai/ukernels/matmul/matmul_clamp_f32_qai8dxp_qsi4c32p/kai_matmul_clamp_f32_qai8dxp4x8_qsi4c32p8x8_4x8_neon_i8mm.h b/kai/ukernels/matmul/matmul_clamp_f32_qai8dxp_qsi4c32p/kai_matmul_clamp_f32_qai8dxp4x8_qsi4c32p8x8_4x8_neon_i8mm.h new file mode 100644 index 0000000000000000000000000000000000000000..a9aa9bb05a4089357530604b5e7b0bdecff13dd7 --- /dev/null +++ b/kai/ukernels/matmul/matmul_clamp_f32_qai8dxp_qsi4c32p/kai_matmul_clamp_f32_qai8dxp4x8_qsi4c32p8x8_4x8_neon_i8mm.h @@ -0,0 +1,146 @@ +// +// 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_qai8dxp_f32 to dynamically quantize and pack the LHS matrix in a single step. +/// -# @ref kai_rhs_pack_nxk_qsi4c32p_qsu4c32s1s0 to pack the RHS NxK matrix. +/// -# @ref kai_rhs_pack_kxn_qsi4c32p_qsu4c32s1s0 to pack the RHS KxN 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_qai8dxp4x8_qsi4c32p8x8_4x8_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_qai8dxp4x8_qsi4c32p8x8_4x8_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_qai8dxp4x8_qsi4c32p8x8_4x8_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_qai8dxp4x8_qsi4c32p8x8_4x8_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_qai8dxp4x8_qsi4c32p8x8_4x8_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_qai8dxp4x8_qsi4c32p8x8_4x8_neon_i8mm(void); + +/// Gets the offset in bytes for the packed LHS matrix, +/// which contains the packed Quantized Asymmetric Signed 8-bit with per-row quantization (qai8dx) 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_qai8dxp4x8_qsi4c32p8x8_4x8_neon_i8mm( + size_t m_idx, // + size_t k); // + +/// Gets the offset in bytes for the packed RHS matrix, +/// which contains the packed Quantized Symmetric Signed 4-bit with per-block (32) quantization (qsi4c32) 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). +/// It must be a multiple of the block length (bl). +/// @param[in] bl Block length. It must be a multiple of 32. +/// +/// @return the offset in bytes to the packed RHS matrix +size_t kai_get_rhs_packed_offset_matmul_clamp_f32_qai8dxp4x8_qsi4c32p8x8_4x8_neon_i8mm( + size_t n_idx, // + size_t k, // + size_t bl); // + +/// Gets the offset in bytes for the destination matrix +/// +/// @param[in] m_idx Row index in the destination matrix. It must be a multiple of m_step. +/// @param[in] n_idx Column index in the destination matrix. It must be multiple of n_step. +/// @param[in] dst_stride The number of bytes in each row of the destination matrix +/// +/// @return the destination offset in bytes +size_t kai_get_dst_offset_matmul_clamp_f32_qai8dxp4x8_qsi4c32p8x8_4x8_neon_i8mm( + size_t m_idx, // + size_t n_idx, // + size_t dst_stride); // + +/// Gets the size in bytes for the destination matrix. +/// +/// @param[in] m Number of rows in the destination matrix. +/// @param[in] n Number of columns in the destination matrix. +/// +/// @return the destination matrix size in bytes +size_t kai_get_dst_size_matmul_clamp_f32_qai8dxp4x8_qsi4c32p8x8_4x8_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 Asymmetric Signed 8-bit with per-row quantization (qai8dx) and packed. +/// RHS matrix: Quantized Symmetric Signed 4-bit with per-block (32) quantization (qsi4c32) 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. +/// It must be a multiple of the block length (bl). +/// @param[in] bl Block length. Block length. It must be a multiple of 32. +/// @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 destination matrix. +/// @param[in] dst_stride_row Stride in bytes between two rows of the destination matrix. +/// @param[in] dst_stride_col Stride in bytes between two columns of the destination 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_qai8dxp4x8_qsi4c32p8x8_4x8_neon_i8mm( + size_t m, // + size_t n, // + size_t k, // + size_t bl, // + 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_qai8dxp_qsi4c32p/kai_matmul_clamp_f32_qai8dxp4x8_qsi4c32p8x8_4x8_neon_i8mm_asm.S b/kai/ukernels/matmul/matmul_clamp_f32_qai8dxp_qsi4c32p/kai_matmul_clamp_f32_qai8dxp4x8_qsi4c32p8x8_4x8_neon_i8mm_asm.S new file mode 100644 index 0000000000000000000000000000000000000000..2545be796cbf7a44a69c98bfb8bb923322594d10 --- /dev/null +++ b/kai/ukernels/matmul/matmul_clamp_f32_qai8dxp_qsi4c32p/kai_matmul_clamp_f32_qai8dxp4x8_qsi4c32p8x8_4x8_neon_i8mm_asm.S @@ -0,0 +1,340 @@ +// +// 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_qai8dxp4x8_qsi4c32p8x8_4x8_neon_i8mm) + KAI_ASM_ALIGN + + KAI_ASM_GLOBAL(kai_kernel_matmul_clamp_f32_qai8dxp4x8_qsi4c32p8x8_4x8_neon_i8mm) + +KAI_ASM_FUNCTION_TYPE(kai_kernel_matmul_clamp_f32_qai8dxp4x8_qsi4c32p8x8_4x8_neon_i8mm) +KAI_ASM_FUNCTION_LABEL(kai_kernel_matmul_clamp_f32_qai8dxp4x8_qsi4c32p8x8_4x8_neon_i8mm) + stp x20, x21, [sp, -144]! + 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 d12, d13, [sp, 88] + stp d14, d15, [sp, 104] + stp d8, d9, [sp, 120] + mov x16, #0x80 + movi v9.16b, #0xf0 + mov x21, #0x20 + ldr x15, [x0, #0x40] + ldr x20, [x0, #0x28] + ldr x14, [x0, #0x38] + ldr x13, [x0, #0x8] + ldr x12, [x0, #0x10] + ldr x11, [x0, #0x30] + mul x16, x15, x16 + mov x10, x20 + ldr x9, [x0, #0x0] + ldr x28, [x0, #0x20] + ldr x27, [x0, #0x18] + madd x16, x14, x16, x21 + cbz x10, label_12 +KAI_ASM_LABEL(label_1) // Row loop + mov x26, x12 + mov x25, x11 + add x24, x9, x28, LSL #2 +KAI_ASM_LABEL(label_2) // Column loop + movi v12.16b, #0x0 + movi v5.16b, #0x0 + mov x22, x13 + mov x21, x14 + movi v11.16b, #0x0 + movi v13.16b, #0x0 + movi v21.16b, #0x0 + movi v27.16b, #0x0 + movi v7.16b, #0x0 + movi v4.16b, #0x0 +KAI_ASM_LABEL(label_3) // Block loop + movi v10.4s, #0x0 + movi v26.4s, #0x0 + mov x20, x15 + movi v18.4s, #0x0 + movi v2.4s, #0x0 + movi v3.4s, #0x0 + movi v16.4s, #0x0 + movi v8.4s, #0x0 + movi v19.4s, #0x0 +KAI_ASM_LABEL(label_4) // Sub block loop + ldr q14, [x26, #0x0] + ldr q28, [x26, #0x10] + subs x20, x20, #0x1 + ldr q23, [x26, #0x20] + ldr q1, [x26, #0x30] + ldr q22, [x22, #0x0] + ldr q15, [x22, #0x10] + ldr q25, [x26, #0x40] + ldr q17, [x26, #0x50] + shl v31.16b, v14.16b, #0x4 + shl v0.16b, v28.16b, #0x4 + ldr q6, [x26, #0x60] + ldr q24, [x26, #0x70] + shl v20.16b, v23.16b, #0x4 + shl v29.16b, v1.16b, #0x4 + ldr q30, [x22, #0x20] + and v14.16b, v14.16b, v9.16b + and v28.16b, v28.16b, v9.16b + add x26, x26, #0x80 + KAI_ASM_INST(0x4e9fa6ca) // smmla v10.4s, v22.16b, v31.16b + KAI_ASM_INST(0x4e80a6d2) // smmla v18.4s, v22.16b, v0.16b + and v23.16b, v23.16b, v9.16b + KAI_ASM_INST(0x4e94a6da) // smmla v26.4s, v22.16b, v20.16b + KAI_ASM_INST(0x4e9da6c2) // smmla v2.4s, v22.16b, v29.16b + ldr q22, [x22, #0x30] + and v1.16b, v1.16b, v9.16b + KAI_ASM_INST(0x4e9fa5e3) // smmla v3.4s, v15.16b, v31.16b + ldr q31, [x22, #0x40] + KAI_ASM_INST(0x4e80a5e8) // smmla v8.4s, v15.16b, v0.16b + ldr q0, [x22, #0x50] + KAI_ASM_INST(0x4e94a5f0) // smmla v16.4s, v15.16b, v20.16b + ldr q20, [x22, #0x60] + KAI_ASM_INST(0x4e9da5f3) // smmla v19.4s, v15.16b, v29.16b + ldr q29, [x22, #0x70] + shl v15.16b, v25.16b, #0x4 + and v25.16b, v25.16b, v9.16b + add x22, x22, #0x80 + KAI_ASM_INST(0x4e8fa7ca) // smmla v10.4s, v30.16b, v15.16b + KAI_ASM_INST(0x4e8fa6c3) // smmla v3.4s, v22.16b, v15.16b + shl v15.16b, v17.16b, #0x4 + and v17.16b, v17.16b, v9.16b + KAI_ASM_INST(0x4e8fa7d2) // smmla v18.4s, v30.16b, v15.16b + KAI_ASM_INST(0x4e8fa6c8) // smmla v8.4s, v22.16b, v15.16b + shl v15.16b, v6.16b, #0x4 + and v6.16b, v6.16b, v9.16b + KAI_ASM_INST(0x4e8ea7ea) // smmla v10.4s, v31.16b, v14.16b + KAI_ASM_INST(0x4e8ea403) // smmla v3.4s, v0.16b, v14.16b + shl v14.16b, v24.16b, #0x4 + and v24.16b, v24.16b, v9.16b + KAI_ASM_INST(0x4e8fa7da) // smmla v26.4s, v30.16b, v15.16b + KAI_ASM_INST(0x4e8fa6d0) // smmla v16.4s, v22.16b, v15.16b + KAI_ASM_INST(0x4e9ca7f2) // smmla v18.4s, v31.16b, v28.16b + KAI_ASM_INST(0x4e9ca408) // smmla v8.4s, v0.16b, v28.16b + KAI_ASM_INST(0x4e8ea7c2) // smmla v2.4s, v30.16b, v14.16b + KAI_ASM_INST(0x4e8ea6d3) // smmla v19.4s, v22.16b, v14.16b + KAI_ASM_INST(0x4e99a68a) // smmla v10.4s, v20.16b, v25.16b + KAI_ASM_INST(0x4e99a7a3) // smmla v3.4s, v29.16b, v25.16b + KAI_ASM_INST(0x4e97a7fa) // smmla v26.4s, v31.16b, v23.16b + KAI_ASM_INST(0x4e97a410) // smmla v16.4s, v0.16b, v23.16b + KAI_ASM_INST(0x4e91a692) // smmla v18.4s, v20.16b, v17.16b + KAI_ASM_INST(0x4e91a7a8) // smmla v8.4s, v29.16b, v17.16b + KAI_ASM_INST(0x4e81a7e2) // smmla v2.4s, v31.16b, v1.16b + KAI_ASM_INST(0x4e81a413) // smmla v19.4s, v0.16b, v1.16b + KAI_ASM_INST(0x4e86a69a) // smmla v26.4s, v20.16b, v6.16b + KAI_ASM_INST(0x4e86a7b0) // smmla v16.4s, v29.16b, v6.16b + KAI_ASM_INST(0x4e98a682) // smmla v2.4s, v20.16b, v24.16b + KAI_ASM_INST(0x4e98a7b3) // smmla v19.4s, v29.16b, v24.16b + bgt label_4 + ldr q0, [x26, #0x0] + uzp1 v14.2d, v10.2d, v18.2d + uzp2 v30.2d, v10.2d, v18.2d + add x26, x26, #0x10 + uzp1 v25.2d, v26.2d, v2.2d + uzp2 v22.2d, v26.2d, v2.2d + uzp1 v24.2d, v3.2d, v8.2d + uzp2 v20.2d, v3.2d, v8.2d + uzp1 v28.2d, v16.2d, v19.2d + uzp2 v18.2d, v16.2d, v19.2d + shll v17.4s, v0.4h, #0x10 + shll2 v16.4s, v0.8h, #0x10 + scvtf v14.4s, v14.4s, #0x4 + scvtf v25.4s, v25.4s, #0x4 + scvtf v30.4s, v30.4s, #0x4 + scvtf v22.4s, v22.4s, #0x4 + scvtf v24.4s, v24.4s, #0x4 + scvtf v28.4s, v28.4s, #0x4 + scvtf v20.4s, v20.4s, #0x4 + scvtf v18.4s, v18.4s, #0x4 + fmla v12.4s, v14.4s, v17.4s + fmla v5.4s, v25.4s, v16.4s + fmla v11.4s, v30.4s, v17.4s + fmla v13.4s, v22.4s, v16.4s + fmla v21.4s, v24.4s, v17.4s + fmla v27.4s, v28.4s, v16.4s + fmla v7.4s, v20.4s, v17.4s + fmla v4.4s, v18.4s, v16.4s + subs x21, x21, #0x1 + bgt label_3 + ld1 { v23.4s }, [x22] + ldr q22, [x26, #0x0] + add x22, x22, #0x10 + add x20, x27, #0x4 + ldr q6, [x26, #0x10] + ldr q20, [x22, #0x0] + cmp x25, #0x8 + ldr q19, [x26, #0x20] + ldr q18, [x26, #0x30] + add x26, x26, #0x40 + ld1r { v17.4s }, [x27] + ld1r { v16.4s }, [x20] + scvtf v23.4s, v23.4s + fmla v12.4s, v22.4s, v23.s[0] + fmla v5.4s, v6.4s, v23.s[0] + fmla v11.4s, v22.4s, v23.s[1] + fmla v13.4s, v6.4s, v23.s[1] + fmla v21.4s, v22.4s, v23.s[2] + fmla v27.4s, v6.4s, v23.s[2] + fmla v7.4s, v22.4s, v23.s[3] + fmla v4.4s, v6.4s, v23.s[3] + fmul v12.4s, v12.4s, v20.s[0] + fmul v5.4s, v5.4s, v20.s[0] + fmul v11.4s, v11.4s, v20.s[1] + fmul v13.4s, v13.4s, v20.s[1] + fmul v21.4s, v21.4s, v20.s[2] + fmul v27.4s, v27.4s, v20.s[2] + fmul v7.4s, v7.4s, v20.s[3] + fmul v4.4s, v4.4s, v20.s[3] + fadd v12.4s, v12.4s, v19.4s + fadd v5.4s, v5.4s, v18.4s + fadd v11.4s, v11.4s, v19.4s + fadd v13.4s, v13.4s, v18.4s + fadd v21.4s, v21.4s, v19.4s + fadd v27.4s, v27.4s, v18.4s + fadd v7.4s, v7.4s, v19.4s + fadd v4.4s, v4.4s, v18.4s + fmax v12.4s, v12.4s, v17.4s + fmax v5.4s, v5.4s, v17.4s + fmax v11.4s, v11.4s, v17.4s + fmax v13.4s, v13.4s, v17.4s + fmax v21.4s, v21.4s, v17.4s + fmax v27.4s, v27.4s, v17.4s + fmax v7.4s, v7.4s, v17.4s + fmax v4.4s, v4.4s, v17.4s + fmin v12.4s, v12.4s, v16.4s + fmin v5.4s, v5.4s, v16.4s + fmin v11.4s, v11.4s, v16.4s + fmin v13.4s, v13.4s, v16.4s + fmin v21.4s, v21.4s, v16.4s + fmin v27.4s, v27.4s, v16.4s + fmin v7.4s, v7.4s, v16.4s + fmin v4.4s, v4.4s, v16.4s + blt label_6 + mov x20, x9 + cmp x10, #0x1 + str q12, [x20, #0x0] + str q5, [x20, #0x10] + add x20, x20, x28 + ble label_11 + cmp x10, #0x2 + str q11, [x20, #0x0] + str q13, [x20, #0x10] + add x20, x20, x28 + ble label_11 + cmp x10, #0x3 + str q21, [x20, #0x0] + str q27, [x20, #0x10] + add x20, x20, x28 + ble label_11 + str q7, [x20, #0x0] + str q4, [x20, #0x10] + b label_11 +KAI_ASM_LABEL(label_6) // Partial output + mov x23, x9 + cmp x10, #0x1 + add x22, x23, x28 + csel x22, x22, x23, GT + cmp x10, #0x2 + add x21, x23, x28, LSL #1 + csel x21, x21, x22, GT + cmp x10, #0x3 + add x20, x21, x28 + csel x20, x20, x21, GT + tbz x25, #2, label_8 + st1 { v7.4s }, [x20], #0x10 + st1 { v21.4s }, [x21], #0x10 + st1 { v11.4s }, [x22], #0x10 + st1 { v12.4s }, [x23], #0x10 + tbz x25, #1, label_7 + st1 { v4.d }[0], [x20], #0x8 + st1 { v27.d }[0], [x21], #0x8 + st1 { v13.d }[0], [x22], #0x8 + st1 { v5.d }[0], [x23], #0x8 + tbz x25, #0, label_10 + st1 { v4.s }[2], [x20] + st1 { v27.s }[2], [x21] + st1 { v13.s }[2], [x22] + st1 { v5.s }[2], [x23] + b label_10 +KAI_ASM_LABEL(label_7) // Output block 0: partial_1_4 + tbz x25, #0, label_10 + st1 { v4.s }[0], [x20] + st1 { v27.s }[0], [x21] + st1 { v13.s }[0], [x22] + st1 { v5.s }[0], [x23] + b label_10 +KAI_ASM_LABEL(label_8) // Output block 0: partial_2_0 + tbz x25, #1, label_9 + st1 { v7.d }[0], [x20], #0x8 + st1 { v21.d }[0], [x21], #0x8 + st1 { v11.d }[0], [x22], #0x8 + st1 { v12.d }[0], [x23], #0x8 + tbz x25, #0, label_10 + st1 { v7.s }[2], [x20] + st1 { v21.s }[2], [x21] + st1 { v11.s }[2], [x22] + st1 { v12.s }[2], [x23] + b label_10 +KAI_ASM_LABEL(label_9) // Output block 0: partial_1_0 + st1 { v7.s }[0], [x20] + st1 { v21.s }[0], [x21] + st1 { v11.s }[0], [x22] + st1 { v12.s }[0], [x23] +KAI_ASM_LABEL(label_10) // Output block 0: Done +KAI_ASM_LABEL(label_11) // Output stage exit + subs x25, x25, #0x8 + add x9, x9, #0x20 + bgt label_2 + subs x10, x10, #0x4 + add x13, x13, x16 + mov x9, x24 + bgt label_1 +KAI_ASM_LABEL(label_12) // 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 d12, d13, [sp, 88] + ldp d14, d15, [sp, 104] + ldp d8, d9, [sp, 120] + ldp x20, x21, [sp], 144 + ret + KAI_ASM_FUNCTION_END(kai_kernel_matmul_clamp_f32_qai8dxp4x8_qsi4c32p8x8_4x8_neon_i8mm) + + KAI_ASM_END diff --git a/test/tests/matmul_clamp_f32_qai8dxp_qsi4c32p_test.cpp b/test/tests/matmul_clamp_f32_qai8dxp_qsi4c32p_test.cpp index 5ebdfc1f992b99c242a0f73af3cb7c6c89340d36..90000e97cbff905c002827ed40d4dac1a5c13f88 100644 --- a/test/tests/matmul_clamp_f32_qai8dxp_qsi4c32p_test.cpp +++ b/test/tests/matmul_clamp_f32_qai8dxp_qsi4c32p_test.cpp @@ -29,6 +29,7 @@ #include "kai/ukernels/matmul/matmul_clamp_f32_qai8dxp_qsi4c32p/kai_matmul_clamp_f32_qai8dxp4x4_qsi4c32p8x4_4x8_neon_dotprod.h" #include "kai/ukernels/matmul/matmul_clamp_f32_qai8dxp_qsi4c32p/kai_matmul_clamp_f32_qai8dxp4x8_qsi4c32p4x8_16x4x32_neon_i8mm.h" #include "kai/ukernels/matmul/matmul_clamp_f32_qai8dxp_qsi4c32p/kai_matmul_clamp_f32_qai8dxp4x8_qsi4c32p4x8_8x4x32_neon_i8mm.h" +#include "kai/ukernels/matmul/matmul_clamp_f32_qai8dxp_qsi4c32p/kai_matmul_clamp_f32_qai8dxp4x8_qsi4c32p8x8_4x8_neon_i8mm.h" #include "kai/ukernels/matmul/matmul_clamp_f32_qai8dxp_qsi4c32p/kai_matmul_clamp_f32_qai8dxp4x8_qsi4c32p8x8_4x8x32_neon_i8mm.h" #include "kai/ukernels/matmul/matmul_clamp_f32_qai8dxp_qsi4c32p/kai_matmul_clamp_f32_qai8dxp_qsi4c32p_interface.h" #include "kai/ukernels/matmul/pack/kai_lhs_quant_pack_qai8dxp_f32.h" @@ -54,7 +55,7 @@ namespace kai::test { static auto cpu_has_dotprod_and_bf16 = []() { return cpu_has_dotprod() && cpu_has_bf16(); }; static auto cpu_has_i8mm_and_bf16 = []() { return cpu_has_i8mm() && cpu_has_bf16(); }; -static const std::array, 10> +static const std::array, 11> variants_kai_matmul_clamp_f32_qai8dxp_qsi4c32p = { {{UKERNEL_MATMUL_VARIANT(clamp_f32_qai8dxp1x4_qsi4c32p4x4_1x4_neon_dotprod), "kai_matmul_clamp_f32_qai8dxp1x4_qsi4c32p4x4_1x4_neon_dotprod", cpu_has_dotprod_and_bf16}, @@ -75,7 +76,9 @@ static const std::array;