-
Notifications
You must be signed in to change notification settings - Fork 408
Expand file tree
/
Copy pathBasicEp.cs
More file actions
83 lines (72 loc) · 2.8 KB
/
BasicEp.cs
File metadata and controls
83 lines (72 loc) · 2.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
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace Contoso.ML.OnnxRuntime.EP.Basic;
public static class BasicEp
{
/// <summary>
/// Returns the path to the plugin EP library DLL contained by this package.
/// Can be passed to OrtEnv::RegisterExecutionProviderLibrary().
///
/// Note: It is recommended that plugin EP packages provide this information to applications.
/// </summary>
/// <returns>EP library path</returns>
/// <exception cref="FileNotFoundException">If the EP DLL file path does not exist</exception>
public static string GetLibraryPath()
{
string rootDir = GetNativeDirectory();
string osArch = $"{GetOSTag()}-{GetArchTag()}";
string epDllPath = Path.GetFullPath(Path.Combine(rootDir, "runtimes", osArch,
"native", "basic_plugin_ep.dll"));
if (!File.Exists(epDllPath))
{
// This indicates a packaging error.
throw new FileNotFoundException($"Did not find EP DLL file: {epDllPath}");
}
return epDllPath;
}
/// <summary>
/// Returns the names of the EPs created by the plugin EP library.
/// Can be used to select a OrtEpDevice from those returned by OrtEnv::GetEpDevices().
///
/// Note: It is recommended that plugin EP packages provide this information to applications.
/// </summary>
/// <returns>Array of EP names</returns>
public static string[] GetEpNames()
{
return ["BasicPluginExecutionProvider"];
}
/// <summary>
/// Returns the name of the one EP supported by this plugin EP library.
///
/// Note: This is a convenience function exposed by plugin EP packages that only have one EP name.
/// </summary>
/// <returns></returns>
public static string GetEpName()
{
return GetEpNames()[0];
}
private static string GetNativeDirectory()
{
var assemblyDir = Path.GetDirectoryName(typeof(BasicEp).Assembly.Location);
// Try returning where this assembly lives (works for framework-dependent)
if (!string.IsNullOrEmpty(assemblyDir) && Directory.Exists(assemblyDir))
return assemblyDir;
// Fallback to AppContext.BaseDirectory (works for single-file/self-contained)
return AppContext.BaseDirectory;
}
private static string GetOSTag()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) return "win";
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) return "linux";
return "unknown";
}
private static string GetArchTag()
{
return RuntimeInformation.OSArchitecture switch
{
Architecture.X64 => "x64",
Architecture.Arm64 => "arm64",
_ => "unknown"
};
}
}