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]MatrixNMS allow zero output box (#38729)
Chenhu Wang252322cae70a
diff --git a/src/plugins/intel_cpu/src/nodes/matrix_nms.cpp b/src/plugins/intel_cpu/src/nodes/matrix_nms.cppindex c4297d32e1b..987858a0448 100644--- a/src/plugins/intel_cpu/src/nodes/matrix_nms.cpp+++ b/src/plugins/intel_cpu/src/nodes/matrix_nms.cpp@@ -432,7 +432,7 @@ void MatrixNms::execute([[maybe_unused]] const dnnl::stream& strm) {// And on the other hand in case of api 2.0, keep them internal dynamic for better performance and functionality.if (!m_outStaticShape) {auto totalBox = std::accumulate(m_numPerBatch.begin(), m_numPerBatch.end(), int64_t{0});- CPU_NODE_ASSERT(totalBox > 0, "Total number of boxes is less or equal than 0");+ CPU_NODE_ASSERT(totalBox >= 0, "Total number of boxes is negative: ", totalBox);redefineOutputMemory({{static_cast<size_t>(totalBox), 6}, {static_cast<size_t>(totalBox), 1}, {m_numBatches}});}auto* selectedOutputs = selectedOutputsMemPtr->getDataAs<float>();diff --git a/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/common/matrix_nms_zero_boxes.cpp b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/common/matrix_nms_zero_boxes.cppnew file mode 100644index 00000000000..ab495a89673--- /dev/null+++ b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/common/matrix_nms_zero_boxes.cpp@@ -0,0 +1,58 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "openvino/op/matrix_nms.hpp"+#include "openvino/op/parameter.hpp"+#include "openvino/op/result.hpp"+#include "shared_test_classes/base/ov_subgraph.hpp"++namespace ov {+namespace test {++// MatrixNms must not throw when none of the boxes pass the score/post threshold+// and the resulting output is legitimately empty (0 selected boxes).+class MatrixNmsZeroBoxes : public SubgraphBaseTest {+protected:+ void SetUp() override {+ targetDevice = ov::test::utils::DEVICE_CPU;++ const ov::Shape boxesShape{1, 3, 4};+ const ov::Shape scoresShape{1, 2, 3};+ init_input_shapes({{{}, {boxesShape}}, {{}, {scoresShape}}});++ auto boxes = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, boxesShape);+ auto scores = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, scoresShape);++ ov::op::v8::MatrixNms::Attributes attrs;+ // All generated scores are 0, so a positive score_threshold guarantees+ // every box is filtered out and the op must produce an empty (but valid) output.+ attrs.score_threshold = 0.5f;+ attrs.output_type = ov::element::i32;++ auto nms = std::make_shared<ov::op::v8::MatrixNms>(boxes, scores, attrs);++ ov::ResultVector results;+ for (const auto& output : nms->outputs()) {+ results.push_back(std::make_shared<ov::op::v0::Result>(output));+ }+ function = std::make_shared<ov::Model>(results, ov::ParameterVector{boxes, scores}, "MatrixNmsZeroBoxes");+ }++ void generate_inputs(const std::vector<ov::Shape>& targetInputStaticShapes) override {+ inputs.clear();+ const auto& funcInputs = function->inputs();+ for (size_t i = 0; i < funcInputs.size(); ++i) {+ ov::Tensor tensor(funcInputs[i].get_element_type(), targetInputStaticShapes[i]);+ std::fill_n(tensor.data<float>(), tensor.get_size(), 0.0f);+ inputs.insert({funcInputs[i].get_node_shared_ptr(), tensor});+ }+ }+};++TEST_F(MatrixNmsZeroBoxes, smoke_CompareWithRefs) {+ run();+}++} // namespace test+} // namespace ovdiff --git a/src/plugins/intel_cpu/tests/functional/sources.cmake b/src/plugins/intel_cpu/tests/functional/sources.cmakeindex a3ce7d1e11e..873a16640de 100644--- a/src/plugins/intel_cpu/tests/functional/sources.cmake+++ b/src/plugins/intel_cpu/tests/functional/sources.cmake@@ -248,6 +248,7 @@ set(CPU_FUNC_TESTS_CUSTOM_SRCS${CMAKE_CURRENT_LIST_DIR}/custom/subgraph_tests/src/common/lora_pattern.cpp${CMAKE_CURRENT_LIST_DIR}/custom/subgraph_tests/src/common/matmul_decompress_convert.cpp${CMAKE_CURRENT_LIST_DIR}/custom/subgraph_tests/src/common/matmul_strided_inputs_outputs.cpp+ ${CMAKE_CURRENT_LIST_DIR}/custom/subgraph_tests/src/common/matrix_nms_zero_boxes.cpp${CMAKE_CURRENT_LIST_DIR}/custom/subgraph_tests/src/common/merge_transpose_reorder.cpp${CMAKE_CURRENT_LIST_DIR}/custom/subgraph_tests/src/common/ngram.cpp${CMAKE_CURRENT_LIST_DIR}/custom/subgraph_tests/src/common/not_fused_conv_simple_op.cpp@@ -784,6 +785,7 @@ set(TMP_LIST_OF_COMMON_SUBGRAPH_TESTS${CMAKE_CURRENT_LIST_DIR}/custom/subgraph_tests/src/common/lora_pattern.cpp${CMAKE_CURRENT_LIST_DIR}/custom/subgraph_tests/src/common/matmul_decompress_convert.cpp${CMAKE_CURRENT_LIST_DIR}/custom/subgraph_tests/src/common/matmul_strided_inputs_outputs.cpp+ ${CMAKE_CURRENT_LIST_DIR}/custom/subgraph_tests/src/common/matrix_nms_zero_boxes.cpp${CMAKE_CURRENT_LIST_DIR}/custom/subgraph_tests/src/common/merge_transpose_reorder.cpp${CMAKE_CURRENT_LIST_DIR}/custom/subgraph_tests/src/common/ngram.cpp${CMAKE_CURRENT_LIST_DIR}/custom/subgraph_tests/src/common/not_fused_conv_simple_op.cpp