Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[CPU] Enable dnnl matmul primitive executor for FullyConnected node (#25416)

Egor Duplenskiif3546c721847
diff --git a/src/plugins/intel_cpu/src/nodes/executors/dnnl/dnnl_convolution_primitive.cpp b/src/plugins/intel_cpu/src/nodes/executors/dnnl/dnnl_convolution_primitive.cpp
index dac4fc044c7..8f9d7ad0805 100644
--- a/src/plugins/intel_cpu/src/nodes/executors/dnnl/dnnl_convolution_primitive.cpp
+++ b/src/plugins/intel_cpu/src/nodes/executors/dnnl/dnnl_convolution_primitive.cpp
@@ -14,6 +14,7 @@
#include "memory_desc/dnnl_memory_desc.h"
#include "nodes/executors/convolution_config.hpp"
#include "nodes/executors/dnnl/dnnl_aliases.hpp"
+#include "nodes/executors/dnnl/dnnl_fullyconnected_primitive.hpp"
#include "nodes/executors/dnnl/dnnl_shape_agnostic_data.hpp"
#include "nodes/executors/executor.hpp"
#include "nodes/executors/fullyconnected_config.hpp"
@@ -90,12 +91,12 @@ static dnnl::convolution_forward::primitive_desc createDescriptorInternal(const
// @todo create general mapping from node configuration to backend configuration
static const std::map<memory::data_type, memory::data_type> weightsTypeByInputType{
- // input data type weights data type
- {memory::data_type::f32, memory::data_type::f32 },
- {memory::data_type::f16, memory::data_type::f16 },
+ // input data type weights data type
+ {memory::data_type::f32, memory::data_type::f32},
+ {memory::data_type::f16, memory::data_type::f16},
{memory::data_type::bf16, memory::data_type::bf16},
- {memory::data_type::u8, memory::data_type::s8 },
- {memory::data_type::s8, memory::data_type::s8 },
+ {memory::data_type::u8, memory::data_type::s8},
+ {memory::data_type::s8, memory::data_type::s8},
};
// make a fake shape: OC, IC, 1
@@ -156,15 +157,8 @@ static DnnlPrimitiveAttrs createPrimitiveAttrs(const ConvAttrs& attrs,
one_of(srcDesc->getPrecision(), ov::element::u8, ov::element::i8) && weiDesc->getPrecision() == ov::element::i8;
auto outputDataType = DnnlExtensionUtils::ElementTypeToDataType(dstDesc->getPrecision());
- DnnlPostOpsComposer dnnlpoc(postOps,
- context->getEngine(),
- dims,
- 1,
- isINT8,
- 1 << 0,
- {},
- attrs.withBias,
- outputDataType);
+ DnnlPostOpsComposer
+ dnnlpoc(postOps, context->getEngine(), dims, 1, isINT8, 1 << 0, {}, attrs.withBias, outputDataType);
return dnnlpoc.compose();
}
@@ -210,6 +204,12 @@ std::shared_ptr<DnnlConvolutionPrimitive> DnnlConvolutionPrimitive::create(
return primitive;
}
+DnnlMemoryDescPtr DnnlConvolutionPrimitive::makeTransposedWeightDescriptor(const DnnlMemoryDescPtr srcDesc,
+ const DnnlMemoryDescPtr dstDesc,
+ bool weightsNonTransposed) {
+ return DnnlFCPrimitive::makeTransposedWeightDescriptor(srcDesc, dstDesc, weightsNonTransposed);
+}
+
DnnlConvolutionPrimitive::DnnlConvolutionPrimitive(const Key& key,
const dnnl::engine& engine,
const std::vector<impl_desc_type>& implPriorities)
diff --git a/src/plugins/intel_cpu/src/nodes/executors/dnnl/dnnl_convolution_primitive.hpp b/src/plugins/intel_cpu/src/nodes/executors/dnnl/dnnl_convolution_primitive.hpp
index 4fdd83abc8a..79501782922 100644
--- a/src/plugins/intel_cpu/src/nodes/executors/dnnl/dnnl_convolution_primitive.hpp
+++ b/src/plugins/intel_cpu/src/nodes/executors/dnnl/dnnl_convolution_primitive.hpp
@@ -61,6 +61,10 @@ public:
return m_implType;
}
+ static DnnlMemoryDescPtr makeTransposedWeightDescriptor(const DnnlMemoryDescPtr srcDesc,
+ const DnnlMemoryDescPtr dstDesc,
+ bool weightsNonTransposed);
+
// create shape agnostic data using FC attributes (1x1 Convolution as FC executor)
static DnnlShapeAgnosticDataPtr createShapeAgnosticData(const FCAttrs& attrs,
const PostOps& postOps,
diff --git a/src/plugins/intel_cpu/src/nodes/executors/dnnl/dnnl_fullyconnected.hpp b/src/plugins/intel_cpu/src/nodes/executors/dnnl/dnnl_fullyconnected.hpp
index c63de3e2872..95ba35cb8f3 100644
--- a/src/plugins/intel_cpu/src/nodes/executors/dnnl/dnnl_fullyconnected.hpp
+++ b/src/plugins/intel_cpu/src/nodes/executors/dnnl/dnnl_fullyconnected.hpp
@@ -123,8 +123,7 @@ private:
if (currentPrimitive && currentPrimitive->weightsDesc()->isCompatible(*newPrimMemDesc))
return;
- if (m_attrs.weightsNonTransposed)
- originalMemDesc = utils::makeTransposedWeightDescriptor(originalMemDesc, newPrimMemDesc);
+ originalMemDesc = Primitive::makeTransposedWeightDescriptor(originalMemDesc, newPrimMemDesc, m_attrs.weightsNonTransposed);
const auto weiMemory = utils::prepareWeightsMemory(originalMemDesc, newPrimMemDesc, memory, m_context, true);
m_primArgs[DNNL_ARG_WEIGHTS] = weiMemory->getPrimitive();
diff --git a/src/plugins/intel_cpu/src/nodes/executors/dnnl/dnnl_fullyconnected_primitive.cpp b/src/plugins/intel_cpu/src/nodes/executors/dnnl/dnnl_fullyconnected_primitive.cpp
index 4b8716f03aa..fcb70d4753b 100644
--- a/src/plugins/intel_cpu/src/nodes/executors/dnnl/dnnl_fullyconnected_primitive.cpp
+++ b/src/plugins/intel_cpu/src/nodes/executors/dnnl/dnnl_fullyconnected_primitive.cpp
@@ -9,7 +9,6 @@
#include <common/primitive_attr.hpp>
#include <common/primitive_desc_iface.hpp>
#include <common/primitive_iface.hpp>
-#include <iostream>
#include <memory>
#include <oneapi/dnnl/dnnl.hpp>
#include <oneapi/dnnl/dnnl_common.hpp>
@@ -24,6 +23,7 @@
#include "memory_desc/dnnl_blocked_memory_desc.h"
#include "memory_desc/dnnl_memory_desc.h"
#include "nodes/executors/dnnl/dnnl_shape_agnostic_data.hpp"
+#include "nodes/executors/dnnl/dnnl_utils.hpp"
#include "nodes/executors/executor.hpp"
#include "nodes/executors/fullyconnected_config.hpp"
#include "nodes/executors/memory_arguments.hpp"
@@ -51,7 +51,6 @@ size_t DnnlFCPrimitive::Key::hash() const {
seed = hash_combine(seed, get_attr_hash(*attr.get()));
seed = hash_combine(seed, sparseWeights);
- seed = hash_combine(seed, transposedWeights);
seed = hash_combine(seed, modelType);
return seed;
@@ -73,8 +72,9 @@ bool DnnlFCPrimitive::Key::operator==(const Key& rhs) const {
result = result && dst && rhs.dst && dst->getDnnlDesc() == rhs.dst->getDnnlDesc();
}
- result = result && *attr.get() == *rhs.attr.get() && sparseWeights == rhs.sparseWeights &&
- transposedWeights == rhs.transposedWeights && modelType == rhs.modelType;
+ result = result && *attr.get() == *rhs.attr.get() &&
+ sparseWeights == rhs.sparseWeights &&
+ modelType == rhs.modelType;
return result;
}
@@ -88,16 +88,13 @@ std::shared_ptr<DnnlFCPrimitive> DnnlFCPrimitive::create(const MemoryArgs& memor
const auto& biaDesc = MemoryDescUtils::convertToDnnlMemoryDesc(memory.at(ARG_BIAS)->getDescPtr());
const auto& dstDesc = MemoryDescUtils::convertToDnnlMemoryDesc(memory.at(ARG_DST)->getDescPtr());
- Key dnnlFCKey{
- srcDesc,
- weiDesc,
- biaDesc,
- dstDesc,
- shapeAgnosticData->primAttrs.attr,
- attrs.sparseWeights,
- attrs.weightsNonTransposed,
- attrs.modelType
- };
+ Key dnnlFCKey{srcDesc,
+ weiDesc,
+ biaDesc,
+ dstDesc,
+ shapeAgnosticData->primAttrs.attr,
+ attrs.sparseWeights,
+ attrs.modelType};
auto builder = [&context](const Key& dnnlKey) {
return std::make_shared<DnnlFCPrimitive>(dnnlKey, context->getEngine(), context->getImplPriorities());
@@ -111,6 +108,20 @@ std::shared_ptr<DnnlFCPrimitive> DnnlFCPrimitive::create(const MemoryArgs& memor
return primitive;
}
+DnnlMemoryDescPtr DnnlFCPrimitive::makeTransposedWeightDescriptor(const DnnlMemoryDescPtr srcDesc,
+ const DnnlMemoryDescPtr dstDesc,
+ bool weightsNonTransposed) {
+ if (!weightsNonTransposed)
+ return srcDesc;
+
+ const auto& weiDesc = srcDesc->getDnnlDesc();
+ const auto reorderedWeiDesc =
+ dnnl::memory::desc{weiDesc.get_dims(), weiDesc.get_data_type(), dnnl::memory::format_tag::ba};
+ const auto transposedWeiDesc = reorderedWeiDesc.reshape(dstDesc->getDnnlDesc().get_dims());
+
+ return DnnlExtensionUtils::makeDescriptor(transposedWeiDesc);
+}
+
bool DnnlFCPrimitive::useWeightsDecompressionImpl(const ov::element::Type inputType,
const ov::element::Type weightsType,
const ov::intel_cpu::Config::ModelType modelType) {
@@ -129,8 +140,12 @@ bool DnnlFCPrimitive::useWeightsDecompressionImpl(const ov::element::Type inputT
return false;
}
-bool DnnlFCPrimitive::useDynamicQuantizationImpl(size_t dqGroupSize, const MemoryDescPtr srcDesc, const MemoryDescPtr weightsDesc,
- MemoryCPtr scalesPtr, MemoryCPtr zpPtr, bool needTranspose) {
+bool DnnlFCPrimitive::useDynamicQuantizationImpl(size_t dqGroupSize,
+ const MemoryDescPtr srcDesc,
+ const MemoryDescPtr weightsDesc,
+ MemoryCPtr scalesPtr,
+ MemoryCPtr zpPtr,
+ bool needTranspose) {
if (dqGroupSize == 0)
return false;
@@ -232,7 +247,9 @@ static DnnlPrimitiveAttrs createPrimitiveAttrs(const FCAttrs& attrs,
uint8_t zp_value = (wei_precision == ov::element::i8) ? 128 : 8;
DnnlBlockedMemoryDesc zpMemoryDesc(ov::element::u8, Shape({1}));
auto decompressionSubtractPtr = std::make_shared<Memory>(context->getEngine(), zpMemoryDesc, &zp_value);
- dnnlpoc.appendDecompressionZeroPoints(decompressionSubtractPtr, !attrs.weightsNonTransposed, ov::element::u8);
+ dnnlpoc.appendDecompressionZeroPoints(decompressionSubtractPtr,
+ !attrs.weightsNonTransposed,
+ ov::element::u8);
}
dnnlpoc.setDynamicQuantizationParams(attrs.dynamicQuantizationGroupSize);
}
@@ -364,10 +381,15 @@ DnnlShapeAgnosticDataPtr DnnlFCPrimitive::createShapeAgnosticData(const FCAttrs&
const auto& biasDesc = memory.at(ARG_BIAS)->getDescPtr();
auto dstDesc = memory.at(ARG_DST)->getDescPtr();
- const auto useWeightsDecompression = useWeightsDecompressionImpl(srcDesc->getPrecision(), weiDesc->getPrecision(), attrs.modelType);
- const auto useDynamicQuantization = useWeightsDecompression &&
- useDynamicQuantizationImpl(attrs.dynamicQuantizationGroupSize, srcDesc, weiDesc,
- attrs.decompressionMultiplyPtr, attrs.decompressionSubtractPtr, !attrs.weightsNonTransposed);
+ const auto useWeightsDecompression =
+ useWeightsDecompressionImpl(srcDesc->getPrecision(), weiDesc->getPrecision(), attrs.modelType);
+ const auto useDynamicQuantization =
+ useWeightsDecompression && useDynamicQuantizationImpl(attrs.dynamicQuantizationGroupSize,
+ srcDesc,
+ weiDesc,
+ attrs.decompressionMultiplyPtr,
+ attrs.decompressionSubtractPtr,
+ !attrs.weightsNonTransposed);
const auto postOpData = createPrimitiveAttrs(attrs, postOps, memory, context, useDynamicQuantization);
@@ -402,8 +424,8 @@ DnnlShapeAgnosticDataPtr DnnlFCPrimitive::createShapeAgnosticData(const FCAttrs&
const auto weightsDesc = DnnlExtensionUtils::makeDescriptor(primDesc.weights_desc());
auto originalWeightsDesc = MemoryDescUtils::convertToDnnlMemoryDesc(weiDesc);
- if (attrs.weightsNonTransposed)
- originalWeightsDesc = utils::makeTransposedWeightDescriptor(originalWeightsDesc, weightsDesc);
+
+ originalWeightsDesc = makeTransposedWeightDescriptor(originalWeightsDesc, weightsDesc, attrs.weightsNonTransposed);
// ignore the result since we just need to put the packed weights into the cache
(void)utils::prepareWeightsMemory(originalWeightsDesc,
@@ -429,15 +451,16 @@ DnnlFCPrimitive::DnnlFCPrimitive(const Key& key,
const dnnl::engine& engine,
const std::vector<impl_desc_type>& implPriorities)
: m_stream(dnnl::stream(engine)),
- m_primDesc(createPrimitiveDesc(key.src->getDnnlDesc(),
- key.wei->getDnnlDesc(),
- key.bias->getDnnlDesc(),
- key.dst->getDnnlDesc(),
- key.attr,
- engine,
- implPriorities,
- key.sparseWeights,
- useWeightsDecompressionImpl(key.src->getPrecision(), key.wei->getPrecision(), key.modelType))),
+ m_primDesc(createPrimitiveDesc(
+ key.src->getDnnlDesc(),
+ key.wei->getDnnlDesc(),
+ key.bias->getDnnlDesc(),
+ key.dst->getDnnlDesc(),
+ key.attr,
+ engine,
+ implPriorities,
+ key.sparseWeights,
+ useWeightsDecompressionImpl(key.src->getPrecision(), key.wei->getPrecision(), key.modelType))),
m_implType(implTypeFromPrimDesc(m_primDesc)),
m_srcDesc(DnnlExtensionUtils::makeDescriptor(m_primDesc.src_desc())),
m_weiDesc(DnnlExtensionUtils::makeDescriptor(m_primDesc.weights_desc())),
diff --git a/src/plugins/intel_cpu/src/nodes/executors/dnnl/dnnl_fullyconnected_primitive.hpp b/src/plugins/intel_cpu/src/nodes/executors/dnnl/dnnl_fullyconnected_primitive.hpp
index fce2a9b29a6..5295b965506 100644
--- a/src/plugins/intel_cpu/src/nodes/executors/dnnl/dnnl_fullyconnected_primitive.hpp
+++ b/src/plugins/intel_cpu/src/nodes/executors/dnnl/dnnl_fullyconnected_primitive.hpp
@@ -10,7 +10,6 @@
#include "cpu_memory.h"
#include "memory_desc/dnnl_memory_desc.h"
#include "nodes/executors/dnnl/dnnl_shape_agnostic_data.hpp"
-#include "nodes/executors/dnnl/dnnl_utils.hpp"
#include "nodes/executors/executor.hpp"
#include "nodes/executors/fullyconnected_config.hpp"
@@ -25,7 +24,6 @@ class DnnlFCPrimitive {
DnnlMemoryDescCPtr dst;
dnnl::primitive_attr attr;
bool sparseWeights;
- bool transposedWeights;
Config::ModelType modelType;
size_t hash() const;
@@ -63,7 +61,13 @@ public:
const ExecutorContext::CPtr context,
const bool cacheWeights);
- static bool useWeightsDecompressionImpl(const ov::element::Type inputType, const ov::element::Type weightsType, const Config::ModelType modelType);
+ static bool useWeightsDecompressionImpl(const ov::element::Type inputType,
+ const ov::element::Type weightsType,
+ const Config::ModelType modelType);
+
+ static DnnlMemoryDescPtr makeTransposedWeightDescriptor(const DnnlMemoryDescPtr srcDesc,
+ const DnnlMemoryDescPtr dstDesc,
+ bool weightsNonTransposed);
static std::shared_ptr<DnnlFCPrimitive> create(const MemoryArgs& memory,
const FCAttrs& attrs,
@@ -71,8 +75,12 @@ public:
const DnnlShapeAgnosticDataPtr& shapeAgnosticData);
private:
- static bool useDynamicQuantizationImpl(size_t dqGroupSize, const MemoryDescPtr srcDesc, const MemoryDescPtr weightsDesc,
- MemoryCPtr scalesPtr, MemoryCPtr zpPtr, bool needTranspose);
+ static bool useDynamicQuantizationImpl(size_t dqGroupSize,
+ const MemoryDescPtr srcDesc,
+ const MemoryDescPtr weightsDesc,
+ MemoryCPtr scalesPtr,
+ MemoryCPtr zpPtr,
+ bool needTranspose);
dnnl::stream m_stream;
dnnl::primitive_desc m_primDesc;
diff --git a/src/plugins/intel_cpu/src/nodes/executors/dnnl/dnnl_matmul_primitive.cpp b/src/plugins/intel_cpu/src/nodes/executors/dnnl/dnnl_matmul_primitive.cpp
new file mode 100644
index 00000000000..1b8646c858e
--- /dev/null
+++ b/src/plugins/intel_cpu/src/nodes/executors/dnnl/dnnl_matmul_primitive.cpp
@@ -0,0 +1,343 @@
+// Copyright (C) 2023 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "nodes/executors/dnnl/dnnl_matmul_primitive.hpp"
+
+#include <oneapi/dnnl/dnnl_types.h>
+
+#include <algorithm>
+#include <common/primitive_attr.hpp>
+#include <common/primitive_desc_iface.hpp>
+#include <common/primitive_iface.hpp>
+#include <memory>
+#include <oneapi/dnnl/dnnl.hpp>
+#include <oneapi/dnnl/dnnl_common.hpp>
+
+#include "cpu_memory.h"
+#include "cpu_types.h"
+#include "dnnl_extension_utils.h"
+#include "dnnl_postops_composer.h"
+#include "dnnl_utils.hpp"
+#include "memory_desc/cpu_memory_desc.h"
+#include "memory_desc/cpu_memory_desc_utils.h"
+#include "memory_desc/dnnl_memory_desc.h"
+#include "nodes/executors/dnnl/dnnl_shape_agnostic_data.hpp"
+#include "nodes/executors/executor.hpp"
+#include "nodes/executors/fullyconnected_config.hpp"
+#include "nodes/executors/matmul_config.hpp"
+#include "nodes/executors/memory_arguments.hpp"
+#include "utils/debug_capabilities.h"
+
+namespace ov {
+namespace intel_cpu {
+
+using namespace dnnl;
+using namespace ov::element;
+using namespace executor;
+
+// @todo rewrite using hash_builder
+size_t DnnlMatMulPrimitive::Key::hash() const {
+ using namespace dnnl::impl;
+ using namespace dnnl::impl::primitive_hashing;
+
+ size_t seed = 0;
+
+ for (const auto& ptr : {src, wei, bias, dst}) {
+ if (ptr) {
+ seed = hash_combine(seed, get_md_hash(*ptr->getDnnlDesc().get()));
+ }
+ }
+
+ seed = hash_combine(seed, get_attr_hash(*attr.get()));
+
+ return seed;
+}
+
+bool DnnlMatMulPrimitive::Key::operator==(const Key& rhs) const {
+ bool result = true;
+
+ if (src != rhs.src) {
+ result = result && src && rhs.src && src->getDnnlDesc() == rhs.src->getDnnlDesc();
+ }
+ if (wei != rhs.wei) {
+ result = result && wei && rhs.wei && wei->getDnnlDesc() == rhs.wei->getDnnlDesc();
+ }
+ if (bias != rhs.bias) {
+ result = result && bias && rhs.bias && bias->getDnnlDesc() == rhs.bias->getDnnlDesc();
+ }
+ if (dst != rhs.dst) {
+ result = result && dst && rhs.dst && dst->getDnnlDesc() == rhs.dst->getDnnlDesc();
+ }
+
+ result = result && *attr.get() == *rhs.attr.get();
+
+ return result;
+}
+
+std::shared_ptr<DnnlMatMulPrimitive> DnnlMatMulPrimitive::create(const MemoryArgs& memory,
+ const MatMulAttrs& attrs,
+ const ExecutorContext::CPtr context,
+ const DnnlShapeAgnosticDataPtr& shapeAgnosticData) {
+ const auto& srcDesc = MemoryDescUtils::convertToDnnlMemoryDesc(memory.at(ARG_SRC)->getDescPtr());
+ const auto& weiDesc = MemoryDescUtils::convertToDnnlMemoryDesc(memory.at(ARG_WEI)->getDescPtr());
+ const auto& biaDesc = MemoryDescUtils::convertToDnnlMemoryDesc(memory.at(ARG_BIAS)->getDescPtr());
+ const auto& dstDesc = MemoryDescUtils::convertToDnnlMemoryDesc(memory.at(ARG_DST)->getDescPtr());
+
+ Key dnnlMatMulKey{srcDesc, weiDesc, biaDesc, dstDesc, shapeAgnosticData->primAttrs.attr};
+
+ auto builder = [&context](const Key& dnnlKey) {
+ return std::make_shared<DnnlMatMulPrimitive>(dnnlKey, context->getEngine(), context->getImplPriorities());
+ };
+
+ auto runtimeCache = context->getRuntimeCache();
+ const auto result = runtimeCache->getOrCreate(dnnlMatMulKey, builder);
+ const auto& primitive = result.first;
+ assert(primitive);
+
+ return primitive;
+}
+
+DnnlMemoryDescPtr DnnlMatMulPrimitive::makeTransposedWeightDescriptor(const DnnlMemoryDescPtr srcDesc,
+ const DnnlMemoryDescPtr dstDesc,
+ bool weightsNonTransposed) {
+ const auto& weiDesc = srcDesc->getDnnlDesc();
+ auto wDims = weiDesc.get_dims();
+ auto wDataType = weiDesc.get_data_type();
+ std::swap(wDims[wDims.size() - 1], wDims[wDims.size() - 2]);
+
+ const auto format = weightsNonTransposed ? dnnl::memory::format_tag::ab : dnnl::memory::format_tag::ba;
+ const auto transposedWeiDesc = dnnl::memory::desc{wDims, wDataType, format};
+
+ return DnnlExtensionUtils::makeDescriptor(transposedWeiDesc);
+}
+
+static DnnlPrimitiveAttrs createPrimitiveAttrs(const MatMulAttrs& attrs,
+ const PostOps& postOps,
+ const MemoryArgs& memory,
+ ExecutorContext::CPtr context,
+ bool useDynamicQuantization) {
+ const auto& srcDesc = memory.at(ARG_SRC)->getDescPtr();
+ const auto& weiDesc = memory.at(ARG_WEI)->getDescPtr();
+ const auto& dstDesc = memory.at(ARG_DST)->getDescPtr();
+
+ const auto& originalDims = dstDesc->getShape().getMinDims();
+ const auto& dims = originalDims;
+
+ auto isINT8 =
+ one_of(srcDesc->getPrecision(), ov::element::u8, ov::element::i8) && weiDesc->getPrecision() == ov::element::i8;
+ auto outputDataType = DnnlExtensionUtils::ElementTypeToDataType(dstDesc->getPrecision());
+
+ DnnlPostOpsComposer dnnlpoc(postOps,
+ context->getEngine(),
+ dims,
+ dims.size() - 1,
+ isINT8,
+ 1 << 0,
+ attrs.dequantizationScales,
+ !memory.at(ARG_BIAS)->getDesc().empty(),
+ outputDataType);
+
+ return dnnlpoc.compose();
+}
+
+static dnnl::matmul::primitive_desc createDescriptorInternal(const dnnl::memory::desc& inputDesc,
+ const dnnl::memory::desc& weightDesc,
+ const dnnl::memory::desc& biasDesc,
+ const dnnl::memory::desc& outputDesc,
+ const dnnl::primitive_attr& attr,
+ const dnnl::engine& engine,
+ const bool useSparseWeights,
+ const bool useWeightsDecompression) {
+ auto normalizeToRank = [](const dnnl::memory::dims& vec, size_t rank) -> dnnl::memory::dims {
+ if (vec.size() == rank || vec.empty())
+ return vec;
+
+ dnnl::memory::dims result;
+ result.reserve(rank);
+
+ for (size_t i = vec.size(); i < rank; ++i) {
+ result.push_back(1);
+ }
+
+ result.insert(result.end(), vec.begin(), vec.end());
+
+ return result;
+ };
+
+ auto weiDims = weightDesc.get_dims();
+ std::swap(weiDims[weiDims.size() - 1], weiDims[weiDims.size() - 2]);
+
+ const auto maxRank =
+ std::max({inputDesc.get_ndims(), weightDesc.get_ndims(), biasDesc.get_ndims(), outputDesc.get_ndims()});
+ const auto inpDims = normalizeToRank(inputDesc.get_dims(), maxRank);
+ const auto biaDims = normalizeToRank(biasDesc.get_dims(), maxRank);
+ const auto outDims = normalizeToRank(outputDesc.get_dims(), maxRank);
+ weiDims = normalizeToRank(weiDims, maxRank);
+
+ const dnnl::memory::desc inputsDesc = inputDesc.reshape(inpDims);
+ const dnnl::memory::desc outputsDesc = outputDesc.reshape(outDims);
+ auto newBiasDesc = !biasDesc.is_zero() ? biasDesc.reshape(biaDims) : biasDesc;
+
+ auto idt = inputDesc.get_data_type();
+ auto wdt = idt;
+ if (idt == dnnl::memory::data_type::u8 || idt == dnnl::memory::data_type::s8) {
+ wdt = memory::data_type::s8;
+ }
+
+ const dnnl::memory::desc weightsDesc =
+ dnnl::memory::desc(weiDims, wdt, memory::format_tag::any);
+
+ return dnnl::matmul::primitive_desc(engine, inputsDesc, weightsDesc, newBiasDesc, outputsDesc, attr);
+}
+
+static primitive_desc createPrimitiveDesc(const dnnl::memory::desc& inputDesc,
+ const dnnl::memory::desc& weightDesc,
+ const dnnl::memory::desc& biasDesc,
+ const dnnl::memory::desc& outputDesc,
+ const dnnl::primitive_attr& attr,
+ const dnnl::engine& engine,
+ const std::vector<impl_desc_type>& implPriorities,
+ const bool useSparseWeights,
+ const bool useWeightsDecompression) {
+ auto prim_desc = createDescriptorInternal(inputDesc,
+ weightDesc,
+ biasDesc,
+ outputDesc,
+ attr,
+ engine,
+ useSparseWeights,
+ useWeightsDecompression);
+ OPENVINO_ASSERT(prim_desc, "Failed to create matmul primitive descriptor");
+ auto first_desc = dnnl::matmul::primitive_desc(prim_desc.get());
+
+ const bool found = DnnlExtensionUtils::find_implementation(prim_desc, [&](impl_desc_type implType) {
+ return contains(implPriorities, implType);
+ });
+
+ if (found)
+ return std::move(prim_desc);
+
+ return std::move(first_desc);
+}
+
+static VectorDims makeDummyInputDims(const Shape& inShape, const Shape& wShape) {
+ const auto& weightDims = wShape.getStaticDims();
+
+ auto inMinDims = inShape.getMinDims();
+ auto inMaxDims = inShape.getMaxDims();
+ inMinDims.back() = weightDims.back();
+ inMaxDims.back() = weightDims.back();
+
+ return MemoryDescUtils::makeDummyShape(Shape(inMinDims, inMaxDims)).getStaticDims();
+}
+
+static VectorDims makeDummyOutputDims(const VectorDims& inShape, const VectorDims& wShape, const size_t out_rank) {
+ size_t activationRank = inShape.size();
+ size_t channelRank = wShape.size() - 1;
+ // activation weight output_shape
+ // NCHW CoCHW NCo
+ // TNC CoC TNCo
+ // NC CoC NCo
+ VectorDims outputShape(out_rank, 1);
+ // set Co
+ outputShape.back() = wShape[0];
+ // set batch dims
+ size_t batchRank = activationRank - channelRank;
+ size_t startIdx = out_rank - batchRank - 1;
+ for (size_t i = 0; i < batchRank; i++) {
+ outputShape[i + startIdx] = inShape[i];
+ }
+
+ return outputShape;
+}
+
+DnnlShapeAgnosticDataPtr DnnlMatMulPrimitive::createShapeAgnosticData(const FCAttrs& attrs,
+ const PostOps& postOps,
+ const MemoryArgs& memory,
+ const ExecutorContext::CPtr context,
+ const bool cacheWeights) {
+ DEBUG_LOG("Creating shape agnostic data");
+ auto srcDesc = memory.at(ARG_SRC)->getDescPtr();
+ const auto& weiDesc = memory.at(ARG_WEI)->getDescPtr();
+ const auto& biasDesc = memory.at(ARG_BIAS)->getDescPtr();
+ auto dstDesc = memory.at(ARG_DST)->getDescPtr();
+ MatMulAttrs mmAttrs{false, false, attrs.dequantizationScales};
+
+ const auto postOpData = createPrimitiveAttrs(mmAttrs, postOps, memory, context, false);
+
+ if (!cacheWeights)
+ return std::make_shared<DnnlShapeAgnosticData>(postOpData);
+
+ if (srcDesc->getShape().isDynamic()) {
+ const auto& inShape = srcDesc->getShape();
+ const auto& wShape = weiDesc->getShape();
+ const auto& inDymmyDims = makeDummyInputDims(inShape, wShape);
+ srcDesc = srcDesc->cloneWithNewDims(inDymmyDims);
+ const auto& outDymmyDims =
+ makeDummyOutputDims(inDymmyDims, wShape.getStaticDims(), dstDesc->getShape().getRank());
+ dstDesc = dstDesc->cloneWithNewDims(outDymmyDims);
+ }
+
+ const dnnl::memory::desc srcDnnlDesc = MemoryDescUtils::convertToDnnlMemoryDesc(srcDesc)->getDnnlDesc();
+ const dnnl::memory::desc weiDnnlDesc = MemoryDescUtils::convertToDnnlMemoryDesc(weiDesc)->getDnnlDesc();
+ const dnnl::memory::desc dstDnnlDesc = MemoryDescUtils::convertToDnnlMemoryDesc(dstDesc)->getDnnlDesc();
+ const dnnl::memory::desc biaDnnlDesc = MemoryDescUtils::convertToDnnlMemoryDesc(biasDesc)->getDnnlDesc();
+
+ const auto primDesc = createPrimitiveDesc(srcDnnlDesc,
+ weiDnnlDesc,
+ biaDnnlDesc,
+ dstDnnlDesc,
+ postOpData.attr,
+ context->getEngine(),
+ context->getImplPriorities(),
+ false,
+ false);
+
+ const auto weightsDesc = DnnlExtensionUtils::makeDescriptor(primDesc.weights_desc());
+ auto originalWeightsDesc = MemoryDescUtils::convertToDnnlMemoryDesc(weiDesc);
+ originalWeightsDesc = makeTransposedWeightDescriptor(originalWeightsDesc, weightsDesc, attrs.weightsNonTransposed);
+
+ // ignore the result since we just need to put the packed weights into the cache
+ (void)utils::prepareWeightsMemory(originalWeightsDesc, weightsDesc, memory.at(ARG_WEI), context);
+
+ return std::make_shared<DnnlShapeAgnosticData>(postOpData);
+}
+
+static impl_desc_type implTypeFromPrimDesc(const dnnl::primitive_desc primDesc) {
+ const auto implType = parse_impl_name(primDesc.impl_info_str());
+ if (implType == ov::intel_cpu::brgemm_avx512_amx &&
+ primDesc.weights_desc().get_format_kind() == memory::format_kind::sparsed) {
+ return ov::intel_cpu::brgemm_sparse_avx512_amx;
+ }
+
+ return implType;
+}
+
+DnnlMatMulPrimitive::DnnlMatMulPrimitive(const Key& key,
+ const dnnl::engine& engine,
+ const std::vector<impl_desc_type>& implPriorities)
+ : m_stream(dnnl::stream(engine)),
+ m_primDesc(createPrimitiveDesc(key.src->getDnnlDesc(),
+ key.wei->getDnnlDesc(),
+ key.bias->getDnnlDesc(),
+ key.dst->getDnnlDesc(),
+ key.attr,
+ engine,
+ implPriorities,
+ false,
+ false)),
+ m_implType(implTypeFromPrimDesc(m_primDesc)),
+ m_srcDesc(DnnlExtensionUtils::makeDescriptor(m_primDesc.src_desc())),
+ m_weiDesc(DnnlExtensionUtils::makeDescriptor(m_primDesc.weights_desc())),
+ m_dstDesc(DnnlExtensionUtils::makeDescriptor(m_primDesc.dst_desc())),
+ m_scratchPadDesc(DnnlExtensionUtils::makeDescriptor(m_primDesc.scratchpad_desc())),
+ m_prim(primitive(m_primDesc)) {}
+
+void DnnlMatMulPrimitive::execute(const dnnl_primitive_args& primArgs) const {
+ std::cout << "Executing MM primitive" << "\n";
+ m_prim.execute(m_stream, primArgs);
+}
+
+} // namespace intel_cpu
+} // namespace ov
diff --git a/src/plugins/intel_cpu/src/nodes/executors/dnnl/dnnl_matmul_primitive.hpp b/src/plugins/intel_cpu/src/nodes/executors/dnnl/dnnl_matmul_primitive.hpp
new file mode 100644
index 00000000000..618d3abdf8b
--- /dev/null
+++ b/src/plugins/intel_cpu/src/nodes/executors/dnnl/dnnl_matmul_primitive.hpp
@@ -0,0 +1,85 @@
+// Copyright (C) 2018-2022 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include <cstddef>
+#include <oneapi/dnnl/dnnl.hpp>
+
+#include "memory_desc/dnnl_memory_desc.h"
+#include "nodes/executors/dnnl/dnnl_shape_agnostic_data.hpp"
+#include "nodes/executors/executor.hpp"
+#include "nodes/executors/fullyconnected_config.hpp"
+#include "nodes/executors/matmul_config.hpp"
+
+namespace ov {
+namespace intel_cpu {
+
+class DnnlMatMulPrimitive {
+ struct Key {
+ DnnlMemoryDescCPtr src;
+ DnnlMemoryDescCPtr wei;
+ DnnlMemoryDescCPtr bias;
+ DnnlMemoryDescCPtr dst;
+ dnnl::primitive_attr attr;
+
+ size_t hash() const;
+ bool operator==(const Key& rhs) const;
+ };
+
+public:
+ DnnlMatMulPrimitive(const Key& key, const dnnl::engine& engine, const std::vector<impl_desc_type>& implPriorities);
+
+ void execute(const dnnl_primitive_args& primArgs) const;
+
+ const DnnlMemoryDescPtr srcDesc() const {
+ return m_srcDesc;
+ }
+
+ const DnnlMemoryDescPtr dstDesc() const {
+ return m_dstDesc;
+ }
+
+ const DnnlMemoryDescPtr weightsDesc() const {
+ return m_weiDesc;
+ }
+
+ const DnnlMemoryDescPtr scratchPadDesc() const {
+ return m_scratchPadDesc;
+ }
+
+ impl_desc_type implType() const {
+ return m_implType;
+ }
+
+ static DnnlShapeAgnosticDataPtr createShapeAgnosticData(const FCAttrs& attrs,
+ const PostOps& postOps,
+ const MemoryArgs& memory,
+ const ExecutorContext::CPtr context,
+ const bool cacheWeights);
+
+ static DnnlMemoryDescPtr makeTransposedWeightDescriptor(const DnnlMemoryDescPtr srcDesc,
+ const DnnlMemoryDescPtr dstDesc,
+ bool weightsNonTransposed);
+
+ static std::shared_ptr<DnnlMatMulPrimitive> create(const MemoryArgs& memory,
+ const MatMulAttrs& attrs,
+ const ExecutorContext::CPtr context,
+ const DnnlShapeAgnosticDataPtr& shapeAgnosticData);
+
+private:
+ dnnl::stream m_stream;
+ dnnl::primitive_desc m_primDesc;
+ impl_desc_type m_implType;
+ DnnlMemoryDescPtr m_srcDesc;
+ DnnlMemoryDescPtr m_weiDesc;
+ DnnlMemoryDescPtr m_dstDesc;
+ DnnlMemoryDescPtr m_scratchPadDesc;
+ dnnl::primitive m_prim;
+};
+
+using DnnlMatMulPrimitivePtr = std::shared_ptr<DnnlMatMulPrimitive>;
+
+} // namespace intel_cpu
+} // namespace ov
diff --git a/src/plugins/intel_cpu/src/nodes/executors/dnnl/dnnl_utils.cpp b/src/plugins/intel_cpu/src/nodes/executors/dnnl/dnnl_utils.cpp
index c801eca5bbe..bdcece78b28 100644
--- a/src/plugins/intel_cpu/src/nodes/executors/dnnl/dnnl_utils.cpp
+++ b/src/plugins/intel_cpu/src/nodes/executors/dnnl/dnnl_utils.cpp
@@ -18,14 +18,6 @@ namespace ov {
namespace intel_cpu {
namespace utils {
-DnnlMemoryDescPtr makeTransposedWeightDescriptor(const DnnlMemoryDescPtr srcDesc, const DnnlMemoryDescPtr dstDesc) {
- const auto& weiDesc = srcDesc->getDnnlDesc();
- const auto reorderedWeiDesc = dnnl::memory::desc{weiDesc.get_dims(), weiDesc.get_data_type(), dnnl::memory::format_tag::ba};
- const auto transposedWeiDesc = reorderedWeiDesc.reshape(dstDesc->getDnnlDesc().get_dims());
-
- return DnnlExtensionUtils::makeDescriptor(transposedWeiDesc);
-}
-
MemoryPtr prepareWeightsMemory(const DnnlMemoryDescPtr srcWeightDesc,
const DnnlMemoryDescPtr dstWeightDesc,
const MemoryCPtr weightsMem,
diff --git a/src/plugins/intel_cpu/src/nodes/executors/dnnl/dnnl_utils.hpp b/src/plugins/intel_cpu/src/nodes/executors/dnnl/dnnl_utils.hpp
index 89ab9122cc9..1f35caef0f7 100644
--- a/src/plugins/intel_cpu/src/nodes/executors/dnnl/dnnl_utils.hpp
+++ b/src/plugins/intel_cpu/src/nodes/executors/dnnl/dnnl_utils.hpp
@@ -14,7 +14,6 @@
namespace ov {
namespace intel_cpu {
namespace utils {
-DnnlMemoryDescPtr makeTransposedWeightDescriptor(const DnnlMemoryDescPtr srcDesc, const DnnlMemoryDescPtr dstDesc);
MemoryPtr prepareWeightsMemory(const DnnlMemoryDescPtr srcWeightDesc,
const DnnlMemoryDescPtr dstWeightDesc,
const MemoryCPtr weightsMem,
diff --git a/src/plugins/intel_cpu/src/nodes/executors/fullyconnected_implementations.cpp b/src/plugins/intel_cpu/src/nodes/executors/fullyconnected_implementations.cpp
index d9caa23c9b1..be098a3c539 100644
--- a/src/plugins/intel_cpu/src/nodes/executors/fullyconnected_implementations.cpp
+++ b/src/plugins/intel_cpu/src/nodes/executors/fullyconnected_implementations.cpp
@@ -12,6 +12,7 @@
#include "nodes/executors/convolution_config.hpp"
#include "nodes/executors/dnnl/dnnl_convolution_primitive.hpp"
#include "nodes/executors/dnnl/dnnl_fullyconnected.hpp"
+#include "nodes/executors/dnnl/dnnl_matmul_primitive.hpp"
#include "nodes/executors/dnnl/dnnl_shape_agnostic_data.hpp"
#include "nodes/executors/executor.hpp"
#include "nodes/executors/executor_implementation.hpp"
@@ -25,6 +26,7 @@
#include "openvino/core/type/element_type.hpp"
#include "ov_optional.hpp"
#include "utils/cpp/maybe_unused.hpp"
+#include "utils/debug_capabilities.h"
#if defined(OV_CPU_WITH_SHL)
# include "nodes/executors/shl/shl_fullyconnected.hpp"
@@ -93,6 +95,24 @@ static const TypeMapping dnnlConvolutionTypeMapping {
// @todo should we fallback to _fxx instead of _f32 (currenly legacy logic is replicated)
{{_any, _any, _any, _any}, pt(just<f32>(), just<f32>(), just<f32>(), just<f32>())},
};
+
+static const TypeMapping dnnlMatMulTypeMapping {
+ // {src, wei, bia, dst} pt<src, wei, bias, dst>
+ {{_bf16, _bf16 | _f32, _any, _bf16 | _f32}, pt(bypass(), bypass(), use<3>(), bypass())},
+ {{_f16, _f16, _any, _f16 | _f32}, pt(bypass(), bypass(), use<3>(), bypass())},
+ // integer precision outputs are not supported for float precision inputs
+ {{_f32 | _bf16 | _f16, _any, _any, _i8 | _u8}, pt(bypass(), bypass(), use<0>(), use<0>())},
+ // compresses float weights which do not match input data precision
+ {{_f32, _half_float, _any, _any | _any}, pt(bypass(), bypass(), use<0>(), use<0>())},
+ {{_bf16, _f16, _any, _any | _any}, pt(bypass(), bypass(), use<0>(), use<0>())},
+ {{_f16, _bf16, _any, _any | _any}, pt(bypass(), bypass(), use<0>(), use<0>())},
+ // quantization configuration
+ {{_u8 | _i8, _i8, _u8|_i8|_i32|_bf16|_f16|_f32|_undefined, _u8|_i8|_i32|_bf16|_f16|_f32}, pt(bypass(), bypass(), bypass(), bypass())},
+ {{_u8 | _i8, _i8, _any, _any}, pt(bypass(), bypass(), just<f32>(), just<f32>())},
+ // @todo should we fallback to FPXX instead of _f32?
+ {{_any, _any, _any, _any}, pt(just<f32>(), just<f32>(), just<f32>(), just<f32>())},
+ // @todo explicitly cover configuration limitations for oneDNN on ARM
+};
// clang-format on
static bool fullyMatchConfiguration(const MemoryDescArgs& currentDescriptors,
@@ -335,6 +355,65 @@ const std::vector<ExecutorImplementation<FCAttrs>>& getImplementations() {
return std::make_shared<ShlFCExecutor>(attrs, postOps, memory, context);
}
)
+ OV_CPU_INSTANCE_X64(
+ "matmul_dnnl",
+ ExecutorType::Dnnl,
+ OperationType::MatMul,
+ ShapeTolerance::Dependant,
+ // supports
+ [](const FCConfig& config) -> bool {
+ // enable only with debug caps and env variable defined for now
+ CPU_DEBUG_CAP_ENABLE(
+ if (getEnvBool("OV_CPU_ENABLE_DNNL_MAMTUL_FOR_FC")) {
+ VERIFY(noSparseDecompression(config), UNSUPPORTED_SPARSE_WEIGHTS);
+ VERIFY(noWeightsDecompression(config), UNSUPPORTED_WEIGHTS_DECOMPRESSION);
+ return true;
+ })
+ return false;
+ },
+ // requiresFallback
+ [](const FCConfig& config) -> ov::optional<executor::Config<FCAttrs>> {
+ return requiresFallbackCommon(config,
+ dnnlMatMulTypeMapping,
+ dnnlFCLayoutConfig,
+ dnnlFCMappingNotation);
+ },
+ // acceptsShapes
+ [](const MemoryArgs& memory) -> bool {
+ return true;
+ },
+ // create
+ [](const FCAttrs& attrs,
+ const PostOps& postOps,
+ const MemoryArgs& memory,
+ ExecutorContext::CPtr context) -> std::shared_ptr<Executor> {
+ struct MatMulInstantiator {
+ std::shared_ptr<DnnlMatMulPrimitive> operator()(
+ const MemoryArgs& memory,
+ const FCAttrs& attrs,
+ const ExecutorContext::CPtr context,
+ std::shared_ptr<DnnlShapeAgnosticData> shareAgnosticData) const {
+ MatMulAttrs matMulAttrs{false,
+ false,
+ attrs.dequantizationScales};
+ auto primitive =
+ DefaultInstantiator<DnnlMatMulPrimitive, MatMulAttrs, DnnlShapeAgnosticData>{}(
+ memory,
+ matMulAttrs,
+ context,
+ shareAgnosticData);
+ return primitive;
+ }
+ };
+
+ return std::make_shared<
+ DnnlFCExecutor<DnnlMatMulPrimitive, FCAttrs, DnnlShapeAgnosticData, MatMulInstantiator>>(
+ attrs,
+ postOps,
+ memory,
+ context,
+ false);
+ })
OV_CPU_INSTANCE_DNNL(
"fullyconnected_dnnl",
ExecutorType::Dnnl,
diff --git a/src/plugins/intel_cpu/src/nodes/executors/matmul_config.hpp b/src/plugins/intel_cpu/src/nodes/executors/matmul_config.hpp
new file mode 100644
index 00000000000..9e484b24a29
--- /dev/null
+++ b/src/plugins/intel_cpu/src/nodes/executors/matmul_config.hpp
@@ -0,0 +1,20 @@
+// Copyright (C) 2024 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include "executor_config.hpp"
+
+namespace ov {
+namespace intel_cpu {
+
+struct MatMulAttrs {
+ bool transposeA;
+ bool transposeB;
+ std::vector<float> dequantizationScales;
+};
+
+using MatMulConfig = executor::Config<MatMulAttrs>;
+} // namespace intel_cpu
+} // namespace ov
diff --git a/src/plugins/intel_cpu/src/utils/debug_capabilities.cpp b/src/plugins/intel_cpu/src/utils/debug_capabilities.cpp
index 74b13244b54..0a400c5edd4 100644
--- a/src/plugins/intel_cpu/src/utils/debug_capabilities.cpp
+++ b/src/plugins/intel_cpu/src/utils/debug_capabilities.cpp
@@ -684,4 +684,9 @@ void print_dnnl_memory(const dnnl::memory& memory, const size_t size, const int
} // namespace intel_cpu
} // namespace ov
+bool getEnvBool(const char* name) {
+ static const bool env = ov::util::getenv_bool(name);
+ return env;
+}
+
#endif
diff --git a/src/plugins/intel_cpu/src/utils/debug_capabilities.h b/src/plugins/intel_cpu/src/utils/debug_capabilities.h
index 416f17d848b..cea96c6cfdb 100644
--- a/src/plugins/intel_cpu/src/utils/debug_capabilities.h
+++ b/src/plugins/intel_cpu/src/utils/debug_capabilities.h
@@ -3,6 +3,7 @@
//
#pragma once
+#include "openvino/util/env_util.hpp"
#ifdef CPU_DEBUG_CAPS
#include <string>
@@ -21,7 +22,6 @@
namespace ov {
namespace intel_cpu {
-
// OV_CPU_DEBUG_LOG controls DEBUG_LOGs to output
//
// positive filter: enables patterns in filter
@@ -263,6 +263,7 @@ struct EnforceInferPrcDebug {
}
};
+bool getEnvBool(const char* name);
#else // !CPU_DEBUG_CAPS
#define CPU_DEBUG_CAP_ENABLE(...)