Исходный код СУБД 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#include "orttraining/core/framework/checkpointing.h"
 
#include <algorithm>
#include <fstream>
#include <utility>
#include <vector>
 
#include "core/common/common.h"
#include "core/common/logging/logging.h"
#include "core/framework/data_transfer_utils.h"
#include "core/framework/endian_utils.h"
#include "core/framework/ort_value.h"
#include "core/framework/tensor_external_data_info.h"
#include "core/framework/tensorprotoutils.h"
#include "core/platform/env.h"
#include "core/platform/path_lib.h"
#include "orttraining/core/framework/protobuf_message_sequence.h"
#include "core/util/protobuf_parsing_utils.h"
#include "orttraining/core/framework/checkpoint_common.h"
 
namespace onnxruntime {
namespace training {
 
namespace {
 
constexpr const PathChar* k_tensors_file_name = ORT_TSTR("tensors.pbseq");
constexpr const PathChar* k_tensors_data_file_name = ORT_TSTR("tensors.bin");
constexpr const PathChar* k_properties_file_name = ORT_TSTR("properties.pbseq");
 
PathString GetCheckpointTensorsFilePath(const PathString& checkpoint_directory) {
  return ConcatPathComponent(checkpoint_directory, k_tensors_file_name);
}
 
PathString GetCheckpointTensorsDataFilePath(const PathString& checkpoint_directory) {
  return ConcatPathComponent(checkpoint_directory, k_tensors_data_file_name);
}
 
PathString GetCheckpointPropertiesFilePath(const PathString& checkpoint_directory) {
  return ConcatPathComponent(checkpoint_directory, k_properties_file_name);
}
 
Status SaveRuntimeTensor(
    const std::string& tensor_name,
    const Tensor& tensor,
    gsl::span<const char> tensor_data,
    const PathString& relative_data_path,
    std::ofstream& data_file,
    ONNX_NAMESPACE::TensorProto& tensor_proto) {
  ORT_RETURN_IF(tensor.DataType() == DataTypeImpl::GetType<std::string>(), "tensor.DataType() is std::string");
 
  VLOGS_DEFAULT(1) << "Saving tensor " << tensor_name;
 
  ONNX_NAMESPACE::TensorProto saved_tensor_proto{};
 
  for (const auto dim : tensor.Shape().GetDims()) {
    saved_tensor_proto.add_dims(dim);
  }
 
  saved_tensor_proto.set_data_type(tensor.GetElementType());
 
  saved_tensor_proto.set_name(tensor_name);
 
  auto add_external_data = [&saved_tensor_proto](const std::string& key, const std::string& value) {
    auto* kvp = saved_tensor_proto.add_external_data();
    kvp->set_key(key);
    kvp->set_value(value);
  };
 
  // TODO is the encoding correct? https://github.com/onnx/onnx/issues/2392
  add_external_data("location", ToUTF8String(relative_data_path));
  const std::streamoff offset = data_file.tellp();
  add_external_data("offset", std::to_string(offset));
  const auto length = tensor_data.size_bytes();
  add_external_data("length", std::to_string(length));
 
  saved_tensor_proto.set_data_location(ONNX_NAMESPACE::TensorProto_DataLocation_EXTERNAL);
 
  if constexpr (endian::native != endian::little) {
    std::vector<char> le_data;
    le_data.resize(length);
 
    size_t element_size = onnxruntime::utils::GetElementSizeOfTensor(static_cast<ONNX_NAMESPACE::TensorProto_DataType>(saved_tensor_proto.data_type()));
    auto src_span = gsl::make_span(reinterpret_cast<const unsigned char*>(tensor_data.data()), tensor_data.size_bytes());
    auto dst_span = gsl::make_span(reinterpret_cast<unsigned char*>(le_data.data()), le_data.size());
 
    // If element size is unknown, set it to 1 to disable byteswapping
    if (element_size < 1) element_size = 1;
 
    ORT_RETURN_IF_ERROR(onnxruntime::utils::WriteLittleEndian(element_size, src_span, dst_span));
 
    ORT_RETURN_IF_NOT(
        data_file.write(le_data.data(), length),
        "Failed to write to data file: ", ToUTF8String(relative_data_path));
  } else {
    ORT_RETURN_IF_NOT(
        data_file.write(tensor_data.data(), length),
        "Failed to write to data file: ", ToUTF8String(relative_data_path));
  }
 
  tensor_proto = std::move(saved_tensor_proto);
  return Status::OK();
}
 
std::vector<std::string> GetOrderedOrtValueNames(const NameMLValMap& name_to_value) {
  std::vector<std::string> ordered_names{};
  ordered_names.reserve(name_to_value.size());
  std::transform(
      name_to_value.begin(), name_to_value.end(), std::back_inserter(ordered_names),
      [](const NameMLValMap::value_type& v) { return v.first; });
  std::sort(ordered_names.begin(), ordered_names.end());
  return ordered_names;
}
 
Status SaveRuntimeTensors(
    const PathString& tensors_path,
    const PathString& tensors_data_path,
    const DataTransferManager& data_transfer_manager,
    const NameMLValMap& ort_values) {
  // just write data file basename to TensorProto - this will get overwritten
  //   with the actual path when loading the checkpoint
  const PathString tensors_data_relative_path = GetLastComponent(tensors_data_path);
 
  const std::vector<std::string> ordered_tensor_names = GetOrderedOrtValueNames(ort_values);
  std::vector<char> tensor_data_buffer{};
  static const OrtMemoryInfo cpu_alloc_info{onnxruntime::CPU, OrtDeviceAllocator};
  std::vector<ONNX_NAMESPACE::TensorProto> saved_tensor_protos{};
  saved_tensor_protos.reserve(ordered_tensor_names.size());
  std::ofstream tensors_data_file{tensors_data_path};
 
  for (const auto& tensor_name : ordered_tensor_names) {
    const OrtValue& ort_value = ort_values.at(tensor_name);
    ORT_RETURN_IF_NOT(ort_value.IsTensor(), "ort_value.IsTensor() was false");
    const Tensor& tensor = ort_value.Get<Tensor>();
 
    tensor_data_buffer.resize(tensor.SizeInBytes());
    ORT_RETURN_IF_ERROR(CopyTensorDataToByteSpan(
        data_transfer_manager, tensor, cpu_alloc_info, gsl::make_span(tensor_data_buffer)));
 
    saved_tensor_protos.emplace_back();
    ORT_RETURN_IF_ERROR(SaveRuntimeTensor(
        tensor_name, tensor, tensor_data_buffer, tensors_data_relative_path,
        tensors_data_file, saved_tensor_protos.back()));
  }
 
  ORT_RETURN_IF_ERROR(WithOpenFile(
      tensors_path, false,
      [&saved_tensor_protos](int fd) {
        google::protobuf::io::FileOutputStream output{fd};
        ORT_RETURN_IF_ERROR(WriteProtoMessageSequence(saved_tensor_protos, output));
        return Status::OK();
      }));
 
  return Status::OK();
}
 
Status SaveProperties(
    const PathString& properties_path,
    const std::unordered_map<std::string, std::string>& properties) {
  std::vector<ONNX_NAMESPACE::StringStringEntryProto> property_protos{};
  property_protos.reserve(properties.size());
  std::transform(
      properties.begin(), properties.end(), std::back_inserter(property_protos),
      [](const std::pair<std::string, std::string>& property) {
        ONNX_NAMESPACE::StringStringEntryProto property_proto{};
        property_proto.set_key(property.first);
        property_proto.set_value(property.second);
        return property_proto;
      });
 
  // ensure consistent ordering
  std::sort(
      property_protos.begin(), property_protos.end(),
      [](const ONNX_NAMESPACE::StringStringEntryProto& a,
         const ONNX_NAMESPACE::StringStringEntryProto& b) {
        return a.key() < b.key();
      });
 
  ORT_RETURN_IF_ERROR(WithOpenFile(
      properties_path, false,
      [&property_protos](int fd) {
        google::protobuf::io::FileOutputStream output{fd};
        ORT_RETURN_IF_ERROR(WriteProtoMessageSequence(property_protos, output));
        return Status::OK();
      }));
 
  return Status::OK();
}
 
}  // namespace
 
Status SaveModelCheckpoint(
    const PathString& checkpoint_path,
    const DataTransferManager& data_transfer_manager,
    const NameMLValMap& runtime_tensors,
    const std::unordered_map<std::string, std::string>& properties) {
  LOGS_DEFAULT(INFO) << "Saving model checkpoint files to " << ToUTF8String(checkpoint_path);
 
  LOGS_DEFAULT_IF(Env::Default().FolderExists(checkpoint_path), WARNING)
      << "Checkpoint directory exists - data may be overwritten.";
 
  ORT_RETURN_IF_ERROR(Env::Default().CreateFolder(checkpoint_path));
 
  // write tensors files
  ORT_RETURN_IF_ERROR(SaveRuntimeTensors(
      GetCheckpointTensorsFilePath(checkpoint_path),
      GetCheckpointTensorsDataFilePath(checkpoint_path),
      data_transfer_manager, runtime_tensors));
 
  // write properties file
  ORT_RETURN_IF_ERROR(SaveProperties(
      GetCheckpointPropertiesFilePath(checkpoint_path), properties));
 
  LOGS_DEFAULT(INFO) << "Model checkpoint saved successfully.";
 
  return Status::OK();
}
 
namespace {
Status UpdateTensorsExternalDataLocations(
    const PathString& external_data_path,
    std::vector<ONNX_NAMESPACE::TensorProto>& tensor_protos) {
  for (auto& tensor_proto : tensor_protos) {
    if (tensor_proto.data_location() != ONNX_NAMESPACE::TensorProto_DataLocation_EXTERNAL) {
      continue;
    }
 
    auto& external_data = *tensor_proto.mutable_external_data();
    auto location_it = std::find_if(
        external_data.begin(), external_data.end(),
        [](ONNX_NAMESPACE::StringStringEntryProto& kvp) { return kvp.key() == "location"; });
    ORT_RETURN_IF_NOT(location_it != external_data.end(), "location_it == external_data.end()");
 
    // TODO is the encoding correct? https://github.com/onnx/onnx/issues/2392
    location_it->set_value(ToUTF8String(external_data_path));
  }
 
  return Status::OK();
}
}  // namespace
 
Status LoadModelCheckpoint(
    const PathString& checkpoint_path,
    const PathString& model_path,
    std::vector<ONNX_NAMESPACE::TensorProto>& tensor_protos,
    std::unordered_map<std::string, std::string>& properties) {
  LOGS_DEFAULT(INFO) << "Loading model checkpoint files from " << ToUTF8String(checkpoint_path);
 
  // read tensors file
  std::vector<ONNX_NAMESPACE::TensorProto> loaded_tensor_protos{};
  ORT_RETURN_IF_ERROR(WithOpenFile(
      GetCheckpointTensorsFilePath(checkpoint_path), true,
      [&loaded_tensor_protos](int fd) {
        google::protobuf::io::FileInputStream input{fd};
        ORT_RETURN_IF_ERROR(ReadProtoMessageSequence(loaded_tensor_protos, input));
        return Status::OK();
      }));
 
  // set external data locations
  {
    PathString model_directory_path{}, model_directory_canonical_path{};
    ORT_RETURN_IF_ERROR(GetDirNameFromFilePath(
        model_path, model_directory_path));
    ORT_RETURN_IF_ERROR(Env::Default().GetCanonicalPath(
        model_directory_path, model_directory_canonical_path));
 
    PathString checkpoint_canonical_path{};
    ORT_RETURN_IF_ERROR(Env::Default().GetCanonicalPath(
        checkpoint_path, checkpoint_canonical_path));
 
    std::filesystem::path relative_tensors_data_path_obj = std::filesystem::relative(
        GetCheckpointTensorsDataFilePath(checkpoint_canonical_path), model_directory_canonical_path);
    ORT_RETURN_IF_ERROR(UpdateTensorsExternalDataLocations(
        relative_tensors_data_path_obj.native(), loaded_tensor_protos));
  }
 
  // read properties file
  std::vector<ONNX_NAMESPACE::StringStringEntryProto> loaded_property_protos{};
  ORT_RETURN_IF_ERROR(WithOpenFile(
      GetCheckpointPropertiesFilePath(checkpoint_path), true,
      [&loaded_property_protos](int fd) {
        google::protobuf::io::FileInputStream input{fd};
        ORT_RETURN_IF_ERROR(ReadProtoMessageSequence(loaded_property_protos, input));
        return Status::OK();
      }));
 
  std::unordered_map<std::string, std::string> loaded_properties{};
  std::transform(
      loaded_property_protos.begin(), loaded_property_protos.end(),
      std::inserter(loaded_properties, loaded_properties.end()),
      [](const ONNX_NAMESPACE::StringStringEntryProto& property_proto) {
        return std::make_pair(property_proto.key(), property_proto.value());
      });
 
  tensor_protos = std::move(loaded_tensor_protos);
  properties = std::move(loaded_properties);
 
  LOGS_DEFAULT(INFO) << "Model checkpoint loaded successfully.";
 
  return Status::OK();
}
 
}  // namespace training
}  // namespace onnxruntime