Open
Conversation
…oogle/gemma-4-31b-it for triattention calibration and generation commands.
thegodone
pushed a commit
to guillaume-osmo/mlx-lm
that referenced
this pull request
Apr 10, 2026
…#985 Add trigonometric-series-based token pruning for KV cache compression, from "TriAttention: Efficient Long Reasoning with Trigonometric KV Compression" (Lin et al., 2026, arXiv:2604.04921). Scores post-RoPE key importance using calibrated Q-center statistics and evicts low-scoring tokens when cache exceeds budget. No inverse RoPE needed — position terms cancel in the phase difference. New files: - mlx_lm/models/triattention.py: core scoring + TriAttentionKVCache - mlx_lm/triattention_calibrate.py: offline Q-center calibration CLI Usage: python -m mlx_lm.triattention_calibrate --model <m> --output calib.safetensors python -m mlx_lm.generate --model <m> --triattention-calib calib.safetensors \ --triattention-budget 512 Tested on SmolLM3-3B: exact match at budget>=seq_len, coherent output with 22% memory reduction at aggressive budget=200. Ported from Blaizzy/mlx-vlm#985 (Blaizzy). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Q/K centers are model-intrinsic properties that converge from very few tokens (paper Appendix H). Online mode computes calibration from prefill tokens automatically — no separate calibration file needed. Usage: just pass --triattention-budget 512 (no --triattention-calib). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
triattention_calibrate.py) and drop-inTriAttentionKVCachethat integrates with the existing generation pipeline via--triattention-caliband--triattention-budgetCLI argsKey design decisions
a·cos_tw - b·sin_twdecomposition enables matrix multiply over 17 log-spaced offsets instead of naive loopKVCachelayers are compressed;RotatingKVCache(Gemma4 sliding window) left untouchedfrom_cache, self-contained compression insideupdate_and_fetchHow It Works
Quick Start
Step 1: Calibrate (one-time per model, takes ~30s)
Step 2: Generate with compression
mlx_vlm generate \ --model google/gemma-4-31b-it \ --triattention-calib gemma4_calib.safetensors \ --triattention-budget 512 \ --prompt "Your prompt here..." \ --max-tokens 2048Benchmarks
Code: https://gist.github.com/Blaizzy/008df4f0a2f6df88db6f36569f06ea25
MATH 500 (30 problems, Gemma4-26B-A4B 5-bit, max_tokens=4096):
MM-NIAH (Gemma4-31B, multimodal needle-in-a-haystack, 1K–60K tokens):
Files changed
mlx_vlm/triattention.py— Core: RoPEConfig, scoring, TriAttentionKVCache, calibration I/Omlx_vlm/triattention_calibrate.py— Calibration script with CaptureWrapper hooksmlx_vlm/generate.py— CLI args +maybe_apply_triattentionintegrationmlx_vlm/models/cache.py— Re-export TriAttentionKVCacheREADME.md— Documentation with quick start, benchmarks, compatibilityTest plan
python -m mlx_vlm.triattention_calibrate --model <model> --output calib.safetensorsmlx_vlm generate --model <model> --triattention-calib calib.safetensors --triattention-budget 512 --prompt "..." --max-tokens 2048