oarust 0.20.0

GPU-first semantic computing runtime written in Rust
Documentation
// @generated by tool/gen/fn/generate.py; DO NOT EDIT.
// schema=matrix_blas.json schema_version=1 generator_version=64
// schema_sha256=923f078871da395b32c267810611a04885c383d5cb159a144fb1186d1d866ce8
// Semantic operation: matrix.matmul; stable kernel ID: 21.
// FP32 specialization of OA's 64x64x16 tiled GEMM: float4 vectorized
// global loads and shared-memory tiles (row-major left, K-major right for
// conflict-free fragment reads) with two barriers per K tile.

import storage;
import attributes;

struct PushConstants {
	uint left_index;
	uint right_index;
	uint output_index;
	uint m;
	uint n;
	uint k;
};

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

static const uint BM = 64;
static const uint BN = 64;
static const uint BK = 16;
static const uint TM = 4;
static const uint TN = 4;
static const uint THREADS_COL = BN / TN;
static const uint LOAD_WIDTH = 4;
static const uint KVEC = BK / LOAD_WIDTH + 1;
static const uint BN_F4 = BN / LOAD_WIDTH + 1;

groupshared float4 shared_left[BM * KVEC];
groupshared float4 shared_right[BK * BN_F4];

float4 load_slice(
	uint row,
	uint k_begin,
	uint buffer_index,
	uint row_limit
) {
	if (row < row_limit && k_begin + LOAD_WIDTH <= push.k) {
		return storage_buffers[buffer_index].Load<float4>(
			(row * push.k + k_begin) * 4u
		);
	}
	float4 values;
	[[unroll]] for (uint element = 0; element < LOAD_WIDTH; ++element) {
		values[element] = row < row_limit && k_begin + element < push.k
			? load_f32(storage_buffers[buffer_index], row * push.k + k_begin + element)
			: 0.0f;
	}
	return values;
}

[kernel_name("matmul")]
[domain("matrix")]
[variant("tiled")]
[dtype("f32")]
[status("experimental")]
[shader("compute")]
[numthreads(256, 1, 1)]
void main(uint3 group_id : SV_GroupID, uint3 group_thread_id : SV_GroupThreadID) {
	uint thread = group_thread_id.x;
	uint thread_row = thread / THREADS_COL;
	uint thread_column = thread % THREADS_COL;
	uint row_base = group_id.x * BM;
	uint column_base = group_id.y * BN;
	uint load_row = (thread * LOAD_WIDTH) / BK;
	uint load_column = (thread * LOAD_WIDTH) % BK;
	uint load_slot = load_row * KVEC + load_column / LOAD_WIDTH;
	uint right_slot = load_row / LOAD_WIDTH;
	uint right_lane = load_row % LOAD_WIDTH;

	float accumulators[TM * TN];
	[[unroll]] for (uint index = 0; index < TM * TN; ++index) {
		accumulators[index] = 0.0f;
	}

	uint k_tile_count = (push.k + BK - 1u) / BK;
	for (uint tile = 0; tile < k_tile_count; ++tile) {
		uint k_base = tile * BK;
		float4 stage_left = load_slice(
			row_base + load_row, k_base + load_column, push.left_index, push.m
		);
		float4 stage_right = load_slice(
			column_base + load_row, k_base + load_column, push.right_index, push.n
		);
		shared_left[load_slot] = stage_left;
		shared_right[(load_column + 0u) * BN_F4 + right_slot][right_lane] = stage_right.x;
		shared_right[(load_column + 1u) * BN_F4 + right_slot][right_lane] = stage_right.y;
		shared_right[(load_column + 2u) * BN_F4 + right_slot][right_lane] = stage_right.z;
		shared_right[(load_column + 3u) * BN_F4 + right_slot][right_lane] = stage_right.w;
		GroupMemoryBarrierWithGroupSync();

		[[unroll]] for (uint kk = 0; kk < BK / LOAD_WIDTH; ++kk) {
			float4 left_fragment[TM];
			[[unroll]] for (uint row = 0; row < TM; ++row) {
				left_fragment[row] =
					shared_left[(thread_row * TM + row) * KVEC + kk];
			}
			[[unroll]] for (uint lane = 0; lane < LOAD_WIDTH; ++lane) {
				float4 right_fragment =
					shared_right[(kk * LOAD_WIDTH + lane) * BN_F4 + thread_column];
				[[unroll]] for (uint row = 0; row < TM; ++row) {
					[[unroll]] for (uint column = 0; column < TN; ++column) {
						accumulators[row * TN + column] +=
							left_fragment[row][lane] * right_fragment[column];
					}
				}
			}
		}

		GroupMemoryBarrierWithGroupSync();
	}

	[[unroll]] for (uint row = 0; row < TM; ++row) {
		[[unroll]] for (uint column = 0; column < TN; ++column) {
			uint output_row = row_base + thread_row * TM + row;
			uint output_column = column_base + thread_column * TN + column;
			if (output_row < push.m && output_column < push.n) {
				store_f32(
					storage_buffers[push.output_index],
					output_row * push.n + output_column,
					accumulators[row * TN + column]
				);
			}
		}
	}
}