Исходный код СУБД 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#pragma once
 
#include <algorithm>
#include <functional>
#include <vector>
#include <cmath>
#include <filesystem>
#include "core/common/common.h"
#include "core/common/narrow.h"
#include "core/framework/allocator.h"
#include "core/framework/ort_value.h"
#include "core/framework/tensor_shape.h"
#include "core/framework/tensorprotoutils.h"
#include "core/graph/onnx_protobuf.h"
#include "core/optimizer/graph_transformer.h"
#include "core/util/math.h"
 
namespace onnxruntime {
 
class Initializer final {
 public:
  // Construct an initializer with the provided name and data type, with all values initialized to 0
  Initializer(ONNX_NAMESPACE::TensorProto_DataType data_type,
              std::string_view name,
              gsl::span<const int64_t> dims);
 
  Initializer(const ONNX_NAMESPACE::TensorProto& tensor_proto,
              const std::filesystem::path& model_path = {});
 
  Initializer(const Graph& graph, const ONNX_NAMESPACE::TensorProto& tensor_proto,
              const std::filesystem::path& model_path = {}, bool check_outer_scope = false);
 
  ~Initializer();
 
  /// <summary>
  /// This function creates a new tensor_proto with a complete copy of the data
  /// </summary>
  /// <param name="tensor_proto">output</param>
  void ToProto(ONNX_NAMESPACE::TensorProto& tensor_proto) const {
    tensor_proto = utils::TensorToTensorProto(*data_, name_);
  }
 
  /// <summary>
  /// This function creates a pair of TensorProto and OrtValue. Unless the data
  /// is short, tensor_proto will be a reference to the data in OrtValue.
  /// Useful when adding a new initializer to the graph with external data in memory.
  /// </summary>
  /// <param name="tensor_proto"></param>
  /// <param name="ort_value"></param>
  void ToProtoWithOrtValue(ONNX_NAMESPACE::TensorProto& tensor_proto, OrtValue& ort_value) const;
 
#if !defined(ORT_EXTENDED_MINIMAL_BUILD)
  // XXX: Below two used only in training, convert to OrtValue result
  ONNX_NAMESPACE::TensorProto ToFP16(const std::string& name) const;
  ONNX_NAMESPACE::TensorProto ToBFloat16(const std::string& name) const;
 
  OrtValue ToFP16() const;
  OrtValue ToBFloat16() const;
  OrtValue ToFloat32(onnxruntime::concurrency::ThreadPool* thread_pool = nullptr) const;
 
#endif  // ORT_EXTENDED_MINIMAL_BUILD
  int data_type() const {
    return data_->GetElementType();
  }
 
  const std::string& name() const {
    return name_;
  }
 
  template <typename T>
  T* data() {
    return data_->MutableData<T>();
  }
 
  template <typename T>
  const T* data() const {
    return data_->Data<T>();
  }
 
  const void* data_raw() const {
    return data_->DataRaw();
  }
 
  void* mutable_data_raw() {
    return data_->MutableDataRaw();
  }
 
  template <typename T>
  auto DataAsSpan() const {
    return data_->DataAsSpan<T>();
  }
 
  gsl::span<const uint8_t> DataAsByteSpan() const {
    return gsl::make_span(reinterpret_cast<const uint8_t*>(data_->DataRaw()), data_->SizeInBytes());
  }
 
  gsl::span<uint8_t> MutableDataAsByteSpan() {
    return gsl::make_span(reinterpret_cast<uint8_t*>(data_->MutableDataRaw()), data_->SizeInBytes());
  }
 
  gsl::span<const int64_t> dims() const {
    return data_->Shape().GetDims();
  }
 
  size_t size() const { return narrow<size_t>(data_->Shape().Size()); }
 
#if !defined(ORT_EXTENDED_MINIMAL_BUILD)
  Initializer& add(float value);
 
  Initializer& add(const Initializer& other);
 
  Initializer& sub(const Initializer& other);
 
  Initializer& mul(const Initializer& other);
 
  Initializer& div(const Initializer& other);
 
  Initializer& sqrt();
 
  void scale_by_axis(const Initializer& other, int axis, bool column_major = false);
#endif  // ORT_EXTENDED_MINIMAL_BUILD
 private:
  std::string name_;
  OrtValue ort_value_;
  Tensor* data_;
};
 
}  // namespace onnxruntime