Исходный код СУБД 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#include "test/perftest/utils.h"
#include "test/perftest/strings_helper.h"
#include <core/platform/path_lib.h>
 
#include <cstdint>
 
#include <filesystem>
 
namespace onnxruntime {
namespace perftest {
namespace utils {
 
void ListEpDevices(const Ort::Env& env) {
  std::vector<Ort::ConstEpDevice> ep_devices = env.GetEpDevices();
 
  for (size_t i = 0; i < ep_devices.size(); ++i) {
    auto device = ep_devices[i];
    std::string device_info_msg = "===== device id " + std::to_string(i) + " ======\n";
    device_info_msg += "name: " + std::string(device.EpName()) + "\n";
    device_info_msg += "vendor: " + std::string(device.EpVendor()) + "\n";
 
    auto metadata = device.EpMetadata();
    std::unordered_map<std::string, std::string> metadata_entries = metadata.GetKeyValuePairs();
    if (!metadata_entries.empty()) {
      device_info_msg += "metadata:\n";
    }
 
    for (auto& entry : metadata_entries) {
      device_info_msg += "  " + entry.first + ": " + entry.second + "\n";
    }
    device_info_msg += "\n";
    fprintf(stdout, "%s", device_info_msg.c_str());
  }
}
 
void RegisterExecutionProviderLibrary(Ort::Env& env, PerformanceTestConfig& test_config) {
  if (!test_config.plugin_ep_names_and_libs.empty()) {
    std::unordered_map<std::string, std::string> ep_names_to_libs;
    ParseSessionConfigs(ToUTF8String(test_config.plugin_ep_names_and_libs), ep_names_to_libs);
    if (ep_names_to_libs.size() > 0) {
      for (auto& pair : ep_names_to_libs) {
        const std::filesystem::path library_path = pair.second;
        const std::string registration_name = pair.first;
        Ort::Status status(Ort::GetApi().RegisterExecutionProviderLibrary(env, registration_name.c_str(), ToPathString(library_path.string()).c_str()));
        if (status.IsOK()) {
          test_config.registered_plugin_eps.push_back(registration_name);
        } else {
          fprintf(stderr, "Can't register %s plugin library: %s\n", registration_name.c_str(), status.GetErrorMessage().c_str());
        }
      }
    }
  }
}
 
void UnregisterExecutionProviderLibrary(Ort::Env& env, PerformanceTestConfig& test_config) {
  for (auto& registration_name : test_config.registered_plugin_eps) {
    Ort::Status status(Ort::GetApi().UnregisterExecutionProviderLibrary(env, registration_name.c_str()));
    if (!status.IsOK()) {
      fprintf(stderr, "%s", status.GetErrorMessage().c_str());
    }
  }
}
 
std::vector<std::string> ConvertArgvToUtf8Strings(int argc, ORTCHAR_T* argv[]) {
  std::vector<std::string> utf8_args;
  utf8_args.reserve(argc);
  for (int i = 0; i < argc; ++i) {
    std::string utf8_string = ToUTF8String(argv[i]);
 
    // Abseil flags doens't natively alias "-h" to "--help".
    // We make "-h" alias to "--help" here.
    if (utf8_string == "-h" || utf8_string == "--h") {
      utf8_args.push_back("--help");
    } else {
      utf8_args.push_back(utf8_string);
    }
  }
  return utf8_args;
}
 
std::vector<char*> CStringsFromStrings(std::vector<std::string>& utf8_args) {
  std::vector<char*> utf8_argv;
  utf8_argv.reserve(utf8_args.size());
  for (auto& str : utf8_args) {
    utf8_argv.push_back(&str[0]);
  }
  return utf8_argv;
}
 
std::vector<Ort::ConstEpDevice> AppendPluginExecutionProviders(Ort::Env& env,
                                                               Ort::SessionOptions& session_options,
                                                               const PerformanceTestConfig& test_config) {
  if (test_config.registered_plugin_eps.empty()) {
    return {};
  }
 
  std::vector<Ort::ConstEpDevice> ep_devices = env.GetEpDevices();
  // EP -> associated EP devices (All OrtEpDevice instances must be from the same execution provider)
  std::unordered_map<std::string, std::vector<Ort::ConstEpDevice>> added_ep_devices;
  std::unordered_set<int> added_ep_device_index_set;
 
  auto& ep_list = test_config.machine_config.plugin_provider_type_list;
  std::unordered_set<std::string> ep_set(ep_list.begin(), ep_list.end());
 
  // Select EP devices by provided device index
  if (!test_config.selected_ep_device_indices.empty()) {
    std::vector<int> device_list;
    device_list.reserve(test_config.selected_ep_device_indices.size());
    ParseEpDeviceIndexList(test_config.selected_ep_device_indices, device_list);
    for (auto index : device_list) {
      if (static_cast<size_t>(index) > (ep_devices.size() - 1)) {
        fprintf(stderr, "%s", "The device index provided is not correct. Will skip this device id.");
        continue;
      }
 
      Ort::ConstEpDevice& device = ep_devices[index];
      if (ep_set.find(std::string(device.EpName())) != ep_set.end()) {
        if (added_ep_device_index_set.find(index) == added_ep_device_index_set.end()) {
          added_ep_devices[device.EpName()].push_back(device);
          added_ep_device_index_set.insert(index);
          fprintf(stdout, "[Plugin EP] EP Device [Index: %d, Name: %s, Type: %d] has been added to session.\n", static_cast<int>(index), device.EpName(), device.Device().Type());
        }
      } else {
        std::string err_msg = "[Plugin EP] [WARNING] : The EP device index and its corresponding OrtEpDevice is not created from " +
                              test_config.machine_config.provider_type_name + ". Will skip adding this device.\n";
        fprintf(stderr, "%s", err_msg.c_str());
      }
    }
  } else if (!test_config.filter_ep_device_kv_pairs.empty()) {
    // Find and select the OrtEpDevice associated with the EP in "--filter_ep_devices".
    for (const auto& kv : test_config.filter_ep_device_kv_pairs) {
      for (size_t index = 0; index < ep_devices.size(); ++index) {
        auto device = ep_devices[index];
        if (ep_set.find(std::string(device.EpName())) == ep_set.end())
          continue;
 
        // Skip if deviceid was already added
        if (added_ep_devices.find(device.EpName()) != added_ep_devices.end() &&
            std::find(added_ep_devices[device.EpName()].begin(), added_ep_devices[device.EpName()].end(), device) != added_ep_devices[device.EpName()].end())
          continue;
 
        // Check both EP metadata and device metadata for a match
        auto ep_metadata_kv_pairs = device.EpMetadata().GetKeyValuePairs();
        auto device_metadata_kv_pairs = device.Device().Metadata().GetKeyValuePairs();
        auto ep_metadata_itr = ep_metadata_kv_pairs.find(kv.first);
        auto device_metadata_itr = device_metadata_kv_pairs.find(kv.first);
 
        if ((ep_metadata_itr != ep_metadata_kv_pairs.end() && kv.second == ep_metadata_itr->second) ||
            (device_metadata_itr != device_metadata_kv_pairs.end() && kv.second == device_metadata_itr->second)) {
          added_ep_devices[device.EpName()].push_back(device);
          fprintf(stdout, "[Plugin EP] EP Device [Index: %d, Name: %s, Type: %d] has been added to session.\n", static_cast<int>(index), device.EpName(), device.Device().Type());
          break;
        }
      }
    }
  } else {
    // Find and select the OrtEpDevice associated with the EP in "--plugin_eps".
    for (size_t index = 0; index < ep_devices.size(); ++index) {
      Ort::ConstEpDevice& device = ep_devices[index];
      if (ep_set.find(std::string(device.EpName())) != ep_set.end()) {
        added_ep_devices[device.EpName()].push_back(device);
        fprintf(stdout, "[Plugin EP] EP Device [Index: %d, Name: %s] has been added to session.\n", static_cast<int>(index), device.EpName());
      }
    }
  }
 
  if (added_ep_devices.empty()) {
    ORT_THROW("[ERROR] [Plugin EP]: No matching EP devices found.");
  }
 
  std::string ep_option_string = ToUTF8String(test_config.run_config.ep_runtime_config_string);
 
  // EP's associated provider option lists
  std::vector<std::unordered_map<std::string, std::string>> ep_options_list;
  ParseEpOptions(ep_option_string, ep_options_list);
 
  // If user only provide the EPs' provider option lists for the first several EPs,
  // add empty provider option lists for the rest EPs.
  if (ep_options_list.size() < ep_list.size()) {
    for (size_t i = ep_options_list.size(); i < ep_list.size(); ++i) {
      ep_options_list.emplace_back();  // Adds a new empty map
    }
  } else if (ep_options_list.size() > ep_list.size()) {
    ORT_THROW("[ERROR] [Plugin EP]: Too many EP provider option lists provided.");
  }
 
  // EP -> associated provider options
  std::unordered_map<std::string, std::unordered_map<std::string, std::string>> ep_options_map;
  for (size_t i = 0; i < ep_list.size(); ++i) {
    ep_options_map.emplace(ep_list[i], ep_options_list[i]);
  }
 
  std::vector<Ort::ConstEpDevice> selected_ep_devices;
  for (auto& ep_and_devices : added_ep_devices) {
    auto& ep = ep_and_devices.first;
    auto& devices = ep_and_devices.second;
    session_options.AppendExecutionProvider_V2(env, devices, ep_options_map[ep]);
    selected_ep_devices.insert(selected_ep_devices.end(), devices.begin(), devices.end());
  }
 
  return selected_ep_devices;
}
 
std::optional<PluginEpAllocatorSelection> GetPluginEpAllocator(Ort::Env& env,
                                                               const std::vector<Ort::ConstEpDevice>& ep_devices) {
  // More than one device appended means no single unambiguous allocator to pick.
  if (ep_devices.size() != 1) {
    fprintf(stdout, "[Plugin EP] %d EP devices appended. Using the CPU allocator.\n",
            static_cast<int>(ep_devices.size()));
    return std::nullopt;
  }
 
  const Ort::ConstEpDevice& ep_device = ep_devices[0];
 
  Ort::ConstMemoryInfo default_mem_info = ep_device.GetMemoryInfo(OrtDeviceMemoryType_DEFAULT);
  if (default_mem_info) {
    Ort::UnownedAllocator allocator = env.GetSharedAllocator(default_mem_info);
    if (allocator) {
      fprintf(stdout, "[Plugin EP] Using the default allocator.\n");
      return PluginEpAllocatorSelection{allocator, /*is_host_accessible*/ false};
    }
  }
 
  Ort::ConstMemoryInfo host_accessible_mem_info = ep_device.GetMemoryInfo(OrtDeviceMemoryType_HOST_ACCESSIBLE);
  if (host_accessible_mem_info) {
    Ort::UnownedAllocator allocator = env.GetSharedAllocator(host_accessible_mem_info);
    if (allocator) {
      fprintf(stdout, "[Plugin EP] Using the host accessible allocator.\n");
      return PluginEpAllocatorSelection{allocator, /*is_host_accessible*/ true};
    }
  }
 
  return std::nullopt;
}
 
bool UsesNvidiaDevice(Ort::Env& env, const PerformanceTestConfig& test_config) {
  const auto ep_devices = env.GetEpDevices();
  const auto& plugin_eps = test_config.machine_config.plugin_provider_type_list;
 
  const bool uses_nvidia_device = std::any_of(ep_devices.begin(), ep_devices.end(),
                                              [&plugin_eps](const Ort::ConstEpDevice& ep_device) {
                                                const std::string ep_name(ep_device.EpName());
                                                if (std::find(plugin_eps.begin(), plugin_eps.end(), ep_name) == plugin_eps.end()) {
                                                  return false;
                                                }
 
                                                const auto hardware_device = ep_device.Device();
                                                return hardware_device.Type() == OrtDevice::GPU &&
                                                       hardware_device.VendorId() == OrtDevice::VendorIds::NVIDIA;
                                              });
 
  return uses_nvidia_device;
}
 
}  // namespace utils
}  // namespace perftest
}  // namespace onnxruntime