Skip to main content

GEMM_SUBSTRATE_WGSL

Constant GEMM_SUBSTRATE_WGSL 

Source
pub const GEMM_SUBSTRATE_WGSL: &str = "// Substrate GEMM \u{2014} C = A \u{b7} B, all row-major f32.\n// A is m\u{d7}k, B is k\u{d7}n, C is m\u{d7}n. One invocation computes one output element.\n// This is the portable wgpu compute kernel the STEM substrate dispatches to when the\n// measured capability matrix says the GPU wins (correctness-gated against the CPU\n// reference). Deliberately simple (one thread per output, no tiling) \u{2014} a correct first\n// kernel; shared-memory tiling is a later optimization, not a correctness concern.\n\nstruct Dims {\n    m: u32,\n    k: u32,\n    n: u32,\n    _pad: u32,\n};\n\n@group(0) @binding(0) var<storage, read>       a: array<f32>;\n@group(0) @binding(1) var<storage, read>       b: array<f32>;\n@group(0) @binding(2) var<uniform>             d: Dims;\n@group(0) @binding(3) var<storage, read_write> c: array<f32>;\n\n@compute @workgroup_size(8, 8, 1)\nfn gemm(@builtin(global_invocation_id) gid: vec3<u32>) {\n    let row = gid.x;\n    let col = gid.y;\n    if (row >= d.m || col >= d.n) {\n        return;\n    }\n    var acc: f32 = 0.0;\n    for (var l: u32 = 0u; l < d.k; l = l + 1u) {\n        acc = acc + a[row * d.k + l] * b[l * d.n + col];\n    }\n    c[row * d.n + col] = acc;\n}\n";
Expand description

Canonical GEMM substrate compute shader.