Use nearest or bilinear resize on a grid of numbers. This example uses Vulkan 1.3.
dotnet add package RecursiveAI.OneGpu.Backends dotnet add package RecursiveAI.OneGpu.Kernels
Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using System; using RecursiveAI.OneGpu; using RecursiveAI.OneGpu.Kernels; using var gpu = GpuBackends.CreateDevice( new() { Adapter = "discrete" }); using var src = gpu.CreateBuffer<float>( [0, 4, 8, 12]); using var dst = gpu.CreateBuffer<float>(9); using var resize = new ResizeKernels(gpu); foreach (var mode in new[] { GpuResizeMode.Nearest, GpuResizeMode.Linear }) { var shape = new ResizeProblem( 1, 2, 2, 3, 3, mode) { Coordinate = GpuResizeCoordinate.AlignCorners }; using var plan = resize.CreatePlan(shape); var commands = gpu.BeginCommands(); resize.Encode(commands, plan, src, 0, dst, 0); gpu.Submit(commands); var values = new float[9]; await dst.ReadAsync(values); Console.WriteLine($"{mode}: " + string.Join(", ", values)); }
Unzip the example, then dotnet run.
Prints:
Nearest: 0, 0, 4, 0, 0, 4, 8, 8, 12 Linear: 0, 2, 4, 4, 6, 8, 8, 10, 12
The full program
The input rows are [0, 4] and [8, 12]. Both modes keep the four corners; the full program checks every output, including the center. Invalid dimensions or undersized buffers throw GpuKernelShapeException.
On Vulkan, Adapter = "discrete" picks the first adapter of that kind or with “discrete” in its name and throws GpuUnsupportedException if none matches; for integrated graphics, use Adapter = "integrated" or omit Adapter to take the default, but neither will help if there’s no usable Vulkan 1.3 adapter.
using System; using RecursiveAI.OneGpu; using RecursiveAI.OneGpu.Kernels; using var gpu = GpuBackends.CreateDevice( new() { Adapter = "discrete" }); using var src = gpu.CreateBuffer<float>( [0, 4, 8, 12]); using var dst = gpu.CreateBuffer<float>(9); using var resize = new ResizeKernels(gpu); foreach (var mode in new[] { GpuResizeMode.Nearest, GpuResizeMode.Linear }) { var shape = new ResizeProblem( 1, 2, 2, 3, 3, mode) { Coordinate = GpuResizeCoordinate.AlignCorners }; using var plan = resize.CreatePlan(shape); var commands = gpu.BeginCommands(); resize.Encode(commands, plan, src, 0, dst, 0); gpu.Submit(commands); var values = new float[9]; await dst.ReadAsync(values); float[] expected = mode == GpuResizeMode.Nearest ? [0, 0, 4, 0, 0, 4, 8, 8, 12] : [0, 2, 4, 4, 6, 8, 8, 10, 12]; if (!values.AsSpan().SequenceEqual(expected)) throw new InvalidOperationException($"Unexpected {mode} samples."); Console.WriteLine($"{mode}: " + string.Join(", ", values)); } Console.WriteLine($"{gpu.Backend}: {gpu.Adapter.Name}");
Output:
Nearest: 0, 0, 4, 0, 0, 4, 8, 8, 12 Linear: 0, 2, 4, 4, 6, 8, 8, 10, 12 Vulkan: NVIDIA GeForce RTX 4070
Variations
Round nearest ties up
In the snippet at the top, replace the shape initializer with:
var shape = new ResizeProblem(1, 2, 2, 3, 3, mode) { Coordinate = GpuResizeCoordinate.AlignCorners, Nearest = GpuResizeNearest.RoundPreferCeil };
Output:
Nearest: 0, 4, 4, 8, 12, 12, 8, 12, 12 Linear: 0, 2, 4, 4, 6, 8, 8, 10, 12
Related recipes
The short answer
Use ResizeKernels from OneGpu. Set the input and output dimensions, interpolation mode, and coordinate rule in ResizeProblem; create a plan and pass it with your GPU buffers to Encode. The example resizes one 2 × 2 plane to 3 × 3. The numbers are already in a buffer: there is no image file to decode or encode.
What’s the difference between nearest-neighbor and bilinear interpolation?
Nearest-neighbor copies just one input value for each output cell. Bilinear interpolation uses the fractional coordinates in both directions to blend the neighboring values. In the center of the example, both input coordinates are 0.5. Nearest with ties rounded down takes the top-left value 0; bilinear gives equal weight to all four inputs and returns 6. On the top edge, the middle cell is halfway between 0 and 4, so the linear result is 2. Nearest is useful for a grid of labels, where blending would invent values that aren’t labels.
How do align-corners and half-pixel coordinates differ?
AlignCorners maps the first and last output positions to the first and last input positions. On an axis growing from two to three cells, that gives source positions 0, 0.5 and 1. HalfPixel uses (j + 0.5) / scale - 0.5, where j is the output index. Those are different sampling rules even when the input and output shapes match between implementations. When matching a model, use its coordinate mode as well as its interpolation mode. If it specifies scales rather than sizes, ScaleHeight and ScaleWidth can carry those values; zero derives the scale from the extents.
A ResizePlan builds the source positions and weights on the host and keeps the tables on the device. Reuse it for the same geometry and rules rather than rebuilding it for every input. Leading dimensions are independent planes: for [N, C, H, W], pass N * C as the plane count. The resize documentation lists what the kernel does not implement, including cubic interpolation and antialiasing.