OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[CPU][CORE] EmbeddingBagOffset: added a guard against non-monotonic offsets (#35232)
Vladislav Golubev3a556a3f98db
diff --git a/src/core/reference/include/openvino/reference/embedding_bag_offsets.hpp b/src/core/reference/include/openvino/reference/embedding_bag_offsets.hppindex fe4b39522ec..32cdf63a234 100644--- a/src/core/reference/include/openvino/reference/embedding_bag_offsets.hpp+++ b/src/core/reference/include/openvino/reference/embedding_bag_offsets.hpp@@ -45,8 +45,19 @@ void embeddingBagOffsets(const T* emb_table,if (emb_index == offsets_size - 1lu)indices_num = indices_count - offsets[emb_index];- else+ else {+ // Guard against non-monotonic offsets: a decrease would underflow size_t+ OPENVINO_ASSERT(offsets[emb_index + 1lu] >= offsets[emb_index],+ "Offsets must be monotonically non-decreasing: offsets[",+ emb_index + 1lu,+ "]=",+ offsets[emb_index + 1lu],+ " < offsets[",+ emb_index,+ "]=",+ offsets[emb_index]);indices_num = offsets[emb_index + 1lu] - offsets[emb_index];+ }if (indices_num != 0lu) {indices_ref = indices + offsets[emb_index];diff --git a/src/plugins/intel_cpu/src/nodes/embedding_bag_offsets.cpp b/src/plugins/intel_cpu/src/nodes/embedding_bag_offsets.cppindex 4a5df2bdbfb..47e61c2e0d3 100644--- a/src/plugins/intel_cpu/src/nodes/embedding_bag_offsets.cpp+++ b/src/plugins/intel_cpu/src/nodes/embedding_bag_offsets.cpp@@ -146,6 +146,16 @@ void EmbeddingBagOffset::getIndices(size_t embIndex,if (embIndex == _offsetsLen - 1LU) {size = _indicesLen - offsetsData_[embIndex];} else {+ // Guard against non-monotonic offsets: a decrease would underflow size_t+ CPU_NODE_ASSERT(offsetsData_[embIndex + 1LU] >= offsetsData_[embIndex],+ "Offsets must be monotonically non-decreasing: offsets[",+ embIndex + 1LU,+ "]=",+ offsetsData_[embIndex + 1LU],+ " < offsets[",+ embIndex,+ "]=",+ offsetsData_[embIndex]);size = offsetsData_[embIndex + 1LU] - offsetsData_[embIndex];}diff --git a/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/embedding_bag_offsets.cpp b/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/embedding_bag_offsets.cppindex 11b4c5c069f..072ebb946ac 100644--- a/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/embedding_bag_offsets.cpp+++ b/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/embedding_bag_offsets.cpp@@ -83,6 +83,18 @@ TEST_P(EmbeddingBagOffsetsLayerCPUTest, CompareWithRefs) {CheckPluginRelatedResults(compiledModel, "embeddingBagOffsets");}+class EmbeddingBagOffsetsLayerNegativeTest : public EmbeddingBagOffsetsLayerCPUTest {};++TEST_P(EmbeddingBagOffsetsLayerNegativeTest, CompareWithRefsNegative) {+ bool exception_caught = false;+ set_callback_exception([&exception_caught](const std::exception& ex) {+ exception_caught = true;+ EXPECT_NE(dynamic_cast<const ov::Exception*>(&ex), nullptr) << "Expected ov::Exception but got: " << ex.what();+ });+ run();+ EXPECT_TRUE(exception_caught) << "Expected an ov::Exception to be thrown for non-monotonic offsets";+}+namespace {const std::vector<ElementType> netPrecisions = {ElementType::f32, ElementType::i32, ElementType::u8};@@ -145,6 +157,24 @@ INSTANTIATE_TEST_SUITE_P(smoke_EmbeddingBagOffsets_No_Weights,::testing::ValuesIn(indPrecisions),::testing::Values(ov::test::utils::DEVICE_CPU)),EmbeddingBagOffsetsLayerCPUTest::getTestCaseName);++const auto embBagOffsetArgSetNegative = ::testing::Combine(+ ::testing::Values(InputShape{{5, 6}, {{5, 6}}}),+ ::testing::Values(std::vector<size_t>{0, 2, 3, 4}),+ ::testing::Values(std::vector<size_t>{0, 3, 2}), // offsets (non-monotonic: 2 < 3)+ ::testing::Values(0),+ ::testing::Values(false),+ ::testing::Values(false),+ ::testing::Values(ov::op::util::EmbeddingBagOffsetsBase::Reduction::SUM)+);++INSTANTIATE_TEST_SUITE_P(smoke_EmbeddingBagOffsets_Negative,+ EmbeddingBagOffsetsLayerNegativeTest,+ ::testing::Combine(embBagOffsetArgSetNegative,+ ::testing::ValuesIn(netPrecisions),+ ::testing::ValuesIn(indPrecisions),+ ::testing::Values(ov::test::utils::DEVICE_CPU)),+ EmbeddingBagOffsetsLayerCPUTest::getTestCaseName);} // namespace} // namespace test} // namespace ovdiff --git a/src/plugins/template/tests/functional/op_reference/embeddingbag_offsets.cpp b/src/plugins/template/tests/functional/op_reference/embeddingbag_offsets.cppindex 27cc2240fcd..2f1f42c6515 100644--- a/src/plugins/template/tests/functional/op_reference/embeddingbag_offsets.cpp+++ b/src/plugins/template/tests/functional/op_reference/embeddingbag_offsets.cpp@@ -307,3 +307,25 @@ INSTANTIATE_TEST_SUITE_P(ov::op::v15::EmbeddingBagOffsets::Reduction::MEAN,std::make_shared<ov::op::v0::Constant>(element::i32, ov::Shape(), std::vector<int32_t>{-1}))),ReferenceEmbeddingBagOffsetsLayerTest::getTestCaseName);++class ReferenceEmbeddingBagOffsetsLayerNegativeTest : public ReferenceEmbeddingBagOffsetsLayerTest {};++TEST_P(ReferenceEmbeddingBagOffsetsLayerNegativeTest, InvalidEmbeddingBagOffsetsThrows) {+ EXPECT_THROW(Exec(), ov::Exception);+}++INSTANTIATE_TEST_SUITE_P(+ smoke_EmbeddingBagOffsets_Negative,+ ReferenceEmbeddingBagOffsetsLayerNegativeTest,+ ::testing::Values(EmbeddingBagOffsetsParams(+ ov::Shape{5, 2},+ ov::element::f32,+ std::vector<float>{-0.2, -0.6, -0.1, -0.4, -1.9, -1.8, -1., 1.5, 0.8, -0.7},+ ov::Shape{}, // Dummy output shape+ ov::element::f32,+ std::vector<float>(), // Dummy output data+ std::make_shared<ov::op::v0::Constant>(element::i32, ov::Shape({4}), std::vector<int32_t>{0, 2, 3, 4}),+ // non-monotonic offsets: offsets[2] = 2 < offsets[1] = 3 violates monotonic property+ std::make_shared<ov::op::v0::Constant>(element::i32, ov::Shape({3}), std::vector<int32_t>{0, 3, 2}),+ ov::op::v15::EmbeddingBagOffsets::Reduction::SUM)),+ ReferenceEmbeddingBagOffsetsLayerTest::getTestCaseName);