Исходный код СУБД 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#include "core/session/interop_api.h"
 
#if !defined(ORT_MINIMAL_BUILD)
#include <memory>
 
#include "core/session/ort_apis.h"
#include "core/session/onnxruntime_c_api.h"
#include "core/session/ort_env.h"
#include "core/session/abi_devices.h"
#include "core/common/logging/logging.h"
#include "core/framework/error_code_helper.h"
#else
#include "core/framework/error_code_helper.h"
#include "core/session/ort_apis.h"
#endif  // !defined(ORT_MINIMAL_BUILD)
 
using namespace onnxruntime;
 
#if !defined(ORT_MINIMAL_BUILD)
 
// Wrapper class for OrtExternalResourceImporterImpl
namespace {
struct ExternalResourceImporterWrapper {
  const OrtEpDevice* ep_device;
  OrtExternalResourceImporterImpl* impl;
 
  ExternalResourceImporterWrapper(const OrtEpDevice* device, OrtExternalResourceImporterImpl* importer)
      : ep_device(device), impl(importer) {}
 
  ~ExternalResourceImporterWrapper() {
    if (impl && impl->Release) {
      impl->Release(impl);
    }
  }
 
  ORT_DISALLOW_COPY_ASSIGNMENT_AND_MOVE(ExternalResourceImporterWrapper);
};
 
}  // namespace
 
ORT_API_STATUS_IMPL(OrtInteropAPI::CreateExternalResourceImporterForDevice, _In_ const OrtEpDevice* ep_device,
                    _Outptr_result_maybenull_ OrtExternalResourceImporter** out_importer) {
  API_IMPL_BEGIN
  if (ep_device == nullptr || out_importer == nullptr) {
    return OrtApis::CreateStatus(ORT_INVALID_ARGUMENT, "ep_device and out_importer must be provided.");
  }
 
  *out_importer = nullptr;
 
  // OrtEpFactory::CreateExternalResourceImporterForDevice was added in ORT 1.24.
  const auto* factory = ep_device->ep_factory;
  if (factory == nullptr ||
      factory->ort_version_supported < 24 ||
      factory->CreateExternalResourceImporterForDevice == nullptr) {
    // EP doesn't support this optional feature
    return nullptr;
  }
 
  OrtExternalResourceImporterImpl* impl = nullptr;
  ORT_API_RETURN_IF_ERROR(factory->CreateExternalResourceImporterForDevice(
      ep_device->GetMutableFactory(),
      ep_device,
      &impl));
 
  if (impl == nullptr) {
    // EP doesn't support this for the specific device
    return nullptr;
  }
 
  auto wrapper = std::make_unique<ExternalResourceImporterWrapper>(ep_device, impl);
  *out_importer = reinterpret_cast<OrtExternalResourceImporter*>(wrapper.release());
 
  return nullptr;
  API_IMPL_END
}
 
ORT_API(void, OrtInteropAPI::ReleaseExternalResourceImporter, _Frees_ptr_opt_ OrtExternalResourceImporter* importer) {
#if !defined(ORT_MINIMAL_BUILD)
  delete reinterpret_cast<ExternalResourceImporterWrapper*>(importer);
#else
  ORT_UNUSED_PARAMETER(importer);
#endif  // !defined(ORT_MINIMAL_BUILD)
}
 
ORT_API_STATUS_IMPL(OrtInteropAPI::CanImportMemory, _In_ const OrtExternalResourceImporter* importer,
                    _In_ OrtExternalMemoryHandleType handle_type,
                    _Out_ bool* out_supported) {
  API_IMPL_BEGIN
  if (importer == nullptr || out_supported == nullptr) {
    return OrtApis::CreateStatus(ORT_INVALID_ARGUMENT, "importer and out_supported must be provided.");
  }
 
  auto* wrapper = reinterpret_cast<const ExternalResourceImporterWrapper*>(importer);
  if (wrapper->impl == nullptr || wrapper->impl->CanImportMemory == nullptr) {
    *out_supported = false;
    return nullptr;
  }
 
  *out_supported = wrapper->impl->CanImportMemory(wrapper->impl, handle_type);
  return nullptr;
  API_IMPL_END
}
 
ORT_API_STATUS_IMPL(OrtInteropAPI::ImportMemory, _In_ OrtExternalResourceImporter* importer,
                    _In_ const OrtExternalMemoryDescriptor* desc,
                    _Outptr_ OrtExternalMemoryHandle** out_handle) {
  API_IMPL_BEGIN
  if (importer == nullptr || desc == nullptr || out_handle == nullptr) {
    return OrtApis::CreateStatus(ORT_INVALID_ARGUMENT, "importer, desc, and out_handle must be provided.");
  }
 
  auto* wrapper = reinterpret_cast<ExternalResourceImporterWrapper*>(importer);
  if (wrapper->impl == nullptr || wrapper->impl->ImportMemory == nullptr) {
    return OrtApis::CreateStatus(ORT_NOT_IMPLEMENTED, "External memory import is not supported by this EP.");
  }
 
  // EP creates derived type and returns base pointer. EP owns the handle lifetime.
  OrtExternalMemoryHandle* handle = nullptr;
  ORT_API_RETURN_IF_ERROR(wrapper->impl->ImportMemory(wrapper->impl, desc, &handle));
 
  if (handle == nullptr) {
    return OrtApis::CreateStatus(ORT_FAIL, "ImportMemory returned null handle.");
  }
 
  *out_handle = handle;
 
  return nullptr;
  API_IMPL_END
}
 
ORT_API(void, OrtInteropAPI::ReleaseExternalMemoryHandle, _Frees_ptr_opt_ OrtExternalMemoryHandle* handle) {
#if !defined(ORT_MINIMAL_BUILD)
  if (handle != nullptr && handle->Release != nullptr) {
    handle->Release(handle);
  }
#else
  ORT_UNUSED_PARAMETER(handle);
#endif  // !defined(ORT_MINIMAL_BUILD)
}
 
ORT_API_STATUS_IMPL(OrtInteropAPI::CreateTensorFromMemory, _In_ OrtExternalResourceImporter* importer,
                    _In_ const OrtExternalMemoryHandle* mem_handle,
                    _In_ const OrtExternalTensorDescriptor* tensor_desc,
                    _Outptr_ OrtValue** out_tensor) {
  API_IMPL_BEGIN
  if (importer == nullptr || mem_handle == nullptr || tensor_desc == nullptr || out_tensor == nullptr) {
    return OrtApis::CreateStatus(ORT_INVALID_ARGUMENT, "importer, mem_handle, tensor_desc, and out_tensor must be provided.");
  }
 
  auto* imp_wrapper = reinterpret_cast<ExternalResourceImporterWrapper*>(importer);
 
  if (imp_wrapper->impl == nullptr || imp_wrapper->impl->CreateTensorFromMemory == nullptr) {
    return OrtApis::CreateStatus(ORT_NOT_IMPLEMENTED, "CreateTensorFromMemory is not supported by this EP.");
  }
 
  OrtValue* tensor = nullptr;
  ORT_API_RETURN_IF_ERROR(imp_wrapper->impl->CreateTensorFromMemory(imp_wrapper->impl, mem_handle, tensor_desc, &tensor));
 
  *out_tensor = tensor;
  return nullptr;
  API_IMPL_END
}
 
ORT_API_STATUS_IMPL(OrtInteropAPI::CanImportSemaphore, _In_ const OrtExternalResourceImporter* importer,
                    _In_ OrtExternalSemaphoreType type,
                    _Out_ bool* out_supported) {
  API_IMPL_BEGIN
  if (importer == nullptr || out_supported == nullptr) {
    return OrtApis::CreateStatus(ORT_INVALID_ARGUMENT, "importer and out_supported must be provided.");
  }
 
  auto* wrapper = reinterpret_cast<const ExternalResourceImporterWrapper*>(importer);
  if (wrapper->impl == nullptr || wrapper->impl->CanImportSemaphore == nullptr) {
    *out_supported = false;
    return nullptr;
  }
 
  *out_supported = wrapper->impl->CanImportSemaphore(wrapper->impl, type);
  return nullptr;
  API_IMPL_END
}
 
ORT_API_STATUS_IMPL(OrtInteropAPI::ImportSemaphore, _In_ OrtExternalResourceImporter* importer,
                    _In_ const OrtExternalSemaphoreDescriptor* desc,
                    _Outptr_ OrtExternalSemaphoreHandle** out_handle) {
  API_IMPL_BEGIN
  if (importer == nullptr || desc == nullptr || out_handle == nullptr) {
    return OrtApis::CreateStatus(ORT_INVALID_ARGUMENT, "importer, desc, and out_handle must be provided.");
  }
 
  auto* wrapper = reinterpret_cast<ExternalResourceImporterWrapper*>(importer);
  if (wrapper->impl == nullptr || wrapper->impl->ImportSemaphore == nullptr) {
    return OrtApis::CreateStatus(ORT_NOT_IMPLEMENTED, "External semaphore import is not supported by this EP.");
  }
 
  // EP creates derived type and returns base pointer. EP owns the handle lifetime.
  OrtExternalSemaphoreHandle* handle = nullptr;
  ORT_API_RETURN_IF_ERROR(wrapper->impl->ImportSemaphore(wrapper->impl, desc, &handle));
 
  if (handle == nullptr) {
    return OrtApis::CreateStatus(ORT_FAIL, "ImportSemaphore returned null handle.");
  }
 
  *out_handle = handle;
 
  return nullptr;
  API_IMPL_END
}
 
ORT_API(void, OrtInteropAPI::ReleaseExternalSemaphoreHandle, _Frees_ptr_opt_ OrtExternalSemaphoreHandle* handle) {
#if !defined(ORT_MINIMAL_BUILD)
  if (handle != nullptr && handle->Release != nullptr) {
    handle->Release(handle);
  }
#else
  ORT_UNUSED_PARAMETER(handle);
#endif  // !defined(ORT_MINIMAL_BUILD)
}
 
ORT_API_STATUS_IMPL(OrtInteropAPI::WaitSemaphore, _In_ OrtExternalResourceImporter* importer,
                    _In_ OrtExternalSemaphoreHandle* semaphore_handle,
                    _In_ OrtSyncStream* stream,
                    _In_ uint64_t value) {
  API_IMPL_BEGIN
  if (importer == nullptr || semaphore_handle == nullptr || stream == nullptr) {
    return OrtApis::CreateStatus(ORT_INVALID_ARGUMENT, "importer, semaphore_handle, and stream must be provided.");
  }
 
  auto* imp_wrapper = reinterpret_cast<ExternalResourceImporterWrapper*>(importer);
 
  if (imp_wrapper->impl == nullptr || imp_wrapper->impl->WaitSemaphore == nullptr) {
    return OrtApis::CreateStatus(ORT_NOT_IMPLEMENTED, "WaitSemaphore is not supported by this EP.");
  }
 
  ORT_API_RETURN_IF_ERROR(imp_wrapper->impl->WaitSemaphore(imp_wrapper->impl, semaphore_handle, stream, value));
 
  return nullptr;
  API_IMPL_END
}
 
ORT_API_STATUS_IMPL(OrtInteropAPI::SignalSemaphore, _In_ OrtExternalResourceImporter* importer,
                    _In_ OrtExternalSemaphoreHandle* semaphore_handle,
                    _In_ OrtSyncStream* stream,
                    _In_ uint64_t value) {
  API_IMPL_BEGIN
  if (importer == nullptr || semaphore_handle == nullptr || stream == nullptr) {
    return OrtApis::CreateStatus(ORT_INVALID_ARGUMENT, "importer, semaphore_handle, and stream must be provided.");
  }
 
  auto* imp_wrapper = reinterpret_cast<ExternalResourceImporterWrapper*>(importer);
 
  if (imp_wrapper->impl == nullptr || imp_wrapper->impl->SignalSemaphore == nullptr) {
    return OrtApis::CreateStatus(ORT_NOT_IMPLEMENTED, "SignalSemaphore is not supported by this EP.");
  }
 
  ORT_API_RETURN_IF_ERROR(imp_wrapper->impl->SignalSemaphore(imp_wrapper->impl, semaphore_handle, stream, value));
 
  return nullptr;
  API_IMPL_END
}
 
ORT_API_STATUS_IMPL(OrtInteropAPI::InitGraphicsInteropForEpDevice, _In_ const OrtEpDevice* ep_device,
                    _In_ const OrtGraphicsInteropConfig* config) {
  API_IMPL_BEGIN
  if (ep_device == nullptr || config == nullptr) {
    return OrtApis::CreateStatus(ORT_INVALID_ARGUMENT, "ep_device and config must be provided.");
  }
 
  auto* factory = ep_device->ep_factory;
  if (factory == nullptr) {
    return OrtApis::CreateStatus(ORT_INVALID_ARGUMENT, "ep_device does not have an associated factory.");
  }
 
  if (factory->InitGraphicsInterop == nullptr) {
    return OrtApis::CreateStatus(ORT_NOT_IMPLEMENTED, "The execution provider does not support graphics interop.");
  }
 
  return factory->InitGraphicsInterop(ep_device->GetMutableFactory(), ep_device, config);
 
  API_IMPL_END
}
 
ORT_API_STATUS_IMPL(OrtInteropAPI::DeinitGraphicsInteropForEpDevice, _In_ const OrtEpDevice* ep_device) {
  API_IMPL_BEGIN
  if (ep_device == nullptr) {
    return OrtApis::CreateStatus(ORT_INVALID_ARGUMENT, "ep_device must be provided.");
  }
 
  auto* factory = ep_device->ep_factory;
  if (factory == nullptr) {
    return OrtApis::CreateStatus(ORT_INVALID_ARGUMENT, "ep_device does not have an associated factory.");
  }
 
  if (factory->DeinitGraphicsInterop == nullptr) {
    return OrtApis::CreateStatus(ORT_NOT_IMPLEMENTED, "The execution provider does not support graphics interop.");
  }
 
  return factory->DeinitGraphicsInterop(ep_device->GetMutableFactory(), ep_device);
 
  API_IMPL_END
}
 
#else  // defined(ORT_MINIMAL_BUILD)
 
ORT_API_STATUS_IMPL(OrtInteropAPI::CreateExternalResourceImporterForDevice, _In_ const OrtEpDevice* ep_device,
                    _Outptr_result_maybenull_ OrtExternalResourceImporter** out_importer) {
  API_IMPL_BEGIN
  ORT_UNUSED_PARAMETER(ep_device);
  ORT_UNUSED_PARAMETER(out_importer);
  return OrtApis::CreateStatus(ORT_NOT_IMPLEMENTED, "Interop API is not supported in this build");
  API_IMPL_END
}
 
ORT_API_STATUS_IMPL(OrtInteropAPI::CanImportMemory, _In_ const OrtExternalResourceImporter* importer,
                    _In_ OrtExternalMemoryHandleType handle_type,
                    _Out_ bool* out_supported) {
  API_IMPL_BEGIN
  ORT_UNUSED_PARAMETER(importer);
  ORT_UNUSED_PARAMETER(handle_type);
  ORT_UNUSED_PARAMETER(out_supported);
  return OrtApis::CreateStatus(ORT_NOT_IMPLEMENTED, "Interop API is not supported in this build");
  API_IMPL_END
}
 
ORT_API_STATUS_IMPL(OrtInteropAPI::ImportMemory, _In_ OrtExternalResourceImporter* importer,
                    _In_ const OrtExternalMemoryDescriptor* desc,
                    _Outptr_ OrtExternalMemoryHandle** out_handle) {
  API_IMPL_BEGIN
  ORT_UNUSED_PARAMETER(importer);
  ORT_UNUSED_PARAMETER(desc);
  ORT_UNUSED_PARAMETER(out_handle);
  return OrtApis::CreateStatus(ORT_NOT_IMPLEMENTED, "Interop API is not supported in this build");
  API_IMPL_END
}
 
ORT_API_STATUS_IMPL(OrtInteropAPI::CreateTensorFromMemory, _In_ OrtExternalResourceImporter* importer,
                    _In_ const OrtExternalMemoryHandle* mem_handle,
                    _In_ const OrtExternalTensorDescriptor* tensor_desc,
                    _Outptr_ OrtValue** out_tensor) {
  API_IMPL_BEGIN
  ORT_UNUSED_PARAMETER(importer);
  ORT_UNUSED_PARAMETER(mem_handle);
  ORT_UNUSED_PARAMETER(tensor_desc);
  ORT_UNUSED_PARAMETER(out_tensor);
  return OrtApis::CreateStatus(ORT_NOT_IMPLEMENTED, "Interop API is not supported in this build");
  API_IMPL_END
}
 
ORT_API_STATUS_IMPL(OrtInteropAPI::CanImportSemaphore, _In_ const OrtExternalResourceImporter* importer,
                    _In_ OrtExternalSemaphoreType type,
                    _Out_ bool* out_supported) {
  API_IMPL_BEGIN
  ORT_UNUSED_PARAMETER(importer);
  ORT_UNUSED_PARAMETER(type);
  ORT_UNUSED_PARAMETER(out_supported);
  return OrtApis::CreateStatus(ORT_NOT_IMPLEMENTED, "Interop API is not supported in this build");
  API_IMPL_END
}
 
ORT_API_STATUS_IMPL(OrtInteropAPI::ImportSemaphore, _In_ OrtExternalResourceImporter* importer,
                    _In_ const OrtExternalSemaphoreDescriptor* desc,
                    _Outptr_ OrtExternalSemaphoreHandle** out_handle) {
  API_IMPL_BEGIN
  ORT_UNUSED_PARAMETER(importer);
  ORT_UNUSED_PARAMETER(desc);
  ORT_UNUSED_PARAMETER(out_handle);
  return OrtApis::CreateStatus(ORT_NOT_IMPLEMENTED, "Interop API is not supported in this build");
  API_IMPL_END
}
 
ORT_API_STATUS_IMPL(OrtInteropAPI::WaitSemaphore, _In_ OrtExternalResourceImporter* importer,
                    _In_ OrtExternalSemaphoreHandle* semaphore_handle,
                    _In_ OrtSyncStream* stream,
                    _In_ uint64_t value) {
  API_IMPL_BEGIN
  ORT_UNUSED_PARAMETER(importer);
  ORT_UNUSED_PARAMETER(semaphore_handle);
  ORT_UNUSED_PARAMETER(stream);
  ORT_UNUSED_PARAMETER(value);
  return OrtApis::CreateStatus(ORT_NOT_IMPLEMENTED, "Interop API is not supported in this build");
  API_IMPL_END
}
 
ORT_API_STATUS_IMPL(OrtInteropAPI::SignalSemaphore, _In_ OrtExternalResourceImporter* importer,
                    _In_ OrtExternalSemaphoreHandle* semaphore_handle,
                    _In_ OrtSyncStream* stream,
                    _In_ uint64_t value) {
  API_IMPL_BEGIN
  ORT_UNUSED_PARAMETER(importer);
  ORT_UNUSED_PARAMETER(semaphore_handle);
  ORT_UNUSED_PARAMETER(stream);
  ORT_UNUSED_PARAMETER(value);
  return OrtApis::CreateStatus(ORT_NOT_IMPLEMENTED, "Interop API is not supported in this build");
  API_IMPL_END
}
 
ORT_API_STATUS_IMPL(OrtInteropAPI::InitGraphicsInteropForEpDevice, _In_ const OrtEpDevice* /*ep_device*/,
                    _In_ const OrtGraphicsInteropConfig* /*config*/) {
  API_IMPL_BEGIN
  return OrtApis::CreateStatus(ORT_NOT_IMPLEMENTED, "InitGraphicsInteropForEpDevice is not supported in this build");
  API_IMPL_END
}
 
ORT_API_STATUS_IMPL(OrtInteropAPI::DeinitGraphicsInteropForEpDevice, _In_ const OrtEpDevice* /*ep_device*/) {
  API_IMPL_BEGIN
  return OrtApis::CreateStatus(ORT_NOT_IMPLEMENTED, "DeinitGraphicsInteropForEpDevice is not supported in this build");
  API_IMPL_END
}
 
#endif  // !defined(ORT_MINIMAL_BUILD)
 
static constexpr OrtInteropApi ort_interop_api = {
    // NOTE: Application compatibility with newer versions of ORT depends on the Api order within this struct so
    // all new functions must be added at the end, and no functions that already exist in an officially released version
    // of ORT can be reordered or removed.
 
    &OrtInteropAPI::CreateExternalResourceImporterForDevice,
    &OrtInteropAPI::ReleaseExternalResourceImporter,
    &OrtInteropAPI::CanImportMemory,
    &OrtInteropAPI::ImportMemory,
    &OrtInteropAPI::ReleaseExternalMemoryHandle,
    &OrtInteropAPI::CreateTensorFromMemory,
    &OrtInteropAPI::CanImportSemaphore,
    &OrtInteropAPI::ImportSemaphore,
    &OrtInteropAPI::ReleaseExternalSemaphoreHandle,
    &OrtInteropAPI::WaitSemaphore,
    &OrtInteropAPI::SignalSemaphore,
    // End of Version 24 - DO NOT MODIFY ABOVE
    &OrtInteropAPI::InitGraphicsInteropForEpDevice,
    &OrtInteropAPI::DeinitGraphicsInteropForEpDevice,
    // End of Version 25 - DO NOT MODIFY ABOVE
};
 
// Checks that we don't violate the rule that the functions must remain in the slots they were originally assigned
static_assert(offsetof(OrtInteropApi, SignalSemaphore) / sizeof(void*) == 10,
              "Size of version 24 Api cannot change");  // initial version in ORT 1.24
static_assert(offsetof(OrtInteropApi, DeinitGraphicsInteropForEpDevice) / sizeof(void*) == 12,
              "Size of version 25 API cannot change");
 
ORT_API(const OrtInteropApi*, OrtInteropAPI::GetInteropApi) {
  return &ort_interop_api;
}