---
title: "Resize a dense tensor on the GPU in C#"
url: https://recursiveai.org/onegpu/cookbook/resize-dense-tensor-on-gpu/
description: "Run nearest-neighbor and bilinear tensor resize in C# with explicit coordinate mapping and a runnable Vulkan example."
version: "1.0.0"
---

# Resize a dense tensor on the GPU in C#

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

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneGpu.Kernels) · [GitHub](https://github.com/recursive-ai-oss/onegpu-csharp) · [Docs](https://recursiveai.org/onegpu/docs/) · [Download example (.zip)](https://recursiveai.org/onegpu/cookbook/resize-dense-tensor-on-gpu/resize-dense-tensor-on-gpu.zip)

Use nearest or bilinear resize on a grid of numbers. This example uses Vulkan 1.3.

```shell
dotnet add package RecursiveAI.OneGpu.Backends
dotnet add package RecursiveAI.OneGpu.Kernels
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
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](https://recursiveai.org/onegpu/cookbook/resize-dense-tensor-on-gpu/resize-dense-tensor-on-gpu.zip), then `dotnet run`.

Prints:

```text
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.

```csharp
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:

```text
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:

```csharp
var shape = new ResizeProblem(1, 2, 2, 3, 3, mode)
{
    Coordinate = GpuResizeCoordinate.AlignCorners,
    Nearest = GpuResizeNearest.RoundPreferCeil
};
```

Output:

```text
Nearest: 0, 4, 4, 8, 12, 12, 8, 12, 12
Linear: 0, 2, 4, 4, 6, 8, 8, 10, 12
```

## Related recipes

- [Pool tensors on the GPU](https://recursiveai.org/onegpu/cookbook/pool-tensors-on-gpu/)
- [Compute softmax on the GPU](https://recursiveai.org/onegpu/cookbook/compute-softmax-on-gpu/)

## 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](https://recursiveai.org/onegpu/docs/conv/) lists what the kernel does not implement, including cubic interpolation and antialiasing.
