---
title: "Run an in-place GPU activation in C#"
url: https://recursiveai.org/onegpu/cookbook/run-in-place-gpu-kernel/
description: "Apply ReLU to a Half buffer in place with OneGpu and Vulkan, using the same input and output range instead of allocating a second GPU buffer."
version: "1.0.0"
---

# Run an in-place GPU activation 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/run-in-place-gpu-kernel/run-in-place-gpu-kernel.zip)

ReLU zeros negative values. On Vulkan 1.3, pass the same buffer twice to `EncodeUnary` to overwrite it.

```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 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](https://recursiveai.org/onegpu/cookbook/run-in-place-gpu-kernel/run-in-place-gpu-kernel.zip), then `dotnet run`.

Prints:

```text
0, 2
```

## The 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.

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

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

```csharp
ops.EncodeUnary(cmd, GpuUnaryOp.LeakyRelu,
    data, 0, data, 0, values.Length, p0: 0.25f);
```

Prints:

```text
-1, 2
```

## Related recipes

- [Add a bias and apply ReLU on the GPU](https://recursiveai.org/onegpu/cookbook/broadcast-bias-and-activation-on-gpu/)
- [Reuse GPU bindings across compute dispatches](https://recursiveai.org/onegpu/cookbook/reuse-gpu-kernel-bindings/)

## 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](https://recursiveai.org/onegpu/docs/kernels/#half-precision) 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](https://recursiveai.org/onegpu/docs/compute/#passes-and-ordering).

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.
