oarust 0.13.0

GPU-first semantic computing engine written in Rust
// @generated by tool/gen/fn/generate.py; DO NOT EDIT.
// schema=matrix_blas.json schema_version=1 generator_version=64
// schema_sha256=160e3299221cb767c356fd9ab1d5229633ad4a7fc60117a7dc81665ca3b24f94
// Semantic operation: matrix.mat_mul_nt; stable kernel ID: 701.
// FP32 arbitrary-layout tiled GEMM: the 64x64x16 shared-memory schedule
// with per-operand strided addressing and `group_id.z` batch selection.
// Loads stay scalar because operand column strides are runtime values.

import storage;
import attributes;

struct PushConstants {
	uint left_index;
	uint right_index;
	uint output_index;
	uint m;
	uint n;
	uint k;
	uint a_row_stride;
	uint a_col_stride;
	uint a_batch_stride;
	uint a_offset;
	uint b_row_stride;
	uint b_col_stride;
	uint b_batch_stride;
	uint b_offset;
	uint c_row_stride;
	uint c_col_stride;
	uint c_batch_stride;
	uint c_offset;
};

[[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 BK_PAD = BK + 1;

groupshared float shared_left[BM * BK_PAD];
groupshared float shared_right[BN * BK_PAD];

void load_slice_strided(
	uint row,
	uint k_begin,
	uint buffer_index,
	uint row_limit,
	uint base_offset,
	uint row_stride,
	uint col_stride,
	out float values[LOAD_WIDTH]
) {
	uint row_address = base_offset + row * row_stride;
	[[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_address + (k_begin + element) * col_stride)
			: 0.0f;
	}
}

[kernel_name("mat_mul_nt_strided_tiled")]
[domain("matrix")]
[variant("strided_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 batch = group_id.z;
	uint row_base = group_id.x * BM;
	uint column_base = group_id.y * BN;
	uint shared_row = (thread * LOAD_WIDTH) / BK;
	uint shared_column = (thread * LOAD_WIDTH) % BK;
	uint shared_offset = shared_row * BK_PAD + shared_column;
	uint left_base = push.a_offset + batch * push.a_batch_stride;
	uint right_base = push.b_offset + batch * push.b_batch_stride;

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

	for (uint k_base = 0; k_base < push.k; k_base += BK) {
		float left_values[LOAD_WIDTH];
		float right_values[LOAD_WIDTH];
		load_slice_strided(
			row_base + shared_row,
			k_base + shared_column,
			push.left_index,
			push.m,
			left_base,
			push.a_row_stride,
			push.a_col_stride,
			left_values
		);
		load_slice_strided(
			column_base + shared_row,
			k_base + shared_column,
			push.right_index,
			push.n,
			right_base,
			push.b_row_stride,
			push.b_col_stride,
			right_values
		);
		[[unroll]] for (uint element = 0; element < LOAD_WIDTH; ++element) {
			shared_left[shared_offset + element] = left_values[element];
			shared_right[shared_offset + element] = right_values[element];
		}
		GroupMemoryBarrierWithGroupSync();

		[[unroll]] for (uint inner = 0; inner < BK; ++inner) {
			float left_fragment[TM];
			float right_fragment[TN];
			[[unroll]] for (uint row = 0; row < TM; ++row) {
				left_fragment[row] = shared_left[(thread_row * TM + row) * BK_PAD + inner];
			}
			[[unroll]] for (uint column = 0; column < TN; ++column) {
				right_fragment[column] = shared_right[(thread_column * TN + column) * BK_PAD + inner];
			}
			[[unroll]] for (uint row = 0; row < TM; ++row) {
				[[unroll]] for (uint column = 0; column < TN; ++column) {
					accumulators[row * TN + column] += left_fragment[row] * right_fragment[column];
				}
			}
		}
		GroupMemoryBarrierWithGroupSync();
	}

	uint output_base = push.c_offset + batch * push.c_batch_stride;
	[[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_base + output_row * push.c_row_stride
						+ output_column * push.c_col_stride,
					accumulators[row * TN + column]
				);
			}
		}
	}
}