Исходный код СУБД 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#pragma once
 
#if !defined(ORT_EP_API_ADAPTER_HEADER_INCLUDED)
#error "This header should not be included directly. Include ep/adapters.h instead."
#endif
 
#include <memory>
#include <shared_mutex>
#include <string>
#include <utility>
#include <vector>
 
#include "core/common/common.h"
#include "core/common/inlined_containers.h"
#include "core/common/narrow.h"
#include "core/common/status.h"
#include "core/framework/config_options.h"
#include "core/framework/error_code_helper.h"
#include "core/framework/tensor_shape.h"
#include "core/framework/tensor.h"
 
#include "allocator.h"
#include "node.h"
#include "kernel_def.h"
#include "tensor_helper.h"
 
namespace onnxruntime {
class DataTransferManager;
class IExecutionProvider;
}  // namespace onnxruntime
 
namespace onnxruntime {
namespace ep {
namespace adapter {
 
/// <summary>
/// An adapter class partially implementing the interface of `onnxruntime::OpKernelInfo`.
/// </summary>
struct OpKernelInfo {
  //
  // A helper struct to cache kernel info data
  //
  // Because `KernelCreatePtrFn` is defined to use `const OpKernelInfo&` as parameter type of the kernel creation function, `OpKernelInfo` has to be copyable.
  // This means we cannot store cached data like `constant_input_tensors_` in `OpKernelInfo` directly to avoid ownership issues.
  //
  // As a workaround, we define this struct `KernelInfoCache` here to represent the cached data. We use a shared pointer to `KernelInfoCache` in `OpKernelInfo`
  // to manage the lifetime of the cached data.
  struct KernelInfoCache {
    explicit KernelInfoCache(const OrtKernelInfo* kernel_info) : kernel_info_(kernel_info) {
      Ort::ConstKernelInfo info{kernel_info};
      ort_ep_ = info.GetEp();
      ORT_ENFORCE(ort_ep_ != nullptr, "Plugin EP adapter requires a non-null OrtEp");
      ep_impl_ = static_cast<const Ep*>(ort_ep_)->EpImpl();
 
      const size_t input_count = info.GetInputCount();
      constant_input_tensors.resize(input_count);
      for (size_t i = 0; i < input_count; ++i) {
        int is_constant = 0;
        Ort::ConstValue const_input = info.GetTensorConstantInput(gsl::narrow_cast<int>(i), &is_constant);
        if (is_constant && const_input != nullptr && const_input.IsTensor()) {
          constant_input_tensors[i] = CreateTensorFromApiValue(const_cast<OrtValue*>(static_cast<const OrtValue*>(const_input)));
        }
      }
    }
    const OrtKernelInfo* kernel_info_;
    const OrtEp* ort_ep_{};
    const ::onnxruntime::IExecutionProvider* ep_impl_{};
    std::vector<Tensor> constant_input_tensors;
 
    mutable std::shared_mutex allocator_cache_mutex_;
    mutable InlinedHashMap<OrtMemType, AllocatorPtr> allocator_cache_;
 
    ORT_DISALLOW_COPY_ASSIGNMENT_AND_MOVE(KernelInfoCache);
  };
 
  explicit OpKernelInfo(const OrtKernelInfo* info) : info_(info), cache_{std::make_shared<KernelInfoCache>(info)} {
  }
 
  const DataTransferManager& GetDataTransferManager() const noexcept {
    return (static_cast<const Ep*>(cache_->ort_ep_))->GetDataTransferManager();
  }
 
  AllocatorPtr GetAllocator(OrtMemType mem_type) const {
    {
      std::shared_lock lock(cache_->allocator_cache_mutex_);
      auto it = cache_->allocator_cache_.find(mem_type);
      if (it != cache_->allocator_cache_.end()) {
        return it->second;
      }
    }
 
    std::unique_lock lock(cache_->allocator_cache_mutex_);
    // Double-check after acquiring exclusive lock
    auto it = cache_->allocator_cache_.find(mem_type);
    if (it != cache_->allocator_cache_.end()) {
      return it->second;
    }
 
    OrtAllocator* ort_allocator_raw = nullptr;
    Ort::Status status(Ort::GetApi().KernelInfoGetAllocator(cache_->kernel_info_, mem_type, &ort_allocator_raw));
 
    if (!status.IsOK() || ort_allocator_raw == nullptr) {
      cache_->allocator_cache_.emplace(mem_type, nullptr);
      return nullptr;
    }
 
    Ort::Allocator ort_allocator{ort_allocator_raw};
    auto allocator = std::make_shared<IAllocatorWrappingOrtAllocator>(std::move(ort_allocator));
    cache_->allocator_cache_.emplace(mem_type, allocator);
    return allocator;
  }
 
  Node node() const noexcept {
    return Node{cache_->kernel_info_};
  }
  const IExecutionProvider* GetExecutionProvider() const noexcept {
    return cache_->ep_impl_;
  }
  const OrtEp* GetOrtEp() const noexcept {
    return cache_->ort_ep_;
  }
 
  KernelDef GetKernelDef() const noexcept {
    return KernelDef{cache_->kernel_info_};
  }
 
  const Ort::ConstKernelInfo GetKernelInfo() const noexcept {
    return Ort::ConstKernelInfo{cache_->kernel_info_};
  }
 
  ConfigOptions GetConfigOptions() const noexcept {
    ConfigOptions config_options;
    config_options.configurations = info_.GetConfigEntries().GetKeyValuePairs();
    return config_options;
  }
 
  int GetInputCount() const noexcept {
    return gsl::narrow_cast<int>(info_.GetInputCount());
  }
 
  const std::vector<Tensor>& GetConstantInputTensors() const noexcept {
    return cache_->constant_input_tensors;
  }
 
  bool TryGetConstantInput(int input_index, const Tensor** constant_input_value) const {
    if (input_index < 0 || static_cast<size_t>(input_index) >= cache_->constant_input_tensors.size()) {
      return false;
    }
    const Tensor& tensor = cache_->constant_input_tensors[input_index];
    if (tensor.DataRaw() != nullptr) {
      *constant_input_value = &tensor;
      return true;
    }
    return false;
  }
 
  template <typename T>
  [[nodiscard]] T GetAttrOrDefault(const std::string& name, const T& default_value) const {
    T tmp{};
    return GetAttr<T>(name, &tmp).IsOK() ? tmp : default_value;
  }
  template <typename T>
  void GetAttrOrDefault(const std::string& name, T* value, const T& default_value) const {
    if (!GetAttr<T>(name, value).IsOK())
      *value = default_value;
  }
  template <typename T>
  [[nodiscard]] T GetAttr(const std::string& name) const {
    T value{};
    ORT_THROW_IF_ERROR(GetAttr(name, &value));
    return value;
  }
  template <typename T>
  Status GetAttr(const std::string& name, T* value) const {
    return GetAttrImpl(cache_->kernel_info_, name.c_str(), value);
  }
  template <typename T>
  Status GetAttrs(const std::string& name, std::vector<T>& values) const {
    return GetAttrsImpl(cache_->kernel_info_, name.c_str(), values);
  }
 
  Status GetAttrs(const std::string& name, TensorShapeVector& out) const {
    std::vector<int64_t> shape;
    Status status = GetAttrs<int64_t>(name, shape);
    if (status.IsOK()) {
      out.reserve(shape.size());
      out.assign(shape.begin(), shape.end());
    }
    return status;
  }
 
  template <typename T>
  [[nodiscard]] std::vector<T> GetAttrsOrDefault(const std::string& name,
                                                 const std::vector<T>& default_value = {}) const {
    std::vector<T> tmp;
    return GetAttrs<T>(name, tmp).IsOK() ? tmp : default_value;
  }
  [[nodiscard]] TensorShapeVector GetAttrsOrDefault(const std::string& name,
                                                    const TensorShapeVector& default_value = {}) const {
    TensorShapeVector tmp;
    return GetAttrs(name, tmp).IsOK() ? tmp : default_value;
  }
 
 private:
  static Status GetAttrImpl(const OrtKernelInfo* info, const char* name, float* out) {
    return ToStatusAndRelease(Ort::GetApi().KernelInfoGetAttribute_float(info, name, out));
  }
 
  static Status GetAttrImpl(const OrtKernelInfo* info, const char* name, int64_t* out) {
    return ToStatusAndRelease(Ort::GetApi().KernelInfoGetAttribute_int64(info, name, out));
  }
 
  static Status GetAttrImpl(const OrtKernelInfo* info, const char* name, std::string* out) {
    size_t size = 0;
    // Feed nullptr for the data buffer to query the true size of the string attribute.
    ORT_RETURN_IF_ERROR(ToStatusAndRelease(Ort::GetApi().KernelInfoGetAttribute_string(info, name, nullptr, &size)));
 
    std::string value;
    value.resize(size);
    ORT_RETURN_IF_ERROR(
        ToStatusAndRelease(Ort::GetApi().KernelInfoGetAttribute_string(info, name, value.data(), &size)));
    value.resize(size - 1);  // remove the terminating character '\0'
    *out = std::move(value);
    return Status::OK();
  }
 
  static Status GetAttrsImpl(const OrtKernelInfo* info, const char* name, std::vector<float>& out) {
    size_t size = 0;
    ORT_RETURN_IF_ERROR(
        ToStatusAndRelease(Ort::GetApi().KernelInfoGetAttributeArray_float(info, name, nullptr, &size)));
 
    std::vector<float> values(size);
    ORT_RETURN_IF_ERROR(
        ToStatusAndRelease(Ort::GetApi().KernelInfoGetAttributeArray_float(info, name, values.data(), &size)));
    out.swap(values);
    return Status::OK();
  }
 
  static Status GetAttrsImpl(const OrtKernelInfo* info, const char* name, std::vector<int64_t>& out) {
    size_t size = 0;
    ORT_RETURN_IF_ERROR(
        ToStatusAndRelease(Ort::GetApi().KernelInfoGetAttributeArray_int64(info, name, nullptr, &size)));
 
    std::vector<int64_t> values(size);
    ORT_RETURN_IF_ERROR(
        ToStatusAndRelease(Ort::GetApi().KernelInfoGetAttributeArray_int64(info, name, values.data(), &size)));
    out.swap(values);
    return Status::OK();
  }
 
  static Status GetAttrsImpl(const OrtKernelInfo* info, const char* name, std::vector<std::string>& out) {
    OrtAllocator* allocator = nullptr;
    ORT_RETURN_IF_ERROR(ToStatusAndRelease(Ort::GetApi().GetAllocatorWithDefaultOptions(&allocator)));
    size_t size = 0;
    ORT_RETURN_IF_ERROR(
        ToStatusAndRelease(Ort::GetApi().KernelInfoGetAttributeArray_string(info, name, allocator, nullptr, &size)));
    if (size == 0) {
      out.clear();
      return Status::OK();
    }
 
    char** raw_values = nullptr;
    ORT_RETURN_IF_ERROR(
        ToStatusAndRelease(
            Ort::GetApi().KernelInfoGetAttributeArray_string(info, name, allocator, &raw_values, &size)));
 
    // The allocator owns both the array and every string in it, so release them on all exit paths: constructing the
    // std::string copies below can throw. Free through the OrtAllocator function pointer rather than
    // Ort::AllocatorWithDefaultOptions::Free(), which throws on failure and so must not run in this deleter.
    auto free_raw_values = [allocator, size](char** values_to_free) {
      for (size_t i = 0; i < size; ++i) {
        if (values_to_free[i] != nullptr) {
          allocator->Free(allocator, values_to_free[i]);
        }
      }
      allocator->Free(allocator, values_to_free);
    };
    std::unique_ptr<char*, decltype(free_raw_values)> raw_values_guard{raw_values, std::move(free_raw_values)};
 
    std::vector<std::string> values;
    values.reserve(size);
    for (size_t i = 0; i < size; ++i) {
      values.emplace_back(raw_values[i] != nullptr ? raw_values[i] : "");
    }
    out.swap(values);
    return Status::OK();
  }
 
  const Ort::ConstKernelInfo info_;
  std::shared_ptr<KernelInfoCache> cache_;
};
 
}  // namespace adapter
}  // namespace ep
}  // namespace onnxruntime