Исходный код СУБД 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#include "test/ep_graph/test_ep_graph_utils.h"
 
#include "core/graph/ep_api_types.h"
#include "core/graph/model.h"
 
namespace onnxruntime {
namespace test {
 
TestGraph::TestGraph(std::shared_ptr<Model> model)
    : model(model), graph_viewer(model->MainGraph()) {
  std::unique_ptr<EpGraph> ep_graph = nullptr;
  ORT_ENFORCE(EpGraph::Create(graph_viewer, ep_graph).IsOK());
  api_graph = std::move(ep_graph);
}
 
TestGraph::~TestGraph() {}
 
std::unique_ptr<TestGraph> TestGraph::Load(const ORTCHAR_T* model_path) {
  std::shared_ptr<Model> model;
  auto status = Model::Load(model_path, model, nullptr, DefaultLoggingManager().DefaultLogger());
  if (!status.IsOK()) {
    return nullptr;
  }
 
  return std::make_unique<TestGraph>(model);
}
 
const OrtGraph& TestGraph::GetOrtGraph() const { return *api_graph; }
const GraphViewer& TestGraph::GetGraphViewer() const { return graph_viewer; }
const Model& TestGraph::GetModel() const { return *model; }
 
static Status GetInputIndices(const Node& consumer_node, const std::string& name,
                              /*out*/ std::vector<int64_t>& indices) {
  bool found = false;
  auto add_input_indices =
      [&found, &name, &indices](ConstPointerContainer<std::vector<NodeArg*>> input_defs,
                                bool is_implicit) -> void {
    for (size_t i = 0; i < input_defs.size(); i++) {
      if (input_defs[i]->Name() == name) {
        indices.push_back(is_implicit ? -1 : static_cast<int64_t>(i));
        found = true;
      }
    }
  };
 
  add_input_indices(consumer_node.InputDefs(), false);
  add_input_indices(consumer_node.ImplicitInputDefs(), true);
 
  ORT_RETURN_IF(!found, "Did not find input indices for NodeArg ", name);
  return Status::OK();
}
 
Status GetOutputIndex(const Node& producer_node, const std::string& name, /*out*/ size_t& index) {
  const auto outputs = producer_node.OutputDefs();
 
  bool found = false;
  for (size_t i = 0; i < outputs.size(); i++) {
    if (outputs[i]->Name() == name) {
      index = i;
      found = true;
    }
  }
  ORT_RETURN_IF(!found, "Did not find output index of NodeArg ", name);
  return Status::OK();
}
 
Status GetNodeArgConsumers(const GraphViewer& graph_viewer, const NodeArg& node_arg,
                           /*out*/ std::vector<NodeArgConsumer>& consumers) {
  std::vector<const Node*> nodes = graph_viewer.GetConsumerNodes(node_arg.Name());
  if (nodes.empty()) {
    return Status::OK();
  }
 
  consumers.reserve(nodes.size());
  for (const Node* node : nodes) {
    bool within_graph_viewer = node != nullptr && graph_viewer.GetNode(node->Index()) != nullptr;
    if (!within_graph_viewer) {
      continue;  // Node is not in this GraphViewer
    }
 
    std::vector<int64_t> input_indices;
    ORT_RETURN_IF_ERROR(GetInputIndices(*node, node_arg.Name(), input_indices));
 
    for (int64_t input_index : input_indices) {
      consumers.emplace_back(node, input_index);
    }
  }
  return Status::OK();
}
 
// Get the number of input edges that come from another node upstream.
Ort::Status GetNodeInputEdgeCount(const OrtNode* node, size_t& num_input_edges) {
  const OrtApi& ort_api = Ort::GetApi();
 
  size_t num_inputs = 0;
  RETURN_IF_API_ERROR(ort_api.Node_GetNumInputs(node, &num_inputs));
 
  std::vector<const OrtValueInfo*> inputs(num_inputs);
  RETURN_IF_API_ERROR(ort_api.Node_GetInputs(node, inputs.data(), inputs.size()));
 
  // Sum the number of inputs with a producer node.
  num_input_edges = 0;
 
  for (const OrtValueInfo* ort_input : inputs) {
    Ort::ConstValueInfo input{ort_input};
    if (input == nullptr) continue;  // Skip missing optional input
 
    auto producer_info = input.GetProducerNode();
    num_input_edges += static_cast<size_t>(producer_info.node != nullptr);
  }
 
  return Ort::Status{nullptr};
}
 
// Get all output nodes that consume an output from the given node.
Ort::Status GetOutputNodes(const OrtNode* node, std::vector<Ort::ConstNode>& result) {
  const OrtApi& ort_api = Ort::GetApi();
 
  size_t num_outputs = 0;
  RETURN_IF_API_ERROR(ort_api.Node_GetNumOutputs(node, &num_outputs));
 
  std::vector<const OrtValueInfo*> outputs(num_outputs);
  RETURN_IF_API_ERROR(ort_api.Node_GetOutputs(node, outputs.data(), outputs.size()));
 
  std::vector<Ort::ConstNode> output_nodes;
  output_nodes.reserve(num_outputs);  // May have more than `num_outputs`
 
  // Gather the OrtNode consumers of every output.
  for (const OrtValueInfo* ort_output : outputs) {
    Ort::ConstValueInfo output{ort_output};
    if (output == nullptr) continue;  // Skip missing optional output
 
    auto consumers_info = output.GetConsumers();
    for (const auto& consumer : consumers_info) {
      output_nodes.push_back(consumer.node);
    }
  }
 
  result = std::move(output_nodes);
  return Ort::Status{nullptr};
}
 
// Kahn's topological sort.
// Adapted from onnxruntime/core/graph/graph.cc to use public C API graph types.
Ort::Status KahnsTopologicalSort(const OrtGraph& graph,
                                 const std::function<void(const OrtNode*)>& enter,
                                 const std::function<bool(const OrtNode*, const OrtNode*)>& comp) {
  const OrtApi& ort_api = Ort::GetApi();
 
  try {
    // Get all nodes
    size_t num_nodes = 0;
    RETURN_IF_API_ERROR(ort_api.Graph_GetNumNodes(&graph, &num_nodes));
 
    if (num_nodes == 0) {
      return Ort::Status{nullptr};  // Nothing to sort.
    }
 
    std::vector<const OrtNode*> nodes(num_nodes);
    RETURN_IF_API_ERROR(ort_api.Graph_GetNodes(&graph, nodes.data(), nodes.size()));
 
    // Get the maximum node ID. Not really required if we chose to represent the `in_degree` as a map instead of vector.
    size_t max_node_id = 0;
    for (const OrtNode* node : nodes) {
      size_t node_id = 0;
      RETURN_IF_API_ERROR(ort_api.Node_GetId(node, &node_id));
      max_node_id = std::max(max_node_id, node_id);
    }
 
    std::vector<size_t> in_degree(max_node_id + 1, 0);
    std::vector<size_t> topo_order;
    VisitorPriorityQueue<const OrtNode*> to_visit(comp);
 
    topo_order.reserve(num_nodes);
 
    // Initialize in_degree and initial nodes to visit first.
    for (const OrtNode* node : nodes) {
      size_t input_edge_count = 0;
      RETURN_IF_API_ERROR(GetNodeInputEdgeCount(node, input_edge_count));
 
      size_t node_id = 0;
      RETURN_IF_API_ERROR(ort_api.Node_GetId(node, &node_id));
 
      in_degree[node_id] = input_edge_count;
      if (input_edge_count == 0) {
        to_visit.push(node);
      }
    }
 
    while (!to_visit.empty()) {
      const OrtNode* current_node = to_visit.top();
      to_visit.pop();
 
      if (!current_node) continue;
 
      if (enter) {
        enter(current_node);
      }
 
      std::vector<Ort::ConstNode> output_nodes;
      RETURN_IF_API_ERROR(GetOutputNodes(current_node, output_nodes));
 
      for (const auto& output_node : output_nodes) {
        size_t output_node_id = 0;
        RETURN_IF_API_ERROR(ort_api.Node_GetId(output_node, &output_node_id));
 
        auto& node_in_degree = in_degree[output_node_id];
        node_in_degree--;
 
        if (node_in_degree == 0) {
          to_visit.push(output_node);
        }
      }
 
      size_t current_node_id = 0;
      RETURN_IF_API_ERROR(ort_api.Node_GetId(current_node, &current_node_id));
      topo_order.push_back(current_node_id);
    }
 
    if (num_nodes != topo_order.size()) {
      return Ort::Status("Some nodes are not included in the topological sort: graph has a cycle", ORT_FAIL);
    }
  } catch (const Ort::Exception& ex) {
    Ort::Status status(ex);
    return status;
  } catch (const std::exception& ex) {
    Ort::Status status(ex.what(), ORT_EP_FAIL);
    return status;
  }
 
  return Ort::Status{nullptr};
}
}  // namespace test
}  // namespace onnxruntime