Skip to content

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

[CPU][x64] Fix i8/u8 SquaredDifference returning zeros in Snippets (#39683)

Arshil Ahmedb5101e5790f0
diff --git a/src/plugins/intel_cpu/src/emitters/plugin/x64/jit_eltwise_emitters.cpp b/src/plugins/intel_cpu/src/emitters/plugin/x64/jit_eltwise_emitters.cpp
index 48965a0e1ba..af889d19b56 100644
--- a/src/plugins/intel_cpu/src/emitters/plugin/x64/jit_eltwise_emitters.cpp
+++ b/src/plugins/intel_cpu/src/emitters/plugin/x64/jit_eltwise_emitters.cpp
@@ -328,8 +328,7 @@ std::set<std::vector<element::Type>> jit_multiply_emitter::get_supported_precisi
/// DIVIDE ///
jit_divide_emitter::jit_divide_emitter(x64::jit_generator_t* host,
x64::cpu_isa_t host_isa,
- const std::shared_ptr<ov::Node>& node,
- ov::element::Type /*exec_prc*/)
+ const std::shared_ptr<ov::Node>& node)
: jit_emitter(host, host_isa, get_arithmetic_binary_exec_precision(node)),
m_pythondiv(ov::as_type_ptr<ov::op::v1::Divide>(node)->is_pythondiv()) {}
jit_divide_emitter::jit_divide_emitter(x64::jit_generator_t* host, x64::cpu_isa_t host_isa, ov::element::Type exec_prc)
@@ -745,9 +744,8 @@ std::set<std::vector<element::Type>> jit_minimum_emitter::get_supported_precisio
/// SQUARED_DIFFERENCE ///
jit_squared_difference_emitter::jit_squared_difference_emitter(x64::jit_generator_t* host,
x64::cpu_isa_t host_isa,
- [[maybe_unused]] const std::shared_ptr<ov::Node>& node,
- ov::element::Type exec_prc)
- : jit_emitter(host, host_isa, exec_prc) {}
+ const std::shared_ptr<ov::Node>& node)
+ : jit_emitter(host, host_isa, get_arithmetic_binary_exec_precision(node)) {}
jit_squared_difference_emitter::jit_squared_difference_emitter(x64::jit_generator_t* host,
x64::cpu_isa_t host_isa,
ov::element::Type exec_prc)
@@ -1044,8 +1042,7 @@ size_t i32_compare_aux_vecs_count(x64::cpu_isa_t isa) {
/// EQUAL ///
jit_equal_emitter::jit_equal_emitter(x64::jit_generator_t* host,
x64::cpu_isa_t host_isa,
- const std::shared_ptr<ov::Node>& node,
- [[maybe_unused]] ov::element::Type exec_prc)
+ const std::shared_ptr<ov::Node>& node)
: jit_emitter(host, host_isa, get_arithmetic_binary_exec_precision(node)) {
prepare_table();
}
@@ -1132,8 +1129,7 @@ size_t jit_equal_emitter::aux_vecs_count() const {
/// NOT_EQUAL ///
jit_not_equal_emitter::jit_not_equal_emitter(x64::jit_generator_t* host,
x64::cpu_isa_t host_isa,
- const std::shared_ptr<ov::Node>& node,
- [[maybe_unused]] ov::element::Type exec_prc)
+ const std::shared_ptr<ov::Node>& node)
: jit_emitter(host, host_isa, get_arithmetic_binary_exec_precision(node)) {
prepare_table();
}
@@ -1222,8 +1218,7 @@ size_t jit_not_equal_emitter::aux_vecs_count() const {
/// GREATER ///
jit_greater_emitter::jit_greater_emitter(x64::jit_generator_t* host,
x64::cpu_isa_t host_isa,
- const std::shared_ptr<ov::Node>& node,
- [[maybe_unused]] ov::element::Type exec_prc)
+ const std::shared_ptr<ov::Node>& node)
: jit_emitter(host, host_isa, get_arithmetic_binary_exec_precision(node)) {
prepare_table();
}
@@ -1312,8 +1307,7 @@ size_t jit_greater_emitter::aux_vecs_count() const {
/// GREATER_EQUAL ///
jit_greater_equal_emitter::jit_greater_equal_emitter(x64::jit_generator_t* host,
x64::cpu_isa_t host_isa,
- const std::shared_ptr<ov::Node>& node,
- [[maybe_unused]] ov::element::Type exec_prc)
+ const std::shared_ptr<ov::Node>& node)
: jit_emitter(host, host_isa, get_arithmetic_binary_exec_precision(node)) {
prepare_table();
}
@@ -1402,8 +1396,7 @@ size_t jit_greater_equal_emitter::aux_vecs_count() const {
/// LESS ///
jit_less_emitter::jit_less_emitter(x64::jit_generator_t* host,
x64::cpu_isa_t host_isa,
- const std::shared_ptr<ov::Node>& node,
- [[maybe_unused]] ov::element::Type exec_prc)
+ const std::shared_ptr<ov::Node>& node)
: jit_emitter(host, host_isa, get_arithmetic_binary_exec_precision(node)) {
prepare_table();
}
@@ -1489,8 +1482,7 @@ size_t jit_less_emitter::aux_vecs_count() const {
/// LESS_EQUAL ///
jit_less_equal_emitter::jit_less_equal_emitter(x64::jit_generator_t* host,
x64::cpu_isa_t host_isa,
- const std::shared_ptr<ov::Node>& node,
- [[maybe_unused]] ov::element::Type exec_prc)
+ const std::shared_ptr<ov::Node>& node)
: jit_emitter(host, host_isa, get_arithmetic_binary_exec_precision(node)) {
prepare_table();
}
diff --git a/src/plugins/intel_cpu/src/emitters/plugin/x64/jit_eltwise_emitters.hpp b/src/plugins/intel_cpu/src/emitters/plugin/x64/jit_eltwise_emitters.hpp
index 06ca747063c..80750838517 100644
--- a/src/plugins/intel_cpu/src/emitters/plugin/x64/jit_eltwise_emitters.hpp
+++ b/src/plugins/intel_cpu/src/emitters/plugin/x64/jit_eltwise_emitters.hpp
@@ -111,8 +111,7 @@ public:
ov::element::Type exec_prc = ov::element::f32);
jit_divide_emitter(dnnl::impl::cpu::x64::jit_generator_t* host,
dnnl::impl::cpu::x64::cpu_isa_t host_isa,
- const std::shared_ptr<ov::Node>& n,
- ov::element::Type exec_prc = ov::element::f32);
+ const std::shared_ptr<ov::Node>& n);
size_t get_inputs_num() const override;
static std::set<std::vector<element::Type>> get_supported_precisions(
@@ -263,8 +262,7 @@ public:
ov::element::Type exec_prc = ov::element::f32);
jit_squared_difference_emitter(dnnl::impl::cpu::x64::jit_generator_t* host,
dnnl::impl::cpu::x64::cpu_isa_t host_isa,
- const std::shared_ptr<ov::Node>& n,
- ov::element::Type exec_prc = ov::element::f32);
+ const std::shared_ptr<ov::Node>& n);
size_t get_inputs_num() const override;
static std::set<std::vector<element::Type>> get_supported_precisions(
@@ -305,8 +303,7 @@ public:
ov::element::Type exec_prc = ov::element::f32);
jit_equal_emitter(dnnl::impl::cpu::x64::jit_generator_t* host,
dnnl::impl::cpu::x64::cpu_isa_t host_isa,
- const std::shared_ptr<ov::Node>& n,
- ov::element::Type exec_prc = ov::element::f32);
+ const std::shared_ptr<ov::Node>& n);
size_t get_inputs_num() const override;
static std::set<std::vector<element::Type>> get_supported_precisions(
@@ -329,8 +326,7 @@ public:
ov::element::Type exec_prc = ov::element::f32);
jit_not_equal_emitter(dnnl::impl::cpu::x64::jit_generator_t* host,
dnnl::impl::cpu::x64::cpu_isa_t host_isa,
- const std::shared_ptr<ov::Node>& n,
- ov::element::Type exec_prc = ov::element::f32);
+ const std::shared_ptr<ov::Node>& n);
size_t get_inputs_num() const override;
static std::set<std::vector<element::Type>> get_supported_precisions(
@@ -353,8 +349,7 @@ public:
ov::element::Type exec_prc = ov::element::f32);
jit_greater_emitter(dnnl::impl::cpu::x64::jit_generator_t* host,
dnnl::impl::cpu::x64::cpu_isa_t host_isa,
- const std::shared_ptr<ov::Node>& n,
- ov::element::Type exec_prc = ov::element::f32);
+ const std::shared_ptr<ov::Node>& n);
size_t get_inputs_num() const override;
static std::set<std::vector<element::Type>> get_supported_precisions(
@@ -377,8 +372,7 @@ public:
ov::element::Type exec_prc = ov::element::f32);
jit_greater_equal_emitter(dnnl::impl::cpu::x64::jit_generator_t* host,
dnnl::impl::cpu::x64::cpu_isa_t host_isa,
- const std::shared_ptr<ov::Node>& n,
- ov::element::Type exec_prc = ov::element::f32);
+ const std::shared_ptr<ov::Node>& n);
size_t get_inputs_num() const override;
static std::set<std::vector<element::Type>> get_supported_precisions(
@@ -401,8 +395,7 @@ public:
ov::element::Type exec_prc = ov::element::f32);
jit_less_emitter(dnnl::impl::cpu::x64::jit_generator_t* host,
dnnl::impl::cpu::x64::cpu_isa_t host_isa,
- const std::shared_ptr<ov::Node>& n,
- ov::element::Type exec_prc = ov::element::f32);
+ const std::shared_ptr<ov::Node>& n);
size_t get_inputs_num() const override;
static std::set<std::vector<element::Type>> get_supported_precisions(
@@ -426,8 +419,7 @@ public:
jit_less_equal_emitter(dnnl::impl::cpu::x64::jit_generator_t* host,
dnnl::impl::cpu::x64::cpu_isa_t host_isa,
- const std::shared_ptr<ov::Node>& n,
- ov::element::Type exec_prc = ov::element::f32);
+ const std::shared_ptr<ov::Node>& n);
size_t get_inputs_num() const override;
static std::set<std::vector<element::Type>> get_supported_precisions(
diff --git a/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/classes/eltwise_chain.cpp b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/classes/eltwise_chain.cpp
index 5931acc414a..04c642e333b 100644
--- a/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/classes/eltwise_chain.cpp
+++ b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/classes/eltwise_chain.cpp
@@ -47,7 +47,9 @@ std::string EltwiseChainTest::getTestCaseName(const testing::TestParamInfo<Eltwi
return results.str();
}
-ov::Tensor EltwiseChainTest::generate_eltwise_input(const ov::element::Type& type, const ov::Shape& shape) {
+ov::Tensor EltwiseChainTest::generate_eltwise_input(const ov::element::Type& type,
+ const ov::Shape& shape,
+ int32_t seed) {
struct gen_params {
uint32_t range;
int32_t start_from;
@@ -63,6 +65,7 @@ ov::Tensor EltwiseChainTest::generate_eltwise_input(const ov::element::Type& typ
in_data.start_from = params.start_from;
in_data.range = params.range;
in_data.resolution = params.resolution;
+ in_data.seed = seed;
auto tensor = ov::test::utils::create_and_fill_tensor(type, shape, in_data);
return tensor;
}
@@ -72,9 +75,11 @@ void EltwiseChainTest::generate_inputs(const std::vector<ov::Shape>& targetInput
const auto& funcInputs = function->inputs();
for (size_t i = 0; i < funcInputs.size(); ++i) {
const auto& funcInput = funcInputs[i];
- inputs.insert({funcInput.get_node_shared_ptr(), generate_eltwise_input(
- funcInput.get_element_type(),
- targetInputStaticShapes[i])});
+ // A different seed per input, so that inputs of the same shape are not equal
+ inputs.insert({funcInput.get_node_shared_ptr(),
+ generate_eltwise_input(funcInput.get_element_type(),
+ targetInputStaticShapes[i],
+ static_cast<int32_t>(i + 1))});
}
}
@@ -113,10 +118,13 @@ void EltwiseChainTest::SetUp() {
(conversion == ov::element::dynamic)
? param
: std::dynamic_pointer_cast<ov::Node>(std::make_shared<ov::op::v0::Convert>(param, conversion));
+ // As for constant secondary inputs, the first input starts the chain
+ // and the following ones are the second inputs of the operations
if (inputNodes1.empty()) {
inputNodes1.push_back(inputNode);
+ } else {
+ inputNodes2.push_back(inputNode);
}
- inputNodes2.push_back(inputNode);
}
} else {
paramVec = ov::ParameterVector {std::make_shared<ov::op::v0::Parameter>(inputPrecisions[0], inputDynamicShapes.front())};
diff --git a/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/classes/eltwise_chain.hpp b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/classes/eltwise_chain.hpp
index 30b743cffe3..e96a5993f31 100644
--- a/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/classes/eltwise_chain.hpp
+++ b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/classes/eltwise_chain.hpp
@@ -60,7 +60,7 @@ class EltwiseChainTest : public testing::WithParamInterface<EltwiseChainTuple>,
virtual public SubgraphBaseTest {
public:
static std::string getTestCaseName(const testing::TestParamInfo<EltwiseChainTuple> &obj);
- ov::Tensor generate_eltwise_input(const ov::element::Type& type, const ov::Shape& shape);
+ ov::Tensor generate_eltwise_input(const ov::element::Type& type, const ov::Shape& shape, int32_t seed);
void generate_inputs(const std::vector<ov::Shape>& targetInputStaticShapes) override;
protected:
diff --git a/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/x64/eltwise_chain.cpp b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/x64/eltwise_chain.cpp
index cf432403a88..dabb902fe5a 100644
--- a/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/x64/eltwise_chain.cpp
+++ b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/x64/eltwise_chain.cpp
@@ -41,6 +41,33 @@ INSTANTIATE_TEST_SUITE_P(smoke_EltwiseChain_Comparison,
::testing::Values(ov::test::utils::DEVICE_CPU)),
EltwiseChainTest::getTestCaseName);
+// An 8-bit SquaredDifference is computed in i32 by the snippets kernel.
+// The parameters are in [10, 20), so the result never leaves the 8-bit range.
+// The chain must run as a snippets Subgraph: the Eltwise node would leave the emitter untested.
+class EltwiseChainSnippetsTest : public EltwiseChainTest {
+protected:
+ void validate() override {
+ EltwiseChainTest::validate();
+ CheckNumberOfNodesWithType(compiledModel, "Subgraph", 1);
+ }
+};
+
+TEST_P(EltwiseChainSnippetsTest, CompareWithRefs) {
+ run();
+}
+
+INSTANTIATE_TEST_SUITE_P(smoke_EltwiseChain_SquaredDifference_8bit,
+ EltwiseChainSnippetsTest,
+ ::testing::Combine(::testing::ValuesIn(static_shapes_to_test_representation(inputShapes())),
+ ::testing::Values(InputLayerType::PARAMETER),
+ ::testing::Values(std::vector<ElementType>(4, ElementType::i8),
+ std::vector<ElementType>(4, ElementType::u8)),
+ ::testing::Values(std::vector<EltwiseChainOp>{EltwiseTypes::SQUARED_DIFF}),
+ ::testing::Values(PostNode::EMPTY),
+ ::testing::Values(ov::element::dynamic),
+ ::testing::Values(ov::test::utils::DEVICE_CPU)),
+ EltwiseChainTest::getTestCaseName);
+
} // namespace
} // namespace test
} // namespace ov