forked from openvinotoolkit/openvino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.cpp
More file actions
1160 lines (1046 loc) · 44.8 KB
/
model.cpp
File metadata and controls
1160 lines (1046 loc) · 44.8 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
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (C) 2018-2023 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include <algorithm>
#include <list>
#include <memory>
#include <string>
#include <unordered_map>
#include "itt.hpp"
#include "layout_utils.hpp"
#include "meta_data.hpp"
#include "ngraph/evaluator.hpp"
#include "ngraph/function.hpp"
#include "ngraph/graph_util.hpp"
#include "ngraph/log.hpp"
#include "ngraph/ops.hpp"
#include "ngraph/opsets/opset7.hpp"
#include "ngraph/validation_util.hpp"
#include "openvino/core/attribute_visitor.hpp"
#include "openvino/core/except.hpp"
#include "openvino/core/partial_shape.hpp"
#include "openvino/op/parameter.hpp"
#include "openvino/op/util/op_types.hpp"
#include "openvino/op/util/variable_context.hpp"
#include "openvino/op/util/variable_extension.hpp"
#include "openvino/pass/manager.hpp"
#include "shared_node_info.hpp"
#include "tensor_conversion_util.hpp"
#include "transformations/smart_reshape/smart_reshape.hpp"
using namespace std;
atomic<size_t> ov::Model::m_next_instance_id(0);
namespace {
void check_all_variables_registered(const std::vector<shared_ptr<ov::Node>>& ordered_ops,
const ov::op::util::VariableVector& variables) {
OV_ITT_SCOPED_TASK(ov::itt::domains::ov_pass, "Model::check_all_variables_registered");
std::stringstream unregistered_variables;
for (auto& node : ordered_ops) {
const auto& variable_op = dynamic_pointer_cast<ov::op::util::VariableExtension>(node);
if (variable_op &&
std::find(variables.begin(), variables.end(), variable_op->get_variable()) == variables.end())
unregistered_variables << variable_op->get_variable_id() << std::endl;
}
if (!unregistered_variables.str().empty())
throw ov::Exception("Model references undeclared variables: " + unregistered_variables.str());
}
void check_all_parameters_registered(const std::vector<shared_ptr<ov::Node>>& ordered_ops,
const ngraph::ParameterVector& parameters) {
OV_ITT_SCOPED_TASK(ov::itt::domains::core, "Model::check_all_parameters_registered");
std::stringstream unregistered_parameters;
for (auto& node : ordered_ops) {
if (ov::op::util::is_parameter(node) &&
std::find(parameters.begin(), parameters.end(), node) == parameters.end())
unregistered_parameters << node << std::endl;
}
if (!unregistered_parameters.str().empty())
throw ov::Exception("Model references undeclared parameters: " + unregistered_parameters.str());
}
ov::op::util::VariableVector auto_detect_variables(const std::vector<std::shared_ptr<ov::Node>>& ordered_ops) {
OV_ITT_SCOPED_TASK(ov::itt::domains::core, "Model::auto_detect_variables");
unordered_set<ov::op::util::Variable::Ptr> variables;
for (const auto& op : ordered_ops) {
if (const auto& variable_op = dynamic_pointer_cast<ov::op::util::VariableExtension>(op)) {
variables.insert(variable_op->get_variable());
}
}
return ov::op::util::VariableVector(variables.begin(), variables.end());
}
ngraph::ParameterVector auto_detect_parameters(const std::vector<std::shared_ptr<ov::Node>>& ordered_ops) {
OV_ITT_SCOPED_TASK(ov::itt::domains::core, "Model::auto_detect_parameters");
ngraph::ParameterVector parameter_vector;
for (const auto& op : ordered_ops) {
if (const auto& param = dynamic_pointer_cast<ngraph::opset7::Parameter>(op)) {
parameter_vector.push_back(param);
}
}
return parameter_vector;
}
} // namespace
ov::Model::Model(const ResultVector& results, const ngraph::ParameterVector& parameters, const std::string& name)
: m_name(name),
m_unique_name("Model" + to_string(m_next_instance_id.fetch_add(1))),
m_topological_sorter(ngraph::topological_sort<std::vector<std::shared_ptr<ov::Node>>>),
m_results(results),
m_parameters(parameters) {
prerequirements(true, false);
}
ov::Model::Model(const OutputVector& results, const ngraph::ParameterVector& parameters, const std::string& name)
: m_name(name),
m_unique_name("Model" + to_string(m_next_instance_id.fetch_add(1))),
m_topological_sorter(ngraph::topological_sort<std::vector<std::shared_ptr<ov::Node>>>),
m_results(as_result_vector(results)),
m_parameters(parameters) {
prerequirements(true, false);
}
ov::Model::Model(const NodeVector& results, const ngraph::ParameterVector& parameters, const std::string& name)
: m_name(name),
m_unique_name("Model" + to_string(m_next_instance_id.fetch_add(1))),
m_topological_sorter(ngraph::topological_sort<std::vector<std::shared_ptr<ov::Node>>>),
m_results(as_result_vector(as_output_vector(results))),
m_parameters(parameters) {
prerequirements(true, false);
}
ov::Model::Model(const std::shared_ptr<Node>& result,
const ngraph::ParameterVector& parameters,
const std::string& name)
: Model(result->outputs(), parameters, name) {}
ov::Model::Model(const ngraph::ResultVector& results,
const ngraph::SinkVector& sinks,
const ngraph::ParameterVector& parameters,
const std::string& name)
: m_name(name),
m_unique_name("Model" + to_string(m_next_instance_id.fetch_add(1))),
m_topological_sorter(ngraph::topological_sort<std::vector<std::shared_ptr<Node>>>),
m_results(results),
m_sinks(sinks),
m_parameters(parameters) {
prerequirements(true, false);
}
ov::Model::Model(const OutputVector& results,
const ngraph::SinkVector& sinks,
const ngraph::ParameterVector& parameters,
const std::string& name)
: Model(as_result_vector(results), sinks, parameters, name) {}
ov::Model::Model(const ngraph::ResultVector& results,
const ngraph::SinkVector& sinks,
const ngraph::ParameterVector& parameters,
const ngraph::VariableVector& variables,
const std::string& name)
: m_name(name),
m_unique_name("Model" + to_string(m_next_instance_id.fetch_add(1))),
m_topological_sorter(ngraph::topological_sort<std::vector<std::shared_ptr<Node>>>),
m_results(results),
m_sinks(sinks),
m_parameters(parameters),
m_variables(variables) {
prerequirements(false, false);
}
ov::Model::Model(const OutputVector& results,
const ngraph::SinkVector& sinks,
const ngraph::ParameterVector& parameters,
const ngraph::VariableVector& variables,
const std::string& name)
: Model(as_result_vector(results), sinks, parameters, variables, name) {}
ov::Model::Model(const ngraph::OutputVector& results,
const ngraph::ParameterVector& parameters,
const ngraph::VariableVector& variables,
const std::string& name)
: Model(as_result_vector(results), {}, parameters, variables, name) {}
ov::Model::Model(const ngraph::ResultVector& results,
const ngraph::ParameterVector& parameters,
const ngraph::VariableVector& variables,
const std::string& name)
: Model(results, {}, parameters, variables, name) {}
ov::Model::Model(const ngraph::OutputVector& results, const ngraph::SinkVector& sinks, const string& name)
: m_name(name),
m_unique_name("Model" + to_string(m_next_instance_id.fetch_add(1))),
m_topological_sorter(ngraph::topological_sort<std::vector<std::shared_ptr<Node>>>),
m_results(as_result_vector(results)),
m_sinks(sinks) {
prerequirements(true, true);
}
ov::Model::Model(const OutputVector& results, const string& name) : Model(results, ngraph::SinkVector{}, name) {}
void ov::Model::prerequirements(bool detect_variables, bool detect_parameters) {
OV_ITT_SCOPED_TASK(ov::itt::domains::core, "Model::prerequirements");
for (const auto& param : m_parameters) {
OPENVINO_ASSERT(param != nullptr, "Model is incorrect! Some Parameter operation equals to nullptr.");
}
for (const auto& result : m_results) {
OPENVINO_ASSERT(result != nullptr, "Model is incorrect! Some Result operation equals to nullptr.");
}
for (const auto& sink : m_sinks) {
OPENVINO_ASSERT(sink != nullptr, "Model is incorrect! Some Sink operation equals to nullptr.");
}
for (const auto& variable : m_variables) {
OPENVINO_ASSERT(variable != nullptr, "Model is incorrect! Some Variable equals to nullptr.");
}
m_shared_rt_info = std::make_shared<SharedRTInfo>();
const auto& ordered_ops = get_ordered_ops();
if (detect_parameters)
m_parameters = auto_detect_parameters(ordered_ops);
else
check_all_parameters_registered(ordered_ops, m_parameters);
if (detect_variables)
m_variables = auto_detect_variables(ordered_ops);
else
check_all_variables_registered(ordered_ops, m_variables);
}
void ov::Model::validate_nodes_and_infer_types() const {
OV_ITT_SCOPED_TASK(ov::itt::domains::core, "Model::validate_nodes_and_infer_types");
struct Counter {
int cnt_assign = 0;
int cnt_read_val = 0;
};
std::map<ov::op::util::Variable*, Counter> pair_checker;
std::stringstream unregistered_parameters;
std::stringstream unregistered_variables;
std::unordered_set<const ov::descriptor::Tensor*> tensors;
for (auto& node : get_ordered_ops()) {
node->revalidate_and_infer_types();
for (const auto& output : node->outputs()) {
const auto& tensor = output.get_tensor();
// Skip results outputs tensors because result_input_tensor == result_output_tensor
if (tensors.count(&tensor))
continue;
tensors.insert(&tensor);
}
if (op::util::is_parameter(node) &&
std::find(m_parameters.begin(), m_parameters.end(), node) == m_parameters.end())
unregistered_parameters << node << std::endl;
const auto& variable_op = dynamic_pointer_cast<op::util::VariableExtension>(node);
if (variable_op &&
std::find(m_variables.begin(), m_variables.end(), variable_op->get_variable()) == m_variables.end())
unregistered_variables << variable_op->get_variable_id() << std::endl;
if (const auto& assign = std::dynamic_pointer_cast<ngraph::op::AssignBase>(node)) {
pair_checker[assign->get_variable().get()].cnt_assign++;
} else if (const auto& read_value = std::dynamic_pointer_cast<ngraph::op::ReadValueBase>(node)) {
pair_checker[read_value->get_variable().get()].cnt_read_val++;
}
}
if (!unregistered_parameters.str().empty())
throw ov::Exception("Model references undeclared parameters: " + unregistered_parameters.str());
if (!unregistered_variables.str().empty())
throw ov::Exception("Model references undeclared Variables: " + unregistered_variables.str());
bool only_pairs =
std::all_of(pair_checker.begin(), pair_checker.end(), [](const std::pair<op::util::Variable*, Counter>& val) {
return val.second.cnt_assign == 1 && val.second.cnt_read_val == 1;
});
if (!only_pairs)
throw ov::Exception("Model is incorrect. Assign and ReadValue operations must be in pairs on the "
"network.");
for (const auto& output : outputs()) {
OPENVINO_ASSERT(ov::layout::utils::is_compatible(ov::layout::get_layout(output), output.get_partial_shape()),
"Result '",
output,
"' with shape ",
output.get_partial_shape(),
" is incompatible with layout ",
ov::layout::get_layout(output).to_string());
}
}
std::vector<shared_ptr<ov::Node>> ov::Model::get_ordered_ops() const {
OV_ITT_SCOPED_TASK(ov::itt::domains::core, "Model::get_ordered_ops");
lock_guard<mutex> lock(m_model_mutex);
NodeVector nodes;
if (m_shared_rt_info->get_use_topological_cache()) {
for (const auto& node : m_cached_ordered_ops) {
if (auto locked_node = node.lock()) {
nodes.emplace_back(locked_node);
}
}
return nodes;
}
for (const auto& r : get_results()) {
nodes.emplace_back(r);
}
for (auto& r : get_sinks()) {
nodes.emplace_back(r);
}
for (auto& param : get_parameters()) {
nodes.push_back(param);
}
auto order = m_topological_sorter(nodes);
// Update nodes cache and update all nodes to have shared rt info
// which belongs to the current Model.
m_cached_ordered_ops.clear();
for_each(order.cbegin(), order.cend(), [this](const shared_ptr<Node>& node) {
m_cached_ordered_ops.push_back(node);
node->insert_info(m_shared_rt_info);
});
m_cached_output_names.clear();
m_cached_op_names.clear();
m_shared_rt_info->set_use_topological_cache(true);
return order;
}
void ov::Model::map_unordered_ops(std::function<void(Node*)> f) const {
std::unordered_set<Node*> unordered_ops;
std::stack<Node*, std::vector<Node*>> remaining_ops;
for (auto& r : get_results()) {
remaining_ops.push(r.get());
}
for (auto& r : get_sinks()) {
remaining_ops.push(r.get());
}
for (auto& param : get_parameters()) {
remaining_ops.push(param.get());
}
while (!remaining_ops.empty()) {
Node* op = remaining_ops.top();
remaining_ops.pop();
if (unordered_ops.insert(op).second) {
f(op);
for (size_t i = 0; i < op->get_input_size(); ++i) {
remaining_ops.push(op->get_input_node_ptr(i));
}
for (auto& cdep : op->get_control_dependencies()) {
remaining_ops.push(cdep.get());
}
}
}
}
const std::string& ov::Model::get_friendly_name() const {
if (m_name.empty()) {
return m_unique_name;
}
return m_name;
}
const std::string& ov::Model::get_name() const {
return m_unique_name;
}
void ov::Model::set_friendly_name(const string& name) {
m_name = name;
}
std::ostream& ov::operator<<(std::ostream& out, const ov::Model& f) {
out << "Model(" << f.get_name() << ")";
return out;
}
size_t ov::Model::get_output_size() const {
return m_results.size();
}
const ov::element::Type& ov::Model::get_output_element_type(size_t i) const {
return m_results.at(i)->get_element_type();
}
const ov::Shape& ov::Model::get_output_shape(size_t i) const {
return m_results.at(i)->get_shape();
}
const ov::PartialShape& ov::Model::get_output_partial_shape(size_t i) const {
return m_results.at(i)->get_output_partial_shape(0);
}
shared_ptr<ov::Node> ov::Model::get_output_op(size_t i) const {
return m_results.at(i);
}
shared_ptr<ov::Node> ov::Model::get_result() const {
if (m_results.size() != 1) {
throw ov::Exception("get_result() must be called on a Model with exactly one result.");
}
return m_results.at(0);
}
std::vector<shared_ptr<ov::Node>> ov::Model::get_ops() const {
std::vector<std::shared_ptr<Node>> ops;
ngraph::traverse_nodes(this, [&](shared_ptr<Node> node) {
ops.push_back(node);
});
return ops;
}
void ov::Model::replace_node(std::shared_ptr<Node> old, std::shared_ptr<Node> repl) {
ngraph::replace_node(old, repl);
}
size_t ov::Model::get_graph_size() const {
size_t total_size = 0;
for (auto node : get_ops()) {
total_size += sizeof(*node);
if (node->description() == "Constant") {
const ov::Shape& shape = node->get_output_shape(0);
size_t const_size = node->get_output_element_type(0).size();
if (shape.size() == 0) {
total_size += const_size;
} else {
total_size += (const_size * shape_size(node->get_output_shape(0)));
}
}
}
return total_size;
}
bool ov::Model::is_dynamic() const {
auto list_of_nodes = this->get_ops();
for (auto& node : list_of_nodes) {
if (node->get_output_partial_shape(0).is_dynamic()) {
return true;
}
}
return false;
}
void ov::Model::replace_parameter(size_t parameter_index, const shared_ptr<ngraph::op::Parameter>& parameter) {
NGRAPH_CHECK(parameter_index < m_parameters.size(),
"replace_parameter(): Tried to replace parameter at index ",
parameter_index,
" but the Model only has ",
m_parameters.size(),
" parameters.");
replace_node(m_parameters[parameter_index], parameter);
m_parameters[parameter_index] = parameter;
}
void ov::Model::set_topological_sort(topological_sort_t sorter) {
m_topological_sorter = sorter;
// reset topological nodes order cache as new sorter can have different behaviour
m_shared_rt_info->set_use_topological_cache(false);
}
int64_t ov::Model::get_parameter_index(const std::shared_ptr<ngraph::op::Parameter>& parameter) const {
int64_t pos = 0;
for (auto p : get_parameters()) {
if (p == parameter) {
return pos;
}
pos++;
}
return -1;
}
int64_t ov::Model::get_result_index(const Output<Node>& value) const {
return get_result_index(Output<const Node>(value.get_node(), value.get_index()));
}
int64_t ov::Model::get_result_index(const Output<const Node>& value) const {
int64_t pos = 0;
if (is_type<ngraph::op::Result>(value.get_node_shared_ptr())) {
auto result = value.get_node_shared_ptr();
for (auto r : get_results()) {
if (r == result) {
return pos;
}
pos++;
}
} else {
for (auto r : get_results()) {
const auto& input_value = r->input_value(0);
const auto result_input = Output<const Node>(input_value.get_node(), input_value.get_index());
if (result_input == value) {
return pos;
}
pos++;
}
}
return -1;
}
bool ov::Model::evaluate(const HostTensorVector& output_tensors,
const HostTensorVector& input_tensors,
EvaluationContext evaluation_context) const {
OPENVINO_SUPPRESS_DEPRECATED_START
auto outputs = ov::util::wrap_tensors(output_tensors);
auto inputs = ov::util::wrap_tensors(input_tensors);
bool sts = evaluate(outputs, inputs, std::move(evaluation_context));
ov::util::update_output_host_tensors(output_tensors, outputs);
OPENVINO_SUPPRESS_DEPRECATED_END
return sts;
}
bool ov::Model::evaluate(ov::TensorVector& output_tensors,
const ov::TensorVector& input_tensors,
ov::EvaluationContext evaluation_context) const {
evaluation_context.emplace("VariableContext", ov::op::util::VariableContext());
std::map<RawNodeOutput, ov::Tensor> value_map;
for (size_t i = 0; i < m_parameters.size(); ++i) {
value_map[m_parameters.at(i)->output(0)] = input_tensors.at(i);
}
OutputVector outputs;
std::map<RawNodeOutput, ov::Tensor> output_tensor_map;
for (size_t i = 0; i < m_results.size(); ++i) {
auto result = m_results.at(i)->output(0);
output_tensor_map[result] = output_tensors.at(i);
outputs.push_back(result);
}
for (const auto& m_sink : m_sinks) {
outputs.push_back(m_sink);
}
// evaluate nodes
OPENVINO_SUPPRESS_DEPRECATED_START
ngraph::Evaluator<ov::Tensor> evaluator({}, value_map);
evaluator.set_universal_handler(
[&output_tensor_map, &evaluation_context](Node* node,
const ov::TensorVector& input_tensors) -> ov::TensorVector {
ov::TensorVector output_tensors;
for (const auto& v : node->outputs()) {
auto it = output_tensor_map.find(v);
if (it == output_tensor_map.end()) {
output_tensors.push_back(util::wrap_tensor(v));
} else {
output_tensors.push_back(it->second);
}
}
if (node->evaluate(output_tensors, input_tensors, evaluation_context)) {
for (size_t i = 0; i < node->outputs().size(); i++) {
const auto& v = node->output(i);
auto it = output_tensor_map.find(v);
if (it != output_tensor_map.end()) {
it->second = output_tensors[i];
}
}
return output_tensors;
} else {
OPENVINO_ASSERT(false, "Evaluation failed on ", node);
}
});
for (const auto& value : outputs) {
evaluator.evaluate(value);
}
OPENVINO_SUPPRESS_DEPRECATED_END
for (size_t i = 0; i < m_results.size(); ++i) {
auto result = m_results.at(i)->output(0);
output_tensors.at(i) = output_tensor_map[result];
}
return true;
}
bool ov::Model::visit_attributes(AttributeVisitor& visitor) {
visitor.on_attribute("parameters", m_parameters);
visitor.on_attribute("results", m_results);
return true;
}
void ov::Model::add_sinks(const ngraph::SinkVector& sinks) {
m_sinks.insert(m_sinks.end(), sinks.begin(), sinks.end());
for (const auto& sink : sinks) {
if (const auto& variable_op = dynamic_pointer_cast<op::util::VariableExtension>(sink)) {
if (find(m_variables.begin(), m_variables.end(), variable_op->get_variable()) == m_variables.end()) {
m_variables.push_back(variable_op->get_variable());
}
}
}
// reset topological nodes order cache as new sinks/results/parameters
// can be in a separate connectivity component.
m_shared_rt_info->set_use_topological_cache(false);
}
void ov::Model::remove_sink(const std::shared_ptr<ngraph::op::Sink>& sink) {
m_sinks.erase(std::remove_if(m_sinks.begin(),
m_sinks.end(),
[&sink](std::shared_ptr<ngraph::op::Sink>& s) {
return s == sink;
}),
m_sinks.end());
m_shared_rt_info->set_use_topological_cache(false);
}
void ov::Model::add_results(const ResultVector& results) {
m_results.insert(m_results.end(), results.begin(), results.end());
// reset topological nodes order cache as new sinks/results/parameters
// can be in a separate connectivity component.
m_shared_rt_info->set_use_topological_cache(false);
}
void ov::Model::remove_result(const std::shared_ptr<ngraph::op::Result>& result) {
m_results.erase(std::remove_if(m_results.begin(),
m_results.end(),
[&result](std::shared_ptr<ngraph::op::v0::Result>& r) {
return r == result;
}),
m_results.end());
m_shared_rt_info->set_use_topological_cache(false);
}
void ov::Model::add_parameters(const ngraph::ParameterVector& params) {
for (size_t i = 0; i < params.size(); i++) {
for (size_t j = 0; j < m_parameters.size(); j++) {
NGRAPH_CHECK(params[i] != m_parameters[j],
"add_parameters(): Tried to add parameter (index in array ",
i,
") but Model already have the same parameter with index ",
j);
}
}
m_parameters.insert(m_parameters.end(), params.begin(), params.end());
// reset topological nodes order cache as new sinks/results/parameters
// can be in a separate connectivity component.
m_shared_rt_info->set_use_topological_cache(false);
}
void ov::Model::remove_parameter(const std::shared_ptr<ngraph::op::Parameter>& param) {
m_parameters.erase(std::remove_if(m_parameters.begin(),
m_parameters.end(),
[¶m](std::shared_ptr<ngraph::op::v0::Parameter>& r) {
return r == param;
}),
m_parameters.end());
m_shared_rt_info->set_use_topological_cache(false);
}
void ov::Model::add_variables(const op::util::VariableVector& variables) {
m_variables.insert(m_variables.end(), variables.begin(), variables.end());
}
void ov::Model::remove_variable(const op::util::Variable::Ptr& variable) {
m_variables.erase(std::remove_if(m_variables.begin(),
m_variables.end(),
[&variable](op::util::Variable::Ptr& v) {
return v == variable;
}),
m_variables.end());
}
ov::op::util::Variable::Ptr ov::Model::get_variable_by_id(const string& variable_id) const {
auto variable =
std::find_if(m_variables.begin(), m_variables.end(), [&variable_id](const ov::op::util::Variable::Ptr& cur) {
return cur->get_info().variable_id == variable_id;
});
if (variable != m_variables.end())
return *variable;
else
return ov::op::util::Variable::Ptr();
}
/// Output Model
std::vector<ov::Output<const ov::Node>> ov::Model::outputs() const {
std::vector<ov::Output<const ov::Node>> results;
for (const auto& res : m_results) {
std::shared_ptr<const ov::Node> result = res;
results.emplace_back(result);
}
return results;
}
ov::Output<const ov::Node> ov::Model::output() const {
if (m_results.size() != 1) {
throw ov::Exception("output() must be called on a Model with exactly one result.");
}
std::shared_ptr<const ov::Node> result = m_results.at(0);
return result;
}
ov::Output<const ov::Node> ov::Model::output(size_t i) const {
std::shared_ptr<const ov::Node> result = m_results.at(i);
return result;
}
ov::Output<const ov::Node> ov::Model::output(const std::string& tensor_name) const {
for (const auto& res : m_results) {
if (res->get_input_tensor(0).get_names().count(tensor_name)) {
std::shared_ptr<const ov::Node> result = res;
return result;
}
}
throw ov::Exception("Output for tensor name '" + tensor_name + "' is not found.");
}
std::vector<ov::Output<ov::Node>> ov::Model::outputs() {
std::vector<ov::Output<ov::Node>> results;
for (const auto& result : m_results) {
results.emplace_back(result);
}
return results;
}
ov::Output<ov::Node> ov::Model::output() {
if (m_results.size() != 1) {
throw ov::Exception("output() must be called on a Model with exactly one result.");
}
return m_results.at(0);
}
ov::Output<ov::Node> ov::Model::output(size_t i) {
return m_results.at(i);
}
ov::Output<ov::Node> ov::Model::output(const std::string& tensor_name) {
for (const auto& res : m_results) {
if (res->get_input_tensor(0).get_names().count(tensor_name))
return res;
}
throw ov::Exception("Output for tensor name '" + tensor_name + "' is not found.");
}
/// Input Model
std::vector<ov::Output<const ov::Node>> ov::Model::inputs() const {
std::vector<ov::Output<const ov::Node>> inputs;
for (const auto& input : m_parameters) {
std::shared_ptr<const ov::Node> parameter = input;
inputs.emplace_back(parameter);
}
return inputs;
}
ov::Output<const ov::Node> ov::Model::input() const {
if (m_parameters.size() != 1) {
throw ov::Exception("input() must be called on a Model with exactly one parameter.");
}
std::shared_ptr<const ov::Node> parameter = m_parameters.at(0);
return parameter;
}
ov::Output<const ov::Node> ov::Model::input(size_t i) const {
std::shared_ptr<const ov::Node> parameter = m_parameters.at(i);
return parameter;
}
ov::Output<const ov::Node> ov::Model::input(const std::string& tensor_name) const {
for (const auto& param : m_parameters) {
if (param->get_output_tensor(0).get_names().count(tensor_name)) {
std::shared_ptr<const ov::Node> parameter = param;
return parameter;
}
}
throw ov::Exception("Input for tensor name '" + tensor_name + "' is not found.");
}
std::vector<ov::Output<ov::Node>> ov::Model::inputs() {
std::vector<ov::Output<ov::Node>> inputs;
for (const auto& input : m_parameters) {
inputs.emplace_back(input);
}
return inputs;
}
ov::Output<ov::Node> ov::Model::input() {
if (m_parameters.size() != 1) {
throw ov::Exception("input() must be called on a Model with exactly one parameter.");
}
return m_parameters.at(0);
}
ov::Output<ov::Node> ov::Model::input(size_t i) {
return m_parameters.at(i);
}
ov::Output<ov::Node> ov::Model::input(const std::string& tensor_name) {
for (const auto& param : m_parameters) {
if (param->get_output_tensor(0).get_names().count(tensor_name))
return param;
}
throw ov::Exception("Input for tensor name '" + tensor_name + "' is not found.");
}
void ov::Model::reshape(const ov::PartialShape& partial_shape) {
if (m_parameters.size() != 1) {
throw ov::Exception("reshape(const ov::PartialShape&) must be called on a Model with exactly one parameter.");
}
std::map<size_t, ov::PartialShape> shapes;
shapes[0] = partial_shape;
reshape(shapes);
}
void ov::Model::reshape(const std::map<size_t, ov::PartialShape>& partial_shapes) {
std::map<ov::Output<ov::Node>, ov::PartialShape> const_pshape;
std::unordered_map<ov::Node*, std::string> port_tensor_map;
for (const auto& it : partial_shapes) {
const auto port = input(it.first);
port_tensor_map[port.get_node()] = std::to_string(it.first);
const_pshape[port] = it.second;
}
reshape(const_pshape);
}
void ov::Model::reshape(const std::map<std::string, ov::PartialShape>& partial_shapes) {
std::map<ov::Output<ov::Node>, ov::PartialShape> const_pshape;
std::unordered_map<ov::Node*, std::string> port_tensor_map;
for (const auto& it : partial_shapes) {
const auto port = input(it.first);
if (port_tensor_map.find(port.get_node()) != port_tensor_map.end()) {
OPENVINO_ASSERT(it.second == const_pshape.at(port),
"Tensor with names {'",
it.first,
"', '",
port_tensor_map[port.get_node()],
"'} has "
"conflicting shapes ",
it.second,
" and ",
const_pshape.at(port),
", but they define the same tensor");
}
port_tensor_map[port.get_node()] = it.first;
const_pshape[port] = it.second;
}
reshape(const_pshape);
}
void ov::Model::reshape(const std::map<ov::Output<ov::Node>, ov::PartialShape>& partial_shapes) {
if (partial_shapes.empty())
return;
const auto& params = get_parameters();
std::unordered_map<ov::op::v0::Parameter*, ov::PartialShape> new_param_shapes;
// Check that we need to do reshape only if input shapes will be changed
bool need_reshape = false;
for (const auto& partial_shape : partial_shapes) {
bool shape_is_used = false;
for (const auto& param : params) {
const auto port = param->output(0);
if (port == partial_shape.first) {
shape_is_used = true;
if (param->get_output_partial_shape(0).is_dynamic() ||
param->get_output_partial_shape(0) != partial_shape.second) {
need_reshape = true;
new_param_shapes[param.get()] = partial_shape.second;
}
break;
}
}
OPENVINO_ASSERT(shape_is_used,
"PartialShape for port '",
*partial_shape.first.get_node(),
"' is not used in ov::Model::reshape");
}
if (!need_reshape)
return;
// save original parameters shape
std::unordered_map<ov::op::v0::Parameter*, ov::PartialShape> original_input_shapes;
for (const auto& param : params) {
original_input_shapes[param.get()] = param->get_output_partial_shape(0);
}
auto reshape_only = [&](const std::unordered_map<ov::op::v0::Parameter*, ov::PartialShape>& pshapes) {
for (const auto& pshape : pshapes) {
pshape.first->set_partial_shape(pshape.second);
}
validate_nodes_and_infer_types();
};
try {
ov::pass::Manager ssr_manager;
ssr_manager.register_pass<ov::pass::SmartReshape>();
ssr_manager.run_passes(shared_from_this());
reshape_only(new_param_shapes);
} catch (...) {
// restore shapes to original ones
reshape_only(original_input_shapes);
throw;
}
}
ov::Output<ov::Node> ov::Model::add_output(const std::string& tensor_name) {
auto cache_valid = [&]() {
return m_cached_output_names.count(tensor_name) &&
m_cached_output_names[tensor_name].get_names().count(tensor_name) > 0;
};
if (!m_shared_rt_info->get_use_topological_cache() || !cache_valid()) {
m_cached_output_names.clear();
// get_ordered_ops will update topological cache if necessary
for (const auto& op : get_ordered_ops()) {
for (const auto& output : op->outputs()) {
for (const auto& name : output.get_names()) {
m_cached_output_names[name] = output;
}
}
}
}
OPENVINO_ASSERT(m_cached_output_names.count(tensor_name),
"Model::add_output. Tensor name '",
tensor_name + "' was not found.");
return add_output(m_cached_output_names.at(tensor_name));
}
ov::Output<ov::Node> ov::Model::add_output(const std::string& op_name, size_t output_idx) {
auto cache_valid = [&]() {
if (m_cached_op_names.count(op_name)) {
auto op = m_cached_op_names[op_name].lock();
return op && op->get_friendly_name() == op_name && op->get_output_size() > output_idx;
}
return false;
};
if (!m_shared_rt_info->get_use_topological_cache() || !cache_valid()) {
m_cached_op_names.clear();
// get_ordered_ops will update topological cache to 'true' if necessary
for (const auto& op : get_ordered_ops()) {
m_cached_op_names[op->get_friendly_name()] = op;
}
}
OPENVINO_ASSERT(m_cached_op_names.count(op_name),
"Model::add_output. Operation with name '",
op_name,
"' was not found.");
auto op = m_cached_op_names[op_name].lock();
OPENVINO_ASSERT(op, "Model::add_output. Operation with name '", op_name, "' is expired.");
OPENVINO_ASSERT(output_idx < op->get_output_size(),
"Cannot add output to port ",
std::to_string(output_idx),
" operation ",
op->get_friendly_name(),
" has only ",
std::to_string(op->get_output_size()),
" outputs.");
return add_output(op->output(output_idx));
}
ov::Output<ov::Node> ov::Model::add_output(const ov::Output<ov::Node>& port) {
if (ov::op::util::is_output(port.get_node()))
return port;
for (const auto& input : port.get_target_inputs()) {
// Do not add result if port is already connected with result
if (ov::op::util::is_output(input.get_node())) {
return input.get_node()->output(0);
}
}
auto result = std::make_shared<ov::op::v0::Result>(port);
m_results.push_back(result);
if (m_shared_rt_info->get_use_topological_cache()) {
// Full update of topological cache is not needed, 'result' can be just inserted to the end
m_cached_ordered_ops.push_back(result);
result->insert_info(m_shared_rt_info); // Just for consistency, not required for Result nodes
}
return result->output(0);
}
std::shared_ptr<ov::Model> ov::Model::clone() const {
OPENVINO_SUPPRESS_DEPRECATED_START
return ov::clone_model(*this);
OPENVINO_SUPPRESS_DEPRECATED_END
}
bool ov::Model::has_rt_info(const std::vector<std::string>& args) const {
ov::AnyMap info = m_rt_info;
for (size_t i = 0; i < args.size(); i++) {
bool has_attr = has_rt_arg(info, args[i]);
if (!has_attr)
return false;
if (i == args.size() - 1)
break;
const ov::Any& rt_attr = get_rt_arg<std::string>(info, args[i]);
info = get_map_from_attr(rt_attr);
}
return true;
}
ov::Any& ov::Model::get_rt_info(ov::AnyMap& info,
const std::vector<std::string>::const_iterator& begin,
const std::vector<std::string>::const_iterator& end) {
if (begin == end - 1) {
return get_rt_arg(info, *begin);
} else {
ov::Any& rt_attr = get_rt_arg<std::string>(info, *begin);
return get_rt_info(get_map_from_attr(rt_attr), begin + 1, end);
}
}
// Allow to get constant attribute for the vector
const ov::Any& ov::Model::get_rt_info(const ov::AnyMap& info,
const std::vector<std::string>::const_iterator& begin,
const std::vector<std::string>::const_iterator& end) const {
if (begin == end - 1) {
return get_rt_arg(info, *begin);
} else {
const ov::Any& rt_attr = get_rt_arg<std::string>(info, *begin);
return get_rt_info(get_map_from_attr(rt_attr), begin + 1, end);
}
}
const ov::AnyMap& ov::Model::get_map_from_attr(const ov::Any& info) const {
// lock to get meta from different threads in order to avoid thread safety
// implementations of meta information for each frontend
std::lock_guard<mutex> lock(m_model_mutex);
if (info.is<ov::AnyMap>()) {
return info.as<ov::AnyMap>();
} else if (info.is<std::shared_ptr<ov::Meta>>()) {
std::shared_ptr<ov::Meta> meta = info.as<std::shared_ptr<ov::Meta>>();
return *info.as<std::shared_ptr<ov::Meta>>();
}
throw ov::Exception("Cannot get runtime attribute. Path to runtime attribute is incorrect.");
}
ov::AnyMap& ov::Model::get_map_from_attr(ov::Any& info) const {