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
Refactored Kleidiai executor and Support for INT4 groupwise asym Quantization (#37086)
hrushitfujitsu161d6118fd10
diff --git a/src/plugins/intel_cpu/src/nodes/executors/fullyconnected_implementations.cpp b/src/plugins/intel_cpu/src/nodes/executors/fullyconnected_implementations.cppindex a0b5cdc04ca..2ce2c13f73f 100644--- a/src/plugins/intel_cpu/src/nodes/executors/fullyconnected_implementations.cpp+++ b/src/plugins/intel_cpu/src/nodes/executors/fullyconnected_implementations.cpp@@ -106,6 +106,20 @@ static const TypeMapping aclLowpFCTypeMapping {{{_u8 | _i8, _i8, _any, _f32}, {bypass(), bypass(), use<3>(), bypass()}}};+#if defined(OV_CPU_WITH_KLEIDIAI)+static const TypeMapping kleidiaiFCTypeMapping {+ // {src, wei, bia, dst} pt<src, wei, bias, dst>+ // f32 weights are used as-is by the F32 NEON MLA kernel+ {{_f32, _f32, _any, _any}, {bypass(), bypass(), use<0>(), use<0>()}},+ // Keep low-precision (compressed) weights intact so the dynamic-quant+ // KleidiAI kernels (i8 / i4 / u4, incl. group asymmetric) receive the+ // original weight precision instead of an upconverted f32 tensor.+ {{_f32, _i8 | _i4 | _u4, _any, _any}, {bypass(), bypass(), use<0>(), use<0>()}},+ // fallback+ {{_any, _any, _any, _any}, {just<f32>(), just<f32>(), just<f32>(), just<f32>()}},+};+#endif+static const MappingNotation fcMappingNotation {{ARG_SRC, 0},{ARG_WEI, 1},@@ -371,14 +385,20 @@ const std::vector<ExecutorImplementation<FCAttrs>>& getImplementations() {VERIFY(noPostOps(config), UNSUPPORTED_POST_OPS);VERIFY(noSparseDecompression(config), UNSUPPORTED_SPARSE_WEIGHTS);VERIFY(all_of(f32, srcType(config), dstType(config)), UNSUPPORTED_SRC_PRECISIONS);- VERIFY(any_of(weiType(config), f32, i8, i4), UNSUPPORTED_WEI_PRECISIONS);+ VERIFY(any_of(weiType(config), f32, i8, i4, u4), UNSUPPORTED_WEI_PRECISIONS);VERIFY(implication(hasBias(config), biaType(config) == f32), UNSUPPORTED_SRC_PRECISIONS);VERIFY(weiRank(config) == 2U, UNSUPPORTED_WEI_RANK);VERIFY(MatMulKleidiAIExecutor::supports(config), UNSUPPORTED_BY_EXECUTOR);return true;},- HasNoOptimalConfig<FCAttrs>{},+ // createOptimalConfig+ [](const FCConfig& config) -> std::optional<executor::Config<FCAttrs>> {+ return createOptimalConfigCommon(config,+ kleidiaiFCTypeMapping,+ dnnlFCLayoutConfig,+ fcMappingNotation);+ },AcceptsAnyShape<FCAttrs>,CreateDefault<MatMulKleidiAIExecutor, FCAttrs>{})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 79e134609e6..400fc4aeac0 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@@ -57,11 +57,16 @@#include "kai/ukernels/matmul/pack/kai_rhs_pack_kxn_qsi4c32p_qsu4c32s1s0.h"#include "kai/ukernels/matmul/pack/kai_rhs_pack_nxk_qsi4c32p_qsu4c32s1s0.h"-namespace ov::intel_cpu::kai_common {+// Headers for INT4 group asymmetric KAI kernels+#include "kai/ukernels/matmul/matmul_clamp_f32_qsi8d32p_qai4c32p/kai_matmul_clamp_f32_qsi8d32p4x4_qai4c32p4x4_8x4_neon_dotprod.h"+#include "kai/ukernels/matmul/matmul_clamp_f32_qsi8d32p_qai4c32p/kai_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm.h"+#include "kai/ukernels/matmul/matmul_clamp_f32_qsi8d32p_qai4c32p/kai_matmul_clamp_f32_qsi8d32p_qai4c32p_interface.h"+#include "kai/ukernels/matmul/pack/kai_lhs_quant_pack_qsi8d32pscalef32_f32_neon.h"+#include "kai/ukernels/matmul/pack/kai_rhs_pack_nxk_qai4c32p_qau4c32s0s1_f32_f32_f32_neon.h"+namespace ov::intel_cpu::kai_common {inline constexpr float FLOAT_MAX = std::numeric_limits<float>::max();inline constexpr float FLOAT_MIN = (-std::numeric_limits<float>::max());-enum class KAIKernelTag : std::uint8_t {F32_NEON_MLA,I8_NEON_DOTPROD,@@ -69,13 +74,16 @@ enum class KAIKernelTag : std::uint8_t {I4_NEON_DOTPROD,I4_NEON_IMM,I4_NEON_IMM_GROUP,- I4_NEON_DOTPROD_GROUP+ I4_NEON_DOTPROD_GROUP,+ I4_NEON_IMM_GROUP_ASYM,+ I4_NEON_DOTPROD_GROUP_ASYM};using KernelInterface = std::variant<kai_matmul_clamp_f32_f32_f32p_ukernel,kai_matmul_clamp_f32_qai8dxp_qsi8cxp_ukernel,kai_matmul_clamp_f32_qai8dxp_qsi4cxp_ukernel,- kai_matmul_clamp_f32_qai8dxp_qsi4c32p_ukernel>;+ kai_matmul_clamp_f32_qai8dxp_qsi4c32p_ukernel,+ kai_matmul_clamp_f32_qsi8d32p_qai4c32p_ukernel>;class uKernelBase {protected:@@ -1181,4 +1189,425 @@ public:});} // end of execute()...};-} // namespace ov::intel_cpu::kai_common\ No newline at end of file+template <>+class uKernel<KAIKernelTag::I4_NEON_DOTPROD_GROUP_ASYM> : public uKernelBase {+private:+ static constexpr kai_matmul_clamp_f32_qsi8d32p_qai4c32p_ukernel uKernelInterface{+ kai_get_m_step_matmul_clamp_f32_qsi8d32p4x4_qai4c32p4x4_8x4_neon_dotprod,+ kai_get_n_step_matmul_clamp_f32_qsi8d32p4x4_qai4c32p4x4_8x4_neon_dotprod,+ kai_get_mr_matmul_clamp_f32_qsi8d32p4x4_qai4c32p4x4_8x4_neon_dotprod,+ kai_get_nr_matmul_clamp_f32_qsi8d32p4x4_qai4c32p4x4_8x4_neon_dotprod,+ kai_get_kr_matmul_clamp_f32_qsi8d32p4x4_qai4c32p4x4_8x4_neon_dotprod,+ kai_get_sr_matmul_clamp_f32_qsi8d32p4x4_qai4c32p4x4_8x4_neon_dotprod,+ kai_get_lhs_packed_offset_matmul_clamp_f32_qsi8d32p4x4_qai4c32p4x4_8x4_neon_dotprod,+ kai_get_rhs_packed_offset_matmul_clamp_f32_qsi8d32p4x4_qai4c32p4x4_8x4_neon_dotprod,+ kai_get_dst_offset_matmul_clamp_f32_qsi8d32p4x4_qai4c32p4x4_8x4_neon_dotprod,+ kai_get_dst_size_matmul_clamp_f32_qsi8d32p4x4_qai4c32p4x4_8x4_neon_dotprod,+ kai_run_matmul_clamp_f32_qsi8d32p4x4_qai4c32p4x4_8x4_neon_dotprod};++ MemoryPtr rhsPackedMem;+ MemoryPtr& lhsPackedMem;+ MemoryCPtr rhsZeroPointsMem;+ size_t group_size = 0;++public:+ uKernel(size_t N, size_t K, MemoryPtr& lhsPackedMem, const MemoryArgs& memory) : lhsPackedMem(lhsPackedMem) {+ this->N = N;+ this->K = K;+ const auto scalesDims = memory.at(ARG_WEI | ARG_ATTR_SCALES)->getDesc().getShape().getDims();+ OPENVINO_ASSERT(scalesDims.size() > 1,+ "Group quantization requires scales with at least two dimensions. Got ",+ scalesDims.size(),+ ".");+ OPENVINO_ASSERT(scalesDims[1] != 0 && K % scalesDims[1] == 0, "Invalid scales shape for group quantization.");+ group_size = K / scalesDims[1];+ OPENVINO_ASSERT(group_size % 32 == 0, "Group size must be a multiple of 32. Got ", group_size);+ rhsZeroPointsMem = memory.at(ARG_WEI | ARG_ATTR_ZERO_POINTS);+ OPENVINO_ASSERT(rhsZeroPointsMem, "INT4 asymmetric group quantization requires weight zero-points.");++ const auto zeroPointDims = rhsZeroPointsMem->getDesc().getShape().getDims();++ OPENVINO_ASSERT(zeroPointDims == scalesDims, "Zero-point and scale tensors must have the same shape.");+ const auto zpPrec = rhsZeroPointsMem->getDescPtr()->getPrecision();+ OPENVINO_ASSERT(zpPrec == ov::element::u4 || zpPrec == ov::element::f32,+ "INT4 asymmetric group KleidiAI kernel expects u4 or f32 "+ "zero-points. Got ",+ zpPrec,+ ".");+ this->BLOCK_SIZE = uKernelInterface.get_m_step();+ this->mr = uKernelInterface.get_mr();+ this->nr = uKernelInterface.get_nr();+ this->kr = uKernelInterface.get_kr();+ this->sr = uKernelInterface.get_sr();+ }++ size_t get_rhsPackedSize() override {+ return kai_get_rhs_packed_size_rhs_pack_nxk_qai4c32p_qau4c32s0s1_f32_f32_f32_neon(N, K, nr, kr, group_size);+ }++ void packData(bool isTransposed,+ MemoryCPtr weightsMemory,+ MemoryPtr biasMem,+ bool hasBias,+ float* rhs_scales,+ MemoryPtr rhsPackedMemory) override {+ OPENVINO_ASSERT(rhs_scales != nullptr, "INT4 asymmetric group quantization requires scales.");++ OPENVINO_ASSERT(!isTransposed, "ASYM qai4c32p path currently supports non-transposed NxK weights.");++ OPENVINO_ASSERT(rhsZeroPointsMem, "INT4 asymmetric group quantization requires zero-points.");++ rhsPackedMem = rhsPackedMemory;++ const auto* bias_ptr = hasBias ? biasMem->getDataAs<float>() : nullptr;++ const size_t groups_per_row = K / group_size;+ const size_t num_zero_points = N * groups_per_row;+ const size_t weight_bytes = N * K / 2;++ const auto* rhs_qau4_s1s0 = weightsMemory->getDataAs<uint8_t>();++ std::vector<uint8_t> rhs_qau4_s0s1(weight_bytes);+ for (size_t i = 0; i < weight_bytes; ++i) {+ const uint8_t b = rhs_qau4_s1s0[i];++ rhs_qau4_s0s1[i] = static_cast<uint8_t>(((b & 0x0FU) << 4) | ((b & 0xF0U) >> 4));+ }++ // Read a logical U4 value from OpenVINO's packed s1s0 storage.+ auto read_u4_s1s0 = [](const uint8_t* data, size_t index) -> uint8_t {+ const uint8_t b = data[index / 2];++ return (index & 1U) ? ((b >> 4) & 0x0FU) : (b & 0x0FU);+ };++ const auto* zp_qau4_s1s0 = rhsZeroPointsMem->getDataAs<uint8_t>();++ std::vector<float> zero_points_f32(num_zero_points);++ for (size_t i = 0; i < num_zero_points; ++i) {+ const int32_t zp_signed = static_cast<int32_t>(read_u4_s1s0(zp_qau4_s1s0, i)) - 8;++ zero_points_f32[i] = -static_cast<float>(zp_signed) * rhs_scales[i];+ }++ kai_rhs_pack_nxk_qai4c32p_params params{};+ params.lhs_zero_point = 1;+ params.rhs_zero_point = 8;++ kai_run_rhs_pack_nxk_qai4c32p_qau4c32s0s1_f32_f32_f32_neon(1,+ N,+ K,+ nr,+ kr,+ sr,+ group_size,+ rhs_qau4_s0s1.data(),+ zero_points_f32.data(),+ bias_ptr,+ rhs_scales,+ rhsPackedMem->getData(),+ 0,+ ¶ms);+ }+ KernelInterface getuKernelInterface() override {+ return uKernelInterface;+ }++ KAIKernelTag getKernelTag() override {+ return KAIKernelTag::I4_NEON_DOTPROD_GROUP_ASYM;+ }++ 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_qsi8d32pscalef32_f32_neon(BLOCK_SIZE, K, group_size, mr, kr, sr);++ return m_blocks * packedlhs_block_in_bytes;+ }++ 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 {+ const auto ukernel = std::get<kai_matmul_clamp_f32_qsi8d32p_qai4c32p_ukernel>(getuKernelInterface());++ auto* lhs = srcMem->getDataAs<float>();+ auto* dst = dstMem->getDataAs<float>();++ auto* lhs_packed = lhsPackedMem->getDataAs<int8_t>();+ auto* rhs_packed = rhsPackedMem->getDataAs<int8_t>();++ const size_t m_step = ukernel.get_m_step();+ 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_offset = ukernel.get_lhs_packed_offset(0, K, group_size);++ const size_t lhs_stride = K * sizeof(float);+ 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 + m_blk * packedlhs_block_in_bytes;++ kai_run_lhs_quant_pack_qsi8d32pscalef32_f32_neon(m_iter,+ K,+ group_size,+ mr,+ kr,+ sr,+ 0,+ lhs + m_blk * m_step * K,+ lhs_stride,+ lhs_packed_block);++ cpu_parallel->parallel_for(n_blocks, [&](size_t n_blk) {+ const size_t n_start = n_blk * n_step;+ const size_t n_iter = std::min(N - n_start, n_step);++ const size_t rhs_packed_offset = ukernel.get_rhs_packed_offset(n_start, K, group_size);++ const size_t dst_offset = ukernel.get_dst_offset(m_blk * m_step, n_start, dst_stride_row);++ const auto* lhs_ptr = static_cast<const void*>(lhs_packed_block + lhs_packed_offset);++ const auto* rhs_ptr = static_cast<const void*>(rhs_packed + rhs_packed_offset);++ auto* dst_ptr = dst + dst_offset / sizeof(float);++ ukernel.run_matmul(m_iter,+ n_iter,+ K,+ group_size,+ lhs_ptr,+ rhs_ptr,+ dst_ptr,+ dst_stride_row,+ dst_stride_col,+ FLOAT_MIN,+ FLOAT_MAX);+ });+ });+ }+};++template <>+class uKernel<KAIKernelTag::I4_NEON_IMM_GROUP_ASYM> : public uKernelBase {+private:+ static constexpr kai_matmul_clamp_f32_qsi8d32p_qai4c32p_ukernel uKernelInterface{+ kai_get_m_step_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm,+ kai_get_n_step_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm,+ kai_get_mr_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm,+ kai_get_nr_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm,+ kai_get_kr_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm,+ kai_get_sr_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm,+ kai_get_lhs_packed_offset_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm,+ kai_get_rhs_packed_offset_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm,+ kai_get_dst_offset_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm,+ kai_get_dst_size_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm,+ kai_run_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm};++ MemoryPtr rhsPackedMem;+ MemoryPtr& lhsPackedMem;+ MemoryCPtr rhsZeroPointsMem;+ size_t group_size = 0;++public:+ uKernel(size_t N, size_t K, MemoryPtr& lhsPackedMem, const MemoryArgs& memory) : lhsPackedMem(lhsPackedMem) {+ this->N = N;+ this->K = K;+ const auto scalesDims = memory.at(ARG_WEI | ARG_ATTR_SCALES)->getDesc().getShape().getDims();+ OPENVINO_ASSERT(scalesDims.size() > 1,+ "Group quantization requires scales with at least two dimensions. Got ",+ scalesDims.size(),+ ".");+ OPENVINO_ASSERT(scalesDims[1] != 0 && K % scalesDims[1] == 0, "Invalid scales shape for group quantization.");+ group_size = K / scalesDims[1];+ OPENVINO_ASSERT(group_size % 32 == 0, "Group size must be a multiple of 32. Got ", group_size);+ rhsZeroPointsMem = memory.at(ARG_WEI | ARG_ATTR_ZERO_POINTS);+ OPENVINO_ASSERT(rhsZeroPointsMem, "INT4 asymmetric group quantization requires weight zero-points.");++ const auto zeroPointDims = rhsZeroPointsMem->getDesc().getShape().getDims();++ OPENVINO_ASSERT(zeroPointDims == scalesDims, "Zero-point and scale tensors must have the same shape.");+ const auto zpPrec = rhsZeroPointsMem->getDescPtr()->getPrecision();+ OPENVINO_ASSERT(zpPrec == ov::element::u4 || zpPrec == ov::element::f32,+ "INT4 asymmetric group KleidiAI kernel expects u4 or f32 "+ "zero-points. Got ",+ zpPrec,+ ".");+ this->BLOCK_SIZE = uKernelInterface.get_m_step();+ this->mr = uKernelInterface.get_mr();+ this->nr = uKernelInterface.get_nr();+ this->kr = uKernelInterface.get_kr();+ this->sr = uKernelInterface.get_sr();+ }++ size_t get_rhsPackedSize() override {+ return kai_get_rhs_packed_size_rhs_pack_nxk_qai4c32p_qau4c32s0s1_f32_f32_f32_neon(N, K, nr, kr, group_size);+ }++ void packData(bool isTransposed,+ MemoryCPtr weightsMemory,+ MemoryPtr biasMem,+ bool hasBias,+ float* rhs_scales,+ MemoryPtr rhsPackedMemory) override {+ OPENVINO_ASSERT(rhs_scales != nullptr, "INT4 asymmetric group quantization requires scales.");++ OPENVINO_ASSERT(!isTransposed, "ASYM qai4c32p path currently supports non-transposed NxK weights.");++ OPENVINO_ASSERT(rhsZeroPointsMem, "INT4 asymmetric group quantization requires zero-points.");++ rhsPackedMem = rhsPackedMemory;++ const auto* bias_ptr = hasBias ? biasMem->getDataAs<float>() : nullptr;++ const size_t groups_per_row = K / group_size;+ const size_t num_zero_points = N * groups_per_row;+ const size_t weight_bytes = N * K / 2;++ const auto* rhs_qau4_s1s0 = weightsMemory->getDataAs<uint8_t>();++ std::vector<uint8_t> rhs_qau4_s0s1(weight_bytes);+ for (size_t i = 0; i < weight_bytes; ++i) {+ const uint8_t b = rhs_qau4_s1s0[i];++ rhs_qau4_s0s1[i] = static_cast<uint8_t>(((b & 0x0FU) << 4) | ((b & 0xF0U) >> 4));+ }++ // Read a logical U4 value from OpenVINO's packed s1s0 storage.+ auto read_u4_s1s0 = [](const uint8_t* data, size_t index) -> uint8_t {+ const uint8_t b = data[index / 2];++ return (index & 1U) ? ((b >> 4) & 0x0FU) : (b & 0x0FU);+ };++ const auto* zp_qau4_s1s0 = rhsZeroPointsMem->getDataAs<uint8_t>();++ std::vector<float> zero_points_f32(num_zero_points);++ for (size_t i = 0; i < num_zero_points; ++i) {+ const int32_t zp_signed = static_cast<int32_t>(read_u4_s1s0(zp_qau4_s1s0, i)) - 8;++ zero_points_f32[i] = -static_cast<float>(zp_signed) * rhs_scales[i];+ }++ kai_rhs_pack_nxk_qai4c32p_params params{};+ params.lhs_zero_point = 1;+ params.rhs_zero_point = 8;++ kai_run_rhs_pack_nxk_qai4c32p_qau4c32s0s1_f32_f32_f32_neon(1,+ N,+ K,+ nr,+ kr,+ sr,+ group_size,+ rhs_qau4_s0s1.data(),+ zero_points_f32.data(),+ bias_ptr,+ rhs_scales,+ rhsPackedMem->getData(),+ 0,+ ¶ms);+ }+ KernelInterface getuKernelInterface() override {+ return uKernelInterface;+ }++ KAIKernelTag getKernelTag() override {+ return KAIKernelTag::I4_NEON_IMM_GROUP_ASYM;+ }++ 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_qsi8d32pscalef32_f32_neon(BLOCK_SIZE, K, group_size, mr, kr, sr);++ return m_blocks * packedlhs_block_in_bytes;+ }++ 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 {+ const auto ukernel = std::get<kai_matmul_clamp_f32_qsi8d32p_qai4c32p_ukernel>(getuKernelInterface());++ auto* lhs = srcMem->getDataAs<float>();+ auto* dst = dstMem->getDataAs<float>();++ auto* lhs_packed = lhsPackedMem->getDataAs<int8_t>();+ auto* rhs_packed = rhsPackedMem->getDataAs<int8_t>();++ const size_t m_step = ukernel.get_m_step();+ 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_offset = ukernel.get_lhs_packed_offset(0, K, group_size);++ const size_t lhs_stride = K * sizeof(float);+ 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 + m_blk * packedlhs_block_in_bytes;++ kai_run_lhs_quant_pack_qsi8d32pscalef32_f32_neon(m_iter,+ K,+ group_size,+ mr,+ kr,+ sr,+ 0,+ lhs + m_blk * m_step * K,+ lhs_stride,+ lhs_packed_block);++ cpu_parallel->parallel_for(n_blocks, [&](size_t n_blk) {+ const size_t n_start = n_blk * n_step;+ const size_t n_iter = std::min(N - n_start, n_step);++ const size_t rhs_packed_offset = ukernel.get_rhs_packed_offset(n_start, K, group_size);++ const size_t dst_offset = ukernel.get_dst_offset(m_blk * m_step, n_start, dst_stride_row);++ const auto* lhs_ptr = static_cast<const void*>(lhs_packed_block + lhs_packed_offset);++ const auto* rhs_ptr = static_cast<const void*>(rhs_packed + rhs_packed_offset);++ auto* dst_ptr = dst + dst_offset / sizeof(float);++ ukernel.run_matmul(m_iter,+ n_iter,+ K,+ group_size,+ lhs_ptr,+ rhs_ptr,+ dst_ptr,+ dst_stride_row,+ dst_stride_col,+ FLOAT_MIN,+ FLOAT_MAX);+ });+ });+ }+};+} // namespace ov::intel_cpu::kai_commondiff --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 f21fafd566a..3e2c6248f34 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@@ -53,11 +53,21 @@ static bool useDynamicQuantizationImpl(const FCAttrs& attrs, const MemoryDescPtrreturn false;}- return weightDesc->getPrecision() == element::i8 || weightDesc->getPrecision() == element::i4;+ return weightDesc->getPrecision() == element::i8 || weightDesc->getPrecision() == element::i4 ||+ weightDesc->getPrecision() == element::u4;}bool MatMulKleidiAIExecutor::supports(const FCConfig& config) {VERIFY(hasArmISASupport(ArmISA::ASIMD), UNSUPPORTED_ISA);++ // ASYM INT4 groupwise kernel is N×K only (no K×N variant); it cannot+ // consume non-transposed weights, and scale/zero-point repacking for that+ // layout isn't implemented. Reject so another executor is selected.+ const auto& weiPrec = config.descs.at(ARG_WEI)->getPrecision();+ if (weiPrec == element::u4 && config.attrs.weightsNonTransposed) {+ return false;+ }+return config.descs.at(ARG_WEI)->getPrecision() == element::f32 ||useDynamicQuantizationImpl(config.attrs, config.descs.at(ARG_WEI));}@@ -70,6 +80,11 @@ bool MatMulKleidiAIExecutor::isGroupQuantizationEnabled(const MemoryArgs& memoryreturn (scales[1] > 1);}+bool MatMulKleidiAIExecutor::isAsymmetricQuantizationEnabled(const MemoryArgs& memory) {+ const auto zpIt = memory.find(ARG_WEI | ARG_ATTR_ZERO_POINTS);+ return zpIt != memory.end() && zpIt->second != nullptr && !zpIt->second->getDesc().empty();+}+MatMulKleidiAIExecutor::MatMulKleidiAIExecutor(const FCAttrs& attrs,const MemoryArgs& memory,const ExecutorContext::CPtr& context)@@ -99,108 +114,141 @@ MatMulKleidiAIExecutor::MatMulKleidiAIExecutor(const FCAttrs& attrs,originalWeightsDesc = std::make_shared<CpuBlockedMemoryDesc>(originalWeightsDesc->getPrecision(), Shape{wgtDims2D});auto dnnlSrcDesc = MemoryDescUtils::convertToDnnlMemoryDesc(originalWeightsDesc);- bool isTransposed = false;- float* rhs_scales = nullptr;+ kernelLookupKey = 0;- // Whether dynamic quantization is enabledconst bool useDynamicQuant = useDynamicQuantizationImpl(attrs, originalWeightsDesc);-if (!useDynamicQuant) {+ kernelLookupKey = kernelLookup::WEIGHT_FP32;+ } else {+ const auto weightPrecision = memory.at(ARG_WEI)->getDescPtr()->getPrecision();+ if (weightPrecision == element::i4 || weightPrecision == element::u4) {+ kernelLookupKey |= kernelLookup::WEIGHT_INT4;+ } else if (weightPrecision == element::i8) {+ kernelLookupKey |= kernelLookup::WEIGHT_INT8;+ } else {+ OPENVINO_THROW_NOT_IMPLEMENTED("Unsupported weight format by KleidiAI executor: ", weightPrecision);+ }++ if (hasArmISASupport(ArmISA::I8MM)) {+ kernelLookupKey |= kernelLookup::ISA_I8MM;+ } else if (hasArmISASupport(ArmISA::DOTPROD)) {+ kernelLookupKey |= kernelLookup::ISA_DOTPROD;+ } else {+ OPENVINO_THROW_NOT_IMPLEMENTED("KleidiAI quantized kernels require Arm dotprod or i8mm support.");+ }++ kernelLookupKey |= isGroupQuantizationEnabled(memory) ? kernelLookup::QUANT_GROUP : kernelLookup::QUANT_CHANNEL;+ kernelLookupKey |=+ isAsymmetricQuantizationEnabled(memory) ? kernelLookup::QUANT_ASYMMETRIC : kernelLookup::QUANT_SYMMETRIC;+ }++ const auto createPackedMemory = [&](size_t size, const element::Type& precision) {+ auto desc = std::make_shared<CpuBlockedMemoryDesc>(precision, Shape({size}));+ rhsPackedMem = std::make_shared<Memory>(context->getEngine(), desc);+ };++ const auto packQuantizedWeights = [&](bool isTransposed, const MemoryCPtr& weightsMemory) {+ createPackedMemory(_kernel->get_rhsPackedSize(), i8);+ auto* rhsScales = static_cast<float*>(memory.at(ARG_WEI | ARG_ATTR_SCALES)->getData());+ std::vector<float> transposedScales;+ if (isTransposed && isGroupQuantizationEnabled(memory)) {+ const auto numGroups = memory.at(ARG_WEI | ARG_ATTR_SCALES)->getDesc().getShape().getStaticDims()[1];+ transposedScales.resize(N * numGroups);+ for (size_t g = 0; g < numGroups; ++g) {+ for (size_t n = 0; n < N; ++n) {+ transposedScales[n * numGroups + g] = rhsScales[g * N + n];+ }+ }+ rhsScales = transposedScales.data();+ }+ _kernel->packData(isTransposed, weightsMemory, biasMem, hasBias, rhsScales, rhsPackedMem);+ };++ switch (kernelLookupKey) {+ case WEIGHT_FP32: {_kernel = std::make_shared<kai_common::uKernel<kai_common::KAIKernelTag::F32_NEON_MLA>>(N, K);auto dstDesc = originalWeightsDesc->cloneWithNewPrecision(memory.at(ARG_SRC)->getDescPtr()->getPrecision());auto dnnlDstDesc = MemoryDescUtils::convertToDnnlMemoryDesc(dstDesc);-if (!attrs.weightsNonTransposed) {dnnlDstDesc = acl_fc_executor::makeTransposedWeightDescriptor(dnnlDstDesc, dnnlSrcDesc);aclfcAttrs.isWeightsRepacked = true;}- MemoryCPtr packedWeights =- acl_fc_executor::reorderWeights(memory, context, aclfcAttrs, dnnlSrcDesc, dnnlDstDesc);- const size_t rhsPackedSize = _kernel->get_rhsPackedSize();- auto rhsPackedDesc = std::make_shared<CpuBlockedMemoryDesc>(u8, Shape({rhsPackedSize}));- rhsPackedMem = std::make_shared<Memory>(context->getEngine(), rhsPackedDesc);+ const MemoryCPtr packedWeights =+ acl_fc_executor::reorderWeights(memory, context, aclfcAttrs, dnnlSrcDesc, dnnlDstDesc);+ createPackedMemory(_kernel->get_rhsPackedSize(), u8);_kernel->packData(false, packedWeights, biasMem, hasBias, nullptr, rhsPackedMem);- } else {- MemoryPtr weightsMemory = memory.at(ARG_WEI);- isTransposed = attrs.weightsNonTransposed;- // Check if weights are in int4 or int8- if (weightsMemory->getDescPtr()->getPrecision() == element::i4) {- if (isGroupQuantizationEnabled(memory)) {- if (hasArmISASupport(ArmISA::I8MM)) {- _kernel =- std::make_shared<kai_common::uKernel<kai_common::KAIKernelTag::I4_NEON_IMM_GROUP>>(N,- K,- lhsPackedMem,- memory);- } else {- _kernel = std::make_shared<kai_common::uKernel<kai_common::KAIKernelTag::I4_NEON_DOTPROD_GROUP>>(- N,- K,- lhsPackedMem,- memory);- }- } else {- if (hasArmISASupport(ArmISA::I8MM)) {- _kernel =- std::make_shared<kai_common::uKernel<kai_common::KAIKernelTag::I4_NEON_IMM>>(N,+ break;+ }++ case WEIGHT_INT4 | ISA_I8MM | QUANT_GROUP | QUANT_SYMMETRIC:+ _kernel = std::make_shared<kai_common::uKernel<kai_common::KAIKernelTag::I4_NEON_IMM_GROUP>>(N,K,- lhsPackedMem);- } else {- _kernel =- std::make_shared<kai_common::uKernel<kai_common::KAIKernelTag::I4_NEON_DOTPROD>>(N,+ lhsPackedMem,+ memory);+ packQuantizedWeights(attrs.weightsNonTransposed, memory.at(ARG_WEI));+ break;++ case WEIGHT_INT4 | ISA_DOTPROD | QUANT_GROUP | QUANT_SYMMETRIC:+ _kernel = std::make_shared<kai_common::uKernel<kai_common::KAIKernelTag::I4_NEON_DOTPROD_GROUP>>(N,K,- lhsPackedMem);- }- }- const size_t rhsPackedSize = _kernel->get_rhsPackedSize();- auto rhsPackedDesc = std::make_shared<CpuBlockedMemoryDesc>(i8, Shape({rhsPackedSize}));- rhsPackedMem = std::make_shared<Memory>(context->getEngine(), rhsPackedDesc);+ lhsPackedMem,+ memory);+ packQuantizedWeights(attrs.weightsNonTransposed, memory.at(ARG_WEI));+ break;++ case WEIGHT_INT4 | ISA_I8MM | QUANT_GROUP | QUANT_ASYMMETRIC:+ _kernel = std::make_shared<kai_common::uKernel<kai_common::KAIKernelTag::I4_NEON_IMM_GROUP_ASYM>>(N,+ K,+ lhsPackedMem,+ memory);+ packQuantizedWeights(attrs.weightsNonTransposed, memory.at(ARG_WEI));+ break;++ case WEIGHT_INT4 | ISA_DOTPROD | QUANT_GROUP | QUANT_ASYMMETRIC:+ _kernel =+ std::make_shared<kai_common::uKernel<kai_common::KAIKernelTag::I4_NEON_DOTPROD_GROUP_ASYM>>(N,+ K,+ lhsPackedMem,+ memory);+ packQuantizedWeights(attrs.weightsNonTransposed, memory.at(ARG_WEI));+ break;++ case WEIGHT_INT4 | ISA_I8MM | QUANT_CHANNEL | QUANT_SYMMETRIC:+ _kernel = std::make_shared<kai_common::uKernel<kai_common::KAIKernelTag::I4_NEON_IMM>>(N, K, lhsPackedMem);+ packQuantizedWeights(attrs.weightsNonTransposed, memory.at(ARG_WEI));+ break;++ case WEIGHT_INT4 | ISA_DOTPROD | QUANT_CHANNEL | QUANT_SYMMETRIC:+ _kernel = std::make_shared<kai_common::uKernel<kai_common::KAIKernelTag::I4_NEON_DOTPROD>>(N, K, lhsPackedMem);+ packQuantizedWeights(attrs.weightsNonTransposed, memory.at(ARG_WEI));+ break;++ case WEIGHT_INT8 | ISA_DOTPROD | QUANT_CHANNEL | QUANT_SYMMETRIC:+ 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);} else {- if (hasArmISASupport(ArmISA::I8MM)) {- _kernel =- std::make_shared<kai_common::uKernel<kai_common::KAIKernelTag::I8_NEON_IMM>>(N, K, lhsPackedMem);- } else {- _kernel =- std::make_shared<kai_common::uKernel<kai_common::KAIKernelTag::I8_NEON_DOTPROD>>(N,- K,- lhsPackedMem);- }-- if (!attrs.weightsNonTransposed) {- auto dnnlSrcDesc = MemoryDescUtils::convertToDnnlMemoryDesc(originalWeightsDesc);- auto dnnlDstDesc = acl_fc_executor::makeTransposedWeightDescriptor(dnnlSrcDesc, dnnlSrcDesc);- weightsMemory = acl_fc_executor::reorderData(dnnlSrcDesc, dnnlDstDesc, memory.at(ARG_WEI), context);- }- const size_t rhsPackedSize = _kernel->get_rhsPackedSize();- auto rhsPackedDesc = std::make_shared<CpuBlockedMemoryDesc>(i8, Shape({rhsPackedSize}));- rhsPackedMem = std::make_shared<Memory>(context->getEngine(), rhsPackedDesc);+ _kernel =+ std::make_shared<kai_common::uKernel<kai_common::KAIKernelTag::I8_NEON_DOTPROD>>(N, K, lhsPackedMem);}- rhs_scales = static_cast<float*>(memory.at(ARG_WEI | ARG_ATTR_SCALES)->getData());-- std::vector<float> transposedScales;- // When the weight's Transpose was elided by the graph optimizer (weightsNonTransposed==true,- // see FuseFCAndTransposeOnWeights), the per-group decompression scales feeding this FC were- // transposed together with the weights (ConvertFullyConnectedToFullyConnectedCompressed::- // process_compressed_weights) and had that Transpose elided the same way. Their physical layout- // is therefore still [numGroups, N] instead of the canonical [N, numGroups] the KAI group- // kernels expect, so it must be repacked here before use.- if (isTransposed && isGroupQuantizationEnabled(memory)) {- const auto numGroups = memory.at(ARG_WEI | ARG_ATTR_SCALES)->getDesc().getShape().getStaticDims()[1];- transposedScales.resize(N * numGroups);- for (size_t g = 0; g < numGroups; ++g) {- for (size_t n = 0; n < N; ++n) {- transposedScales[n * numGroups + g] = rhs_scales[g * N + n];- }- }- rhs_scales = transposedScales.data();+ MemoryPtr weightsMemory = memory.at(ARG_WEI);+ if (!attrs.weightsNonTransposed) {+ const auto srcDesc = MemoryDescUtils::convertToDnnlMemoryDesc(originalWeightsDesc);+ const auto dstDesc = acl_fc_executor::makeTransposedWeightDescriptor(srcDesc, srcDesc);+ weightsMemory = acl_fc_executor::reorderData(srcDesc, dstDesc, memory.at(ARG_WEI), context);}+ packQuantizedWeights(false, weightsMemory);+ break;+ }- _kernel->packData(isTransposed, weightsMemory, biasMem, hasBias, rhs_scales, rhsPackedMem);+ default:+ OPENVINO_THROW_NOT_IMPLEMENTED("Unsupported KleidiAI kernel configuration. Lookup key: ", kernelLookupKey);}- // Create scratchpad to initialize memory for LHS in update()+scratchPad = context->getScratchPad();}diff --git a/src/plugins/intel_cpu/src/nodes/executors/kleidiai/kleidiai_mm.hpp b/src/plugins/intel_cpu/src/nodes/executors/kleidiai/kleidiai_mm.hppindex f72ebbb4041..805cfb261b3 100644--- a/src/plugins/intel_cpu/src/nodes/executors/kleidiai/kleidiai_mm.hpp+++ b/src/plugins/intel_cpu/src/nodes/executors/kleidiai/kleidiai_mm.hpp@@ -33,6 +33,18 @@ public:void setKaiExecutorImplAsGatherMatmul();void set_gather_idx(const std::vector<std::pair<int32_t, int32_t>>& idxMap);+ enum kernelLookup : std::uint16_t {+ WEIGHT_FP32 = 1,+ WEIGHT_INT8 = 1 << 2,+ WEIGHT_INT4 = 1 << 3,+ ISA_DOTPROD = 1 << 4,+ ISA_I8MM = 1 << 5,+ QUANT_CHANNEL = 1 << 6,+ QUANT_GROUP = 1 << 7,+ QUANT_SYMMETRIC = 1 << 8,+ QUANT_ASYMMETRIC = 1 << 9+ };+private:static bool isGroupQuantizationEnabled(const MemoryArgs& memory);// IMPL_TYPE :: Default@@ -40,6 +52,7 @@ private:// IMPL_TYPE :: GatherMatmul// [B, M, K] -> gather -> [M', K] * [N', K] -> scatter -> [B, N, K]enum class IMPL_TYPE : uint8_t { Default, GatherMatmul };+ static bool isAsymmetricQuantizationEnabled(const MemoryArgs& memory);DnnlScratchPadPtr scratchPad;IMPL_TYPE KaiExecutorImpl = IMPL_TYPE::Default;std::vector<std::pair<int32_t, int32_t>> gather_idx;@@ -53,6 +66,7 @@ private:size_t M = 0UL, N = 0UL, K = 0UL;ExecutorContext::CPtr executorContext;std::shared_ptr<kai_common::uKernelBase> _kernel;+ size_t kernelLookupKey = 0;};using MatMulKleidiAIExecutorPtr = std::shared_ptr<MatMulKleidiAIExecutor>;diff --git a/src/plugins/intel_cpu/src/nodes/fullyconnected.cpp b/src/plugins/intel_cpu/src/nodes/fullyconnected.cppindex a6f15ad2fc3..8887a65ec2f 100644--- a/src/plugins/intel_cpu/src/nodes/fullyconnected.cpp+++ b/src/plugins/intel_cpu/src/nodes/fullyconnected.cpp@@ -78,7 +78,9 @@ ov::element::TypeVector FullyConnected::getSupportedCompressedWeightsTypes([[may}return supportedDataTypes;#elif defined(OV_CPU_WITH_KLEIDIAI)- return {Type_t::i8, Type_t::i4};+ // Symmetric weight-only compression uses i4. Asymmetric groupwise+ // compression exported by Optimum uses u4 weights plus u4 zero-points.+ return {Type_t::i8, Type_t::i4, Type_t::u4};#elsereturn {};#endif@@ -195,17 +197,36 @@ bool FullyConnected::isSupportedCompressedOperation([[maybe_unused]] const std::return false;}- bool isNotGroupWise = (IC % G != 0 || IC / G < 4 || OC == 1 || (IC / G) % 32 != 0);- bool isNotChannelWise =- (op->get_input_size() > WEIGHT_SCALES && shape_size(op->input(WEIGHT_SCALES).get_shape()) != OC);- if (isNotChannelWise && isNotGroupWise) {+ const auto scalesShape = op->input(WEIGHT_SCALES).get_shape();+ const bool isChannelWise = shape_size(scalesShape) == OC;+ const bool hasValidGroupGeometry = IC % G == 0 && IC / G >= 4 && OC != 1 && (IC / G) % 32 == 0;+ const bool isGroupWise = !isChannelWise && hasValidGroupGeometry;++ if (!isChannelWise && !isGroupWise) {return false;}- if (op->get_input_size() > WEIGHT_ZERO_POINTS &&- op->input(WEIGHT_ZERO_POINTS).get_element_type() != ov::element::dynamic) {+ const auto weightsType = op->input(WEIGHTS).get_element_type();+ const bool hasWeightZeroPoints = op->get_input_size() > WEIGHT_ZERO_POINTS &&+ op->input(WEIGHT_ZERO_POINTS).get_element_type() != ov::element::dynamic;++ if (weightsType == ov::element::u4 && !hasWeightZeroPoints) {return false;}++ if (hasWeightZeroPoints) {+ const auto zeroPointsType = op->input(WEIGHT_ZERO_POINTS).get_element_type();+ // KleidiAI supports asymmetric INT4 only for group-wise u4.+ if (weightsType != ov::element::u4 || zeroPointsType != ov::element::u4 || !isGroupWise) {+ return false;+ }++ const auto zeroPointsShape = op->input(WEIGHT_ZERO_POINTS).get_shape();++ if (zeroPointsShape != scalesShape) {+ return false;+ }+ }} catch (...) {return false;}@@ -577,7 +598,6 @@ void FullyConnected::initSupportedPrimitiveDescriptors() {attrs.modelType = context->getConfig().modelType;attrs.dqScales = getDQScales();-attrs.postOps = getPostOps(fusedWith);const auto& srcTypes = getOriginalInputPrecisions();diff --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 93c9e77e592..410588bd8b6 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@@ -91,6 +91,27 @@ INSTANTIATE_TEST_SUITE_P(smoke_MatMulCompressedWeightsGrp_Kleidiai,::testing::Values(true)),MatmulWeightsDecompression::getTestCaseName);+const std::vector<MatMulDecompressionShapeParams> input_shapes_with_groups = {+ {{{}, {{1, 7, 256}}}, {256, 128}, 32lu}, // group_size = 32+ { {{-1, -1, -1}, {{10, 40, 64}, {11, 40, 64}}}, // data_shape+ {64, 128}, // weights_shape+ 32lu}, // group_size = 16+};++INSTANTIATE_TEST_SUITE_P(smoke_MatMulCompressedWeightsGrp_Kleidiai_asym,+ MatmulWeightsDecompression,+ ::testing::Combine(::testing::ValuesIn(input_shapes_with_groups),+ ::testing::Values(ov::element::u4),+ ::testing::ValuesIn(decompression_precisions),+ ::testing::Values(ov::element::dynamic),+ ::testing::Values(true),+ ::testing::Values(DecompressionType::full), // group-wise requires full+ ::testing::Values(DecompressionType::full), // asymmetric with zero-point+ ::testing::Values(false),+ ::testing::Values(enable_dyn_quant_config_kleidiai),+ ::testing::ValuesIn(fusing_params),+ ::testing::Values(true)), // should_use_decompression_impl() can also be passed+ MatmulWeightsDecompression::getTestCaseName);const std::vector<ov::test::ElementType> weights_precisions = {ov::element::u8, ov::element::i8};