WGSL DSL Helpers for Common Patterns #
Provides high-level helpers for common GPU operations:
- Matrix-vector multiplication
- Element-wise operations with activation functions
- Reductions (sum, max, softmax)
- Fused operations (MatMul + Bias + Activation)
Matrix Operations #
Matrix-vector multiplication with bias and activation (fused Layer operation)
Computes: output[i] = activation(sum_j(weights[j, i] * input[j]) + bias[i])
Parameters:
- inputName: name of input buffer
- weightsName: name of weights buffer
- biasName: name of bias buffer
- outputName: name of output buffer
- inputSize: number of input features
- outputSize: number of output features
- activation: activation function to apply (or identity)
- outputIdx: expression for output index (usually from global_invocation_id)
Equations
- One or more equations did not get rendered due to their size.
Instances For
ReLU activation: max(0, x)
Equations
Instances For
Identity activation: x
Equations
Instances For
Tanh activation (using WGSL built-in)
Equations
Instances For
Softmax Operation #
Softmax normalization (single-workgroup version for small arrays)
Computes: output[i] = exp(input[i] - max) / sum(exp(input - max))
Note: This is a simplified version for small arrays that fit in one workgroup. For production use with large arrays, use multi-pass reduction.
Parameters:
- dataName: name of buffer (input/output in-place)
- size: number of elements
Equations
- One or more equations did not get rendered due to their size.
Instances For
Shader Generation Helpers #
Generate complete compute shader for matrix-vector multiply + bias + activation
Generates a WGSL compute shader with proper bindings for:
- @binding(0): input array [inputSize]
- @binding(1): weights array [inputSize * outputSize]
- @binding(2): bias array [outputSize]
- @binding(3): output array [outputSize]
The shader fuses MatMul + Bias + Activation into a single kernel.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Generate complete compute shader for softmax (in-place)
Generates a WGSL compute shader for softmax normalization:
- @binding(0): data array [size] (read_write)
Note: Uses single workgroup, suitable for small arrays (size ≤ 1024)
Equations
- One or more equations did not get rendered due to their size.