Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#include "core/common/inlined_containers.h"
#include "core/framework/tensorprotoutils.h"
#include "core/graph/function_impl.h"
#include "core/graph/graph_utils.h"
#include "core/graph/graph_viewer.h"
#include "core/graph/model.h"
 
namespace onnxruntime {
// Creates domain to version map for onnx function
static std::unordered_map<std::string, int> GetFunctionOpsetImports(const ONNX_NAMESPACE::FunctionProto& func_proto, const std::unordered_map<std::string, int>& graph_imports) {
  std::unordered_map<std::string, int> function_opset_imports{graph_imports};
  for (const auto& opset_import : func_proto.opset_import()) {
    // If graph imports does not contain opset_import then insert it otherwise the one in graph imports overrides.
    // If the opset imports are not compatible then this will be caught during function body inline.
    function_opset_imports.insert({opset_import.domain(), static_cast<int>(opset_import.version())});
  }
  return function_opset_imports;
}
 
// Construct it with fused index graph, instantiate the function directly
FunctionImpl::FunctionImpl(onnxruntime::Graph& graph,
                           const IndexedSubGraph& nodes_to_fuse)
    : function_body_graph_(graph.GetModel(),
                           graph.GetSchemaRegistry(),
                           function_storage_proto_,
                           graph.DomainToVersionMap(),
                           graph.GetLogger(),
                           graph.StrictShapeTypeInference()) {
  auto* meta_def = nodes_to_fuse.GetMetaDef();
 
  int i = 0;
  std::vector<const NodeArg*> function_body_graph_inputs;
  function_body_graph_inputs.resize(meta_def->inputs.size());
  for (auto& input : meta_def->inputs) {
    auto input_arg = graph.GetNodeArg(input);
    auto& function_body_graph_input_arg = function_body_graph_.GetOrCreateNodeArg(input_arg->Name(), input_arg->TypeAsProto());
    function_body_graph_inputs[i] = &function_body_graph_input_arg;
    ++i;
  }
 
  i = 0;
  std::vector<const NodeArg*> function_body_graph_outputs;
  function_body_graph_outputs.resize(meta_def->outputs.size());
  for (auto& output : meta_def->outputs) {
    auto output_arg = graph.GetNodeArg(output);
    auto& function_body_graph_output_arg = function_body_graph_.GetOrCreateNodeArg(output_arg->Name(), output_arg->TypeAsProto());
    function_body_graph_outputs[i] = &function_body_graph_output_arg;
    ++i;
  }
 
  function_body_graph_.SetInputs(function_body_graph_inputs);
  function_body_graph_.SetOutputs(function_body_graph_outputs);
 
  // Add node and node args
  // TODO: for better performance, we could try to transfer the nodes in parent graph to sub-graph directly,
  // instead of create new nodes.
  for (auto& node_index : nodes_to_fuse.nodes) {
    auto node = graph.GetNode(node_index);
    std::vector<onnxruntime::NodeArg*> inputs;
    std::vector<onnxruntime::NodeArg*> outputs;
    for (auto input : node->InputDefs()) {
      auto& n_input = function_body_graph_.GetOrCreateNodeArg(input->Name(), input->TypeAsProto());
      inputs.push_back(&n_input);
    }
    for (auto output : node->OutputDefs()) {
      auto& n_output = function_body_graph_.GetOrCreateNodeArg(output->Name(), output->TypeAsProto());
      outputs.push_back(&n_output);
    }
    function_body_graph_.AddNode(node->Name(), node->OpType(), node->Description(), inputs, outputs, &node->GetAttributes(), node->Domain());
  }
 
  for (const auto& input : meta_def->inputs) {
    graph_utils::MakeInitializerCopyIfNotExist(graph, function_body_graph_, input);
  }
 
  for (const auto& constant_initializer : meta_def->constant_initializers) {
    graph_utils::MakeConstantInitializerCopyIfNotExist(graph, function_body_graph_, constant_initializer, true);
  }
 
  // TODO: if we reuse the nodes in parent graph, maybe we don't need to resolve it.
  auto status = function_body_graph_.Resolve();
  ORT_ENFORCE(status.IsOK(), status.ErrorMessage());
}
 
FunctionImpl::FunctionImpl(onnxruntime::Graph& graph,
                           const ONNX_NAMESPACE::FunctionProto& onnx_func)
    : function_body_graph_(graph.GetModel(),
                           graph.GetSchemaRegistry(),
                           function_storage_proto_,
                           onnx_func.opset_import_size() != 0 ? GetFunctionOpsetImports(onnx_func, graph.DomainToVersionMap()) : graph.DomainToVersionMap(),
                           graph.GetLogger(),
                           graph.StrictShapeTypeInference()) {
}
 
FunctionImpl::~FunctionImpl() = default;
 
const onnxruntime::Graph& FunctionImpl::Body() const {
  return function_body_graph_;
}
 
onnxruntime::Graph& FunctionImpl::MutableBody() {
  return function_body_graph_;
}
 
}  // namespace onnxruntime