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
[IR FE] Enhance pre-processing by check if weight provider available (#37669)
Pawel Raaszea3f2f20fb53
diff --git a/src/frontends/ir/src/input_model.cpp b/src/frontends/ir/src/input_model.cppindex 6bac9a0f41b..0fc17519c36 100644--- a/src/frontends/ir/src/input_model.cpp+++ b/src/frontends/ir/src/input_model.cpp@@ -123,6 +123,7 @@ void parse_pre_process(pugi::xml_node& root,mean_scalar_values.insert({chanNo, ov::util::pugixml::get_float_attr(meanNode, "value")});}if (meanNode.attribute("size") && meanNode.attribute("offset")) {+ OPENVINO_ASSERT(weights_provider, "Weights are required, but not provided");auto const_size = ov::util::pugixml::get_uint64_attr(meanNode, "size");auto const_offset = ov::util::pugixml::get_uint64_attr(meanNode, "offset");if (shape_size(mean_shape) * input_type.size() != const_size) {diff --git a/src/frontends/ir/tests/frontend_test.hpp b/src/frontends/ir/tests/frontend_test.hppindex bb51e1683f7..9349f3916ea 100644--- a/src/frontends/ir/tests/frontend_test.hpp+++ b/src/frontends/ir/tests/frontend_test.hpp@@ -5,6 +5,7 @@#include <gtest/gtest.h>#include <fstream>+#include <optional>#include "common_test_utils/common_utils.hpp"#include "common_test_utils/graph_comparator.hpp"@@ -21,16 +22,23 @@ protected:std::filesystem::path xmlFileName = std::filesystem::path(prefix).concat("_IrFrontendTestModel.xml");std::filesystem::path binFileName = std::filesystem::path(prefix).concat("_IrFrontendTestModel.bin");+ /**+ * Creates temporary XML and BIN files for the IR model.+ * @param xmlFileContent Content of the XML file.+ * @param binFileContent Optional content of the BIN file. If not provided, the BIN file is not created.+ */void createTemporalModelFile(std::string xmlFileContent,- std::vector<unsigned char> binFileContent = std::vector<unsigned char>()) {+ std::optional<std::vector<unsigned char>> binFileContent = std::nullopt) {ASSERT_TRUE(xmlFileContent.size() > 0);if (std::ofstream xml_file(xmlFileName); xml_file.is_open()) {xml_file << xmlFileContent;}- if (std::ofstream bin_file(binFileName, std::ios::binary); bin_file.is_open()) {- bin_file.write(reinterpret_cast<const char*>(binFileContent.data()), binFileContent.size());+ if (binFileContent) {+ if (std::ofstream bin_file(binFileName, std::ios::binary); bin_file.is_open()) {+ bin_file.write(reinterpret_cast<const char*>(binFileContent->data()), binFileContent->size());+ }}}diff --git a/src/frontends/ir/tests/pre_processing_deserialization.cpp b/src/frontends/ir/tests/pre_processing_deserialization.cppindex e912813643f..3f8591313d6 100644--- a/src/frontends/ir/tests/pre_processing_deserialization.cpp+++ b/src/frontends/ir/tests/pre_processing_deserialization.cpp@@ -74,13 +74,93 @@ TEST_F(IRFrontendTestsPreProcessing, pre_processing) {ASSERT_TRUE(!!model);}-namespace ov {-namespace test {+namespace ov::test {using testing::ElementsAre;using testing::Property;using testing::UnorderedElementsAre;+namespace {+std::string build_model_with_pre_process(const std::string& channels_xml) {+ return R"V0G0N(<?xml version="1.0" ?>+<net name="Network" version="10">+ <pre-process mean-precision="FP32" reference-layer-name="input">+)V0G0N" + channels_xml ++ R"V0G0N(+ </pre-process>+ <layers>+ <layer name="input" type="Parameter" id="0" version="opset1">+ <data shape="1,3,22,22" element_type="f32"/>+ <output>+ <port id="0" precision="FP32">+ <dim>1</dim>+ <dim>3</dim>+ <dim>22</dim>+ <dim>22</dim>+ </port>+ </output>+ </layer>+ <layer name="output" type="Result" id="1" version="opset1">+ <input>+ <port id="0" precision="FP32">+ <dim>1</dim>+ <dim>3</dim>+ <dim>22</dim>+ <dim>22</dim>+ </port>+ </input>+ </layer>+ </layers>+ <edges>+ <edge from-layer="0" from-port="0" to-layer="1" to-port="0"/>+ </edges>+</net>+)V0G0N";+}+} // namespace++TEST_F(IRFrontendTestsPreProcessing, pre_processing_offset_without_weights_throws) {+ const auto xml_model = build_model_with_pre_process(R"V0G0N(+ <channel id="0">+ <mean offset="0" size="1936"/>+ </channel>+ <channel id="1">+ <mean offset="1936" size="1936"/>+ </channel>+ <channel id="2">+ <mean offset="3872" size="1936"/>+ </channel>)V0G0N");++ createTemporalModelFile(xml_model, std::nullopt);++ OV_EXPECT_THROW_HAS_SUBSTRING(core.read_model(xmlFileName), Exception, "Weights are required");+}++TEST_F(IRFrontendTestsPreProcessing, pre_processing_mean_value_without_weights_still_applied) {+ const auto xml_model = build_model_with_pre_process(R"V0G0N(+ <channel id="0">+ <mean value="1.1"/>+ </channel>+ <channel id="1">+ <mean value="2.2"/>+ </channel>+ <channel id="2">+ <mean value="3.3"/>+ </channel>)V0G0N");++ createTemporalModelFile(xml_model, std::nullopt);++ std::shared_ptr<Model> model;+ OV_ASSERT_NO_THROW(model = core.read_model(xmlFileName));+ ASSERT_TRUE(!!model);++ const auto param = model->get_parameters().at(0);+ const auto consumers = param->output(0).get_target_inputs();+ ASSERT_EQ(consumers.size(), 1u);+ const auto consumer_node = consumers.begin()->get_node();+ EXPECT_EQ(std::string(consumer_node->get_type_name()), "Subtract");+}+TEST_F(IRFrontendTestsPreProcessing, check_tensor_names_after_read_and_pre_post_processing) {std::string xml_model = R"V0G0N(<?xml version="1.0" ?>@@ -168,5 +248,4 @@ TEST_F(IRFrontendTestsPreProcessing, check_tensor_names_after_read_and_pre_post_// After PPP (inserts convert node) the tensor names stay on model's input.Property("Output 1", &Output<Node>::get_names, testing::IsEmpty())));}-} // namespace test-} // namespace ov+} // namespace ov::test