Исходный код СУБД 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#pragma once
 
#include <memory>
#include <string>
#include <vector>
 
#include "core/common/inlined_containers_fwd.h"
#include "core/framework/resource_accountant.h"
#include "core/graph/basic_types.h"
#include "core/graph/onnx_protobuf.h"
 
namespace onnxruntime {
 
class OpKernel;
class OpKernelInfo;
 
/**
@class IndexedSubGraph
 
Class containing information about a subgraph of Nodes from a Graph.
It contains a NodeIndex array of the Nodes covered by the subgraph,
and the meta definition needed for representing this subgraph as a FunctionProto,
which could be serialized/saved to a model file.
*/
struct IndexedSubGraph {
  struct MetaDef {
    std::string name;    ///< Name of customized SubGraph/FunctionProto
    std::string domain;  ///< Domain of customized SubGraph/FunctionProto
    int since_version;   ///< Since version of customized SubGraph/FunctionProto.
 
    ONNX_NAMESPACE::OperatorStatus status{ONNX_NAMESPACE::OperatorStatus::STABLE};  ///< Status of customized SubGraph/FunctionProto.
 
    std::vector<std::string> inputs;                 ///< Inputs of customized SubGraph/FunctionProto.
    std::vector<std::string> outputs;                ///< Outputs of customized SubGraph/FunctionProto.
    std::vector<std::string> constant_initializers;  ///< Constant initializers of customized SubGraph/FunctionProto.
    NodeAttributes attributes;                       ///< Attributes of customized SubGraph/FunctionProto.
 
    std::string doc_string;  ///< Doc string of customized SubGraph/FunctionProto.
#if !defined(ORT_MINIMAL_BUILD)
    /** Type and shape inference function that can optionally be defined for the fused node */
    std::function<void(ONNX_NAMESPACE::InferenceContext&)> type_and_shape_inference_function;
#endif
  };
 
  /** Nodes covered by this subgraph. The NodeIndex values are from the parent Graph.*/
  std::vector<onnxruntime::NodeIndex> nodes;
 
  enum class SourceOfSchema : uint8_t {
    CREATE,           /// create new schema from info in IndexedSubGraph instance.
                      /// schema instance will not be re-usable.
    REUSE_OR_CREATE,  /// re-use existing dynamically created schema with matching domain+name.
                      /// create re-usable schema if one is not found.
    EXISTING,         /// use existing statically registered schema.
                      /// e.g. domain+name matches ONNX or contrib op domain+op_type+opset.
  };
  // Either using an existing schema or generating reusable one when fusing nodes using the MetaDef.
  // MetaDef.domain + MetaDef.name => the domain.op_type that a schema must exist for with a valid since_version.
  SourceOfSchema schema_source{SourceOfSchema::CREATE};
 
  /** Set the meta definition needed to represent this subgraph as a FunctionProto
  It's needed IF AND ONLY IF there are multiple indexes contained in #nodes. */
  void SetMetaDef(std::unique_ptr<MetaDef>&& meta_def) {
    meta_def_ = std::move(meta_def);
  }
 
  /** Gets the meta definition needed to represent this subgraph as a FunctionProto.
  @returns MetaDef instance if it has been set. nullptr if not. */
  const MetaDef* GetMetaDef() const {
    return meta_def_.get();
  }
 
  /** Gets the mutable meta definition needed to represent this subgraph as a FunctionProto.
  @returns MetaDef instance if it has been set. nullptr if not. */
  MetaDef* GetMutableMetaDef() {
    return meta_def_.get();
  }
 
  // Check if the accounting is enabled for the current EP
  bool IsAccountingEnabled() const {
    return resource_accountant != nullptr &&
           nodes_costs.size() == nodes.size();
  }
 
  // Should call IsAccountingEnabled() first
  // Takes the previously computed ResourceCount for the node
  // (usually during GetCapability())
  // if present and adds it to the consumed amount
  void AccountForNode(size_t cost_index) const {
    assert(cost_index < nodes_costs.size());
    resource_accountant->AddConsumedAmount(nodes_costs[cost_index]);
    resource_accountant->CommitResourcesForNode(nodes[cost_index]);
  }
 
  // Accounts for all constituent nodes by summing their pre-stored costs.
  // Use this when fusing nodes into a single node so the total cost
  // reflects what was computed during GetCapability() (with correct
  // cross-node weight deduplication already applied).
  void AccountForAllNodes() const {
    assert(resource_accountant != nullptr);
    for (size_t i = 0; i < nodes_costs.size(); ++i) {
      resource_accountant->AddConsumedAmount(nodes_costs[i]);
      resource_accountant->CommitResourcesForNode(nodes[i]);
    }
  }
 
  // Accounts for all constituent nodes and moves their workspace reservations
  // to the fused node that survives graph partitioning.
  void AccountForAllNodes(const void* graph_identity, NodeIndex fused_node_index) const {
    AccountForAllNodes();
    resource_accountant->ConsolidateCommittedWorkspaceReservations(
        graph_identity, gsl::make_span(nodes), fused_node_index);
  }
 
  // Accounts for a node given its index and a pre-computed resource cost.
  // Use this when the cost was computed externally (e.g. for a fused node).
  void AccountForNode(NodeIndex node_index, const ResourceCount& resource_count) const {
    assert(resource_accountant != nullptr);
    resource_accountant->AddConsumedAmount(resource_count);
    resource_accountant->CommitResourcesForNode(node_index);
  }
 
  void SetAccountant(IResourceAccountant* res_accountant) {
    resource_accountant = res_accountant;
  }
 
  // Append resource count to the list of costs for the nodes.
  void AppendNodeCost(const ResourceCount& cost) {
    assert(resource_accountant != nullptr);
    nodes_costs.emplace_back(cost);
  }
 
  // Read-only access to the pre-computed resource cost for the node at cost_index.
  // Should call IsAccountingEnabled() first.
  const ResourceCount& GetNodeCost(size_t cost_index) const {
    assert(cost_index < nodes_costs.size());
    return nodes_costs[cost_index];
  }
 
 private:
  // subgraph meta definition.
  std::unique_ptr<MetaDef> meta_def_;
  // Optional resource accountant for this subgraph.
  IResourceAccountant* resource_accountant = nullptr;
  // Vector with resource costs for nodes above. Should have the same size
  InlinedVector<ResourceCount> nodes_costs;
};
 
}  // namespace onnxruntime