diff --git a/src/SopHelper.Engine/Inference/CpuSessionTuning.cs b/src/SopHelper.Engine/Inference/CpuSessionTuning.cs new file mode 100644 index 0000000..00c3717 --- /dev/null +++ b/src/SopHelper.Engine/Inference/CpuSessionTuning.cs @@ -0,0 +1,17 @@ +using Microsoft.ML.OnnxRuntime; + +namespace SopHelper.Engine.Inference; + +internal static class CpuSessionTuning +{ + // Opt in at deployment; existing Windows installations retain their defaults. + internal static void Apply(SessionOptions options) + { + if (!int.TryParse(Environment.GetEnvironmentVariable("SOPHELPER_CPU_THREADS"), out var threads)) return; + if (threads < 1 || threads > Environment.ProcessorCount) + throw new ArgumentOutOfRangeException("SOPHELPER_CPU_THREADS", "线程数必须介于 1 和逻辑处理器数量之间"); + options.IntraOpNumThreads = threads; + options.InterOpNumThreads = 1; + options.AddSessionConfigEntry("session.intra_op.allow_spinning", "0"); + } +} diff --git a/src/SopHelper.Engine/Inference/MediaPipeHandDetector.cs b/src/SopHelper.Engine/Inference/MediaPipeHandDetector.cs index 7add33e..1c0b7f5 100644 --- a/src/SopHelper.Engine/Inference/MediaPipeHandDetector.cs +++ b/src/SopHelper.Engine/Inference/MediaPipeHandDetector.cs @@ -476,6 +476,7 @@ public class MediaPipeHandDetector : IDisposable try { + if (!cuda) CpuSessionTuning.Apply(o); if (cuda) { CudaRuntime.Prepare(); diff --git a/src/SopHelper.Engine/Inference/YoloDetector.cs b/src/SopHelper.Engine/Inference/YoloDetector.cs index 5fd5e69..5131aed 100644 --- a/src/SopHelper.Engine/Inference/YoloDetector.cs +++ b/src/SopHelper.Engine/Inference/YoloDetector.cs @@ -434,6 +434,7 @@ public class YoloDetector : IDisposable try { + if (!cuda) CpuSessionTuning.Apply(o); if (cuda) { CudaRuntime.Prepare(); diff --git a/src/SopHelper.Linux.Engine/SopHelper.Linux.Engine.csproj b/src/SopHelper.Linux.Engine/SopHelper.Linux.Engine.csproj new file mode 100644 index 0000000..3f2976a --- /dev/null +++ b/src/SopHelper.Linux.Engine/SopHelper.Linux.Engine.csproj @@ -0,0 +1,21 @@ + + + net8.0 + enable + enable + true + false + + + + + + + + + + + + + +