Heimdall Gitea / Detect CI scope (push) Successful in 6s
Heimdall Gitea / Build and tests (push) Successful in 19m19s
Heimdall Gitea / Publish versioned release (push) Skipped
Heimdall Gitea / Build and scan server image (push) Successful in 6m13s
Heimdall Gitea / Build and scan agent image (push) Successful in 14m44s
Heimdall Gitea / Publish Docker Hub latest tags (push) Successful in 1m48s
Heimdall Gitea / Build and scan NVIDIA Agent image (push) Successful in 19m9s
Heimdall Gitea / CI policy (push) Successful in 10s
Merge pull request #138 Co-authored-by: Arnaud Dartois <admin@tordais.cc>
99 lines
3.3 KiB
C#
99 lines
3.3 KiB
C#
using Heimdall.Agent.Services;
|
|
|
|
namespace Heimdall.Agent.Tests;
|
|
|
|
public sealed class OnnxExecutionProviderSelectionTests
|
|
{
|
|
[Fact]
|
|
public void CreateSession_CpuRequest_UsesOnlyCpu()
|
|
{
|
|
var cudaCalls = 0;
|
|
var fallbacks = new List<string>();
|
|
|
|
var session = OnnxExecutionProviderSelection.CreateSession(
|
|
"CPU",
|
|
[OnnxExecutionProviderSelection.CudaProviderName, OnnxExecutionProviderSelection.CpuProviderName],
|
|
() => { cudaCalls++; return "cuda"; },
|
|
() => "cpu",
|
|
fallbacks.Add,
|
|
out var actualProvider);
|
|
|
|
session.ShouldBe("cpu");
|
|
cudaCalls.ShouldBe(0);
|
|
fallbacks.ShouldBeEmpty();
|
|
actualProvider.ShouldBe(OnnxExecutionProviderSelection.CpuProviderName);
|
|
}
|
|
|
|
[Fact]
|
|
public void CreateSession_CudaRegisteredAndCreated_ReportsCuda()
|
|
{
|
|
var session = OnnxExecutionProviderSelection.CreateSession(
|
|
"CUDA",
|
|
[OnnxExecutionProviderSelection.CudaProviderName],
|
|
() => "cuda",
|
|
() => "cpu",
|
|
_ => throw new InvalidOperationException("Unexpected fallback"),
|
|
out var actualProvider);
|
|
|
|
session.ShouldBe("cuda");
|
|
actualProvider.ShouldBe(OnnxExecutionProviderSelection.CudaProviderName);
|
|
}
|
|
|
|
[Fact]
|
|
public void CreateSession_CudaNotRegistered_FallsBackToCpu()
|
|
{
|
|
var fallbacks = new List<string>();
|
|
|
|
var session = OnnxExecutionProviderSelection.CreateSession(
|
|
"CUDA",
|
|
[OnnxExecutionProviderSelection.CpuProviderName],
|
|
() => "cuda",
|
|
() => "cpu",
|
|
fallbacks.Add,
|
|
out var actualProvider);
|
|
|
|
session.ShouldBe("cpu");
|
|
fallbacks.ShouldContain("CUDAExecutionProvider is not registered in this ONNX Runtime build");
|
|
actualProvider.ShouldBe(OnnxExecutionProviderSelection.CpuProviderName);
|
|
}
|
|
|
|
[Fact]
|
|
public void CreateSession_CudaInitializationFails_FallsBackToCpu()
|
|
{
|
|
var fallbacks = new List<string>();
|
|
|
|
var session = OnnxExecutionProviderSelection.CreateSession(
|
|
"CUDA",
|
|
[OnnxExecutionProviderSelection.CudaProviderName],
|
|
() => throw new DllNotFoundException("synthetic missing CUDA runtime"),
|
|
() => "cpu",
|
|
fallbacks.Add,
|
|
out var actualProvider);
|
|
|
|
session.ShouldBe("cpu");
|
|
fallbacks.ShouldContain(nameof(DllNotFoundException));
|
|
actualProvider.ShouldBe(OnnxExecutionProviderSelection.CpuProviderName);
|
|
}
|
|
[Fact]
|
|
public void CreateSession_CudaDeviceUnavailable_FallsBackWithoutTryingCuda()
|
|
{
|
|
var cudaCalls = 0;
|
|
var fallbacks = new List<string>();
|
|
|
|
var session = OnnxExecutionProviderSelection.CreateSession(
|
|
"CUDA",
|
|
[OnnxExecutionProviderSelection.CudaProviderName, OnnxExecutionProviderSelection.CpuProviderName],
|
|
() => { cudaCalls++; return "cuda"; },
|
|
() => "cpu",
|
|
fallbacks.Add,
|
|
out var actualProvider,
|
|
cudaDeviceAvailable: false);
|
|
|
|
session.ShouldBe("cpu");
|
|
cudaCalls.ShouldBe(0);
|
|
fallbacks.ShouldContain("No NVIDIA CUDA device was detected by the Agent");
|
|
actualProvider.ShouldBe(OnnxExecutionProviderSelection.CpuProviderName);
|
|
}
|
|
|
|
}
|