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

[core] Result node may uses inputs names on creation #28168

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2441,9 +2441,6 @@ TEST(TransformationTests, ConvertPrecisionExplicitConvertsSingleNodeMultipleOutp
auto convert_1 = make_shared<opset10::Convert>(param_1, element::f32);
auto axis = opset10::Constant::create(element::i32, Shape{}, {0});
auto split = make_shared<opset10::Split>(convert_1, axis, 3);
split->get_output_tensor(0).add_names({"split:0"});
split->get_output_tensor(1).add_names({"split:1"});
split->get_output_tensor(2).add_names({"split:2"});

auto convert_split_0 = make_shared<opset10::Convert>(split->output(0), element::f64);
auto convert_split_1 = make_shared<opset10::Convert>(split->output(1), element::f64);
Expand Down Expand Up @@ -2567,7 +2564,6 @@ TEST(TransformationTests, ConvertPrecisionExplicitConvertsMultiSubgraphs) {
if_op->set_input(convert_1, param_1_then, param_1_else);
if_op->set_input(convert_2, param_2_then, param_2_else);
auto result = if_op->set_output(result_then, result_else);
result.add_names({"if_result:0"});
auto converted_result = make_shared<opset10::Convert>(result, element::f64);
converted_result->get_output_tensor(0).add_names({"if_result:0"});

Expand Down
3 changes: 2 additions & 1 deletion src/core/include/openvino/op/result.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ class OPENVINO_API Result : public Op {
/// \brief Allows a value to be used as a function result.
///
/// \param arg Node that produces the input tensor.
Result(const Output<Node>& arg);
/// \param use_input_names When true Result will use input node tensor names as output names (Default: false).
Result(const Output<Node>& arg, bool use_input_names = false);

void validate_and_infer_types() override;

Expand Down
2 changes: 1 addition & 1 deletion src/core/src/node_vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ ov::ResultVector ov::as_result_vector(const OutputVector& values) {
for (const auto& value : values) {
std::shared_ptr<Node> node = value.get_node_shared_ptr();
result.push_back(ov::is_type<ov::op::v0::Result>(node) ? ov::as_type_ptr<ov::op::v0::Result>(node)
: std::make_shared<ov::op::v0::Result>(value));
: std::make_shared<ov::op::v0::Result>(value, true));
Copy link
Contributor

Choose a reason for hiding this comment

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

should you also fix ov::Model::add_output ?

}
return result;
}
8 changes: 7 additions & 1 deletion src/core/src/op/result.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@

#include "itt.hpp"
#include "openvino/core/descriptor_tensor.hpp"
#include "openvino/op/util/op_types.hpp"

namespace ov {
namespace op {
namespace v0 {

Result::Result(const Output<Node>& arg) : Op({arg}) {
Result::Result(const Output<Node>& arg, bool use_input_names) : Op({arg}) {
constructor_validate_and_infer_types();

if (use_input_names && !util::is_parameter(arg.get_node())) {
// On create use inputs names which will be used as model output names (except Paramater, model's inputs names).
get_output_tensor(0).add_names(get_input_tensor(0).get_names());
}
}

void Result::validate_and_infer_types() {
Expand Down
47 changes: 45 additions & 2 deletions src/core/tests/preprocess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2096,12 +2096,55 @@ TEST(pre_post_process, postprocess_one_node_many_outputs) {
p.output(2).tensor().set_element_type(element::f32);
model = p.build();
EXPECT_EQ(model->get_results().size(), 3);
// Tensor names on output is lost as origin named tensor is before convert op
// Tensor names on modified outputs are set to Split tensors not model output.
// New result has different precision means different tensor.
EXPECT_EQ(model->output(0).get_tensor().get_names().count("tensor_Split0"), 0);
EXPECT_EQ(model->output(2).get_tensor().get_names().count("tensor_Split2"), 0);
// Add output node still on output after pre-processing
EXPECT_EQ(model->output(0).get_tensor().get_names().count("output_split0"), 1);
// Not modified output still have name
EXPECT_EQ(model->output(1).get_tensor().get_names().count("tensor_Split1"), 1);
EXPECT_EQ(model->output(2).get_tensor().get_names().count("tensor_Split2"), 0);
EXPECT_EQ(model->get_results()[0]->input(0).get_source_output().get_node()->get_friendly_name(), "Split.0");
EXPECT_EQ(model->get_results()[1]->input(0).get_source_output().get_node()->get_friendly_name(), "Split");
EXPECT_EQ(model->get_results()[2]->input(0).get_source_output().get_node()->get_friendly_name(), "Split.2");
}

TEST(pre_post_process, postprocess_one_node_many_outputs_results_created_by_model) {
auto data1 = std::make_shared<op::v0::Parameter>(element::i32, Shape{3});
auto c1 = opset8::Constant::create(element::i32, Shape{}, {0});
auto op = std::make_shared<opset8::Split>(data1, c1, 3);
op->set_friendly_name("Split");
op->output(0).set_names({"tensor_Split0"});
auto r1 = std::make_shared<op::v0::Result>(op->output(0));

OutputVector outputs{r1};
for (size_t i = 1; i < op->get_num_splits(); i++) {
auto output = op->output(i);
output.set_names({"tensor_Split" + std::to_string(i)});
outputs.push_back(std::move(output));
}
auto model = std::make_shared<Model>(outputs, ParameterVector{data1});
// Set tensor name to model output 0
model->output(0).set_names({"output_split0"});
EXPECT_EQ(model->output(0).get_tensor().get_names().count("output_split0"), 1);
// Result input has still tensor_split0 names from split op
EXPECT_EQ(model->output(0).get_node()->get_input_tensor(0).get_names().count("tensor_Split0"), 1);
EXPECT_EQ(model->output(1).get_tensor().get_names().count("tensor_Split1"), 1);
EXPECT_EQ(model->output(2).get_tensor().get_names().count("tensor_Split2"), 1);

auto p = PrePostProcessor(model);
p.output(0).tensor().set_element_type(element::f32);
p.output(2).tensor().set_element_type(element::f32);
model = p.build();
EXPECT_EQ(model->get_results().size(), 3);

// output 0 by user,not use input nodes names as its own, modified by PPP (tensor_Split0 is on split output)
EXPECT_EQ(model->output(0).get_tensor().get_names().count("tensor_Split0"), 0);
EXPECT_EQ(model->output(0).get_tensor().get_names().count("output_split0"), 1);
// output 1 created by model, assume its names is output name, not modified by PPP
EXPECT_EQ(model->output(1).get_tensor().get_names().count("tensor_Split1"), 1);
// output 2 created by model, assume its names is output name, modified by PPP
EXPECT_EQ(model->output(2).get_tensor().get_names().count("tensor_Split2"), 1);
EXPECT_EQ(model->get_results()[0]->input(0).get_source_output().get_node()->get_friendly_name(), "Split.0");
EXPECT_EQ(model->get_results()[1]->input(0).get_source_output().get_node()->get_friendly_name(), "Split");
EXPECT_EQ(model->get_results()[2]->input(0).get_source_output().get_node()->get_friendly_name(), "Split.2");
Expand Down
21 changes: 20 additions & 1 deletion src/core/tests/type_prop/result.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace ov {
namespace test {

using ov::op::v0::Constant;
using ov::op::v0::Parameter;
using std::make_shared;
using testing::UnorderedElementsAre;
Expand Down Expand Up @@ -135,7 +136,7 @@ TEST_F(TypePropResultV0Test, preserve_specific_name_on_input_replace) {
const auto a = std::make_shared<Parameter>(element::f32, PartialShape::dynamic());
a->get_output_tensor(0).set_names({"input a"});

const auto result = make_op(a);
const auto result = make_op(a, true);
result->output(0).set_names({"out"});

EXPECT_THAT(result->input(0).get_tensor().get_names(), UnorderedElementsAre("out", "input a"));
Expand All @@ -151,5 +152,23 @@ TEST_F(TypePropResultV0Test, preserve_specific_name_on_input_replace) {
EXPECT_THAT(result->output(0).get_names(), UnorderedElementsAre("out"));
EXPECT_THAT(a->output(0).get_names(), UnorderedElementsAre("input a"));
}

TEST_F(TypePropResultV0Test, take_input_node_names) {
const auto c = std::make_shared<Constant>(element::f32, Shape{2}, std::vector<float>{2.f, 1.f});
c->get_output_tensor(0).set_names({"constant data"});
const auto result = make_op(c, true);

EXPECT_THAT(result->input(0).get_tensor().get_names(), UnorderedElementsAre("constant data"));
EXPECT_THAT(result->output(0).get_names(), UnorderedElementsAre("constant data"));

const auto new_const = std::make_shared<Constant>(element::f32, Shape{2}, std::vector<float>{0.f, 0.f});

result->input(0).replace_source_output(new_const);
result->validate_and_infer_types();

EXPECT_THAT(c->get_output_tensor(0).get_names(), testing::IsEmpty());
EXPECT_THAT(result->get_input_tensor(0).get_names(), UnorderedElementsAre("constant data"));
EXPECT_THAT(result->get_output_tensor(0).get_names(), UnorderedElementsAre("constant data"));
}
} // namespace test
} // namespace ov
2 changes: 1 addition & 1 deletion src/frontends/ir/src/ir_deserializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1033,7 +1033,7 @@ std::shared_ptr<ov::Node> ov::XmlDeserializer::create_node(const std::vector<ov:
if (!ov::op::util::is_parameter(result->get_input_source_output(0).get_node())) {
// Copy names if parent node is not parameter, model's input names should not be dedicated
// output names as they could be removed from Parameter's tensor during model transformations.
ov::descriptor::copy_tensor_names(result->get_output_tensor(0), result->get_input_tensor(0));
result->get_output_tensor(0).add_names(result->get_input_tensor(0).get_names());
}
}
}
Expand Down
7 changes: 0 additions & 7 deletions src/frontends/onnx/frontend/src/input_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -533,13 +533,6 @@ void InputModel::add_tensor_names(std::shared_ptr<Model>& model) {
it->add_names(tensor_names.second);
}
}

// Set model output names
for (auto&& result : model->get_results()) {
if (!is_type<op::v0::Parameter>(result->get_input_source_output(0).get_node())) {
result->get_output_tensor(0).add_names(result->get_input_tensor(0).get_names());
}
}
}

void InputModel::reshape_model_inputs(std::shared_ptr<Model>& model) {
Expand Down
Loading