---
title: "Run argmax over a tensor axis on the GPU in C#"
url: https://recursiveai.org/onegpu/cookbook/run-argmax-on-gpu/
description: "Find the zero-based index of the largest value along a tensor axis with OneGpu, and read each result back as a signed 64-bit C# long."
version: "1.0.0"
---

# Run argmax over a tensor axis on the GPU in C#

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

[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/run-argmax-on-gpu/run-argmax-on-gpu.zip)

Use `EncodeArg` for the index of the largest value in each row. The example uses a discrete GPU with 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 scores = gpu.CreateBuffer<float>(
    [2, 9, 9, 8, 4, 0]);
using var indices = gpu.CreateBuffer<long>(2);
using var reduce = new ReduceKernels(gpu);
var commands = gpu.BeginCommands();
reduce.EncodeArg(commands, GpuArgOp.Max,
    scores, 0, [2, 3], 1, indices, 0);
gpu.Submit(commands);
var result = new long[2];
await indices.ReadAsync(result);
Console.WriteLine(string.Join(", ", result));
```

Unzip [the example](https://recursiveai.org/onegpu/cookbook/run-argmax-on-gpu/run-argmax-on-gpu.zip), then `dotnet run`.

In `EncodeArg`, the arguments after `scores` are the input element offset (`0`), input shape (`[2, 3]`), axis (`1`), output buffer (`indices`), and output element offset (`0`).

Prints:

```text
1, 0
```

## The full program

For integrated graphics, pass `Adapter = "integrated"`; if there’s no suitable Vulkan 1.3 adapter, device creation throws `GpuUnsupportedException`.

The input is a 2×3 tensor with rows `[2, 9, 9]` and `[8, 4, 0]`. Axis `1` scans across columns, so the result has two `long` elements. An out-of-range axis, an empty reduced axis, or a buffer that’s too short throws `GpuKernelShapeException`.

```csharp
using System;
using RecursiveAI.OneGpu;
using RecursiveAI.OneGpu.Kernels;

using var gpu = GpuBackends.CreateDevice(
    new() { Adapter = "discrete" });
using var scores = gpu.CreateBuffer<float>(
    [2, 9, 9, 8, 4, 0]);
using var indices = gpu.CreateBuffer<long>(2);
using var reduce = new ReduceKernels(gpu);
var commands = gpu.BeginCommands();
reduce.EncodeArg(commands, GpuArgOp.Max,
    scores, 0, [2, 3], 1, indices, 0);
gpu.Submit(commands);
var result = new long[2];
await indices.ReadAsync(result);
Console.WriteLine(string.Join(", ", result));
Console.WriteLine($"{gpu.Backend}: {gpu.Adapter.Name}");
```

Output from `dotnet run`:

```text
1, 0
Vulkan: NVIDIA GeForce RTX 4070
```

## Variations

### Get one winning row index per column

In the snippet at the top, change `indices` and `result` to hold three `long` elements each, then record the reduction on axis `0`:

```csharp
reduce.EncodeArg(commands, GpuArgOp.Max,
    scores, 0, [2, 3], 0, indices, 0);
```

Prints:

```text
1, 0, 0
```

## Related recipes

- [Reduce tensor axes on the GPU](https://recursiveai.org/onegpu/cookbook/reduce-tensor-axes-on-gpu/)
- [Read back a GPU buffer](https://recursiveai.org/onegpu/cookbook/read-back-a-gpu-buffer/)

## The short answer

Call `ReduceKernels.EncodeArg` with `GpuArgOp.Max`, the input shape and the axis to scan. Give it a `GpuBuffer<long>` with one slot per combination of the other dimensions. Submit the commands, then read back the result as signed 64-bit indices.

## Does argmax return the index or the maximum value?

Argmax returns the zero-based position along the selected axis, not the value and not an offset into the entire input buffer. For `[2, 9, 9]` the maximum value is `9` and argmax is `1`. The second row’s winner is `0`, even though the winning value is the fourth element of the flat input. A max reduction is the operation to use when you need `9` and `8` instead.

OneGpu’s [kernel reference](https://recursiveai.org/onegpu/docs/kernels/#families) specifies a 64-bit result. In C# the destination is `GpuBuffer<long>`, even if the input is a `float` or `Half` tensor and the index would fit in an `int`. The two offset arguments in `EncodeArg` are element offsets into their respective buffers; changing the input offset does not make the returned index a buffer address.

## What do axis 0 and axis 1 mean for argmax?

For shape `[rows, columns]`, axis `0` scans down the rows for each column. Axis `1` scans across the columns for each row. `-1` also selects the last axis. Pass the shape of the dense input, in row-major order: the first row comes first in the buffer, followed by the second row.

To size the output, leave out the dimension you’re scanning and multiply the remaining lengths. With shape `[2, 3]` that gives two results when scanning columns or three when scanning rows. If you want to scan an entire dense buffer as one sequence, describe it as a one-dimensional tensor and choose axis `0`; the one result is then relative to the start of that sequence.

## Which index does argmax return when values tie?

OneGpu picks the lowest index. The two `9`s in the first row are at positions `1` and `2`, so the result is `1`. The kernel also treats `NaN` as the extreme value, so a row containing `NaN` can return that position rather than the position of a finite number. If you need to ignore missing scores represented by `NaN`, handle them before this reduction.
