Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for model outputs without names #2850

Merged
merged 19 commits into from
Jan 23, 2025
2 changes: 2 additions & 0 deletions src/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -1933,6 +1933,8 @@ cc_test(
"test/dummy_tflite/1/dummy.tflite",
"test/dummyUppercase/1/dummy.xml",
"test/dummyUppercase/1/dummy.bin",
"test/no_name_output/1/model.xml",
"test/no_name_output/1/model.bin",
"test/increment_1x3x4x5/1/increment_1x3x4x5.xml",
"test/increment_1x3x4x5/1/increment_1x3x4x5.bin",
"test/mediapipe/config_mediapipe_openai_chat_completions_mock.json",
Expand Down
32 changes: 32 additions & 0 deletions src/modelinstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <set>
#include <string>
#include <thread>
#include <unordered_set>
#include <utility>

// TODO windows
Expand Down Expand Up @@ -408,12 +409,43 @@ ov::AnyMap ModelInstance::getRTInfo() {
return anyMap;
}

Status ModelInstance::adjustForEmptyOutputNames() {
size_t outputIndex = 0;
for (ov::Output<ov::Node>& output : this->model->outputs()) {
try {
OV_LOGGER("ov::Output<ov::Node> output: {}, output.get_any_name()", reinterpret_cast<const void*>(&output));
if (output.get_names().size() == 0) {
std::unordered_set<std::string> dummy_name{"out_" + std::to_string(outputIndex)};
output.add_names(dummy_name);
}
} catch (const ov::Exception& e) {
SPDLOG_LOGGER_ERROR(modelmanager_logger, "Failed to set the missing name in output for model:{}; version:{}; Error:{}",
getName(),
getVersion(),
e.what());
return StatusCode::UNKNOWN_ERROR;
} catch (...) {
SPDLOG_LOGGER_ERROR(modelmanager_logger, "Failed to set the missing name in output for model:{}; version:{};",
getName(),
getVersion());
return StatusCode::UNKNOWN_ERROR;
}
outputIndex++;
}
return StatusCode::OK;
}

Status ModelInstance::loadTensors(const ModelConfig& config, bool needsToApplyLayoutConfiguration, const DynamicModelParameter& parameter) {
Status status = validateConfigurationAgainstNetwork(config, this->model);
if (!status.ok()) {
SPDLOG_LOGGER_ERROR(modelmanager_logger, "Error during configuration validation against model");
return status;
}
status = adjustForEmptyOutputNames();
if (!status.ok()) {
SPDLOG_LOGGER_ERROR(modelmanager_logger, "Error during adjusting output names");
return status;
}
if (needsToApplyLayoutConfiguration) {
status = applyLayoutConfiguration(config, this->model, getName(), getVersion());
if (!status.ok()) {
Expand Down
1 change: 1 addition & 0 deletions src/modelinstance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ class ModelInstance {
* @brief Determines if during inference we are able to reset ov::InferRequest output tensor to original state, which is required for setting output functionality to be interoperable with both inferences with and without output set.
*/
void checkForOutputTensorResetAbility();
Status adjustForEmptyOutputNames();
bool supportOutputTensorsReset = true;
bool doesSupportOutputReset() const;
Status gatherReshapeInfo(bool isBatchingModeAuto, const DynamicModelParameter& parameter, bool& isReshapeRequired, std::map<std::string, ov::PartialShape>& modelShapes);
Expand Down
12 changes: 12 additions & 0 deletions src/test/modelinstance_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,18 @@ TEST_F(TestUnloadModel, CanUnloadModelNotHoldingModelInstanceAtPredictPath) {
EXPECT_TRUE(modelInstance.canUnloadInstance());
}

TEST_F(TestUnloadModel, NoNameOutput) {
ovms::ModelInstance modelInstance("UNUSED_NAME", UNUSED_MODEL_VERSION, *ieCore);
ASSERT_EQ(modelInstance.loadModel(NO_NAME_MODEL_CONFIG), ovms::StatusCode::OK);
ASSERT_EQ(ovms::ModelVersionState::AVAILABLE, modelInstance.getStatus().getState());
EXPECT_EQ(modelInstance.getInputsInfo().count("input_1"), 1);
EXPECT_EQ(modelInstance.getInputsInfo().count("input_2"), 1);
EXPECT_EQ(modelInstance.getOutputsInfo().count("out_0"), 1);
EXPECT_EQ(modelInstance.getOutputsInfo().count("out_1"), 1);
modelInstance.retireModel();
EXPECT_EQ(ovms::ModelVersionState::END, modelInstance.getStatus().getState());
}

TEST_F(TestUnloadModel, UnloadWaitsUntilMetadataResponseIsBuilt) {
static std::thread thread;
static std::shared_ptr<ovms::ModelInstance> instance;
Expand Down
Empty file.
86 changes: 86 additions & 0 deletions src/test/no_name_output/1/model.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?xml version="1.0"?>
<net name="no_name_output" version="11">
<layers>
<layer id="1" name="input_1" type="Parameter" version="opset1">
<data shape="1,10" element_type="i8" />
<output>
<port id="0" precision="I8" names="input_1">
<dim>1</dim>
<dim>10</dim>
</port>
</output>
</layer>
<layer id="0" name="input_2" type="Parameter" version="opset1">
<data shape="1,10" element_type="i8" />
<output>
<port id="0" precision="I8" names="input_2">
<dim>1</dim>
<dim>10</dim>
</port>
</output>
</layer>
<layer id="2" name="ADD" type="Add" version="opset1">
<data auto_broadcast="numpy" />
<input>
<port id="0" precision="I8">
<dim>1</dim>
<dim>10</dim>
</port>
<port id="1" precision="I8">
<dim>1</dim>
<dim>10</dim>
</port>
</input>
<output>
<port id="2" precision="I8">
<dim>1</dim>
<dim>10</dim>
</port>
</output>
</layer>
<layer id="4" name="MULTIPLY" type="Multiply" version="opset1">
<data auto_broadcast="numpy" />
<input>
<port id="0" precision="I8">
<dim>1</dim>
<dim>10</dim>
</port>
<port id="1" precision="I8">
<dim>1</dim>
<dim>10</dim>
</port>
</input>
<output>
<port id="2" precision="I8">
<dim>1</dim>
<dim>10</dim>
</port>
</output>
</layer>
<layer id="5" name="Result_5" type="Result" version="opset1">
<input>
<port id="0" precision="I8">
<dim>1</dim>
<dim>10</dim>
</port>
</input>
</layer>
<layer id="3" name="Result_6" type="Result" version="opset1">
<input>
<port id="0" precision="I8">
<dim>1</dim>
<dim>10</dim>
</port>
</input>
</layer>
</layers>
<edges>
<edge from-layer="0" from-port="0" to-layer="2" to-port="0" />
<edge from-layer="0" from-port="0" to-layer="4" to-port="0" />
<edge from-layer="1" from-port="0" to-layer="2" to-port="1" />
<edge from-layer="1" from-port="0" to-layer="4" to-port="1" />
<edge from-layer="2" from-port="2" to-layer="3" to-port="0" />
<edge from-layer="4" from-port="2" to-layer="5" to-port="0" />
</edges>
<rt_info />
</net>
16 changes: 16 additions & 0 deletions src/test/test_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ const std::string passthrough_string_model_location = getGenericFullPathForSrcTe
const std::string dummy_saved_model_location = getGenericFullPathForSrcTest(std::filesystem::current_path().u8string() + "/src/test/dummy_saved_model", false);
const std::string dummy_tflite_location = getGenericFullPathForSrcTest(std::filesystem::current_path().u8string() + "/src/test/dummy_tflite", false);
const std::string scalar_model_location = getGenericFullPathForSrcTest(std::filesystem::current_path().u8string() + "/src/test/scalar", false);
const std::string no_name_output_model_location = getGenericFullPathForSrcTest(std::filesystem::current_path().u8string() + "/src/test/no_name_output", false);

const ovms::ModelConfig DUMMY_MODEL_CONFIG{
"dummy",
Expand Down Expand Up @@ -215,6 +216,21 @@ const ovms::ModelConfig SCALAR_MODEL_CONFIG{
scalar_model_location, // local path
};

const ovms::ModelConfig NO_NAME_MODEL_CONFIG{
"no_name_output",
no_name_output_model_location, // base path
"CPU", // target device
"1", // batchsize
1, // NIREQ
false, // is stateful
true, // idle sequence cleanup enabled
false, // low latency transformation enabled
500, // stateful sequence max number
"", // cache directory
1, // model_version unused since version are read from path
no_name_output_model_location, // local path
};

constexpr const char* DUMMY_MODEL_INPUT_NAME = "b";
constexpr const char* DUMMY_MODEL_OUTPUT_NAME = "a";
constexpr const int DUMMY_MODEL_INPUT_SIZE = 10;
Expand Down
55 changes: 55 additions & 0 deletions tests/models/no_name_output.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#
# Copyright (c) 2025 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this script meant to be used only manually by developers?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a reference how the test model got created in case some changes should be applied.

import openvino.runtime as ov
import numpy as np
import os

batch_dim = []
shape = [1, 10]
dtype = np.int8
model_name = "no_name_output"
model_version_dir = model_name
print(batch_dim + shape)
in0 = ov.opset1.parameter(shape=batch_dim + shape, dtype=dtype, name="input_1")
in1 = ov.opset1.parameter(shape=batch_dim + shape, dtype=dtype, name="input_2")
op0 = ov.opset1.multiply(in1, in0, name="MULTIPLY")
op1 = ov.opset1.add(in1, in0, name="ADD")

model = ov.Model([op0, op1], [in0, in1], model_name)

for idx, inp in enumerate(model.inputs):
print(f"Input {idx}: {inp.get_names()} {inp.get_shape()} {inp.get_index()}")
print(model.outputs)
for idx, out in enumerate(model.outputs):
print(f"Output {idx}: {out.get_names()} {out.get_shape()} {out.get_index()} ")
assert len(out.get_names()) == 0, "number of output names should be 0"

try:
os.makedirs(model_version_dir)
except OSError as ex:
pass # ignore existing dir

ov.serialize(model, model_version_dir + "/model.xml", model_version_dir + "/model.bin")

ov_model = ov.Core().read_model(model_version_dir + "/model.xml")
compiled_model = ov.Core().compile_model(model, "CPU")

input_data = np.ones((1, 10),dtype=np.int8)*10
results = compiled_model({"input_1": input_data, "input_2": input_data})
assert np.all(results[0] == 100), "for inputs np.ones((1, 10), the expected output is 100 in every element: 10*10"
assert np.all(results[1] == 20), "for inputs np.ones((1, 10), the expected output is 20 in every element: 10+10"