---
title: "Convert NCHW to NHWC on the GPU in C#"
url: https://recursiveai.org/onegpu/cookbook/transpose-tensor-layout-on-gpu/
description: "Convert a dense tensor from NCHW to NHWC in C# with OneGpu, then read it back and check which input value belongs at each output offset."
version: "1.0.0"
---

# Convert NCHW to NHWC 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/transpose-tensor-layout-on-gpu/transpose-tensor-layout-on-gpu.zip)

Use `EncodeNchwToNhwc` to group channels by pixel instead of storing each channel in a separate plane. On Windows or Linux, use a Vulkan 1.3 driver and loader.

```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 RecursiveAI.OneGpu;
using RecursiveAI.OneGpu.Kernels;

using var gpu =
    GpuBackends.CreateDevice();
using var transpose =
    new TransposeKernels(gpu);
using var input = gpu.CreateBuffer<int>(
    [10, 11, 12, 13, 14, 15,
     20, 21, 22, 23, 24, 25]);
using var output = gpu.CreateBuffer<int>(12);
var cmd = gpu.BeginCommands();
// N=1, C=2, H=2, W=3
transpose.EncodeNchwToNhwc(cmd, input, 0,
    1, 2, 2, 3, output, 0);
gpu.Submit(cmd);
var values = new int[12];
await output.ReadAsync(values);
Console.WriteLine(string.Join(", ", values));
```

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

Prints the channel values interleaved by pixel:

```text
10, 20, 11, 21, 12, 22, 13, 23, 14, 24, 15, 25
```

## The full program

The download checks every output position using its batch, channel, row and column coordinates.

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

using var gpu = GpuBackends.CreateDevice();
const int batches = 1, channels = 2, height = 2, width = 3;
int[] sample = [10, 11, 12, 13, 14, 15,
     20, 21, 22, 23, 24, 25];
using var input = gpu.CreateBuffer<int>(sample);
using var output = gpu.CreateBuffer<int>(sample.Length);
using var transpose = new TransposeKernels(gpu);
var commands = gpu.BeginCommands();
transpose.EncodeNchwToNhwc(commands, input, 0,
    batches, channels, height, width, output, 0);
gpu.Submit(commands);
var values = new int[sample.Length];
await output.ReadAsync(values);

for (int n = 0; n < batches; n++)
    for (int h = 0; h < height; h++)
        for (int w = 0; w < width; w++)
            for (int c = 0; c < channels; c++)
            {
                int nchw = ((n * channels + c) * height + h) * width + w;
                int nhwc = ((n * height + h) * width + w) * channels + c;
                if (values[nhwc] != sample[nchw])
                    throw new InvalidOperationException($"Wrong value at NHWC offset {nhwc}.");
            }
Console.WriteLine(string.Join(", ", values));
Console.WriteLine($"NHWC shape: [{batches}, {height}, {width}, {channels}]");
Console.WriteLine("Every offset matched the CPU mapping.");
Console.WriteLine($"{gpu.Backend}: {gpu.Adapter.Name}");
```

```text
10, 20, 11, 21, 12, 22, 13, 23, 14, 24, 15, 25
NHWC shape: [1, 2, 3, 2]
Every offset matched the CPU mapping.
Vulkan: NVIDIA GeForce RTX 4070
```

An invalid permutation or buffer range throws `GpuKernelShapeException` before recording the pass. Use separate input and output buffers for this transpose.

## Variations

**Supply the permutation yourself.** Replace `EncodeNchwToNhwc` with the general `Encode` call. It prints the same sequence.

```csharp
transpose.Encode(cmd, input, 0,
    [1, 2, 2, 3], [0, 2, 3, 1],
    output, 0);
```

## Related recipes

- [Run NCHW convolution on the GPU](https://recursiveai.org/onegpu/cookbook/gpu-convolution/)
- [Read a GPU buffer back to the CPU in C#](https://recursiveai.org/onegpu/cookbook/read-back-a-gpu-buffer/)

## The short answer

Call `TransposeKernels.EncodeNchwToNhwc` with a dense NCHW buffer and the four input dimensions. Allocate the same number of elements for the output and describe it as `[N, H, W, C]`. This copies values into their new positions on the GPU; it doesn’t just attach a new shape to the original bytes.

## What’s the difference between NCHW and NHWC?

`N` is batch size, `C` is channel count, `H` is height and `W` is width. In a dense NCHW tensor, width varies fastest. For one two-channel image, the first plane contains all the values for channel zero, then the second plane contains all the values for channel one. In dense NHWC, channel varies fastest: once you reach a pixel, its channels occupy consecutive elements. These [memory layout diagrams](https://docs.nvidia.com/deeplearning/cudnn/backend/v9.3.0/developer/core-concepts.html) show these two memory arrangements.

The example starts with a single image of height `2`, width `3` and two channels, so NCHW is `[1, 2, 2, 3]`. After transposing, NHWC is `[1, 2, 3, 2]`. The first pixel contains `10, 20` and the pixel to its right contains `11, 21`. The shape tells you how to interpret those values; the contents actually had to move to put them there.

## Which permutation converts NCHW to NHWC?

Use `[0, 2, 3, 1]`: the dimensions of the output come from input axes `0` (batch), `2` (height), `3` (width), then `1` (channel). That is the permutation [the OneGpu helper passes to `Encode`](https://github.com/recursive-ai-oss/onegpu-csharp/blob/5d3317caecb82fedd96642e54b0ab33bcb9759c5/src/RecursiveAI.OneGpu.Kernels/TransposeKernels.cs#L167-L182). To go the other way, use `EncodeNhwcToNchw`, which applies `[0, 3, 1, 2]` to an NHWC input. Both helpers take the dimension arguments in `n, c, h, w` order.

The CPU loop in the download computes both flat offsets from the same coordinates. For `(n=0, c=1, h=0, w=0)`, the NCHW offset is `6` and the NHWC offset is `1`. Both contain `20`. In a general permutation, the destination shape and the destination offset both depend on the order of the axes.

This is not a reshape or a matrix multiplication transpose flag. A reshape keeps the linear order, which would put `10, 11` at the first pixel. A [matrix multiplication transpose flag](https://recursiveai.org/onegpu/docs/gemm/) changes how that multiplication reads its operand; it doesn’t write a new four-dimensional layout for a later GPU pass.
