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

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
2 changes: 2 additions & 0 deletions src/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -1948,6 +1948,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
9 changes: 8 additions & 1 deletion 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 @@ -305,9 +306,14 @@ static Status applyLayoutConfiguration(const ModelConfig& config, std::shared_pt
}

OV_LOGGER("ov::Model: {}, model->outputs()", reinterpret_cast<void*>(model.get()));
for (const ov::Output<ov::Node>& output : model->outputs()) {
size_t outputIndex = 0;
for (ov::Output<ov::Node>& output : model->outputs()) {
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 change required?

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)};
Copy link
Collaborator

Choose a reason for hiding this comment

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

Shouldn't we use lowercase? Isn't it a standard in input/output naming?

output.add_names(dummy_name);
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is not logic related to "applyLayoutConfiguration". Move those changes outside of this method.

}
std::string name = output.get_any_name();
std::string mappedName = config.getMappingOutputByKey(name).empty() ? name : config.getMappingOutputByKey(name);
if (config.getLayouts().count(mappedName) > 0) {
Expand Down Expand Up @@ -355,6 +361,7 @@ static Status applyLayoutConfiguration(const ModelConfig& config, std::shared_pt
modelVersion);
return StatusCode::UNKNOWN_ERROR;
}
outputIndex++;
}

try {
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("INPUT1"), 1);
EXPECT_EQ(modelInstance.getInputsInfo().count("INPUT2"), 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="INPUT1" type="Parameter" version="opset1">
<data shape="1,10" element_type="i8" />
<output>
<port id="0" precision="I8" names="INPUT1">
<dim>1</dim>
<dim>10</dim>
</port>
</output>
</layer>
<layer id="0" name="INPUT2" type="Parameter" version="opset1">
<data shape="1,10" element_type="i8" />
<output>
<port id="0" precision="I8" names="INPUT2">
<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 @@ -77,6 +77,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 = std::filesystem::current_path().u8string() + "/src/test/no_name_output";
Copy link
Collaborator

Choose a reason for hiding this comment

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

This will likely not work on windows, see usage of getGenericFullPathForSrcTest above


const ovms::ModelConfig DUMMY_MODEL_CONFIG{
"dummy",
Expand Down Expand Up @@ -213,6 +214,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) 2020 Intel Corporation
Copy link
Collaborator

Choose a reason for hiding this comment

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

Change year

#
# 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="INPUT1")
in1 = ov.opset1.parameter(shape=batch_dim + shape, dtype=dtype, name="INPUT2")
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()} {out.get_any_name()}")

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({"INPUT1": input_data, "INPUT2": input_data})

Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe we could have some assertion here instead of prints?
I suppose the goal of this script is to generate a model and check if it's valid. In such case checking if output looks as expected would be good.

print(input_data)
print(results)