Skip to content

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

[CPU] Gather node support mixed precision real16 to f32 (#29442)

Chenhu Wange5a5d9b9e474
diff --git a/src/plugins/intel_cpu/src/emitters/plugin/x64/jit_conversion_emitters.cpp b/src/plugins/intel_cpu/src/emitters/plugin/x64/jit_conversion_emitters.cpp
index cc3dfa05647..70257b662a2 100644
--- a/src/plugins/intel_cpu/src/emitters/plugin/x64/jit_conversion_emitters.cpp
+++ b/src/plugins/intel_cpu/src/emitters/plugin/x64/jit_conversion_emitters.cpp
@@ -19,9 +19,17 @@ jit_convert_emitter::jit_convert_emitter(jit_generator* host,
cpu_isa_t host_isa,
const std::shared_ptr<ov::Node>& node,
ov::element::Type exec_prc)
+ : jit_convert_emitter(host, host_isa, node->get_input_element_type(0), node->get_output_element_type(0), exec_prc) {
+}
+
+jit_convert_emitter::jit_convert_emitter(jit_generator* host,
+ cpu_isa_t host_isa,
+ const ov::element::Type& in_prec,
+ const ov::element::Type& out_prec,
+ ov::element::Type exec_prc)
: jit_emitter(host, host_isa, exec_prc),
- input_type(node->get_input_element_type(0)),
- output_type(node->get_output_element_type(0)) {
+ input_type(in_prec),
+ output_type(out_prec) {
if (output_type == ov::element::bf16) {
uni_vcvtneps2bf16 = std::make_shared<jit_uni_vcvtneps2bf16>(host, host_isa);
}
@@ -74,6 +82,15 @@ jit_convert_truncation_emitter::jit_convert_truncation_emitter(jit_generator* ho
prepare_table();
}
+jit_convert_truncation_emitter::jit_convert_truncation_emitter(jit_generator* host,
+ cpu_isa_t host_isa,
+ const ov::element::Type& in_prec,
+ const ov::element::Type& out_prec,
+ ov::element::Type exec_prc)
+ : jit_convert_emitter(host, host_isa, in_prec, out_prec, exec_prc) {
+ prepare_table();
+}
+
bool jit_convert_truncation_emitter::is_i8_and_u8_case() const {
return one_of(input_type, ov::element::i8, ov::element::u8) &&
one_of(output_type, ov::element::i8, ov::element::u8);
@@ -234,6 +251,13 @@ jit_convert_saturation_emitter::jit_convert_saturation_emitter(jit_generator* ho
ov::element::Type exec_prc)
: jit_convert_emitter(host, host_isa, node, exec_prc) {}
+jit_convert_saturation_emitter::jit_convert_saturation_emitter(jit_generator* host,
+ cpu_isa_t host_isa,
+ const ov::element::Type& in_prec,
+ const ov::element::Type& out_prec,
+ ov::element::Type exec_prc)
+ : jit_convert_emitter(host, host_isa, in_prec, out_prec, exec_prc) {}
+
void jit_convert_saturation_emitter::emit_impl(const std::vector<size_t>& in_vec_idxs,
const std::vector<size_t>& out_vec_idxs) const {
validate_types();
diff --git a/src/plugins/intel_cpu/src/emitters/plugin/x64/jit_conversion_emitters.hpp b/src/plugins/intel_cpu/src/emitters/plugin/x64/jit_conversion_emitters.hpp
index 4b34704a786..0f8f5bef7d8 100644
--- a/src/plugins/intel_cpu/src/emitters/plugin/x64/jit_conversion_emitters.hpp
+++ b/src/plugins/intel_cpu/src/emitters/plugin/x64/jit_conversion_emitters.hpp
@@ -15,11 +15,16 @@ public:
dnnl::impl::cpu::x64::cpu_isa_t host_isa,
const std::shared_ptr<ov::Node>& n,
ov::element::Type exec_prc = ov::element::f32);
+ jit_convert_emitter(dnnl::impl::cpu::x64::jit_generator* host,
+ dnnl::impl::cpu::x64::cpu_isa_t host_isa,
+ const ov::element::Type& in_prec,
+ const ov::element::Type& out_prec,
+ ov::element::Type exec_prc = ov::element::f32);
size_t get_inputs_num() const override;
+ void emit_data() const override;
protected:
- void emit_data() const override;
void validate_types() const;
template <dnnl::impl::cpu::x64::cpu_isa_t isa>
@@ -44,6 +49,11 @@ public:
dnnl::impl::cpu::x64::cpu_isa_t host_isa,
const std::shared_ptr<ov::Node>& n,
ov::element::Type exec_prc = ov::element::f32);
+ jit_convert_truncation_emitter(dnnl::impl::cpu::x64::jit_generator* host,
+ dnnl::impl::cpu::x64::cpu_isa_t host_isa,
+ const ov::element::Type& in_prec,
+ const ov::element::Type& out_prec,
+ ov::element::Type exec_prc = ov::element::f32);
private:
void emit_impl(const std::vector<size_t>& in, const std::vector<size_t>& out) const override;
@@ -67,6 +77,11 @@ public:
dnnl::impl::cpu::x64::cpu_isa_t host_isa,
const std::shared_ptr<ov::Node>& n,
ov::element::Type exec_prc = ov::element::f32);
+ jit_convert_saturation_emitter(dnnl::impl::cpu::x64::jit_generator* host,
+ dnnl::impl::cpu::x64::cpu_isa_t host_isa,
+ const ov::element::Type& in_prec,
+ const ov::element::Type& out_prec,
+ ov::element::Type exec_prc = ov::element::f32);
private:
void emit_impl(const std::vector<size_t>& in, const std::vector<size_t>& out) const override;
diff --git a/src/plugins/intel_cpu/src/graph_optimizer.cpp b/src/plugins/intel_cpu/src/graph_optimizer.cpp
index 7e6ee3db28a..e3775e64827 100644
--- a/src/plugins/intel_cpu/src/graph_optimizer.cpp
+++ b/src/plugins/intel_cpu/src/graph_optimizer.cpp
@@ -162,6 +162,10 @@ void GraphOptimizer::ApplyCommonGraphOptimizations(Graph& graph) {
FuseReduceAndSimpleOperation(graph);
graph.RemoveDroppedNodes();
+ OV_ITT_SCOPE_NEXT(FIRST_INFERENCE, taskChain, "FuseGatherAndConvert");
+ FuseGatherAndConvert(graph);
+ graph.RemoveDroppedNodes();
+
OV_ITT_SCOPE_NEXT(FIRST_INFERENCE, taskChain, "FuseEltwiseAndSimple");
FuseEltwiseAndSimple(graph);
graph.RemoveDroppedNodes();
@@ -1862,6 +1866,36 @@ void GraphOptimizer::FuseMVNAndSimpleOperation(Graph& graph) {
}
}
+void GraphOptimizer::FuseGatherAndConvert(Graph& graph) {
+ auto& graphNodes = graph.GetNodes();
+
+ auto isSuitableParentNode = [](const NodePtr& node) {
+ return (node->getType() == Type::Gather) && (node->getChildEdges().size() == 1) &&
+ one_of(node->getOriginalInputPrecisionAtPort(0), element::f16, element::bf16);
+ };
+
+ auto parent = graphNodes.begin();
+ while (parent != graphNodes.end()) {
+ auto parentNode = *parent;
+ if (!isSuitableParentNode(parentNode)) {
+ parent++;
+ continue;
+ }
+
+ CPU_GRAPH_OPTIMIZER_SCOPE(FuseGatherAndConvert);
+
+ auto childNode = parentNode->getChildEdgeAt(0)->getChild();
+ // currently Gather could only fuse Convert. If extend to fuse other nodes, this pass should be updated.
+ if (childNode->getType() != Type::Convert || !parentNode->canFuse(childNode)) {
+ parent++;
+ continue;
+ }
+
+ childNode->fuseInto(parentNode);
+ graph.DropNode(childNode);
+ }
+}
+
void GraphOptimizer::FuseInterpolateAndSimpleOperation(Graph& graph) {
auto& graphNodes = graph.GetNodes();
diff --git a/src/plugins/intel_cpu/src/graph_optimizer.h b/src/plugins/intel_cpu/src/graph_optimizer.h
index 935078d4ba5..2b1e75ed396 100644
--- a/src/plugins/intel_cpu/src/graph_optimizer.h
+++ b/src/plugins/intel_cpu/src/graph_optimizer.h
@@ -38,6 +38,7 @@ private:
void FuseInterpolateAndSimpleOperation(Graph& graph);
void FuseNormalizeL2AndSimpleOperation(Graph& graph);
void FuseReduceAndSimpleOperation(Graph& graph);
+ void FuseGatherAndConvert(Graph& graph);
void DropDoubleReorders(Graph& graph);
void FuseConvolutionAndZeroPoints(Graph& graph);
diff --git a/src/plugins/intel_cpu/src/nodes/gather.cpp b/src/plugins/intel_cpu/src/nodes/gather.cpp
index 70024dbf679..aa1d7571616 100644
--- a/src/plugins/intel_cpu/src/nodes/gather.cpp
+++ b/src/plugins/intel_cpu/src/nodes/gather.cpp
@@ -16,6 +16,7 @@
#include "common/cpu_memcpy.h"
#include "kernels/x64/gather_uni_kernel.hpp"
+#include "nodes/common/cpu_convert.h"
#include "openvino/core/parallel.hpp"
#include "ov_ops/gather_compressed.hpp"
#include "selective_build.h"
@@ -127,7 +128,14 @@ void Gather::initSupportedPrimitiveDescriptors() {
return;
}
- dataTypeSize = getOriginalInputPrecisionAtPort(GATHER_DATA).size();
+ dataPrecision = getOriginalInputPrecisionAtPort(GATHER_DATA);
+ outPrecision = getOriginalOutputPrecisionAtPort(0);
+ if (!fusedWith.empty()) {
+ outPrecision = fusedWith[fusedWith.size() - 1]->getOriginalOutputPrecisionAtPort(0);
+ }
+
+ dataTypeSize = dataPrecision.size();
+ outTypeSize = outPrecision.size();
const auto& dataDims = getInputShapeAtPort(GATHER_DATA).getDims();
if (isAxisInputConst && isDataShapeStat) {
@@ -138,6 +146,7 @@ void Gather::initSupportedPrimitiveDescriptors() {
afterAxisSize = std::accumulate(dataDims.begin() + axis + 1, dataDims.end(), 1lu, std::multiplies<>());
afterAxisSizeInBytes = afterAxisSize * dataTypeSize;
+ afterAxisSizeInBytesOut = afterAxisSize * outTypeSize;
axisAndAfterAxisSize = axisDim * afterAxisSize;
axisAndAfterAxisSizeInBytes = axisDim * afterAxisSizeInBytes;
srcAfterBatchSize = betweenBatchAndAxisSize * axisAndAfterAxisSize;
@@ -153,12 +162,12 @@ void Gather::initSupportedPrimitiveDescriptors() {
if (isDataShapeStat) {
specIdxAndAfterAxSize = specIndicesSize * afterAxisSize;
specIdxAndAfterAxSizeB = specIndicesSize * afterAxisSizeInBytes;
+ specIdxAndAfterAxSizeBOut = specIndicesSize * afterAxisSizeInBytesOut;
totalWork = beforeBatchSize * betweenBatchAndAxisSize * specIndicesSize * afterAxisSize;
}
}
-
- ov::element::Type dataPrecision = getOriginalInputPrecisionAtPort(GATHER_DATA);
if (compressed) {
+ // gatherCompressed support input precision (u4/i4/u8/i8) to output precision (f16/bf16/f32).
if (!one_of(dataPrecision, ov::element::u8, ov::element::u4, ov::element::i8, ov::element::i4)) {
dataPrecision = ov::element::f32;
}
@@ -168,7 +177,6 @@ void Gather::initSupportedPrimitiveDescriptors() {
scalePrecision = ov::element::f32;
}
- ov::element::Type outPrecision = getOriginalOutputPrecisionAtPort(0);
if (!one_of(outPrecision, ov::element::f32, ov::element::f16, ov::element::bf16)) {
outPrecision = ov::element::f32;
}
@@ -206,12 +214,16 @@ void Gather::initSupportedPrimitiveDescriptors() {
addSupportedPrimDesc({{LayoutType::ncsp, dataPrecision},
{LayoutType::ncsp, ov::element::i32},
{LayoutType::ncsp, ov::element::i32, isAxisInputConst}},
- {{LayoutType::ncsp, dataPrecision}},
+ {{LayoutType::ncsp, outPrecision}},
ref_any);
// Let's check for the special inPlace memory use case
// in place only makes sense when we split by dense blocks since strided tensors are not supported by most nodes
+ if (dataPrecision != outPrecision) {
+ return;
+ }
+
if (!isAxisInputConst) {
return;
}
@@ -269,6 +281,8 @@ void Gather::createPrimitive() {
(x64::mayiuse(x64::avx512_core) || (x64::mayiuse(x64::avx2) && dataTypeSize == 4))))) {
jGatherConfParams jcp;
jcp.dataTypeSize = dataTypeSize;
+ jcp.in_prec = dataPrecision;
+ jcp.out_prec = outPrecision;
jcp.reverseIndexing = reverseIndexing;
jcp.dynamicShapes = isDynamicNode();
jcp.batchDims = batchDims;
@@ -382,6 +396,7 @@ void Gather::prepareParams() {
afterAxisSize = std::accumulate(dataDims.begin() + axis + 1, dataDims.end(), 1lu, std::multiplies<>());
afterAxisSizeInBytes = afterAxisSize * dataTypeSize;
+ afterAxisSizeInBytesOut = afterAxisSize * outTypeSize;
axisAndAfterAxisSize = axisDim * afterAxisSize;
axisAndAfterAxisSizeInBytes = axisDim * afterAxisSizeInBytes;
srcAfterBatchSize = betweenBatchAndAxisSize * axisAndAfterAxisSize;
@@ -390,6 +405,7 @@ void Gather::prepareParams() {
if (isIdxShapeStat) {
specIdxAndAfterAxSize = specIndicesSize * afterAxisSize;
specIdxAndAfterAxSizeB = specIndicesSize * afterAxisSizeInBytes;
+ specIdxAndAfterAxSizeBOut = specIndicesSize * afterAxisSizeInBytesOut;
totalWork = beforeBatchSize * betweenBatchAndAxisSize * specIndicesSize * afterAxisSize;
}
}
@@ -400,6 +416,7 @@ void Gather::prepareParams() {
specIdxAndAfterAxSize = specIndicesSize * afterAxisSize;
specIdxAndAfterAxSizeB = specIndicesSize * afterAxisSizeInBytes;
+ specIdxAndAfterAxSizeBOut = specIndicesSize * afterAxisSizeInBytesOut;
totalWork = beforeBatchSize * betweenBatchAndAxisSize * specIndicesSize * afterAxisSize;
}
@@ -441,7 +458,7 @@ void Gather::execute(const dnnl::stream& strm) {
auto arg = gatherJitExecArgs();
arg.src = srcData;
- arg.dst = dstData + p.dstStart * dataTypeSize;
+ arg.dst = dstData + p.dstStart * outTypeSize;
arg.indices = srcIndices;
arg.start = &p.dstStart;
arg.axisDim = &axisDim;
@@ -508,12 +525,12 @@ void Gather::executeDynamicImpl(const dnnl::stream& strm) {
const uint64_t wpt = ((totalWork / dataElPerVec) / nthr + 1) * dataElPerVec;
const uint64_t start = std::min(wpt * ithr, totalWork);
const uint64_t end = std::min(wpt * (ithr + 1), totalWork);
- const uint64_t workAmount = end - start;
+ const uint64_t workAmount = end - start; // how many elements processed in this thread
auto arg = gatherJitExecArgs();
arg.src = srcData;
- arg.dst = dstData + afterAxisSizeInBytes * start;
+ arg.dst = dstData + afterAxisSizeInBytesOut * start;
arg.indices = srcIndices;
arg.start = &start;
arg.axisDim = &axisDim;
@@ -886,7 +903,7 @@ void Gather::execReference() {
const auto* srcData = getSrcDataAtPortAs<const uint8_t>(GATHER_DATA);
auto* dstData = getDstDataAtPortAs<uint8_t>(0);
- const size_t dstAfterBatchSize = betweenBatchAndAxisSize * specIdxAndAfterAxSizeB;
+ const size_t dstAfterBatchSize = betweenBatchAndAxisSize * specIdxAndAfterAxSizeBOut;
parallel_for2d(beforeBatchSize, specIndicesSize, [&](const size_t b, const size_t j) {
int ii = srcIndices[b * specIndicesSize + j];
if (ii < 0) {
@@ -897,18 +914,22 @@ void Gather::execReference() {
}
}
const size_t idx = ii;
- const size_t c2 = dstAfterBatchSize * b + afterAxisSizeInBytes * j;
+ const size_t c2 = dstAfterBatchSize * b + afterAxisSizeInBytesOut * j;
if (idx < static_cast<size_t>(axisDim)) {
size_t c1 = srcAfterBatchSizeInBytes * b + afterAxisSizeInBytes * idx;
for (size_t i = 0; i < betweenBatchAndAxisSize; i++) {
size_t srcIdx = c1 + axisAndAfterAxisSizeInBytes * i;
- size_t dstIdx = c2 + specIdxAndAfterAxSizeB * i;
+ size_t dstIdx = c2 + specIdxAndAfterAxSizeBOut * i;
- cpu_memcpy(&dstData[dstIdx], &srcData[srcIdx], afterAxisSizeInBytes);
+ if (dataPrecision == outPrecision) {
+ cpu_memcpy(&dstData[dstIdx], &srcData[srcIdx], afterAxisSizeInBytes);
+ } else {
+ cpu_convert(&srcData[srcIdx], &dstData[dstIdx], dataPrecision, outPrecision, afterAxisSize);
+ }
}
} else {
for (size_t i = 0; i < betweenBatchAndAxisSize; i++) {
- memset(&dstData[c2 + specIdxAndAfterAxSizeB * i], 0, afterAxisSizeInBytes);
+ memset(&dstData[c2 + specIdxAndAfterAxSizeBOut * i], 0, afterAxisSizeInBytesOut);
}
}
});
@@ -980,4 +1001,15 @@ void Gather::resolveInPlaceEdges(Edge::LOOK look) {
}
}
+bool Gather::canFuse(const NodePtr& node) const {
+ if (node->getType() != Type::Convert) {
+ return false;
+ }
+ if (!one_of(node->getOriginalInputPrecisionAtPort(0), element::f16, element::bf16) ||
+ node->getOriginalOutputPrecisionAtPort(0) != ov::element::f32) {
+ return false;
+ }
+ return true;
+}
+
} // namespace ov::intel_cpu::node
diff --git a/src/plugins/intel_cpu/src/nodes/gather.h b/src/plugins/intel_cpu/src/nodes/gather.h
index 1a05f88638b..8cdb1fef435 100644
--- a/src/plugins/intel_cpu/src/nodes/gather.h
+++ b/src/plugins/intel_cpu/src/nodes/gather.h
@@ -55,6 +55,7 @@ public:
static int8_t get_u4(const uint8_t& val, bool high);
template <typename OUT_TYPE, int8_t get4Bit(const uint8_t&, bool)>
void execCompressed4Bit();
+ bool canFuse(const NodePtr& node) const override;
protected:
void executeDynamicImpl(const dnnl::stream& strm) override;
@@ -77,7 +78,10 @@ private:
bool reverseIndexing = false;
+ ov::element::Type dataPrecision;
+ ov::element::Type outPrecision;
uint64_t dataTypeSize = 1lu;
+ uint64_t outTypeSize = 1lu;
static constexpr uint64_t idxTypeSize = sizeof(int);
int axis = 0;
@@ -90,11 +94,13 @@ private:
uint64_t betweenBatchAndAxisSize = 0lu;
uint64_t afterAxisSize = 0lu;
uint64_t afterAxisSizeInBytes = 0lu;
+ uint64_t afterAxisSizeInBytesOut = 0lu;
uint64_t axisAndAfterAxisSizeInBytes = 0lu;
uint64_t axisAndAfterAxisSize = 0lu;
uint64_t srcAfterBatchSizeInBytes = 0lu;
uint64_t srcAfterBatchSize = 0lu;
uint64_t specIdxAndAfterAxSizeB = 0lu;
+ uint64_t specIdxAndAfterAxSizeBOut = 0lu;
uint64_t specIdxAndAfterAxSize = 0lu;
uint64_t totalWork = 0lu;
diff --git a/src/plugins/intel_cpu/src/nodes/kernels/x64/gather_uni_kernel.cpp b/src/plugins/intel_cpu/src/nodes/kernels/x64/gather_uni_kernel.cpp
index 80dacd375eb..521006a5115 100644
--- a/src/plugins/intel_cpu/src/nodes/kernels/x64/gather_uni_kernel.cpp
+++ b/src/plugins/intel_cpu/src/nodes/kernels/x64/gather_uni_kernel.cpp
@@ -69,6 +69,11 @@ jitUniGatherKernel<isa>::jitUniGatherKernel(const jGatherConfParams& jcp)
permMask8bitUni = permMask8bitA5;
permMask16bitUni = permMask16bitA5;
}
+ dstStep = is_real16_to_f32 ? 2 * vlen : vlen;
+ if (is_real16_to_f32) {
+ convert_emitter =
+ std::make_unique<jit_convert_saturation_emitter>(this, isa, jcp.in_prec, jcp.out_prec, ov::element::f32);
+ }
}
template <x64::cpu_isa_t isa>
@@ -320,6 +325,10 @@ void jitUniGatherKernel<isa>::generate() {
}
this->postamble();
+
+ if (convert_emitter) {
+ convert_emitter->emit_data();
+ }
}
template <>
@@ -751,24 +760,60 @@ void jitUniGatherKernel<isa>::process(bool isShortIdx, bool blocked) {
L(lEndProc);
}
+template <x64::cpu_isa_t isa>
+void jitUniGatherKernel<isa>::store(const Xbyak::Reg64& reg_dst, Vmm& vmmSrc) {
+ if (is_real16_to_f32) {
+ // keep reg_dst, incremented outside
+ constexpr bool is_zmm = std::is_same<Vmm, Xbyak::Zmm>::value;
+ Xbyak::Ymm ymmSrc(vmmSrc.getIdx());
+ Xbyak::Xmm xmmSrc(vmmSrc.getIdx());
+ if (is_zmm) {
+ // if zmm, split to 2 ymms, up convert to 2 zmms, store
+ // last(31) is used as temp
+ Xbyak::Zmm zmmTemp(31);
+ Xbyak::Ymm ymmTemp(31);
+ convert_emitter->emit_code({static_cast<size_t>(ymmSrc.getIdx())}, {static_cast<size_t>(zmmTemp.getIdx())});
+ uni_vmovups(ptr[reg_dst], zmmTemp);
+ vshuff64x2(zmmTemp, vmmSrc, vmmSrc, 0b00001110);
+ convert_emitter->emit_code({static_cast<size_t>(ymmTemp.getIdx())},
+ {static_cast<size_t>(zmmTemp.getIdx())});
+ uni_vmovups(ptr[reg_dst + vlen], zmmTemp);
+ } else {
+ // if ymm, split to 2 xmms, up convert to 2 ymms, store
+ // vmmZeros is used as temp
+ Xbyak::Ymm ymmTemp(vmmZeros.getIdx());
+ Xbyak::Xmm xmmTemp(vmmZeros.getIdx());
+ convert_emitter->emit_code({static_cast<size_t>(xmmSrc.getIdx())}, {static_cast<size_t>(ymmTemp.getIdx())});
+ uni_vmovups(ptr[reg_dst], ymmTemp);
+ vperm2f128(ymmTemp, ymmSrc, ymmSrc, 0x1);
+ convert_emitter->emit_code({static_cast<size_t>(xmmTemp.getIdx())},
+ {static_cast<size_t>(ymmTemp.getIdx())});
+ uni_vmovups(ptr[reg_dst + vlen], ymmTemp);
+ uni_vpxor(vmmZeros, vmmZeros, vmmZeros);
+ }
+ } else {
+ uni_vmovups(ptr[reg_dst], vmmSrc);
+ }
+}
+
template <x64::cpu_isa_t isa>
void jitUniGatherKernel<isa>::process32b(bool isShortIdx, bool blocked) {
Xbyak::Label lDstIdxLoop, lTail;
// First iteration
shiftIdxAndGather(vmmAuxContainer, isShortIdx, false, blocked);
- uni_vmovups(ptr[regDst], vmmAuxContainer[2]);
+ store(regDst, vmmAuxContainer[2]);
// Main loop
L(lDstIdxLoop);
{
- add(regDst, vlen);
+ add(regDst, dstStep);
sub(regWorkAmount, dataElPerVec);
cmp(regWorkAmount, dataElPerVec);
jl(lTail, T_NEAR);
shiftIdxAndGather(vmmAuxContainer, isShortIdx, true, blocked);
- uni_vmovups(ptr[regDst], vmmAuxContainer[2]);
+ store(regDst, vmmAuxContainer[2]);
jmp(lDstIdxLoop, T_NEAR);
}
@@ -808,12 +853,12 @@ void jitUniGatherKernel<isa>::process16b(bool isShortIdx, bool blocked) {
uni_vmovups(vPermMask, ptr[regAux1]);
vpermd(vmmAuxContainer[0], vPermMask, vmmAuxContainer[0]);
- uni_vmovups(ptr[regDst], vmmAuxContainer[0]);
+ store(regDst, vmmAuxContainer[0]);
// Main loop.
L(lDstIdxLoop1);
{
- add(regDst, vlen);
+ add(regDst, dstStep);
sub(regWorkAmount, dataElPerVec);
cmp(regWorkAmount, dataElPerVec);
jl(lTail, T_NEAR);
@@ -832,7 +877,7 @@ void jitUniGatherKernel<isa>::process16b(bool isShortIdx, bool blocked) {
}
vpermd(vmmAuxContainer[0], vPermMask, vmmAuxContainer[0]);
- uni_vmovups(ptr[regDst], vmmAuxContainer[0]);
+ store(regDst, vmmAuxContainer[0]);
jmp(lDstIdxLoop1, T_NEAR);
}
@@ -883,12 +928,12 @@ void jitUniGatherKernel<isa>::process8b(bool isShortIdx, bool blocked) {
vpermd(vmmAuxContainer[0], vPermMask, vmmAuxContainer[0]);
- uni_vmovups(ptr[regDst], vmmAuxContainer[0]);
+ store(regDst, vmmAuxContainer[0]);
// Main loop.
L(lDstIdxLoop1);
{
- add(regDst, vlen);
+ add(regDst, dstStep);
sub(regWorkAmount, dataElPerVec);
cmp(regWorkAmount, dataElPerVec);
jl(lTail, T_NEAR);
@@ -917,7 +962,7 @@ void jitUniGatherKernel<isa>::process8b(bool isShortIdx, bool blocked) {
}
vpermd(vmmAuxContainer[0], vPermMask, vmmAuxContainer[0]);
- uni_vmovups(ptr[regDst], vmmAuxContainer[0]);
+ store(regDst, vmmAuxContainer[0]);
jmp(lDstIdxLoop1, T_NEAR);
}
@@ -1051,6 +1096,8 @@ void jitUniGatherKernel<isa>::storeVectorPart(const Xbyak::Reg64& rDst,
const Xbyak::Reg64& rToStoreCounter,
Vmm& vmmSrc,
Vmm& vAux) {
+ Xbyak::Ymm ymmTemp(vmmZeros.getIdx());
+ Xbyak::Xmm xmmTemp(vmmZeros.getIdx());
Xbyak::Label lEnd;
Xbyak::Xmm xAux(vAux.getIdx());
for (size_t j = 0; j < vlen / vlenXmm; j++) {
@@ -1067,15 +1114,31 @@ void jitUniGatherKernel<isa>::storeVectorPart(const Xbyak::Reg64& rDst,
if (jcp.dataTypeSize == 4) {
uni_vpextrd(ptr[rDst], xAux, k);
} else if (jcp.dataTypeSize == 2) {
- uni_vpextrw(ptr[rDst], xAux, k * 2);
+ if (jcp.in_prec == jcp.out_prec) {
+ uni_vpextrw(ptr[rDst], xAux, k * 2);
+ } else if (jcp.out_prec == element::f32) {
+ // xAux should not changed
+ convert_emitter->emit_code({static_cast<size_t>(xAux.getIdx())},
+ {static_cast<size_t>(ymmTemp.getIdx())});
+ if (k < 2) {
+ uni_vpextrd(ptr[rDst], xmmTemp, k * 2);
+ } else {
+ vperm2f128(ymmTemp, ymmTemp, ymmTemp, 0x1);
+ uni_vpextrd(ptr[rDst], xmmTemp, k * 2 - 4);
+ }
+ }
} else if (jcp.dataTypeSize == 1) {
uni_vpextrb(ptr[rDst], xAux, k * 4);
}
- add(rDst, jcp.dataTypeSize);
+ add(rDst, jcp.out_prec.size());
sub(rToStoreCounter, 1);
}
}
+ if (is_real16_to_f32) {
+ uni_vpxor(vmmZeros, vmmZeros, vmmZeros);
+ }
+
L(lEnd);
}
diff --git a/src/plugins/intel_cpu/src/nodes/kernels/x64/gather_uni_kernel.hpp b/src/plugins/intel_cpu/src/nodes/kernels/x64/gather_uni_kernel.hpp
index 3b812892422..0278ba6b215 100644
--- a/src/plugins/intel_cpu/src/nodes/kernels/x64/gather_uni_kernel.hpp
+++ b/src/plugins/intel_cpu/src/nodes/kernels/x64/gather_uni_kernel.hpp
@@ -23,12 +23,15 @@
#include "cpu/x64/jit_generator.hpp"
#include "dnnl_types.h"
+#include "emitters/plugin/x64/jit_conversion_emitters.hpp"
#include "jit_kernel_base.hpp"
namespace ov::intel_cpu {
struct jGatherConfParams {
uint64_t dataTypeSize = 1lu;
+ ov::element::Type in_prec = ov::element::f32;
+ ov::element::Type out_prec = ov::element::f32;
bool reverseIndexing = true;
bool dynamicShapes = false;
uint64_t batchDims = 0lu;
@@ -78,7 +81,9 @@ struct jitGatherKernelBase {
: jcp(jcp),
vlen(vlen),
dataElPerVec(vlen / jcp.dataTypeSize),
- idxElPerVec(vlen / indicesTypeSize) {}
+ idxElPerVec(vlen / indicesTypeSize),
+ is_real16_to_f32((jcp.in_prec == element::f16 || jcp.in_prec == element::bf16) &&
+ jcp.out_prec == element::f32) {}
virtual ~jitGatherKernelBase() = default;
virtual void create_ker() = 0;
@@ -108,6 +113,7 @@ protected:
int shortPermIdx[16];
int shortBeforeAxisDiff[16];
+ const bool is_real16_to_f32 = false;
};
template <dnnl::impl::cpu::x64::cpu_isa_t isa>
@@ -212,9 +218,12 @@ protected:
void storeVectorPart(const Xbyak::Reg64& rDst, const Xbyak::Reg64& rToStoreCounter, Vmm& vmmSrc, Vmm& vAux);
void uniVpGatherDd(Vmm& vDst, const Xbyak::Address& srcAddr, Vmask& vMask);
void fillVlenVector();
+ void store(const Xbyak::Reg64& dst_reg, Vmm& vmmSrc);
const unsigned* permMask8bitUni;
const unsigned* permMask16bitUni;
+ size_t dstStep = 0;
+ std::unique_ptr<jit_convert_saturation_emitter> convert_emitter = nullptr;
};
} // namespace ov::intel_cpu
diff --git a/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/snippets_mark_skipped.cpp b/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/snippets_mark_skipped.cpp
index 756bab2df7f..9285da07c52 100644
--- a/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/snippets_mark_skipped.cpp
+++ b/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/snippets_mark_skipped.cpp
@@ -175,6 +175,13 @@ int getChannelAxis(const ov::AxisSet& axes, bool keep_dims) {
}
return channelAxis;
}
+bool isSuitableGatherParent(const std::shared_ptr<const Node>& node) {
+ const bool is_suitable_node = ov::is_type_any_of<ov::op::v7::Gather, ov::op::v8::Gather>(node);
+ // has a single output, connected to a single child
+ const auto out = node->outputs();
+ const bool has_only_child = (out.size() == 1) && (out[0].get_target_inputs().size() == 1);
+ return is_suitable_node && has_only_child;
+}
bool isSuitableMiscParent(const std::shared_ptr<const Node>& node) {
const bool is_suitable_node = ov::is_type_any_of<ov::op::v0::MVN,
ov::op::v6::MVN,
@@ -439,6 +446,11 @@ bool isSuitableChildForFusingSumActivation(const std::shared_ptr<const Node>& no
bool isSuitableReduceChild(const std::shared_ptr<const Node>& node, const int channelAxis = DEFAULT_AXIS) {
return isSuitableChildForFusingSimple(node, channelAxis);
}
+bool isSuitableGatherChild(const std::shared_ptr<const Node>& node) {
+ return ov::is_type<ov::op::v0::Convert>(node) &&
+ one_of(node->get_input_element_type(0), element::f16, element::bf16) &&
+ node->get_output_element_type(0) == ov::element::f32;
+}
bool isSuitableMatMulWithConstantPath(const std::shared_ptr<Node>& node) {
return ov::is_type<ov::opset1::MatMul>(node) &&
!ov::is_type<ov::opset1::Constant>(node->get_input_node_shared_ptr(1)) &&
@@ -535,6 +547,9 @@ bool SnippetsMarkSkipped::run_on_model(const std::shared_ptr<ov::Model>& m) {
const auto reduce = ov::as_type_ptr<const ov::op::util::ArithmeticReductionKeepDims>(node);
channelAxis = getChannelAxis(reduce->get_reduction_axes(), reduce->get_keep_dims());
SetNodeFusingType(node, NodeFusingType::FusedWithReduce);
+ } else if (isSuitableGatherParent(node)) {
+ SetNodeFusingType(node, NodeFusingType::FusedWithGather);
+ channelAxis = DEFAULT_AXIS;
} else if (isSuitableMiscParent(node)) {
if (const auto reduce = ov::as_type_ptr<const ov::op::util::ArithmeticReductionKeepDims>(node)) {
channelAxis = getChannelAxis(reduce->get_reduction_axes(), reduce->get_keep_dims());
@@ -566,6 +581,11 @@ bool SnippetsMarkSkipped::run_on_model(const std::shared_ptr<ov::Model>& m) {
if (isSuitableReduceChild(node, channelAxis)) {
PropagateIfHasOnlyChild(node, fusingChainType);
}
+ } else if (fusingChainType == NodeFusingType::FusedWithGather) {
+ if (isSuitableGatherChild(node)) {
+ // can fuse single real16 to f32 convert
+ SetNodeFusingType(node, NodeFusingType::FusedTerminator);
+ }
} else if (isSuitableChildForFusingSimple(node, channelAxis)) {
PropagateIfHasOnlyChild(node, fusingChainType);
} else if (fusingChainType == NodeFusingType::FusedWithConvolution ||
diff --git a/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/snippets_mark_skipped.hpp b/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/snippets_mark_skipped.hpp
index 89e7704e1cd..262a0c101d1 100644
--- a/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/snippets_mark_skipped.hpp
+++ b/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/snippets_mark_skipped.hpp
@@ -48,6 +48,7 @@ enum class NodeFusingType : int64_t {
FusedWithMatMulI8,
FusedWithFCI8,
FusedWithReduce,
+ FusedWithGather,
FusedWithMisc
};
diff --git a/src/plugins/intel_cpu/tests/functional/shared_tests_instances/single_layer_tests/gather.cpp b/src/plugins/intel_cpu/tests/functional/shared_tests_instances/single_layer_tests/gather.cpp
index 9dd69807b12..2948600f6be 100644
--- a/src/plugins/intel_cpu/tests/functional/shared_tests_instances/single_layer_tests/gather.cpp
+++ b/src/plugins/intel_cpu/tests/functional/shared_tests_instances/single_layer_tests/gather.cpp
@@ -11,6 +11,7 @@ using ov::test::Gather7LayerTest;
using ov::test::Gather8LayerTest;
using ov::test::Gather8withIndicesDataLayerTest;
using ov::test::GatherStringWithIndicesDataLayerTest;
+using ov::test::GatherMixedPrecLayerTest;
const std::vector<ov::element::Type> model_types = {
ov::element::f32,
@@ -288,4 +289,20 @@ INSTANTIATE_TEST_CASE_P(smoke_gather_string,
gatherWithStringParams,
GatherStringWithIndicesDataLayerTest::getTestCaseName);
+const std::vector<ov::element::Type> mixed_model_types = {
+ ov::element::f16,
+ ov::element::bf16,
+};
+
+INSTANTIATE_TEST_SUITE_P(
+ smoke_static_4D_mixed,
+ GatherMixedPrecLayerTest,
+ testing::Combine(testing::ValuesIn(ov::test::static_shapes_to_test_representation(data_shapes_4d_gather8)),
+ testing::ValuesIn(idx_shapes_4d_gather8),
+ testing::ValuesIn(axes_batches_4d_gather8),
+ testing::ValuesIn(mixed_model_types),
+ testing::Values(ov::element::f32),
+ testing::Values(ov::test::utils::DEVICE_CPU)),
+ GatherMixedPrecLayerTest::getTestCaseName);
+
} // namespace
diff --git a/src/plugins/intel_cpu/tests/functional/shared_tests_instances/subgraph_tests/gather_weights_decompression.cpp b/src/plugins/intel_cpu/tests/functional/shared_tests_instances/subgraph_tests/gather_weights_decompression.cpp
index 184195e0c69..07700b34fad 100644
--- a/src/plugins/intel_cpu/tests/functional/shared_tests_instances/subgraph_tests/gather_weights_decompression.cpp
+++ b/src/plugins/intel_cpu/tests/functional/shared_tests_instances/subgraph_tests/gather_weights_decompression.cpp
@@ -45,7 +45,7 @@ INSTANTIATE_TEST_SUITE_P(smoke_GatherCompressedWeights_basic,
::testing::ValuesIn(per_tensor_scale)),
GatherWeightsDecompression::get_test_case_name);
-// fp16/bf16 constant + convert(16bit to f32) + gather case
+// fp16/bf16 constant + convert(16bit to f32) + gather to gather(f16/bf16 input and f32 output)
TEST_P(GatherWeightsDecompressionWithoutScale, CompareWithRefs) {
SKIP_IF_CURRENT_TEST_IS_DISABLED()
run();
diff --git a/src/tests/functional/plugin/shared/include/single_op_tests/gather.hpp b/src/tests/functional/plugin/shared/include/single_op_tests/gather.hpp
index 5cf4e167200..c1721078e78 100644
--- a/src/tests/functional/plugin/shared/include/single_op_tests/gather.hpp
+++ b/src/tests/functional/plugin/shared/include/single_op_tests/gather.hpp
@@ -32,5 +32,9 @@ TEST_P(GatherStringWithIndicesDataLayerTest, Inference) {
run();
};
+TEST_P(GatherMixedPrecLayerTest, Inference) {
+ run();
+};
+
} // namespace test
} // namespace ov
diff --git a/src/tests/functional/shared_test_classes/include/shared_test_classes/single_op/gather.hpp b/src/tests/functional/shared_test_classes/include/shared_test_classes/single_op/gather.hpp
index 1017915a5bc..256250fc564 100644
--- a/src/tests/functional/shared_test_classes/include/shared_test_classes/single_op/gather.hpp
+++ b/src/tests/functional/shared_test_classes/include/shared_test_classes/single_op/gather.hpp
@@ -101,5 +101,23 @@ private:
std::vector<std::string> string_data;
};
+typedef std::tuple<std::vector<InputShape>, // Input shapes
+ ov::Shape, // Indices shape
+ std::tuple<int, int>, // Gather axis and batch
+ ov::element::Type, // Model type
+ ov::element::Type, // Output type
+ std::string // Device name
+ >
+ gatherMixPrecParamsTuple;
+
+class GatherMixedPrecLayerTest : public testing::WithParamInterface<gatherMixPrecParamsTuple>,
+ virtual public ov::test::SubgraphBaseTest {
+public:
+ static std::string getTestCaseName(const testing::TestParamInfo<gatherMixPrecParamsTuple>& obj);
+
+protected:
+ void SetUp() override;
+};
+
} // namespace test
} // namespace ov
diff --git a/src/tests/functional/shared_test_classes/src/single_op/gather.cpp b/src/tests/functional/shared_test_classes/src/single_op/gather.cpp
index 6aa71f584ae..b7fee7c6d66 100644
--- a/src/tests/functional/shared_test_classes/src/single_op/gather.cpp
+++ b/src/tests/functional/shared_test_classes/src/single_op/gather.cpp
@@ -316,5 +316,47 @@ void GatherStringWithIndicesDataLayerTest::generate_inputs(const std::vector<ov:
inputs.insert({data_input.get_node_shared_ptr(), ov::Tensor(element::string, data_input.get_shape(), string_data.data())});
}
+std::string GatherMixedPrecLayerTest::getTestCaseName(const testing::TestParamInfo<gatherMixPrecParamsTuple>& obj) {
+ auto [ shapes, indices_shape, axis_batch_idx, model_type, out_type, device_name ] = obj.param;
+ std::ostringstream result;
+ result << "IS=(";
+ for (size_t i = 0lu; i < shapes.size(); i++) {
+ result << shapes[i] << (i < shapes.size() - 1lu ? "_" : "");
+ }
+ result << ")";
+ result << "axis=" << std::get<0>(axis_batch_idx) << "_";
+ result << "batch_idx=" << std::get<1>(axis_batch_idx) << "_";
+ result << "indices_shape=" << ov::test::utils::vec2str(indices_shape) << "_";
+ result << "netPRC=" << model_type.get_type_name() << "_";
+ result << "outPRC=" << out_type.get_type_name() << "_";
+ result << "trgDev=" << device_name << "_";
+ return result.str();
+}
+
+void GatherMixedPrecLayerTest::SetUp() {
+ auto [ shapes, indices_shape, axis_batch_idx, model_type, out_type, device_name ] = GetParam();
+ outType = out_type;
+ targetDevice = device_name;
+ init_input_shapes(shapes);
+
+ int axis = std::get<0>(axis_batch_idx);
+ int batch_idx = std::get<1>(axis_batch_idx);
+
+ auto param = std::make_shared<ov::op::v0::Parameter>(model_type, inputDynamicShapes.front());
+
+ int axis_dim = targetStaticShapes[0][0][axis < 0 ? axis + targetStaticShapes[0][0].size() : axis];
+ ov::test::utils::InputGenerateData in_data;
+ in_data.start_from = -axis_dim;
+ in_data.range = 2 * axis_dim;
+ auto indices_node_tensor = ov::test::utils::create_and_fill_tensor(ov::element::i64, indices_shape, in_data);
+ auto indices_node = std::make_shared<ov::op::v0::Constant>(indices_node_tensor);
+ auto axis_node = ov::op::v0::Constant::create(ov::element::i64, ov::Shape(), {axis});
+
+ auto gather = std::make_shared<ov::op::v8::Gather>(param, indices_node, axis_node, batch_idx);
+
+ auto result = std::make_shared<ov::op::v0::Result>(gather);
+ function = std::make_shared<ov::Model>(result, ov::ParameterVector{param}, "gather");
+}
+
} // namespace test
} // namespace ov