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.
- funcName : String
- workgroupSize : WGSL.WorkgroupSize
Instances For
Typeclass for GPU compute backends.
- Buf : Type
- CachedDispatch : Type
- CompiledKernel : Type
- executeWithConfig : β → WGSL.Monad.ShaderM Unit → List (String × Buf β) → ExecConfig → IO Unit
- executeWithConfigCached : β → WGSL.Monad.ShaderM Unit → List (String × Buf β) → ExecConfig → UInt64 → IO.Ref (Option (CachedDispatch β)) → IO Unit
- executeWithConfigCachedArrays : β → WGSL.Monad.ShaderM Unit → List (String × Buf β) → List (String × List (Buf β)) → ExecConfig → UInt64 → IO.Ref (Option (CachedDispatch β)) → IO Unit
Variant that accepts both single-buffer bindings and buffer-array bindings (for cross-layer kernel fusion). Each entry in
namedBufferArraysmaps a declaredbufferArrayname to the N underlying buffers; the backend materialises a device-side pointer table and passes its pointer as the kernel argument. Default impl: falls back to the single-buffer path if there are no array bindings. Stable identity of a buffer for tracing (JS-replay provenance); 0 = backend has no notion of identity.
- buildKernel : β → WGSL.Monad.ShaderM Unit → ExecConfig → IO (CompiledKernel β)
- newCacheRef : IO (IO.Ref (Option (CachedDispatch β)))
Raw device pointer for a buffer, if the backend has a native address space exposable as a USize. CUDA returns
some(CUdeviceptr), WebGPU returnsnone. Used by Phase-0 hybrid path that calls externally-JIT'd PTX directly. Must not be used by portable code.Wrap an already-device-resident pointer (e.g. from
cuMemHostGetDevicePointeror an externally-allocated CUdeviceptr) as aBufwithout copying. CUDA returnssome buf; WebGPU returnsnone(no native pointer abstraction). The pointer must remain valid for the buffer's lifetime; the backend will NOT free it (callers handle deregistration externally).Begin recording dispatches for batch submission. CUDA: no-op.
Submit all recorded dispatches and wait. CUDA: no-op (sync is per-launch).
Instances
Convenience: execute with ExecConfig
Equations
- Hesper.GPUBackend.execute ctx computation namedBuffers config = Hesper.GPUBackend.executeWithConfig ctx computation namedBuffers config
Instances For
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.
- computation : WGSL.Monad.ShaderM Unit
- cacheKey : UInt64
- cacheRef : IO.Ref (Option (GPUBackend.CachedDispatch β))
Instances For
Create a cached kernel. Call once at init time (e.g., in createInferenceState).
Equations
Instances For
Dispatch with caching. First call compiles; subsequent calls skip PTX generation.
Equations
- k.exec ctx namedBuffers config = Hesper.GPUBackend.executeWithConfigCached ctx k.computation namedBuffers config k.cacheKey k.cacheRef
Instances For
For debug/test use only. Creates an ephemeral cache (not reused).
Equations
- Hesper.GPUBackend.debugExecuteOnce ctx computation namedBuffers config = do let ref ← liftM (IO.mkRef none) Hesper.GPUBackend.executeWithConfigCached ctx computation namedBuffers config 0 ref
Instances For
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
Equations
Equations
- Hesper.instBEqBackendChoice.beq x✝ y✝ = (x✝.ctorIdx == y✝.ctorIdx)
Instances For
Equations
- One or more equations did not get rendered due to their size.
- Hesper.instReprBackendChoice.repr Hesper.BackendChoice.CUDA prec✝ = Repr.addAppParen (Std.Format.nest (if prec✝ ≥ 1024 then 1 else 2) (Std.Format.text "Hesper.BackendChoice.CUDA")).group prec✝
Instances For
Equations
- Hesper.instReprBackendChoice = { reprPrec := Hesper.instReprBackendChoice.repr }
Detect backend from HESPER_BACKEND env var. Default: WebGPU.
Equations
- One or more equations did not get rendered due to their size.