8000 Transformer & generation optimizations: Gemma 4 support, zero-copy WithSeqLens KV-cache prefill/decode, OnlyLastTokenLogits, and WithDonateStore by janpfeifer · Pull Request #501 · gomlx/gomlx · GitHub
[go: up one dir, main page]

Skip to content

Transformer & generation optimizations: Gemma 4 support, zero-copy WithSeqLens KV-cache prefill/decode, OnlyLastTokenLogits, and WithDonateStore - #501

Merged
janpfeifer merged 8 commits into
mainfrom
demo-gen-ai
Nov 24, 2026

Conversation

@janpfeifer
Copy link
Copy Markdown
Contributor

Summary

Transformer inference, KV-cache, and Gemma 4 improvements in gomlx (companion to the compute and go-huggingface demo-gen-ai PRs):

  • Gemma 4 Architecture Support:
    • Added use_double_wide_mlp support for KV-shared layers in ml/zoo/transformer.
    • Added proportional partial RoPE (pos.WithPartialRotaryFactor) and Float32 RoPE angle computation for half-precision (Float16 / BFloat16) accuracy (ml/layers/attention/pos).
  • Zero-Copy KV-Cache Prefill & Decode (WithSeqLens):
    • Implemented zero-copy KV-cache decode and causal prefill via WithSeqLens (kvLimit), avoiding full-cache buffer copies/slices during autoregressive generation, and fixed an off-by-one bug in PadTensors.
    • Switched prefill attention masks to head-invariant masks and dynamic promptExec to avoid per-length recompilation and unnecessary mask broadcasts.
  • Prefill & Memory Optimizations:
    • Added CallOptions.OnlyLastTokenLogits to skip unused prompt lm_head projections during prefill (only computing logits for the final prompt token).
    • Added Generator.WithDonateStore (ml/zoo/transformer/generate) to donate model weights to shared backend constants across prefill and decode graphs without duplicating memory.
  • Examples:
    • Added MaxRSS printout at the end of examples/FlowMatching/demo/main.go.

Verification

  • Added/updated unit tests in ml/layers/attention/pos/rope_test.go and ml/zoo/transformer/transformer_test.go.
  • Verified end-to-end token-for-token exact-match generation on google/gemma-4-E2B-it (BFloat16) against Ollama (gemma4:e2b-it-bf16).

@janpfeifer
janpfeifer merged commit 41a32a0 into main Nov 24, 2026
4 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