ReLU zeros negative values. On Vulkan 1.3, pass the same buffer twice to EncodeUnary to overwrite it.
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 ops = new ElementwiseKernels(gpu); Half[] values = [(Half)(-4), (Half)2]; using var data = gpu.CreateBuffer<Half>(values); var cmd = gpu.BeginCommands(); ops.EncodeUnary(cmd, GpuUnaryOp.Relu, data, 0, data, 0, values.Length); gpu.Submit(cmd); await data.ReadAsync(values); Console.WriteLine(string.Join(", ", values));
Unzip the example, then dotnet run.
Prints:
0, 2The full program
The program prints the uploaded values and then reads back from the same GpuBuffer<Half>. Both uses of data start at element 0 and cover two elements, so there’s no second device buffer for the result.
If you only have integrated graphics, pass Adapter = "integrated"; Adapter = "discrete" throws GpuUnsupportedException if no Vulkan adapter matches.
If the range goes past the buffer, or the buffer doesn’t end on a whole 32-bit word, the kernel throws GpuKernelShapeException before recording the work.
using System; using RecursiveAI.OneGpu; using RecursiveAI.OneGpu.Kernels; using var gpu = GpuBackends.CreateDevice( new() { Adapter = "discrete" }); using var ops = new ElementwiseKernels(gpu); Half[] values = [(Half)(-4), (Half)2]; Console.WriteLine("Before: " + string.Join(", ", values)); using var data = gpu.CreateBuffer<Half>(values); var cmd = gpu.BeginCommands(); ops.EncodeUnary(cmd, GpuUnaryOp.Relu, data, 0, data, 0, values.Length); gpu.Submit(cmd); await data.ReadAsync(values); Console.WriteLine("After: " + string.Join(", ", values)); Console.WriteLine($"{gpu.Backend}: {gpu.Adapter.Name}");
Output:
Before: -4, 2 After: 0, 2 Vulkan: NVIDIA GeForce RTX 4070
Variations
Keep part of each negative value
Replace the EncodeUnary call in the snippet at the top. Leaky ReLU scales negatives by p0 instead of setting them to zero.
ops.EncodeUnary(cmd, GpuUnaryOp.LeakyRelu, data, 0, data, 0, values.Length, p0: 0.25f);
Prints:
-1, 2Related recipes
The short answer
On Vulkan, pass the same GpuBuffer<Half> as both operands of ElementwiseKernels.EncodeUnary, with the same input and output offset and GpuUnaryOp.Relu. Submit the pass and read back that buffer, not a new one. The original negative entries are gone.
What does “in place” mean for ReLU?
“In place” means ReLU writes each result back to the element it read. In the example, the GPU changes -4 to 0 at index 0 and leaves the 2 at index 1. The two data arguments are references to one GPU allocation, not copies of it. Reassigning a C# variable name to a new buffer wouldn’t do the same thing.
The kernel docs allow overlapping half-precision inputs and outputs only when the offset and extent are the same. Two Half elements fill one 32-bit word. For an odd number of elements, use GpuKernelBuffers.PaddedHalfLength(count) to size the buffer, but still pass the actual element count to the kernel. Don’t move the destination forward by one element: a write could replace an input that another thread hasn’t read.
When should you use in-place ReLU?
Use it after the last pass that needs the original values. If another branch still needs the signed input, either record that reader first or put the activated values in a separate buffer. ReLU isn’t reversible: after a value becomes zero, you can’t tell how negative it was. A later pass recorded in the same command encoder sees the writes from the earlier pass, as described in the compute ordering docs.
This example is for Vulkan, not browser WebGPU: the unary kernel binds the buffer for input and output in one pass, and the browser disallows that combination. Don’t take the same-buffer pattern as permission to alias other kernels’ operands; check each kernel’s overlap rules.