-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathONNXModel.cpp
More file actions
169 lines (124 loc) · 4.8 KB
/
ONNXModel.cpp
File metadata and controls
169 lines (124 loc) · 4.8 KB
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
#include "ONNXModel.h"
#include <dml_provider_factory.h>
#include <onnxruntime_c_api.h>
// We can't include this (or embed in the ONNXModel header) because the linker thinks they're already defined in
// subclasses like Crepe (???)
#include "ONNXUtil.hpp"
#include <iostream>
#include <filesystem>
#include <windows.h>
#include <string>
#include <dxgi1_6.h>
#include <wrl/client.h>
bool DoesFileExist(const std::wstring& path) {
DWORD fileAttributes = GetFileAttributesW(path.c_str());
if (fileAttributes == INVALID_FILE_ATTRIBUTES) {
return false; // The file does not exist or there is an error.
}
return true; // The file exists.
}
std::wstring GetDefaultAdapterName()
{
Microsoft::WRL::ComPtr<IDXGIFactory6> factory;
if (FAILED(CreateDXGIFactory1(IID_PPV_ARGS(&factory)))) {
return L"GPU";
}
Microsoft::WRL::ComPtr<IDXGIAdapter1> adapter;
if (FAILED(factory->EnumAdapterByGpuPreference(0, DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE, IID_PPV_ARGS(&adapter)))) {
return L"GPU";
}
DXGI_ADAPTER_DESC1 desc = {};
if (FAILED(adapter->GetDesc1(&desc))) {
return L"GPU";
}
return std::wstring(desc.Description);
}
bool ONNXModel::SetUpDirectML(Ort::SessionOptions& SessionOptions)
{
// This functions tells us it's deprecated but the alternative causes a crash, plus it's not mentioned in documentation (????)
OrtStatusPtr Stat = OrtSessionOptionsAppendExecutionProvider_DML(SessionOptions, 0);
OrtStatus* Status = (Stat);
if (Status)
return false; // Status NOT nullptr indicates failure
// https://onnxruntime.ai/docs/execution-providers/DirectML-ExecutionProvider.html#configuration-options
/*
The DirectML execution provider does not support the use of memory pattern optimizations or parallel execution in onnxruntime
When supplying session options during InferenceSession creation, these options must be disabled or an error will be returned.
*/
SessionOptions.DisableMemPattern();
SessionOptions.SetExecutionMode(ExecutionMode::ORT_SEQUENTIAL);
return true;
}
void ONNXModel::DisableMemReuse(OrtSessionOptions* SessPtr)
{
}
std::pair<std::vector<const char*>, std::vector<const char*>> ONNXModel::GetInputOutputNamesChar(const std::vector<std::string>& InputNames, const std::vector<std::string>& OutputNames)
{
return std::pair< std::vector<const char*>, std::vector<const char*>>{
ONNXUtil::StringVectoCharVec(InputNames),
ONNXUtil::StringVectoCharVec(OutputNames),
};
}
ONNXModel::ONNXModel()
{
ModelLoaded = false;
IsGPU = false;
DeviceName = L"CPU";
}
void ONNXModel::_overrideDevices(bool is_gpu, std::wstring device_name)
{
IsGPU = is_gpu;
DeviceName = device_name;
}
bool ONNXModel::Load(const std::wstring& ModelPath, const std::string& EnvName)
{
if (!DoesFileExist(ModelPath))
return false;
Environment = std::make_unique<Ort::Env>(ORT_LOGGING_LEVEL_WARNING, EnvName.c_str());
// Set up DirectML
Ort::SessionOptions session_options;
bool DML_Succ = SetUpDirectML(session_options);
Sess = std::make_unique<Ort::Session>(*Environment, ModelPath.c_str(), session_options);
Alloc = std::make_unique<Ort::AllocatorWithDefaultOptions>();
if (!Sess || !Alloc)
return false;
IsGPU = DML_Succ;
DeviceName = DML_Succ ? GetDefaultAdapterName() : L"CPU";
ModelLoaded = true;
return true;
}
std::vector<std::string> ONNXModel::GetOutputNames()
{
std::vector<std::string> OutputNames;
for (std::size_t i = 0; i < Sess->GetOutputCount(); i++) {
OutputNames.emplace_back(Sess->GetOutputNameAllocated(i, *Alloc).get());
}
return OutputNames;
}
std::vector<std::string> ONNXModel::GetInputNames()
{
std::vector<std::string> InputNames;
for (std::size_t i = 0; i < Sess->GetInputCount(); i++) {
InputNames.emplace_back(Sess->GetInputNameAllocated(i, *Alloc).get());
}
return InputNames;
}
std::vector<ONNXTensorElementDataType> ONNXModel::GetInputTypes()
{
std::vector<ONNXTensorElementDataType> InputTypes;
for (std::size_t i = 0; i < Sess->GetInputCount(); i++) {
InputTypes.emplace_back(Sess->GetInputTypeInfo(i).GetTensorTypeAndShapeInfo().GetElementType());
}
return InputTypes;
}
std::pair<std::vector<std::vector<int64_t>>, std::vector<std::string>> ONNXModel::GetInputs()
{
std::vector<std::vector<int64_t>> InputShapes;
std::vector<std::string> InputNames;
for (std::size_t i = 0; i < Sess->GetInputCount(); i++) {
InputNames.emplace_back(Sess->GetInputNameAllocated(i, *Alloc).get());
InputShapes.emplace_back(Sess->GetInputTypeInfo(i).GetTensorTypeAndShapeInfo().GetShape());
}
std::pair<std::vector<std::vector<int64_t>>, std::vector<std::string>> Ret(InputShapes, InputNames);
return Ret;
}