Documentation

Hesper.WGSL.Kernel

Composable kernel abstraction for kernel fusion.

This module provides a high-level abstraction for composable GPU kernels. Instead of writing imperative procedures, we treat kernels as composable functions (Input → Output), enabling kernel fusion optimizations.

Key benefits:

Example: -- Fuse: Load → Multiply → Add → ReLU → Store into single pass fusedKernel : Kernel 256 1 1 (Exp (.scalar .u32)) Unit fusedKernel = loadVec inputPtr |> andThen (mapK (· * Exp.litF32 2.0)) |> andThen (mapK (· + Exp.litF32 1.0)) |> andThen (mapK relu) |> andThen (storeVec outputPtr)

@[reducible, inline]

ShaderM monad - builds up statement lists while computing a value. This is a state monad that accumulates statements.

Equations
Instances For
    structure Hesper.WGSL.Kernel (wX wY wZ : Nat) (i o : Type) :

    A composable kernel function running on a specific workgroup size.

    Type parameters: wX, wY, wZ: Workgroup dimensions (natural numbers) i: Input type (e.g., Exp (.scalar .f32), or tuples) o: Output type

    This abstraction allows us to compose operations and perform kernel fusion. Multiple operations can be fused into a single shader pass, reducing global memory roundtrips.

    Instances For
      def Hesper.WGSL.Kernel.id {wX wY wZ : Nat} {α : Type} :
      Kernel wX wY wZ α α

      Identity kernel: passes input through unchanged

      Equations
      Instances For
        def Hesper.WGSL.Kernel.comp {wX wY wZ : Nat} {a b c : Type} (g : Kernel wX wY wZ b c) (f : Kernel wX wY wZ a b) :
        Kernel wX wY wZ a c

        Compose two kernels: g ∘ f means "f first, then g" The composition is performed in the ShaderM monad, so side effects (like memory operations) are properly sequenced.

        Equations
        Instances For

          Infix operator for kernel composition (like >>> in Haskell Category)

          Equations
          • One or more equations did not get rendered due to their size.
          Instances For
            def Hesper.WGSL.Kernel.andThen {wX wY wZ : Nat} {a b : Type} (k : Kernel wX wY wZ a b) (f : bKernel wX wY wZ b b) :
            Kernel wX wY wZ a b

            Monadic bind for kernels

            Equations
            Instances For
              def Hesper.WGSL.mapK {wX wY wZ : Nat} {ty : WGSLType} (f : Exp tyExp ty) :
              Kernel wX wY wZ (Exp ty) (Exp ty)

              Lift a pure DSL expression transformation into a Kernel.

              This allows you to turn any pure expression transformation (like (· * 2.0) or (· + 1.0)) into a composable kernel.

              Example: mapK (· * Exp.litF32 2.0) -- Multiply by 2 mapK (· + Exp.litF32 1.0) -- Add 1 mapK relu -- Apply ReLU

              These can be composed: mapK (· * 2.0) |> mapK (· + 1.0) |> mapK relu

              Equations
              Instances For
                def Hesper.WGSL.emit {wX wY wZ : Nat} (s : Stmt) :
                Kernel wX wY wZ Unit Unit

                Emit a statement (side effect) in a kernel. This adds the statement to the accumulated list.

                Equations
                Instances For
                  def Hesper.WGSL.emitMany {wX wY wZ : Nat} (stmts : List Stmt) :
                  Kernel wX wY wZ Unit Unit

                  Emit multiple statements

                  Equations
                  Instances For
                    def Hesper.WGSL.loadBuffer {wX wY wZ : Nat} {ty : WGSLType} {n : Nat} (bufferName : String) :

                    Load operation: reads from a buffer at given index.

                    Input: index expression Output: loaded value

                    Note: The buffer must be an array type. The size parameter n is for type checking.

                    Equations
                    • One or more equations did not get rendered due to their size.
                    Instances For
                      def Hesper.WGSL.storeBuffer {wX wY wZ : Nat} {ty : WGSLType} (bufferName : String) :

                      Store operation: writes to a buffer at given index.

                      Input: (index, value) pair Output: unit (side effect only)

                      Equations
                      • One or more equations did not get rendered due to their size.
                      Instances For

                        Pair an expression with an index for storage

                        Equations
                        Instances For
                          def Hesper.WGSL.runKernel {wX wY wZ : Nat} {i o : Type} (k : Kernel wX wY wZ i o) (input : i) :

                          Execute a kernel and extract the generated statements.

                          This runs the kernel computation and returns both the result value and the list of statements that were generated.

                          Equations
                          Instances For
                            def Hesper.WGSL.execKernel {wX wY wZ : Nat} {i o : Type} (k : Kernel wX wY wZ i o) (input : i) :

                            Execute a kernel and return only the statements (for side-effect kernels)

                            Equations
                            Instances For