-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathProvider.cs
More file actions
201 lines (166 loc) · 7.16 KB
/
Provider.cs
File metadata and controls
201 lines (166 loc) · 7.16 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
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
// Copyright (c) TensorStack. All rights reserved.
// Licensed under the Apache 2.0 License.
using Microsoft.ML.OnnxRuntime;
using System;
using System.Collections.Generic;
using System.Linq;
using TensorStack.Common;
namespace TensorStack.Providers
{
public static class Provider
{
private static bool _isInitialized;
private const string _providerName = "DMLExecutionProvider";
/// <summary>
/// Initializes the Provider
/// </summary>
public static void Initialize()
{
if (_isInitialized)
return;
_isInitialized = true;
DeviceManager.Initialize(_providerName, SessionValidator);
}
/// <summary>
/// Initializes the Provider with the specified environment options.
/// </summary>
/// <param name="environmentOptions">The environment options.</param>
public static void Initialize(EnvironmentCreationOptions environmentOptions)
{
if (_isInitialized)
return;
_isInitialized = true;
DeviceManager.Initialize(environmentOptions, _providerName, SessionValidator);
}
/// <summary>
/// Gets the name of the provider.
/// </summary>
public static string ProviderName => _providerName;
/// <summary>
/// Gets the devices.
/// </summary>
public static IReadOnlyList<Device> GetDevices()
{
Initialize(); // Ensure Initialized
return DeviceManager.Devices;
}
/// <summary>
/// Gets the best device.
/// </summary>
/// <param name="deviceType">Type of the device.</param>
public static Device GetDevice()
{
return GetDevice(DeviceType.GPU);
}
/// <summary>
/// Gets the best device.
/// </summary>
/// <param name="deviceType">Type of the device.</param>
public static Device GetDevice(DeviceType deviceType)
{
return GetDevices().FirstOrDefault(x => x.Type == deviceType);
}
/// <summary>
/// Gets the Device by DeviceId.
/// </summary>
/// <param name="deviceType">Type of the device.</param>
/// <param name="deviceId">The device identifier.</param>
public static Device GetDevice(DeviceType deviceType, int deviceId)
{
return GetDevices().FirstOrDefault(x => x.Type == deviceType && x.DeviceId == deviceId);
}
/// <summary>
/// Gets the DirectML provider this DeviceType.
/// </summary>
/// <param name="optimizationLevel">The optimization level.</param>
public static ExecutionProvider GetProvider(GraphOptimizationLevel optimizationLevel = GraphOptimizationLevel.ORT_ENABLE_ALL)
{
return GetDevice().GetProvider(optimizationLevel);
}
/// <summary>
/// Gets the DirectML provider this DeviceType.
/// </summary>
/// <param name="deviceType">Type of the device.</param>
/// <param name="optimizationLevel">The optimization level.</param>
public static ExecutionProvider GetProvider(DeviceType deviceType, GraphOptimizationLevel optimizationLevel = GraphOptimizationLevel.ORT_ENABLE_ALL)
{
return GetDevice(deviceType).GetProvider(optimizationLevel);
}
/// <summary>
/// Gets the DirectML provider this DeviceType, DeviceId.
/// </summary>
/// <param name="deviceType">Type of the device.</param>
/// <param name="deviceId">The device identifier.</param>
/// <param name="optimizationLevel">The optimization level.</param>
public static ExecutionProvider GetProvider(DeviceType deviceType, int deviceId, GraphOptimizationLevel optimizationLevel = GraphOptimizationLevel.ORT_ENABLE_ALL)
{
return GetDevice(deviceType, deviceId).GetProvider(optimizationLevel);
}
/// <summary>
/// Gets the DirectML provider for this Device.
/// </summary>
/// <param name="device">The device.</param>
/// <param name="optimizationLevel">The optimization level.</param>
public static ExecutionProvider GetProvider(this Device device, GraphOptimizationLevel optimizationLevel = GraphOptimizationLevel.ORT_ENABLE_ALL)
{
if (device == null)
return default;
else if (device.Type == DeviceType.NPU)
return default;
else if (device.Type == DeviceType.CPU)
return CreateProvider(optimizationLevel);
return CreateProvider(device.DeviceId, optimizationLevel);
}
/// <summary>
/// Gets the DirectML provider for this DeviceId.
/// </summary>
/// <param name="deviceId">The device identifier.</param>
/// <param name="optimizationLevel">The optimization level.</param>
private static ExecutionProvider CreateProvider(int deviceId, GraphOptimizationLevel optimizationLevel = GraphOptimizationLevel.ORT_ENABLE_ALL)
{
var memoryInfo = new OrtMemoryInfo(OrtMemoryInfo.allocatorCPU, OrtAllocatorType.DeviceAllocator, deviceId, OrtMemType.Default);
return new ExecutionProvider(_providerName, memoryInfo, configuration =>
{
var sessionOptions = new SessionOptions
{
GraphOptimizationLevel = optimizationLevel
};
sessionOptions.AddSessionConfigEntries(configuration.SessionOptions);
sessionOptions.AppendExecutionProvider_DML(deviceId);
sessionOptions.AppendExecutionProvider_CPU();
return sessionOptions;
});
}
/// <summary>
/// Gets the CPU provider.
/// </summary>
/// <param name="optimizationLevel">The optimization level.</param>
/// <returns>ExecutionProvider.</returns>
private static ExecutionProvider CreateProvider(GraphOptimizationLevel optimizationLevel = GraphOptimizationLevel.ORT_ENABLE_ALL)
{
return new ExecutionProvider(DeviceManager.CPUProviderName, OrtMemoryInfo.DefaultInstance, configuration =>
{
var sessionOptions = new SessionOptions
{
EnableCpuMemArena = true,
EnableMemoryPattern = true,
GraphOptimizationLevel = optimizationLevel
};
sessionOptions.AddSessionConfigEntries(configuration.SessionOptions);
sessionOptions.AppendExecutionProvider_CPU();
return sessionOptions;
});
}
/// <summary>
/// Get SessionOptions for validation the device againts the ExecutionProvider
/// </summary>
/// <param name="device">The device.</param>
private static SessionOptions SessionValidator(Device device)
{
var session = new SessionOptions();
session.AppendExecutionProvider_DML(device.Id);
session.LogSeverityLevel = OrtLoggingLevel.ORT_LOGGING_LEVEL_ERROR;
return session;
}
}
}