Add Linux CPU engine target sharing existing inference and SOP code

This commit is contained in:
sophelper 2026-11-08 18:57:15 +08:00
parent d772440c07
commit 50782fafec
4 changed files with 40 additions and 0 deletions

View File

@ -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");
}
}

View File

@ -476,6 +476,7 @@ public class MediaPipeHandDetector : IDisposable
try
{
if (!cuda) CpuSessionTuning.Apply(o);
if (cuda)
{
CudaRuntime.Prepare();

View File

@ -434,6 +434,7 @@ public class YoloDetector : IDisposable
try
{
if (!cuda) CpuSessionTuning.Apply(o);
if (cuda)
{
CudaRuntime.Prepare();

View File

@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
</PropertyGroup>
<ItemGroup>
<Compile Include="../SopHelper.Engine/Common/*.cs" Link="Common/%(Filename)%(Extension)" />
<Compile Include="../SopHelper.Engine/Sop/*.cs" Link="Sop/%(Filename)%(Extension)" />
<Compile Include="../SopHelper.Engine/Inference/*.cs" Link="Inference/%(Filename)%(Extension)" />
<Compile Include="../SopHelper.Engine/Training/*.cs" Link="Training/%(Filename)%(Extension)" />
<Compile Include="../SopHelper.Engine/Camera/ICameraService.cs" Link="Camera/ICameraService.cs" />
<Compile Include="../SopHelper.Engine/Camera/VideoRecorder.cs" Link="Camera/VideoRecorder.cs" />
<PackageReference Include="OpenCvSharp4" Version="4.10.0.20241108" />
<PackageReference Include="OpenCvSharp4.official.runtime.linux-x64" Version="4.10.0.20241108" />
<PackageReference Include="Microsoft.ML.OnnxRuntime" Version="1.23.0" />
<PackageReference Include="YamlDotNet" Version="16.3.0" />
</ItemGroup>
</Project>