Documentation

Hesper.WGSL.Helpers

WGSL DSL Helpers for Common Patterns #

Provides high-level helpers for common GPU operations:

Matrix Operations #

def Hesper.WGSL.Helpers.matVecMulBiasActivation (inputName weightsName biasName outputName : String) (inputSize outputSize : Nat) (activation : Exp (WGSLType.scalar ScalarType.f32)Exp (WGSLType.scalar ScalarType.f32)) (outputIdx : Exp (WGSLType.scalar ScalarType.u32)) :

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

    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 #

      def Hesper.WGSL.Helpers.generateMatVecBiasActivationShader (inputSize outputSize : Nat) (activation : Exp (WGSLType.scalar ScalarType.f32)Exp (WGSLType.scalar ScalarType.f32)) (workgroupSizeX : Nat := 256) (workgroupSizeY workgroupSizeZ : Nat := 1) :

      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
        def Hesper.WGSL.Helpers.generateSoftmaxShader (size : Nat) (workgroupSizeX workgroupSizeY workgroupSizeZ : Nat := 1) :

        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.
        Instances For