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:
copyDst: Can receive data from CPUcopySrc: Can send data to CPUstorage: Can be read/written by compute shadersuniform: Can be used as uniform buffer
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:
- Submit compute commands → Returns immediately
- Use
queue.onSubmittedWorkDone()to wait - Map buffer → Wait for GPU → Read data → Unmap
References #
- WebGPU Spec: https://www.w3.org/TR/webgpu/
- Buffer mapping: https://gpuweb.github.io/gpuweb/#buffer-mapping
Data Type Conversion #
Convert Float32 to bytes (little-endian)
@param f Float32 value @return 4 bytes representing the float
Instances For
Alias for backward compatibility
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 ByteArray to Array Float
@param bytes ByteArray (must be multiple of 4) @return Array of Float32 values
Equations
- Hesper.WebGPU.BufferOps.bytesToFloatArray bytes = Hesper.WebGPU.BufferOps.bytesToFloatArray.loop bytes (bytes.size / 4) 0 #[]
Instances For
Convert Array Nat (token IDs) to ByteArray (UInt32 format)
@param arr Array of token IDs @return Packed ByteArray
Equations
- Hesper.WebGPU.BufferOps.tokenArrayToBytes arr = Array.foldl (fun (bytes : ByteArray) (n : Nat) => bytes ++ Hesper.WebGPU.BufferOps.uint32ToBytes n.toUInt32) ByteArray.empty arr
Instances For
Convert ByteArray to Array Nat (token IDs)
@param bytes ByteArray (must be multiple of 4) @return Array of token IDs
Equations
- Hesper.WebGPU.BufferOps.bytesToTokenArray bytes = Hesper.WebGPU.BufferOps.bytesToTokenArray.loop bytes (bytes.size / 4) 0 #[]
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
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
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 #
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
- Hesper.WebGPU.BufferOps.printBufferInfo buffer name = do Hesper.Logging.logVerbose (toString "[Buffer] " ++ toString name) pure ()