Исходный код СУБД 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#include "lib/Api.Ort/pch.h"
 
#ifdef USE_DML
 
#include "OnnxruntimeDmlSessionBuilder.h"
#include "OnnxruntimeEngine.h"
#include "OnnxruntimeErrors.h"
#include "LearningModelDevice.h"
 
using namespace _winml;
 
HRESULT OnnxruntimeDmlSessionBuilder::RuntimeClassInitialize(
  OnnxruntimeEngineFactory* engine_factory, ID3D12Device* device, ID3D12CommandQueue* queue, bool metacommands_enabled
) {
  engine_factory_ = engine_factory;
  device_.copy_from(device);
  queue_.copy_from(queue);
  metacommands_enabled_ = metacommands_enabled;
  return S_OK;
}
 
HRESULT
OnnxruntimeDmlSessionBuilder::CreateSessionOptions(OrtSessionOptions** options) {
  RETURN_HR_IF_NULL(E_POINTER, options);
 
  auto ort_api = engine_factory_->UseOrtApi();
  auto winml_adapter_api = engine_factory_->UseWinmlAdapterApi();
 
  OrtSessionOptions* ort_options;
  RETURN_HR_IF_NOT_OK_MSG(ort_api->CreateSessionOptions(&ort_options), ort_api);
 
  auto session_options = UniqueOrtSessionOptions(ort_options, ort_api->ReleaseSessionOptions);
 
  // set the graph optimization level to all (used to be called level 3)
  RETURN_HR_IF_NOT_OK_MSG(
    ort_api->SetSessionGraphOptimizationLevel(session_options.get(), GraphOptimizationLevel::ORT_ENABLE_ALL), ort_api
  );
 
  // Disable the mem pattern session option for DML. It will cause problems with how memory is allocated.
  RETURN_HR_IF_NOT_OK_MSG(ort_api->DisableMemPattern(session_options.get()), ort_api);
 
  // Request the dml ep
  RETURN_HR_IF_NOT_OK_MSG(
    winml_adapter_api->OrtSessionOptionsAppendExecutionProvider_DML(
      session_options.get(), device_.get(), queue_.get(), metacommands_enabled_
    ),
    ort_api
  );
 
#ifndef _WIN64
  auto use_arena = false;
#else
  auto use_arena = true;
#endif
  RETURN_HR_IF_NOT_OK_MSG(
    winml_adapter_api->OrtSessionOptionsAppendExecutionProvider_CPU(session_options.get(), use_arena), ort_api
  );
 
  // call release() so the underlying OrtSessionOptions object isn't freed
  *options = session_options.release();
 
  return S_OK;
}
 
HRESULT OnnxruntimeDmlSessionBuilder::CreateSession(
  OrtSessionOptions* options,
  OrtThreadPool* inter_op_thread_pool,
  OrtThreadPool* intra_op_thread_pool,
  OrtSession** session
) {
  RETURN_HR_IF_NULL(E_POINTER, session);
 
  auto ort_api = engine_factory_->UseOrtApi();
  auto winml_adapter_api = engine_factory_->UseWinmlAdapterApi();
 
  OrtEnv* ort_env;
  RETURN_IF_FAILED(engine_factory_->GetOrtEnvironment(&ort_env));
 
  OrtSession* ort_session_raw;
  RETURN_HR_IF_NOT_OK_MSG(
    winml_adapter_api->CreateSessionWithoutModel(
      ort_env, options, inter_op_thread_pool, intra_op_thread_pool, &ort_session_raw
    ),
    engine_factory_->UseOrtApi()
  );
  auto ort_session = UniqueOrtSession(ort_session_raw, ort_api->ReleaseSession);
 
  *session = ort_session.release();
 
  return S_OK;
}
 
HRESULT OnnxruntimeDmlSessionBuilder::Initialize(OrtSession* session) {
  RETURN_HR_IF_NULL(E_INVALIDARG, session);
  auto winml_adapter_api = engine_factory_->UseWinmlAdapterApi();
 
  RETURN_HR_IF_NOT_OK_MSG(winml_adapter_api->SessionInitialize(session), engine_factory_->UseOrtApi());
 
  OrtExecutionProvider* ort_provider;
  RETURN_HR_IF_NOT_OK_MSG(
    winml_adapter_api->SessionGetExecutionProvider(session, 0, &ort_provider), engine_factory_->UseOrtApi()
  );
 
  // Flush the D3D12 work from the DML execution provider
  RETURN_HR_IF_NOT_OK_MSG(
    winml_adapter_api->DmlExecutionProviderFlushContext(ort_provider), engine_factory_->UseOrtApi()
  );
 
  return S_OK;
}
 
#endif USE_DML