Documentation
¶
Index ¶
Constants ¶
const ChunkSize = 16 << 20
ChunkSize is the size of one weight upload: tensors are read from the model file and sent to the GPU in pieces of at most this many bytes, so the whole model never sits in guest memory.
const PieceSize = 4 << 20
PieceSize is the size of one read from the model file while loading.
const PrefillChunk = 8
PrefillChunk is the number of prompt tokens recorded per submit: a submit must stay far below the host's 2 s limit.
Variables ¶
This section is empty.
Functions ¶
func Argmax ¶
Argmax returns the index of the largest logit (the first on ties, as llama.cpp's greedy sampler does).
Types ¶
type CPU ¶
type CPU struct {
Cfg Config
ExactGGML bool
// MaxLayers, when non-zero, runs only that many layers and returns nil
// (debugging); LastX is the residual stream after the layers run.
MaxLayers int
LastX []float32
// contains filtered or unexported fields
}
CPU is the forward pass in Go: the equality oracle for the WGSL kernels and the native fallback. It holds every weight in memory.
ExactGGML reproduces llama.cpp's CPU backend: activations are quantised to Q8_0 (Q8_1 for Q4_1 weights) before every quantised matrix product and the KV cache is stored in binary16. Without it the products use f32 activations and an f32 cache, which is what the GPU kernels compute.
type Config ¶
type Config struct {
Layers, Embd, FF, Heads, KVHeads, HeadDim, Vocab, Ctx int
RopeBase, Eps float32
}
Config is the llama-architecture hyperparameters read from GGUF metadata (llama.* keys; gguf.md "LLM" section).
type GPU ¶
type GPU struct {
Cfg Config
Stats Stats
// Layers, when non-zero, runs only that many layers (debugging; no
// logits and no sampling are then produced).
Layers int
// contains filtered or unexported fields
}
GPU is the forward pass on the GPU for the llama architecture, in f32.
func NewGPU ¶
NewGPU creates the kernels, allocates every buffer and streams the weights of the model to the GPU. ctx is the KV cache length in tokens.
func (*GPU) Generate ¶
Generate decodes greedily: the prompt in submits of PrefillChunk tokens, then one submit per token whose only read-back is the 4-byte sampled token (argmax on the GPU). emit is called with each generated token; Generate stops after n tokens, at the end-of-sequence token (which is not emitted) or when emit returns false.
func (*GPU) MemoryUsed ¶
MemoryUsed is the GPU memory the model, its cache and its scratch hold.
type Limits ¶
type Limits struct {
MaxInvocationsPerWorkgroup uint32
MaxWorkgroupStorage uint32 // bytes
MaxWorkgroupsPerDimension uint32
MaxStorageBuffers uint32 // per shader stage
MaxBindingSize uint64 // maxStorageBufferBindingSize
MaxBufferSize uint64
MemoryBudget uint64 // GPU memory the container may hold (capset max_memory)
}
Limits are the adapter limits the kernels adapt to (the capset's DeviceLimits, go-virtio/gpu wgpubatch). Nothing in the kernels assumes a vendor's values: the workgroup size and the grids are derived from these.
func DefaultLimits ¶
func DefaultLimits() Limits
DefaultLimits are WebGPU's defaults (the guaranteed minimum).
func (Limits) WorkgroupSize ¶
WorkgroupSize is the workgroup size of the reductions: 64 (a size every WebGPU adapter supports, a multiple of the 32-wide and 64-wide waves of current hardware but not relying on either), reduced when the adapter's invocation or workgroup-memory limit is lower. A power of two.
type Timing ¶
type Timing struct {
Prefill, Decode time.Duration
PromptTokens int
Generated int
Submits int
Dispatches int
// Decode time split: the guest recording the pass (Go, in wasm), and
// Submit (the crossing, the replay, the GPU, the read-back).
Record, Submit time.Duration
}
Timing of a generation.
type Tokenizer ¶
Tokenizer is the byte-level BPE ("gpt2") tokenizer stored in GGUF metadata (tokenizer.ggml.tokens, .merges, .token_type), with the "smollm" pre-tokenizer of llama.cpp's llama-vocab.cpp: digits split one by one, then the GPT-2 pattern 's|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S).
func NewTokenizer ¶
NewTokenizer builds the tokenizer from a parsed header.
func (*Tokenizer) Decode ¶
Decode returns the text of tokens. Special tokens are written as they are.
func (*Tokenizer) Encode ¶
Encode tokenizes text. Special tokens written in the text are matched as such (llama.cpp's parse_special).
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
wgsldump
command
Command wgsldump prints the WGSL kernels as JSON ({"name": "source"}), for validation in a browser (go-wasmcontainers/gpu e2e TestLLMShaders).
|
Command wgsldump prints the WGSL kernels as JSON ({"name": "source"}), for validation in a browser (go-wasmcontainers/gpu e2e TestLLMShaders). |
|
examples
|
|
|
generate
command
Command generate is a WASI container that generates text with a GGUF llama model on the GPU, through the batched command stream (wasi_webgpu_batch).
|
Command generate is a WASI container that generates text with a GGUF llama model on the GPU, through the batched command stream (wasi_webgpu_batch). |
|
Package gguf parses the header of a GGUF file (ggml docs/gguf.md, version 3): metadata key/values and tensor infos.
|
Package gguf parses the header of a GGUF file (ggml docs/gguf.md, version 3): metadata key/values and tensor infos. |