Documentation

Hesper.Backend

GPU Backend Abstraction #

Typeclass-based backend abstraction. Same ShaderM kernels run on WebGPU or CUDA — selected by HESPER_BACKEND env var at runtime.

Design #

GPUBackend β provides all GPU operations parameterized by context type β.

Layer/model code is written as:

def myLayer [GPUBackend β] (ctx : β) (input output : GPUBackend.Buf β) : IO Unit := do
  GPUBackend.execute ctx myKernel [("input", input), ("output", output)]
    (GPUBackend.dispatch1D 1024)

Execution config — backend-agnostic. extensions/diagnostics are used by WebGPU (e.g., chromium_experimental_subgroup_matrix) and ignored by CUDA.

Instances For
    def Hesper.ExecConfig.dispatch1D (n : Nat) (wgSize : Nat := 256) :
    Equations
    Instances For
      def Hesper.ExecConfig.dispatch2D (nx ny : Nat) (bx by_ : Nat := 16) :
      Equations
      Instances For
        class Hesper.GPUBackend (β : Type) :

        Typeclass for GPU compute backends.

        Instances
          @[inline]
          def Hesper.GPUBackend.execute {β : Type} [GPUBackend β] (ctx : β) (computation : WGSL.Monad.ShaderM Unit) (namedBuffers : List (String × Buf β)) (config : ExecConfig) :

          Convenience: execute with ExecConfig

          Equations
          Instances For
            structure Hesper.CachedKernel (β : Type) [GPUBackend β] :

            A kernel bundled with its dispatch cache. Production code should use this instead of bare GPUBackend.execute to ensure dispatch caching. Create with CachedKernel.create, dispatch with CachedKernel.exec.

            Instances For

              Create a cached kernel. Call once at init time (e.g., in createInferenceState).

              Equations
              Instances For
                @[inline]
                def Hesper.CachedKernel.exec {β : Type} [GPUBackend β] (k : CachedKernel β) (ctx : β) (namedBuffers : List (String × GPUBackend.Buf β)) (config : ExecConfig) :

                Dispatch with caching. First call compiles; subsequent calls skip PTX generation.

                Equations
                Instances For
                  def Hesper.GPUBackend.debugExecuteOnce {β : Type} [GPUBackend β] (ctx : β) (computation : WGSL.Monad.ShaderM Unit) (namedBuffers : List (String × Buf β)) (config : ExecConfig) :

                  For debug/test use only. Creates an ephemeral cache (not reused).

                  Equations
                  Instances For
                    def Hesper.smartDispatch (totalThreads : Nat) (wgSize : Nat := 256) :

                    Smart dispatch: 1D if fits, 2D otherwise. Returns (config, gridDimX) — same signature as TTT.Kernels.smartDispatch.

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

                      Which backend to use

                      Instances For
                        Equations
                        Instances For

                          Detect backend from HESPER_BACKEND env var. Default: WebGPU.

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