Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#include "onnxruntime_pybind_exceptions.h"
#include "onnxruntime_pybind_module_functions.h"
#include <pybind11/stl.h>
#include "core/providers/get_execution_providers.h"
#include "onnxruntime_config.h"
#include "core/common/common.h"
#include "core/session/environment.h"
#include "core/session/ort_env.h"
#include "core/session/inference_session.h"
#include "core/session/provider_bridge_ort.h"
#include "core/framework/provider_options.h"
#include "core/platform/env.h"
#include "core/common/inlined_containers.h"
 
namespace onnxruntime {
namespace python {
std::unique_ptr<IExecutionProvider> CreateExecutionProviderInstance(
    const SessionOptions& session_options,
    const std::string& type,
    const ProviderOptionsMap& provider_options_map);
bool InitArray();
static OrtEnv* ort_env = nullptr;
static OrtThreadingOptions global_tp_options;
static bool use_global_tp = false;
onnxruntime::Environment& GetEnv() {
  return ort_env->GetEnvironment();
}
 
OrtEnv* GetOrtEnv() {
  return ort_env;
}
static Status CreateOrtEnv() {
  Env::Default().GetTelemetryProvider().SetLanguageProjection(OrtLanguageProjection::ORT_PROJECTION_PYTHON);
  OrtEnv::LoggingManagerConstructionInfo lm_info{nullptr, nullptr, ORT_LOGGING_LEVEL_WARNING, "Default"};
  Status status;
  ort_env = OrtEnv::GetOrCreateInstance(lm_info, status, use_global_tp ? &global_tp_options : nullptr).release();
  if (!status.IsOK()) return status;
  // Keep the ort_env alive, don't free it. It's ok to leak the memory.
#if !defined(__APPLE__) && !defined(ORT_MINIMAL_BUILD)
  if (!InitProvidersSharedLibrary()) {
    const logging::Logger& default_logger = ort_env->GetLoggingManager()->DefaultLogger();
    LOGS(default_logger, WARNING) << "Init provider bridge failed.";
  }
#endif
  return Status::OK();
}
 
void SetGlobalThreadingOptions(const OrtThreadingOptions&& tp_options) {
  if (ort_env != nullptr) {
    OrtPybindThrowIfError(GetEnv().SetGlobalThreadingOptions(tp_options));
  }
  global_tp_options = tp_options;
  use_global_tp = true;
}
bool CheckIfUsingGlobalThreadPool() {
  return use_global_tp;
}
 
namespace py = pybind11;
 
/*
 * Register execution provider with options.
 */
static void RegisterExecutionProviders(InferenceSession* sess, const std::vector<std::string>& provider_types,
                                       const ProviderOptionsMap& provider_options_map) {
  for (const std::string& type : provider_types) {
    auto ep = CreateExecutionProviderInstance(sess->GetSessionOptions(), type, provider_options_map);
    if (ep) {
      OrtPybindThrowIfError(sess->RegisterExecutionProvider(std::move(ep)));
    }
  }
}
 
Status CreateInferencePybindStateModule(py::module& m) {
  m.doc() = "pybind11 stateful interface to ONNX runtime";
  RegisterExceptions(m);
  if (!InitArray()) {
    return Status(onnxruntime::common::ONNXRUNTIME, onnxruntime::common::FAIL, "import numpy failed");
  }
 
  ORT_RETURN_IF_ERROR(CreateOrtEnv());
 
  addGlobalMethods(m);
  addObjectMethods(m, RegisterExecutionProviders);
  addOrtValueMethods(m);
  addSparseTensorMethods(m);
  addIoBindingMethods(m);
  addAdapterFormatMethods(m);
  addGlobalSchemaFunctions(m);
  addOpSchemaSubmodule(m);
  addOpKernelSubmodule(m);
  return Status::OK();
}
 
#if defined(USE_MPI) && defined(ORT_USE_NCCL)
static constexpr bool HAS_COLLECTIVE_OPS = true;
#else
static constexpr bool HAS_COLLECTIVE_OPS = false;
#endif
 
void CreateQuantPybindModule(py::module& m);
 
// Check if we are building with GIL disabled (Free-threaded)
#ifdef Py_GIL_DISABLED
PYBIND11_MODULE(onnxruntime_pybind11_state, m, py::mod_gil_not_used()) {
#else
PYBIND11_MODULE(onnxruntime_pybind11_state, m) {
#endif
  auto st = CreateInferencePybindStateModule(m);
  if (!st.IsOK())
    throw pybind11::import_error(st.ErrorMessage());
  // move it out of shared method since training build has a little different behavior.
  m.def(
      "get_available_providers", []() -> std::vector<std::string> {
        auto available = GetAvailableExecutionProviderNames();
#if !defined(ORT_MINIMAL_BUILD)
        const auto& ep_devices = GetEnv().GetOrtEpDevices();
        available.reserve(available.size() + ep_devices.size());
 
        InlinedHashSet<std::string> existing;
        existing.reserve(available.size() + ep_devices.size());
        for (const auto& ep_name : available) {
          existing.insert(ep_name);
        }
 
        for (const OrtEpDevice* ep_device : ep_devices) {
          if (!ep_device) {
            continue;
          }
 
          if (existing.insert(ep_device->ep_name).second) {
            available.push_back(ep_device->ep_name);
          }
        }
#endif
        return available;
      },
      "Return list of available Execution Providers in this installed version of Onnxruntime. "
      "The order of elements represents the default priority order of Execution Providers "
      "from highest to lowest.");
 
  m.def("get_version_string", []() -> std::string { return ORT_VERSION; });
  m.def("get_build_info", []() -> std::string { return ORT_BUILD_INFO; });
  m.def("has_collective_ops", []() -> bool { return HAS_COLLECTIVE_OPS; });
  CreateQuantPybindModule(m);
}
}  // namespace python
}  // namespace onnxruntime