8000 Configure transformer KVCache with LayoutBSHD and SinkPositions=0 for zero-copy decode by janpfeifer · Pull Request #98 · gomlx/go-huggingface · GitHub
[go: up one dir, main page]

Skip to content

Configure transformer KVCache with LayoutBSHD and SinkPositions=0 for zero-copy decode - #98

Merged
janpfeifer merged 2 commits into
mainfrom
optimize-generate-2
Dec 2, 2026
Merged

janpfeifer merged 2 commits into
mainfrom
optimize-generate-2

Conversation

@janpfeifer
Copy link
Copy Markdown
Contributor

Summary

Companion to gomlx/gomlx#510 and gomlx/compute#101.

Configures HuggingFace transformer models in models/transformer/model.go with:

  • WithLayout(attention.LayoutBSHD): Matches MultiHeadAttention's native BSHD projection layout, eliminating full-cache TransposeAllDims(0, 2, 1, 3) on both KV updates and reads for multi-KV-head GQA models (e.g., Gemma 4 E4B, Gemma 2 2B, Qwen3/Qwen3.5).
  • WithSinkPositions(0): Standard HuggingFace causal/sliding-window transformers do not use StreamingLLM attention sinks (SinkPositions=4 was the kvcache.NewKVCache() default), and setting SinkPositions=0 activates zero-copy WithSeqLens decode attention instead of constructing dense (Bool)[1, 1, 1, cacheLen] masks every decode step.

Note: depends on WithLayout added in gomlx/gomlx#510.

Measured Impact (with gomlx/gomlx#510 & gomlx/compute#101)

  • Go CPU backend (Ryzen 9 9950X3D, par=16):
  • Pure-Go CUDA backend (RTX 5090, 9 warm examples, gen=64):
    • Gemma 4 E2B: decode 67.11 → 79.99 tok/s (+19.2%)
    • Gemma 2 2B (kv_heads=4): decode 105.19 → 117.52 tok/s (+11.7%)
    • Qwen3-0.6B (kv_heads=8): decode 128.58 → 182.94 tok/s (+42.3%)

Verification

  • GOEXPERIMENT=simd GOMLX_BACKEND=go go test -count=1 ./models/...
  • GOEXPERIMENT=simd GOMLX_BACKEND=go go test -v -timeout=15m ./modeltests -modeltests -tag=generate -warmup=1 -cached_only passed with full parity (gemma4-e4b: 271/274 exact top-1; qwen3.5-4b: 287/288 exact top-1).

@janpfeifer
janpfeifer merged commit 9336f0b into main Dec 2, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

0