forked from openvinotoolkit/openvino
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconvert_saved_model.cpp
More file actions
238 lines (213 loc) · 10.9 KB
/
convert_saved_model.cpp
File metadata and controls
238 lines (213 loc) · 10.9 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// Copyright (C) 2018-2026 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include "common_test_utils/test_common.hpp"
#include "conversion_with_reference.hpp"
#include "gtest/gtest.h"
#include "openvino/frontend/exception.hpp"
#include "openvino/op/add.hpp"
#include "openvino/op/constant.hpp"
#include "openvino/op/gather.hpp"
#include "openvino/op/multiply.hpp"
#include "openvino/op/parameter.hpp"
#include "openvino/op/result.hpp"
#include "openvino/op/subtract.hpp"
#include "tf_utils.hpp"
using namespace std;
using namespace ov;
using namespace ov::op;
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<std::string> input_tensor_names = {"y"};
unordered_set<std::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<v0::Constant>(element::f32, Shape{2, 3}, vector<float>{1, 2, 3, 3, 2, 1});
auto y = make_shared<v0::Parameter>(element::f32, Shape{1});
auto add = make_shared<v1::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<v0::Parameter>(element::f32, Shape{1});
auto y = make_shared<v0::Constant>(element::f32, Shape{}, vector<float>{123});
auto multiply = make_shared<v1::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<std::string> input_tensor_name1 = {"params"};
unordered_set<std::string> input_tensor_name2 = {"indices"};
unordered_set<std::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<v0::Parameter>(element::f32, Shape{10, 5});
auto indices = make_shared<v0::Parameter>(element::i32, Shape{3});
auto gather_axis = make_shared<v0::Constant>(element::i32, Shape{}, 0);
auto gather = make_shared<v8::Gather>(params, indices, gather_axis);
auto const_mul = make_shared<v0::Constant>(element::f32, Shape{}, 5);
auto mul = make_shared<v1::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<std::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<v0::Parameter>(element::f32, Shape{20, 5});
auto result = make_shared<v0::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<v0::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<v0::Constant>(element::f32, Shape{2, 3}, vector<float>{1, 2, 3, 3, 2, 1});
auto y = make_shared<v0::Parameter>(element::f32, Shape{1});
auto add = make_shared<v1::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<v0::Parameter>(element::f32, Shape{2});
auto input2 = make_shared<v0::Parameter>(element::f32, Shape{2});
auto add = make_shared<v1::Add>(input1, input2);
auto sub = make_shared<v1::Subtract>(input2, add);
auto result1 = make_shared<v0::Result>(add);
auto result2 = make_shared<v0::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 collision between naming input and output ports may occur
{ model = convert_model("saved_model_with_numerical_names"); }
{
// create a reference graph
auto x = make_shared<v0::Parameter>(element::f32, Shape{1});
x->output(0).set_names({"0"});
auto y = make_shared<v0::Parameter>(element::f32, Shape{1});
y->output(0).set_names({"1"});
auto z = make_shared<v0::Parameter>(element::f32, Shape{1});
z->output(0).set_names({"2"});
auto add = make_shared<v1::Add>(x, y);
add->output(0).set_names({"3:0"});
auto sub = make_shared<v1::Subtract>(add, z);
sub->output(0).set_names({"4"});
auto result = make_shared<v0::Result>(sub);
result->output(0).set_names({"4"});
model_ref = make_shared<Model>(ResultVector{result}, ParameterVector{x, y, z});
}
}
// Test that a crafted SavedModel with integer overflow in BundleEntryProto
// offset/size fields produces a clean exception, not a SIGSEGV.
// The malicious variables.index has entry.offset = 0x7FFFFFFFFFFFFFF0 and entry.size = 16.
// The sum overflows signed int64 to INT64_MIN, which would bypass the old bounds check.
TEST(FrontEndConvertModelTest, SavedModelMaliciousOverflowOffset) {
shared_ptr<Model> model = nullptr;
// Test with mmap enabled (default) — triggers crash at variables_index.cpp CKOG path
try {
model = convert_model("saved_model_malicious_overflow");
FAIL() << "Loading a malicious SavedModel with overflow offset should throw an exception.";
} catch (const ov::Exception& error) {
string error_message = error.what();
EXPECT_TRUE(error_message.find("entry") != string::npos || error_message.find("offset") != string::npos ||
error_message.find("bounds") != string::npos || error_message.find("negative") != string::npos ||
error_message.find("size") != string::npos)
<< "Unexpected error message: " << error_message;
EXPECT_EQ(model, nullptr);
} catch (const std::exception& e) {
FAIL() << "Unexpected exception type: " << e.what();
} catch (...) {
FAIL() << "Unexpected non-std exception thrown";
}
}
// Same test with mmap disabled to verify the stream code path is also protected
TEST(FrontEndConvertModelTest, SavedModelMaliciousOverflowOffsetNoMmap) {
shared_ptr<Model> model = nullptr;
try {
model = convert_model("saved_model_malicious_overflow", nullptr, {}, {}, {}, {}, {}, true /* disable_mmap */);
FAIL() << "Loading a malicious SavedModel with overflow offset should throw an exception.";
} catch (const ov::Exception& error) {
string error_message = error.what();
EXPECT_TRUE(error_message.find("entry") != string::npos || error_message.find("offset") != string::npos ||
error_message.find("bounds") != string::npos || error_message.find("negative") != string::npos ||
error_message.find("size") != string::npos)
<< "Unexpected error message: " << error_message;
EXPECT_EQ(model, nullptr);
} catch (const std::exception& e) {
FAIL() << "Unexpected exception type: " << e.what();
} catch (...) {
FAIL() << "Unexpected non-std exception thrown";
}
}