Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#include "core/graph/graph_utils.h"
 
#include "core/framework/tensorprotoutils.h"
#include "core/graph/graph.h"
#include "core/common/logging/logging.h"
 
#include <algorithm>
#include <array>
#include <limits>
#include <queue>
#include <string>
#include <vector>
 
namespace onnxruntime {
 
namespace graph_utils {
 
//---------------------
//--- local helpers ---
//---------------------
 
#if !defined(ORT_MINIMAL_BUILD) || defined(ORT_EXTENDED_MINIMAL_BUILD)
 
static int GetIndexFromName(const Node& node, const std::string& name, bool is_input) {
  const auto& node_args = is_input ? node.InputDefs() : node.OutputDefs();
  auto itr = std::find_if(node_args.begin(), node_args.end(),
                          [&name](const NodeArg* node_arg) { return node_arg->Name() == name; });
  ORT_ENFORCE(itr != node_args.end(),
              "Attempting to get index by a name which does not exist:", name, "for node: ", node.Name());
  auto index = std::distance(node_args.begin(), itr);
  return static_cast<int>(index);
}
 
Status CreateFilteredIndexedGraph(gsl::span<const Node* const> nodes, const Graph& graph,
                                  std::unique_ptr<IndexedSubGraph>& result) {
  // Following data structures help determine the final inputs/outputs of the subgraph.
  // Note: The 'subgraph' here refers to a graph that contains a subset of nodes in the 'src_graph'.
 
  // Pre-pass: Identify all outputs produced by nodes within the subgraph.
  // This allows O(1) checks to determine if an input is internal or from the boundary.
  InlinedHashSet<NodeIndex> node_set;
  InlinedHashSet<const NodeArg*> internal_outputs;
  for (size_t i = 0, lim = nodes.size(); i < lim; i++) {
    const auto& node = *nodes[i];
    node_set.insert(node.Index());
    for (const auto& output : node.OutputDefs()) {
      internal_outputs.insert(output);
    }
  }
 
  // Source graph output names
  InlinedHashSet<std::string> graph_output_names;
  for (const auto* output_arg : graph.GetOutputs()) {
    graph_output_names.insert(output_arg->Name());
  }
 
  // These maps store the inputs and outputs of the subgraph.
  // Value is order index to maintain deterministic order.
  InlinedHashMap<const NodeArg*, int> subgraph_inputs, subgraph_outputs;
 
  int input_order = 0;
  int output_order = 0;
 
  std::unique_ptr<IndexedSubGraph> indexed_sub_graph = std::make_unique<IndexedSubGraph>();
  InlinedVector<std::string> initializers;
 
  // Add nodes and identify boundary inputs/outputs
  for (size_t i = 0, lim = nodes.size(); i < lim; i++) {
    const auto& node = *nodes[i];
    indexed_sub_graph->nodes.push_back(node.Index());
 
    // Process Inputs: If an input is not produced internally, it's a subgraph input.
    auto process_inputs = [&](gsl::span<const NodeArg* const> inputs) {
      for (const auto& input : inputs) {
        if (!input->Exists()) continue;
 
        const auto* tensor_proto = graph.GetConstantInitializer(input->Name(), true);
        if (tensor_proto != nullptr) {
          initializers.push_back(input->Name());
          continue;
        }
 
        // If not produced by this subgraph, it's a boundary input
        if (internal_outputs.count(input) == 0) {
          // Use insert to keep the first occurrence's order
          auto emplace_result = subgraph_inputs.emplace(input, input_order);
          if (emplace_result.second) {
            ++input_order;
          }
        }
      }
    };
 
    process_inputs(gsl::make_span(node.InputDefs().data(), node.InputDefs().size()));
    process_inputs(gsl::make_span(node.ImplicitInputDefs().data(), node.ImplicitInputDefs().size()));
 
    // Process Outputs: If an output is graph output OR consumed externally, it's a subgraph output.
    for (const auto& output : node.OutputDefs()) {
      if (!output->Exists()) continue;
 
      bool is_boundary_output = false;
 
      // 1. Is it a graph output?
      if (graph_output_names.count(output->Name()) > 0) {
        is_boundary_output = true;
      } else {
        // 2. Is it consumed by any node outside the subgraph?
        for (auto it = node.OutputEdgesBegin(), end = node.OutputEdgesEnd(); it != end; ++it) {
          // Check if the edge uses this specific output
          if (it->GetSrcArgIndex() < static_cast<int>(node.OutputDefs().size()) &&
              node.OutputDefs()[it->GetSrcArgIndex()] == output) {
            if (node_set.count(it->GetNode().Index()) == 0) {
              is_boundary_output = true;
              break;
            }
          }
        }
      }
 
      if (is_boundary_output) {
        subgraph_outputs.insert({output, output_order++});
      }
    }
  }
 
  std::multimap<int, const NodeArg*> inputs, outputs;
 
  // Get the input order of the original graph
  InlinedHashMap<const NodeArg*, int> original_inputs;
  int order = 0;
  for (const auto* input : graph.GetInputs()) {
    original_inputs[input] = order++;
  }
 
  // input order needs to be consistent with original graph's input order
  for (const auto& [node_arg, subgraph_input_order] : subgraph_inputs) {
    const auto original_input_it = original_inputs.find(node_arg);
 
    if (original_input_it != original_inputs.end()) {
      inputs.emplace(
          original_input_it->second,  // input order from original graph
          node_arg);
    } else {
      inputs.emplace(
          subgraph_input_order,  // input order from subgraph
          node_arg);
    }
  }
 
  // Sort outputs by the order they were added
  for (const auto& [node_arg, subgraph_output_order] : subgraph_outputs) {
    outputs.emplace(subgraph_output_order, node_arg);
  }
 
  std::unique_ptr<IndexedSubGraph::MetaDef> meta_def = std::make_unique<IndexedSubGraph::MetaDef>();
  meta_def->name = "sub_graph";
  meta_def->since_version = 1;
 
  // Assign inputs and outputs to subgraph's meta_def
  for (const auto& input : inputs) {
    if (input.second->Exists()) {
      meta_def->inputs.push_back(input.second->Name());
    }
  }
 
  for (const auto& initializer : initializers) {
    meta_def->constant_initializers.push_back(initializer);
  }
 
  for (const auto& output : outputs) {
    if (output.second->Exists()) {
      meta_def->outputs.push_back(output.second->Name());
    }
  }
 
  indexed_sub_graph->SetMetaDef(std::move(meta_def));
  result = std::move(indexed_sub_graph);
 
  return Status::OK();
}
 
#endif  // !defined(ORT_MINIMAL_BUILD) || defined(ORT_EXTENDED_MINIMAL_BUILD)
 
#if !defined(ORT_MINIMAL_BUILD)
 
// check if an output edge provides an implicit input to the destination node
static bool OutputEdgeProvidesImplicitInput(const Graph& graph, const GraphEdge& output_edge) {
  // we treat the explicit and implicit inputs as sequential, so if the destination arg index of an output edge
  // is past the valid range for the node's explicit inputs, it is for an implicit input
  const size_t num_explicit_inputs = (*graph.GetNode(output_edge.dst_node)).InputDefs().size();
  return static_cast<size_t>(output_edge.dst_arg_index) >= num_explicit_inputs;
}
 
/** Checks if new_output_name can be used to replace removed_output_name in the subgraph input.
    If there is an existing NodeArg in a subgraph that implicitly consumes removed_output_name, it is not safe. */
static bool CanUpdateImplicitInputNameInSubgraph(const Node& node,
                                                 const std::string& removed_output_name,
                                                 const std::string& new_output_name) {
  if (!node.ContainsSubgraph())
    return true;
 
  for (const gsl::not_null<const Graph*>& subgraph : node.GetSubgraphs()) {
    // if we have an existing NodeArg in the subgraph with the new_output_name that would override an implicit input
    // with the same name
    if (subgraph->GetNodeArg(new_output_name) != nullptr) {
      return false;
    }
 
    for (auto& subgraph_node : subgraph->Nodes()) {
      // recurse if this node also consumes removed_output_name as an implicit input (i.e. there are multiple levels
      // of nested
      // subgraphs, and at least one level lower uses removed_output_name as an implicit input
      const auto subgraph_node_implicit_inputs = subgraph_node.ImplicitInputDefs();
      if (!subgraph_node_implicit_inputs.empty()) {
        auto subgraph_node_also_consumes_nodearg_as_implicit_input =
            std::find_if(subgraph_node_implicit_inputs.cbegin(), subgraph_node_implicit_inputs.cend(),
                         [&removed_output_name](const NodeArg* input) {
                           return input != nullptr && input->Name() == removed_output_name;
                         });
 
        if (subgraph_node_also_consumes_nodearg_as_implicit_input != subgraph_node_implicit_inputs.cend()) {
          if (!CanUpdateImplicitInputNameInSubgraph(subgraph_node, removed_output_name, new_output_name))
            return false;
        }
      }
    }
  }
 
  return true;
}
 
/** Updates removed_output_name with new_output_name in the subgraph input. */
static void UpdateImplicitInputNameInSubgraph(Node& node,
                                              const std::string& removed_output_name,
                                              const std::string& new_output_name) {
  for (auto& attr_subgraph_pair : node.GetAttributeNameToMutableSubgraphMap()) {
    Graph& subgraph = *attr_subgraph_pair.second;
 
    for (auto& subgraph_node : subgraph.Nodes()) {
      // recurse if this node also consumes removed_output_name as an implicit input
      // (i.e. there are multiple levels of nested subgraphs, and at least one level lower uses
      // removed_output_name as an implicit input
      const auto subgraph_node_implicit_inputs = subgraph_node.ImplicitInputDefs();
      if (!subgraph_node_implicit_inputs.empty()) {
        auto subgraph_node_also_consumes_nodearg_as_implicit_input =
            std::find_if(subgraph_node_implicit_inputs.cbegin(), subgraph_node_implicit_inputs.cend(),
                         [&removed_output_name](const NodeArg* input) {
                           return input->Name() == removed_output_name;
                         });
 
        if (subgraph_node_also_consumes_nodearg_as_implicit_input != subgraph_node_implicit_inputs.cend()) {
          UpdateImplicitInputNameInSubgraph(subgraph_node, removed_output_name, new_output_name);
        }
      }
 
      // Need mutable input defs to be able to update the implicit input names
      auto& input_args = subgraph_node.MutableInputDefs();
 
      if (!input_args.empty()) {
        int input_slot_index = -1;
        for (const auto* input_arg : input_args) {
          ++input_slot_index;
          // if the input matches, replace the NodeArg with one using the new name
          if (input_arg->Exists() && input_arg->Name() == removed_output_name) {
            // sanity check there was no edge for this input. implicit inputs from outer scope do not have edges
            ORT_ENFORCE(std::count_if(subgraph_node.InputEdgesBegin(), subgraph_node.InputEdgesEnd(),
                                      [input_slot_index](const Node::EdgeEnd& entry) {
                                        return entry.GetDstArgIndex() == input_slot_index;
                                      }) == 0);
 
            // Create a new NodeArg with the new name
            input_args[input_slot_index] = &attr_subgraph_pair.second->GetOrCreateNodeArg(new_output_name,
                                                                                          input_arg->TypeAsProto());
          }
        }
      }
    }
  }
}
 
/** Given a graph, a list of edges, and a NodeArg name, checks if each of the edges provides an implicit input
    to a subgraph. If so, it checks if there is no clash of the given NodeArg name in each of the subgraphs.
    This is important when removing a node with this NodeArg as input. */
static bool CanUpdateImplicitInputNameInSubgraphs(const Graph& graph,
                                                  const std::vector<GraphEdge>& output_edges,
                                                  const std::string& new_arg_name, const logging::Logger& logger) {
  for (const auto& output_edge : output_edges) {
    if (OutputEdgeProvidesImplicitInput(graph, output_edge)) {
      const Node& output_edge_node = *graph.GetNode(output_edge.dst_node);
      if (!CanUpdateImplicitInputNameInSubgraph(output_edge_node, output_edge.arg_name, new_arg_name)) {
        LOGS(logger, WARNING) << " Implicit input name " << output_edge.arg_name
                              << " cannot be safely updated to " << new_arg_name << " in one of the subgraphs.";
        return false;
      }
    }
  }
 
  return true;
}
 
/** Removes a node with a single incoming node and connects the incoming node with the output node/s.*/
static bool RemoveNodeWithSingleNodeInSingleUsedOutput(Graph& graph, Node& node) {
  const NodeIndex node_index = node.Index();
  // Store info for input and output edges.
  std::vector<GraphEdge> output_edges = GraphEdge::GetNodeOutputEdges(node);
 
  if (!output_edges.empty()) {
    // get non-const incoming Node
    const Node::EdgeEnd& input_edge = *node.InputEdgesBegin();
    Node& incoming_node = *graph.GetNode(input_edge.GetNode().Index());
 
    auto src_idx = output_edges.front().src_arg_index;
    ORT_ENFORCE(std::all_of(output_edges.cbegin(), output_edges.cend(),
                            [&src_idx](const GraphEdge& edge) {
                              return edge.src_arg_index == src_idx;
                            }),
                "Node must only have one used output");
 
    // replace the output edges from 'node' with an edge to node's incoming node
    ReplaceDownstreamNodeInput(graph, node, src_idx, incoming_node, input_edge.GetSrcArgIndex());
  }
 
  graph.RemoveNode(node_index);
  graph.NotifyNodesRemoved(gsl::span<const NodeIndex>{&node_index, 1});
 
  return true;
}
 
void MoveAllNodeInputEdges(Graph& graph, Node& src_node, Node& target_node) {
  auto target_idx = target_node.Index();
  auto input_edges = GraphEdge::GetNodeInputEdges(src_node);
 
  for (auto cur = input_edges.cbegin(), end = input_edges.cend(); cur != end; ++cur) {
    auto target_arg_index = GetNodeInputIndexFromInputName(target_node, cur->arg_name);
    graph.AddEdge(cur->src_node, target_idx, cur->src_arg_index, target_arg_index);
  }
 
  GraphEdge::RemoveGraphEdges(graph, input_edges);
}
 
/** Move the output defs and edges from src_node to target_node.
After the move is complete src_node will have no output edges and can be safely removed by Graph::RemoveNode.
*/
static void MoveAllNodeOutputs(Graph& graph, Node& src_node, Node& target_node) {
  // copy the NodeArg*'s for all output defs.
  target_node.MutableOutputDefs() = src_node.MutableOutputDefs();
 
  auto target_idx = target_node.Index();
  auto output_edges = GraphEdge::GetNodeOutputEdges(src_node);
 
  for (auto cur = output_edges.cbegin(), end = output_edges.cend(); cur != end; ++cur) {
    graph.AddEdge(target_idx, cur->dst_node, cur->src_arg_index, cur->dst_arg_index);
  }
 
  GraphEdge::RemoveGraphEdges(graph, output_edges);
}
 
#endif  // !defined(ORT_MINIMAL_BUILD)
 
//----------------------------
//--- end of local helpers ---
//----------------------------
 
bool MatchesOpSinceVersion(const Node& node, std::initializer_list<ONNX_NAMESPACE::OperatorSetVersion> versions) {
  return std::find(versions.begin(), versions.end(), node.SinceVersion()) != versions.end();
}
 
bool MatchesOpSinceVersion(const Node& node, gsl::span<const ONNX_NAMESPACE::OperatorSetVersion> versions) {
  return std::find(versions.begin(), versions.end(), node.SinceVersion()) != versions.end();
}
 
bool MatchesOpSetDomain(const Node& node, std::string_view domain) {
  const auto& node_domain = node.Domain();
  return node_domain == domain;
}
 
bool CheckInMemoryDataMatch(const ONNX_NAMESPACE::TensorProto& tensor_proto, const Tensor& tensor) {
  if (utils::HasExternalData(tensor_proto)) {
    // Retrieve external data using ExternalData structure
    std::unique_ptr<ExternalDataInfo> external_data;
    ORT_THROW_IF_ERROR(ExternalDataInfo::Create(tensor_proto.external_data(), external_data));
    return ((external_data->GetRelPath().compare(utils::kTensorProtoLittleEndianMemoryAddressTag) == 0) ||
            (external_data->GetRelPath().compare(utils::kTensorProtoNativeEndianMemoryAddressTag) == 0)) &&
           (tensor.DataRaw() == reinterpret_cast<const void*>(external_data->GetOffset()));
  }
  return false;
}
 
bool IsSupportedOptypeVersionAndDomain(const Node& node,
                                       std::string_view op_type,
                                       std::initializer_list<ONNX_NAMESPACE::OperatorSetVersion> versions,
                                       std::string_view domain) {
  return IsSupportedOptypeVersionAndDomain(node, op_type, gsl::span{versions.begin(), versions.size()}, domain);
}
 
bool IsSupportedOptypeVersionAndDomain(const Node& node, std::string_view op_type,
                                       gsl::span<const ONNX_NAMESPACE::OperatorSetVersion> versions,
                                       std::string_view domain) {
  return (node.OpType() == op_type &&
  // we don't have op schemas in the minimal build so there's no way to check the deprecated flag
#if !defined(ORT_MINIMAL_BUILD)
          !node.Op()->Deprecated() &&
#endif
          MatchesOpSinceVersion(node, versions) && MatchesOpSetDomain(node, domain));
}
 
#if !defined(ORT_MINIMAL_BUILD) || defined(ORT_EXTENDED_MINIMAL_BUILD)
 
const ONNX_NAMESPACE::AttributeProto* GetNodeAttribute(const Node& node, const std::string& attr_name) {
  const auto& attrs = node.GetAttributes();
  const auto iter = attrs.find(attr_name);
  return iter == attrs.end() ? nullptr : &iter->second;
}
 
bool IsFullShapeNode(const Node& node) {
  const auto* start_attr = GetNodeAttribute(node, "start");
  const auto* end_attr = GetNodeAttribute(node, "end");
  // end=INT64_MAX is the runtime default meaning "all dimensions" (full shape).
  return (!start_attr || start_attr->i() == 0) &&
         (!end_attr || end_attr->i() == std::numeric_limits<int64_t>::max());
}
 
static NodeArg& GetOrCreateNodeArg(Graph& graph, const ONNX_NAMESPACE::TensorProto& new_initializer) {
  ONNX_NAMESPACE::TypeProto new_type;
  auto* typeproto_tensor = new_type.mutable_tensor_type();
  typeproto_tensor->set_elem_type(new_initializer.data_type());
 
  auto* shape = typeproto_tensor->mutable_shape();
  for (auto dim : new_initializer.dims()) {
    shape->add_dim()->set_dim_value(dim);
  }
 
  return graph.GetOrCreateNodeArg(new_initializer.name(), &new_type);
}
 
static OrtValue GetValidatedInMemoryInitializer(const Graph& graph,
                                                const ONNX_NAMESPACE::TensorProto& initializer) {
  OrtValue ort_value;
  ORT_ENFORCE(graph.GetOrtValueInitializer(initializer.name(), ort_value),
              "Initializer '", initializer.name(),
              "' has external data in memory but no cached OrtValue. This is an invalid state.");
  ORT_ENFORCE(CheckInMemoryDataMatch(initializer, ort_value.Get<Tensor>()),
              "In-memory data pointer mismatch for initializer: ", initializer.name(),
              ". This is an invalid state.");
  return ort_value;
}
 
NodeArg& AddInitializer(Graph& graph, const ONNX_NAMESPACE::TensorProto& new_initializer) {
  // sanity check as AddInitializedTensor silently ignores attempts to add a duplicate initializer
  const ONNX_NAMESPACE::TensorProto* existing = nullptr;
  ORT_ENFORCE(!graph.GetInitializedTensor(new_initializer.name(), existing),
              "Initializer with same name exists. Name:", new_initializer.name());
 
  graph.AddInitializedTensor(new_initializer);
  return GetOrCreateNodeArg(graph, new_initializer);
}
 
NodeArg& AddInitializerWithOrtValue(Graph& graph, const ONNX_NAMESPACE::TensorProto& new_initializer) {
  const bool has_external_data = utils::HasExternalData(new_initializer);
  ORT_ENFORCE(!has_external_data, "Expecting an initializer that contains data inline");
 
  Tensor tensor;
  ORT_THROW_IF_ERROR(utils::CreateTensorFromTensorProto(Env::Default(), graph.ModelPath(),
                                                        new_initializer, tensor));
  auto tensor_proto_with_ptr = utils::TensorToTensorProto(tensor, new_initializer.name(), true);
  return AddInitializerWithOrtValue(graph, tensor_proto_with_ptr, std::move(tensor));
}
 
NodeArg& AddInitializerWithOrtValue(Graph& graph, const ONNX_NAMESPACE::TensorProto& new_initializer,
                                    Tensor&& tensor) {
  OrtValue ort_value;
  if (utils::HasExternalDataInMemory(new_initializer)) {
    Tensor::InitOrtValue(std::move(tensor), ort_value);
  }
 
  ORT_THROW_IF_ERROR(graph.AddInitializedOrtValue(new_initializer, ort_value));
  return GetOrCreateNodeArg(graph, new_initializer);
}
 
NodeArg& AddInitializerWithOrtValue(Graph& graph, const ONNX_NAMESPACE::TensorProto& new_initializer,
                                    OrtValue ort_value) {
  ORT_THROW_IF_ERROR(graph.AddInitializedOrtValue(new_initializer, ort_value));
  return GetOrCreateNodeArg(graph, new_initializer);
}
 
void MakeInitializerCopyIfNotExist(const Graph& src_graph, Graph& dst_graph, const std::string& name,
                                   bool copy_in_memory_data) {
  const ONNX_NAMESPACE::TensorProto* initializer = nullptr;
  if (src_graph.GetInitializedTensor(name, initializer)) {
    // check if the initializer already exists in the destination graph
    const ONNX_NAMESPACE::TensorProto* existing = nullptr;
    if (!dst_graph.GetInitializedTensor(name, existing)) {
      const bool data_in_memory = utils::HasExternalDataInMemory(*initializer);
      if (data_in_memory) {
        OrtValue ort_value = GetValidatedInMemoryInitializer(src_graph, *initializer);
        if (copy_in_memory_data) {
          ONNX_NAMESPACE::TensorProto tensor_proto;
          ORT_THROW_IF_ERROR(utils::TensorProtoWithExternalDataToTensorProto(*initializer, {}, tensor_proto));
          dst_graph.AddInitializedTensor(tensor_proto);
          GetOrCreateNodeArg(dst_graph, tensor_proto);
        } else {
          // add the initializer to the destination graph
          ORT_THROW_IF_ERROR(dst_graph.AddInitializedOrtValue(*initializer, ort_value));
          GetOrCreateNodeArg(dst_graph, *initializer);
        }
      } else {
        dst_graph.AddInitializedTensor(*initializer);
        GetOrCreateNodeArg(dst_graph, *initializer);
      }
    }
  }
}
 
void MakeConstantInitializerCopyIfNotExist(const Graph& src_graph, Graph& dst_graph,
                                           const std::string& name, bool check_outer_scope) {
  const auto* initializer = src_graph.GetConstantInitializer(name, check_outer_scope);
  if (initializer != nullptr) {
    const ONNX_NAMESPACE::TensorProto* subgraph_initializer = nullptr;
    if (!dst_graph.GetInitializedTensor(name, subgraph_initializer)) {
      OrtValue ort_value;
      ORT_IGNORE_RETURN_VALUE(src_graph.GetOrtValueInitializer(name, ort_value, check_outer_scope));
      ORT_THROW_IF_ERROR(dst_graph.AddInitializedOrtValue(*initializer, ort_value));
    }
  }
}
 
Status ConvertInMemoryDataToInline(Graph& graph, const std::string& name) {
  const ONNX_NAMESPACE::TensorProto* initializer = nullptr;
  if (graph.GetInitializedTensor(name, initializer) && utils::HasExternalDataInMemory(*initializer)) {
    ORT_IGNORE_RETURN_VALUE(GetValidatedInMemoryInitializer(graph, *initializer));
    ONNX_NAMESPACE::TensorProto tensor_proto;
    ORT_THROW_IF_ERROR(utils::TensorProtoWithExternalDataToTensorProto(*initializer, {}, tensor_proto));
    graph.RemoveInitializedTensor(name);
    graph.AddInitializedTensor(tensor_proto);
  }
  return Status::OK();
}
 
int GetNodeOutputIndexFromOutputName(const Node& node, const std::string& output_name) {
  return GetIndexFromName(node, output_name, false);
}
 
std::vector<const Node*> FindParentsByType(const Node& node, const std::string& parent_type) {
  // find parents and sort them by destination argument index
  // as there is at most one input edge for each input argument,
  // there is no need of extra work like FindChildrenByType
  std::vector<const Node*> parents(node.InputDefs().size(), nullptr);
  for (auto it = node.InputEdgesBegin(); it != node.InputEdgesEnd(); it++) {
    if (it->GetNode().OpType().compare(parent_type) == 0) {
      parents[it->GetDstArgIndex()] = &(it->GetNode());
    }
  }
 
  // remove unmatched nodes
  parents.erase(std::remove(parents.begin(), parents.end(), nullptr), parents.end());
  return parents;
}
 
std::vector<const Node*> FindChildrenByType(const Node& node, const std::string& child_type) {
  // find children and sort them by source argument index:
  //     Create a 2D vector to hold the result.
  //     1st dimension index is output index,
  //     and the 2nd dimension stores the edges from the output.
  std::vector<std::vector<const Node*>> children(node.OutputDefs().size(), std::vector<const Node*>());
  for (auto it = node.OutputEdgesBegin(); it != node.OutputEdgesEnd(); it++) {
    if (it->GetNode().OpType().compare(child_type) == 0) {
      children[it->GetSrcArgIndex()].push_back(&(it->GetNode()));
    }
  }
 
  // aggregate children
  std::vector<const Node*> agg_res;
  for (size_t output_idx = 0; output_idx < children.size(); output_idx++) {
    agg_res.insert(agg_res.end(), children[output_idx].begin(), children[output_idx].end());
  }
  return agg_res;
}
 
const std::string& GetNodeInputName(const Node& node, int index) {
  const auto& inputs = node.InputDefs();
  ORT_ENFORCE(index >= 0 && static_cast<size_t>(index) < inputs.size(),
              "Attempting to get an input that does not exist.");
  return inputs[index]->Name();
}
 
const std::string& GetNodeOutputName(const Node& node, int index) {
  const auto& outputs = node.OutputDefs();
  ORT_ENFORCE(index >= 0 && static_cast<size_t>(index) < outputs.size(),
              "Attempting to get an output that does not exist.");
  return outputs[index]->Name();
}
 
size_t RemoveNodeOutputEdges(Graph& graph, Node& node) {
  std::vector<GraphEdge> output_edges = GraphEdge::GetNodeOutputEdges(node);
  GraphEdge::RemoveGraphEdges(graph, output_edges);
 
  return output_edges.size();
}
 
size_t RemoveNodeOutputEdges(Graph& graph, Node& node, int output_idx) {
  std::vector<GraphEdge> output_edges = GraphEdge::GetNodeOutputEdges(node, output_idx);
  GraphEdge::RemoveGraphEdges(graph, output_edges);
 
  return output_edges.size();
}
 
const ONNX_NAMESPACE::TensorProto* GetConstantInitializer(const Graph& graph, const std::string& initializer_name,
                                                          bool check_outer_scope) {
  return graph.GetConstantInitializer(initializer_name, check_outer_scope);
}
 
GraphEdge::GraphEdge(NodeIndex src_node,
                     NodeIndex dst_node,
                     int src_arg_index,
                     int dst_arg_index,
                     const std::string& arg_name) : src_node(src_node),
                                                    dst_node(dst_node),
                                                    src_arg_index(src_arg_index),
                                                    dst_arg_index(dst_arg_index),
                                                    arg_name(arg_name) {}
 
// Constructs a GraphEdge given a node, an edge_end, and a boolean for the edge direction.
GraphEdge GraphEdge::CreateGraphEdge(const Node& node, const Node::EdgeEnd& edge_end, bool is_input_edge) {
  return is_input_edge
             ? GraphEdge(edge_end.GetNode().Index(),
                         node.Index(),
                         edge_end.GetSrcArgIndex(),
                         edge_end.GetDstArgIndex(),
                         GetNodeInputName(node, edge_end.GetDstArgIndex()))
             : GraphEdge(node.Index(),
                         edge_end.GetNode().Index(),
                         edge_end.GetSrcArgIndex(),
                         edge_end.GetDstArgIndex(),
                         GetNodeOutputName(node, edge_end.GetSrcArgIndex()));
}
 
const Node::EdgeEnd* GetInputEdge(const Node& node, int arg_index) {
  for (auto it = node.InputEdgesBegin(), end = node.InputEdgesEnd(); it != end; ++it) {
    if (arg_index == it->GetDstArgIndex()) {
      return &(*it);
    }
  }
  return nullptr;
}
 
/** Returns a vector of the input GraphEdges of a node. */
std::vector<GraphEdge> GraphEdge::GetNodeInputEdges(const Node& node) {
  std::vector<GraphEdge> input_edges;
  for (auto it = node.InputEdgesBegin(), end = node.InputEdgesEnd(); it != end; ++it) {
    input_edges.push_back(GraphEdge::CreateGraphEdge(node, *it, true));
  }
 
  return input_edges;
}
 
/** Returns a vector of the input GraphEdges of a node for the provided input index. */
std::vector<GraphEdge> GraphEdge::GetNodeInputEdges(const Node& node, size_t index) {
  std::vector<GraphEdge> input_edges;
  for (auto it = node.InputEdgesBegin(), end = node.InputEdgesEnd(); it != end; ++it) {
    if (static_cast<size_t>(it->GetDstArgIndex()) == index) {
      input_edges.push_back(GraphEdge::CreateGraphEdge(node, *it, true));
    }
  }
 
  return input_edges;
}
 
/** Returns a vector of the output GraphEdges of a node. */
std::vector<GraphEdge> GraphEdge::GetNodeOutputEdges(const Node& node) {
  std::vector<GraphEdge> output_edges;
  for (auto it = node.OutputEdgesBegin(), end = node.OutputEdgesEnd(); it != end; ++it) {
    output_edges.push_back(GraphEdge::CreateGraphEdge(node, *it, false));
  }
 
  return output_edges;
}
 
/** Returns a vector of output GraphEdges of a node for the provided output index. */
std::vector<GraphEdge> GraphEdge::GetNodeOutputEdges(const Node& node, size_t index) {
  std::vector<GraphEdge> output_edges;
  for (auto it = node.OutputEdgesBegin(), end = node.OutputEdgesEnd(); it != end; ++it) {
    if (static_cast<size_t>(it->GetSrcArgIndex()) == index) {
      output_edges.push_back(GraphEdge::CreateGraphEdge(node, *it, false));
    }
  }
 
  return output_edges;
}
 
/** Removes a set of GraphEdges from the graph. */
void GraphEdge::RemoveGraphEdges(Graph& graph, const std::vector<GraphEdge>& edges) {
  for (const auto& edge_to_remove : edges) {
    graph.RemoveEdge(edge_to_remove.src_node,
                     edge_to_remove.dst_node,
                     edge_to_remove.src_arg_index,
                     edge_to_remove.dst_arg_index);
  }
}
 
bool IsSupportedProvider(const Node& node,
                         const InlinedHashSet<std::string_view>& compatible_providers) {
  return !(!compatible_providers.empty() &&
           compatible_providers.find(node.GetExecutionProviderType()) == compatible_providers.end());
}
 
#endif  // !defined(ORT_MINIMAL_BUILD) || defined(ORT_EXTENDED_MINIMAL_BUILD)
 
#if !defined(ORT_MINIMAL_BUILD)
 
int GetNodeInputIndexFromInputName(const Node& node, const std::string& input_name) {
  return GetIndexFromName(node, input_name, true);
}
 
/** Checks for nodes with >= 1 outputs, if only one of the outputs is input to downstream Operators.
Returns the name of the single used output in output_name. */
static bool IsOnlyOneOutputUsed(const Graph& graph, const Node& node, const std::string*& output_name) {
  constexpr int unassigned = -1;
  int first_output = unassigned;
 
  // check that there are only edges for one output, and set the output_name
  if (node.GetOutputEdgesCount() > 0) {
    for (auto it = node.OutputEdgesBegin(), end = node.OutputEdgesEnd(); it != end; ++it) {
      if (first_output == unassigned) {
        first_output = it->GetSrcArgIndex();
      } else if (first_output != it->GetSrcArgIndex()) {
        return false;
      }
    }
 
    output_name = &node.OutputDefs()[first_output]->Name();
  }
 
  // outputs could also be direct graph outputs so check if there are any graph outputs that
  // a) there's only 1, and b) it's the same as any output consumed by another node
  auto output_indexes = graph.GetNodeOutputsInGraphOutputs(node);
  auto num_graph_outputs = output_indexes.size();
  if (num_graph_outputs > 1) {
    return false;
  } else if (num_graph_outputs == 1) {
    if (first_output != unassigned) {
      // an output is consumed by other nodes, so make sure the same output is providing the graph output
      return output_indexes.front() == first_output;
    } else {
      // graph output only as no other nodes are consuming the output, so just update the output_name
      output_name = &node.OutputDefs()[output_indexes.front()]->Name();
    }
  }
 
  return true;
}
 
bool IsOutputUsed(const Node& node, int index) {
  for (auto it = node.OutputEdgesBegin(), end = node.OutputEdgesEnd(); it != end; ++it) {
    if (it->GetSrcArgIndex() == index) {
      return true;
    }
  }
  return false;
}
 
bool CanRemoveNode(const Graph& graph, const Node& node, const logging::Logger& logger) {
  const std::string* output_name = nullptr;
  if (!IsOnlyOneOutputUsed(graph, node, output_name)) {
    return false;
  }
 
  // TODO: Currently we remove the node and use the input name from the node being removed.
  // It may also be possible to instead update an upstream node to use the output name from the node being removed.
  // This would allow removal of a node that is providing a graph output, as that output name would come from updating
  // the upstream node. This should also enable removal if CanUpdateImplicitInputNameInSubgraphs returns false.
 
  if (graph.NodeProducesGraphOutput(node)) {
    return false;
  }
 
  bool can_remove = false;
  const std::string* new_name = nullptr;
 
  if (node.GetInputEdgesCount() == 1) {
    // we will merge the single input edge with the edges for the output that is used
    // Note that the node may have other inputs coming from initializers or graph inputs that do not have edges.
    new_name = &GetNodeInputName(node, node.InputEdgesBegin()->GetDstArgIndex());
  } else if (node.InputDefs().size() == 1) {
    // we can also handle a node with a single input from an initializer or graph input (no edges)
    new_name = &node.InputDefs()[0]->Name();
  } else {
    // No other node removal is supported
  }
 
  if (new_name) {
    // Check that changing the current output name to the new name won't break any subgraphs that consume it
    std::vector<GraphEdge> output_edges = GraphEdge::GetNodeOutputEdges(node);
    can_remove = CanUpdateImplicitInputNameInSubgraphs(graph, output_edges, *new_name, logger);
  }
 
  return can_remove;
}
 
bool RemoveNode(Graph& graph, Node& node) {
  // TODO: enable the check back
  // assert(CanRemoveNode(graph, node, nullptr));
 
  // Note: Node does not produce any graph outputs, and only a single output is used.
 
  // If there is a single input edge from another node (initializers are not connected with edges to nodes)
  if (node.GetInputEdgesCount() == 1) {
    // remove the node and wire its incoming node to its outgoing node/s
    return RemoveNodeWithSingleNodeInSingleUsedOutput(graph, node);
  }
 
  // single input def so replace node with that
  if (node.InputDefs().size() == 1) {
    return ReplaceNodeWithInitializer(graph, node, *node.MutableInputDefs()[0]);
  }
 
  ORT_THROW("Should be unreachable if CanRemoveNodeAndMergeEdges is in sync with the logic here.");
}
 
bool CanReplaceNodeWithInitializer(const Graph& graph, const Node& node, const std::string& initializer_name,
                                   const logging::Logger& logger) {
  // we have no way to handle replacing multiple outputs so check only one is used
  const std::string* output_name = nullptr;
  if (!IsOnlyOneOutputUsed(graph, node, output_name) || output_name == nullptr) {
    return false;
  }
 
  bool output_name_is_changing = *output_name != initializer_name;
 
  auto num_graph_outputs = graph.GetNodeOutputsInGraphOutputs(node).size();
  if (num_graph_outputs > 0) {
    // Cannot remove a node that provides more than one graph output,
    // or a node whose single graph output is not being replaced by an initializer with the same name
    if (num_graph_outputs > 1 || output_name_is_changing) {
      return false;
    }
  }
 
  bool can_remove = true;
 
  if (output_name_is_changing) {
    // Check that changing the current output name to the new name won't break any subgraphs
    // that consume the current name
    std::vector<GraphEdge> output_edges = GraphEdge::GetNodeOutputEdges(node);
    can_remove = CanUpdateImplicitInputNameInSubgraphs(graph, output_edges, initializer_name, logger);
  }
 
  return can_remove;
}
 
bool ReplaceNodeWithInitializer(Graph& graph, Node& node, NodeArg& replacement) {
  const NodeIndex node_index = node.Index();
  // We have to remove the output edges before we create replacement ones, so save the current output edge information
  std::vector<GraphEdge> output_edges = GraphEdge::GetNodeOutputEdges(node);
 
  // Remove the output edges of the node and then the node (this will remove any input edges).
  RemoveNodeOutputEdges(graph, node);
  graph.RemoveNode(node_index);
  graph.NotifyNodesRemoved(gsl::span<const NodeIndex>{&node_index, 1});
 
  // Re-create the output edges using 'replacement' as the source NodeArg (input) to the destination node/s
  for (auto& output_edge : output_edges) {
    // Take care of subgraph inputs.
    if (OutputEdgeProvidesImplicitInput(graph, output_edge)) {
      Node& mutable_output_edge_node = *graph.GetNode(output_edge.dst_node);
      UpdateImplicitInputNameInSubgraph(mutable_output_edge_node, output_edge.arg_name,
                                        replacement.Name());
    }
 
    // Replace outgoing node's input.
    auto& output_node = *graph.GetNode(output_edge.dst_node);
    ReplaceNodeInput(output_node, output_edge.dst_arg_index, replacement);
  }
 
  return true;
}
 
bool IsGraphInput(const Graph& graph, const NodeArg* input) {
  const std::vector<const NodeArg*>& graph_inputs = graph.GetInputsIncludingInitializers();
  return std::find(graph_inputs.begin(), graph_inputs.end(), input) != graph_inputs.end();
}
 
bool IsGraphOutput(const Graph& graph, const NodeArg* output) {
  const auto& graph_outputs = graph.GetOutputs();
  return std::find(graph_outputs.begin(), graph_outputs.end(), output) != graph_outputs.end();
}
 
bool IsInitializer(const Graph& graph, const std::string& name, bool check_outer_scope) {
  bool is_initializer = false;
  const ONNX_NAMESPACE::TensorProto* initializer = nullptr;
  if (graph.GetInitializedTensor(name, initializer)) {
    is_initializer = true;
  } else if (check_outer_scope && graph.IsSubgraph() && graph.IsOuterScopeValue(name)) {
    is_initializer = IsInitializer(*graph.ParentGraph(), name, check_outer_scope);
  }
 
  return is_initializer;
}
 
bool IsConstantInitializer(const Graph& graph, const std::string& initializer_name, bool check_outer_scope) {
  const ONNX_NAMESPACE::TensorProto* initializer = GetConstantInitializer(graph, initializer_name, check_outer_scope);
  return initializer != nullptr;
}
 
bool NodeArgIsConstant(const Graph& graph, const NodeArg& node_arg) {
  return IsConstantInitializer(graph, node_arg.Name(), true);
}
 
bool AllNodeInputsAreConstant(const Graph& graph, const Node& node, InitializedTensorSet& constant_inputs,
                              const InlinedHashSet<std::string>& excluded_initializers) {
  // clear so we have a known state. if we fail part way through we go back to this state.
  constant_inputs.clear();
 
  // only initializers can be constant. There's no edge from a node to an initializer
  // so the input edges count will be 0 if all the inputs are initializers.
  if (node.GetInputEdgesCount() > 0) {
    return false;
  }
 
  for (const auto* input_def : node.InputDefs()) {
    // For optional node inputs which are missing, we can safely ignore them
    if (input_def->Name().empty()) {
      continue;
    }
 
    // Important note: when an initializer appears in the graph's input, this input will not be considered constant,
    // because it can be overridden by the user at runtime. For constant folding to be applied, the initializer should
    // not appear in the graph's inputs (that is the only way to guarantee it will always be constant).
    const ONNX_NAMESPACE::TensorProto* initializer = GetConstantInitializer(graph, input_def->Name(), true);
    if (initializer && excluded_initializers.find(input_def->Name()) == excluded_initializers.cend()) {
      constant_inputs.insert({input_def->Name(), initializer});
    } else {
      constant_inputs.clear();
      return false;
    }
  }
 
  return true;
}
 
const Node* FirstChildByType(const Node& node, const std::string& child_type) {
  for (auto it = node.OutputNodesBegin(); it != node.OutputNodesEnd(); ++it) {
    if ((*it).OpType().compare(child_type) == 0) {
      return &(*it);
    }
  }
  return nullptr;
}
 
const Node* FirstParentByType(const Node& node, const std::string& parent_type) {
  for (auto it = node.InputNodesBegin(); it != node.InputNodesEnd(); ++it) {
    if ((*it).OpType().compare(parent_type) == 0) {
      return &(*it);
    }
  }
  return nullptr;
}
 
void ReplaceDownstreamNodeInput(Graph& graph, Node& node, int output_idx, Node& replacement,
                                int replacement_output_idx) {
  // get the output edges from node for output_idx
  std::vector<GraphEdge> output_edges = GraphEdge::GetNodeOutputEdges(node, output_idx);
 
  if (!output_edges.empty()) {
    const auto& replacement_name = replacement.MutableOutputDefs()[replacement_output_idx]->Name();
 
    // Remove the output edges of the node first
    GraphEdge::RemoveGraphEdges(graph, output_edges);
 
    // Create connections between the replacement node and the outgoing nodes
    for (const auto& output_edge : output_edges) {
      // Take care of subgraph inputs.
      if (OutputEdgeProvidesImplicitInput(graph, output_edge)) {
        Node& mutable_output_edge_node = *graph.GetNode(output_edge.dst_node);
        UpdateImplicitInputNameInSubgraph(mutable_output_edge_node, output_edge.arg_name, replacement_name);
      }
 
      // Add new edge connecting the input with the output nodes directly.
      // This also updates the destination node's input node args
      graph.AddEdge(replacement.Index(), output_edge.dst_node, replacement_output_idx, output_edge.dst_arg_index);
    }
  }
}
 
void ReplaceNodeInput(Node& target, int target_input_idx, NodeArg& new_input) {
  size_t dst_arg_idx = static_cast<size_t>(target_input_idx);
  auto num_explicit_inputs = target.InputDefs().size();
 
  if (dst_arg_idx < num_explicit_inputs) {
    target.MutableInputDefs()[target_input_idx] = &new_input;
  } else if (dst_arg_idx < num_explicit_inputs + target.ImplicitInputDefs().size()) {
    // If we need to update an implicit input.
    target.MutableImplicitInputDefs()[dst_arg_idx - num_explicit_inputs] = &new_input;
  } else {
    // logic error in our code
    ORT_THROW("Invalid input index for node ", target.Name(), ". Index:", target_input_idx,
              " ExplicitInputs:", num_explicit_inputs,
              " ImplicitInputs:", target.ImplicitInputDefs().size());
  }
}
 
void AddNodeInput(Node& target, int target_input_idx, NodeArg& new_input) {
  auto num_explicit_inputs = target.InputDefs().size();
  ORT_ENFORCE(num_explicit_inputs == static_cast<size_t>(target_input_idx),
              "Can only add a new input at the end of the current ones.");
 
  target.MutableInputDefs().push_back(&new_input);
 
  // expect existing entry for all possible inputs
  assert(target.MutableInputArgsCount().size() > static_cast<size_t>(target_input_idx));
  target.MutableInputArgsCount()[target_input_idx] = 1;
}
 
void SetOptionalNodeInput(Graph& graph, Node& target, size_t target_input_idx, NodeArg& new_input) {
  auto& input_defs = target.MutableInputDefs();
  auto& input_arg_counts = target.MutableInputArgsCount();
  ORT_ENFORCE(input_arg_counts.size() > target_input_idx,
              "Missing input metadata for optional input ", target_input_idx,
              " of node ", target.Name(), " (", target.OpType(), ").");
 
  const size_t original_size = input_defs.size();
  if (original_size <= target_input_idx) {
    for (size_t index = original_size; index <= target_input_idx; ++index) {
      ORT_ENFORCE(input_arg_counts[index] == 0,
                  "Expected omitted optional input ", index,
                  " of node ", target.Name(), " (", target.OpType(), ").");
    }
  } else if (input_defs[target_input_idx] == nullptr || !input_defs[target_input_idx]->Exists()) {
    ORT_ENFORCE(input_arg_counts[target_input_idx] == 0 || input_arg_counts[target_input_idx] == 1,
                "Expected omitted optional input ", target_input_idx,
                " of node ", target.Name(), " (", target.OpType(), ").");
  }
 
  graph.SetGraphResolveNeeded().SetGraphProtoSyncNeeded();
  if (original_size <= target_input_idx) {
    NodeArg& empty_input = graph.GetOrCreateNodeArg("", nullptr);
    input_defs.resize(target_input_idx + 1, &empty_input);
    for (size_t index = original_size; index <= target_input_idx; ++index) {
      input_arg_counts[index] = 1;
    }
  } else if (input_defs[target_input_idx] == nullptr || !input_defs[target_input_idx]->Exists()) {
    input_arg_counts[target_input_idx] = 1;
  }
 
  input_defs[target_input_idx] = &new_input;
}
 
void FinalizeNodeFusion(Graph& graph, Node& first_node, Node& second_node) {
  const std::array<NodeIndex, 2> source_node_indices{first_node.Index(), second_node.Index()};
 
  // move the outputs from second_node to first_node
  RemoveNodeOutputEdges(graph, first_node);
  MoveAllNodeOutputs(graph, second_node, first_node);
 
  // second node now has no output edges and can be removed
  graph.RemoveNode(second_node.Index());
  graph.NotifyNodeReplacement(source_node_indices, first_node.Index());
}
 
void FinalizeNodeFusion(Graph& graph, gsl::span<const std::reference_wrapper<Node>> nodes, Node& replacement_node_start,
                        Node& replacement_node_end) {
  std::vector<NodeIndex> source_node_indices;
  source_node_indices.reserve(nodes.size());
  for (const Node& node : nodes) {
    source_node_indices.push_back(node.Index());
  }
 
  MoveAllNodeInputEdges(graph, *nodes.begin(), replacement_node_start);
  MoveAllNodeOutputs(graph, nodes.back(), replacement_node_end);
 
  for (Node& node : nodes) {
    RemoveNodeOutputEdges(graph, node);
    graph.RemoveNode(node.Index());
  }
 
  graph.NotifyNodeReplacement(source_node_indices, replacement_node_start.Index());
}
 
const Node* GetInputNode(const Node& node, int arg_index) {
  const Node::EdgeEnd* edge = GetInputEdge(node, arg_index);
  if (nullptr == edge) {
    return nullptr;
  }
  return &(edge->GetNode());
}
 
inline std::string ToString(gsl::span<const ONNX_NAMESPACE::OperatorSetVersion> versions) {
  std::ostringstream output;
  if (!versions.empty()) {
    // Convert all but the last element to avoid a trailing ";"
    std::copy(versions.begin(), versions.end() - 1,
              std::ostream_iterator<ONNX_NAMESPACE::OperatorSetVersion>(output, ";"));
    // Now add the last element with no delimiter
    output << versions.back();
  }
  return output.str();
}
 
bool FindPath(const Node& node, bool is_input_edge, gsl::span<const EdgeEndToMatch> edges_to_match,
              std::vector<const Node::EdgeEnd*>& result, const logging::Logger& logger) {
  result.clear();
  result.reserve(edges_to_match.size());
 
  const Node* current_node = &node;
  for (const auto& edge : edges_to_match) {
    const Node::EdgeEnd* edge_found = nullptr;
#ifndef NDEBUG
    LOGS(logger, VERBOSE) << (is_input_edge ? "I:" : "O:") << edge.src_arg_index << "," << edge.dst_arg_index
                          << "," << edge.op_type << "," << edge.domain << "," << ToString(edge.versions);
#endif
    auto edges_begin = is_input_edge ? current_node->InputEdgesBegin() : current_node->OutputEdgesBegin();
    auto edges_end = is_input_edge ? current_node->InputEdgesEnd() : current_node->OutputEdgesEnd();
    for (auto it = edges_begin; it != edges_end; ++it) {
#ifndef NDEBUG
      LOGS(logger, VERBOSE) << "E:" << it->GetSrcArgIndex() << "," << it->GetDstArgIndex()
                            << "," << it->GetNode().OpType() << "," << it->GetNode().Domain() << ","
                            << it->GetNode().SinceVersion();
#endif
      if (edge.dst_arg_index == it->GetDstArgIndex() &&
          edge.src_arg_index == it->GetSrcArgIndex() &&
          edge.op_type == it->GetNode().OpType() &&
          MatchesOpSinceVersion(it->GetNode(), edge.versions) &&
          MatchesOpSetDomain(it->GetNode(), edge.domain)) {
        // For output edge, there could be multiple edges matched.
        // This function will return failure in such case by design.
        if (nullptr != edge_found) {
          LOGS(logger, WARNING) << "Failed since multiple edges matched:" << current_node->OpType() << "->"
                                << edge.op_type;
          return false;
        }
        edge_found = &(*it);
 
        // For input edge, each dst_arg_index only accepts one input edge so only there is at most one match.
        if (is_input_edge) {
          break;
        }
      }
    }
 
    if (nullptr == edge_found) {
      return false;
    }
 
    result.push_back(edge_found);
    current_node = &(edge_found->GetNode());
  }
 
  return true;
}
 
bool FindPath(Graph& graph, const Node& node, bool is_input_edge, gsl::span<const EdgeEndToMatch> edges_to_match,
              std::vector<std::reference_wrapper<Node>>& result, const logging::Logger& logger) {
  result.clear();
 
  std::vector<const Node::EdgeEnd*> edge_ends;
  if (!FindPath(node, is_input_edge, edges_to_match, edge_ends, logger)) {
    return false;
  }
 
  result.reserve(edges_to_match.size());
  std::transform(edge_ends.begin(), edge_ends.end(), std::back_inserter(result),
                 [&graph](const Node::EdgeEnd* edge_end) -> Node& {
                   return *graph.GetNode(edge_end->GetNode().Index());
                 });
 
  return true;
}
 
bool RemoveNodesWithOneOutputBottomUp(Graph& graph, const Node& start_node,
                                      std::vector<NodeIndex>* removed_node_indices) {
  std::queue<NodeIndex> q;
  InlinedHashSet<NodeIndex> removed_nodes;
  std::vector<NodeIndex> removed_node_indices_local;
 
  NodeIndex start_node_index = start_node.Index();
  q.push(start_node_index);
 
  // From the current node, remove nodes bottom-up util it reaches a node with multiple outputs/graph output.
  while (!q.empty()) {
    NodeIndex cur_node_index = q.front();
    q.pop();
 
    if (removed_nodes.find(cur_node_index) != removed_nodes.end()) {
      continue;
    }
    // Each eligible node in the subgraph must have less than one output edge and no output should be
    // the graph output
    const Node& cur_node = *graph.GetNode(cur_node_index);
    if (cur_node.GetOutputEdgesCount() > 1 || graph.NodeProducesGraphOutput(cur_node)) {
      continue;
    }
 
    // push the parents of current node to the queue.
    for (size_t i = 0; i < cur_node.InputDefs().size(); ++i) {
      const std::string& input_name = GetNodeInputName(cur_node, static_cast<int>(i));
      if (IsInitializer(graph, input_name, true) || IsGraphInput(graph, cur_node.InputDefs()[static_cast<int>(i)])) {
        // skip initializers and graph inputs
        continue;
      }
      const Node* parent_node = GetInputNode(cur_node, static_cast<int>(i));
      if (nullptr == parent_node) {
        continue;
      }
      q.push(parent_node->Index());
    }
 
    if (cur_node_index == start_node_index || cur_node.GetOutputEdgesCount() == 0) {
      Node* cur_node_p = graph.GetNode(cur_node_index);
      RemoveNodeOutputEdges(graph, *cur_node_p);
      graph.RemoveNode(cur_node_index);
 
      removed_nodes.insert(cur_node_index);
      removed_node_indices_local.push_back(cur_node_index);
    }
  }
 
  if (removed_node_indices_local.empty()) {
    // Nothing to remove
    return false;
  }
 
  if (removed_node_indices != nullptr) {
    removed_node_indices->insert(removed_node_indices->end(),
                                 removed_node_indices_local.cbegin(), removed_node_indices_local.cend());
  } else {
    graph.NotifyNodesRemoved(removed_node_indices_local);
  }
 
  return true;
}
 
NodeArg& CreateNodeArg(Graph& graph, const NodeArg& base_arg) {
  return graph.GetOrCreateNodeArg(graph.GenerateNodeArgName(base_arg.Name()), base_arg.TypeAsProto());
}
 
#endif  // !defined(ORT_MINIMAL_BUILD)
}  // namespace graph_utils
}  // namespace onnxruntime