oarust 0.20.0

GPU-first semantic computing runtime written in Rust
Documentation
// Mechanical adaptation of the NCHW Conv2dTranspose input adjoint to NCDHW:
// each input element accumulates output-gradient × weight over its taps.

import storage;
import attributes;

struct PushConstants {
	uint output_gradient_index;
	uint weight_index;
	uint input_gradient_index;
	uint batch_size;
	uint input_channels;
	uint output_channels;
	uint input_depth;
	uint input_height;
	uint input_width;
	uint kernel_size;
	uint stride;
	uint padding;
	uint output_depth;
	uint output_height;
	uint output_width;
};

[[vk::push_constant]] PushConstants push;
[[vk::binding(0, 0)]] RWByteAddressBuffer storage_buffers[];

[kernel_name("conv_3d_transpose_backward")]
[domain("ml")]
[variant("generic")]
[dtype("f32")]
[status("experimental")]
[shader("compute")]
[numthreads(256, 1, 1)]
void main(uint3 thread_id : SV_DispatchThreadID) {
	uint input_index = thread_id.x;
	uint in_plane = push.input_height * push.input_width;
	uint input_count = push.batch_size * push.input_channels
		* push.input_depth * in_plane;
	if (input_index >= input_count) {
		return;
	}

	uint input_x = input_index % push.input_width;
	uint quotient = input_index / push.input_width;
	uint input_y = quotient % push.input_height;
	quotient /= push.input_height;
	uint input_z = quotient % push.input_depth;
	quotient /= push.input_depth;
	uint input_channel = quotient % push.input_channels;
	uint batch = quotient / push.input_channels;
	uint out_plane = push.output_height * push.output_width;
	uint kernel_volume = push.kernel_size * push.kernel_size * push.kernel_size;
	float gradient = 0.0f;
	for (uint output_channel = 0; output_channel < push.output_channels; ++output_channel) {
		for (uint kernel_z = 0; kernel_z < push.kernel_size; ++kernel_z) {
			int output_z = int(input_z * push.stride + kernel_z) - int(push.padding);
			if (output_z < 0 || output_z >= int(push.output_depth)) {
				continue;
			}
			for (uint kernel_y = 0; kernel_y < push.kernel_size; ++kernel_y) {
				int output_y = int(input_y * push.stride + kernel_y) - int(push.padding);
				if (output_y < 0 || output_y >= int(push.output_height)) {
					continue;
				}
				for (uint kernel_x = 0; kernel_x < push.kernel_size; ++kernel_x) {
					int output_x = int(input_x * push.stride + kernel_x)
						- int(push.padding);
					if (output_x < 0 || output_x >= int(push.output_width)) {
						continue;
					}
					uint output_offset =
						((batch * push.output_channels + output_channel)
							* push.output_depth + uint(output_z)) * out_plane
						+ uint(output_y) * push.output_width
						+ uint(output_x);
					uint weight_offset =
						(input_channel * push.output_channels + output_channel)
							* kernel_volume
						+ kernel_z * push.kernel_size * push.kernel_size
						+ kernel_y * push.kernel_size
						+ kernel_x;
					gradient += load_f32(
						storage_buffers[push.output_gradient_index],
						output_offset
					) * load_f32(storage_buffers[push.weight_index], weight_offset);
				}
			}
		}
	}
	store_f32(storage_buffers[push.input_gradient_index], input_index, gradient);
}