Исходный код СУБД 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#pragma once
 
#include "core/common/optional.h"
#include "core/framework/data_types.h"
#include "orttraining/core/graph/generic_registry.h"
#include "orttraining/core/graph/graph_augmenter.h"
#include "orttraining/core/graph/optimizer_config.h"
#include "onnx/defs/attr_proto_util.h"
#include "onnx/defs/tensor_proto_util.h"
 
namespace onnxruntime {
namespace training {
 
const std::vector<std::string> MOMENTS_PREFIXES({"Moment_1", "Moment_2"});
const std::string LAMB_STEP_TENSOR_NAME = "Step";
const std::string ADAM_UC_PREFIX = "Update_Count";
 
namespace training_internal {
constexpr auto single_element_dims = std::array{int64_t{1}};
}
 
template <class T>
inline ONNX_NAMESPACE::TensorProto CreateTensorProto(
    const std::string& name,
    const std::vector<T>& values,
    std::initializer_list<int64_t> dims) {
  return CreateTensorProto<T>(name, values, gsl::make_span<const int64_t>(dims.begin(), dims.end()));
}
 
template <class T>
inline ONNX_NAMESPACE::TensorProto CreateTensorProto(
    const std::string& name,
    const std::vector<T>& values,
    gsl::span<const int64_t> dims = training_internal::single_element_dims) {
  const size_t count = static_cast<size_t>(std::accumulate(dims.begin(), dims.end(), int64_t(1), std::multiplies<int64_t>{}));
  ORT_ENFORCE(values.size() == count);
  ONNX_NAMESPACE::TensorProto tensor_proto = ONNX_NAMESPACE::ToTensor<T>(values);
  tensor_proto.set_name(name);
  std::for_each(dims.begin(), dims.end(), [&](auto dim) { tensor_proto.add_dims(dim); });
  return tensor_proto;
}
 
template <class T>
inline ONNX_NAMESPACE::TensorProto CreateTensorProto(
    const std::string& name,
    gsl::span<const T> values_span,
    gsl::span<const int64_t> dims = training_internal::single_element_dims) {
  std::vector<T> values;
  values.reserve(values_span.size());
  values.assign(values_span.begin(), values_span.end());
  return CreateTensorProto(name, values, dims);
}
 
template <class T>
inline ONNX_NAMESPACE::TensorProto CreateTensorProto(
    const std::string& name,
    T val,
    gsl::span<const int64_t> dims = training_internal::single_element_dims) {
  size_t count = static_cast<size_t>(std::accumulate(dims.begin(), dims.end(), int64_t(1), std::multiplies<int64_t>{}));
  std::vector<T> values(count, val);
  ONNX_NAMESPACE::TensorProto tensor_proto = ONNX_NAMESPACE::ToTensor<T>(values);
  tensor_proto.set_name(name);
  std::for_each(dims.begin(), dims.end(), [&](auto dim) { tensor_proto.add_dims(dim); });
  return tensor_proto;
}
 
template <class T>
inline ONNX_NAMESPACE::TensorProto CreateTensorProto(
    const std::string& name,
    T val,
    std::initializer_list<int64_t> dims) {
  return CreateTensorProto(name, val, gsl::make_span<const int64_t>(dims.begin(), dims.end()));
}
 
Status IsMatchingTypeAndShape(
    const onnxruntime::Tensor& tensor,
    const int32_t element_type,
    gsl::span<const int64_t> expected_shape);
 
Status IsMatchingTypeAndShape(
    const onnxruntime::Tensor& tensor,
    const int32_t element_type,
    std::initializer_list<int64_t> expected_dims);
 
/**
 * The configuration for optimizer builder.
 */
struct OptimizerBuilderConfig {
  // The ArgDefs of the weights to optimize.
  std::vector<ArgDef> weight_argdefs;
 
  // The ArgDefs of the gradient of the weight to
  // optimize.
  std::vector<ArgDef> gradient_argdefs;
 
  // (Optional) The ArgDef of gradient norm.
  optional<ArgDef> gradient_norm_argdef;
 
  // (Optional) The ArgDef indicates whether
  //  the passed-in gradient norm is finite.
  optional<ArgDef> gradient_norm_finite_argdef;
 
  // The per weight optimizer configuration.
  std::vector<OptimizerNodeConfig> opt_configs;
 
  // (Optional) The flag to force gradient clipping. If planning
  // to use the default behavior of each sub-class, should not be set.
  optional<bool> enable_grad_clipping;
 
  // The initial state for optimizer params
  // shared by all weights.
  NameMLValMap shared_optimizer_states{};
};
 
class OptimizerBuilder {
 public:
  OptimizerBuilder(const OpDef& op_def, const std::vector<std::string>& attribute_names = {})
      : op_def_(op_def),
        attr_names_(attribute_names) {}
 
  virtual ~OptimizerBuilder() {}
 
  /**
   * Adds the optimizer node to the graph.
   * This component may be placed into an If node subgraph to enable
   * conditional execution.
   *
   * @param config The input config for optimizer builder
   * @param[out] graph_defs The GraphDefs corresponding to the graph (possibly
   *             a subgraph) that the component is to be added to.
   * @param[out] new_initializers Any initializers that should be
   *             placed in the parent graph, if there is one.
   *             Other initializers are treated as local to the current
   *             (sub)graph.
   * @param[out] weight_to_opt_mapping Mapping between weight to
   *             their new optimizer states in new_initializers.
   * @param[out] output_weight_argdefs The output weight ArgDef. All optimizers
                 should have this output.
   * @param[out] output_gradient_argdefs The output gradient ArgDef. All optimizers
                 should have this output.
   *
   * @return The status of the optimizer node addition.
   */
  virtual Status Build(
      const OptimizerBuilderConfig& config,
      GraphAugmenter::GraphDefs& graph_defs,
      std::vector<TensorProto>& new_initializers,
      std::unordered_map<std::string, std::unordered_map<std::string, std::string>>& weight_to_opt_mapping,
      std::vector<ArgDef>& output_weight_argdefs,
      std::vector<ArgDef>& output_gradient_argdefs) const = 0;
 
  const OpDef& OpDefinition() const { return op_def_; }
 
 protected:
  const std::string OptimizerNodeName(const std::string& weight_name) const {
    return op_def_.type + "_" + weight_name;
  }
 
  std::vector<ONNX_NAMESPACE::AttributeProto> BuildAttributeProto(const OptimizerNodeConfig& opt_config) const {
    std::vector<ONNX_NAMESPACE::AttributeProto> attribute_protos;
    for (auto attr_name : attr_names_) {
      // Search dictionary of float attributes.
      if (opt_config.attributes.count(attr_name)) {
        attribute_protos.push_back(ONNX_NAMESPACE::MakeAttribute(attr_name, opt_config.attributes.at(attr_name)));
      }
      // Search dictionary of int attributes.
      if (opt_config.int_attributes.count(attr_name)) {
        attribute_protos.push_back(ONNX_NAMESPACE::MakeAttribute(attr_name, opt_config.int_attributes.at(attr_name)));
      }
    }
    return attribute_protos;
  }
 
  static const ONNX_NAMESPACE::TypeProto* CreateLearningRateTypeProto(GraphAugmenter::GraphDefs& graph_defs) {
    return graph_defs.CreateTypeProto(std::array<const int64_t, 1>{1}, ONNX_NAMESPACE::TensorProto_DataType_FLOAT);
  }
 
 private:
  OpDef op_def_;
  std::vector<std::string> attr_names_;
};
 
class OptimizerBuilderRegistry : public GenericRegistry<OptimizerBuilder> {
 public:
  // Register optimizer builders.
  void RegisterBuilders();
 
  static OptimizerBuilderRegistry& GetInstance() {
    static OptimizerBuilderRegistry instance;
    return instance;
  }
 
 private:
  OptimizerBuilderRegistry() = default;
  ORT_DISALLOW_COPY_ASSIGNMENT_AND_MOVE(OptimizerBuilderRegistry);
};
 
}  // namespace training
}  // namespace onnxruntime