Исходный код СУБД 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#include "adapter_format_utils.h"
#include "adapter_format_version.h"
 
#include "core/framework/allocator.h"
#include "core/common/common.h"
#include "core/common/endian.h"
#include "core/common/safeint.h"
#include "core/common/span_utils.h"
#include "core/framework/endian_utils.h"
#include "core/framework/ortdevice.h"
#include "core/framework/ortmemoryinfo.h"
#include "core/framework/ort_value.h"
#include "core/framework/tensor.h"
 
#include <fstream>
 
namespace onnxruntime {
namespace adapters {
namespace utils {
 
namespace {
 
constexpr bool IsSupportedLoraParameterType(TensorDataType data_type) {
  switch (data_type) {
    case TensorDataType::FLOAT:
    case TensorDataType::UINT8:
    case TensorDataType::INT8:
    case TensorDataType::UINT16:
    case TensorDataType::INT16:
    case TensorDataType::INT32:
    case TensorDataType::INT64:
    case TensorDataType::FLOAT16:
    case TensorDataType::DOUBLE:
    case TensorDataType::UINT32:
    case TensorDataType::UINT64:
    case TensorDataType::BFLOAT16:
    case TensorDataType::FLOAT8E4M3FN:
    case TensorDataType::FLOAT8E4M3FNUZ:
    case TensorDataType::FLOAT8E5M2:
    case TensorDataType::FLOAT8E5M2FNUZ:
      return true;
    default:
      return false;
  }
}
 
}  // namespace
 
bool IsAdapterFormatModelBytes(const void* bytes, size_t num_bytes) {
  return num_bytes > 8 &&  // check buffer is large enough to contain identifier so we don't read random memory
         AdapterBufferHasIdentifier(bytes);
}
 
void LoadStringFromLoraFormat(std::string& dst, const flatbuffers::String* fbs_string) {
  if (fbs_string) {
    dst = fbs_string->str();
  }
}
 
std::vector<uint8_t> LoadLoraAdapterBytes(const std::filesystem::path& file_path) {
  Env& env = Env::Default();
 
  size_t file_size = 0;
  ORT_THROW_IF_ERROR(env.GetFileLength(file_path.c_str(), file_size));
 
  std::vector<uint8_t> result;
  result.resize(file_size);
 
  // The API accepts char span, so we need to reinterpret the uint8_t span as char span
  auto dest_span = ReinterpretAsSpan<char>(AsSpan(result));
  ORT_THROW_IF_ERROR(env.ReadFileIntoBuffer(file_path.c_str(), 0, file_size, dest_span));
 
  return result;
}
 
std::pair<Env::MappedMemoryPtr, size_t> MemoryMapAdapterFile(const std::filesystem::path& file_path) {
  Env& env = Env::Default();
 
  size_t file_size = 0;
  ORT_THROW_IF_ERROR(env.GetFileLength(file_path.c_str(), file_size));
 
  Env::MappedMemoryPtr result;
  ORT_THROW_IF_ERROR(env.MapFileIntoMemory(file_path.c_str(), 0, file_size, result));
 
  return {std::move(result), file_size};
}
 
const Adapter* ValidateAndGetAdapterFromBytes(gsl::span<const uint8_t> bytes) {
  if (!IsAdapterFormatModelBytes(bytes.data(), bytes.size())) {
    ORT_THROW("The buffer does not appear to be a valid lora parameter format");
  }
 
  flatbuffers::Verifier verifier(bytes.data(), bytes.size());
  if (!VerifyAdapterBuffer(verifier)) {
    ORT_THROW("The buffer fails lora adapter format verification");
  }
 
  auto* adapter = GetAdapter(bytes.data());
  if (!IsAdapterFormatVersionSupported(adapter->format_version())) {
    ORT_THROW("Unsupported lora format version");
  }
 
  return adapter;
}
 
template <class T>
struct WriteDataForLittleEndian {
  Status operator()(gsl::span<const uint8_t> src, gsl::span<unsigned char> dest) const {
    auto src_span = ReinterpretAsSpan<const T>(src);
    return onnxruntime::utils::WriteLittleEndian<T>(src_span, dest);
  }
};
 
void SaveLoraParameter(flatbuffers::FlatBufferBuilder& flat_builder, std::string_view name,
                       TensorDataType data_type, gsl::span<const int64_t> shape,
                       gsl::span<const uint8_t> data,
                       flatbuffers::Offset<Parameter>& fbs_tensor) {
  auto name_str = (name.empty()) ? 0 : flat_builder.CreateString(name.data(), name.size());
  auto shape_vec = flat_builder.CreateVector(shape.data(), shape.size());
 
  flatbuffers::Offset<flatbuffers::Vector<uint8_t>> data_vec;
  if constexpr (endian::native == endian::big) {
    const auto elem_type = DataTypeImpl::TensorTypeFromONNXEnum(static_cast<int32_t>(data_type))->GetElementType();
    if (elem_type->Size() > 1) {
      InlinedVector<uint8_t> be_data(data.size());
      auto be_data_span = ReinterpretAsSpan<unsigned char>(AsSpan(be_data));
 
      onnxruntime::utils::MLTypeCallDispatcher<float, double,
                                               int16_t, uint16_t, int32_t, uint32_t,
                                               int64_t, uint64_t,
                                               BFloat16, MLFloat16>
          disp(static_cast<int32_t>(data_type));
 
      ORT_THROW_IF_ERROR((disp.InvokeRet<Status, WriteDataForLittleEndian>(data, be_data_span)));
      data_vec = flat_builder.CreateVector<uint8_t>(be_data.data(), be_data.size());
    } else {
      data_vec = flat_builder.CreateVector(data.data(), data.size());
    }
  } else {
    data_vec = flat_builder.CreateVector(data.data(), data.size());
  }
  fbs_tensor = CreateParameter(flat_builder, name_str, shape_vec, data_type, data_vec);
}
 
template <class T>
struct ReadDataForBigEndian {
  Status operator()(gsl::span<const unsigned char> src, Tensor& dst) const {
    auto dst_span = dst.MutableDataAsSpan<T>();
    return onnxruntime::utils::ReadLittleEndian<T>(src, dst_span);
  }
};
 
// If BE, we a allocate memory within the tensor and copy there swapping bytes
[[maybe_unused]] static Status CreateOrtValueForBePlatforms(const Parameter& param, const MLDataType elem_type,
                                                            gsl::span<const int64_t> shape, OrtValue& result) {
  static const AllocatorPtr cpu_allocator = CPUAllocator::DefaultInstance();
 
  auto src_span = ReinterpretAsSpan<const unsigned char>(
      gsl::make_span<const uint8_t>(param.raw_data()->data(), param.raw_data()->size()));
 
  const auto data_type = param.data_type();
 
  Tensor tensor(elem_type, shape, cpu_allocator);
  onnxruntime::utils::MLTypeCallDispatcher<float, double,
                                           int16_t, uint16_t, int32_t, uint32_t,
                                           int64_t, uint64_t,
                                           BFloat16, MLFloat16>
      disp(static_cast<int32_t>(data_type));
 
  ORT_RETURN_IF_ERROR((disp.InvokeRet<Status, ReadDataForBigEndian>(src_span, tensor)));
  Tensor::InitOrtValue(std::move(tensor), result);
  return Status::OK();
}
 
std::pair<std::string, OrtValue> CreateOrtValueOverLoraParameter(const Parameter& param) {
  OrtValue result;
 
  const auto* param_name = param.name();
  ORT_ENFORCE(param_name != nullptr, "Lora Parameter: name is missing");
 
  std::string name;
  LoadStringFromLoraFormat(name, param_name);
 
  const auto data_type = param.data_type();
  // The raw-byte size calculation below is valid only for fixed-size, unpacked scalar types.
  // An allowlist also rejects undeclared enum values because FlatBuffers verifies the field
  // representation but not membership in TensorDataType.
  ORT_ENFORCE(IsSupportedLoraParameterType(data_type),
              "Lora Param '", name, "': data_type '", static_cast<int32_t>(data_type),
              "' is not a supported LoRA parameter type");
 
  const auto* dims = param.dims();
  ORT_ENFORCE(dims != nullptr && dims->size() > 0,
              "Lora Param '", name, "': dims is missing or empty");
 
  const auto* raw_data = param.raw_data();
  ORT_ENFORCE(raw_data != nullptr,
              "Lora Param '", name, "': raw_data is missing");
 
  // Copying shape takes care of endianess using flatbuffers accessors
  TensorShapeVector shape(dims->begin(), dims->end());
  TensorShape tensor_shape(shape);
  const auto elem_type = DataTypeImpl::TensorTypeFromONNXEnum(static_cast<int32_t>(data_type))->GetElementType();
  const size_t expected_raw_data_size = SafeInt<size_t>(tensor_shape.Size()) * elem_type->Size();
  if (raw_data->size() != expected_raw_data_size) {
    ORT_THROW("Lora Param '", name,
              "': raw_data size (", raw_data->size(),
              ") does not match expected size (", expected_raw_data_size,
              ") calculated from tensor shape and element type");
  }
 
  static const OrtMemoryInfo cpu_meminfo(CPU, OrtAllocatorType::OrtDeviceAllocator);
 
  if constexpr (endian::native == endian::big) {
    if (elem_type->Size() > 1) {
      ORT_THROW_IF_ERROR(CreateOrtValueForBePlatforms(param, elem_type, shape, result));
    } else {
      // Single byte elements allow us to create OrtValue directly on top
      // of raw data
      // const_cast is necessary due to Tensor class API
      Tensor::InitOrtValue(elem_type,
                           tensor_shape,
                           const_cast<uint8_t*>(raw_data->data()),
                           cpu_meminfo,
                           result);
    }
  } else {
    // const_cast is necessary due to Tensor class API
    Tensor::InitOrtValue(elem_type,
                         tensor_shape,
                         const_cast<uint8_t*>(raw_data->data()),
                         cpu_meminfo,
                         result);
  }
 
  return std::make_pair(std::move(name), std::move(result));
}
 
void AdapterFormatBuilder::AddParameter(const std::string& name, TensorDataType data_type,
                                        gsl::span<const int64_t> shape, gsl::span<const uint8_t> data) {
  flatbuffers::Offset<Parameter> fbs_param;
  SaveLoraParameter(builder_, name, data_type, shape, data, fbs_param);
  params_.push_back(fbs_param);
}
 
std::vector<uint8_t> AdapterFormatBuilder::Finish(int adapter_version, int model_version) {
  FinishImpl(adapter_version, model_version);
 
  std::vector<uint8_t> result;
  result.reserve(builder_.GetSize());
  gsl::span<uint8_t> buffer(builder_.GetBufferPointer(), builder_.GetSize());
  std::copy(buffer.begin(), buffer.end(), std::back_inserter(result));
  return result;
}
 
gsl::span<uint8_t> AdapterFormatBuilder::FinishWithSpan(int adapter_version, int model_version) {
  FinishImpl(adapter_version, model_version);
  return gsl::make_span(builder_.GetBufferPointer(), builder_.GetSize());
}
 
void AdapterFormatBuilder::FinishImpl(int adapter_version, int model_version) {
  auto fbs_params = builder_.CreateVector(params_);
  auto fbs_adapter = CreateAdapter(builder_, kAdapterFormatVersion, adapter_version,
                                   model_version, fbs_params);
  builder_.Finish(fbs_adapter, AdapterIdentifier());
}
 
}  // namespace utils
}  // namespace adapters
}  // namespace onnxruntime