Исходный код СУБД 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#pragma once
 
#include <memory>
#include <mutex>
#include <string>
#include <unordered_map>
#include <vector>
 
#include "core/framework/execution_provider.h"
#include "core/graph/graph_viewer.h"
#include "core/common/logging/logging.h"
#include "core/platform/telemetry_strings.h"
#include "core/platform/telemetry_redaction.h"
#ifdef _WIN32
#include <Windows.h>
#include <winmeta.h>
#include <evntrace.h>
#include "core/platform/tracing.h"
#include "core/platform/windows/telemetry.h"
#endif
 
namespace onnxruntime {
 
/**
Class for managing lookup of the execution providers in a session.
 
Add() must not run concurrently with accessors or iteration, except for
GetProviderOptionsSnapshot(), which is explicitly synchronized for ETW capture-state callbacks.
*/
class ExecutionProviders {
 public:
  ExecutionProviders() {
#ifdef _WIN32
    // Register callback for ETW capture state (rundown)
    etw_callback_ = onnxruntime::WindowsTelemetry::EtwInternalCallback(
        [this](
            LPCGUID SourceId,
            ULONG IsEnabled,
            UCHAR Level,
            ULONGLONG MatchAnyKeyword,
            ULONGLONG MatchAllKeyword,
            PEVENT_FILTER_DESCRIPTOR FilterData,
            PVOID CallbackContext) {
          (void)SourceId;
          (void)Level;
          (void)MatchAnyKeyword;
          (void)MatchAllKeyword;
          (void)FilterData;
          (void)CallbackContext;
 
          // Check if this callback is for capturing state
          if ((IsEnabled == EVENT_CONTROL_CODE_CAPTURE_STATE) &&
              ((MatchAnyKeyword & static_cast<ULONGLONG>(onnxruntime::logging::ORTTraceLoggingKeyword::Session)) != 0)) {
            for (const auto& [provider_id, options] : GetProviderOptionsSnapshot()) {
              LogProviderOptions(provider_id, options, true);
            }
          }
        });
    WindowsTelemetry::RegisterInternalCallback(etw_callback_);
#endif
  }
 
  ~ExecutionProviders() {
#ifdef _WIN32
    WindowsTelemetry ::UnregisterInternalCallback(etw_callback_);
#endif
  }
 
  common::Status
  Add(const std::string& provider_id, const std::shared_ptr<IExecutionProvider>& p_exec_provider) {
    // A null provider would crash later when we dereference it (e.g. GetProviderOptions()).
    // Fail with a clear error instead so the caller can diagnose the missing provider.
    if (p_exec_provider == nullptr) {
      auto status = ORT_MAKE_STATUS(ONNXRUNTIME, FAIL, "Provider ", provider_id, " is null and cannot be registered.");
      LOGS_DEFAULT(ERROR) << status.ErrorMessage();
      return status;
    }
 
    const auto check_provider_not_registered = [&]() -> common::Status {
      if (provider_idx_map_.find(provider_id) != provider_idx_map_.end()) {
        return ORT_MAKE_STATUS(ONNXRUNTIME, FAIL, "Provider ", provider_id, " has already been registered.");
      }
 
      return Status::OK();
    };
 
    common::Status duplicate_status;
    {
      std::lock_guard<std::mutex> lock(exec_providers_mutex_);
      duplicate_status = check_provider_not_registered();
    }
    if (!duplicate_status.IsOK()) {
      LOGS_DEFAULT(ERROR) << duplicate_status.ErrorMessage();
      return duplicate_status;
    }
 
    ProviderOptions providerOptions = p_exec_provider->GetProviderOptions();
 
    {
      std::lock_guard<std::mutex> lock(exec_providers_mutex_);
      // make sure there are no issues before we change any internal data structures
      duplicate_status = check_provider_not_registered();
      if (duplicate_status.IsOK()) {
        // index that provider will have after insertion
        auto new_provider_idx = exec_providers_.size();
 
        ORT_IGNORE_RETURN_VALUE(provider_idx_map_.insert({provider_id, new_provider_idx}));
 
        // update execution provider options
        exec_provider_options_[provider_id] = providerOptions;
        exec_provider_ids_.push_back(provider_id);
        exec_providers_.push_back(p_exec_provider);
      }
    }
    if (!duplicate_status.IsOK()) {
      LOGS_DEFAULT(ERROR) << duplicate_status.ErrorMessage();
      return duplicate_status;
    }
 
#ifdef _WIN32
    LogProviderOptions(provider_id, providerOptions, false);
#endif
    return Status::OK();
  }
 
  const IExecutionProvider* Get(const onnxruntime::Node& node) const {
    return Get(node.GetExecutionProviderType());
  }
 
  const IExecutionProvider* Get(onnxruntime::ProviderType provider_id) const {
    auto it = provider_idx_map_.find(provider_id);
    if (it == provider_idx_map_.end()) {
      return nullptr;
    }
 
    return exec_providers_[it->second].get();
  }
 
  IExecutionProvider* Get(onnxruntime::ProviderType provider_id) {
    auto it = provider_idx_map_.find(provider_id);
    if (it == provider_idx_map_.end()) {
      return nullptr;
    }
 
    return exec_providers_[it->second].get();
  }
 
  bool Empty() const { return exec_providers_.empty(); }
 
  size_t NumProviders() const { return exec_providers_.size(); }
 
  using const_iterator = typename std::vector<std::shared_ptr<IExecutionProvider>>::const_iterator;
  const_iterator begin() const noexcept { return exec_providers_.cbegin(); }
  const_iterator end() const noexcept { return exec_providers_.cend(); }
 
  const std::vector<std::string>& GetIds() const { return exec_provider_ids_; }
  const ProviderOptionsMap& GetAllProviderOptions() const { return exec_provider_options_; }
 
  using ProviderOptionsSnapshot = std::vector<std::pair<std::string, ProviderOptions>>;
 
  ProviderOptionsSnapshot GetProviderOptionsSnapshot() const {
    std::lock_guard<std::mutex> lock(exec_providers_mutex_);
    ProviderOptionsSnapshot provider_options_snapshot;
    provider_options_snapshot.reserve(exec_provider_ids_.size());
    for (const auto& provider_id : exec_provider_ids_) {
      auto it = exec_provider_options_.find(provider_id);
      if (it != exec_provider_options_.end()) {
        provider_options_snapshot.emplace_back(provider_id, it->second);
      }
    }
 
    return provider_options_snapshot;
  }
 
  bool GetCpuProviderWasImplicitlyAdded() const { return cpu_execution_provider_was_implicitly_added_; }
 
  void SetCpuProviderWasImplicitlyAdded(bool cpu_execution_provider_was_implicitly_added) {
    cpu_execution_provider_was_implicitly_added_ = cpu_execution_provider_was_implicitly_added;
  }
 
 private:
  // Some compilers emit incomprehensive output if this is allowed
  // with a container that has unique_ptr or something move-only.
  ORT_DISALLOW_COPY_AND_ASSIGNMENT(ExecutionProviders);
 
  // Synchronizes provider registration with ETW capture-state snapshots.
  mutable std::mutex exec_providers_mutex_;
 
  void LogProviderOptions(const std::string& provider_id, const ProviderOptions& options, bool capture_state) {
    const Env& env = Env::Default();
    // Convert ProviderOptions to string for telemetry logging
    bool truncated = false;
    const auto provider_options_str = telemetry_detail::FormatTelemetryMap(options, ",", ":", &truncated);
    env.GetTelemetryProvider().LogProviderOptions(
        provider_id, ScrubStringForTelemetry(provider_options_str, truncated), capture_state);
  }
 
  std::vector<std::shared_ptr<IExecutionProvider>> exec_providers_;
  std::vector<std::string> exec_provider_ids_;
  ProviderOptionsMap exec_provider_options_;
 
  // maps for fast lookup of an index into exec_providers_
  std::unordered_map<std::string, size_t> provider_idx_map_;
 
  // Whether the CPU provider was implicitly added to a session for fallback (true),
  // or whether it was explicitly added by the caller.
  bool cpu_execution_provider_was_implicitly_added_ = false;
 
#ifdef _WIN32
  WindowsTelemetry::EtwInternalCallback etw_callback_;
#endif
};
}  // namespace onnxruntime