Use EncodeArg for the index of the largest value in each row. The example uses a discrete GPU with 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 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, 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:
1, 0The 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.
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:
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:
reduce.EncodeArg(commands, GpuArgOp.Max, scores, 0, [2, 3], 0, indices, 0);
Prints:
1, 0, 0Related recipes
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 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 9s 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.