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.
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 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, then dotnet run.
Prints the channel values interleaved by pixel:
10, 20, 11, 21, 12, 22, 13, 23, 14, 24, 15, 25The full program
The download checks every output position using its batch, channel, row and column coordinates.
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}");
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.
transpose.Encode(cmd, input, 0, [1, 2, 2, 3], [0, 2, 3, 1], output, 0);
Related recipes
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 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. 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 changes how that multiplication reads its operand; it doesn’t write a new four-dimensional layout for a later GPU pass.