Исходный код СУБД 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#include "ep_factory.h"
 
#include <cassert>
 
#include "core/session/onnxruntime_ep_device_ep_metadata_keys.h"
 
#include "ep.h"
#include "ep_allocator.h"
#include "ep_kernel_registration.h"
#include "../plugin_ep_utils.h"
 
ExampleKernelEpFactory::ExampleKernelEpFactory(const OrtApi& ort_api, const OrtEpApi& ep_api,
                                               const OrtLogger& /*default_logger*/)
    : OrtEpFactory{},
      ort_api_(ort_api),
      ep_api_(ep_api),
      default_memory_info_{nullptr},
      readonly_memory_info_{nullptr} {
  ort_version_supported = ORT_API_VERSION;  // set to the ORT version we were compiled with.
  GetName = GetNameImpl;
  GetVendor = GetVendorImpl;
  GetVendorId = GetVendorIdImpl;
  GetVersion = GetVersionImpl;
 
  GetSupportedDevices = GetSupportedDevicesImpl;
 
  CreateEp = CreateEpImpl;
  ReleaseEp = ReleaseEpImpl;
 
  CreateAllocator = CreateAllocatorImpl;
  ReleaseAllocator = ReleaseAllocatorImpl;
 
  CreateDataTransfer = CreateDataTransferImpl;
 
  IsStreamAware = IsStreamAwareImpl;
  CreateSyncStreamForDevice = CreateSyncStreamForDeviceImpl;
 
  // Define the default memory info. Allows creating custom OrtAllocators and OrtDataTransferImpls.
  // This is not strictly required for cpu-based EPs, like this example EP. However, we define it here
  // to serve as an example for non-cpu EPs.
  default_memory_info_ = Ort::MemoryInfo{"ExampleKernelEp CPU",
                                         OrtMemoryInfoDeviceType_CPU,
                                         // Use vendor ID 0 for generic allocator (e.g., webGPU)
                                         /* vendor */ 0,
                                         /* device_id */ 0,
                                         OrtDeviceMemoryType_DEFAULT,
                                         /*alignment*/ 0,
                                         OrtAllocatorType::OrtDeviceAllocator};
 
  // create data transfer for the device
  const OrtMemoryDevice* device = ep_api.MemoryInfo_GetMemoryDevice(default_memory_info_);
  data_transfer_impl_ = std::make_unique<ExampleDataTransfer>(ort_api, ep_api, device);
 
  // Create read-only allocator for use with initializers. same info as DEFAULT memory apart from the allocator type.
  // This is optional. It is only required if the readonly allocator differs from the default device allocator.
  // This is not required for this cpu-based example EP, but show it as an example.
  readonly_memory_info_ = Ort::MemoryInfo{"ExampleKernelEp CPU readonly",
                                          OrtMemoryInfoDeviceType_CPU,
                                          /*vendor*/ 0, /* device_id */ 0,
                                          OrtDeviceMemoryType_DEFAULT,
                                          /*alignment*/ 0,
                                          OrtAllocatorType::OrtReadOnlyAllocator};
}
 
ExampleKernelEpFactory::~ExampleKernelEpFactory() {
  if (kernel_registry_ != nullptr) {
    Ort::GetEpApi().ReleaseKernelRegistry(kernel_registry_);
  }
}
 
OrtStatus* ExampleKernelEpFactory::GetKernelRegistryForEp(ExampleKernelEp& ep,
                                                          const OrtKernelRegistry** out_kernel_registry) {
  *out_kernel_registry = nullptr;
 
  if (GetNumKernels() == 0) {
    return nullptr;
  }
 
  if (kernel_registry_ == nullptr) {
    // Optional state that is provided to kernels on creation (can be null).
    // We pass the OrtDataTransferImpl created by this factory to allow kernels to copy data between devices.
    void* op_kernel_state = static_cast<OrtDataTransferImpl*>(data_transfer_impl_.get());
    const char* ep_name = ep.GetName(static_cast<const OrtEp*>(&ep));
 
    // This statement creates the kernel registry and caches it in the OrtEpFactory instance.
    // We assume that all EPs created by this factory can use the same kernel registry. This may not be the
    // case in a more complex OrtEpFactory that can create EP instances that are each configured for different
    // hardware devices. In such a scenario, a different kernel registry may be created for each EP configuration.
    RETURN_IF_ERROR(CreateKernelRegistry(ep_name, op_kernel_state, &kernel_registry_));
  }
 
  *out_kernel_registry = kernel_registry_;
  return nullptr;
}
 
/*static*/
const char* ORT_API_CALL ExampleKernelEpFactory::GetNameImpl(const OrtEpFactory* this_ptr) noexcept {
  const auto* factory = static_cast<const ExampleKernelEpFactory*>(this_ptr);
  return factory->ep_name_.c_str();
}
 
/*static*/
const char* ORT_API_CALL ExampleKernelEpFactory::GetVendorImpl(const OrtEpFactory* this_ptr) noexcept {
  const auto* factory = static_cast<const ExampleKernelEpFactory*>(this_ptr);
  return factory->vendor_.c_str();
}
 
/*static*/
uint32_t ORT_API_CALL ExampleKernelEpFactory::GetVendorIdImpl(const OrtEpFactory* this_ptr) noexcept {
  const auto* factory = static_cast<const ExampleKernelEpFactory*>(this_ptr);
  return factory->vendor_id_;
}
 
/*static*/
const char* ORT_API_CALL ExampleKernelEpFactory::GetVersionImpl(const OrtEpFactory* this_ptr) noexcept {
  const auto* factory = static_cast<const ExampleKernelEpFactory*>(this_ptr);
  return factory->ep_version_.c_str();
}
 
/*static*/
OrtStatus* ORT_API_CALL ExampleKernelEpFactory::GetSupportedDevicesImpl(OrtEpFactory* this_ptr,
                                                                        const OrtHardwareDevice* const* hw_devices,
                                                                        size_t num_devices,
                                                                        OrtEpDevice** ep_devices,
                                                                        size_t max_ep_devices,
                                                                        size_t* p_num_ep_devices) noexcept {
  size_t& num_ep_devices = *p_num_ep_devices;
  auto* factory = static_cast<ExampleKernelEpFactory*>(this_ptr);
 
  num_ep_devices = 0;
 
  for (size_t i = 0; i < num_devices && num_ep_devices < max_ep_devices; ++i) {
    const OrtHardwareDevice& device = *hw_devices[i];
    auto hw_type = factory->ort_api_.HardwareDevice_Type(&device);
    if (hw_type == OrtHardwareDeviceType::OrtHardwareDeviceType_CPU) {
      // these can be returned as nullptr if you have nothing to add.
      OrtKeyValuePairs* ep_metadata = nullptr;
      OrtKeyValuePairs* ep_options = nullptr;
      factory->ort_api_.CreateKeyValuePairs(&ep_metadata);
      factory->ort_api_.CreateKeyValuePairs(&ep_options);
 
      // random example using made up values
      factory->ort_api_.AddKeyValuePair(ep_metadata, "supported_devices", "CrackGriffin 7+");
      factory->ort_api_.AddKeyValuePair(ep_options, "run_really_fast", "true");
 
      // OrtEpDevice copies ep_metadata and ep_options.
      OrtEpDevice* ep_device = nullptr;
      auto* status = factory->ort_api_.GetEpApi()->CreateEpDevice(factory, &device, ep_metadata, ep_options,
                                                                  &ep_device);
 
      factory->ort_api_.ReleaseKeyValuePairs(ep_metadata);
      factory->ort_api_.ReleaseKeyValuePairs(ep_options);
 
      if (status != nullptr) {
        return status;
      }
 
      // register the allocator info required by the EP.
      // registering OrtMemoryInfo for host accessible memory would be done in an additional call.
      // OrtReadOnlyAllocator + OrtDeviceMemoryType_DEFAULT allocator for use with initializers is optional.
      RETURN_IF_ERROR(factory->ep_api_.EpDevice_AddAllocatorInfo(ep_device, factory->default_memory_info_));
      RETURN_IF_ERROR(factory->ep_api_.EpDevice_AddAllocatorInfo(ep_device, factory->readonly_memory_info_));
 
      ep_devices[num_ep_devices++] = ep_device;
    }
  }
 
  return nullptr;
}
 
/*static*/
OrtStatus* ORT_API_CALL ExampleKernelEpFactory::CreateEpImpl(OrtEpFactory* this_ptr,
                                                             const OrtHardwareDevice* const* /*devices*/,
                                                             const OrtKeyValuePairs* const* /*ep_metadata*/,
                                                             size_t num_devices,
                                                             const OrtSessionOptions* session_options,
                                                             const OrtLogger* logger,
                                                             OrtEp** ep) noexcept {
  auto* factory = static_cast<ExampleKernelEpFactory*>(this_ptr);
  *ep = nullptr;
 
  if (num_devices != 1) {
    return factory->ort_api_.CreateStatus(ORT_INVALID_ARGUMENT,
                                          "ExampleKernelEpFactory only supports selection for one device.");
  }
 
  std::string enable_prepack_weight_sharing;
  RETURN_IF_ERROR(GetSessionConfigEntryOrDefault(*session_options, "ep.examplekernelep.enable_prepack_weight_sharing",
                                                 "0", enable_prepack_weight_sharing));
 
  ExampleKernelEp::Config config = {};
  config.enable_prepack_weight_sharing = enable_prepack_weight_sharing == "1";
 
  auto actual_ep = std::make_unique<ExampleKernelEp>(*factory, config, *logger);
  *ep = actual_ep.release();
 
  return nullptr;
}
 
/*static*/
void ORT_API_CALL ExampleKernelEpFactory::ReleaseEpImpl(OrtEpFactory* /*this_ptr*/, OrtEp* ep) noexcept {
  delete static_cast<ExampleKernelEp*>(ep);
}
 
/*static*/
OrtStatus* ORT_API_CALL ExampleKernelEpFactory::CreateAllocatorImpl(OrtEpFactory* this_ptr,
                                                                    const OrtMemoryInfo* memory_info,
                                                                    const OrtKeyValuePairs* /*allocator_options*/,
                                                                    OrtAllocator** allocator) noexcept {
  auto& factory = *static_cast<ExampleKernelEpFactory*>(this_ptr);
  *allocator = nullptr;
 
  bool is_default_allocator = memory_info == factory.default_memory_info_;
  bool is_readonly_allocator = memory_info == factory.readonly_memory_info_;
 
  if (!is_default_allocator && !is_readonly_allocator) {
    return factory.ort_api_.CreateStatus(ORT_INVALID_ARGUMENT,
                                         "INTERNAL ERROR! Unknown memory info provided to CreateAllocator. "
                                         "Value did not come directly from an OrtEpDevice returned by this factory.");
  }
 
  // Note: the same allocator handles both default and readonly allocations. A readonly only allocator would
  // typically be different.
  auto custom_allocator = std::make_unique<CustomAllocator>(memory_info);
  *allocator = custom_allocator.release();
  return nullptr;
}
 
/*static*/
void ORT_API_CALL ExampleKernelEpFactory::ReleaseAllocatorImpl(OrtEpFactory* /*this_ptr*/,
                                                               OrtAllocator* allocator) noexcept {
  delete static_cast<CustomAllocator*>(allocator);
}
 
/*static*/
OrtStatus* ORT_API_CALL ExampleKernelEpFactory::CreateDataTransferImpl(OrtEpFactory* this_ptr,
                                                                       OrtDataTransferImpl** data_transfer) noexcept {
  auto& factory = *static_cast<ExampleKernelEpFactory*>(this_ptr);
  *data_transfer = factory.data_transfer_impl_.get();
  return nullptr;
}
 
/*static*/
bool ORT_API_CALL ExampleKernelEpFactory::IsStreamAwareImpl(const OrtEpFactory* /*this_ptr*/) noexcept {
  return false;
}
 
/*static*/
OrtStatus* ORT_API_CALL ExampleKernelEpFactory::CreateSyncStreamForDeviceImpl(OrtEpFactory* /*this_ptr*/,
                                                                              const OrtMemoryDevice* /*memory_device*/,
                                                                              const OrtKeyValuePairs* /*stream_opts*/,
                                                                              OrtSyncStreamImpl** stream) noexcept {
  // Don't support sync streams in this example.
  *stream = nullptr;
  return nullptr;
}