Documentation

Hesper.Core.VerifiedOpFusion

Verified Operator Pattern with Kernel Fusion #

This module extends the VerifiedOp pattern to support kernel fusion.

Key Design Evolution #

Before (Immediate Execution - No Fusion): #

let tmp ← MatMul.impl_forward A B   -- Writes to VRAM
let res ← ReLU.impl_forward tmp     -- Reads from VRAM, writes again
-- Problem: 2 memory roundtrips!

After (Lazy Composition - Fusion Enabled): #

let fused = MatMul.impl_kernel |> ReLU.impl_kernel  -- Build recipe
let res ← run_kernel fused input                     -- Single GPU dispatch!
-- Benefit: 1 memory roundtrip, fused shader

Type Parameters #

The separation allows:

  1. CPU spec works on convenient high-level types
  2. GPU impl works on low-level WGSL expressions
  3. Kernels can be composed before execution

Workgroup Size #

Kernels are parameterized by workgroup dimensions (wX, wY, wZ). This is tracked at the type level for safety.

Data Abstractions #

CPU tensor data: wrapper around Array Float with shape information. Used for specification and testing.

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

      Create a zero tensor with given shape

      Equations
      Instances For

        Create a tensor filled with a constant value

        Equations
        Instances For

          Get element at flattened index

          Equations
          Instances For

            Set element at flattened index

            Equations
            Instances For

              Total number of elements

              Equations
              Instances For
                def Hesper.Core.TensorData.approxEq (a b : TensorData) (tolerance : Float := 1e-5) :

                Check if two tensors are approximately equal within tolerance

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

                  GPU tensor handle: wrapper around WebGPU Buffer with shape and device. Used for high-performance GPU operations.

                  Instances For

                    Upload CPU tensor data to GPU

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

                      Download GPU tensor data to CPU

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

                        Create a zero tensor on GPU

                        Equations
                        Instances For

                          Fusable Verified Operator Type Class #

                          class Hesper.Core.VerifiedOpFusion (wX wY wZ : Nat) (I O WI WO : Type) :

                          Type class for verified operators with kernel fusion support.

                          Type parameters:

                          • I, O: High-level CPU types (e.g., Matrix, TensorData)
                          • WI, WO: WGSL expression types (e.g., Exp (.array .f32 N))
                          • wX, wY, wZ: Workgroup dimensions (compile-time constants)

                          Design philosophy:

                          1. Specification (CPU): Pure mathematical definition for correctness
                          2. Implementation (GPU): Composable kernel for performance
                          3. Fusion: Kernels compose via |> before execution
                          4. Verification: Compare spec vs impl for correctness

                          Example usage:

                          -- Fuse two operators
                          let fused = MatMul.impl_kernel |> ReLU.impl_kernel
                          
                          -- Execute fused kernel
                          let result ← execute_kernel device fused input
                          
                          -- Verify correctness
                          let cpu_result := MatMul.spec_forward input |> ReLU.spec_forward
                          assert (cpu_result ≈ result)
                          
                          • spec_forward : IO

                            Specification: Pure mathematical definition (CPU). Reference implementation for correctness and formal verification.

                            This should be:

                            • Easy to understand and verify
                            • Provably correct
                            • Used as the "ground truth" for testing
                          • impl_kernel : WGSL.Kernel wX wY wZ WI WO

                            Implementation: Composable GPU kernel. Returns a Kernel that can be fused with other kernels.

                            This should be:

                            • Lazy (doesn't execute immediately)
                            • Composable (can be chained with |>)
                            • Optimized for GPU performance

                            Example:

                            impl_kernel : Kernel 256 1 1 (Exp (.array .f32 N)) (Exp (.array .f32 N))
                            
                          • spec_backward : IOI

                            Backward Specification: CPU gradient computation. Given (input, grad_output), compute grad_input.

                            Used for training and automatic differentiation.

                          • impl_kernel_backward : WGSL.Kernel wX wY wZ (WI × WO) WI

                            Backward Implementation: GPU gradient kernel. Composable kernel for backpropagation.

                            Can be fused with other backward passes.

                          • run_forward : WIIO WO

                            Execution Helper: Optional wrapper for immediate execution. Compiles and runs the kernel immediately (for testing/debugging).

                            Default implementation provided. Can be overridden per operator.

                          • verify_consistency : IFloatIO Bool

                            Verification: Check CPU spec matches GPU impl. Requires conversion between high-level (I/O) and low-level (WI/WO) types.

                            Default implementation is a placeholder.

                          Instances
                            instance Hesper.Core.instDifferentiableVerifiedOpFusion {wX wY wZ : Nat} {I O WI WO : Type} [inst : VerifiedOpFusion wX wY wZ I O WI WO] :
                            Differentiable (VerifiedOpFusion wX wY wZ I O WI WO) I O

                            Linking VerifiedOpFusion to the Unified Differentiable Interface

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

                            Helper Functions for Fusion #

                            def Hesper.Core.composeOps {wX wY wZ : Nat} {I M O WI WM WO : Type} [inst1 : VerifiedOpFusion wX wY wZ I M WI WM] [inst2 : VerifiedOpFusion wX wY wZ M O WM WO] :
                            WGSL.Kernel wX wY wZ WI WO

                            Compose two verified operators into a fused operation. The result is a new kernel that applies f then g in a single pass.

                            Example:

                            let matmul_relu = composeOps (I := MatMulInput) (M := MatMulOutput) (O := ReLUOutput)
                            -- Single GPU kernel that does matmul AND relu!
                            
                            Equations
                            Instances For
                              def Hesper.Core.mkElementwiseOp {wX wY wZ : Nat} {ty : WGSL.WGSLType} (gpu_fn : WGSL.Exp tyWGSL.Exp ty) :
                              WGSL.Kernel wX wY wZ (WGSL.Exp ty) (WGSL.Exp ty)

                              Create a simple element-wise operation verified operator.

                              This is a helper for defining simple pointwise operations like ReLU, sigmoid, etc.

                              Parameters:

                              • gpu_fn: WGSL expression transformation (Exp ty → Exp ty)
                              Equations
                              Instances For