Исходный код СУБД 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#if !defined(ORT_MINIMAL_BUILD)
 
#include "core/graph/model_helpers.h"
 
#include <string>
#include <string_view>
#include <unordered_map>
#include <utility>
#include <vector>
 
#include "core/graph/function_utils.h"
#include "core/graph/onnx_protobuf.h"
 
namespace onnxruntime {
 
namespace {
 
using NodeRange = const google::protobuf::RepeatedPtrField<ONNX_NAMESPACE::NodeProto>*;
using PendingNodeRanges = InlinedVector<std::pair<NodeRange, size_t>>;
 
Status AddAttributeSubgraphs(const ONNX_NAMESPACE::AttributeProto& attr,
                             size_t subgraph_depth,
                             PendingNodeRanges& pending) {
  if ((attr.has_g() || !attr.graphs().empty()) && subgraph_depth > kMaxModelSubgraphDepth) {
    return ORT_MAKE_STATUS(
        ONNXRUNTIME, NOT_IMPLEMENTED,
        "Model subgraph depth ", subgraph_depth,
        " exceeds the maximum supported depth of ", kMaxModelSubgraphDepth, ".");
  }
 
  if (attr.has_g()) {
    pending.push_back({&attr.g().node(), subgraph_depth});
  }
  for (const auto& graph : attr.graphs()) {
    pending.push_back({&graph.node(), subgraph_depth});
  }
 
  return Status::OK();
}
 
Status ValidateSubgraphDepth(
    const google::protobuf::RepeatedPtrField<ONNX_NAMESPACE::NodeProto>& root_nodes,
    const google::protobuf::RepeatedPtrField<ONNX_NAMESPACE::AttributeProto>* root_attributes = nullptr) {
  PendingNodeRanges pending{{&root_nodes, 0}};
  if (root_attributes != nullptr) {
    for (const auto& attr : *root_attributes) {
      ORT_RETURN_IF_ERROR(AddAttributeSubgraphs(attr, 1, pending));
    }
  }
 
  while (!pending.empty()) {
    const auto [nodes, depth] = pending.back();
    pending.pop_back();
    for (const auto& node : *nodes) {
      for (const auto& attr : node.attribute()) {
        ORT_RETURN_IF_ERROR(AddAttributeSubgraphs(attr, depth + 1, pending));
      }
    }
  }
 
  return Status::OK();
}
 
// Iterative collection of local function calls from a sequence of nodes,
// including nodes inside nested subgraph attributes. Avoids recursion to
// prevent stack overflow from maliciously deep subgraph nesting.
template <typename NodeRange>
void CollectLocalFunctionCalls(
    const NodeRange& nodes,
    const std::unordered_map<std::string, const ONNX_NAMESPACE::FunctionProto*>& model_local_functions,
    InlinedHashSet<std::string_view>& seen_calls,
    InlinedVector<std::string_view>& called_functions) {
  InlinedVector<const ONNX_NAMESPACE::GraphProto*> pending_graphs;
 
  auto process_nodes = [&](const auto& node_range) {
    for (const auto& node : node_range) {
      const auto function_id = function_utils::GetFunctionIdentifier(
          node.domain(), node.op_type(), node.overload());
      auto it = model_local_functions.find(function_id);
      if (it != model_local_functions.end()) {
        // Use string_view into the map key (stable storage).
        std::string_view key_view = it->first;
        if (seen_calls.insert(key_view).second) {
          called_functions.push_back(key_view);
        }
      }
 
      for (const auto& attr : node.attribute()) {
        if (attr.has_g()) {
          pending_graphs.push_back(&attr.g());
        }
        for (const auto& sub_graph : attr.graphs()) {
          pending_graphs.push_back(&sub_graph);
        }
      }
    }
  };
 
  process_nodes(nodes);
 
  while (!pending_graphs.empty()) {
    const auto* graph = pending_graphs.back();
    pending_graphs.pop_back();
    process_nodes(graph->node());
  }
}
 
}  // namespace
 
Status ValidateModelSubgraphDepth(const ONNX_NAMESPACE::ModelProto& model_proto) {
  ORT_RETURN_IF_ERROR(ValidateSubgraphDepth(model_proto.graph().node()));
  for (const auto& function : model_proto.functions()) {
    ORT_RETURN_IF_ERROR(ValidateFunctionSubgraphDepth(function));
  }
 
  return Status::OK();
}
 
Status ValidateFunctionSubgraphDepth(const ONNX_NAMESPACE::FunctionProto& function_proto) {
  return ValidateSubgraphDepth(function_proto.node(), &function_proto.attribute_proto());
}
 
Status BuildLocalFunctionCallGraph(
    const std::unordered_map<std::string, const ONNX_NAMESPACE::FunctionProto*>& model_local_functions,
    LocalFunctionCallGraph& call_graph) {
  call_graph.reserve(model_local_functions.size());
 
  for (const auto& [function_id, function_proto] : model_local_functions) {
    if (function_proto == nullptr) {
      return ORT_MAKE_STATUS(ONNXRUNTIME, INVALID_ARGUMENT,
                             "Null function proto for function id: ", function_id);
    }
 
    InlinedHashSet<std::string_view> seen_calls;
    InlinedVector<std::string_view> callees;
    CollectLocalFunctionCalls(function_proto->node(), model_local_functions, seen_calls, callees);
 
    call_graph.emplace(std::string_view(function_id), std::move(callees));
  }
 
  return Status::OK();
}
 
Status ValidateCallGraphAcyclic(const LocalFunctionCallGraph& call_graph) {
  enum class VisitState { kNotVisited,
                          kVisiting,
                          kVisited };
 
  InlinedHashMap<std::string_view, VisitState> visit_states;
  visit_states.reserve(call_graph.size());
  for (const auto& [function_id, _] : call_graph) {
    ORT_UNUSED_PARAMETER(_);
    visit_states.emplace(function_id, VisitState::kNotVisited);
  }
 
  // Each frame records the function being visited and a pointer to its callees vector
  // in the call graph (no per-frame allocation).
  struct DfsFrame {
    std::string_view function_id;
    const InlinedVector<std::string_view>* callees;
    size_t next_callee_index;
  };
 
  std::vector<DfsFrame> dfs_stack;
 
  for (const auto& [root_id, root_callees] : call_graph) {
    auto root_state_it = visit_states.find(root_id);
    if (root_state_it == visit_states.end() || root_state_it->second == VisitState::kVisited) {
      continue;
    }
 
    root_state_it->second = VisitState::kVisiting;
    dfs_stack.push_back({root_id, &root_callees, 0});
 
    while (!dfs_stack.empty()) {
      auto& frame = dfs_stack.back();
 
      if (frame.next_callee_index >= frame.callees->size()) {
        // All callees processed — mark as fully visited and pop.
        auto it = visit_states.find(frame.function_id);
        ORT_ENFORCE(it != visit_states.end());
        it->second = VisitState::kVisited;
        dfs_stack.pop_back();
        continue;
      }
 
      std::string_view callee_id = (*frame.callees)[frame.next_callee_index];
      frame.next_callee_index++;
 
      auto callee_state_it = visit_states.find(callee_id);
      if (callee_state_it == visit_states.end()) {
        // Callee not in the graph — skip.
        continue;
      }
 
      if (callee_state_it->second == VisitState::kVisited) {
        continue;
      }
 
      if (callee_state_it->second == VisitState::kVisiting) {
        // Cycle detected. Build cycle description from the stack.
        std::string cycle;
        bool in_cycle = false;
        for (const auto& f : dfs_stack) {
          if (f.function_id == callee_id) {
            in_cycle = true;
          }
          if (in_cycle) {
            if (!cycle.empty()) {
              cycle.append(" -> ");
            }
            cycle.append(f.function_id);
          }
        }
        cycle.append(" -> ");
        cycle.append(callee_id);
 
        return ORT_MAKE_STATUS(ONNXRUNTIME, INVALID_GRAPH,
                               "Model local function definitions must not be recursive. Cycle detected: ", cycle);
      }
 
      // Push callee onto the DFS stack.
      auto callee_graph_it = call_graph.find(callee_id);
      if (callee_graph_it == call_graph.end()) {
        continue;
      }
 
      callee_state_it->second = VisitState::kVisiting;
      dfs_stack.push_back({callee_id, &callee_graph_it->second, 0});
    }
  }
 
  return Status::OK();
}
 
Status ValidateModelLocalFunctionAcyclic(
    const std::unordered_map<std::string, const ONNX_NAMESPACE::FunctionProto*>& model_local_functions) {
  LocalFunctionCallGraph call_graph;
  ORT_RETURN_IF_ERROR(BuildLocalFunctionCallGraph(model_local_functions, call_graph));
  return ValidateCallGraphAcyclic(call_graph);
}
 
}  // namespace onnxruntime
 
#endif  // !defined(ORT_MINIMAL_BUILD)