Исходный код СУБД 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#include "bridge.h"
#include <torch/torch.h>
 
namespace onnxruntime {
namespace lazytensor {
c10::ScalarType CreateC10ScalarType(const onnxruntime::PrimitiveDataTypeBase* elem_type) {
  ORT_ENFORCE(elem_type, "Element type pointer cannot be NULL.");
  switch (static_cast<ONNX_NAMESPACE::TensorProto_DataType>(elem_type->GetDataType())) {
    case onnxruntime::utils::ToTensorProtoElementType<float>(): {
      return c10::kFloat;
    }
    case onnxruntime::utils::ToTensorProtoElementType<double>(): {
      return c10::kDouble;
    }
    case onnxruntime::utils::ToTensorProtoElementType<onnxruntime::MLFloat16>(): {
      return at::kHalf;
    }
    case onnxruntime::utils::ToTensorProtoElementType<onnxruntime::BFloat16>(): {
      return c10::kBFloat16;
    }
    case onnxruntime::utils::ToTensorProtoElementType<bool>(): {
      return at::kBool;
    }
    case onnxruntime::utils::ToTensorProtoElementType<int16_t>(): {
      return at::kShort;
    }
    case onnxruntime::utils::ToTensorProtoElementType<int>(): {
      return at::kInt;
    }
    case onnxruntime::utils::ToTensorProtoElementType<int64_t>(): {
      return at::kLong;
    }
    default:
      ORT_THROW("Unsupport ORT scalar type.");
  }
}
 
onnxruntime::MLDataType CreateOrtScalarType(
    at::ScalarType dtype) {
  switch (dtype) {
    case at::kFloat:
      return onnxruntime::DataTypeImpl::GetType<float>();
    case at::kDouble:
      return onnxruntime::DataTypeImpl::GetType<double>();
    case at::kHalf:
      return onnxruntime::DataTypeImpl::GetType<onnxruntime::MLFloat16>();
    case at::kBFloat16:
      return onnxruntime::DataTypeImpl::GetType<onnxruntime::BFloat16>();
    case at::kInt:
      return onnxruntime::DataTypeImpl::GetType<int>();
    case at::kShort:
      return onnxruntime::DataTypeImpl::GetType<int16_t>();
    case at::kLong:
      return onnxruntime::DataTypeImpl::GetType<int64_t>();
    case at::kBool:
      return onnxruntime::DataTypeImpl::GetType<bool>();
    default:
      ORT_THROW("Unsupport aten scalar type: ", dtype);
  }
}
 
OrtDevice CreateOrtDevice(const c10::Device device) {
  // c10::Device's ID can be negative, which means current device.
  // Assumptions:
  //  1. c10::Device::CPU is always indexed by -1.
  //     Thus, it's mapped to OrtDevice::CPU with index 0.
  //  2. c10::Device::GPU always has non-negative index.
  //     If the index is i, it's mapped to OrtDevice::GPU with index i.
 
  // For each case, assert if our assumptions are true and then do the work.
  if (device.type() == c10::DeviceType::CPU) {
    ORT_ENFORCE(device.index() == -1);
    return OrtDevice(OrtDevice::CPU, OrtDevice::MemType::DEFAULT, 0);
  } else if (device.type() == c10::DeviceType::CUDA) {
    ORT_ENFORCE(device.index() >= 0);
    return OrtDevice(OrtDevice::GPU, OrtDevice::MemType::DEFAULT, device.index());
  } else {
    ORT_THROW("Unsupport Pytorch c10 device.",
              " Type: ", c10::DeviceTypeName(device.type()), ",",
              " ID: ", device.index());
  }
}
 
c10::Device CreateC10Device(const OrtDevice& device) {
  // Handles CPU, GPU, and throws otherwise.
  switch (device.Type()) {
    case OrtDevice::CPU: {
      ORT_ENFORCE(device.Id() == 0, "ORT CPU device ID must be 0 but got ", device.Id());
      // No need to specify index when creating c10 CPU.
      return c10::Device(c10::DeviceType::CPU);
    }
    case OrtDevice::GPU: {
      ORT_ENFORCE(device.Id() >= 0, "ORT GPU device ID must be >= 0 but got ", device.Id());
      // c10 GPU can have negative index (means current device),
      // but only using non-negative index is enough to cover all ORT cases.
      return c10::Device(c10::DeviceType::CUDA, device.Id());
    }
    default: {
      // Got unsupported device. Throws.
      const char* device_str = nullptr;
      if (device.Type() == OrtDevice::CPU) {
        device_str = "CPU";
      } else if (device.Type() == OrtDevice::GPU) {
        device_str = "GPU";
      } else {
        device_str = "Unknown";
      }
      ORT_THROW("Unsupport ORT device: ", device_str, ", ID: ", device.Id());
    }
  }
}
 
OrtValue CreateOrtTensorValue(const at::Tensor& tensor) {
  onnxruntime::MLDataType element_type = CreateOrtScalarType(tensor.scalar_type());
  onnxruntime::TensorShape shape(tensor.sizes().vec());
  OrtDevice device = CreateOrtDevice(tensor.device());
  OrtMemoryInfo memory_info = OrtMemoryInfo("LTC", OrtAllocatorType::OrtDeviceAllocator, device, device.Id());
  // This tensor's life time is controlled by Pytorch.
  // TODO: consider to let ORT also own that tensor.
  std::unique_ptr<onnxruntime::Tensor> ort_tensor = std::make_unique<onnxruntime::Tensor>(
      element_type, shape,
      tensor.data_ptr(), memory_info);
 
  OrtValue ort_value;
  ort_value.Init(
      ort_tensor.release(),
      onnxruntime::DataTypeImpl::GetType<onnxruntime::Tensor>(),
      onnxruntime::DataTypeImpl::GetType<onnxruntime::Tensor>()->GetDeleteFunc());
  return ort_value;
}
 
c10::IValue CreateC10IvalueTensor(OrtValue value) {
  onnxruntime::Tensor* tensor = value.GetMutable<onnxruntime::Tensor>();
  const OrtDevice& device = tensor->Location().device;
  auto options = torch::TensorOptions()
                     .dtype(CreateC10ScalarType(tensor->DataType()->AsPrimitiveDataType()))
                     .layout(torch::kStrided)
                     .device(CreateC10Device(device))
                     .requires_grad(false);
 
  // Extract shape from onnxruntime::TensorShape as a vector.
  auto create_shape_vector = [](const onnxruntime::TensorShape& shape) {
    std::vector<int64_t> new_shape(shape.NumDimensions());
    shape.CopyDims(new_shape.data(), shape.NumDimensions());
    return new_shape;
  };
 
  at::Tensor new_tensor = torch::from_blob(
      tensor->MutableDataRaw(),
      create_shape_vector(tensor->Shape()),
      // Capture-by-value means
      //  1. A new OrtValue is direct-initialized from "value".
      //  2. The new OrtValue and "value" share the same underlying tensor, so
      //     the tensor's lifetime is controlled by both of them, whichever is longer.
      //  3. The new OrtValue's lifetime is the same as this lambda function.
      //  4. This lambda function is deleted by "new_tensor"'s dtor, which also ends
      //     the underlying tensor's life.
      [value, tensor](void* p) {
        // std::cout << "ORT-LR fake deletes Pytorch tensor wrapping ORT tensor @ " << tensor << std::endl;
      },
      options);
 
  return c10::IValue(new_tensor);
}
 
OrtValue CreateOrtScalarValue(const at::Scalar& scalar) {
  // This tensor's life time is controlled by Pytorch.
  // TODO: consider to let ORT also own that tensor.
  void* data_ptr = nullptr;
  std::function<void()> data_deleter;
  switch (scalar.type()) {
    case at::kFloat: {
      data_ptr = new float;
      *reinterpret_cast<float*>(data_ptr) = scalar.toFloat();
      data_deleter = [=]() {
        delete reinterpret_cast<float*>(data_ptr);
      };
      break;
    }
    case at::kDouble: {
      data_ptr = new double;
      *reinterpret_cast<double*>(data_ptr) = scalar.toDouble();
      data_deleter = [=]() {
        delete reinterpret_cast<double*>(data_ptr);
      };
      break;
    }
    case at::kBFloat16: {
      at::BFloat16 valBFloat16 = scalar.toBFloat16();
      onnxruntime::BFloat16* valOrtBFloat16 = reinterpret_cast<onnxruntime::BFloat16*>(&valBFloat16);
      data_ptr = new onnxruntime::BFloat16;
      *reinterpret_cast<onnxruntime::BFloat16*>(data_ptr) = *valOrtBFloat16;
      data_deleter = [=]() {
        delete reinterpret_cast<onnxruntime::BFloat16*>(data_ptr);
      };
      break;
    }
    case at::kShort: {
      data_ptr = new int16_t;
      *reinterpret_cast<int16_t*>(data_ptr) = scalar.toShort();
      data_deleter = [=]() {
        delete reinterpret_cast<int16_t*>(data_ptr);
      };
      break;
    }
    case at::kInt: {
      data_ptr = new int;
      *reinterpret_cast<int*>(data_ptr) = scalar.toInt();
      data_deleter = [=]() {
        delete reinterpret_cast<int*>(data_ptr);
      };
      break;
    }
    case at::kLong: {
      data_ptr = new int64_t;
      *reinterpret_cast<int64_t*>(data_ptr) = scalar.toLong();
      data_deleter = [=]() {
        delete reinterpret_cast<int64_t*>(data_ptr);
      };
      break;
    }
    case at::kBool: {
      data_ptr = new bool;
      *reinterpret_cast<bool*>(data_ptr) = scalar.toBool();
      data_deleter = [=]() {
        delete reinterpret_cast<bool*>(data_ptr);
      };
      break;
    }
    default:
      ORT_THROW("Unsupport aten scalar type: ", scalar.type());
  }
 
  OrtDevice cpu_device = CreateOrtDevice(c10::Device(c10::DeviceType::CPU));
  OrtMemoryInfo memory_info = OrtMemoryInfo("LTC", OrtAllocatorType::OrtDeviceAllocator, cpu_device, cpu_device.Id());
 
  onnxruntime::MLDataType element_type = CreateOrtScalarType(scalar.type());
  onnxruntime::TensorShape shape({});
  std::unique_ptr<onnxruntime::Tensor> ort_tensor = std::make_unique<onnxruntime::Tensor>(
      element_type, shape,
      data_ptr, memory_info);
 
  std::function<void(void*)> deleter = [=](void* p) {
    data_deleter();
    onnxruntime::DataTypeImpl::GetType<onnxruntime::Tensor>()->GetDeleteFunc()(p);
  };
 
  OrtValue ort_value;
  ort_value.Init(
      ort_tensor.release(),
      onnxruntime::DataTypeImpl::GetType<onnxruntime::Tensor>(),
      deleter);
  return ort_value;
}
 
c10::IValue CreateC10IvalueScalar(OrtValue value) {
  onnxruntime::Tensor* tensor = value.GetMutable<onnxruntime::Tensor>();
  // Here we assume tensors with empty shape are at::Scalar's.
  // Assert on CPU because at::Scalar is always on CPU.
  const OrtDevice::DeviceType& device_type = tensor->Location().device.Type();
  ORT_ENFORCE(device_type == OrtDevice::CPU);
 
  // Scalar ORT values must be put on CPU. Otherwise, the following code may throw.
  // TODO: relax this constraint by
  //       1. specifying output devices when calling ORT session, or
  //       2. manually copying GPU OrtValue to CPU here.
  c10::IValue new_value;
  ORT_ENFORCE(tensor->Location().device.Type() == OrtDevice::CPU);
  switch (static_cast<ONNX_NAMESPACE::TensorProto_DataType>(tensor->DataType()->AsPrimitiveDataType()->GetDataType())) {
    case onnxruntime::utils::ToTensorProtoElementType<float>(): {
      new_value = at::IValue(static_cast<double>(*tensor->Data<float>()));
      break;
    }
    case onnxruntime::utils::ToTensorProtoElementType<double>(): {
      new_value = at::IValue(*tensor->Data<double>());
      break;
    }
    case onnxruntime::utils::ToTensorProtoElementType<bool>(): {
      new_value = at::IValue(*tensor->Data<bool>());
      break;
    }
    case onnxruntime::utils::ToTensorProtoElementType<int64_t>(): {
      new_value = at::IValue(*tensor->Data<int64_t>());
      break;
    }
    default:
      ORT_THROW("Unsupport aten scalar type.");
  }
 
  return new_value;
}
}  // namespace lazytensor
}  // namespace onnxruntime