Recursive AI

Handle a GPU compute device lost exception in C#

OneGpu is a free, open-source GPU compute library for .NET from Recursive AI. MIT or Apache-2.0.

Catch GpuDeviceLostException and rebuild the device before retrying. The example uses a discrete GPU with Vulkan 1.3.

Terminal
dotnet add package RecursiveAI.OneGpu.Backends
dotnet add package RecursiveAI.OneGpu.Kernels

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using System;
using RecursiveAI.OneGpu;
using RecursiveAI.OneGpu.Kernels;
 
try
{
    RunGpu();
}
catch (GpuDeviceLostException error)
{
    var operation =
        error.Operation ?? "unknown";
    Console.WriteLine(
        $"Device lost at {operation}.");
    RunGpu(); // One retry.
}
 
static void RunGpu()
{
    using var gpu = GpuBackends.CreateDevice(
        new() { Adapter = "discrete" });
    using var output =
        gpu.CreateBuffer<int>(3);
    using var kernels =
        new IntegerKernels(gpu);
    var commands = gpu.BeginCommands();
    kernels.EncodeFill(
        commands, output, 0, 3, 7);
    gpu.Submit(commands);
 
    var values = new int[3];
    output.Read(values);
    Console.WriteLine(
        string.Join(", ", values));
}

Unzip the example, then dotnet run.

Prints:

Text
7, 7, 7

In the downloaded example, run dotnet run -- --simulate-loss to try the device-lost path without resetting the driver.

The full program

The compute kernel fills a new three-element buffer with 7 and reads it back. The device, buffer and kernels are local to RunGpu, so they’re disposed before the retry starts. If the replacement device fails too, the second error reaches the caller.

C#
using System;
using RecursiveAI.OneGpu;
using RecursiveAI.OneGpu.Kernels;
 
try
{
    RunGpu(args is ["--simulate-loss"]);
}
catch (GpuDeviceLostException error)
{
    var operation = error.Operation ?? "unknown";
    Console.WriteLine($"Device lost at {operation}.");
    RunGpu(false); // One retry; a second failure propagates.
}
 
static void RunGpu(bool simulateLoss)
{
    using var gpu = GpuBackends.CreateDevice(
        new() { Adapter = "discrete" });
    using var output = gpu.CreateBuffer<int>(3);
    using var kernels = new IntegerKernels(gpu);
    var commands = gpu.BeginCommands();
    kernels.EncodeFill(commands, output, 0, 3, 7);
    gpu.Submit(commands);
 
    if (simulateLoss)
        throw new GpuDeviceLostException(
            "Demo only: no driver reset.", "simulated readback");
 
    var values = new int[3];
    output.Read(values);
    Console.WriteLine(string.Join(", ", values));
    Console.WriteLine($"{gpu.Backend}: {gpu.Adapter.Name}");
}

Output from dotnet run:

Text
7, 7, 7
Vulkan: NVIDIA GeForce RTX 4070

Variations

Exercise the recovery path without resetting the driver

Terminal
dotnet run -- --simulate-loss

The full program throws a demo exception after submitting the fill. The driver is not reset; the retry really creates a new Vulkan device and runs the fill again.

Text
Device lost at simulated readback.
7, 7, 7
Vulkan: NVIDIA GeForce RTX 4070

The short answer

Catch GpuDeviceLostException outside the scope that owns the device. Log error.Operation if it’s present, then create a new device and rebuild everything the job needs. Retry only from data you can recreate, not from the old GPU buffers.

What does VK_ERROR_DEVICE_LOST mean?

A lost GPU device is no longer usable for compute. A driver reset or removal of the GPU can cause this. OneGpu’s Vulkan backend translates VK_ERROR_DEVICE_LOST into GpuDeviceLostException. The exception’s Operation gives the Vulkan call that observed the loss, such as vkQueueSubmit2, when the call is known. That name tells you where the error surfaced, not why the driver or device failed. See OneGpu’s compute errors.

Catch the exception around the whole job, including readback, not just where you create the device. If you use ReadAsync, await it inside the protected scope. Recording or submitting work isn’t the same as receiving a result you can use on the CPU.

How do you recover from a lost Vulkan device?

Dispose of the old device and everything created from it. That includes input and output buffers, compute pipelines, and any cached bindings that refer to them. Recreate them on the replacement device. A call that retries submission on the old device with the old encoder or buffer doesn’t rebuild that state.

Keep the job description and any inputs you need to replay on the CPU. Here the description is just “fill three elements with 7.” For a longer computation, keep the original input or a CPU copy of the last completed checkpoint. Don’t treat a failed readback as a checkpoint. When more than one job shares a device, stop sending new work to it and rebuild the shared state before accepting more work. If you can’t create a replacement device, surface that failure instead of retrying in a loop.

How can you test device loss handling?

Inject GpuDeviceLostException once, as the downloadable example does. The simulated operation name makes it clear that the message came from the program, not the driver. It exercises the catch block and resource rebuild without disrupting other work on the GPU. It doesn’t show that a particular driver can recover from a real reset or that an adapter will still be available after removal.

All onegpu recipes