Skip to content

Commit 03f1585

Browse files
committed
cleanup
1 parent c5c234b commit 03f1585

1 file changed

Lines changed: 13 additions & 59 deletions

File tree

src/OneWare.Core/Services/OnnxRuntimeBootstrapper.cs

Lines changed: 13 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ namespace OneWare.Core.Services;
1010
public class OnnxRuntimeBootstrapper
1111
{
1212
public const string SettingSelectedRuntimeKey = "OnnxRuntime_SelectedRuntime";
13-
public const string RuntimeProviderEnvironmentKey = "ONEWARE_ONNXRUNTIME_PROVIDER";
1413

1514
private readonly ILogger _logger;
1615
private readonly IPaths _paths;
17-
private static readonly object ResolverSync = new();
16+
private static readonly Lock ResolverSync = new();
1817
private static string? _resolverNativeDirectory;
1918
private static IntPtr _resolverOnnxRuntimeHandle;
2019
private static bool _onnxResolverRegistered;
@@ -33,80 +32,43 @@ public void Initialize()
3332

3433
try
3534
{
36-
//EnsureBundledCpuRuntimeAvailable();
37-
38-
var selectedRuntime = ResolveConfiguredValue(SettingSelectedRuntimeKey, RuntimeProviderEnvironmentKey)
39-
?.Trim();
40-
if (string.IsNullOrWhiteSpace(selectedRuntime)) selectedRuntime = "onnxruntime-cpu";
41-
SelectedRuntime = selectedRuntime;
35+
var selectedRuntime = ReadStringSetting(SettingSelectedRuntimeKey)?.Trim() ?? "no-runtime";
4236

4337
var selectedRuntimeRoot = Path.Combine(_paths.OnnxRuntimesDirectory, selectedRuntime);
44-
if (TryLoadFromRoot(selectedRuntimeRoot, $"runtime '{selectedRuntime}'"))
45-
return;
46-
47-
if (NativeLibrary.TryLoad("onnxruntime", out var existingHandle))
48-
{
49-
ConfigureOnnxRuntimeDllImportResolver(null, existingHandle);
50-
_logger.LogInformation("Loaded default ONNX Runtime from bundled/probing paths: {Handle}",
51-
existingHandle);
38+
39+
if (TryLoadFromRoot(selectedRuntimeRoot))
5240
return;
53-
}
5441

55-
_logger.LogInformation(
56-
"ONNX Runtime preload skipped. No matching runtime found in '{Path}'.", _paths.OnnxRuntimesDirectory);
42+
SelectedRuntime = "onnxruntime-cpu";
43+
44+
_logger.LogInformation("ONNX Runtime preload skipped");
5745
}
5846
catch (Exception ex)
5947
{
6048
_logger.LogWarning(ex, "Failed to initialize ONNX Runtime bootstrapper.");
6149
}
6250
}
6351

64-
private void EnsureBundledCpuRuntimeAvailable()
65-
{
66-
try
67-
{
68-
var targetPath = Path.Combine(_paths.OnnxRuntimesDirectory, "onnxruntime-cpu");
69-
if (Directory.Exists(targetPath)) return;
70-
71-
var sourcePathCandidates = new[]
72-
{
73-
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "BundledOnnxRuntimes", "onnxruntime-cpu"),
74-
Path.Combine(Path.GetDirectoryName(typeof(OnnxRuntimeBootstrapper).Assembly.Location) ??
75-
AppDomain.CurrentDomain.BaseDirectory, "BundledOnnxRuntimes", "onnxruntime-cpu")
76-
};
77-
78-
var sourcePath = sourcePathCandidates.FirstOrDefault(Directory.Exists);
79-
if (sourcePath == null) return;
80-
81-
PlatformHelper.CopyDirectory(sourcePath, targetPath);
82-
_logger.LogInformation("Seeded bundled ONNX Runtime CPU package to {Path}", targetPath);
83-
}
84-
catch (Exception ex)
85-
{
86-
_logger.LogDebug(ex, "Failed to seed bundled ONNX Runtime CPU package.");
87-
}
88-
}
89-
90-
private bool TryLoadFromRoot(string rootPath, string source)
52+
private bool TryLoadFromRoot(string rootPath)
9153
{
9254
if (!Directory.Exists(rootPath)) return false;
9355

9456
foreach (var nativeDirectory in EnumerateNativeSearchDirectories(rootPath))
95-
if (TryLoadFromNativeDirectory(nativeDirectory, source))
57+
if (TryLoadFromNativeDirectory(nativeDirectory))
9658
return true;
9759

9860
return false;
9961
}
10062

101-
private bool TryLoadFromNativeDirectory(string nativeDirectory, string source)
63+
private bool TryLoadFromNativeDirectory(string nativeDirectory)
10264
{
10365
if (!Directory.Exists(nativeDirectory)) return false;
10466

10567
var fileNames = GetOnnxRuntimeFileNameCandidates();
10668
var providersShared = PlatformHelper.GetLibraryFileName("onnxruntime_providers_shared");
10769

108-
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
109-
TrySetDllDirectory(nativeDirectory);
70+
// if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
71+
// TrySetDllDirectory(nativeDirectory);
11072

11173
// Ensure provider shared library can be resolved before loading onnxruntime itself.
11274
var providerSharedPath = Path.Combine(nativeDirectory, providersShared);
@@ -121,7 +83,7 @@ private bool TryLoadFromNativeDirectory(string nativeDirectory, string source)
12183
if (!NativeLibrary.TryLoad(fullPath, out var handle)) continue;
12284

12385
ConfigureOnnxRuntimeDllImportResolver(nativeDirectory, handle);
124-
_logger.LogInformation("Loaded ONNX Runtime from {Path} ({Source})", fullPath, source);
86+
_logger.LogInformation("Loaded ONNX Runtime from {Path}", fullPath);
12587
return true;
12688
}
12789

@@ -295,14 +257,6 @@ private static string GetRidArchitectureSuffix()
295257
};
296258
}
297259

298-
private string? ResolveConfiguredValue(string settingKey, string environmentKey)
299-
{
300-
var envValue = Environment.GetEnvironmentVariable(environmentKey);
301-
if (!string.IsNullOrWhiteSpace(envValue)) return envValue;
302-
303-
return ReadStringSetting(settingKey);
304-
}
305-
306260
private string? ReadStringSetting(string key)
307261
{
308262
if (!File.Exists(_paths.SettingsPath)) return null;

0 commit comments

Comments
 (0)