forked from openvinotoolkit/openvino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplaces.cpp
424 lines (350 loc) · 17.2 KB
/
places.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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// Copyright (C) 2018-2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include <frontend/shared/include/utils.hpp>
#include <fstream>
#include <iostream>
#include <openvino/frontend/manager.hpp>
#include "gtest/gtest.h"
#include "paddle_utils.hpp"
using namespace ov::frontend;
const std::string model_file = FrontEndTestUtils::make_model_path(std::string(TEST_PADDLE_MODELS_DIRNAME) +
"place_test_model/place_test_model.pdmodel");
const std::string vars_name_file =
FrontEndTestUtils::make_model_path(std::string(TEST_PADDLE_MODELS_DIRNAME) + "place_test_model/vars_name.txt");
const std::string outputs_name_file =
FrontEndTestUtils::make_model_path(std::string(TEST_PADDLE_MODELS_DIRNAME) + "place_test_model/outputs_name.txt");
class Paddle_Places : public ::testing::Test {
protected:
void SetUp() override {
std::fstream name_file;
name_file.open(vars_name_file, std::ios::in);
if (name_file.is_open()) {
std::string name;
while (std::getline(name_file, name))
tensor_names.push_back(name);
name_file.close();
} else
FRONT_END_THROW("Can not open " + vars_name_file);
std::fstream output_file;
output_file.open(outputs_name_file, std::ios::in);
if (output_file.is_open()) {
std::string name;
while (std::getline(output_file, name))
output_names.push_back(name);
output_file.close();
} else
FRONT_END_THROW("Can not open " + outputs_name_file);
}
std::vector<std::string> tensor_names;
std::vector<std::string> output_names;
};
TEST_F(Paddle_Places, check_tensor_names) {
auto fem = FrontEndManager();
FrontEnd::Ptr frontend;
OV_ASSERT_NO_THROW(frontend = fem.load_by_framework(PADDLE_FE));
InputModel::Ptr input_model;
OV_ASSERT_NO_THROW(input_model = frontend->load(FrontEndTestUtils::make_model_path(model_file)));
for (const auto& tensor_name : tensor_names) {
auto place = input_model->get_place_by_tensor_name(tensor_name);
EXPECT_NE(place, nullptr);
}
}
TEST_F(Paddle_Places, check_input_outputs) {
auto fem = FrontEndManager();
FrontEnd::Ptr frontend;
OV_ASSERT_NO_THROW(frontend = fem.load_by_framework(PADDLE_FE));
InputModel::Ptr input_model;
OV_ASSERT_NO_THROW(input_model = frontend->load(FrontEndTestUtils::make_model_path(model_file)));
auto inputs = input_model->get_inputs();
auto outputs = input_model->get_outputs();
EXPECT_EQ(inputs.size(), 1);
EXPECT_EQ(outputs.size(), 6);
auto tensor_place = input_model->get_place_by_tensor_name("x");
tensor_place->is_equal(inputs[0]);
for (const auto& name : output_names) {
const auto output_place = input_model->get_place_by_tensor_name(name);
auto it = std::find_if(outputs.begin(), outputs.end(), [&output_place](const Place::Ptr& place) {
return output_place->is_equal(place);
});
EXPECT_NE(it, outputs.end());
}
}
// all existed in the model ops have "Out" port
TEST_F(Paddle_Places, check_out_port_of_all_ops) {
auto fem = FrontEndManager();
FrontEnd::Ptr frontend;
OV_ASSERT_NO_THROW(frontend = fem.load_by_framework(PADDLE_FE));
InputModel::Ptr input_model;
OV_ASSERT_NO_THROW(input_model = frontend->load(FrontEndTestUtils::make_model_path(model_file)));
for (const auto& tensor_name : tensor_names) {
auto place = input_model->get_place_by_tensor_name(tensor_name);
EXPECT_NE(place, nullptr);
auto producing_op = place->get_producing_operation();
EXPECT_NE(producing_op, nullptr);
auto out_port_by_name = producing_op->get_output_port("Out");
EXPECT_NE(out_port_by_name, nullptr);
auto out_port_by_name_idx = producing_op->get_output_port("Out", 0);
EXPECT_NE(out_port_by_name_idx, nullptr);
EXPECT_TRUE(out_port_by_name->is_equal(out_port_by_name_idx));
}
}
TEST_F(Paddle_Places, check_in_out_ports_of_model_outputs) {
auto fem = FrontEndManager();
FrontEnd::Ptr frontend;
OV_ASSERT_NO_THROW(frontend = fem.load_by_framework(PADDLE_FE));
InputModel::Ptr input_model;
OV_ASSERT_NO_THROW(input_model = frontend->load(FrontEndTestUtils::make_model_path(model_file)));
auto outputs = input_model->get_outputs();
for (const auto& output : outputs) {
auto producing_op = output->get_producing_operation();
EXPECT_NE(producing_op, nullptr);
auto out_port = producing_op->get_output_port();
EXPECT_NE(out_port, nullptr);
auto out_port_by_name = producing_op->get_output_port("Out");
EXPECT_NE(out_port_by_name, nullptr);
auto out_port_by_name_idx = producing_op->get_output_port("Out", 0);
EXPECT_NE(out_port_by_name_idx, nullptr);
EXPECT_TRUE(out_port->is_equal(out_port_by_name));
EXPECT_TRUE(out_port->is_equal(out_port_by_name_idx));
auto in_port = producing_op->get_input_port();
EXPECT_NE(in_port, nullptr);
auto in_port_by_name = producing_op->get_input_port("X");
EXPECT_NE(in_port_by_name, nullptr);
auto in_port_by_name_idx = producing_op->get_input_port("X", 0);
EXPECT_NE(in_port_by_name_idx, nullptr);
EXPECT_TRUE(in_port->is_equal(in_port_by_name));
EXPECT_TRUE(in_port->is_equal(in_port_by_name_idx));
}
}
TEST_F(Paddle_Places, check_source_target_tensors_of_model_outputs) {
auto fem = FrontEndManager();
FrontEnd::Ptr frontend;
OV_ASSERT_NO_THROW(frontend = fem.load_by_framework(PADDLE_FE));
InputModel::Ptr input_model;
OV_ASSERT_NO_THROW(input_model = frontend->load(FrontEndTestUtils::make_model_path(model_file)));
auto outputs = input_model->get_outputs();
for (const auto& output : outputs) {
auto producing_op = output->get_producing_operation();
EXPECT_NE(producing_op, nullptr);
auto out = producing_op->get_target_tensor();
EXPECT_NE(out, nullptr);
auto out_by_name = producing_op->get_target_tensor("Out");
EXPECT_NE(out_by_name, nullptr);
auto out_by_name_idx = producing_op->get_target_tensor("Out", 0);
EXPECT_NE(out_by_name_idx, nullptr);
EXPECT_TRUE(out->is_equal(out_by_name));
EXPECT_TRUE(out->is_equal(out_by_name_idx));
auto in = producing_op->get_source_tensor();
EXPECT_NE(in, nullptr);
auto in_by_name = producing_op->get_source_tensor("X");
EXPECT_NE(in_by_name, nullptr);
auto in_by_name_idx = producing_op->get_source_tensor("X", 0);
EXPECT_NE(in_by_name_idx, nullptr);
EXPECT_TRUE(in->is_equal(in_by_name));
EXPECT_TRUE(in->is_equal(in_by_name_idx));
}
}
TEST_F(Paddle_Places, check_producing_consuming_ops_of_model_outputs) {
auto fem = FrontEndManager();
FrontEnd::Ptr frontend;
OV_ASSERT_NO_THROW(frontend = fem.load_by_framework(PADDLE_FE));
InputModel::Ptr input_model;
OV_ASSERT_NO_THROW(input_model = frontend->load(FrontEndTestUtils::make_model_path(model_file)));
auto outputs = input_model->get_outputs();
for (const auto& output : outputs) {
auto op = output->get_producing_operation();
EXPECT_NE(op, nullptr);
auto out = op->get_consuming_operations();
EXPECT_EQ(out.size(), 1);
auto out_by_name = op->get_consuming_operations("Out");
EXPECT_EQ(out_by_name.size(), 1);
auto out_by_name_idx = op->get_consuming_operations("Out", 0);
EXPECT_EQ(out_by_name_idx.size(), 1);
EXPECT_TRUE(out[0]->is_equal(out_by_name[0]));
EXPECT_TRUE(out[0]->is_equal(out_by_name_idx[0]));
auto in = op->get_producing_operation();
EXPECT_NE(in, nullptr);
auto in_by_name = op->get_producing_operation("X");
EXPECT_NE(in_by_name, nullptr);
auto in_by_name_idx = op->get_producing_operation("X", 0);
EXPECT_NE(in_by_name_idx, nullptr);
EXPECT_TRUE(in->is_equal(in_by_name));
EXPECT_TRUE(in->is_equal(in_by_name_idx));
}
}
// check data flow [ output port -> tensor -> input port ]
TEST_F(Paddle_Places, check_data_flow) {
auto fem = FrontEndManager();
FrontEnd::Ptr frontend;
OV_ASSERT_NO_THROW(frontend = fem.load_by_framework(PADDLE_FE));
InputModel::Ptr input_model;
OV_ASSERT_NO_THROW(input_model = frontend->load(FrontEndTestUtils::make_model_path(model_file)));
for (const auto& tensor_name : tensor_names) {
auto tensor_place = input_model->get_place_by_tensor_name(tensor_name);
EXPECT_NE(tensor_place, nullptr);
auto out_port = tensor_place->get_producing_port();
auto in_ports = tensor_place->get_consuming_ports();
EXPECT_TRUE(tensor_place->is_equal_data(out_port));
EXPECT_TRUE(out_port->is_equal_data(tensor_place));
EXPECT_FALSE(out_port->is_equal(tensor_place));
auto source_tensor = out_port->get_target_tensor();
EXPECT_TRUE(source_tensor->is_equal(tensor_place));
for (const auto& in_port : in_ports) {
EXPECT_TRUE(out_port->is_equal_data(in_port));
EXPECT_TRUE(in_port->is_equal_data(out_port));
EXPECT_TRUE(in_port->is_equal_data(tensor_place));
EXPECT_TRUE(tensor_place->is_equal_data(in_port));
EXPECT_FALSE(in_port->is_equal(out_port));
EXPECT_FALSE(in_port->is_equal(tensor_place));
EXPECT_TRUE(out_port->is_equal(in_port->get_producing_port()));
EXPECT_TRUE(tensor_place->is_equal(in_port->get_source_tensor()));
}
}
}
// check [ tensor -> input_port
// -> input_port_2
// -> input_port_N]
// input_port, input_port_2, ... input_port_N are equal data
TEST_F(Paddle_Places, check_tensor_to_multiple_ports) {
auto fem = FrontEndManager();
FrontEnd::Ptr frontend;
OV_ASSERT_NO_THROW(frontend = fem.load_by_framework(PADDLE_FE));
InputModel::Ptr input_model;
OV_ASSERT_NO_THROW(input_model = frontend->load(FrontEndTestUtils::make_model_path(model_file)));
for (const auto& tensor_name : tensor_names) {
auto tensor_place = input_model->get_place_by_tensor_name(tensor_name);
auto inputs_to = tensor_place->get_consuming_ports();
for (size_t idx = 0; idx < inputs_to.size(); ++idx) {
for (size_t idx_2 = 0; idx_2 < inputs_to.size(); ++idx_2) {
EXPECT_TRUE(inputs_to[idx]->is_equal_data(inputs_to[idx_2]));
EXPECT_TRUE(inputs_to[idx_2]->is_equal_data(inputs_to[idx]));
if (idx == idx_2) {
EXPECT_TRUE(inputs_to[idx]->is_equal(inputs_to[idx_2]));
} else {
EXPECT_FALSE(inputs_to[idx]->is_equal(inputs_to[idx_2]));
}
}
}
}
}
// consuming ops should be equal for tensor place and producing output port
TEST_F(Paddle_Places, check_consuming_ops) {
auto fem = FrontEndManager();
FrontEnd::Ptr frontend;
OV_ASSERT_NO_THROW(frontend = fem.load_by_framework(PADDLE_FE));
InputModel::Ptr input_model;
OV_ASSERT_NO_THROW(input_model = frontend->load(FrontEndTestUtils::make_model_path(model_file)));
for (const auto& tensor_name : tensor_names) {
auto tensor_place = input_model->get_place_by_tensor_name(tensor_name);
EXPECT_NE(tensor_place, nullptr);
auto consuming_ops_for_tensor = tensor_place->get_consuming_operations();
auto out_port = tensor_place->get_producing_port();
auto consuming_ops_for_out_port = out_port->get_consuming_operations();
bool is_permutation = std::is_permutation(consuming_ops_for_out_port.begin(),
consuming_ops_for_out_port.end(),
consuming_ops_for_tensor.begin(),
[](const Place::Ptr& place1, const Place::Ptr& place2) {
return place1->is_equal(place2);
});
EXPECT_TRUE(is_permutation);
auto consuming_ports_for_tensor = tensor_place->get_consuming_ports();
std::vector<Place::Ptr> consuming_ops_for_in_ports;
for (const auto& port : consuming_ports_for_tensor) {
EXPECT_EQ(port->get_consuming_operations().size(), 1);
consuming_ops_for_in_ports.push_back(port->get_consuming_operations()[0]);
}
is_permutation = std::is_permutation(consuming_ops_for_in_ports.begin(),
consuming_ops_for_in_ports.end(),
consuming_ops_for_tensor.begin(),
[](const Place::Ptr& place1, const Place::Ptr& place2) {
return place1->is_equal(place2);
});
EXPECT_TRUE(is_permutation);
}
}
TEST_F(Paddle_Places, check_consuming_ops_2) {
auto fem = FrontEndManager();
FrontEnd::Ptr frontend;
OV_ASSERT_NO_THROW(frontend = fem.load_by_framework(PADDLE_FE));
InputModel::Ptr input_model;
OV_ASSERT_NO_THROW(input_model = frontend->load(FrontEndTestUtils::make_model_path(model_file)));
auto it = find(tensor_names.begin(), tensor_names.end(), "lstm_0.tmp_2");
EXPECT_NE(it, tensor_names.end());
auto tensor_place = input_model->get_place_by_tensor_name(*it);
auto consuming_ports = tensor_place->get_consuming_ports();
auto consuming_ops = tensor_place->get_consuming_operations();
EXPECT_EQ(consuming_ports.size(), 4);
EXPECT_EQ(consuming_ops.size(), 4);
for (const auto& consuming_port : consuming_ports) {
auto port_consuming_ops = consuming_port->get_consuming_operations();
EXPECT_EQ(port_consuming_ops.size(), 1);
auto in_port = port_consuming_ops[0]->get_input_port();
auto in_port_by_name = port_consuming_ops[0]->get_input_port("X");
auto in_port_by_name_and_idx = port_consuming_ops[0]->get_input_port("X", 0);
EXPECT_TRUE(consuming_port->is_equal(in_port) && consuming_port->is_equal(in_port_by_name) &&
consuming_port->is_equal(in_port_by_name_and_idx));
auto op =
std::find_if(consuming_ops.begin(), consuming_ops.end(), [&port_consuming_ops](const Place::Ptr& place) {
return place->is_equal(port_consuming_ops[0]);
});
EXPECT_NE(op, consuming_ops.end());
const auto source_tensor = port_consuming_ops[0]->get_source_tensor();
EXPECT_TRUE(source_tensor->is_equal(tensor_place));
EXPECT_TRUE(source_tensor->is_equal(consuming_port->get_source_tensor()));
}
}
TEST_F(Paddle_Places, check_producing_ops) {
auto fem = FrontEndManager();
FrontEnd::Ptr frontend;
OV_ASSERT_NO_THROW(frontend = fem.load_by_framework(PADDLE_FE));
InputModel::Ptr input_model;
OV_ASSERT_NO_THROW(input_model = frontend->load(FrontEndTestUtils::make_model_path(model_file)));
for (const auto& tensor_name : tensor_names) {
auto tensor_place = input_model->get_place_by_tensor_name(tensor_name);
EXPECT_NE(tensor_place, nullptr);
auto producing_op = tensor_place->get_producing_operation();
auto consuming_ports = tensor_place->get_consuming_ports();
auto producing_port = tensor_place->get_producing_port();
EXPECT_TRUE(producing_op->is_equal(producing_port->get_producing_operation()));
for (const auto& consuming_port : consuming_ports) {
EXPECT_TRUE(producing_op->is_equal(consuming_port->get_producing_operation()));
}
}
}
TEST_F(Paddle_Places, check_input_output_ports_dy_idx) {
auto fem = FrontEndManager();
FrontEnd::Ptr frontend;
OV_ASSERT_NO_THROW(frontend = fem.load_by_framework(PADDLE_FE));
InputModel::Ptr input_model;
OV_ASSERT_NO_THROW(input_model = frontend->load(FrontEndTestUtils::make_model_path(model_file)));
for (const auto& tensor_name : output_names) {
auto tensor_place = input_model->get_place_by_tensor_name(tensor_name);
EXPECT_NE(tensor_place, nullptr);
auto op = tensor_place->get_producing_operation();
auto input_port = op->get_input_port(0);
EXPECT_NE(input_port, nullptr);
auto out_port = op->get_output_port(0);
EXPECT_NE(out_port, nullptr);
}
}
TEST_F(Paddle_Places, check_ops_tensors_by_idx) {
auto fem = FrontEndManager();
FrontEnd::Ptr frontend;
OV_ASSERT_NO_THROW(frontend = fem.load_by_framework(PADDLE_FE));
InputModel::Ptr input_model;
OV_ASSERT_NO_THROW(input_model = frontend->load(FrontEndTestUtils::make_model_path(model_file)));
for (const auto& tensor_name : output_names) {
auto tensor_place = input_model->get_place_by_tensor_name(tensor_name);
EXPECT_NE(tensor_place, nullptr);
auto op = tensor_place->get_producing_operation();
auto prod_op = op->get_producing_operation(0);
EXPECT_NE(prod_op, nullptr);
auto target_tensor = op->get_target_tensor(0);
EXPECT_EQ(tensor_place, target_tensor);
auto source_tensor = op->get_source_tensor(0);
EXPECT_NE(source_tensor, nullptr);
auto consum_op = op->get_consuming_operations(0);
EXPECT_EQ(consum_op.size(), 1);
}
}