OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[CPU][ARM] KleidiAI INT8 FC: select the GEMV for M == 1 and the 16x4 GEMM for M > 1 on dotprod and i8mm cores (#39358)
Clémence Lesnéa96e6228a52a
diff --git a/src/plugins/intel_cpu/src/nodes/executors/kleidiai/kleidiai_common.hpp b/src/plugins/intel_cpu/src/nodes/executors/kleidiai/kleidiai_common.hppindex 400fc4aeac0..a5320584b2f 100644--- a/src/plugins/intel_cpu/src/nodes/executors/kleidiai/kleidiai_common.hpp+++ b/src/plugins/intel_cpu/src/nodes/executors/kleidiai/kleidiai_common.hpp@@ -39,6 +39,10 @@// Headers for INT8 KAI kernels#include "kai/ukernels/matmul/matmul_clamp_f32_qai8dxp_qsi8cxp/kai_matmul_clamp_f32_qai8dxp1x4_qsi8cxp4x4_1x4_neon_dotprod.h"+#if !defined(_MSC_VER)+# include "kai/ukernels/matmul/matmul_clamp_f32_qai8dxp_qsi8cxp/kai_matmul_clamp_f32_qai8dxp1x8_qsi8cxp4x8_1x4_neon_dotprod.h"+# include "kai/ukernels/matmul/matmul_clamp_f32_qai8dxp_qsi8cxp/kai_matmul_clamp_f32_qai8dxp4x4_qsi8cxp4x4_16x4_neon_dotprod.h"+#endif#include "kai/ukernels/matmul/matmul_clamp_f32_qai8dxp_qsi8cxp/kai_matmul_clamp_f32_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm.h"#include "kai/ukernels/matmul/matmul_clamp_f32_qai8dxp_qsi8cxp/kai_matmul_clamp_f32_qai8dxp_qsi8cxp_interface.h"#include "kai/ukernels/matmul/pack/kai_lhs_quant_pack_qai8dxp_f32.h"@@ -249,10 +253,15 @@ public:} // end of execute()...};+// Kernel interfaces for the INT8 per-channel symmetric path (qai8dxp LHS, qsi8cxp RHS).+// gemv runs single-row calls when the CPU allows it, gemm everything else (see selectKernel()).+// Both kernels must read the same packed RHS. Under MSVC each pair falls back to a single kernel.+template <KAIKernelTag tag>+struct Qai8dxpQsi8cxpKernels;+template <>-class uKernel<KAIKernelTag::I8_NEON_DOTPROD> : public uKernelBase {-private:- static constexpr kai_matmul_clamp_f32_qai8dxp_qsi8cxp_ukernel uKernelInterface{+struct Qai8dxpQsi8cxpKernels<KAIKernelTag::I8_NEON_DOTPROD> {+ static constexpr kai_matmul_clamp_f32_qai8dxp_qsi8cxp_ukernel gemv{kai_get_m_step_matmul_clamp_f32_qai8dxp1x4_qsi8cxp4x4_1x4_neon_dotprod,kai_get_n_step_matmul_clamp_f32_qai8dxp1x4_qsi8cxp4x4_1x4_neon_dotprod,kai_get_mr_matmul_clamp_f32_qai8dxp1x4_qsi8cxp4x4_1x4_neon_dotprod,@@ -264,129 +273,28 @@ private:kai_get_dst_offset_matmul_clamp_f32_qai8dxp1x4_qsi8cxp4x4_1x4_neon_dotprod,kai_get_dst_size_matmul_clamp_f32_qai8dxp1x4_qsi8cxp4x4_1x4_neon_dotprod,kai_run_matmul_clamp_f32_qai8dxp1x4_qsi8cxp4x4_1x4_neon_dotprod};- MemoryPtr rhsPackedMem;- MemoryPtr& lhsPackedMem;--public:- uKernel(size_t N, size_t K, MemoryPtr& lhsPackedMem) : lhsPackedMem(lhsPackedMem) {- this->N = N;- this->K = K;- this->BLOCK_SIZE = 16;- this->mr = uKernelInterface.get_mr();- this->nr = uKernelInterface.get_nr();- this->kr = uKernelInterface.get_kr();- this->sr = uKernelInterface.get_sr();- } // end of uKernel()...-- size_t get_rhsPackedSize() override {- return kai_get_rhs_packed_size_rhs_pack_kxn_qsi8cxp_qsi8cx_neon(N, K, nr, kr, sr);- }-- void packData([[maybe_unused]] bool isTransposed,- MemoryCPtr weightsMemory,- MemoryPtr biasMem,- [[maybe_unused]] bool hasBias,- float* rhs_scales,- MemoryPtr rhsPackedMemory) override {- rhsPackedMem = rhsPackedMemory;- auto* rhs_native_qs8cx = weightsMemory->getDataAs<int8_t>();- auto* bias = biasMem->getDataAs<float>();- auto* rhs_packed_qs8cx = static_cast<int8_t*>(rhsPackedMem->getData());- kai_rhs_pack_qsi8cx_params params{};- params.lhs_zero_point = 1;- kai_run_rhs_pack_kxn_qsi8cxp_qsi8cx_neon(1,- N,- K,- nr,- kr,- sr,- rhs_native_qs8cx,- bias,- rhs_scales,- rhs_packed_qs8cx,- 0,- ¶ms);- }-- KernelInterface getuKernelInterface() override {- return uKernelInterface;- }-- KAIKernelTag getKernelTag() override {- return KAIKernelTag::I8_NEON_DOTPROD;- }-- size_t getLHSPackedSize(size_t m) override {- const size_t _m_blocks = (m + BLOCK_SIZE - 1) / BLOCK_SIZE;- packedlhs_block_in_bytes = kai_get_lhs_packed_size_lhs_quant_pack_qai8dxp_f32(BLOCK_SIZE, K, mr, kr, sr);- const size_t lhsPackedSize = packedlhs_block_in_bytes * _m_blocks;- return lhsPackedSize;- }-- void execute(const ov::intel_cpu::CpuParallelPtr& cpu_parallel,- ov::intel_cpu::Dim M,- ov::intel_cpu::Dim K,- ov::intel_cpu::MemoryPtr dstMem,- ov::intel_cpu::MemoryPtr srcMem) override {- auto _ukernel = std::get<kai_matmul_clamp_f32_qai8dxp_qsi8cxp_ukernel>(getuKernelInterface());- size_t m_step = BLOCK_SIZE;- constexpr size_t n_step = 4;- const size_t M_BLOCKS = (M + m_step - 1) / m_step;- const size_t N_BLOCKS = (N + n_step - 1) / n_step;- const size_t lhs_packed_offset = _ukernel.get_lhs_packed_offset(0, K);- const size_t lhs_stride = K * sizeof(float);-- auto* lhs = srcMem->getDataAs<float>();- auto* dst = dstMem->getDataAs<float>();-- auto* lhs_packed_lowp = lhsPackedMem->getDataAs<int8_t>();- auto* rhs_packed_lowp = rhsPackedMem->getDataAs<int8_t>();-- const size_t dst_stride_row = N * sizeof(float);- const size_t dst_stride_col = sizeof(float);-- ParallelNestingContext nested_context;- cpu_parallel->parallel_for(M_BLOCKS, [&](size_t m_blk) {- const size_t M_iter = std::min(M - m_blk * m_step, m_step);- auto* lhs_packed_block = lhs_packed_lowp + m_blk * packedlhs_block_in_bytes;-- kai_run_lhs_quant_pack_qai8dxp_f32(M_iter,- K,- mr,- kr,- sr,- 0,- lhs + m_blk * m_step * K, // LHS (F32)- lhs_stride,- lhs_packed_block // lhs packed output- );- cpu_parallel->parallel_for(N_BLOCKS, [&](size_t n_blk) {- // matmul exec- const size_t rhs_packed_offset = _ukernel.get_rhs_packed_offset(n_blk * n_step, K);- const size_t dst_offset = _ukernel.get_dst_offset(m_blk * m_step, n_blk * n_step, dst_stride_row);- const auto* rhs_ptr = static_cast<const void*>(rhs_packed_lowp + rhs_packed_offset);- const auto* lhs_ptr = static_cast<const void*>(lhs_packed_block + lhs_packed_offset);- float* dst_ptr = (dst + dst_offset / sizeof(float));- const size_t N_iter = std::min(N - n_blk * n_step, n_step);- _ukernel.run_matmul(M_iter,- N_iter,- K,- lhs_ptr,- rhs_ptr,- dst_ptr,- dst_stride_row,- dst_stride_col,- FLOAT_MIN,- FLOAT_MAX);- });- });- } // end of execute()...+#if !defined(_MSC_VER)+ static constexpr kai_matmul_clamp_f32_qai8dxp_qsi8cxp_ukernel gemm{+ kai_get_m_step_matmul_clamp_f32_qai8dxp4x4_qsi8cxp4x4_16x4_neon_dotprod,+ kai_get_n_step_matmul_clamp_f32_qai8dxp4x4_qsi8cxp4x4_16x4_neon_dotprod,+ kai_get_mr_matmul_clamp_f32_qai8dxp4x4_qsi8cxp4x4_16x4_neon_dotprod,+ kai_get_nr_matmul_clamp_f32_qai8dxp4x4_qsi8cxp4x4_16x4_neon_dotprod,+ kai_get_kr_matmul_clamp_f32_qai8dxp4x4_qsi8cxp4x4_16x4_neon_dotprod,+ kai_get_sr_matmul_clamp_f32_qai8dxp4x4_qsi8cxp4x4_16x4_neon_dotprod,+ kai_get_lhs_packed_offset_matmul_clamp_f32_qai8dxp4x4_qsi8cxp4x4_16x4_neon_dotprod,+ kai_get_rhs_packed_offset_matmul_clamp_f32_qai8dxp4x4_qsi8cxp4x4_16x4_neon_dotprod,+ kai_get_dst_offset_matmul_clamp_f32_qai8dxp4x4_qsi8cxp4x4_16x4_neon_dotprod,+ kai_get_dst_size_matmul_clamp_f32_qai8dxp4x4_qsi8cxp4x4_16x4_neon_dotprod,+ kai_run_matmul_clamp_f32_qai8dxp4x4_qsi8cxp4x4_16x4_neon_dotprod};+#else+ // MSVC builds only the KleidiAI assembly kernels, which do not include the 16x4 dotprod GEMM+ static constexpr kai_matmul_clamp_f32_qai8dxp_qsi8cxp_ukernel gemm = gemv;+#endif};template <>-class uKernel<KAIKernelTag::I8_NEON_IMM> : public uKernelBase {-private:- static constexpr kai_matmul_clamp_f32_qai8dxp_qsi8cxp_ukernel uKernelInterface{+struct Qai8dxpQsi8cxpKernels<KAIKernelTag::I8_NEON_IMM> {+ static constexpr kai_matmul_clamp_f32_qai8dxp_qsi8cxp_ukernel gemm{kai_get_m_step_matmul_clamp_f32_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm,kai_get_n_step_matmul_clamp_f32_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm,kai_get_mr_matmul_clamp_f32_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm,@@ -398,19 +306,60 @@ private:kai_get_dst_offset_matmul_clamp_f32_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm,kai_get_dst_size_matmul_clamp_f32_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm,kai_run_matmul_clamp_f32_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm};+#if !defined(_MSC_VER)+ // dotprod GEMV sharing the qsi8cxp4x8 packed RHS of the i8mm GEMM+ static constexpr kai_matmul_clamp_f32_qai8dxp_qsi8cxp_ukernel gemv{+ kai_get_m_step_matmul_clamp_f32_qai8dxp1x8_qsi8cxp4x8_1x4_neon_dotprod,+ kai_get_n_step_matmul_clamp_f32_qai8dxp1x8_qsi8cxp4x8_1x4_neon_dotprod,+ kai_get_mr_matmul_clamp_f32_qai8dxp1x8_qsi8cxp4x8_1x4_neon_dotprod,+ kai_get_nr_matmul_clamp_f32_qai8dxp1x8_qsi8cxp4x8_1x4_neon_dotprod,+ kai_get_kr_matmul_clamp_f32_qai8dxp1x8_qsi8cxp4x8_1x4_neon_dotprod,+ kai_get_sr_matmul_clamp_f32_qai8dxp1x8_qsi8cxp4x8_1x4_neon_dotprod,+ kai_get_lhs_packed_offset_matmul_clamp_f32_qai8dxp1x8_qsi8cxp4x8_1x4_neon_dotprod,+ kai_get_rhs_packed_offset_matmul_clamp_f32_qai8dxp1x8_qsi8cxp4x8_1x4_neon_dotprod,+ kai_get_dst_offset_matmul_clamp_f32_qai8dxp1x8_qsi8cxp4x8_1x4_neon_dotprod,+ kai_get_dst_size_matmul_clamp_f32_qai8dxp1x8_qsi8cxp4x8_1x4_neon_dotprod,+ kai_run_matmul_clamp_f32_qai8dxp1x8_qsi8cxp4x8_1x4_neon_dotprod};+#else+ // MSVC builds only the KleidiAI assembly kernels, which do not include the 1x8 dotprod GEMV+ static constexpr kai_matmul_clamp_f32_qai8dxp_qsi8cxp_ukernel gemv = gemm;+#endif+};++// Common implementation of the INT8 per-channel symmetric uKernels+template <KAIKernelTag tag>+class uKernelQai8dxpQsi8cxp : public uKernelBase {+private:+ using Kernels = Qai8dxpQsi8cxpKernels<tag>;MemoryPtr rhsPackedMem;MemoryPtr& lhsPackedMem;+ // Whether single-row calls may run Kernels::gemv+ bool useGemv;public:- uKernel(size_t N, size_t K, MemoryPtr& lhsPackedMem) : lhsPackedMem(lhsPackedMem) {+ uKernelQai8dxpQsi8cxp(size_t N, size_t K, MemoryPtr& lhsPackedMem, bool useGemv)+ : lhsPackedMem(lhsPackedMem),+ useGemv(useGemv) {this->N = N;this->K = K;this->BLOCK_SIZE = 16;- this->mr = uKernelInterface.get_mr();- this->nr = uKernelInterface.get_nr();- this->kr = uKernelInterface.get_kr();- this->sr = uKernelInterface.get_sr();- } // end of uKernel()...+ this->nr = Kernels::gemm.get_nr();+ this->kr = Kernels::gemm.get_kr();+ this->sr = Kernels::gemm.get_sr();+ OPENVINO_ASSERT(Kernels::gemv.get_nr() == nr && Kernels::gemv.get_kr() == kr && Kernels::gemv.get_sr() == sr,+ "INT8 GEMV and GEMM kernels must share the packed RHS layout");+ OPENVINO_ASSERT(BLOCK_SIZE % Kernels::gemm.get_m_step() == 0 && BLOCK_SIZE % Kernels::gemm.get_mr() == 0 &&+ BLOCK_SIZE % Kernels::gemv.get_mr() == 0,+ "INT8 LHS block size must be a multiple of the GEMM m_step and of the GEMV and GEMM mr");+ } // end of uKernelQai8dxpQsi8cxp()...++ /// @brief Kernel used by execute() for a call with M rows+ /// @param M number of LHS rows of the call+ /// @param gemvAllowed whether the GEMV kernel may run on this CPU+ /// @return GEMV for single-row calls when allowed, GEMM otherwise+ static const kai_matmul_clamp_f32_qai8dxp_qsi8cxp_ukernel& selectKernel(size_t M, bool gemvAllowed) noexcept {+ return (gemvAllowed && M == 1) ? Kernels::gemv : Kernels::gemm;+ }size_t get_rhsPackedSize() override {return kai_get_rhs_packed_size_rhs_pack_kxn_qsi8cxp_qsi8cx_neon(N, K, nr, kr, sr);@@ -442,18 +391,29 @@ public:¶ms);}+ // The GEMM; execute() picks the kernel per call with selectKernel()KernelInterface getuKernelInterface() override {- return uKernelInterface;+ return Kernels::gemm;}KAIKernelTag getKernelTag() override {- return KAIKernelTag::I8_NEON_IMM;+ return tag;}+ // Fits the packed LHS layout of either kernel, so one scratchpad serves bothsize_t getLHSPackedSize(size_t m) override {const size_t _m_blocks = (m + BLOCK_SIZE - 1) / BLOCK_SIZE;- packedlhs_block_in_bytes = kai_get_lhs_packed_size_lhs_quant_pack_qai8dxp_f32(BLOCK_SIZE, K, mr, kr, sr);- const size_t lhsPackedSize = packedlhs_block_in_bytes * _m_blocks;+ const size_t gemv_block_in_bytes = kai_get_lhs_packed_size_lhs_quant_pack_qai8dxp_f32(BLOCK_SIZE,+ K,+ Kernels::gemv.get_mr(),+ Kernels::gemv.get_kr(),+ Kernels::gemv.get_sr());+ const size_t gemm_block_in_bytes = kai_get_lhs_packed_size_lhs_quant_pack_qai8dxp_f32(BLOCK_SIZE,+ K,+ Kernels::gemm.get_mr(),+ Kernels::gemm.get_kr(),+ Kernels::gemm.get_sr());+ const size_t lhsPackedSize = std::max(gemv_block_in_bytes, gemm_block_in_bytes) * _m_blocks;return lhsPackedSize;}@@ -462,11 +422,16 @@ public:ov::intel_cpu::Dim K,ov::intel_cpu::MemoryPtr dstMem,ov::intel_cpu::MemoryPtr srcMem) override {- auto _ukernel = std::get<kai_matmul_clamp_f32_qai8dxp_qsi8cxp_ukernel>(getuKernelInterface());- size_t m_step = BLOCK_SIZE;- constexpr size_t n_step = 4;+ const auto& _ukernel = selectKernel(M, useGemv);+ const size_t m_step = BLOCK_SIZE;+ const size_t n_step = _ukernel.get_n_step();const size_t M_BLOCKS = (M + m_step - 1) / m_step;const size_t N_BLOCKS = (N + n_step - 1) / n_step;+ const size_t lhs_packed_block_in_bytes = kai_get_lhs_packed_size_lhs_quant_pack_qai8dxp_f32(m_step,+ K,+ _ukernel.get_mr(),+ _ukernel.get_kr(),+ _ukernel.get_sr());const size_t lhs_packed_offset = _ukernel.get_lhs_packed_offset(0, K);const size_t lhs_stride = K * sizeof(float);@@ -482,13 +447,13 @@ public:ParallelNestingContext nested_context;cpu_parallel->parallel_for(M_BLOCKS, [&](size_t m_blk) {const size_t M_iter = std::min(M - m_blk * m_step, m_step);- auto* lhs_packed_block = lhs_packed_lowp + m_blk * packedlhs_block_in_bytes;+ auto* lhs_packed_block = lhs_packed_lowp + m_blk * lhs_packed_block_in_bytes;kai_run_lhs_quant_pack_qai8dxp_f32(M_iter,K,- mr,- kr,- sr,+ _ukernel.get_mr(),+ _ukernel.get_kr(),+ _ukernel.get_sr(),0,lhs + m_blk * m_step * K, // LHS (F32)lhs_stride,@@ -517,6 +482,19 @@ public:} // end of execute()...};+template <>+class uKernel<KAIKernelTag::I8_NEON_DOTPROD> : public uKernelQai8dxpQsi8cxp<KAIKernelTag::I8_NEON_DOTPROD> {+public:+ uKernel(size_t N, size_t K, MemoryPtr& lhsPackedMem) : uKernelQai8dxpQsi8cxp(N, K, lhsPackedMem, true) {}+};++template <>+class uKernel<KAIKernelTag::I8_NEON_IMM> : public uKernelQai8dxpQsi8cxp<KAIKernelTag::I8_NEON_IMM> {+public:+ uKernel(size_t N, size_t K, MemoryPtr& lhsPackedMem, bool hasDotprod)+ : uKernelQai8dxpQsi8cxp(N, K, lhsPackedMem, hasDotprod) {}+};+template <>class uKernel<KAIKernelTag::I4_NEON_DOTPROD> : public uKernelBase {private:diff --git a/src/plugins/intel_cpu/src/nodes/executors/kleidiai/kleidiai_mm.cpp b/src/plugins/intel_cpu/src/nodes/executors/kleidiai/kleidiai_mm.cppindex 3e2c6248f34..195fbacdb2b 100644--- a/src/plugins/intel_cpu/src/nodes/executors/kleidiai/kleidiai_mm.cpp+++ b/src/plugins/intel_cpu/src/nodes/executors/kleidiai/kleidiai_mm.cpp@@ -229,7 +229,12 @@ MatMulKleidiAIExecutor::MatMulKleidiAIExecutor(const FCAttrs& attrs,case WEIGHT_INT8 | ISA_I8MM | QUANT_CHANNEL | QUANT_SYMMETRIC: {const bool useI8MM = (kernelLookupKey & ISA_I8MM) != 0;if (useI8MM) {- _kernel = std::make_shared<kai_common::uKernel<kai_common::KAIKernelTag::I8_NEON_IMM>>(N, K, lhsPackedMem);+ // The M == 1 GEMV paired with the i8mm GEMM is a dotprod kernel, so it needs FEAT_DotProd+ const bool hasDotprod = hasArmISASupport(ArmISA::DOTPROD);+ _kernel = std::make_shared<kai_common::uKernel<kai_common::KAIKernelTag::I8_NEON_IMM>>(N,+ K,+ lhsPackedMem,+ hasDotprod);} else {_kernel =std::make_shared<kai_common::uKernel<kai_common::KAIKernelTag::I8_NEON_DOTPROD>>(N, K, lhsPackedMem);diff --git a/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/instances/arm/grouped_matmul_compressed.cpp b/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/instances/arm/grouped_matmul_compressed.cppindex 6d2cf9c0fe0..f942d49e7bf 100644--- a/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/instances/arm/grouped_matmul_compressed.cpp+++ b/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/instances/arm/grouped_matmul_compressed.cpp@@ -90,4 +90,33 @@ INSTANTIATE_TEST_SUITE_P(smoke_GroupedMatMul_Compressed_CornerCases,::testing::Values(kleidiai_compressed_config)),GroupedMatMulCompressedLayerTest::getTestCaseName);+// INT8 KleidiAI experts run a 1-row GEMV kernel when an expert gets one row and a 16x4 GEMM+// kernel otherwise. Each expert keeps its executor across iterations, so its row count goes+// 1 -> many -> 1. N % 4 != 0. In 2D x 3D entries every T must be distinct (the routing row is+// looked up by input shape) and each routing row must sum to T.+const std::vector<GroupedMatMulShapeParams> shapes_kernel_switch = {+ // 3D x 3D: every expert gets M rows; M: 1 -> 17 -> 1 -> 3+ {{ov::PartialShape{4, -1, 64}, {{4, 1, 64}, {4, 17, 64}, {4, 1, 64}, {4, 3, 64}}}, {4, 30, 64}, {}},+ // 2D x 3D: rows per expert e0: 1, 1, 0; e1: 1, 2, 1; e2: 1, 17, 0; e3: 1, 17, 1+ {{ov::PartialShape{-1, 77}, {{4, 77}, {37, 77}, {2, 77}}},+ {4, 30, 77},+ TokensPerExpert{{1, 1, 1, 1}, {1, 2, 17, 17}, {0, 1, 0, 1}}},+};++INSTANTIATE_TEST_SUITE_P(smoke_GroupedMatMul_Compressed_KernelSwitch,+ GroupedMatMulCompressedLayerTest,+ ::testing::Combine(::testing::ValuesIn(shapes_kernel_switch),+ ::testing::Values(ov::element::f16),+ ::testing::Values(ov::element::i8),+ ::testing::ValuesIn(decompression_precisions),+ ::testing::Values(ov::element::f32),+ ::testing::Values(DecompressionType::full),+ ::testing::Values(DecompressionType::empty),+ ::testing::Values(false),+ ::testing::Values(-1),+ ::testing::Values(ov::test::utils::DEVICE_CPU),+ ::testing::Values("GatherMatmulCompressed"),+ ::testing::Values(kleidiai_compressed_config)),+ GroupedMatMulCompressedLayerTest::getTestCaseName);+} // namespacediff --git a/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/arm/matmul_weights_decompression.cpp b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/arm/matmul_weights_decompression.cppindex 410588bd8b6..63776b4efe8 100644--- a/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/arm/matmul_weights_decompression.cpp+++ b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/arm/matmul_weights_decompression.cpp@@ -180,6 +180,46 @@ INSTANTIATE_TEST_SUITE_P(smoke_MatMulCompressedWeights_corner_cases,::testing::Values(should_use_decompression_impl())),MatmulWeightsDecompression::getTestCaseName);+// INT8 KleidiAI FC runs a 1-row GEMV kernel when M == 1 and a 16x4 GEMM kernel otherwise+// (the i8mm path keeps the GEMM at M == 1 on a CPU without dotprod).+// M is the product of all activation dims except the last one (e.g. {2, 8, 64} -> M = 16).+// Dynamic entries compile once and run every target shape on the same executor, so the+// kernel switches back and forth between calls.+const std::vector<MatMulDecompressionShapeParams> input_shapes_kleidiai_i8_kernel_switch = {+ // M: 1 -> 37 -> 1 -> 2 -> 16 -> 3 (the last two use the batch dim)+ {{{-1, -1, -1}, {{1, 1, 64}, {1, 37, 64}, {1, 1, 64}, {1, 2, 64}, {2, 8, 64}, {3, 1, 64}}}, {64, 96}},+ // M == 1, K not a multiple of 32, N not a multiple of 4+ {{{}, {{1, 1, 77}}}, {77, 30}},+ // M == 1 with 3D weights+ {{{}, {{1, 1, 64}}}, {1, 64, 36}},+ // M smaller than the GEMM mr+ {{{-1, -1, -1}, {{1, 2, 48}, {1, 3, 48}}}, {48, 64}},+ // GEMM block boundaries (16, 17, 33), then back to the GEMV+ {{{-1, -1, -1}, {{1, 16, 128}, {1, 17, 128}, {1, 33, 128}, {1, 1, 128}}}, {128, 40}},+ // N < 4 with M == 1 and M > 1+ {{{-1, -1, -1}, {{1, 1, 32}, {1, 5, 32}}}, {32, 3}},+ // 2D activations, M: 1 -> 20 -> 1+ {{{-1, 96}, {{1, 96}, {20, 96}, {1, 96}}}, {96, 50}},+};++const ov::AnyMap kleidiai_i8_kernel_switch_config = {ov::hint::dynamic_quantization_group_size(UINT64_MAX),+ ov::hint::inference_precision(ov::element::f32)};++INSTANTIATE_TEST_SUITE_P(smoke_MatMulCompressedWeights_Kleidiai_i8_kernel_switch,+ MatmulWeightsDecompression,+ ::testing::Combine(::testing::ValuesIn(input_shapes_kleidiai_i8_kernel_switch),+ ::testing::Values(ov::element::i8),+ ::testing::Values(ov::element::f32),+ ::testing::Values(ov::element::dynamic),+ ::testing::Values(true, false),+ ::testing::Values(DecompressionType::full),+ ::testing::Values(DecompressionType::empty),+ ::testing::Values(false),+ ::testing::Values(kleidiai_i8_kernel_switch_config),+ ::testing::ValuesIn(fusing_params),+ ::testing::Values(true)),+ MatmulWeightsDecompression::getTestCaseName);+} // namespace} // namespace test} // namespace ov\ No newline at end of filediff --git a/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/classes/matmul_weights_decompression.cpp b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/classes/matmul_weights_decompression.cppindex 0dfae8b808e..098e9a96417 100644--- a/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/classes/matmul_weights_decompression.cpp+++ b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/classes/matmul_weights_decompression.cpp@@ -4,6 +4,9 @@#include "matmul_weights_decompression.hpp"+#include <cstdint>+#include <limits>+#include "common_test_utils/subgraph_builders/weights_decompression_builders.hpp"#include "openvino/runtime/intel_cpu/properties.hpp"#include "u3_symmetric_zero_point.hpp"@@ -154,6 +157,18 @@ void MatmulWeightsDecompression::check_results() {}#endif+#if defined(OPENVINO_ARCH_ARM64) && defined(OV_CPU_WITH_KLEIDIAI)+ // Integer weights kept compressed under dynamic quantization (group size UINT64_MAX) must run+ // on the KleidiAI executor; the other ARM FC executors would take the decompressed weights.+ const auto dyn_quant_it = configuration.find(ov::hint::dynamic_quantization_group_size.name());+ const bool kleidiai_dyn_quant = dyn_quant_it != configuration.end() &&+ dyn_quant_it->second.as<uint64_t>() == std::numeric_limits<uint64_t>::max();+ if (use_matmul_decompression_impl && kleidiai_dyn_quant && compressed_weights_precision.is_integral()) {+ const auto implType = fc->get_rt_info().at(ov::exec_model_info::IMPL_TYPE).as<std::string>();+ EXPECT_NE(implType.find("kleidiai"), std::string::npos) << "unexpected impl type: " << implType;+ }+#endif+const auto& expected_weights_precision =use_matmul_decompression_impl ? compressed_weights_precision : fc->get_input_element_type(0);EXPECT_EQ(fc->get_input_element_type(1), expected_weights_precision);diff --git a/src/plugins/intel_cpu/tests/unit/nodes/executors/kleidiai/kleidiai_int8_ukernel_test.cpp b/src/plugins/intel_cpu/tests/unit/nodes/executors/kleidiai/kleidiai_int8_ukernel_test.cppnew file mode 100644index 00000000000..8420ba20342--- /dev/null+++ b/src/plugins/intel_cpu/tests/unit/nodes/executors/kleidiai/kleidiai_int8_ukernel_test.cpp@@ -0,0 +1,215 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++// MSVC builds only the KleidiAI assembly kernels: each INT8 pair falls back to a single kernel there+#if !defined(_MSC_VER)++# include <gtest/gtest.h>++# include <algorithm>+# include <cmath>+# include <cstddef>+# include <cstdint>+# include <cstring>+# include <limits>+# include <memory>+# include <oneapi/dnnl/dnnl.hpp>+# include <random>+# include <string>+# include <tuple>+# include <vector>++# include "cpu_memory.h"+# include "cpu_parallel.hpp"+# include "cpu_shape.h"+# include "kai/ukernels/matmul/matmul_clamp_f32_qai8dxp_qsi8cxp/kai_matmul_clamp_f32_qai8dxp1x4_qsi8cxp4x4_1x4_neon_dotprod.h"+# include "kai/ukernels/matmul/matmul_clamp_f32_qai8dxp_qsi8cxp/kai_matmul_clamp_f32_qai8dxp1x8_qsi8cxp4x8_1x4_neon_dotprod.h"+# include "kai/ukernels/matmul/matmul_clamp_f32_qai8dxp_qsi8cxp/kai_matmul_clamp_f32_qai8dxp4x4_qsi8cxp4x4_16x4_neon_dotprod.h"+# include "kai/ukernels/matmul/matmul_clamp_f32_qai8dxp_qsi8cxp/kai_matmul_clamp_f32_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm.h"+# include "memory_desc/cpu_blocked_memory_desc.h"+# include "nodes/executors/kleidiai/kleidiai_common.hpp"+# include "openvino/core/type/element_type.hpp"+# include "openvino/runtime/intel_cpu/properties.hpp"+# include "utils/arm_isa_support.h"++using namespace ov::intel_cpu;+using kai_common::KAIKernelTag;++namespace {++using DotprodKernel = kai_common::uKernelQai8dxpQsi8cxp<KAIKernelTag::I8_NEON_DOTPROD>;+using I8mmKernel = kai_common::uKernelQai8dxpQsi8cxp<KAIKernelTag::I8_NEON_IMM>;++const std::vector<size_t> multi_row_counts = {0, 2, 3, 4, 5, 15, 16, 17, 33, 1024};++// The GEMV runs only for single-row calls, and only when allowed (it needs FEAT_DotProd on both pairs).+TEST(KleidiAIInt8KernelSelection, DotprodPair) {+ EXPECT_TRUE(DotprodKernel::selectKernel(1, true).run_matmul ==+ &kai_run_matmul_clamp_f32_qai8dxp1x4_qsi8cxp4x4_1x4_neon_dotprod);+ EXPECT_TRUE(DotprodKernel::selectKernel(1, false).run_matmul ==+ &kai_run_matmul_clamp_f32_qai8dxp4x4_qsi8cxp4x4_16x4_neon_dotprod);+ for (const size_t M : multi_row_counts) {+ EXPECT_TRUE(DotprodKernel::selectKernel(M, true).run_matmul ==+ &kai_run_matmul_clamp_f32_qai8dxp4x4_qsi8cxp4x4_16x4_neon_dotprod)+ << "M=" << M;+ }+}++TEST(KleidiAIInt8KernelSelection, I8mmPair) {+ EXPECT_TRUE(I8mmKernel::selectKernel(1, true).run_matmul ==+ &kai_run_matmul_clamp_f32_qai8dxp1x8_qsi8cxp4x8_1x4_neon_dotprod);+ // i8mm without dotprod: keep the i8mm GEMM even for a single row+ EXPECT_TRUE(I8mmKernel::selectKernel(1, false).run_matmul ==+ &kai_run_matmul_clamp_f32_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm);+ for (const size_t M : multi_row_counts) {+ for (const bool useGemv : {true, false}) {+ EXPECT_TRUE(I8mmKernel::selectKernel(M, useGemv).run_matmul ==+ &kai_run_matmul_clamp_f32_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm)+ << "M=" << M << " useGemv=" << useGemv;+ }+ }+}++MemoryPtr makeMemory(const dnnl::engine& eng, ov::element::Type prc, const VectorDims& dims) {+ return std::make_shared<Memory>(eng, std::make_shared<CpuBlockedMemoryDesc>(prc, Shape(dims)));+}++constexpr size_t canary_bytes = 256;+constexpr uint8_t canary_value = 0xA5;++// Runs one execute() call the way MatMulKleidiAIExecutor does: update() sizes the LHS scratchpad+// with getLHSPackedSize(M), then execute(). Checks that the call stays inside the scratchpad and+// writes every output element.+template <KAIKernelTag tag>+std::vector<float> run(kai_common::uKernelQai8dxpQsi8cxp<tag>& kernel,+ MemoryPtr& lhsPackedMem,+ const dnnl::engine& eng,+ const CpuParallelPtr& cpuParallel,+ const float* lhs,+ size_t M,+ size_t N,+ size_t K) {+ const size_t lhsPackedSize = kernel.getLHSPackedSize(M);+ lhsPackedMem = makeMemory(eng, ov::element::i8, {lhsPackedSize + canary_bytes});+ auto* canary = lhsPackedMem->getDataAs<uint8_t>() + lhsPackedSize;+ std::memset(canary, canary_value, canary_bytes);++ auto srcMem = makeMemory(eng, ov::element::f32, {M, K});+ std::memcpy(srcMem->getData(), lhs, M * K * sizeof(float));+ auto dstMem = makeMemory(eng, ov::element::f32, {M, N});+ std::fill_n(dstMem->getDataAs<float>(), M * N, std::numeric_limits<float>::quiet_NaN());++ kernel.execute(cpuParallel, M, K, dstMem, srcMem);++ EXPECT_TRUE(std::all_of(canary,+ canary + canary_bytes,+ [](uint8_t b) {+ return b == canary_value;+ }))+ << "LHS packing wrote past getLHSPackedSize(" << M << ")";+ const auto* dst = dstMem->getDataAs<const float>();+ EXPECT_TRUE(std::none_of(dst,+ dst + M * N,+ [](float v) {+ return std::isnan(v);+ }))+ << "output not fully written for M=" << M;+ return {dst, dst + M * N};+}++using Int8UKernelParams = std::tuple<KAIKernelTag, size_t /*N*/, size_t /*K*/>;++class KleidiAIInt8GemvGemmTest : public ::testing::TestWithParam<Int8UKernelParams> {+public:+ static std::string getTestCaseName(const ::testing::TestParamInfo<Int8UKernelParams>& obj) {+ const auto& [tag, N, K] = obj.param;+ return std::string(tag == KAIKernelTag::I8_NEON_DOTPROD ? "dotprod" : "i8mm") + "_N" + std::to_string(N) ++ "_K" + std::to_string(K);+ }++protected:+ template <KAIKernelTag tag>+ void check(size_t N, size_t K) {+ const dnnl::engine eng(dnnl::engine::kind::cpu, 0);+ const auto cpuParallel = std::make_shared<CpuParallel>(ov::intel_cpu::TbbPartitioner::STATIC);+ std::mt19937 gen(static_cast<uint32_t>(N * 7919 + K));++ // Weights K x N (kxn layout, as packed by the executor), per-channel scales, bias+ auto weiMem = makeMemory(eng, ov::element::i8, {K, N});+ auto* wei = weiMem->getDataAs<int8_t>();+ std::uniform_int_distribution<int> weiDist(-127, 127);+ std::generate_n(wei, K * N, [&] {+ return static_cast<int8_t>(weiDist(gen));+ });+ std::uniform_real_distribution<float> scaleDist(0.001F, 0.01F);+ std::vector<float> scales(N);+ std::generate(scales.begin(), scales.end(), [&] {+ return scaleDist(gen);+ });+ std::uniform_real_distribution<float> unitDist(-1.F, 1.F);+ auto biasMem = makeMemory(eng, ov::element::f32, {N});+ std::generate_n(biasMem->getDataAs<float>(), N, [&] {+ return unitDist(gen);+ });++ // Both objects work on one packed RHS buffer and one LHS scratchpad, so the GEMV+ // has to read the layout the GEMM packs+ MemoryPtr lhsPackedMem;+ kai_common::uKernelQai8dxpQsi8cxp<tag> withGemv(N, K, lhsPackedMem, true);+ kai_common::uKernelQai8dxpQsi8cxp<tag> gemmOnly(N, K, lhsPackedMem, false);+ auto rhsPacked = makeMemory(eng, ov::element::i8, {gemmOnly.get_rhsPackedSize()});+ gemmOnly.packData(false, weiMem, biasMem, true, scales.data(), rhsPacked);+ withGemv.packData(false, weiMem, biasMem, true, scales.data(), rhsPacked);++ for (const size_t M : {1, 2, 3, 4, 5, 15, 16, 17, 33}) {+ SCOPED_TRACE("M=" + std::to_string(M));+ std::vector<float> lhs(M * K);+ std::generate(lhs.begin(), lhs.end(), [&] {+ return unitDist(gen);+ });++ // One call with all rows: GEMM when M > 1, GEMV when M == 1+ const auto batch = run<tag>(withGemv, lhsPackedMem, eng, cpuParallel, lhs.data(), M, N, K);++ for (size_t r = 0; r < M; ++r) {+ const float* row = lhs.data() + r * K;+ // GEMV on the row alone, and the GEMM on the same single row+ const auto gemvRow = run<tag>(withGemv, lhsPackedMem, eng, cpuParallel, row, 1, N, K);+ const auto gemmRow = run<tag>(gemmOnly, lhsPackedMem, eng, cpuParallel, row, 1, N, K);+ // Per-row LHS quantization and the same epilogue make the kernels bit-identical+ EXPECT_EQ(std::memcmp(gemvRow.data(), batch.data() + r * N, N * sizeof(float)), 0)+ << "GEMV row differs from the batched result, row " << r;+ EXPECT_EQ(std::memcmp(gemvRow.data(), gemmRow.data(), N * sizeof(float)), 0)+ << "GEMV and GEMM differ on a single row, row " << r;+ }+ }+ }+};++TEST_P(KleidiAIInt8GemvGemmTest, RowsMatchBitExact) {+ const auto& [tag, N, K] = GetParam();+ // Every GEMV here executes SDOT; the i8mm pair also needs FEAT_I8MM for its GEMM.+ if (!hasArmISASupport(ArmISA::DOTPROD)) {+ GTEST_SKIP() << "FEAT_DotProd is not available";+ }+ if (tag == KAIKernelTag::I8_NEON_IMM) {+ if (!hasArmISASupport(ArmISA::I8MM)) {+ GTEST_SKIP() << "FEAT_I8MM is not available";+ }+ check<KAIKernelTag::I8_NEON_IMM>(N, K);+ } else {+ check<KAIKernelTag::I8_NEON_DOTPROD>(N, K);+ }+}++INSTANTIATE_TEST_SUITE_P(smoke_KleidiAIInt8,+ KleidiAIInt8GemvGemmTest,+ ::testing::Combine(::testing::Values(KAIKernelTag::I8_NEON_DOTPROD, KAIKernelTag::I8_NEON_IMM),+ ::testing::Values(size_t{3}, size_t{30}),+ ::testing::Values(size_t{32}, size_t{77}, size_t{513})),+ KleidiAIInt8GemvGemmTest::getTestCaseName);++} // namespace++#endif // !defined(_MSC_VER)diff --git a/src/plugins/intel_cpu/tests/unit/sources.cmake b/src/plugins/intel_cpu/tests/unit/sources.cmakeindex abc8729e38d..fde30244b33 100644--- a/src/plugins/intel_cpu/tests/unit/sources.cmake+++ b/src/plugins/intel_cpu/tests/unit/sources.cmake@@ -222,6 +222,10 @@ set(CPU_UNIT_TESTS_MLAS_SRCS${CMAKE_CURRENT_LIST_DIR}/gemm_api_test.cpp)+set(CPU_UNIT_TESTS_KLEIDIAI_SRCS+ ${CMAKE_CURRENT_LIST_DIR}/nodes/executors/kleidiai/kleidiai_int8_ukernel_test.cpp+)+set(CPU_UNIT_TESTS_CHECK_SOURCES_EXCLUDE_FILES)if(ARM OR AARCH64)list(APPEND CPU_UNIT_TESTS_SRCS ${CPU_UNIT_TESTS_ARM_SRCS})@@ -247,6 +251,12 @@ else()list(APPEND CPU_UNIT_TESTS_CHECK_SOURCES_EXCLUDE_FILES ${CPU_UNIT_TESTS_MLAS_SRCS})endif()+if(ENABLE_KLEIDIAI_FOR_CPU)+ list(APPEND CPU_UNIT_TESTS_SRCS ${CPU_UNIT_TESTS_KLEIDIAI_SRCS})+else()+ list(APPEND CPU_UNIT_TESTS_CHECK_SOURCES_EXCLUDE_FILES ${CPU_UNIT_TESTS_KLEIDIAI_SRCS})+endif()+function(group_source_files GROUP_NAME)foreach(source_file ${ARGN})set_source_files_properties(${source_file} PROPERTIES UNITY_GROUP ${GROUP_NAME})