Исходный код СУБД 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
// 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 <vector>
 
#include "core/framework/data_types.h"
#include "core/session/onnxruntime_type_conversion.h"
 
namespace onnxruntime {
namespace ep {
namespace adapter {
 
/// <summary>
/// Gets an OrtMLDataType for a tensor type. Throws on error.
/// </summary>
/// <param name="elem_type"></param>
/// <returns></returns>
inline const OrtDataType* GetTensorType(ONNXTensorElementDataType elem_type) {
  const OrtEpApi& ep_api = Ort::GetEpApi();
  const OrtDataType* result = nullptr;
 
  Ort::ThrowOnError(ep_api.GetTensorDataType(elem_type, &result));
  return result;
}
 
/// <summary>
/// Gets an OrtDataType for a tensor type. Returns nullptr if the host ORT does not support the type.
/// </summary>
inline const OrtDataType* TryGetTensorType(ONNXTensorElementDataType elem_type) {
  const OrtEpApi& ep_api = Ort::GetEpApi();
  const OrtDataType* result = nullptr;
 
  Ort::Status status(ep_api.GetTensorDataType(elem_type, &result));
  if (!status.IsOK()) {
    if (status.GetErrorCode() == ORT_INVALID_ARGUMENT || status.GetErrorCode() == ORT_NOT_IMPLEMENTED) {
      return nullptr;
    }
    Ort::ThrowOnError(status);
  }
  return result;
}
 
inline const OrtDataType* MLDataTypeToOrtDataType(MLDataType ml_type) {
  auto tensor_type = ml_type->AsTensorType();
  EP_ENFORCE(tensor_type != nullptr, "EP Kernel registration only supports tensor types.");
  auto elem_type = tensor_type->GetElementType();
  auto primitive_type = static_cast<const PrimitiveDataTypeBase*>(elem_type);
  auto onnx_type = utils::ToOrtTensorElementDataType(primitive_type->GetDataType());
  return GetTensorType(onnx_type);
}
 
/// <summary>
/// Converts an MLDataType to an OrtDataType. Returns nullptr if the host ORT does not support the type.
/// This enables forward-compatible plugins to register kernels with type constraints that include newer
/// data types without failing when loaded into an older host ORT.
/// </summary>
inline const OrtDataType* TryMLDataTypeToOrtDataType(MLDataType ml_type) {
  auto tensor_type = ml_type->AsTensorType();
  EP_ENFORCE(tensor_type != nullptr, "EP Kernel registration only supports tensor types.");
  auto elem_type = tensor_type->GetElementType();
  auto primitive_type = static_cast<const PrimitiveDataTypeBase*>(elem_type);
  auto onnx_type = utils::ToOrtTensorElementDataType(primitive_type->GetDataType());
  return TryGetTensorType(onnx_type);
}
 
/// <summary>
/// An adapter class partially implementing the interface of `onnxruntime::KernelDefBuilder`.
/// </summary>
struct KernelDefBuilder {
  static std::unique_ptr<KernelDefBuilder> Create() { return std::make_unique<KernelDefBuilder>(); }
 
  explicit KernelDefBuilder() {}
 
  KernelDefBuilder& SetName(const char* op_name) {
    builder_.SetOperatorType(op_name);
    return *this;
  }
 
  KernelDefBuilder& SetDomain(const char* domain) {
    builder_.SetDomain(domain);
    return *this;
  }
 
  KernelDefBuilder& SinceVersion(int since_version) {
    return SinceVersion(since_version, INT_MAX);
  }
 
  KernelDefBuilder& SinceVersion(int since_version_start, int since_version_end) {
    builder_.SetSinceVersion(since_version_start, since_version_end);
    return *this;
  }
 
  KernelDefBuilder& Provider(const char* provider_type) {
    builder_.SetExecutionProvider(provider_type);
    return *this;
  }
 
  KernelDefBuilder& TypeConstraint(const char* arg_name, std::vector<MLDataType> types) {
    std::vector<const OrtDataType*> ort_types;
    ort_types.reserve(types.size());
    for (const auto& type : types) {
      const OrtDataType* ort_type = TryMLDataTypeToOrtDataType(type);
      if (ort_type != nullptr) {
        ort_types.push_back(ort_type);
      }
    }
    if (types.empty() || !ort_types.empty()) {
      builder_.AddTypeConstraint(arg_name, ort_types);
    } else {
      valid_ = false;
    }
    return *this;
  }
 
  KernelDefBuilder& TypeConstraint(const char* arg_name, MLDataType type) {
    const OrtDataType* ort_type = TryMLDataTypeToOrtDataType(type);
    if (ort_type != nullptr) {
      builder_.AddTypeConstraint(arg_name, ort_type);
    } else {
      valid_ = false;
    }
    return *this;
  }
 
  KernelDefBuilder& MayInplace(const std::vector<std::pair<int, int>>& inplaces) {
    for (const auto& pair : inplaces) {
      builder_.AddInputOutputMutableAlias(pair.first, pair.second);
    }
    return *this;
  }
  KernelDefBuilder& MayInplace(int input_index, int output_index) {
    builder_.AddInputOutputMutableAlias(input_index, output_index);
    return *this;
  }
 
  KernelDefBuilder& Alias(const std::vector<std::pair<int, int>>& aliases) {
    for (const auto& pair : aliases) {
      builder_.AddInputOutputAlias(pair.first, pair.second);
    }
    return *this;
  }
  KernelDefBuilder& Alias(int input_index, int output_index) {
    builder_.AddInputOutputAlias(input_index, output_index);
    return *this;
  }
 
  KernelDefBuilder& InputMemoryType(OrtMemType type, int input_index) {
    builder_.SetInputMemType(input_index, type);
    return *this;
  }
 
  KernelDefBuilder& InputMemoryType(OrtMemType type, const std::vector<int>& input_indexes) {
    for (int input_index : input_indexes) {
      builder_.SetInputMemType(input_index, type);
    }
    return *this;
  }
 
  KernelDefBuilder& OutputMemoryType(OrtMemType type, int output_index) {
    builder_.SetOutputMemType(output_index, type);
    return *this;
  }
 
  KernelDefBuilder& OutputMemoryType(OrtMemType type, const std::vector<int>& output_indexes) {
    for (int output_index : output_indexes) {
      builder_.SetOutputMemType(output_index, type);
    }
    return *this;
  }
 
  // ExecQueueId is intentionally a no-op. The plugin EP manages stream
  // assignment externally; the queue id hint is not needed.
  KernelDefBuilder& ExecQueueId(int /*queue_id*/) { return *this; }
 
  Ort::KernelDef Build() { return valid_ ? builder_.Build() : Ort::KernelDef{nullptr}; }
 
 private:
  Ort::KernelDefBuilder builder_;
  bool valid_ = true;
};
 
}  // namespace adapter
}  // namespace ep
}  // namespace onnxruntime