Documentation

Hesper.WebGPU.BufferOps

GPU Buffer Operations #

Utilities for transferring data between CPU and GPU.

Memory Model #

WebGPU uses explicit memory transfers:

CPU Memory              GPU Memory
┌─────────┐            ┌──────────┐
│ByteArray│  ────────► │  Buffer  │  writeBuffer (CPU → GPU)
└─────────┘            └──────────┘
                            │
                            ▼
                       [Compute Shader]
                            │
                            ▼
┌─────────┐            ┌──────────┐
│ByteArray│  ◄──────── │  Buffer  │  readBuffer  (GPU → CPU)
└─────────┘            └──────────┘

Buffer Usage Flags #

Buffers must be created with appropriate usage flags:

Example:

-- For model weights (upload once)
usage := [.storage, .copyDst]

-- For output logits (download to CPU)
usage := [.storage, .copySrc]

-- For input tokens (upload + read in shader)
usage := [.storage, .copyDst]

Asynchronous Operations #

WebGPU operations are asynchronous:

  1. Submit compute commands → Returns immediately
  2. Use queue.onSubmittedWorkDone() to wait
  3. Map buffer → Wait for GPU → Read data → Unmap

References #

Data Type Conversion #

Convert Float32 to bytes (little-endian)

@param f Float32 value @return 4 bytes representing the float

Equations
Instances For

    Convert bytes to Float32 (little-endian)

    @param bytes ByteArray (must have at least offset+4 bytes) @param offset Start offset @return Float32 value

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

      Convert UInt32 to bytes (little-endian)

      @param n UInt32 value @return 4 bytes

      Equations
      Instances For

        Convert bytes to UInt32 (little-endian)

        @param bytes ByteArray @param offset Start offset @return UInt32 value

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

          Array Conversions #

          Convert Array Float to ByteArray (Float32 format)

          @param arr Array of floats @return Packed ByteArray

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

            Convert ByteArray to Array Float

            @param bytes ByteArray (must be multiple of 4) @return Array of Float32 values

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

                Convert Array Nat (token IDs) to ByteArray (UInt32 format)

                @param arr Array of token IDs @return Packed ByteArray

                Equations
                Instances For

                  Convert ByteArray to Array Nat (token IDs)

                  @param bytes ByteArray (must be multiple of 4) @return Array of token IDs

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

                      GPU Upload/Download #

                      Upload Float32 array to GPU buffer

                      Creates a new buffer and uploads data.

                      @param device WebGPU device @param data Array of Float32 values @return GPU buffer containing the data

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

                        Upload token IDs to GPU buffer

                        Creates a new buffer and uploads token data.

                        @param device WebGPU device @param tokens Array of token IDs @return GPU buffer containing the tokens

                        Equations
                        • One or more equations did not get rendered due to their size.
                        Instances For
                          def Hesper.WebGPU.BufferOps.downloadFloatArray (device : Device) (buffer : Buffer) (numElements : Nat) :

                          Download Float32 array from GPU buffer

                          Reads data from GPU buffer back to CPU.

                          @param device WebGPU device @param buffer GPU buffer to read from @param numElements Number of Float32 elements to read @return Array of Float32 values

                          Equations
                          • One or more equations did not get rendered due to their size.
                          Instances For
                            def Hesper.WebGPU.BufferOps.downloadLastLogits (device : Device) (logitsBuffer : Buffer) (batchSize seqLen vocabSize : Nat) :

                            Download logits and extract last position

                            Helper function for text generation: downloads logits from GPU and extracts the logits for the last token position.

                            @param device WebGPU device @param logitsBuffer GPU buffer containing logits [batch, seq_len, vocab_size] @param batchSize Batch size @param seqLen Sequence length @param vocabSize Vocabulary size @return Logits for last position [vocab_size]

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

                              Utilities #

                              def Hesper.WebGPU.BufferOps.createLogitsBuffer (device : Device) (batchSize seqLen vocabSize : Nat) :

                              Create buffer for model outputs (logits)

                              @param device WebGPU device @param batchSize Batch size @param seqLen Sequence length @param vocabSize Vocabulary size @return Buffer sized for logits

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

                                Print buffer info for debugging

                                @param buffer GPU buffer @param name Descriptive name

                                Equations
                                Instances For