Исходный код СУБД 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#include "core/graph/graph_viewer.h"
#include "core/graph/indexed_sub_graph.h"
 
namespace onnxruntime {
 
bool NodeCompare::operator()(const Node* n1, const Node* n2) const {
  return n1->Index() < n2->Index();
}
 
#if !defined(ORT_MINIMAL_BUILD)
struct PriorityNodeCompare {
  inline bool IsHighPri(const Node* n) const {
    // local statics so we can compare std::strings in the checks
    static constexpr std::string_view shape_op("Shape");
    static constexpr std::string_view size_op("Size");
 
    const auto& op_type = n->OpType();
    return op_type == shape_op || op_type == size_op;
  }
 
  // Used for std::priority_queue
  // If return false, n1 will be output first
  // If return true, n2 will be output first
  bool operator()(const Node* n1, const Node* n2) const {
    // nodes in global high priority list will be output first
    const bool isN1HighPri = IsHighPri(n1);
    const bool isN2HighPri = IsHighPri(n2);
    if (isN1HighPri != isN2HighPri) {
      return isN2HighPri;
    }
 
    // nodes with lower priority value will be output first
    const auto n1_priority = n1->Priority();
    const auto n2_priority = n2->Priority();
    if (n1_priority != n2_priority) {
      return n1_priority > n2_priority;
    }
 
    // otherwise, nodes with lower index will be output first
    return n1->Index() > n2->Index();
  }
};
#endif
 
GraphViewer::GraphViewer(const Graph& graph)
    : GraphViewer(graph, nullptr) {
}
 
GraphViewer::GraphViewer(const Graph& graph, const IndexedSubGraph& filter_info)
    : GraphViewer(graph, &filter_info) {
}
 
GraphViewer::GraphViewer(const Graph& graph, const IndexedSubGraph* filter_info)
    : graph_{&graph},
      // we can setup the filter here if needed. filtered_node_indices_ will have been populated by the time it's used
      graph_nodes_{graph_->FilteredNodes(
          filter_info ? [this](NodeIndex idx) { return filtered_node_indices_.count(idx) == 0; }
                      : ConstGraphNodes::NodeFilterFunc(nullptr))},
      filter_info_{filter_info} {
  std::vector<const Node*> leaf_nodes;
 
#ifdef ENABLE_TRAINING
  const Node* yield_node = nullptr;
 
  // Keep the info of shape and size nodes and their parents so that after topological sort, we can move them
  // right after their parents. This is to make sure the shape and size nodes are executed right after their parents
  // so it's possible the input tensor memory can be released as soon as possible. This is especially important
  // for non-CPU devices or for training case where some gradient graphs use only shape/size of tensors from forward.
  InlinedHashSet<NodeIndex> shape_size_nodes;
  InlinedHashMap<NodeIndex, InlinedVector<NodeIndex>> shape_size_parents;
#endif
  for (auto& node : graph_->Nodes()) {
    // This is a leaf node (without any output node)
    if (node.OutputNodesBegin() == node.OutputNodesEnd()) {
      leaf_nodes.push_back(&node);
    }
    // This is a root node (without any input node)
    if (node.InputEdgesBegin() == node.InputEdgesEnd()) {
      root_nodes_.push_back(node.Index());
    }
#ifdef ENABLE_TRAINING
    if ((node.OpType() == "Shape" || node.OpType() == "Size") && node.InputEdgesBegin() != node.InputEdgesEnd()) {
      shape_size_nodes.insert(node.Index());
      NodeIndex parent = node.InputNodesBegin()->Index();
      if (shape_size_parents.find(parent) == shape_size_parents.end()) {
        shape_size_parents[parent] = InlinedVector<NodeIndex>{node.Index()};
      } else {
        shape_size_parents[parent].push_back(node.Index());
      }
    }
 
    if (node.OpType() == "YieldOp") {
      yield_node = &node;
    }
#endif
  }
 
  graph.ReverseDFSFrom(
      leaf_nodes,
      nullptr,
      [this](const Node* n) {
        nodes_in_topological_order_.push_back(n->Index());
      },
      NodeCompare());
 
#ifdef ENABLE_TRAINING
  auto original = std::move(nodes_in_topological_order_);
  nodes_in_topological_order_.reserve(original.size());
  InlinedHashSet<NodeIndex> visited;
  for (auto& node : original) {
    if (visited.find(node) != visited.end()) {
      continue;
    }
    nodes_in_topological_order_.push_back(node);
    visited.insert(node);
    if (shape_size_parents.find(node) != shape_size_parents.end()) {
      for (auto& following_node : shape_size_parents[node]) {
        nodes_in_topological_order_.push_back(following_node);
        visited.insert(following_node);
      }
    }
  }
 
#endif
 
#if !defined(ORT_MINIMAL_BUILD)
  graph_->KahnsTopologicalSort(
      [this](const Node* n) {
        nodes_in_topological_order_with_priority_.push_back(n->Index());
      },
      PriorityNodeCompare());
#endif
 
#ifdef ENABLE_TRAINING
  if (yield_node != nullptr) {
    std::vector<NodeIndex> node_orders;
    const size_t num_of_nodes = NumberOfNodes();
    node_orders.reserve(num_of_nodes);
    graph_->MemoryEfficientTopologicalSort(
        yield_node,
        shape_size_parents,
        node_orders);
 
    ORT_ENFORCE(node_orders.size() == num_of_nodes,
                "Topological sort failed.", node_orders.size(), "!=", num_of_nodes);
    nodes_in_mem_efficient_topological_order_ = std::move(node_orders);
  } else {
    nodes_in_mem_efficient_topological_order_ = nodes_in_topological_order_;
  }
#endif
 
  if (filter_info_) {
    // validate. if something is off here it's a bug in our code
    for (NodeIndex idx : filter_info->nodes) {
      ORT_ENFORCE(graph_->GetNode(idx) != nullptr, "IndexedSubGraph contains values not present in the Graph");
    }
 
    // create set of node indexes as we need quick lookups and don't care about the order
    filtered_node_indices_ = FilteredNodeSet(filter_info->nodes.cbegin(),
                                             filter_info->nodes.cend());
 
    const auto& metadef = filter_info->GetMetaDef();
 
    filtered_node_inputs_.reserve(metadef->inputs.size());
    filtered_node_inputs_including_initializers_.reserve(metadef->inputs.size());
 
    for (const auto& input : metadef->inputs) {
      // NodeArgs from the current scope or any outer scopes should be handled correctly.
      //
      // There is an edge case where the model consists of a graph with subgraphs nested across three levels.
      // In this scenario, a third-layer subgraph consumes an input from the first-layer graph (not an initializer).
      // When constructing a new GraphViewer for the second- and third-layer subgraphs,
      // the second-layer graph may not have the corresponding value_info for that first-layer input,
      // because the second-layer graph itself doesn't consume it.
      // Therefore, when working within the second-layer graph, we need to search outer scopes for the missing value_info.
      const auto* nodearg = graph.GetNodeArgIncludingParentGraphs(input);
      ORT_ENFORCE(nodearg, "Mismatch between Graph and IndexedSubGraph. Input not found:", input);
      filtered_node_inputs_including_initializers_.push_back(nodearg);
      if (!graph.IsInitializedTensor(input)) {
        filtered_node_inputs_.push_back(nodearg);
      }
    }
 
    for (const auto& output : metadef->outputs) {
      const auto* nodearg = graph.GetNodeArgIncludingParentGraphs(output);
      ORT_ENFORCE(nodearg, "Mismatch between Graph and IndexedSubGraph. Output not found:", output);
      filtered_node_outputs_.push_back(nodearg);
    }
 
    // filter nodes in topo order to just the nodes in filter_info_
    auto orig_order = std::move(nodes_in_topological_order_);
    nodes_in_topological_order_.reserve(filter_info->nodes.size());
    std::copy_if(orig_order.cbegin(), orig_order.cend(), std::back_inserter(nodes_in_topological_order_),
                 [this](NodeIndex idx) { return filtered_node_indices_.count(idx) != 0; });
 
    // Filter the initializers also
    // Get the names of all the inputs and implicit inputs of all the nodes in this subgraph
    for (const auto node_idx : filtered_node_indices_) {
      const auto* node = GetNode(node_idx);
      ORT_ENFORCE(node, "Mismatch between Graph and IndexedSubGraph. Node not found: ", node_idx);
      const ONNX_NAMESPACE::TensorProto* tensor = nullptr;
      for (const auto* node_input : node->InputDefs()) {
        if (graph.GetInitializedTensor(node_input->Name(), tensor)) {
          filtered_initializers_.insert({node_input->Name(), tensor});
        }
      }
 
      // The implicit inputs for subgraphs (if any)
      for (const auto* node_input : node->ImplicitInputDefs()) {
        if (graph.GetInitializedTensor(node_input->Name(), tensor)) {
          filtered_initializers_.insert({node_input->Name(), tensor});
        }
      }
    }
 
#if !defined(ORT_MINIMAL_BUILD)
    auto orig_priority_order = std::move(nodes_in_topological_order_with_priority_);
    nodes_in_topological_order_with_priority_.reserve(filter_info_->nodes.size());
    std::copy_if(orig_priority_order.cbegin(), orig_priority_order.cend(),
                 std::back_inserter(nodes_in_topological_order_with_priority_),
                 [this](NodeIndex idx) { return filtered_node_indices_.count(idx) != 0; });
#endif
 
#ifdef ENABLE_TRAINING
    auto orig_mem_efficient_order = std::move(nodes_in_mem_efficient_topological_order_);
    nodes_in_mem_efficient_topological_order_.reserve(filter_info_->nodes.size());
    std::copy_if(orig_mem_efficient_order.cbegin(), orig_mem_efficient_order.cend(),
                 std::back_inserter(nodes_in_mem_efficient_topological_order_),
                 [this](NodeIndex idx) { return filtered_node_indices_.count(idx) != 0; });
#endif
  }
}
 
// Graph name.
const std::string& GraphViewer::Name() const noexcept {
  return (filter_info_ == nullptr) ? graph_->Name()
                                   : filter_info_->GetMetaDef()->name;
}
 
const std::string& GraphViewer::Description() const noexcept {
  // filter_info_ doesn't have description so return 'name' instead of nothing
  // and to disambiguate between the full graph's description
  return (filter_info_ == nullptr) ? graph_->Description()
                                   : filter_info_->GetMetaDef()->name;
}
 
bool GraphViewer::GetInitializedTensor(const std::string& tensor_name,
                                       const ONNX_NAMESPACE::TensorProto*& value) const {
  value = nullptr;
 
  // if we are using filtered subgraph, the initializer has to be part of the subgraph
  if (filter_info_ != nullptr && filtered_initializers_.find(tensor_name) == filtered_initializers_.cend())
    return false;
 
  return graph_->GetInitializedTensor(tensor_name, value);
}
 
bool GraphViewer::CanOverrideInitializer() const noexcept {
  return graph_->CanOverrideInitializer();
}
 
// Graph inputs excluding initializers.
const std::vector<const NodeArg*>& GraphViewer::GetInputs() const noexcept {
  return (filter_info_ == nullptr) ? graph_->GetInputs()
                                   : filtered_node_inputs_;
}
// Graph inputs including initializers. Contains no nullptr values.
// This will match the number and order of inputs from the GraphProto.
const std::vector<const NodeArg*>& GraphViewer::GetInputsIncludingInitializers() const noexcept {
  return (filter_info_ == nullptr) ? graph_->GetInputsIncludingInitializers()
                                   : filtered_node_inputs_including_initializers_;
}
 
// Graph outputs. Should have no nullptr values.
const std::vector<const NodeArg*>& GraphViewer::GetOutputs() const noexcept {
  return (filter_info_ == nullptr) ? graph_->GetOutputs()
                                   : filtered_node_outputs_;
}
 
bool GraphViewer::NodeProducesGraphOutput(const Node& node) const {
  const auto& outputs = GetOutputs();
  auto end_outputs = outputs.cend();
  for (auto output_def : node.OutputDefs()) {
    if (std::find(outputs.cbegin(), end_outputs, output_def) != end_outputs) {
      return true;
    }
  }
  return false;
}
 
// Get graph value infos.
const std::unordered_set<const NodeArg*>& GraphViewer::GetValueInfo() const noexcept {
  return graph_->GetValueInfo();
}
 
// Get const Node given specific node index. May return nullptr if node as been freed.
const Node* GraphViewer::GetNode(NodeIndex node_index) const {
  if (filter_info_ && filtered_node_indices_.count(node_index) == 0) {
    return nullptr;
  }
 
  return graph_->GetNode(node_index);
}
 
const ConstGraphNodes& GraphViewer::Nodes() const noexcept {
  return graph_nodes_;
}
 
int GraphViewer::NumberOfNodes() const noexcept {
  return (filter_info_ == nullptr) ? graph_->NumberOfNodes()
                                   : gsl::narrow_cast<int>(filter_info_->nodes.size());
}
 
int GraphViewer::MaxNodeIndex() const noexcept {
  return graph_->MaxNodeIndex();
}
 
const std::vector<NodeIndex>& GraphViewer::GetNodesInTopologicalOrder(ExecutionOrder order) const {
  switch (order) {
    case ExecutionOrder::DEFAULT:
      return nodes_in_topological_order_;
    case ExecutionOrder::PRIORITY_BASED:
#if !defined(ORT_MINIMAL_BUILD)
      return nodes_in_topological_order_with_priority_;
#else
      ORT_THROW("Priority based topological order is not enabled for ORT minimal build.");
#endif
    case ExecutionOrder::MEMORY_EFFICIENT:
#ifdef ENABLE_TRAINING
      return nodes_in_mem_efficient_topological_order_;
#else
      ORT_THROW("Memory efficient topological order is not enabled for non-training build.");
#endif
    default:
      ORT_THROW("Invalid ExecutionOrder");
  }
}
 
const std::vector<NodeIndex>& GraphViewer::GetRootNodes() const {
  // TODO(somebody): See if we need to calculate the root_nodes_ of the filtered graph.
  // GetRootNodes is only used by parallel executor currently, and isn't relevant to the usage of a filtered graph.
  ORT_ENFORCE(filter_info_ == nullptr, "Not supported with filtered graph.");
 
  return root_nodes_;
}
 
const InitializedTensorSet& GraphViewer::GetAllInitializedTensors() const noexcept {
  return (filter_info_ == nullptr)
             ? graph_->GetAllInitializedTensors()
             : filtered_initializers_;
}
 
const NodeArg* GraphViewer::GetNodeArg(const std::string& name) const {
  return graph_->GetNodeArg(name);
}
 
bool GraphViewer::IsSubgraph() const {
  return graph_->IsSubgraph();
}
 
bool GraphViewer::IsConstantInitializer(const std::string& name, bool check_outer_scope) const {
  return GetConstantInitializer(name, check_outer_scope) != nullptr;
}
 
bool GraphViewer::IsInitializedTensor(const std::string& name) const {
  return graph_->IsInitializedTensor(name);
}
 
const ONNX_NAMESPACE::TensorProto* GraphViewer::GetConstantInitializer(const std::string& initializer_name,
                                                                       bool check_outer_scope) const {
  return graph_->GetConstantInitializer(initializer_name, check_outer_scope);
}
 
#if !defined(ORT_MINIMAL_BUILD)
const std::unordered_set<std::string>& GraphViewer::GetOuterScopeNodeArgNames() const noexcept {
  return graph_->GetOuterScopeNodeArgNames();
}
#endif
 
}  // namespace onnxruntime