forked from openvinotoolkit/openvino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_saved_model.cpp
188 lines (164 loc) · 8.08 KB
/
convert_saved_model.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright (C) 2018-2023 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include <openvino/opsets/opset10.hpp>
#include "common_test_utils/test_common.hpp"
#include "conversion_with_reference.hpp"
#include "gtest/gtest.h"
#include "tf_utils.hpp"
using namespace std;
using namespace ov;
using namespace ov::opset10;
using namespace ov::frontend::tensorflow::tests;
TEST_F(FrontEndConversionWithReferenceTestsF, SavedModelProgramOnly) {
{
model = convert_model("saved_model_program-only");
// check tensor names in the resulted model
unordered_set<string> input_tensor_names = {"y"};
unordered_set<string> output_tensor_names = {"z"};
ASSERT_EQ(model->get_results().size(), 1);
ASSERT_TRUE(model->get_results()[0]->input_value(0).get_names() == output_tensor_names);
ASSERT_EQ(model->get_parameters().size(), 1);
ASSERT_TRUE(model->get_parameters()[0]->output(0).get_names() == input_tensor_names);
// check Parameter and Result node names
ASSERT_TRUE(model->get_parameters()[0]->get_friendly_name() == "y");
ASSERT_TRUE(model->get_results()[0]->get_friendly_name() == "z");
}
{
// create a reference graph
auto x = make_shared<Constant>(element::f32, Shape{2, 3}, vector<float>{1, 2, 3, 3, 2, 1});
auto y = make_shared<Parameter>(element::f32, Shape{1});
auto add = make_shared<Add>(x, y);
model_ref = make_shared<Model>(OutputVector{add}, ParameterVector{y});
}
}
TEST_F(FrontEndConversionWithReferenceTestsF, SavedModelVariables) {
{ model = convert_model("saved_model_variables"); }
{
// create a reference graph
auto x = make_shared<Parameter>(element::f32, Shape{1});
auto y = make_shared<Constant>(element::f32, Shape{}, vector<float>{123});
auto multiply = make_shared<Multiply>(x, y);
model_ref = make_shared<Model>(OutputVector{multiply}, ParameterVector{x});
}
}
TEST_F(FrontEndConversionWithReferenceTestsF, SavedModelWithInputIntegerType) {
{
model = convert_model("saved_model_with_gather",
nullptr,
{"params", "indices"},
{},
{PartialShape{10, 5}, PartialShape{3}});
// check tensor names in the resulted model
unordered_set<string> input_tensor_name1 = {"params"};
unordered_set<string> input_tensor_name2 = {"indices"};
unordered_set<string> output_tensor_names = {"test_output_name"};
ASSERT_EQ(model->get_results().size(), 1);
ASSERT_TRUE(model->get_results()[0]->input_value(0).get_names() == output_tensor_names);
ASSERT_EQ(model->get_parameters().size(), 2);
ASSERT_TRUE(model->get_parameters()[0]->output(0).get_names() == input_tensor_name1);
ASSERT_TRUE(model->get_parameters()[1]->output(0).get_names() == input_tensor_name2);
// check Parameter and Result node names
ASSERT_TRUE(model->get_parameters()[0]->get_friendly_name() == "params");
ASSERT_TRUE(model->get_parameters()[1]->get_friendly_name() == "indices");
ASSERT_TRUE(model->get_results()[0]->get_friendly_name() == "test_output_name");
}
{
// create a reference graph
auto params = make_shared<Parameter>(element::f32, Shape{10, 5});
auto indices = make_shared<Parameter>(element::i32, Shape{3});
auto gather_axis = make_shared<Constant>(element::i32, Shape{}, 0);
auto gather = make_shared<Gather>(params, indices, gather_axis);
auto const_mul = make_shared<Constant>(element::f32, Shape{}, 5);
auto mul = make_shared<Multiply>(gather, const_mul);
model_ref = make_shared<Model>(OutputVector{mul}, ParameterVector{params, indices});
}
}
TEST_F(FrontEndConversionWithReferenceTestsF, SavedModelMultipleTensorNames) {
// The test aims to check tensor names of input and output tensors
// it checks that TF FE preserved user specific names for input and output tensor
// and exclude internal names
{
model = convert_model("saved_model_parameter_result");
// check tensor names in the resulted model
unordered_set<string> tensor_names = {"params", "test_output_name"};
ASSERT_EQ(model->get_results().size(), 1);
ASSERT_TRUE(model->get_results()[0]->input_value(0).get_names() == tensor_names);
ASSERT_EQ(model->get_parameters().size(), 1);
ASSERT_TRUE(model->get_parameters()[0]->output(0).get_names() == tensor_names);
// check Parameter and Result node names
ASSERT_TRUE(model->get_parameters()[0]->get_friendly_name() == "params");
ASSERT_TRUE(model->get_results()[0]->get_friendly_name() == "test_output_name");
}
{
// create a reference graph
auto x = make_shared<Parameter>(element::f32, Shape{20, 5});
auto result = make_shared<Result>(x);
model_ref = make_shared<Model>(OutputVector{result}, ParameterVector{x});
}
}
TEST_F(FrontEndConversionWithReferenceTestsF, SavedModelBroadcastIssue) {
{ model = convert_model("saved_model_broadcast_issue"); }
{
// create a reference graph
auto x = make_shared<Constant>(element::i64, Shape{2, 2}, vector<int64_t>{1, 2, -1, -1});
model_ref = make_shared<Model>(OutputVector{x}, ParameterVector{});
}
}
TEST_F(FrontEndConversionWithReferenceTestsF, SavedModelMultiGraph) {
// The test verifies loading of MetaGraph with empty tags as default
// And verifies loading variables with no corresponding RestoreV2
{ model = convert_model("saved_model_multi-graph"); }
{
// create a reference graph
auto x = make_shared<Constant>(element::f32, Shape{2, 3}, vector<float>{1, 2, 3, 3, 2, 1});
auto y = make_shared<Parameter>(element::f32, Shape{1});
auto add = make_shared<Add>(x, y);
model_ref = make_shared<Model>(OutputVector{add}, ParameterVector{y});
}
}
TEST_F(FrontEndConversionWithReferenceTestsF, SavedModelWithIntermediateOutput) {
// The test aims to check that output from intermediate layers presented in the model signature
// must be preserved
{
model = convert_model("saved_model_intermediate_output");
ASSERT_TRUE(model->get_results().size() == 2);
}
{
// create a reference graph
auto input1 = make_shared<Parameter>(element::f32, Shape{2});
auto input2 = make_shared<Parameter>(element::f32, Shape{2});
auto add = make_shared<Add>(input1, input2);
auto sub = make_shared<Subtract>(input2, add);
auto result1 = make_shared<Result>(add);
auto result2 = make_shared<Result>(sub);
model_ref = make_shared<Model>(OutputVector{result1, result2}, ParameterVector{input1, input2});
}
}
TEST_F(FrontEndConversionWithReferenceTestsF, SavedModelMMAPCompare) {
{ model = convert_model("saved_model_variables"); }
{ model_ref = convert_model("saved_model_variables", nullptr, {}, {}, {}, {}, {}, true); }
}
TEST_F(FrontEndConversionWithReferenceTestsF, SavedModelWithNumericalNames) {
comparator.enable(FunctionsComparator::CmpValues::TENSOR_NAMES);
// The test aims to check that model with only numerical names for operation
// is successfully converted
// it is a tricky case because colision between naming input and output ports may occur
{ model = convert_model("saved_model_with_numerical_names"); }
{
// create a reference graph
auto x = make_shared<Parameter>(element::f32, Shape{1});
x->output(0).set_names({"0"});
auto y = make_shared<Parameter>(element::f32, Shape{1});
y->output(0).set_names({"1"});
auto z = make_shared<Parameter>(element::f32, Shape{1});
z->output(0).set_names({"2"});
auto add = make_shared<Add>(x, y);
add->output(0).set_names({"3", "3:0"});
auto sub = make_shared<Subtract>(add, z);
sub->output(0).set_names({"4"});
auto result = make_shared<Result>(sub);
result->output(0).set_names({"4"});
model_ref = make_shared<Model>(ResultVector{result}, ParameterVector{x, y, z});
}
}