<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <id>https://veloxquant-mlx.netlify.app/docs/blog</id>
    <title>VeloxQuant-MLX Blog</title>
    <updated>2026-08-26T00:00:00.000Z</updated>
    <generator>https://github.com/jpmonette/feed</generator>
    <link rel="alternate" href="https://veloxquant-mlx.netlify.app/docs/blog"/>
    <subtitle>VeloxQuant-MLX Blog</subtitle>
    <icon>https://veloxquant-mlx.netlify.app/docs/img/favicon.ico</icon>
    <entry>
        <title type="html"><![CDATA[The ROM Chip That Wasn't, and the 230× Speedup That Was]]></title>
        <id>https://veloxquant-mlx.netlify.app/docs/blog/weight-reservoir</id>
        <link href="https://veloxquant-mlx.netlify.app/docs/blog/weight-reservoir"/>
        <updated>2026-08-26T00:00:00.000Z</updated>
        <summary type="html"><![CDATA[What happens when you take a thought experiment about burning LLM weights into silicon, try to build the closest real thing in software, and let the machine tell you which parts of the idea survive contact with MLX's actual copy semantics.]]></summary>
        <content type="html"><![CDATA[<p><em>What happens when you take a thought experiment about burning LLM weights into silicon, try to build the closest real thing in software, and let the machine tell you which parts of the idea survive contact with MLX's actual copy semantics.</em></p>
<hr>
<p>The idea started as the kind of thing you think about in the shower: what if Apple burned a frozen LLM's weights into a ROM chip sitting right next to RAM? Read-only. Never re-derived. Never fully materialized in general-purpose memory. A model that's just <em>there</em>, the way a calculator's multiplication table is just there.</p>
<p>You can't ship that. This is a software library, not a silicon fab. But strip the hardware away and look at what's actually being asked for — weights that are read-only, computed once, and shared across processes without each one paying full price — and it stops being science fiction. It starts looking like a real, specific gap in how VeloxQuant-MLX loads models today.</p>
<p>So we built the closest real thing, measured it against the actual machine it would run on, and let two of our own assumptions get overturned by the data along the way. This is the record of that — including the part where a "fix" I proposed made things worse, and the part where I had to walk back a claim about compression that turned out to be mathematically impossible.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="what-already-existed">What already existed<a href="https://veloxquant-mlx.netlify.app/docs/blog/weight-reservoir#what-already-existed" class="hash-link" aria-label="Direct link to What already existed" title="Direct link to What already existed" translate="no">​</a></h2>
<p>VeloxQuant-MLX already had most of the ingredients, just not assembled this way.</p>
<p><a href="https://github.com/rajveer43/VeloxQuant-MLX/blob/master/veloxquant_mlx/weight/quantized_linear.py" target="_blank" rel="noopener noreferrer" class=""><code>QuantizedLinear</code></a> compresses a weight matrix by normalizing each row, rotating it (Hadamard transform or a QR-derived rotation, depending on the dimension), and mapping it onto a small Lloyd-Max codebook — 2 to 4 bits per weight instead of 16. <code>quantize_model()</code> walks an entire <code>mlx-lm</code> model and replaces every <code>nn.Linear</code> with one of these. It works, and the compression ratios are real.</p>
<p>But the compressed result only ever lives as an <code>mx.array</code> in one process's memory. Every time you load the model — a new server worker, a restarted process, a second experiment running alongside the first — you pay the full cost again: dequantize the source weights, rotate them, run nearest-centroid search against the codebook, for every layer. Nothing from the last time you did this is reused.</p>
<p>That's the gap. Not "we lack compression" — we had that. "We recompute the compression from scratch on every single load."</p>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="two-questions-that-had-to-be-answered-before-writing-any-code">Two questions that had to be answered before writing any code<a href="https://veloxquant-mlx.netlify.app/docs/blog/weight-reservoir#two-questions-that-had-to-be-answered-before-writing-any-code" class="hash-link" aria-label="Direct link to Two questions that had to be answered before writing any code" title="Direct link to Two questions that had to be answered before writing any code" translate="no">​</a></h2>
<p>The ROM framing implies something specific: that multiple processes could share one physical copy of the weights, the way the OS page cache lets multiple processes reading the same file share pages in RAM. Before designing a file format around that idea, it needed to actually be true for MLX. So, two direct tests against the library itself rather than its documentation.</p>
<p><strong>Does <code>mx.load()</code> already give us this for free?</strong> I saved a 500 MB tensor to <code>.safetensors</code>, then launched two independent processes that both called <code>mx.load()</code> on it, and read each process's physical footprint with <code>vmmap</code>:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">PID 15970: Physical footprint 514.3M</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">PID 15971: Physical footprint 514.3M</span><br></div></code></pre></div></div>
<p>Each process paid the full 514 MB independently — 1.03 GB combined for one 500 MB file. <code>vmmap</code> showed no mapped-file region for the safetensors path at all. The loader reads and copies into a private heap allocation. No sharing, no <code>mmap()</code>, nothing.</p>
<p><strong>Fine — what if I build the mmap myself?</strong> I <code>mmap</code>'d a raw binary file with <code>np.memmap</code> (confirmed lazy — RSS didn't move) and wrapped it with <code>mx.array()</code>:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">RSS after np.memmap (lazy, no touch):        1534.5 MB   (unchanged)</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">RSS after mx.array() wrap (no eval):         2542.3 MB   (+1008 MB — 2× the file size)</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">RSS after mx.eval(x):                        2542.3 MB   (no further change)</span><br></div></code></pre></div></div>
<p><code>mx.array()</code> copies the entire buffer immediately, before <code>mx.eval()</code> is even called, and the jump is roughly double the source size — there's a staging copy in there somewhere before the data lands in MLX's own unified-memory arena. There is no zero-copy constructor from a buffer in this version of MLX's Python API.</p>
<p>That killed the strongest version of the pitch. Processes on this machine are not going to share physical pages of model weights through anything MLX gives you today. If a future MLX version adds a real zero-copy buffer path, this is the test to rerun. Until then, the honest framing is narrower: <strong>skip the recomputation, not the RAM.</strong></p>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="building-the-thing-anyway">Building the thing anyway<a href="https://veloxquant-mlx.netlify.app/docs/blog/weight-reservoir#building-the-thing-anyway" class="hash-link" aria-label="Direct link to Building the thing anyway" title="Direct link to Building the thing anyway" translate="no">​</a></h2>
<p>What survives the two negative findings above is still worth having: a file format that holds a model's already-quantized weights, persisted once, loadable without repeating the compression work. Call it a reservoir instead of a ROM — closer to what it actually is.</p>
<p>The first version was almost embarrassingly simple. Serialize each <code>QuantizedLinear</code> layer's compressed indices and per-row norms into a flat, page-aligned binary file — one blob for the 2–4 bit indices, one for the fp32 norms, a small JSON header recording each layer's shape and the seed used to derive its rotation and codebook. On load, reconstruct each layer from the header and drop the persisted indices straight in, skipping the whole dequantize-rotate-quantize pipeline.</p>
<p>I benchmarked it against <code>quantize_model()</code> on <code>Qwen2.5-0.5B-Instruct-4bit</code> — 168 linear layers, a small enough model to iterate on quickly. The baseline took 81.6 seconds. My new reservoir loader took 66.6 seconds.</p>
<p>That's not a win. That's barely a rounding error.</p>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="where-the-time-actually-went">Where the time actually went<a href="https://veloxquant-mlx.netlify.app/docs/blog/weight-reservoir#where-the-time-actually-went" class="hash-link" aria-label="Direct link to Where the time actually went" title="Direct link to Where the time actually went" translate="no">​</a></h2>
<p>I profiled it instead of guessing.</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">ncalls  tottime  cumtime  function</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">   168    0.126   66.587  QuantizedLinear.__init__</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    24    2.079   57.442  make_rotation_matrix</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    24   54.693   55.147  numpy.linalg.qr</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">   168    0.001    8.907  CodebookFactory.create</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">   168    4.016    8.899  lloyd_max</span><br></div></code></pre></div></div>
<p>There it was. 55 of the 66.6 seconds were spent inside <code>np.linalg.qr</code> — the routine that derives a rotation matrix for any layer whose input dimension doesn't cleanly fit MLX's Hadamard transform (a dimension like 4864, which is neither a clean power of two nor a multiple of the special constants MLX's fast transform supports). Another 8.9 seconds went to fitting Lloyd-Max codebooks.</p>
<p>The reservoir file <em>looked</em> like it had skipped requantization. What it actually skipped was the nearest-centroid search — the smallest part of the cost. Every layer's rotation matrix and codebook were being silently recomputed from the stored seed, on every load, exactly as expensive as before. The format was a placebo.</p>
<p>The fix was to stop being clever about "everything is derivable from the seed" and just persist the actual rotation matrices and codebooks, then reconstruct each layer by bypassing <code>QuantizedLinear.__init__</code>'s expensive branches entirely — direct field assignment onto a bare module instead of calling a constructor that recomputes things you already have on disk.</p>
<p>That dropped the load time to somewhere between 0.36 and 9.2 seconds, against the same 81.6-second baseline. Even the conservative end of that range is a 9× speedup; the isolated, repeatable measurement (0.36s, matching <code>cProfile</code>'s CPU-time reading almost exactly) is closer to 230×. The gap between those two numbers is real and I haven't root-caused it — plausibly Metal shader warm-up, plausibly GPU dispatch contention from running back-to-back with the baseline in the same process. I'm reporting the range rather than picking the number that looks better.</p>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="the-part-where-fixing-one-problem-created-a-worse-one">The part where fixing one problem created a worse one<a href="https://veloxquant-mlx.netlify.app/docs/blog/weight-reservoir#the-part-where-fixing-one-problem-created-a-worse-one" class="hash-link" aria-label="Direct link to The part where fixing one problem created a worse one" title="Direct link to The part where fixing one problem created a worse one" translate="no">​</a></h2>
<p>Feeling good about the speedup, I checked the file size. The source model is 265 MB on disk. My reservoir file was 2.5 gigabytes.</p>
<p>Nine and a half times larger than the model it was supposedly compressing.</p>
<p>I broke down where the bytes went:</p>
<table><thead><tr><th>Blob</th><th>Size</th></tr></thead><tbody><tr><td>index blob (the actual 4-bit weights)</td><td>341 MB</td></tr><tr><td>norms blob</td><td>3.4 MB</td></tr><tr><td><strong>rotation blob</strong></td><td><strong>2168 MB</strong></td></tr><tr><td>centroids blob</td><td>2.6 MB</td></tr></tbody></table>
<p>Eighty-six percent of the file was rotation matrices. Specifically, the 24 layers that fall back to QR rotation instead of the fast Hadamard path — each one has <code>in_features = 4864</code>, and a <code>4864 × 4864</code> matrix at 4 bytes per float is about 95 MB. Twenty-four of those is 2.17 GB, and that's before touching a single actual weight.</p>
<p>The instinct here is "surely you can store that more compactly." I had the same instinct, and I want to walk through why it doesn't work, because the wrong answer is genuinely tempting.</p>
<p><code>np.linalg.qr</code> has a <code>mode='raw'</code> option that returns the underlying Householder reflectors instead of the assembled orthogonal matrix — sounds exactly like what you'd want, a compact representation of the same rotation. I checked what shape it actually returns:</p>
<div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">h</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> tau </span><span class="token operator">=</span><span class="token plain"> np</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">linalg</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">qr</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">G</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> mode</span><span class="token operator">=</span><span class="token string" style="color:rgb(255, 121, 198)">"raw"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token comment" style="color:rgb(98, 114, 164)"># h.shape == (4864, 4864)   — 180.5 MB, even bigger than the dense matrix</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token comment" style="color:rgb(98, 114, 164)"># tau.shape == (4864,)      — 0.037 MB</span><br></div></code></pre></div></div>
<p>Still a full <code>d × d</code> array. The reason isn't an API limitation — it's that a Haar-random orthogonal <code>d × d</code> matrix genuinely contains <code>d(d-1)/2</code> degrees of freedom. That's Θ(d²) information, full stop. There is no encoding, clever or otherwise, that gets a truly random rotation below quadratic storage, because the matrix doesn't have any structure to exploit. It's not compressible the way the <em>weights</em> are compressible — the weights have statistical structure a codebook can exploit; a random rotation, by construction, doesn't.</p>
<p>So the earlier plan — "store the rotation as reflectors, get a smaller file" — was wrong. Not underexplored. Wrong, provably, in about ten minutes of checking.</p>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="making-the-tradeoff-a-choice-instead-of-a-default">Making the tradeoff a choice instead of a default<a href="https://veloxquant-mlx.netlify.app/docs/blog/weight-reservoir#making-the-tradeoff-a-choice-instead-of-a-default" class="hash-link" aria-label="Direct link to Making the tradeoff a choice instead of a default" title="Direct link to Making the tradeoff a choice instead of a default" translate="no">​</a></h2>
<p>Once "compress the rotation matrix" was off the table, what was left was a genuine, irreducible tradeoff: you can have a fast load (persist the rotation matrix, pay the disk space) or a small file (don't persist it, pay the QR cost again on every load). Not both, for any layer that needs QR fallback.</p>
<p>The fix was to stop pretending there was a single right answer and expose the choice:</p>
<div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">save_reservoir</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">model</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> path</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> persist_rotation</span><span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain">  </span><span class="token comment" style="color:rgb(98, 114, 164)"># default: small file</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">save_reservoir</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">model</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> path</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> persist_rotation</span><span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain">  </span><span class="token comment" style="color:rgb(98, 114, 164)"># fast load, large file</span><br></div></code></pre></div></div>
<p>With the default off, the reservoir file for the same model comes out to 349.7 MB — 1.3× the source model, essentially just the compressed weights plus some structural overhead — and load time goes back up to about 59 seconds, since QR-fallback layers regenerate their rotation from the stored seed exactly as the original <code>quantize_model()</code> path does. With it on, you're back to sub-second loads and a 2.5 GB file. Hadamard-compatible layers — 144 of the 168 in this model — are unaffected either way, since their rotation is just a <code>d</code>-length sign vector, cheap to store regardless.</p>
<p>Neither setting is wrong. What was wrong was shipping one of them silently as the only option and calling the result "a smaller reservoir."</p>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="what-didnt-get-tested-and-why">What didn't get tested, and why<a href="https://veloxquant-mlx.netlify.app/docs/blog/weight-reservoir#what-didnt-get-tested-and-why" class="hash-link" aria-label="Direct link to What didn't get tested, and why" title="Direct link to What didn't get tested, and why" translate="no">​</a></h2>
<p>The original plan called for benchmarking <code>Qwen3-4B-4bit</code> — a more realistic model size, and specifically what the ideation phase had proposed. <code>quantize_model()</code> on that model reliably killed the process with SIGKILL on this 24 GB machine. I checked the peak memory footprint before the kill: 21.2 GB, against roughly 14–15 GB of actually available memory at the time.</p>
<p>This isn't a bug I introduced. It's the same headroom constraint documented elsewhere in this repo (<code>docs/MEMORY_CONSTRAINT_FINDINGS.md</code>) for a 32B model on the same hardware — <code>quantize_model()</code>'s dequantize-rotate-quantize pipeline makes several full-size intermediate copies rather than working in place, and a 4B model's pipeline apparently needs more headroom than this particular machine has free right now. It's a real, separate problem, and it blocked the concurrent-process memory benchmark I'd wanted to run — the one that would have measured whether four processes loading the reservoir simultaneously actually show smaller RSS than four processes each running <code>quantize_model()</code> from scratch. That benchmark still needs to happen, on a model this machine can actually load, or on a machine with more headroom.</p>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="what-actually-shipped">What actually shipped<a href="https://veloxquant-mlx.netlify.app/docs/blog/weight-reservoir#what-actually-shipped" class="hash-link" aria-label="Direct link to What actually shipped" title="Direct link to What actually shipped" translate="no">​</a></h2>
<ul>
<li class=""><code>save_reservoir()</code> / <code>load_reservoir()</code> / <code>graft_reservoir()</code> in <code>weight/reservoir.py</code> — a flat, page-aligned binary format, with <code>persist_rotation</code> as an explicit, documented tradeoff rather than a hidden default.</li>
<li class="">A <code>_fast_quantized_linear()</code> construction path that builds a <code>QuantizedLinear</code> from persisted state without touching <code>__init__</code>'s QR or Lloyd-Max branches.</li>
<li class="">Eleven tests, including a deliberately non-Hadamard-compatible layer (<code>in_features=50</code>) to exercise the QR fallback path directly, and bit-exact round-trip checks in both <code>persist_rotation</code> modes — the loaded model's forward pass output is byte-identical to the freshly quantized one, not just "close."</li>
<li class="">A results file with the actual numbers, not just the ones that made the feature look good.</li>
</ul>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="what-this-experiment-is-actually-about">What this experiment is actually about<a href="https://veloxquant-mlx.netlify.app/docs/blog/weight-reservoir#what-this-experiment-is-actually-about" class="hash-link" aria-label="Direct link to What this experiment is actually about" title="Direct link to What this experiment is actually about" translate="no">​</a></h2>
<p>None of the interesting findings here came from writing code that worked on the first try. They came from measuring something, getting an answer that contradicted an assumption, and following that instead of the original plan:</p>
<ul>
<li class="">The "shared ROM" framing died the moment two processes independently paid full memory cost for the same file — a five-minute test with <code>vmmap</code>, run before any format design.</li>
<li class="">The first reservoir format's near-zero speedup came from profiling instead of trusting that "we skip requantization" was true because the code was structured to look like it should be.</li>
<li class="">The Householder-reflector idea died in about ten minutes of checking <code>mode='raw'</code>'s actual return shape — a claim that sounded plausible enough to write into a planning doc, and would have stayed there if nobody had gone and checked.</li>
</ul>
<p>The honest version of this project isn't "we built a ROM chip in software." It's: cross-process sharing doesn't work with MLX's current copy semantics, skipping recomputation is a real and large win once you persist the <em>right</em> things, and file size versus load time is a fundamental tradeoff for anything that needs a truly random rotation — not a bug to be engineered away, just a dial to expose honestly instead of hiding.</p>
<p>That's a smaller claim than the one I started with. It's also the one that's actually true.</p>]]></content>
        <author>
            <name>Rajveer Rathod</name>
            <uri>https://github.com/rajveer43</uri>
        </author>
        <category label="quantization" term="quantization"/>
        <category label="apple-silicon" term="apple-silicon"/>
        <category label="mlx" term="mlx"/>
        <category label="weights" term="weights"/>
        <category label="benchmarking" term="benchmarking"/>
        <category label="memory" term="memory"/>
    </entry>
    <entry>
        <title type="html"><![CDATA[The Sign Was the Whole Paper: Debugging a KV Cache Compressor on Real Models]]></title>
        <id>https://veloxquant-mlx.netlify.app/docs/blog/qfilters-query-geometry</id>
        <link href="https://veloxquant-mlx.netlify.app/docs/blog/qfilters-query-geometry"/>
        <updated>2026-08-14T00:00:00.000Z</updated>
        <summary type="html"><![CDATA[How one arbitrary minus sign turned a KV cache compressor from useful into noise — and what it took to prove it on real models]]></summary>
        <content type="html"><![CDATA[<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="how-one-arbitrary-minus-sign-turned-a-kv-cache-compressor-from-useful-into-noise--and-what-it-took-to-prove-it-on-real-models">How one arbitrary minus sign turned a KV cache compressor from useful into noise — and what it took to prove it on real models<a href="https://veloxquant-mlx.netlify.app/docs/blog/qfilters-query-geometry#how-one-arbitrary-minus-sign-turned-a-kv-cache-compressor-from-useful-into-noise--and-what-it-took-to-prove-it-on-real-models" class="hash-link" aria-label="Direct link to How one arbitrary minus sign turned a KV cache compressor from useful into noise — and what it took to prove it on real models" title="Direct link to How one arbitrary minus sign turned a KV cache compressor from useful into noise — and what it took to prove it on real models" translate="no">​</a></h2>
<hr>
<p>I had a KV cache compression method in my library called Q-Filters. It had tests. It had docs. It had a benchmark harness with committed results. It shipped in version 0.31.0.</p>
<p>It also didn't work.</p>
<p>Not "worked slightly worse than the paper." Not "worked on some heads." On real trained weights, its scoring signal correlated with true attention at <strong>−0.032</strong> — statistically indistinguishable from a coin flip, and pointing the wrong way about half the time.</p>
<p>Fixing it meant implementing the paper properly, writing two Metal kernels, finding a position-encoding bug that made end-to-end measurement impossible, and throwing away two benchmark harnesses that produced confident, meaningless numbers. Along the way the calibrated version went from <strong>ppl 598 to 16.3</strong> on the same workload.</p>
<p>This is the whole run, including the parts where I was wrong.</p>
<!-- -->
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="what-q-filters-is-supposed-to-do">What Q-Filters is supposed to do<a href="https://veloxquant-mlx.netlify.app/docs/blog/qfilters-query-geometry#what-q-filters-is-supposed-to-do" class="hash-link" aria-label="Direct link to What Q-Filters is supposed to do" title="Direct link to What Q-Filters is supposed to do" translate="no">​</a></h2>
<p>Every token a language model generates writes a <strong>Key</strong> and a <strong>Value</strong> vector into the KV cache. At 32K context these outweigh the model itself. So you evict: keep the important entries, drop the rest.</p>
<p>The hard part is deciding what's important. The obvious answer — look at the attention weights — is expensive and, worse, incompatible with FlashAttention, which never materializes the attention matrix you'd need to inspect.</p>
<p><a href="https://arxiv.org/abs/2503.02812" target="_blank" rel="noopener noreferrer" class="">Q-Filters</a> (Godey et al., 2025) makes a sharp observation. For a trained attention head, the Query and Key distributions are <em>jointly anisotropic</em>: they drift away from the origin along a shared direction. Call that direction <code>uʰ</code>. Then the paper's Theorem 3.3 says:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">E_Q [ &lt;Q_i, K_j&gt; ]  ≈  κʰ · &lt;K_j, uʰ&gt;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">with   κʰ = E_Q [ &lt;Q_i, uʰ&gt; ]  &gt;  0</span><br></div></code></pre></div></div>
<p>Read that carefully, because the entire method lives inside it. The expected attention logit for a cached key is proportional to that key's <strong>projection onto a single fixed direction</strong>. One dot product per key. No attention matrix. No query needed at eviction time.</p>
<p>You compute <code>uʰ</code> once, offline, per model. Then eviction is: project, rank, drop the bottom.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="the-bug-i-had-shipped">The bug I had shipped<a href="https://veloxquant-mlx.netlify.app/docs/blog/qfilters-query-geometry#the-bug-i-had-shipped" class="hash-link" aria-label="Direct link to The bug I had shipped" title="Direct link to The bug I had shipped" translate="no">​</a></h2>
<p>The paper gets <code>uʰ</code> from the <strong>SVD of query activations</strong>, collected offline (§3.2, Eq. 1):</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">Qʰ = U Σ Vᵀ,   V = (v₁, v₂, …, v_dH)</span><br></div></code></pre></div></div>
<p>My implementation got it from the SVD of the <strong>keys</strong> it happened to observe at runtime.</p>
<p>The reasoning behind that shortcut wasn't crazy. A KV cache never sees query vectors — it only receives the K and V passed to <code>update_and_fetch</code>. Queries live upstream in the attention module. So I'd substituted "a different estimator of the same head-geometry direction" and documented it honestly. The module docstring literally had a section header reading <code>THE HONESTY CRUX</code>, and the docs said the substitution was "a genuine deviation, not a shortcut."</p>
<p>Documented dishonesty is still dishonesty when the thing you documented is <em>broken</em>.</p>
<p>Here's what I'd missed. Look at <code>κʰ &gt; 0</code> again. That positivity is what makes "higher projection means more attention" a valid ranking rule — flip its sign and you're ranking by <em>least</em> important. And <code>κʰ</code> is defined as an expectation over the <strong>query</strong> distribution. It is not a property of the keys. It is not recoverable from the keys.</p>
<p>Estimating from keys throws away exactly the quantity that tells you which end of the axis matters.</p>
<p>The symptom was visible in my own committed benchmark and I'd rationalized it: the key-SVD recovered the planted axis with <code>filter_cosine ≈ 0.97</code>, but whether <code>sign=+1</code> or <code>sign=-1</code> was the good arm "flips from row to row." I'd shipped the sign as a <em>config knob</em> — an ablation the user could try both ways. That's not a knob. That's a coin flip wearing a parameter name.</p>
<p>There was a second, worse problem I only found later. My key-side estimator <strong>mean-centered</strong> the data before the SVD:</p>
<div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">x </span><span class="token operator">=</span><span class="token plain"> keys</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">astype</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">mx</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">float32</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">x </span><span class="token operator">=</span><span class="token plain"> x </span><span class="token operator">-</span><span class="token plain"> mx</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">mean</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">x</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> axis</span><span class="token operator">=</span><span class="token number">0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> keepdims</span><span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain">  </span><span class="token comment" style="color:rgb(98, 114, 164)"># center</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">cov </span><span class="token operator">=</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">x</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">T @ x</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token operator">/</span><span class="token plain"> </span><span class="token builtin" style="color:rgb(189, 147, 249)">max</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token builtin" style="color:rgb(189, 147, 249)">int</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">x</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">shape</span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token number">0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token operator">-</span><span class="token plain"> </span><span class="token number">1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><br></div></code></pre></div></div>
<p>Centering is standard PCA hygiene. It is also precisely wrong here. The paper's Observation 3.1 is about the cloud's <strong>drift away from the origin</strong> — the mean offset <em>is</em> the signal. Subtracting it leaves you measuring variance, which is a different direction entirely.</p>
<p>I verified this directly:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">drift along u = 6.0, competing spread along w = 2.0</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">uncentered SVD  ·  u = 1.000   (drift recovered)</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">centered   SVD  ·  u = 0.001   (drift destroyed)</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">centered   SVD  ·  w = 1.000   (returns the variance axis instead)</span><br></div></code></pre></div></div>
<p>So the old implementation was doing two things wrong at once: measuring the wrong matrix, and then destroying the drift signal even within that matrix.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="implementing-the-paper">Implementing the paper<a href="https://veloxquant-mlx.netlify.app/docs/blog/qfilters-query-geometry#implementing-the-paper" class="hash-link" aria-label="Direct link to Implementing the paper" title="Direct link to Implementing the paper" translate="no">​</a></h2>
<p>The fix is a separate offline calibration module — which turned out to be well-precedented in my own codebase. I already had <code>amc_calibration.py</code> doing offline SVD calibration, and, more usefully, <code>a2ats.py</code> was already computing <code>H = E[qᵀq]</code> from <strong>query</strong> states. So query access was a solved problem; I'd just never connected it to Q-Filters.</p>
<p><code>qfilters_calibration.py</code> implements §3.2 step 1 as written:</p>
<div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">def</span><span class="token plain"> </span><span class="token function" style="color:rgb(80, 250, 123)">compute_qfilters</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">queries</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> max_svd_samples</span><span class="token operator">=</span><span class="token number">3000</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token triple-quoted-string string" style="color:rgb(255, 121, 198)">"""[H, N, D] query activations -&gt; [H, D] unit-norm Q-Filters."""</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">for</span><span class="token plain"> head </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">in</span><span class="token plain"> </span><span class="token builtin" style="color:rgb(189, 147, 249)">range</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">h</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        mat </span><span class="token operator">=</span><span class="token plain"> q</span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token plain">head</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">astype</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">np</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">float64</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain">  </span><span class="token comment" style="color:rgb(98, 114, 164)"># NOT centered</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        u</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> _s</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> vt </span><span class="token operator">=</span><span class="token plain"> np</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">linalg</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">svd</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">mat</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> full_matrices</span><span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        v1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> u1 </span><span class="token operator">=</span><span class="token plain"> vt</span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token number">0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> u</span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        </span><span class="token comment" style="color:rgb(98, 114, 164)"># Paper §3.2 step 1c: v_1^+ = sgn(1^T u_1) v_1</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        s </span><span class="token operator">=</span><span class="token plain"> </span><span class="token builtin" style="color:rgb(189, 147, 249)">float</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">np</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token builtin" style="color:rgb(189, 147, 249)">sum</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">u1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        v1 </span><span class="token operator">=</span><span class="token plain"> v1 </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">if</span><span class="token plain"> s </span><span class="token operator">&gt;=</span><span class="token plain"> </span><span class="token number">0.0</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">else</span><span class="token plain"> </span><span class="token operator">-</span><span class="token plain">v1</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        out</span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token plain">head</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token plain"> </span><span class="token operator">=</span><span class="token plain"> v1 </span><span class="token operator">/</span><span class="token plain"> </span><span class="token builtin" style="color:rgb(189, 147, 249)">max</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">np</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">linalg</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">norm</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">v1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">1e-12</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><br></div></code></pre></div></div>
<p>Two details carry all the weight:</p>
<p><strong>No mean-centering.</strong> Commented, tested, and justified — because it looks like an omission and a future reader will "fix" it.</p>
<p><strong>Sign anchoring on <code>sgn(1ᵀu₁)</code>.</strong> An SVD's signs are arbitrary: <code>(u₁, v₁)</code> and <code>(−u₁, −v₁)</code> are both valid factorizations. Anchoring on the left singular vector's sum orients the filter along the direction the queries actually drift, which is what makes <code>κʰ &gt; 0</code> hold.</p>
<p>Plus GQA handling — Llama-3.2-1B has 32 query heads feeding 8 KV heads, and the paper says to average each group's filters onto its KV head, renormalizing so projections stay comparable across heads.</p>
<p>The immediate check, on planted geometry:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">head 0  query-SVD cos vs +u = 0.9999</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">head 1  query-SVD cos vs +u = 1.0000</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">key-SVD             cos vs +u = +0.3649   (axis only, sign arbitrary)</span><br></div></code></pre></div></div>
<p>Sign recovered. Now: does any of this hold on a real model?</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="run-1-does-the-anisotropy-actually-exist">Run 1: does the anisotropy actually exist?<a href="https://veloxquant-mlx.netlify.app/docs/blog/qfilters-query-geometry#run-1-does-the-anisotropy-actually-exist" class="hash-link" aria-label="Direct link to Run 1: does the anisotropy actually exist?" title="Direct link to Run 1: does the anisotropy actually exist?" translate="no">​</a></h2>
<p>Everything above assumes trained attention heads really are anisotropic in the way the paper claims. That's the paper's empirical finding, and I'd never checked it.</p>
<p>I wrote a script to measure both observations on real query activations. Observation 3.1 is <code>E&lt;Q,uʰ&gt; &gt; 0</code>. Observation 3.2 is that projections onto every <em>other</em> SVD component have near-zero mean — the anisotropy is one-directional, not diffuse.</p>
<p>Three cached models, 1968 attention heads total:</p>
<table><thead><tr><th>Model</th><th>Obs 3.1: <code>E&lt;Q,uʰ&gt; &gt; 0</code></th><th>Obs 3.2 ratio</th><th>Top-component energy</th></tr></thead><tbody><tr><td>Llama-3.2-1B-Instruct-4bit</td><td><strong>100%</strong> of 512 heads</td><td>44.4×</td><td>90.6%</td></tr><tr><td>Llama-3.2-3B-Instruct-4bit</td><td><strong>100%</strong> of 672 heads</td><td>52.3×</td><td>84.7%</td></tr><tr><td>Qwen2.5-7B-Instruct-4bit</td><td><strong>100%</strong> of 784 heads</td><td>47.9×</td><td>81.3%</td></tr></tbody></table>
<p>Observation 3.1 held in <strong>every single head measured</strong>. Not 95%, not "most heads" — 1968 for 1968, with median projection +11.9 to +15.5.</p>
<p>That universal positivity <em>is</em> <code>κʰ &gt; 0</code>. The quantity my key-side estimator had been throwing away is, empirically, one of the most reliable properties of a trained transformer.</p>
<p>Observation 3.2 held too: the leading component's mean projection runs ~50× the others, which sit near zero. That's the paper's Figure 2c, reproduced.</p>
<p>The Qwen result was a small surprise. The paper's §5 lists Qwen-2.5 as a <em>limitation</em> — its QKV projection bias was expected to break the geometric assumptions. The anisotropy shows up anyway, at 47.9×.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="run-2-does-the-filter-predict-real-attention">Run 2: does the filter predict real attention?<a href="https://veloxquant-mlx.netlify.app/docs/blog/qfilters-query-geometry#run-2-does-the-filter-predict-real-attention" class="hash-link" aria-label="Direct link to Run 2: does the filter predict real attention?" title="Direct link to Run 2: does the filter predict real attention?" translate="no">​</a></h2>
<p>Anisotropy existing is necessary but not sufficient. The claim that matters is that projecting onto <code>v₁⁺</code> predicts <em>attention</em>. So I built the paper's Figure 4: compute the true attention map, measure the actual mean attention each position receives,</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">Sʰ_t = (1 / (L - t + 1)) · Σ_{i=t..L} Aʰ_it</span><br></div></code></pre></div></div>
<p>and rank-correlate each scoring method against it. Calibration on one corpus, evaluation on held-out text.</p>
<table><thead><tr><th>Scorer</th><th>Llama-3.2-1B (128 KV heads)</th><th>Llama-3.2-3B (224 KV heads)</th></tr></thead><tbody><tr><td><strong>Q-Filters, calibrated (query-SVD)</strong></td><td><strong>+0.783</strong> (100% sign-correct)</td><td><strong>+0.863</strong> (100%)</td></tr><tr><td>K-norm (Devoto et al.)</td><td>+0.460 (94.5%)</td><td>+0.410 (94.6%)</td></tr><tr><td>Q-Filters, key-SVD (what I'd shipped)</td><td><strong>−0.032</strong> (46.1%)</td><td><strong>−0.008</strong> (49.1%)</td></tr></tbody></table>
<p>This is the result that made the whole exercise worth it.</p>
<p>The calibrated filter correlates strongly with true attention and <strong>beats K-norm</strong>, reproducing the paper's Figure 4 ordering. Meanwhile the thing I had shipped, tested, documented and released sits at −0.032 with its sign correct <strong>46% of the time</strong>. Worse than guessing.</p>
<p>And the failure was even more complete than "ambiguous sign." With isotropic key noise, the mean-centering estimator has no drift left to lock onto, so it returns a nearly unrelated direction:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">pure-drift key geometry:</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  key-SVD   |cos| vs planted direction = 0.02</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  query-SVD  cos  vs planted direction = 0.99</span><br></div></code></pre></div></div>
<p>Two independent measurements, same conclusion: query-SVD and key-SVD aren't two estimators of one thing. One is a signal; the other is noise.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="metal-kernels-and-one-trap">Metal kernels, and one trap<a href="https://veloxquant-mlx.netlify.app/docs/blog/qfilters-query-geometry#metal-kernels-and-one-trap" class="hash-link" aria-label="Direct link to Metal kernels, and one trap" title="Direct link to Metal kernels, and one trap" translate="no">​</a></h2>
<p>With the math right, the hot path deserved GPU treatment. My library already had fused eviction kernels for H2O and Keyformer, so the pattern existed — but Q-Filters differs structurally in a way that changes the design.</p>
<p>H2O and Keyformer evict <strong>exactly one row per token</strong>. So dispatch 1 is an argmin reduction to a single index, and dispatch 2 shifts rows around it in closed form: <code>src = j + (j &gt;= evict_idx)</code>.</p>
<p>Q-Filters evicts a <strong>whole block down to budget at once</strong>. There's no closed form — a thread writing output row <code>j</code> can't know its source without knowing how many survivors precede it. So:</p>
<ol>
<li class=""><strong><code>qfilters_score.metal</code></strong> — full <code>[BH, n_total]</code> projection scores, fp32 accumulation over fp16 keys, with sink and recent rows forced to <code>+INFINITY</code> so protection is baked into the values the threshold later sees.</li>
<li class=""><strong><code>qfilters_evict_apply.metal</code></strong> — one threadgroup per <code>(batch, head)</code>, cooperative scan to build a survivor index list in threadgroup memory, then a fully parallel gather.</li>
</ol>
<p>The threshold itself stays in MLX via <code>mx.sort</code>. A top-k is a primitive MLX already implements well; reimplementing it in Metal would be slower and harder to trust.</p>
<p><strong>The trap.</strong> The obvious way to apply a threshold is <code>score &gt;= thresh</code>. That silently overflows the budget whenever the threshold value repeats — and here duplicates aren't an edge case, they're guaranteed, because every protected row shares <code>+INFINITY</code>. Overflow would break the cache's size guarantee, which is the one thing a compressor must never break.</p>
<p>So admission runs in two tiers: strictly-greater always survives; equal-to-threshold survives only while quota remains, scanning low index to high. That reproduces <code>mx.argsort</code>'s lowest-index-first tie-break, so kernel and MLX paths agree exactly.</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">tied scores (all keys identical, all scores equal):  kept == budget exactly</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">all-protected kept set (10 sinks + 10 recent == 20): kept == budget exactly</span><br></div></code></pre></div></div>
<p>Twenty kernel tests, mostly parity checks against a numpy argsort reference for keys <em>and</em> values. A kernel that's merely plausible but disagrees with the reference is a silent correctness bug, which is the worst kind.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="harness-bug-1-the-numbers-that-lied-to-me">Harness bug 1: the numbers that lied to me<a href="https://veloxquant-mlx.netlify.app/docs/blog/qfilters-query-geometry#harness-bug-1-the-numbers-that-lied-to-me" class="hash-link" aria-label="Direct link to Harness bug 1: the numbers that lied to me" title="Direct link to Harness bug 1: the numbers that lied to me" translate="no">​</a></h2>
<p>Then I tried to measure end-to-end perplexity, and got this:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">fp16 full cache                    ppl     1.240</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">Q-Filters CALIBRATED (query-SVD)   ppl  4624.480</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">Q-Filters fallback (key-SVD)       ppl  6650.481</span><br></div></code></pre></div></div>
<p>A method correlating +0.78 with true attention does not produce ppl 4624. When results are absurd, suspect the harness.</p>
<p><strong>Harness bug 1: single-pass prefill.</strong> I'd fed the whole sequence as one block. The cache evicts <em>during</em> that block, so most tokens were being predicted from a cache already mutilated in a way real generation never does. The paper is explicit about the setup (§4): let the cache grow to a threshold, then evict as you go, scoring each next-token prediction. Token-by-token, not one shot.</p>
<p>Rewriting it that way helped — and still gave ppl 598. Which meant the harness wasn't the only problem.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="the-bug-underneath-rope-positions">The bug underneath: RoPE positions<a href="https://veloxquant-mlx.netlify.app/docs/blog/qfilters-query-geometry#the-bug-underneath-rope-positions" class="hash-link" aria-label="Direct link to The bug underneath: RoPE positions" title="Direct link to The bug underneath: RoPE positions" translate="no">​</a></h2>
<p>Something structural remained, and the <em>tell</em> was that the calibrated arm was doing worse than the fallback — contradicting two independent correlation measurements. That ordering couldn't be a scoring problem. It had to be downstream of scoring.</p>
<p>I read how <code>mlx_lm</code> actually calls the cache:</p>
<div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">queries </span><span class="token operator">=</span><span class="token plain"> self</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">rope</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">queries</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> offset</span><span class="token operator">=</span><span class="token plain">cache</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">offset</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">keys </span><span class="token operator">=</span><span class="token plain"> self</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">rope</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">keys</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> offset</span><span class="token operator">=</span><span class="token plain">cache</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">offset</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">keys</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> values </span><span class="token operator">=</span><span class="token plain"> cache</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">update_and_fetch</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">keys</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> values</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><br></div></code></pre></div></div>
<p>Rotary position encoding is applied to both queries and keys, at <code>cache.offset</code>, <strong>before</strong> the cache is updated. So <code>cache.offset</code> isn't bookkeeping — it's the position signal the model rotates by.</p>
<p>And my cache was reporting the number of <em>retained</em> rows:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">true pos -&gt; cache.offset used for RoPE:</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  t=  63  offset=  63  drift=   +0</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  t=  64  offset=  64  drift=   +0</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  t=  70  offset=  64  drift=   +6</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  t= 100  offset=  64  drift=  +36</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  t= 199  offset=  64  drift= +135</span><br></div></code></pre></div></div>
<p>Correct until the budget fills, then frozen forever while true position climbs. Every token after eviction was rotated at the wrong position, and the error grew without bound.</p>
<p>I checked whether I'd caused it. <code>git stash</code>, rerun on <code>master</code>: identical <code>offset: 128</code>. <strong>Pre-existing</strong>, and exactly the limitation both docstrings had disclosed as "No RoPE position-ID remapping after eviction" — a line I'd written myself without understanding that it meant end-to-end generation was broken.</p>
<p>The fix is smaller than I expected, and the reason is worth stating. RoPE is <em>relative</em>: <code>&lt;rope(q,i), rope(k,j)&gt;</code> depends only on <code>i − j</code>. Q-Filters <strong>preserves</strong> original positions — it drops rows but never renumbers the survivors. So every stored key already carries the rotation for its true absolute position. Reporting the true position puts queries, new keys, and survivors back on one consistent axis, and no re-rotation is needed.</p>
<p>That's precisely why H2O and Keyformer need a delta-rotation pass in their apply kernels and this cache doesn't: they renumber positions on eviction, and Q-Filters doesn't.</p>
<div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">self</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">_true_offset </span><span class="token operator">+=</span><span class="token plain"> S</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">self</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">offset </span><span class="token operator">=</span><span class="token plain"> self</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">_true_offset</span><br></div></code></pre></div></div>
<p>Effect: <strong>ppl 598.5 → 17.6.</strong></p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="harness-bug-2-the-baseline-that-should-have-stopped-me">Harness bug 2: the baseline that should have stopped me<a href="https://veloxquant-mlx.netlify.app/docs/blog/qfilters-query-geometry#harness-bug-2-the-baseline-that-should-have-stopped-me" class="hash-link" aria-label="Direct link to Harness bug 2: the baseline that should have stopped me" title="Direct link to Harness bug 2: the baseline that should have stopped me" translate="no">​</a></h2>
<p>With RoPE fixed the numbers were finally sane — and now the <em>other</em> problem became visible. My fp16 baseline was <strong>1.240</strong>.</p>
<p>That number should have stopped me on day one. A real 1B model on ordinary prose scores somewhere around 4–15. I'd built the eval text as <code>'...short paragraph...' * 30</code>, which is trivially predictable: the model just learns the loop and echoes it. With the baseline flattened to 1.24 the dynamic range was gone, and every eviction policy looked equally catastrophic against it.</p>
<p>Swapping in continuous, non-repetitive prose:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">fp16 full cache (no eviction)      ppl     4.050    &lt;- realistic at last</span><br></div></code></pre></div></div>
<p>Both harness mistakes are now documented in the benchmark script's docstring, specifically so nobody repeats them — including me.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="run-3-the-measurement-that-was-blocked">Run 3: the measurement that was blocked<a href="https://veloxquant-mlx.netlify.app/docs/blog/qfilters-query-geometry#run-3-the-measurement-that-was-blocked" class="hash-link" aria-label="Direct link to Run 3: the measurement that was blocked" title="Direct link to Run 3: the measurement that was blocked" translate="no">​</a></h2>
<p>Now the comparison means something. 1024 tokens, generation mode, eviction active:</p>
<p><strong>Llama-3.2-1B</strong> — fp16 baseline <strong>4.050</strong></p>
<table><thead><tr><th>Budget</th><th>Calibrated</th><th>Key-SVD fallback</th><th>Gap to fp16 closed</th></tr></thead><tbody><tr><td>256 (~4×)</td><td><strong>8.476</strong></td><td>13.358</td><td><strong>52%</strong></td></tr><tr><td>128 (~8×)</td><td><strong>16.307</strong></td><td>23.645</td><td><strong>37%</strong></td></tr><tr><td>64 (~16×)</td><td><strong>25.933</strong></td><td>31.274</td><td><strong>20%</strong></td></tr></tbody></table>
<p><strong>Llama-3.2-3B</strong> — fp16 baseline <strong>3.305</strong></p>
<table><thead><tr><th>Budget</th><th>Calibrated</th><th>Key-SVD fallback</th><th>Gap to fp16 closed</th></tr></thead><tbody><tr><td>256 (~4×)</td><td><strong>5.076</strong></td><td>7.264</td><td><strong>55%</strong></td></tr><tr><td>128 (~8×)</td><td><strong>10.046</strong></td><td>14.909</td><td><strong>42%</strong></td></tr></tbody></table>
<p>Calibrated wins at every budget on both models, in the same order the correlation numbers predicted. Three independent measurements — anisotropy, attention correlation, generation perplexity — agreeing.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="the-finding-i-wasnt-looking-for">The finding I wasn't looking for<a href="https://veloxquant-mlx.netlify.app/docs/blog/qfilters-query-geometry#the-finding-i-wasnt-looking-for" class="hash-link" aria-label="Direct link to The finding I wasn't looking for" title="Direct link to The finding I wasn't looking for" translate="no">​</a></h2>
<p>While debugging, I tested one hypothesis that turned out to matter more than anything else in the perplexity table. Q-Filters as specified has no recency protection. But next-token prediction depends enormously on the <em>immediately preceding</em> tokens — and a long-range importance score will happily evict them.</p>
<p>Llama-3.2-1B, budget 128, calibrated, sweeping the trailing protected window:</p>
<table><thead><tr><th><code>qfilters_recent</code></th><th>0</th><th>32</th><th>64</th><th>96</th></tr></thead><tbody><tr><td>perplexity</td><td><strong>263.2</strong></td><td><strong>16.3</strong></td><td>20.3</td><td>24.9</td></tr></tbody></table>
<p>A <strong>16× swing</strong> from a parameter that defaults to zero. Everything in the table above depends on it being set.</p>
<p>This doesn't contradict the paper. Q-Filters is evaluated on Ruler and needle-in-a-haystack — <em>retrieval</em> tasks, where the answer sits somewhere in the middle of a long context and recency is not what you need. In open-ended generation the priority inverts. Projection ranking finds what matters globally; a recency window covers what matters locally; you need both.</p>
<p>I left the default at 0 so the out-of-box configuration stays paper-faithful, and documented ≈budget/4 for generation.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="what-im-not-claiming">What I'm not claiming<a href="https://veloxquant-mlx.netlify.app/docs/blog/qfilters-query-geometry#what-im-not-claiming" class="hash-link" aria-label="Direct link to What I'm not claiming" title="Direct link to What I'm not claiming" translate="no">​</a></h2>
<p>The honest boundary, because it's the part most easily overstated:</p>
<p><strong>These are not a reproduction of the paper's Figure 5.</strong> Perplexity is still well above fp16 at every ratio. My setup has uniform per-head budgets, no RoPE renumbering, and a much smaller calibration set than the paper's §4.2 (which uses 20 samples × 2048 tokens). These numbers compare <em>policies</em> against each other; they don't reproduce the paper's headline results.</p>
<p><strong>Not measured at all:</strong> TTFT and throughput (paper Figure 10), Ruler, needle-in-a-haystack, and any comparison against SnapKV, Expected Attention, or StreamingLLM.</p>
<p><strong>L2Norm is excluded from the tables</strong> — deliberately, not by oversight. It carries the identical un-fixed <code>offset</code> defect (<code>knorm_cache.py:165</code>), so its numbers would be dominated by position drift rather than eviction quality. Including it would have been an unfair fight in my favor. Generalizing the RoPE fix across the other evicting caches is the obvious next task, and probably affects H2O and TOVA too.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="what-id-take-away-from-this">What I'd take away from this<a href="https://veloxquant-mlx.netlify.app/docs/blog/qfilters-query-geometry#what-id-take-away-from-this" class="hash-link" aria-label="Direct link to What I'd take away from this" title="Direct link to What I'd take away from this" translate="no">​</a></h2>
<p><strong>A documented limitation is not a discharged one.</strong> I had written "no RoPE position-ID remapping after eviction" in two docstrings. I'd written <code>THE HONESTY CRUX</code> as a section header. Both were true, both were prominent, and neither told me the method was broken — because I'd never run it on a real model. Documentation records a decision; it doesn't validate one.</p>
<p><strong>Absurd numbers are information.</strong> Every genuinely useful step here came from refusing to accept an implausible result: ppl 4624 found the prefill bug, a baseline of 1.24 found the repetitive-text bug, and the calibrated arm underperforming found the RoPE bug. The temptation each time was to report the number with a caveat.</p>
<p><strong>Test the paper's premise, not just your code.</strong> My original test suite was thorough about mechanics — 27 tests, all passing, on a method that scored −0.032 against real attention. Synthetic tests confirm you implemented what you intended. They cannot tell you whether what you intended is what the paper meant.</p>
<p><strong>Sometimes the fix is one line.</strong> <code>self.offset = self._true_offset</code>, once I understood that RoPE is relative and Q-Filters preserves positions. Understanding the geometry made the code trivial; guessing at it would have produced a delta-rotation pass I didn't need.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="tldr">TL;DR<a href="https://veloxquant-mlx.netlify.app/docs/blog/qfilters-query-geometry#tldr" class="hash-link" aria-label="Direct link to TL;DR" title="Direct link to TL;DR" translate="no">​</a></h2>
<ul>
<li class="">My Q-Filters implementation derived its filter from <strong>key</strong> SVD instead of the paper's <strong>query</strong> SVD, discarding the <code>κʰ &gt; 0</code> term that fixes the sign</li>
<li class="">On real weights it scored <strong>−0.032</strong> Spearman against true attention, sign correct <strong>46%</strong> of the time — noise</li>
<li class="">The paper's anisotropy is real: Observation 3.1 held in <strong>1968 of 1968 heads</strong> across Llama-3.2-1B/3B and Qwen2.5-7B, including the paper's own stated limitation case</li>
<li class="">Properly calibrated: <strong>+0.783 / +0.863</strong> Spearman, 100% sign-correct, beating K-norm's +0.460 / +0.410</li>
<li class="">Generation perplexity, calibrated vs fallback: <strong>8.48 vs 13.36</strong> at 4×, <strong>16.31 vs 23.65</strong> at 8×, <strong>25.93 vs 31.27</strong> at 16× (1B); <strong>5.08 vs 7.26</strong> and <strong>10.05 vs 14.91</strong> (3B)</li>
<li class="">Found and fixed a pre-existing RoPE bug where <code>cache.offset</code> froze at the budget — position drift <strong>+135 by token 199</strong>, and <strong>ppl 598.5 → 17.6</strong> once fixed</li>
<li class=""><code>qfilters_recent</code> causes a <strong>16× perplexity swing</strong> in generation (263 → 16.3) and defaults to off; set ≈budget/4 if you generate text</li>
<li class="">Two Metal kernels for the eviction hot path, with a tie-handling rule that keeps the budget exact when protected rows all score <code>+inf</code></li>
<li class="">Two benchmark harnesses were discarded for producing confident, meaningless numbers; both pitfalls are documented in the script</li>
<li class="">Not claimed: Figure 5 reproduction, TTFT, Ruler, NIAH, or baselines against SnapKV/StreamingLLM</li>
</ul>
<p>Code, scripts, and raw numbers:
<a href="https://github.com/rajveer43/VeloxQuant-MLX" target="_blank" rel="noopener noreferrer" class="">github.com/rajveer43/VeloxQuant-MLX</a> — see <code>benchmark_scripts/qfilters_real_model_*.py</code> and <code>figures/qfilters/real_model_results.json</code>.</p>
<p>The paper: <a href="https://arxiv.org/abs/2503.02812" target="_blank" rel="noopener noreferrer" class="">Q-Filters: Leveraging Query-Key Geometry for Efficient KV Cache Compression</a>, Godey, Devoto, Zhao, Scardapane, Minervini, de la Clergerie, Sagot.</p>
<p>If you maintain a KV cache implementation with a documented limitation you've never load-bearing-tested, this is your sign to go run it on a real model.</p>]]></content>
        <author>
            <name>Rajveer Rathod</name>
            <uri>https://github.com/rajveer43</uri>
        </author>
        <category label="kv-cache" term="kv-cache"/>
        <category label="q-filters" term="q-filters"/>
        <category label="metal" term="metal"/>
        <category label="apple-silicon" term="apple-silicon"/>
        <category label="mlx" term="mlx"/>
        <category label="benchmarks" term="benchmarks"/>
    </entry>
    <entry>
        <title type="html"><![CDATA[A 5.65× Metal Kernel]]></title>
        <id>https://veloxquant-mlx.netlify.app/docs/blog/kivi-metal-kernel-honest-benchmark</id>
        <link href="https://veloxquant-mlx.netlify.app/docs/blog/kivi-metal-kernel-honest-benchmark"/>
        <updated>2026-08-12T00:00:00.000Z</updated>
        <summary type="html"><![CDATA[How I fused KIVI's KV-cache quantization into two bit-exact Metal kernels, measured a 1.40×–5.65× op-level speedup, watched it vanish completely end-to-end — and the four separate times my own benchmarks gave me confidently wrong answers along the way.]]></summary>
        <content type="html"><![CDATA[<p><em>How I fused KIVI's KV-cache quantization into two bit-exact Metal kernels, measured a 1.40×–5.65× op-level speedup, watched it vanish completely end-to-end — and the four separate times my own benchmarks gave me confidently wrong answers along the way.</em></p>
<hr>
<p>There's a particular satisfaction in watching a GPU kernel you wrote beat the framework's version by 5×. There's a different feeling entirely when you plug it into the actual model and the tokens-per-second doesn't move at all.</p>
<p>This post is about both, and about the part in between: <strong>four occasions where my benchmarks confidently told me something false.</strong> One said the kernel was 11× <em>slower</em> than baseline. One said it was 28× faster. One said quantization consumed 97.83% of prefill time, when the real figure is about 1–2%. One showed a clean 127 MB memory saving that turned out to be nothing at all.</p>
<p>All four were my fault. All four produced a clean-looking number with a plausible story attached, which is exactly what made them dangerous.</p>
<p>The kernel is real, it's bit-exact, it ships, and I'd merge it again. But the honest headline is the one above, and the useful content is why both halves of it are true at the same time.</p>
<p>This is the long version, with the complete experimental record — every measurement generation, including the ones that were wrong.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="what-were-actually-optimizing">What we're actually optimizing<a href="https://veloxquant-mlx.netlify.app/docs/blog/kivi-metal-kernel-honest-benchmark#what-were-actually-optimizing" class="hash-link" aria-label="Direct link to What we're actually optimizing" title="Direct link to What we're actually optimizing" translate="no">​</a></h2>
<p>If you run a language model locally, the thing that eventually stops you isn't compute. It's the KV cache.</p>
<p>Every token the model has seen leaves behind a key and a value vector in every attention layer. The model needs them to attend to the past, so they stay resident for the whole generation. The cache grows linearly with context — and unlike model weights, which you load once, it grows <em>while you're using it</em>.</p>
<p>On a 7B model at 32k context that's several gigabytes, comparable to the quantized weights themselves. On a Mac with unified memory, it's the difference between a long conversation working and your machine swapping itself to death.</p>
<p><a class="" href="https://veloxquant-mlx.netlify.app/docs/algorithms/kivi">KIVI</a> (Liu et al., ICML 2024) is one answer, and it's the baseline every other algorithm in this library gets measured against. The insight is that keys and values want to be quantized along <em>different axes</em>:</p>
<ul>
<li class=""><strong>Keys</strong> are quantized <strong>per-channel</strong> — each channel gets its own scale, computed across a group of tokens.</li>
<li class=""><strong>Values</strong> are quantized <strong>per-token</strong> — each token gets its own scale, computed across a group of channels.</li>
</ul>
<p>Why asymmetric? Key tensors have a few channels with consistently huge magnitudes. Quantize per-token and those outliers blow up the scale for every other channel sharing the group. Value tensors lack that structure, and per-token suits them better.</p>
<p>A third piece matters for everything that follows: the most recent <code>residual_length</code> tokens stay in fp16. They're what attention weights most heavily, and they're also the tokens whose group isn't full yet. Once enough fresh tokens accumulate, they get quantized as a batch and folded into the compressed store. <strong>That batching event is a flush</strong>, and it is the operation this entire post is about.</p>
<p>The quantization itself is textbook asymmetric min/max:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">zero  = min(group)</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">scale = (max(group) - min(group)) / (2^bits - 1)</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">q     = round((x - zero) / scale)</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">recon = q * scale + zero</span><br></div></code></pre></div></div>
<p>In MLX this is roughly eight array operations: reshape to expose the group axis, <code>min</code>, <code>max</code>, subtract, divide, <code>round</code>, <code>clip</code>, multiply, add, reshape back.</p>
<p>Eight operations means eight kernel launches and — the expensive part — <strong>eight round trips to memory</strong>. Every intermediate is materialized. The quantized codes, which never needed to exist as a full-size tensor, get written to RAM and read straight back.</p>
<p>That's the target. One fused kernel, one pass, no intermediates.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="the-kernel">The kernel<a href="https://veloxquant-mlx.netlify.app/docs/blog/kivi-metal-kernel-honest-benchmark#the-kernel" class="hash-link" aria-label="Direct link to The kernel" title="Direct link to The kernel" translate="no">​</a></h2>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="the-layout-problem-that-shapes-everything">The layout problem that shapes everything<a href="https://veloxquant-mlx.netlify.app/docs/blog/kivi-metal-kernel-honest-benchmark#the-layout-problem-that-shapes-everything" class="hash-link" aria-label="Direct link to The layout problem that shapes everything" title="Direct link to The layout problem that shapes everything" translate="no">​</a></h3>
<p>KV tensors are <code>[batch, heads, seq, head_dim]</code>, row-contiguous. Flatten batch and heads and you get <code>[BH, S, D]</code>, where element <code>(bh, s, d)</code> sits at <code>bh*S*D + s*D + d</code>.</p>
<p>Which means the two modes face opposite problems:</p>
<ul>
<li class=""><strong>Values (per-token)</strong> — the group runs along <code>D</code>, the <em>contiguous</em> axis. Adjacent elements in a group are adjacent in memory.</li>
<li class=""><strong>Keys (per-channel)</strong> — the group runs along <code>S</code>, the <em>strided</em> axis. Adjacent elements are <code>D</code> floats apart. Typically 128.</li>
</ul>
<p>My first version was one kernel handling both, which meant transposing the key tensor so the token axis became contiguous, then reusing the same code path.</p>
<p>That transpose was the whole problem. It's a full-size materializing copy — exactly the memory traffic the kernel exists to eliminate. I'd removed eight round trips and added one large one back. On the key path, the "optimized" kernel was a net loss.</p>
<p>So I threw it out and wrote two kernels, one per layout.</p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="kernel-a--per-channel-keys-one-thread-one-whole-group">Kernel A — per-channel keys: one thread, one whole group<a href="https://veloxquant-mlx.netlify.app/docs/blog/kivi-metal-kernel-honest-benchmark#kernel-a--per-channel-keys-one-thread-one-whole-group" class="hash-link" aria-label="Direct link to Kernel A — per-channel keys: one thread, one whole group" title="Direct link to Kernel A — per-channel keys: one thread, one whole group" translate="no">​</a></h3>
<p>The trick is almost aggressively simple: <strong>give each thread an entire group, and don't reduce at all.</strong></p>
<div class="language-cpp codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-cpp codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">uint tid </span><span class="token operator">=</span><span class="token plain"> thread_position_in_grid</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">x</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">const</span><span class="token plain"> uint BH </span><span class="token operator">=</span><span class="token plain"> x_shape</span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token number">0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">const</span><span class="token plain"> uint S  </span><span class="token operator">=</span><span class="token plain"> x_shape</span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token number">1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">const</span><span class="token plain"> uint NG </span><span class="token operator">=</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">S </span><span class="token operator">+</span><span class="token plain"> GROUP_SIZE </span><span class="token operator">-</span><span class="token plain"> </span><span class="token number">1u</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token operator">/</span><span class="token plain"> GROUP_SIZE</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain">   </span><span class="token comment" style="color:rgb(98, 114, 164)">// token groups</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">if</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">tid </span><span class="token operator">&gt;=</span><span class="token plain"> BH </span><span class="token operator">*</span><span class="token plain"> NG </span><span class="token operator">*</span><span class="token plain"> DHEAD</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">return</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">const</span><span class="token plain"> uint d   </span><span class="token operator">=</span><span class="token plain"> tid </span><span class="token operator">%</span><span class="token plain"> DHEAD</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">const</span><span class="token plain"> uint r   </span><span class="token operator">=</span><span class="token plain"> tid </span><span class="token operator">/</span><span class="token plain"> DHEAD</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">const</span><span class="token plain"> uint grp </span><span class="token operator">=</span><span class="token plain"> r </span><span class="token operator">%</span><span class="token plain"> NG</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">const</span><span class="token plain"> uint bh  </span><span class="token operator">=</span><span class="token plain"> r </span><span class="token operator">/</span><span class="token plain"> NG</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">const</span><span class="token plain"> uint base </span><span class="token operator">=</span><span class="token plain"> bh </span><span class="token operator">*</span><span class="token plain"> S </span><span class="token operator">*</span><span class="token plain"> DHEAD </span><span class="token operator">+</span><span class="token plain"> d</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">const</span><span class="token plain"> uint s0   </span><span class="token operator">=</span><span class="token plain"> grp </span><span class="token operator">*</span><span class="token plain"> GROUP_SIZE</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">const</span><span class="token plain"> uint s1   </span><span class="token operator">=</span><span class="token plain"> </span><span class="token function" style="color:rgb(80, 250, 123)">min</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">s0 </span><span class="token operator">+</span><span class="token plain"> GROUP_SIZE</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> S</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">float</span><span class="token plain"> gmin </span><span class="token operator">=</span><span class="token plain">  INFINITY</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">float</span><span class="token plain"> gmax </span><span class="token operator">=</span><span class="token plain"> </span><span class="token operator">-</span><span class="token plain">INFINITY</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">for</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">uint s </span><span class="token operator">=</span><span class="token plain"> s0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"> s </span><span class="token operator">&lt;</span><span class="token plain"> s1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"> </span><span class="token operator">++</span><span class="token plain">s</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">float</span><span class="token plain"> v </span><span class="token operator">=</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">float</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">x</span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token plain">base </span><span class="token operator">+</span><span class="token plain"> s </span><span class="token operator">*</span><span class="token plain"> DHEAD</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    gmin </span><span class="token operator">=</span><span class="token plain"> </span><span class="token function" style="color:rgb(80, 250, 123)">min</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">gmin</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> v</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    gmax </span><span class="token operator">=</span><span class="token plain"> </span><span class="token function" style="color:rgb(80, 250, 123)">max</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">gmax</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> v</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><br></div></code></pre></div></div>
<p>Each thread strides by <code>DHEAD</code> — 128 floats between consecutive reads. In isolation that looks like the worst access pattern available.</p>
<p>But look at the indexing. <code>d = tid % DHEAD</code> means <strong>consecutive threads take consecutive channels</strong>. At any step of that loop, the 32 threads in a SIMD group read 32 <em>adjacent</em> addresses. The warp's access is fully coalesced. The stride is per-thread; the warp moves through memory as a solid block.</p>
<p>And because a thread owns its group outright, there is <strong>no cross-thread reduction</strong>. No threadgroup memory, no barriers, no butterfly, no transpose. The strided-looking layout turned out to need the least machinery.</p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="kernel-b--per-token-values-one-simd-group-one-quantization-group">Kernel B — per-token values: one SIMD group, one quantization group<a href="https://veloxquant-mlx.netlify.app/docs/blog/kivi-metal-kernel-honest-benchmark#kernel-b--per-token-values-one-simd-group-one-quantization-group" class="hash-link" aria-label="Direct link to Kernel B — per-token values: one SIMD group, one quantization group" title="Direct link to Kernel B — per-token values: one SIMD group, one quantization group" translate="no">​</a></h3>
<p>The contiguous axis wants the mirror image. Lanes split a single group and cooperate:</p>
<div class="language-cpp codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-cpp codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">uint lane </span><span class="token operator">=</span><span class="token plain"> thread_position_in_threadgroup</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">x</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">uint gid  </span><span class="token operator">=</span><span class="token plain"> threadgroup_position_in_grid</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">x</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token comment" style="color:rgb(98, 114, 164)">// Whole threadgroups exit together, so every lane still reaches the</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token comment" style="color:rgb(98, 114, 164)">// butterfly below — a divergent return would deadlock the shuffle.</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">if</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">gid </span><span class="token operator">&gt;=</span><span class="token plain"> x_shape</span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token number">0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token plain"> </span><span class="token operator">*</span><span class="token plain"> S </span><span class="token operator">*</span><span class="token plain"> NGD</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">return</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">float</span><span class="token plain"> gmin </span><span class="token operator">=</span><span class="token plain">  INFINITY</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">float</span><span class="token plain"> gmax </span><span class="token operator">=</span><span class="token plain"> </span><span class="token operator">-</span><span class="token plain">INFINITY</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">for</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">uint i </span><span class="token operator">=</span><span class="token plain"> lane</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"> i </span><span class="token operator">&lt;</span><span class="token plain"> GROUP_SIZE</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"> i </span><span class="token operator">+=</span><span class="token plain"> </span><span class="token number">32u</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    uint d </span><span class="token operator">=</span><span class="token plain"> d0 </span><span class="token operator">+</span><span class="token plain"> i</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">if</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">d </span><span class="token operator">&lt;</span><span class="token plain"> d1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">float</span><span class="token plain"> v </span><span class="token operator">=</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">float</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">x</span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token plain">row_base </span><span class="token operator">+</span><span class="token plain"> d</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        gmin </span><span class="token operator">=</span><span class="token plain"> </span><span class="token function" style="color:rgb(80, 250, 123)">min</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">gmin</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> v</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        gmax </span><span class="token operator">=</span><span class="token plain"> </span><span class="token function" style="color:rgb(80, 250, 123)">max</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">gmax</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> v</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token comment" style="color:rgb(98, 114, 164)">// Butterfly: after 5 XOR shuffles every lane holds the group-wide min/max.</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">for</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">uint off </span><span class="token operator">=</span><span class="token plain"> </span><span class="token number">16u</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"> off </span><span class="token operator">&gt;</span><span class="token plain"> </span><span class="token number">0u</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"> off </span><span class="token operator">&gt;&gt;=</span><span class="token plain"> </span><span class="token number">1u</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    gmin </span><span class="token operator">=</span><span class="token plain"> </span><span class="token function" style="color:rgb(80, 250, 123)">min</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">gmin</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token function" style="color:rgb(80, 250, 123)">simd_shuffle_xor</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">gmin</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> off</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    gmax </span><span class="token operator">=</span><span class="token plain"> </span><span class="token function" style="color:rgb(80, 250, 123)">max</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">gmax</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token function" style="color:rgb(80, 250, 123)">simd_shuffle_xor</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">gmax</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> off</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><br></div></code></pre></div></div>
<p>The <code>simd_shuffle_xor</code> butterfly is the nice part. Five shuffles reduce 32 lanes to a min/max that <em>every lane already holds</em> — no broadcast step. And because lanes advance in lockstep, it needs <strong>no threadgroup memory and no barriers</strong>, unlike a tree reduction.</p>
<p>Note the comment on the bounds check. That <code>return</code> must be uniform across the threadgroup. If individual lanes bailed early, the survivors would shuffle against threads that no longer exist and the reduction would hang or return garbage. Whole threadgroups exit together, so every lane reaching the butterfly reaches it with all 32 partners intact.</p>
<p>At KIVI's default <code>GROUP_SIZE=32</code> this is an exact fit: one lane per element, one butterfly, done.</p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="two-optimizations-i-was-sure-would-work-and-didnt">Two optimizations I was sure would work, and didn't<a href="https://veloxquant-mlx.netlify.app/docs/blog/kivi-metal-kernel-honest-benchmark#two-optimizations-i-was-sure-would-work-and-didnt" class="hash-link" aria-label="Direct link to Two optimizations I was sure would work, and didn't" title="Direct link to Two optimizations I was sure would work, and didn't" translate="no">​</a></h3>
<p>I assumed caching each group in registers would help — read once, use twice, skip the second global load. Controlled same-process A/B said otherwise:</p>
<table><thead><tr><th>kernel</th><th>register caching</th><th>result</th></tr></thead><tbody><tr><td>per-channel (keys)</td><td>group in registers</td><td><strong>0.76×</strong> at S=2048 — actively harmful</td></tr><tr><td>per-token (values)</td><td>group in registers</td><td><strong>1.00×–1.02×</strong> — exactly neutral</td></tr></tbody></table>
<p>In the channel kernel, a whole group is <code>GROUP_SIZE</code> floats <em>per thread</em>. The occupancy that costs outweighs the saved loads, which were hitting cache anyway. In the token kernel it's split across 32 lanes, so it's cheap — and buys nothing, for the same cache reason.</p>
<p>Both reverted, and the <code>REG_SLOTS</code> machinery deleted. I also swept threadgroup width and found the defaults (256 for channel, 32 for token) already optimal.</p>
<div class="theme-admonition theme-admonition-tip admonition_xJq3 alert alert--success"><div class="admonitionHeading_Gvgb"><span class="admonitionIcon_Rf37"><svg viewBox="0 0 12 16"><path fill-rule="evenodd" d="M6.5 0C3.48 0 1 2.19 1 5c0 .92.55 2.25 1 3 1.34 2.25 1.78 2.78 2 4v1h5v-1c.22-1.22.66-1.75 2-4 .45-.75 1-2.08 1-3 0-2.81-2.48-5-5.5-5zm3.64 7.48c-.25.44-.47.8-.67 1.11-.86 1.41-1.25 2.06-1.45 3.23-.02.05-.02.11-.02.17H5c0-.06 0-.13-.02-.17-.2-1.17-.59-1.83-1.45-3.23-.2-.31-.42-.67-.67-1.11C2.44 6.78 2 5.65 2 5c0-2.2 2.02-4 4.5-4 1.22 0 2.36.42 3.22 1.19C10.55 2.94 11 3.94 11 5c0 .66-.44 1.78-.86 2.48zM4 14h5c-.23 1.14-1.3 2-2.5 2s-2.27-.86-2.5-2z"></path></svg></span>The pattern</div><div class="admonitionContent_BuS1"><p>Two hypotheses, both plausible, both wrong, both settled in about twenty minutes by a controlled A/B rather than by argument. The measurement was cheaper than the reasoning.</p></div></div>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="three-ways-to-be-off-by-one-bit">Three ways to be off by one bit<a href="https://veloxquant-mlx.netlify.app/docs/blog/kivi-metal-kernel-honest-benchmark#three-ways-to-be-off-by-one-bit" class="hash-link" aria-label="Direct link to Three ways to be off by one bit" title="Direct link to Three ways to be off by one bit" translate="no">​</a></h2>
<p>My acceptance criterion was bit-exactness against the MLX reference — not "close enough," but <em>identical output</em>. That turned out to be the most instructive constraint in the project, because it surfaced three failure modes a tolerance test sails straight past.</p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="1-fma-contraction">1. FMA contraction<a href="https://veloxquant-mlx.netlify.app/docs/blog/kivi-metal-kernel-honest-benchmark#1-fma-contraction" class="hash-link" aria-label="Direct link to 1. FMA contraction" title="Direct link to 1. FMA contraction" translate="no">​</a></h3>
<p>My first parity run failed on <strong>192 of 300 configurations</strong> — and every failure was off by exactly 1 ULP. That uniformity is a fingerprint: not an algorithm bug, a rounding difference.</p>
<p>The culprit was <code>q * scale + gmin</code>. Metal's compiler sees a multiply feeding an add and contracts it into a fused multiply-add: one instruction, one rounding. MLX does them separately, with two roundings. Same math, different result on ~0.02% of elements.</p>
<p>The fix is to break the pattern the optimizer looks for:</p>
<div class="language-cpp codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-cpp codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">float</span><span class="token plain"> prod </span><span class="token operator">=</span><span class="token plain"> q </span><span class="token operator">*</span><span class="token plain"> scale</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain">      </span><span class="token comment" style="color:rgb(98, 114, 164)">// NOT an fma</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">out</span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token plain">base </span><span class="token operator">+</span><span class="token plain"> s </span><span class="token operator">*</span><span class="token plain"> DHEAD</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token plain"> </span><span class="token operator">=</span><span class="token plain"> </span><span class="token function" style="color:rgb(80, 250, 123)">T</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">prod </span><span class="token operator">+</span><span class="token plain"> gmin</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><br></div></code></pre></div></div>
<p>The irony is that the fused version is <em>more</em> accurate — it carries more intermediate precision. But the contract is parity with the reference, not maximum accuracy, so the less accurate version is the correct one. Uncomfortable sentence; right call. 300/300 after the fix.</p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="2-rounding-mode">2. Rounding mode<a href="https://veloxquant-mlx.netlify.app/docs/blog/kivi-metal-kernel-honest-benchmark#2-rounding-mode" class="hash-link" aria-label="Direct link to 2. Rounding mode" title="Direct link to 2. Rounding mode" translate="no">​</a></h3>
<p>Metal's <code>round()</code> is half-away-from-zero. <code>mx.round</code> is half-to-even. They agree on everything except exact <code>.5</code> codes — rare in random data, and <em>systematically common</em> in real quantization, because uniform grids produce exact midpoints. The fix is <code>rint()</code>. There's a test pinning it with inputs hand-built to land on <code>.5</code>.</p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="3-padding-semantics">3. Padding semantics<a href="https://veloxquant-mlx.netlify.app/docs/blog/kivi-metal-kernel-honest-benchmark#3-padding-semantics" class="hash-link" aria-label="Direct link to 3. Padding semantics" title="Direct link to 3. Padding semantics" translate="no">​</a></h3>
<p>When a group doesn't divide evenly, MLX pads the tail by <strong>replicating the edge value</strong>, <code>x[..., -1:]</code>, not with zeros. Pad with zeros and you've silently dragged <code>gmin</code> to 0 for every ragged group, corrupting the scale. Since every pad slot holds that same value, folding it in once is equivalent to looping:</p>
<div class="language-cpp codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-cpp codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">if</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">s1 </span><span class="token operator">&lt;</span><span class="token plain"> s0 </span><span class="token operator">+</span><span class="token plain"> GROUP_SIZE</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">float</span><span class="token plain"> pad_val </span><span class="token operator">=</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">float</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">x</span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token plain">base </span><span class="token operator">+</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">S </span><span class="token operator">-</span><span class="token plain"> </span><span class="token number">1u</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token operator">*</span><span class="token plain"> DHEAD</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    gmin </span><span class="token operator">=</span><span class="token plain"> </span><span class="token function" style="color:rgb(80, 250, 123)">min</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">gmin</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> pad_val</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    gmax </span><span class="token operator">=</span><span class="token plain"> </span><span class="token function" style="color:rgb(80, 250, 123)">max</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">gmax</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> pad_val</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><br></div></code></pre></div></div>
<p>All three have dedicated regression tests now. They're the kind of bug that produces <em>plausible</em> output — slightly different, never obviously broken. Without bit-exactness as the bar, all three would have shipped.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="the-bug-that-made-generation-hang-forever">The bug that made generation hang forever<a href="https://veloxquant-mlx.netlify.app/docs/blog/kivi-metal-kernel-honest-benchmark#the-bug-that-made-generation-hang-forever" class="hash-link" aria-label="Direct link to The bug that made generation hang forever" title="Direct link to The bug that made generation hang forever" translate="no">​</a></h2>
<p>My favourite failure of the project, because the fix made the code simpler and the symptom was so much worse than the cause.</p>
<p><code>mx.fast.metal_kernel</code> JIT-compiles from source. I was passing shape constants through the header as <code>#define</code>s, which lets the compiler turn <code>tid % DHEAD</code> into a shift-and-mask instead of integer division. Good idea for <code>DHEAD</code> — it's the model's head dimension, fixed for the life of a cache.</p>
<p>I did the same for the sequence length.</p>
<p>The sequence length <strong>grows by one every decode step</strong>. So every token triggered a fresh shader compilation. Generation didn't crash and didn't error — it just stopped. I killed the process after several minutes with no output.</p>
<p>The fix was to pass shape as a runtime buffer. MLX provides <code>x_shape</code> for free on every input, so this meant <em>deleting</em> code, not adding it.</p>
<div class="theme-admonition theme-admonition-danger admonition_xJq3 alert alert--danger"><div class="admonitionHeading_Gvgb"><span class="admonitionIcon_Rf37"><svg viewBox="0 0 12 16"><path fill-rule="evenodd" d="M5.05.31c.81 2.17.41 3.38-.52 4.31C3.55 5.67 1.98 6.45.9 7.98c-1.45 2.05-1.7 6.53 3.53 7.7-2.2-1.16-2.67-4.52-.3-6.61-.61 2.03.53 3.33 1.94 2.86 1.39-.47 2.3.53 2.27 1.67-.02.78-.31 1.44-1.13 1.81 3.42-.59 4.78-3.42 4.78-5.56 0-2.84-2.53-3.22-1.25-5.61-1.52.13-2.03 1.13-1.89 2.75.09 1.08-1.02 1.8-1.86 1.33-.67-.41-.66-1.19-.06-1.78C8.18 5.31 8.68 2.45 5.05.32L5.03.3l.02.01z"></path></svg></span>Before → after</div><div class="admonitionContent_BuS1"><p><strong>Before:</strong> hung indefinitely (killed after minutes)
<strong>After:</strong> 1.0 second</p><p>Guarded now by a test that runs 55 sequence lengths through both kernels and asserts the dispatch cache holds exactly <strong>2</strong> entries — not 110. That test isn't checking performance. It's checking that one specific catastrophic bug can't come back.</p></div></div>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="four-benchmarks-that-lied">Four benchmarks that lied<a href="https://veloxquant-mlx.netlify.app/docs/blog/kivi-metal-kernel-honest-benchmark#four-benchmarks-that-lied" class="hash-link" aria-label="Direct link to Four benchmarks that lied" title="Direct link to Four benchmarks that lied" translate="no">​</a></h2>
<p>Here the post stops being about GPU programming and starts being about measurement, which is the part I'd actually want to read.</p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="lie-1--quantization-is-9783-of-prefill">Lie #1 — "Quantization is 97.83% of prefill"<a href="https://veloxquant-mlx.netlify.app/docs/blog/kivi-metal-kernel-honest-benchmark#lie-1--quantization-is-9783-of-prefill" class="hash-link" aria-label="Direct link to Lie #1 — &quot;Quantization is 97.83% of prefill&quot;" title="Direct link to Lie #1 — &quot;Quantization is 97.83% of prefill&quot;" translate="no">​</a></h3>
<p>I wanted to know how much runtime quantization accounted for, so I instrumented the call: timer before, timer after, <code>mx.eval()</code> in between to force the computation. Here's the raw output:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain"># mlx-community/Llama-3.2-3B-Instruct-4bit  layers=28</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">PREFILL-dominated (8k prompt, 4 new tokens):</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  prefill  metal=False  wall= 20.754s  quant= 20303.3ms (97.83% of wall)  calls=224  [keys 19483.2ms / values  820.0ms]</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  prefill  metal=True   wall= 20.940s  quant= 20489.1ms (97.85% of wall)  calls=224  [keys 19805.5ms / values  683.6ms]</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">DECODE-dominated (2k prompt, 240 new tokens):</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  decode   metal=False  wall= 10.114s  quant=  4829.6ms (47.75% of wall)  calls=504  [keys  4550.0ms / values  279.6ms]</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  decode   metal=True   wall=  9.952s  quant=  4682.5ms (47.05% of wall)  calls=504  [keys  4457.8ms / values  224.7ms]</span><br></div></code></pre></div></div>
<p>Quantization was apparently the entire bottleneck. It was nonsense, and the mistake is in the description above: <strong><code>mx.eval()</code> inside the measured region.</strong></p>
<p>MLX is lazily evaluated. Operations build a graph; nothing computes until something forces it. By calling <code>eval()</code> inside my timer I wasn't measuring quantization — I was measuring <em>every pending operation in the graph at that moment</em>: attention, the MLP, the whole layer stack, all attributed to the one function that happened to trigger the flush.</p>
<div class="theme-admonition theme-admonition-warning admonition_xJq3 alert alert--warning"><div class="admonitionHeading_Gvgb"><span class="admonitionIcon_Rf37"><svg viewBox="0 0 16 16"><path fill-rule="evenodd" d="M8.893 1.5c-.183-.31-.52-.5-.887-.5s-.703.19-.886.5L.138 13.499a.98.98 0 0 0 0 1.001c.193.31.53.501.886.501h13.964c.367 0 .704-.19.877-.5a1.03 1.03 0 0 0 .01-1.002L8.893 1.5zm.133 11.497H6.987v-2.003h2.039v2.003zm0-3.004H6.987V5.987h2.039v4.006z"></path></svg></span>Claimed vs actual</div><div class="admonitionContent_BuS1"><p><strong>Claimed:</strong> 97.83% of prefill
<strong>Actual:</strong> ~1–2% of prefill</p><p>Off by roughly <strong>fifty times</strong>, in the flattering direction, and it looked entirely plausible.</p></div></div>
<p>Notice too that the numbers are self-refuting if you read them properly: <code>metal=False</code> and <code>metal=True</code> report <em>the same</em> 97.8% share. A measurement that can't distinguish the two arms is measuring something other than the thing you changed.</p>
<p>The one genuinely useful output was incidental: <code>calls=224</code> across 28 layers on an 8k prompt means 8 flushes per layer — which revealed that <strong>mlx_lm chunks prefill at 2048 tokens.</strong> That number matters later.</p>
<div class="theme-admonition theme-admonition-tip admonition_xJq3 alert alert--success"><div class="admonitionHeading_Gvgb"><span class="admonitionIcon_Rf37"><svg viewBox="0 0 12 16"><path fill-rule="evenodd" d="M6.5 0C3.48 0 1 2.19 1 5c0 .92.55 2.25 1 3 1.34 2.25 1.78 2.78 2 4v1h5v-1c.22-1.22.66-1.75 2-4 .45-.75 1-2.08 1-3 0-2.81-2.48-5-5.5-5zm3.64 7.48c-.25.44-.47.8-.67 1.11-.86 1.41-1.25 2.06-1.45 3.23-.02.05-.02.11-.02.17H5c0-.06 0-.13-.02-.17-.2-1.17-.59-1.83-1.45-3.23-.2-.31-.42-.67-.67-1.11C2.44 6.78 2 5.65 2 5c0-2.2 2.02-4 4.5-4 1.22 0 2.36.42 3.22 1.19C10.55 2.94 11 3.94 11 5c0 .66-.44 1.78-.86 2.48zM4 14h5c-.23 1.14-1.3 2-2.5 2s-2.27-.86-2.5-2z"></path></svg></span>Lesson</div><div class="admonitionContent_BuS1"><p>In a lazy framework, a synchronization point inside your timer measures everything the framework was putting off. Force the graph to a known state <em>before</em> you start the clock.</p></div></div>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="lie-2--009-and-2808">Lie #2 — "0.09× and 28.08×"<a href="https://veloxquant-mlx.netlify.app/docs/blog/kivi-metal-kernel-honest-benchmark#lie-2--009-and-2808" class="hash-link" aria-label="Direct link to Lie #2 — &quot;0.09× and 28.08×&quot;" title="Direct link to Lie #2 — &quot;0.09× and 28.08×&quot;" translate="no">​</a></h3>
<p>Fresh microbenchmark, both flush sizes:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">per-flush cost (keys+values, H=8 D=128, 28 layers)</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">     S    off ms     on ms  speedup |  x28 layers off        on     saved</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    32    1.1334   12.4505    0.09x |           31.7ms    348.6ms   -316.9ms</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  2048   19.8718    0.7076   28.08x |          556.4ms     19.8ms    536.6ms</span><br></div></code></pre></div></div>
<p>Eleven times <em>slower</em> at small sizes, twenty-eight times <em>faster</em> at large ones. I nearly wrote a whole section theorizing about launch-overhead crossovers — there's a tidy story available where fixed dispatch cost dominates at S=32 and bandwidth savings dominate at S=2048.</p>
<p>The story was fiction. <strong>I had left an LLM benchmark running in the background on the same GPU.</strong></p>
<p>Same benchmark, idle GPU:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">     S    off ms     on ms  speedup |  x28 layers off        on     saved</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    32    0.5050    0.3601    1.40x |           14.1ms     10.1ms      4.1ms</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  2048    3.8337    0.6785    5.65x |          107.3ms     19.0ms     88.3ms</span><br></div></code></pre></div></div>
<p>Both processes were fighting for the same hardware, and contention landed unevenly across runs. These weren't noisy-around-the-truth — off by <strong>15× in one direction and 5× in the other</strong>, <em>and they looked like a coherent narrative.</em> That's the dangerous part. Random noise looks random. Contention produces confident, structured, wrong answers.</p>
<p>The downstream damage is worth showing, because the wrong numbers propagated into a wrong <em>prediction</em>:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">--- predicted end-to-end (from the CONTENDED numbers) ---</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">PREFILL 8k prompt: 4 chunks x 537ms saved = 2146ms of ~21000ms  -&gt;  10.2% faster</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">DECODE 240 tokens: 7 flushes x -316.9ms   = -2218ms of ~10000ms -&gt; -22.18% "faster"</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">--- predicted end-to-end (from the IDLE numbers) ---</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">PREFILL 8k prompt: 4 chunks x 88ms saved  =  353ms of ~21000ms  -&gt;   1.7% faster</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">DECODE 240 tokens: 7 flushes x 4.1ms      =   28ms of ~10000ms  -&gt;   0.28% faster</span><br></div></code></pre></div></div>
<p>A predicted 10.2% prefill win and a 22% decode <em>regression</em>, versus the truth of +1.7% and +0.28%. Had I stopped there, I'd have gone hunting for a decode regression that never existed.</p>
<div class="theme-admonition theme-admonition-tip admonition_xJq3 alert alert--success"><div class="admonitionHeading_Gvgb"><span class="admonitionIcon_Rf37"><svg viewBox="0 0 12 16"><path fill-rule="evenodd" d="M6.5 0C3.48 0 1 2.19 1 5c0 .92.55 2.25 1 3 1.34 2.25 1.78 2.78 2 4v1h5v-1c.22-1.22.66-1.75 2-4 .45-.75 1-2.08 1-3 0-2.81-2.48-5-5.5-5zm3.64 7.48c-.25.44-.47.8-.67 1.11-.86 1.41-1.25 2.06-1.45 3.23-.02.05-.02.11-.02.17H5c0-.06 0-.13-.02-.17-.2-1.17-.59-1.83-1.45-3.23-.2-.31-.42-.67-.67-1.11C2.44 6.78 2 5.65 2 5c0-2.2 2.02-4 4.5-4 1.22 0 2.36.42 3.22 1.19C10.55 2.94 11 3.94 11 5c0 .66-.44 1.78-.86 2.48zM4 14h5c-.23 1.14-1.3 2-2.5 2s-2.27-.86-2.5-2z"></path></svg></span>Lesson</div><div class="admonitionContent_BuS1"><p>A GPU is one resource. Check what else is running — and be most suspicious when a surprising result arrives with a satisfying explanation already attached.</p></div></div>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="lie-3--the-benchmark-that-gave-four-different-answers">Lie #3 — the benchmark that gave four different answers<a href="https://veloxquant-mlx.netlify.app/docs/blog/kivi-metal-kernel-honest-benchmark#lie-3--the-benchmark-that-gave-four-different-answers" class="hash-link" aria-label="Direct link to Lie #3 — the benchmark that gave four different answers" title="Direct link to Lie #3 — the benchmark that gave four different answers" translate="no">​</a></h3>
<p>Subtler, and I think the most broadly applicable. For the same kernel and the same configuration, my attempts produced <strong>0.31×, 1.0×, 2.16×, and 3.4×</strong>. Not scatter around a value — four different conclusions, each internally consistent.</p>
<p>The root cause: I was calling the function repeatedly <strong>on the same input tensor</strong>.</p>
<p>MLX can recognize it has already computed something and reuse the result. The reference path — eight standard, individually cacheable array ops — benefits enormously. My custom kernel benefits far less. So the "baseline" was quietly handed a shortcut the kernel couldn't take, and every extra repetition widened the gap. Layering best-of-N on top amplified it further, because best-of-N systematically selects the run where caching helped most.</p>
<p>I got this badly wrong. <strong>I concluded the kernel was slower, set the feature flag off, and wrote that conclusion into the code and the tests.</strong> It was only when two independently-designed methods disagreed with me that I went back:</p>
<ol>
<li class=""><strong>Interleaved A/B</strong> — alternate on/off inside one process, so drift and thermal state hit both arms equally.</li>
<li class=""><strong>Rotating input pool</strong> — cycle 20 distinct tensors so nothing can be reused.</li>
</ol>
<p>Both landed at <strong>1.36×–2.14×</strong>, agreeing with each other and disagreeing with me. I reverted the flag and corrected the tests.</p>
<p>The benchmark that ships in <code>test_kivi_quant.py</code> now does both, and its docstring explains both traps so the next person doesn't re-derive them.</p>
<div class="theme-admonition theme-admonition-tip admonition_xJq3 alert alert--success"><div class="admonitionHeading_Gvgb"><span class="admonitionIcon_Rf37"><svg viewBox="0 0 12 16"><path fill-rule="evenodd" d="M6.5 0C3.48 0 1 2.19 1 5c0 .92.55 2.25 1 3 1.34 2.25 1.78 2.78 2 4v1h5v-1c.22-1.22.66-1.75 2-4 .45-.75 1-2.08 1-3 0-2.81-2.48-5-5.5-5zm3.64 7.48c-.25.44-.47.8-.67 1.11-.86 1.41-1.25 2.06-1.45 3.23-.02.05-.02.11-.02.17H5c0-.06 0-.13-.02-.17-.2-1.17-.59-1.83-1.45-3.23-.2-.31-.42-.67-.67-1.11C2.44 6.78 2 5.65 2 5c0-2.2 2.02-4 4.5-4 1.22 0 2.36.42 3.22 1.19C10.55 2.94 11 3.94 11 5c0 .66-.44 1.78-.86 2.48zM4 14h5c-.23 1.14-1.3 2-2.5 2s-2.27-.86-2.5-2z"></path></svg></span>Lesson</div><div class="admonitionContent_BuS1"><p>If your benchmark reuses inputs, you're partly measuring your framework's cache. And when two well-designed methods agree against your conclusion, the conclusion is what's wrong.</p></div></div>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="lie-4--the-127-mb-memory-saving-that-wasnt">Lie #4 — the 127 MB memory saving that wasn't<a href="https://veloxquant-mlx.netlify.app/docs/blog/kivi-metal-kernel-honest-benchmark#lie-4--the-127-mb-memory-saving-that-wasnt" class="hash-link" aria-label="Direct link to Lie #4 — the 127 MB memory saving that wasn't" title="Direct link to Lie #4 — the 127 MB memory saving that wasn't" translate="no">​</a></h3>
<p>This one I caught only because I ran a third model.</p>
<p>Llama's peak memory, from a clean interleaved run:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">  peak memory (GB): fp16=2.604  off=2.726  on=2.599</span><br></div></code></pre></div></div>
<p>A <strong>127 MB reduction</strong> with the kernel on. And there's a beautiful explanation sitting right there: the fused kernel eliminates MLX's intermediate tensors, so of course the high-water mark drops. Mechanistically plausible, exactly the result I wanted, and it would have made this post better.</p>
<p>Then the other two models came back:</p>
<table><thead><tr><th>model</th><th>kernel off</th><th>kernel on</th><th>delta</th></tr></thead><tbody><tr><td>Llama-3.2-3B</td><td>2.726 GB</td><td>2.599 GB</td><td><strong>−127 MB</strong></td></tr><tr><td>Qwen2.5-7B</td><td>5.080 GB</td><td>5.130 GB</td><td><strong>+50 MB</strong></td></tr><tr><td>Mistral-7B</td><td>4.921 GB</td><td>4.941 GB</td><td><strong>+20 MB</strong></td></tr></tbody></table>
<p>Two of three moved the <em>opposite</em> direction. It's allocator noise — MLX's memory pool responds to allocation ordering in ways unrelated to which kernel ran, and 127 MB out of 2.7 GB sits well inside that.</p>
<div class="theme-admonition theme-admonition-warning admonition_xJq3 alert alert--warning"><div class="admonitionHeading_Gvgb"><span class="admonitionIcon_Rf37"><svg viewBox="0 0 16 16"><path fill-rule="evenodd" d="M8.893 1.5c-.183-.31-.52-.5-.887-.5s-.703.19-.886.5L.138 13.499a.98.98 0 0 0 0 1.001c.193.31.53.501.886.501h13.964c.367 0 .704-.19.877-.5a1.03 1.03 0 0 0 .01-1.002L8.893 1.5zm.133 11.497H6.987v-2.003h2.039v2.003zm0-3.004H6.987V5.987h2.039v4.006z"></path></svg></span>The near-miss</div><div class="admonitionContent_BuS1"><p>If I'd only run the model named in the original issue, I'd have shipped a false claim with a compelling mechanism attached. <strong>The third model is what turns a result into a finding</strong> — and the strongest argument for running it is precisely when the first one already told you what you hoped to hear.</p></div></div>
<p>There's a deeper reason this had to be noise, which I'll come back to at the end: <strong>quantize-then-dequantize cannot reduce peak memory, by construction.</strong></p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="the-full-end-to-end-record">The full end-to-end record<a href="https://veloxquant-mlx.netlify.app/docs/blog/kivi-metal-kernel-honest-benchmark#the-full-end-to-end-record" class="hash-link" aria-label="Direct link to The full end-to-end record" title="Direct link to The full end-to-end record" translate="no">​</a></h2>
<p>Four generations of end-to-end measurement, in the order I ran them. I'm including the early ones because their disagreement is the point.</p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="generation-1--single-shot-three-prompt-lengths">Generation 1 — single-shot, three prompt lengths<a href="https://veloxquant-mlx.netlify.app/docs/blog/kivi-metal-kernel-honest-benchmark#generation-1--single-shot-three-prompt-lengths" class="hash-link" aria-label="Direct link to Generation 1 — single-shot, three prompt lengths" title="Direct link to Generation 1 — single-shot, three prompt lengths" translate="no">​</a></h3>
<p>First real run. Single-shot timings, no repeats, <code>Llama-3.2-3B-Instruct-4bit</code>, 28 layers, 8 KV heads, <code>head_dim=128</code>:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">## PREFILL (prompt tokens/sec)</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">prompt tok       fp16  kernel off  kernel on  on vs off  flush/layer</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">       559      488.2       481.6      486.0      1.01x          512</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">      2059      463.4       451.7      465.7      1.03x         2016</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">      8209      386.4       355.9      351.6      0.99x         8160</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">## DECODE (generation tokens/sec, 120 tokens)</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">config             tok/s  vs fp16   peak MB  KV comp</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">fp16               46.06     100%       0.0        -</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">off                46.00     100%       0.0    4.99x</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">on                 44.24      96%       0.0    4.99x</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">decode kernel on vs off: 0.962x</span><br></div></code></pre></div></div>
<p>Two problems. First, <code>peak MB</code> reads <code>0.0</code> — a unit bug on my side: <code>mlx_lm.stream_generate</code> reports <code>peak_memory</code> in <strong>GB</strong>, and I was dividing by <code>1024**2</code> as though it were bytes. The raw JSON shows the real values hiding at <code>2.48e-06</code>.</p>
<p>Second, and more importantly: <strong>decode at 0.962× looks like a 4% regression.</strong> Single-shot numbers on one prompt, with no repeat structure — nowhere near enough to distinguish a real regression from thermal drift. Generation 3 is what settles it.</p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="generation-2--the-sync-instrumented-run">Generation 2 — the sync-instrumented run<a href="https://veloxquant-mlx.netlify.app/docs/blog/kivi-metal-kernel-honest-benchmark#generation-2--the-sync-instrumented-run" class="hash-link" aria-label="Direct link to Generation 2 — the sync-instrumented run" title="Direct link to Generation 2 — the sync-instrumented run" translate="no">​</a></h3>
<p>Lie #1 above. Produced the 97.83% figure, which was wrong, and the 2048-token prefill chunking discovery, which was right and load-bearing.</p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="generation-3--repeated-runs-with-medians">Generation 3 — repeated runs with medians<a href="https://veloxquant-mlx.netlify.app/docs/blog/kivi-metal-kernel-honest-benchmark#generation-3--repeated-runs-with-medians" class="hash-link" aria-label="Direct link to Generation 3 — repeated runs with medians" title="Direct link to Generation 3 — repeated runs with medians" translate="no">​</a></h3>
<p>Same model, but now multiple repeats per configuration reporting median/min/max, so spread is visible:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">## PREFILL (prompt tok/s, max_tokens=4)</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  prompt=2065 tok  (~2 chunks of 2048)</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    fp16  median=  468.49  min=  448.16  max=  473.22   vs fp16 100.0%</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    off   median=  451.76  min=  420.91  max=  464.51   vs fp16  96.4%</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    on    median=  460.48  min=  424.83  max=  470.01   vs fp16  98.3%</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    -&gt; kernel on vs off: 1.019x</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  prompt=8213 tok  (~5 chunks of 2048)</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    fp16  median=  300.09  min=  280.98  max=  352.22   vs fp16 100.0%</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    off   median=  297.13  min=  291.43  max=  320.70   vs fp16  99.0%</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    on    median=  296.32  min=  293.69  max=  309.83   vs fp16  98.7%</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    -&gt; kernel on vs off: 0.997x</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">## DECODE (generation tok/s, 240 tokens, 2k prompt)</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  240 new tokens</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    fp16  median=   38.48  min=   38.01  max=   40.82   vs fp16 100.0%</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    off   median=   40.21  min=   39.72  max=   41.10   vs fp16 104.5%</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    on    median=   39.78  min=   37.22  max=   41.05   vs fp16 103.4%</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    -&gt; kernel on vs off: 0.989x</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  peak memory (GB): fp16=2.604  off=2.726  on=2.599</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  KV compression:   off=5.02x  on=5.02x</span><br></div></code></pre></div></div>
<p><strong>This table is the most useful thing I measured</strong>, and not because of the ratios. Look at the <code>min</code>/<code>max</code> columns.</p>
<p>At 8k prompt, the <strong>unchanged fp16 baseline</strong> — same code, same model, nothing swapped — ranges from <strong>280.98 to 352.22 tok/s</strong>. That's a <strong>±25% spread</strong> from thermal state and system scheduling alone, on a configuration where nothing about the code changed between runs.</p>
<p>Now recall the prediction: +1.7% on prefill. <strong>Looking for a 1.7% effect through ±25% run-to-run variance is like weighing a signature on a bathroom scale.</strong> No amount of care in the on/off comparison fixes that; the instrument simply doesn't resolve the quantity.</p>
<p>Note also that KIVI <em>itself</em> (off, 96.4%) is slightly slower than fp16 at 2k — the quantization work is real, it's just small. And the decode ordering (off at 104.5% of fp16, i.e. <em>faster</em> than no quantization at all) is a tell that we're deep inside noise, since compressing the cache cannot make decode faster than not compressing it.</p>
<p>Generation 1's apparent 0.962× decode regression shows up here as 0.989×, with overlapping min/max ranges. It was drift.</p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="generation-4--three-models-interleaved-single-process">Generation 4 — three models, interleaved, single process<a href="https://veloxquant-mlx.netlify.app/docs/blog/kivi-metal-kernel-honest-benchmark#generation-4--three-models-interleaved-single-process" class="hash-link" aria-label="Direct link to Generation 4 — three models, interleaved, single process" title="Direct link to Generation 4 — three models, interleaved, single process" translate="no">​</a></h3>
<p>The final protocol: one process per model, configurations interleaved rather than run in blocks, output text compared byte-for-byte between arms.</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">### mlx-community/Llama-3.2-3B-Instruct-4bit</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  layers=28 kv_heads=8 prompt=2065 tok</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  -&gt; kernel on vs off:  prefill 1.019x (2k) / 0.997x (8k)   decode 0.989x</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  -&gt; identical text on/off: True</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">### mlx-community/Qwen2.5-7B-Instruct-4bit</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  layers=28 kv_heads=4 prompt=2064 tok</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  config  prefill tok/s  decode tok/s   peak GB   KV comp</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  fp16            206.0         23.61     5.105         -</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  off             203.7         23.40     5.080     4.94x</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  on              208.3         23.26     5.130     4.94x</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  -&gt; kernel on vs off:  prefill 1.023x   decode 0.994x</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  -&gt; identical text on/off: True</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">### mlx-community/Mistral-7B-Instruct-v0.3-4bit</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  layers=32 kv_heads=8 prompt=2054 tok</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  config  prefill tok/s  decode tok/s   peak GB   KV comp</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  fp16            143.4         20.86     4.891         -</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  off             140.0         20.34     4.921     4.75x</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  on              140.2         20.26     4.941     4.75x</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  -&gt; kernel on vs off:  prefill 1.001x   decode 0.996x</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  -&gt; identical text on/off: True</span><br></div></code></pre></div></div>
<p>Consolidated:</p>
<table><thead><tr><th>model</th><th>layers</th><th>KV heads</th><th>prefill</th><th>decode</th><th>identical text</th><th>KV compression</th></tr></thead><tbody><tr><td>Llama-3.2-3B-4bit</td><td>28</td><td>8</td><td>1.019× / 0.997×</td><td>0.989×</td><td>✅</td><td>5.02×</td></tr><tr><td>Qwen2.5-7B-4bit</td><td>28</td><td><strong>4</strong></td><td>1.023×</td><td>0.994×</td><td>✅</td><td>4.94×</td></tr><tr><td>Mistral-7B-v0.3-4bit</td><td>32</td><td>8</td><td>1.001×</td><td>0.996×</td><td>✅</td><td>4.75×</td></tr></tbody></table>
<p>Everything within ±2%, which given ±25% baseline variance is indistinguishable from nothing.</p>
<p><strong>Qwen is the most valuable row.</strong> Four KV heads instead of eight means a completely different flush geometry, exercising different bounds-check and ragged-tail paths. It still produces byte-identical output — which is the strongest evidence that the bit-exactness work held up outside the unit tests.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="why-its-invisible-and-why-that-was-predictable">Why it's invisible, and why that was predictable<a href="https://veloxquant-mlx.netlify.app/docs/blog/kivi-metal-kernel-honest-benchmark#why-its-invisible-and-why-that-was-predictable" class="hash-link" aria-label="Direct link to Why it's invisible, and why that was predictable" title="Direct link to Why it's invisible, and why that was predictable" translate="no">​</a></h2>
<p>The true per-flush picture, idle GPU, Apple M4, 8 KV heads × 128 head dim, keys and values together:</p>
<table><thead><tr><th>flush size</th><th>kernel off</th><th>kernel on</th><th>speedup</th></tr></thead><tbody><tr><td>32 <em>(decode)</em></td><td>0.5050 ms</td><td>0.3601 ms</td><td><strong>1.40×</strong></td></tr><tr><td>2048 <em>(prefill chunk)</em></td><td>3.8337 ms</td><td>0.6785 ms</td><td><strong>5.65×</strong></td></tr></tbody></table>
<p>Recall that mlx_lm chunks prefill at 2048 tokens. That's a happy accident: prefill flushes land exactly on the kernel's strongest case, where there's enough work to amortize dispatch and memory-traffic savings dominate. Decode flushes are always small — <code>residual_length</code> tokens, 32 here — the weak case.</p>
<p>Scale it to 28 layers, an 8k prompt (4 prefill chunks), 240 decode tokens (7 flushes):</p>
<table><thead><tr><th>phase</th><th>saved</th><th>of wall</th><th>predicted</th><th>measured</th></tr></thead><tbody><tr><td>prefill (8k)</td><td>353 ms</td><td>~21,000 ms</td><td><strong>+1.7%</strong></td><td>0.997×</td></tr><tr><td>decode (240 tok)</td><td>28 ms</td><td>~10,000 ms</td><td><strong>+0.28%</strong></td><td>0.989×</td></tr></tbody></table>
<p>There's the whole story, available before running a single model.</p>
<blockquote>
<p><strong>A 5.65× speedup on 1.7% of the work is a 1.7% speedup.</strong> Amdahl's law doesn't care how good the kernel is.</p>
</blockquote>
<p>And 1.7% is four times smaller than the noise floor of the measurement. The end-to-end result wasn't a disappointment — it was arithmetic, and I could have computed it in ten minutes before writing any Metal at all.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="so-why-does-the-kernel-ship">So why does the kernel ship?<a href="https://veloxquant-mlx.netlify.app/docs/blog/kivi-metal-kernel-honest-benchmark#so-why-does-the-kernel-ship" class="hash-link" aria-label="Direct link to So why does the kernel ship?" title="Direct link to So why does the kernel ship?" translate="no">​</a></h2>
<p>Given that it's invisible end-to-end, why merge it?</p>
<ul>
<li class=""><strong>It's free.</strong> Bit-exact output, no regression on any model, 84 dedicated tests, byte-identical generations across three architectures. It never makes anything slower.</li>
<li class=""><strong>It removes a floor.</strong> Quantization is ~1–2% of runtime <em>now</em>. If the surrounding work gets faster — better attention kernels, better matmuls — that share grows. Fixed costs matter more as everything else shrinks.</li>
<li class=""><strong>Op-level wins are real even when invisible.</strong> 1.40× and 5.65× are honest measurements of the operation. That the operation is a small slice of the whole is a separate fact, and both belong in the report.</li>
</ul>
<p>But the real reason to stay clear-eyed: <strong>the kernel was never where the memory win lived.</strong></p>
<p>KIVI as implemented does quantize-then-<em>dequantize</em> — it computes the compressed representation and immediately expands it back to fp16 for attention. The 4.75×–5.02× compression is real <em>arithmetic</em>, but it is an <strong>accounting result, not a storage result.</strong> The tensor sitting in memory is still fp16.</p>
<p>This is also the structural reason Lie #4 had to be noise: if nothing is stored in compressed form, no kernel that computes the compression faster can reduce the high-water mark. I should have known the 127 MB was suspect on those grounds alone, before the other two models contradicted it.</p>
<p>To actually reduce memory you need two more things:</p>
<ol>
<li class=""><strong>Packed storage</strong> — keep quantized codes as <code>uint8</code>, never materialize the fp16 reconstruction.</li>
<li class=""><strong>Dequant-in-SDPA</strong> — teach attention to read packed codes directly, so expansion happens in registers and never in RAM.</li>
</ol>
<p>That's where both the memory win and the <em>real</em> speedup live, because it doesn't fuse 1–2% of the work — it shrinks the tensors every other operation has to move.</p>
<blockquote>
<p>The kernel was step one. It was worth doing. It just isn't the point.</p>
</blockquote>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="what-id-take-away">What I'd take away<a href="https://veloxquant-mlx.netlify.app/docs/blog/kivi-metal-kernel-honest-benchmark#what-id-take-away" class="hash-link" aria-label="Direct link to What I'd take away" title="Direct link to What I'd take away" translate="no">​</a></h2>
<p>If you're writing GPU kernels against a framework like MLX or PyTorch:</p>
<p><strong>Match the thread mapping to the memory layout, not to intuition.</strong> The strided access pattern needed <em>less</em> machinery than the contiguous one — no reduction, no barriers, no transpose. My first instinct, transposing to make the layout "nice," was the version that lost.</p>
<p><strong>Bit-exactness is a debugging tool, not just a correctness bar.</strong> FMA contraction, rounding mode, and padding semantics all produce <em>plausible</em> output. A tolerance test passes all three. Demanding identical output turned three silent behavioral differences into three failing tests with obvious causes.</p>
<p><strong>Never specialize on a value that grows.</strong> Baking sequence length into a JIT header compiles one shader per token. The symptom was an indefinite hang; the fix deleted code.</p>
<p><strong>Estimate the ceiling before you optimize.</strong> Ten minutes with Amdahl's law would have predicted the end-to-end result up front. It wouldn't have changed the decision to build it — but it would have set the expectation correctly, and I'd have spent my time on the parts that were load-bearing.</p>
<p><strong>Measure your noise floor before your effect.</strong> The single most useful number in this entire project was ±25% — the run-to-run spread of an <em>unchanged</em> baseline. Without it, every ratio in every table is unfalsifiable.</p>
<p><strong>Be most suspicious when the number is good.</strong> All four bad measurements came with satisfying stories. 97.83% "proved" the work mattered. 28.08× had a tidy overhead-crossover explanation. The 127 MB saving had a clean mechanism. Every one was wrong, and the plausible explanation is what let each survive as long as it did.</p>
<p>And: run the third model.</p>
<hr>
<p><em>Kernels live in <code>veloxquant_mlx/metal/src/</code>, tests in <code>veloxquant_mlx/tests/metal/test_kivi_quant.py</code>. See the <a class="" href="https://veloxquant-mlx.netlify.app/docs/algorithms/kivi">KIVI algorithm reference</a> for the method itself, and <a class="" href="https://veloxquant-mlx.netlify.app/docs/guides/metal-kernels">Metal kernels</a> for how kernels are dispatched library-wide. KIVI: <a href="https://arxiv.org/abs/2402.02750" target="_blank" rel="noopener noreferrer" class="">Liu et al., ICML 2024</a>. All measurements on an Apple M4 with MLX.</em></p>]]></content>
        <author>
            <name>Rajveer Rathod</name>
            <uri>https://github.com/rajveer43</uri>
        </author>
        <category label="metal" term="metal"/>
        <category label="apple-silicon" term="apple-silicon"/>
        <category label="mlx" term="mlx"/>
        <category label="gpu" term="gpu"/>
        <category label="performance" term="performance"/>
        <category label="kivi" term="kivi"/>
        <category label="benchmarking" term="benchmarking"/>
    </entry>
    <entry>
        <title type="html"><![CDATA[TensorOps Research: What We Learned Optimizing KV Caches]]></title>
        <id>https://veloxquant-mlx.netlify.app/docs/blog/tensorops-research</id>
        <link href="https://veloxquant-mlx.netlify.app/docs/blog/tensorops-research"/>
        <updated>2026-06-20T00:00:00.000Z</updated>
        <summary type="html"><![CDATA[A deep-dive into Apple's Metal Shading Language specification, what TensorOps promised, why it didn't work through MLX, and the two real improvements we shipped from three sessions of research.]]></summary>
        <content type="html"><![CDATA[<p><em>A deep-dive into Apple's Metal Shading Language specification, what TensorOps promised, why it didn't work through MLX, and the two real improvements we shipped from three sessions of research.</em></p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="where-this-story-starts">Where This Story Starts<a href="https://veloxquant-mlx.netlify.app/docs/blog/tensorops-research#where-this-story-starts" class="hash-link" aria-label="Direct link to Where This Story Starts" title="Direct link to Where This Story Starts" translate="no">​</a></h2>
<p>A few weeks ago I shipped a FlashAttention-style Metal kernel for VeloxQuant-MLX that was correct, fast in isolation, and completely useless end-to-end. The blog post about that mistake is <a href="https://medium.com/@rajveer.rathod1301" target="_blank" rel="noopener noreferrer" class="">here</a>. The short version: I built a fused dequant+attention kernel that beat <code>mx.fast.scaled_dot_product_attention</code> by 1.3× in benchmarks — then discovered mlx_lm had already eliminated the dequant cost via a persistent fp16 K_hat buffer, making my kernel 3-4× slower than the baseline it was supposed to beat.</p>
<p>I kept the kernel in the library as an opt-in API. It's correct, it's tested, and it loses.</p>
<p>After writing that post, a reader suggested I look at the Metal Shading Language specification — specifically Metal 4, which Apple released with macOS Sequoia. The argument was: Metal 4 adds hardware tensor operations that could replace the slow part of the kernel. Maybe there was a path to winning that I hadn't found yet.</p>
<p>So I read the spec. All 346 pages of it.</p>
<p>This is what I found.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="the-kernels-hot-path">The Kernel's Hot Path<a href="https://veloxquant-mlx.netlify.app/docs/blog/tensorops-research#the-kernels-hot-path" class="hash-link" aria-label="Direct link to The Kernel's Hot Path" title="Direct link to The Kernel's Hot Path" translate="no">​</a></h2>
<p>To understand why the spec research mattered, I need to explain the bottleneck.</p>
<p>The fused SDPA kernel computes attention directly from VecInfer's compressed codebook indices without materializing the fp16 key matrix. For each query, it needs to compute a <strong>Look-Up Table</strong> first:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">LUT[sub, centroid] = q_sub_vector · codebook_row[centroid]</span><br></div></code></pre></div></div>
<p>For VecInfer's default config (<code>n_sub=16</code>, <code>sub_dim=8</code>, <code>n_centroids=256</code>), this is a <code>[16, 8] @ [8, 256]</code> matrix multiply — 4,096 dot products. In the current kernel, 32 GPU lanes stripe these across the SIMD group: each lane computes 128 scalar dot products independently.</p>
<p>This LUT precompute is Phase 0. Everything else — the online softmax, the V accumulation — comes after. If the LUT is slow, everything is slow.</p>
<p>The Metal 4 spec describes two potential hardware paths to speed this up:</p>
<ol>
<li class=""><strong><code>simdgroup_float8x8</code></strong> (Metal 2.3+, Section 2.4 / 6.7): 8×8 hardware matmul tiles via <code>simdgroup_multiply_accumulate</code>. Available today.</li>
<li class=""><strong>TensorOps <code>matmul2d</code></strong> (Metal 4+, Section 7.2): A full hardware matrix multiply API with a <code>cooperative_tensor</code> destination. Potentially much faster.</li>
</ol>
<p>I tested both.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="attempt-1-simdgroup_float8x8">Attempt 1: <code>simdgroup_float8x8</code><a href="https://veloxquant-mlx.netlify.app/docs/blog/tensorops-research#attempt-1-simdgroup_float8x8" class="hash-link" aria-label="Direct link to attempt-1-simdgroup_float8x8" title="Direct link to attempt-1-simdgroup_float8x8" translate="no">​</a></h2>
<p>The Metal spec (Section 6.7, Table 6.9) shows <code>simdgroup_float8x8</code> as a cooperative 8×8 float matrix multiply tile. The <code>&lt;metal_simdgroup_matrix&gt;</code> header is accessible via MLX's <code>header=</code> parameter:</p>
<div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">k </span><span class="token operator">=</span><span class="token plain"> mx</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">fast</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">metal_kernel</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    name</span><span class="token operator">=</span><span class="token string" style="color:rgb(255, 121, 198)">"my_kernel"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    source</span><span class="token operator">=</span><span class="token plain">src</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    header</span><span class="token operator">=</span><span class="token string" style="color:rgb(255, 121, 198)">"#include &lt;metal_simdgroup_matrix&gt;\nusing namespace metal;\n"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><br></div></code></pre></div></div>
<p>The tiling plan for our LUT: <code>n_sub=16</code> rows / 8 = 2 row-tiles, <code>n_centroids=256</code> cols / 8 = 32 col-tiles, <code>sub_dim=8</code> = 1 K-tile. Total: 64 hardware matmul operations.</p>
<p>I implemented it. Correctness test: zero diff vs reference.</p>
<p>Then I benchmarked it against the current scalar loop:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">scalar loop:     212 µs per LUT precompute</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">simdgroup 8×8:   255 µs per LUT precompute</span><br></div></code></pre></div></div>
<p><strong>The hardware matrix multiply was slower.</strong></p>
<p>The reason is protocol overhead. <code>simdgroup_float8x8</code> is a cooperative operation — all 32 lanes must execute each tile in lock-step. For our 64 tile iterations, that's 64 synchronization points. The scalar loop has zero synchronization: each lane independently computes 128 dot products in parallel. For a small matrix like <code>[16,8]@[8,256]</code>, the cooperation overhead dominates the compute savings.</p>
<p><code>simdgroup_matrix</code> wins at large, batched matmuls (MLX uses it for GEMM with 128×128 tiles). For our 16×256 LUT, it's the wrong tool. Reverted.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="attempt-2-metal-4-tensorops">Attempt 2: Metal 4 TensorOps<a href="https://veloxquant-mlx.netlify.app/docs/blog/tensorops-research#attempt-2-metal-4-tensorops" class="hash-link" aria-label="Direct link to Attempt 2: Metal 4 TensorOps" title="Direct link to Attempt 2: Metal 4 TensorOps" translate="no">​</a></h2>
<p>Section 7.2 of the spec describes <code>tensor_ops::matmul2d</code> — a hardware-accelerated matrix multiply that operates on <code>tensor&lt;&gt;</code> types and writes to a <code>cooperative_tensor</code> destination held in thread registers. The pitch is exactly right: no threadgroup memory round-trip, hardware tensor units, single API call.</p>
<p>The example from the spec:</p>
<div class="language-metal codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-metal codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">#include &lt;metal_tensor&gt;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">#include &lt;MetalPerformancePrimitives/MetalPerformancePrimitives.h&gt;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">using namespace metal;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">using namespace mpp;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">[[ kernel ]] void matrixMultiply(</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    tensor&lt;device half, dextents&lt;int, 2&gt;&gt; a [[ buffer(0) ]],</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    tensor&lt;device half, dextents&lt;int, 2&gt;&gt; b [[ buffer(1) ]],</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    tensor&lt;device half, dextents&lt;int, 2&gt;&gt; c [[ buffer(2) ]]) {</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    constexpr auto desc = tensor_ops::matmul2d_descriptor(64, 32, 0);</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    tensor_ops::matmul2d&lt;desc, execution_simdgroups&lt;4&gt;&gt; op;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    matmulOp.run(a, b, c);</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">}</span><br></div></code></pre></div></div>
<p>Clean. Exactly what we need.</p>
<p>I confirmed the header is accessible:</p>
<div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">k </span><span class="token operator">=</span><span class="token plain"> mx</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">fast</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">metal_kernel</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    name</span><span class="token operator">=</span><span class="token string" style="color:rgb(255, 121, 198)">"test"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    source</span><span class="token operator">=</span><span class="token plain">src</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    header</span><span class="token operator">=</span><span class="token triple-quoted-string string" style="color:rgb(255, 121, 198)">"""</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token triple-quoted-string string" style="color:rgb(255, 121, 198)">    #include &lt;metal_tensor&gt;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token triple-quoted-string string" style="color:rgb(255, 121, 198)">    #include &lt;MetalPerformancePrimitives/MetalPerformancePrimitives.h&gt;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token triple-quoted-string string" style="color:rgb(255, 121, 198)">    using namespace metal;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token triple-quoted-string string" style="color:rgb(255, 121, 198)">    using namespace mpp;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token triple-quoted-string string" style="color:rgb(255, 121, 198)">    """</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token comment" style="color:rgb(98, 114, 164)"># Compiles. Header is reachable.</span><br></div></code></pre></div></div>
<p>And Metal 4 is available:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">Metal version: 400.0   (M4, macOS Sequoia)</span><br></div></code></pre></div></div>
<p>Then I tried to actually use <code>matmul2d</code>. Three blockers, in order of discovery:</p>
<p><strong>Blocker 1: Type support.</strong></p>
<p>Table 7.3 of the spec lists supported type combinations. <code>float/float/float</code> is listed — but when I tried it:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">static_assert failed: "Unsupported type"</span><br></div></code></pre></div></div>
<p>Table 7.4 clarifies: <code>bfloat/bfloat/bfloat</code> and several mixed-precision combinations require <strong>OS 26.1 and later</strong>. That's iOS/macOS naming — it maps to macOS 26.1 (not released yet as of this writing). The <code>float/float/float</code> path in Table 7.3 is supported, but only with certain <code>execution_scope</code> + K-dimension combinations that are hardware-dependent.</p>
<p><strong>Blocker 2: <code>tensor_handle</code> vs <code>tensor_inline</code>.</strong></p>
<p>The spec's <code>matmul2d</code> example uses tensors declared as kernel parameters with <code>[[buffer(N)]]</code> attributes — these are <code>tensor_handle</code> type. MLX's <code>metal_kernel</code> generates the function signature automatically: it only creates raw pointer buffers (<code>const device float* a [[buffer(0)]]</code>), not <code>tensor&lt;device half, ..., tensor_handle&gt;</code> parameters.</p>
<p>The only tensor type you can construct at runtime from a pointer is <code>tensor_inline</code>. But <code>cooperative_tensor.store()</code> only accepts <code>tensor_handle</code> targets for device memory writes. The round-trip <code>cooperative_tensor → tensor_inline → device output</code> is blocked:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">error: candidate template ignored: could not match 'tensor_handle' against 'tensor_inline'</span><br></div></code></pre></div></div>
<p><strong>Blocker 3: Dynamic K hangs the GPU compiler.</strong></p>
<p>When I tried <code>K=0</code> (dynamic length, matching the spec example exactly), the MLX JIT compilation hung. The TensorOps template instantiation with <code>dynamic_length_v&lt;int&gt;</code> appears to trigger a very long (possibly infinite) compile path under MLX's inline Metal JIT. The process never returned.</p>
<p><strong>Summary:</strong> TensorOps is architecturally incompatible with MLX's <code>mx.fast.metal_kernel</code> API. The API generates raw pointer buffers; TensorOps requires tensor-typed formal parameters. The mismatch is fundamental, not a workaround.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="what-actually-worked">What Actually Worked<a href="https://veloxquant-mlx.netlify.app/docs/blog/tensorops-research#what-actually-worked" class="hash-link" aria-label="Direct link to What Actually Worked" title="Direct link to What Actually Worked" translate="no">​</a></h2>
<p>Two improvements from the spec research did ship.</p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="1-metalpreciseexp--a-correctness-fix-hiding-as-a-performance-question">1. <code>metal::precise::exp</code> — a correctness fix hiding as a performance question<a href="https://veloxquant-mlx.netlify.app/docs/blog/tensorops-research#1-metalpreciseexp--a-correctness-fix-hiding-as-a-performance-question" class="hash-link" aria-label="Direct link to 1-metalpreciseexp--a-correctness-fix-hiding-as-a-performance-question" title="Direct link to 1-metalpreciseexp--a-correctness-fix-hiding-as-a-performance-question" translate="no">​</a></h3>
<p>Section 8.2 of the spec describes rounding mode. Section 8.3 covers floating-point exceptions. Table 8.2 documents accuracy under fast math.</p>
<p>The relevant line: <code>exp()</code> in fast math mode (<code>-fmetal-math-mode=fast</code>) does not guarantee <code>exp(-INFINITY) = 0.0</code>. The spec's ULP table for fast math lists relaxed accuracy bounds for transcendentals.</p>
<p>Our kernel uses <code>exp(score - running_max)</code> for the online softmax. When a lane is masked (causal or sliding-window), we set <code>score = -INFINITY</code>. In fast math mode, <code>exp(-INFINITY)</code> may not be exactly <code>0.0</code> — which would corrupt the softmax denominator.</p>
<p>The fix: use the <code>metal::precise::</code> namespace to force IEEE-compliant <code>exp</code> regardless of compiler math mode:</p>
<div class="language-metal codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-metal codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">// Before (math-mode dependent):</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">float w = exp(score - m_new);</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">// After (always correct):</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">float w = metal::precise::exp(score - m_new);</span><br></div></code></pre></div></div>
<p>MLX's <code>metal_kernel</code> API has no parameter for compiler flags, so <code>-fmetal-math-mode=relaxed</code> isn't accessible. The namespace workaround is better anyway — it's surgical, affects only these two <code>exp</code> calls, and documents intent in the code.</p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="2-simd_broadcast_first--eliminating-two-threadgroup-barriers-per-tile">2. <code>simd_broadcast_first</code> — eliminating two threadgroup barriers per tile<a href="https://veloxquant-mlx.netlify.app/docs/blog/tensorops-research#2-simd_broadcast_first--eliminating-two-threadgroup-barriers-per-tile" class="hash-link" aria-label="Direct link to 2-simd_broadcast_first--eliminating-two-threadgroup-barriers-per-tile" title="Direct link to 2-simd_broadcast_first--eliminating-two-threadgroup-barriers-per-tile" translate="no">​</a></h3>
<p>Section 6.9.2 of the spec (Table 6.14) lists the full SIMD-group permute function set. One entry:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">simd_broadcast_first(x)  →  broadcasts lane 0's value to all lanes</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">                             without a threadgroup barrier</span><br></div></code></pre></div></div>
<p>The original kernel used threadgroup memory to share the running max and rescale factor:</p>
<div class="language-metal codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-metal codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">// Before: two threadgroup variables, two barriers per tile</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">if (lane == 0) {</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    tg_m_shared = m_new;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    tg_factor   = factor;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">}</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">threadgroup_barrier(mem_flags::mem_threadgroup);</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">m_new  = tg_m_shared;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">factor = tg_factor;</span><br></div></code></pre></div></div>
<p>With <code>simd_broadcast_first</code>, both threadgroup variables disappear entirely — <code>running_m</code> becomes a lane-local float that all 32 lanes keep synchronized:</p>
<div class="language-metal codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-metal codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">// After: no threadgroup variables, no barriers for scalar sharing</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">float m_new  = simd_broadcast_first(max(running_m, tile_max));</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">float factor = simd_broadcast_first(</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    isfinite(running_m) ? metal::precise::exp(running_m - m_new) : 0.0f);</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">running_m = m_new;</span><br></div></code></pre></div></div>
<p>With S_kv=4096 and 128 tiles, this removes 256 threadgroup barriers from the hot loop. Threadgroup barriers are expensive — they serialize the entire threadgroup and flush threadgroup memory. Removing them reduces both latency and the register pressure from storing shared state.</p>
<p>Both of these are in the current kernel. 9 parity tests pass. The improvements are real even if the end-to-end situation hasn't changed.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="the-actual-answer-to-what-is-section-72-useful-for">The Actual Answer to "What Is Section 7.2 Useful For?"<a href="https://veloxquant-mlx.netlify.app/docs/blog/tensorops-research#the-actual-answer-to-what-is-section-72-useful-for" class="hash-link" aria-label="Direct link to The Actual Answer to &quot;What Is Section 7.2 Useful For?&quot;" title="Direct link to The Actual Answer to &quot;What Is Section 7.2 Useful For?&quot;" translate="no">​</a></h2>
<p>TensorOps would be transformative <strong>if</strong> MLX supported tensor-typed kernel parameters. The current <code>mx.fast.metal_kernel</code> API exposes only raw device pointers — the <code>[[buffer(N)]]</code> binding that TensorOps needs is auto-generated as <code>const device float*</code>, not <code>tensor&lt;device half, ..., tensor_handle&gt;</code>.</p>
<p>To use TensorOps for our LUT precompute, MLX would need one of:</p>
<ol>
<li class="">
<p><strong>Support <code>tensor&lt;&gt;</code> as a formal parameter type</strong> in <code>metal_kernel</code>'s auto-generated signature. Something like <code>input_tensor_types=[("a", mx.float16, 2)]</code> that generates <code>tensor&lt;device half, dextents&lt;int,2&gt;&gt; a [[buffer(0)]]</code>.</p>
</li>
<li class="">
<p><strong>A new <code>mx.fast.metal_tensor_kernel</code> variant</strong> that accepts tensor operands natively and dispatches via TensorOps internally.</p>
</li>
</ol>
<p>This is exactly the GitHub issue we filed at <a href="https://github.com/ml-explore/mlx" target="_blank" rel="noopener noreferrer" class="">ml-explore/mlx</a>. The issue covers three requests — compiler options, integer template parameters, and Metal 4 tensor type access — all confirmed by direct testing.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="the-broader-pattern">The Broader Pattern<a href="https://veloxquant-mlx.netlify.app/docs/blog/tensorops-research#the-broader-pattern" class="hash-link" aria-label="Direct link to The Broader Pattern" title="Direct link to The Broader Pattern" translate="no">​</a></h2>
<p>Three sessions, three attempts at the LUT precompute, three different techniques:</p>
<table><thead><tr><th>Attempt</th><th>Technique</th><th>Result</th></tr></thead><tbody><tr><td>Original</td><td>Scalar loop, 32 lanes stripe independently</td><td>Baseline</td></tr><tr><td>Attempt 1</td><td><code>simdgroup_float8x8</code>, cooperative 8×8 tiles</td><td>20 µs slower — protocol overhead wins</td></tr><tr><td>Attempt 2</td><td>TensorOps <code>matmul2d</code>, hardware tensor units</td><td>API incompatible with MLX's kernel wrapper</td></tr></tbody></table>
<p>The pattern: each attempt was technically sound, correctly implemented, and blocked by something orthogonal to the GPU math.</p>
<ul>
<li class="">Simdgroup matrix: the hardware works, the tile size is wrong.</li>
<li class="">TensorOps: the hardware works, the API binding doesn't exist.</li>
</ul>
<p>In both cases, the blocker wasn't that the hardware was slow. The blocker was that the <strong>interface</strong> between our code and the hardware had a constraint we couldn't see until we hit it.</p>
<p>The right mental model for GPU kernel work on Apple Silicon: there are three layers — the math you want to do, the hardware that can do it, and the API that connects them. Breakthroughs happen at the API layer, not the math layer. The math for attention has been solved. The hardware for matrix multiply has been built. The gap is the binding.</p>
<p>That gap is the GitHub issue. If MLX adds <code>tensor&lt;&gt;</code> support to <code>metal_kernel</code>, this whole investigation becomes a one-afternoon project. Until then, the scalar LUT is the fastest thing we can write.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="what-is-in-the-library-now">What Is in the Library Now<a href="https://veloxquant-mlx.netlify.app/docs/blog/tensorops-research#what-is-in-the-library-now" class="hash-link" aria-label="Direct link to What Is in the Library Now" title="Direct link to What Is in the Library Now" translate="no">​</a></h2>
<p><a href="https://github.com/rajveer43/VeloxQuant-MLX/blob/master/veloxquant_mlx/metal/fused_sdpa.py" target="_blank" rel="noopener noreferrer" class="">veloxquant_mlx/metal/fused_sdpa.py</a> has:</p>
<ul>
<li class=""><code>metal::precise::exp</code> for both softmax <code>exp</code> calls — correctness guarantee regardless of MLX math mode</li>
<li class=""><code>simd_broadcast_first</code> replacing threadgroup barriers for <code>running_m</code> — 256 fewer barriers at S_kv=4096</li>
<li class=""><code>tg_m_shared</code> and <code>tg_factor</code> threadgroup variables removed — smaller threadgroup memory footprint</li>
<li class="">All 9 parity tests passing: causal, non-causal, sliding-window, GQA, short-sequence, long-sequence, dispatcher patch</li>
</ul>
<p>The end-to-end situation is unchanged from <a href="https://medium.com/@rajveer.rathod1301" target="_blank" rel="noopener noreferrer" class="">the previous post</a> — the kernel only helps if mlx_lm exposes a way to skip K_hat materialization, which requires an upstream change.</p>
<p>But the kernel is now more correct and slightly better engineered. That's what reading 346 pages of a GPU spec gets you when the hardware feature you wanted is one API version away.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="the-one-practical-takeaway">The One Practical Takeaway<a href="https://veloxquant-mlx.netlify.app/docs/blog/tensorops-research#the-one-practical-takeaway" class="hash-link" aria-label="Direct link to The One Practical Takeaway" title="Direct link to The One Practical Takeaway" translate="no">​</a></h2>
<p>Before spending time implementing a GPU optimization, answer this question:</p>
<p><strong>Which layer is blocking you — the math, the hardware, or the API?</strong></p>
<p>If the math is solved and the hardware exists, the answer is almost always the API. Find the API gap first. File the issue or write the binding. Don't write the kernel until the API exists to call it from.</p>
<p>I wrote the kernel first. I found the API gap last. Three sessions later.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="links">Links<a href="https://veloxquant-mlx.netlify.app/docs/blog/tensorops-research#links" class="hash-link" aria-label="Direct link to Links" title="Direct link to Links" translate="no">​</a></h2>
<ul>
<li class="">VeloxQuant-MLX on PyPI: <a href="https://pypi.org/project/VeloxQuant-MLX" target="_blank" rel="noopener noreferrer" class="">pypi.org/project/VeloxQuant-MLX</a></li>
<li class="">GitHub: <a href="https://github.com/rajveer43/VeloxQuant-MLX" target="_blank" rel="noopener noreferrer" class="">github.com/rajveer43/VeloxQuant-MLX</a></li>
<li class="">The kernel: <a href="https://github.com/rajveer43/VeloxQuant-MLX/blob/master/veloxquant_mlx/metal/fused_sdpa.py" target="_blank" rel="noopener noreferrer" class="">veloxquant_mlx/metal/fused_sdpa.py</a></li>
<li class="">MLX issue filed: <a href="https://github.com/ml-explore/mlx/issues" target="_blank" rel="noopener noreferrer" class="">github.com/ml-explore/mlx/issues</a></li>
<li class="">Previous post (Phase 2 mistake): <a href="https://medium.com/@rajveer.rathod1301" target="_blank" rel="noopener noreferrer" class="">Medium — I Spent 8 Hours Writing a FlashAttention Kernel</a></li>
<li class="">Previous post (Phase 1 win): <a href="https://medium.com/@rajveer.rathod1301" target="_blank" rel="noopener noreferrer" class="">Medium — I Wrote a Metal Kernel to Stop My Mac From OOMing</a></li>
</ul>]]></content>
        <author>
            <name>Rajveer Rathod</name>
            <uri>https://github.com/rajveer43</uri>
        </author>
        <category label="metal" term="metal"/>
        <category label="apple-silicon" term="apple-silicon"/>
        <category label="mlx" term="mlx"/>
        <category label="gpu" term="gpu"/>
        <category label="research" term="research"/>
    </entry>
    <entry>
        <title type="html"><![CDATA[KIVI: The Most-Cited KV Cache Baseline, Implemented in MLX]]></title>
        <id>https://veloxquant-mlx.netlify.app/docs/blog/kivi</id>
        <link href="https://veloxquant-mlx.netlify.app/docs/blog/kivi"/>
        <updated>2026-06-10T00:00:00.000Z</updated>
        <summary type="html"><![CDATA[TL;DR — VeloxQuant-MLX 0.8.0 adds KIVI (Liu, Yuan et al., ICML 2024, arXiv:2402.02750), a faithful re-implementation of the most-cited KV-cache quantization baseline. It's asymmetric (per-channel keys, per-token values), keeps a small fp16 residual window, and is fully deterministic. On an Apple M4, across Llama-3.2-3B, Qwen2.5-7B, and Mistral-7B (4-bit, ~2.2–2.4k-token prompts), KIVI-2bit measured 5.8× key / ~4.0× full-KV compression at roughly fp16 throughput. On Apple Silicon the win is memory, not speed — and we report the throughput as measured, not as hoped. Every number below traces to a committed results.json.]]></summary>
        <content type="html"><![CDATA[<blockquote>
<p><strong>TL;DR</strong> — VeloxQuant-MLX 0.8.0 adds <strong>KIVI</strong> (Liu, Yuan et al., <em>ICML 2024</em>, <a href="https://arxiv.org/abs/2402.02750" target="_blank" rel="noopener noreferrer" class="">arXiv:2402.02750</a>), a faithful re-implementation of the most-cited KV-cache quantization baseline. It's asymmetric (per-channel keys, per-token values), keeps a small fp16 residual window, and is fully deterministic. On an Apple M4, across Llama-3.2-3B, Qwen2.5-7B, and Mistral-7B (4-bit, ~2.2–2.4k-token prompts), KIVI-2bit measured <strong>5.8× key / ~4.0× full-KV compression at roughly fp16 throughput</strong>. On Apple Silicon the win is memory, not speed — and we report the throughput as measured, not as hoped. Every number below traces to a committed <code>results.json</code>.</p>
</blockquote>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="the-wall-again">The wall, again<a href="https://veloxquant-mlx.netlify.app/docs/blog/kivi#the-wall-again" class="hash-link" aria-label="Direct link to The wall, again" title="Direct link to The wall, again" translate="no">​</a></h2>
<p>If you run local LLMs on a Mac, you've met the unified-memory wall. The CPU, GPU, and Neural Engine all share one pool of RAM, and the KV cache — the keys and values the model stores for every past token — grows linearly with context length until it crowds out everything else. Weight quantization (GGUF, GPTQ, AWQ) is an <em>offline, one-time</em> compression of the model's parameters; it does nothing for the cache, which is rebuilt token by token at inference time.</p>
<p>VeloxQuant-MLX exists to compress that cache. It already ships a suite of methods — TurboQuant, RVQ, VecInfer, SpectralQuant, RaBitQ, CommVQ, QJL, PolarQuant, RateQuant. With 0.8.0 we added the one method a reviewer asks about first, and which the library conspicuously lacked: <strong>KIVI</strong>.</p>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="what-kivi-is">What KIVI is<a href="https://veloxquant-mlx.netlify.app/docs/blog/kivi#what-kivi-is" class="hash-link" aria-label="Direct link to What KIVI is" title="Direct link to What KIVI is" translate="no">​</a></h2>
<p>KIVI is <strong>"A Tuning-Free Asymmetric 2-bit Quantization for KV Cache"</strong> (<a href="https://arxiv.org/abs/2402.02750" target="_blank" rel="noopener noreferrer" class="">arXiv:2402.02750</a>, ICML 2024). Its central observation is that keys and values want <em>different</em> quantization layouts:</p>
<ul>
<li class=""><strong>Keys → per channel.</strong> Key distributions have a few high-variance channels. Quantizing along the token axis, one (scale, zero) per channel-group, keeps those channels accurate.</li>
<li class=""><strong>Values → per token.</strong> Value distributions are flatter across channels but vary token to token, so the group runs along the channel axis instead.</li>
<li class=""><strong>A residual window.</strong> The most recently generated tokens dominate attention and are cheap to keep exact, so KIVI holds the last <code>R</code> tokens in <strong>fp16</strong> and only quantizes tokens once they age out of that window.</li>
</ul>
<p>The quantizer itself is plain asymmetric min/max group quantization — no codebook, no calibration, no randomness:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">for each group g (a slice along the quantization axis):</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    zero  = min(g)</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    scale = (max(g) - min(g)) / (2**b - 1)</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    q     = round((g - zero) / scale)      # uint code in [0, 2**b-1]</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    g_hat = q * scale + zero               # asymmetric dequant</span><br></div></code></pre></div></div>
<p>Because there's no k-means and no RNG, KIVI is <strong>deterministic</strong> — same input, identical output, every run. That matters in this codebase: our vector-quantization methods train codebooks and can vary run to run; KIVI adds none of that.</p>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="why-we-added-it-and-what-we-didnt-invent">Why we added it (and what we didn't invent)<a href="https://veloxquant-mlx.netlify.app/docs/blog/kivi#why-we-added-it-and-what-we-didnt-invent" class="hash-link" aria-label="Direct link to Why we added it (and what we didn't invent)" title="Direct link to Why we added it (and what we didn't invent)" translate="no">​</a></h2>
<p>To be clear about credit: <strong>KIVI is Liu, Yuan, and colleagues' algorithm. We ported it to MLX.</strong> There is no novelty claim here.</p>
<p>The value is comparative. KIVI is the field's reference baseline — nearly every KV-cache paper measures against it. Until 0.8.0, VeloxQuant-MLX couldn't answer "how does your method compare to KIVI?" Now every other method in the library has a recognized point of comparison, in the same framework, on the same Apple-Silicon hardware. (The full reasoning, including why we chose KIVI over KVQuant, GEAR, and ZipCache, is in <code>paper/NEW_METHOD_SURVEY.md</code>: KIVI was the highest-value missing baseline, deterministic, and a clean architectural fit that needs no RoPE or attention-score hooks.)</p>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="how-it-plugs-in">How it plugs in<a href="https://veloxquant-mlx.netlify.app/docs/blog/kivi#how-it-plugs-in" class="hash-link" aria-label="Direct link to How it plugs in" title="Direct link to How it plugs in" translate="no">​</a></h2>
<p>Three lines, and <code>mlx_lm.generate</code> runs unchanged:</p>
<div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">import</span><span class="token plain"> mlx_lm</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">from</span><span class="token plain"> veloxquant_mlx </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">import</span><span class="token plain"> KVCacheConfig</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> KVCacheBuilder</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">model</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> tokenizer </span><span class="token operator">=</span><span class="token plain"> mlx_lm</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">load</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token string" style="color:rgb(255, 121, 198)">"mlx-community/Llama-3.2-3B-Instruct-4bit"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">config </span><span class="token operator">=</span><span class="token plain"> KVCacheConfig</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    method</span><span class="token operator">=</span><span class="token string" style="color:rgb(255, 121, 198)">"kivi"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    bit_width_inlier</span><span class="token operator">=</span><span class="token number">2</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain">  </span><span class="token comment" style="color:rgb(98, 114, 164)"># KIVI's default 2-bit</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    kivi_group_size</span><span class="token operator">=</span><span class="token number">32</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain">  </span><span class="token comment" style="color:rgb(98, 114, 164)"># min/max group size (paper default)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    residual_length</span><span class="token operator">=</span><span class="token number">32</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain">  </span><span class="token comment" style="color:rgb(98, 114, 164)"># recent tokens kept in fp16</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">caches </span><span class="token operator">=</span><span class="token plain"> KVCacheBuilder</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">for_model</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">model</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> config</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">model</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">make_cache </span><span class="token operator">=</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">lambda</span><span class="token plain"> </span><span class="token operator">*</span><span class="token plain">_a</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token operator">**</span><span class="token plain">_k</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain"> caches</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">response </span><span class="token operator">=</span><span class="token plain"> mlx_lm</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">generate</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    model</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    tokenizer</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    prompt</span><span class="token operator">=</span><span class="token string" style="color:rgb(255, 121, 198)">"Summarize the attention mechanism in three sentences."</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    max_tokens</span><span class="token operator">=</span><span class="token number">300</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><br></div></code></pre></div></div>
<p>The cache quantizes the aged-out keys/values and immediately dequantizes them, so the downstream scaled-dot-product attention sees standard fp16 tensors — no model surgery, no custom attention path.</p>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="results">Results<a href="https://veloxquant-mlx.netlify.app/docs/blog/kivi#results" class="hash-link" aria-label="Direct link to Results" title="Direct link to Results" translate="no">​</a></h2>
<p>All numbers below come from <code>figures/kivi/results_summary.json</code> (aggregated from the per-model <code>figures/kivi/&lt;model&gt;/results.json</code>). Conditions, identical across runs: <strong>Apple M4, 24 GB</strong>, 4-bit <code>mlx-community</code> models, <code>group_size=32</code>, <code>residual_length=32</code>, <code>max_tokens=120</code>, prompts of ~2.2–2.4k tokens (a long prompt is required for KIVI to actually exercise the quantized path rather than sit inside the fp16 residual window).</p>
<p><strong>KIVI-2bit, across three models:</strong></p>
<table><thead><tr><th>Model</th><th>head_dim / KV heads</th><th>Key compression</th><th>Full-KV compression</th><th>Throughput vs fp16</th><th>Tokens</th></tr></thead><tbody><tr><td>Llama-3.2-3B</td><td>128 / 8</td><td><strong>5.79×</strong></td><td><strong>3.98×</strong></td><td>16.3 vs 16.0 tok/s (<strong>102%</strong>)</td><td>121/121</td></tr><tr><td>Qwen2.5-7B</td><td>128 / 4</td><td><strong>5.78×</strong></td><td><strong>3.98×</strong></td><td>7.6 vs 7.6 tok/s (<strong>100%</strong>)</td><td>120/120</td></tr><tr><td>Mistral-7B</td><td>128 / 8</td><td><strong>5.76×</strong></td><td><strong>4.03×</strong></td><td>6.8 vs 6.5 tok/s (<strong>105%</strong>)</td><td>122/122</td></tr></tbody></table>
<p><strong>Bit-width sweep (Llama-3.2-3B):</strong></p>
<table><thead><tr><th>Config</th><th>Key compression</th><th>Full-KV compression</th><th>Throughput</th></tr></thead><tbody><tr><td>fp16 baseline</td><td>1.00×</td><td>1.00×</td><td>16.0 tok/s</td></tr><tr><td>KIVI-2bit</td><td>5.79×</td><td>3.98×</td><td>16.3 tok/s</td></tr><tr><td>KIVI-3bit</td><td>4.34×</td><td>3.24×</td><td>15.9 tok/s</td></tr><tr><td>KIVI-4bit</td><td>3.47×</td><td>2.73×</td><td>16.0 tok/s</td></tr></tbody></table>
<p>Two honest observations from this data:</p>
<ol>
<li class=""><strong>Throughput is flat, not faster.</strong> KIVI here runs at 100–105% of fp16 — i.e. it does <em>not</em> slow generation down at these sizes, but it also doesn't speed it up. The published KIVI speedups come from a fused CUDA kernel; that kernel does not port to Metal. On Apple Silicon the deliverable is <strong>memory compression</strong> (the 4× full-KV figure), and the throughput column is there so you can see it costs you nothing, not so you can claim a speedup.</li>
<li class=""><strong>Full-KV compression is lower than key-only</strong>, and deliberately so. The full-KV figure includes the fp16 residual window. At <code>residual_length=32</code> over a 120-token generation, a meaningful slice stays in fp16; that drags the end-to-end ratio below the key-only number. We report both rather than quoting the flattering one.</li>
</ol>
<p>The implementation is covered by <strong>25 passing tests</strong> (<code>veloxquant_mlx/tests/quantizers/test_kivi.py</code> and <code>tests/cache/test_kivi_cache.py</code>), including reconstruction-fidelity bounds, the per-token/per-channel asymmetry, the residual-window behavior, and a determinism check.</p>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="honest-limitations">Honest limitations<a href="https://veloxquant-mlx.netlify.app/docs/blog/kivi#honest-limitations" class="hash-link" aria-label="Direct link to Honest limitations" title="Direct link to Honest limitations" translate="no">​</a></h2>
<ul>
<li class=""><strong>Memory win, not a speed win on Metal.</strong> As above — the CUDA kernel fusion from the paper isn't available here. If you need raw throughput, this isn't your lever.</li>
<li class=""><strong>Quality is measured as reconstruction fidelity, not task accuracy.</strong> Our tests check cosine similarity / MSE against the fp16 cache on synthetic and real key distributions. We have <strong>not</strong> run LongBench or a perplexity sweep across configs, so we do not claim "no quality loss" on downstream tasks.</li>
<li class=""><strong>2-bit is genuinely lossy.</strong> On unit-norm synthetic keys, KIVI-2bit reconstruction cosine sits around 0.93 — which is exactly <em>why</em> KIVI keeps an fp16 residual window. If you push <code>residual_length</code> to 0 you'll feel it. The defaults exist for a reason.</li>
<li class=""><strong>Single chip, short generations.</strong> Everything above is one M4 at ~120 generated tokens. Behavior across other M-series tiers and very long generations isn't characterized yet.</li>
</ul>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="where-kivi-fits">Where KIVI fits<a href="https://veloxquant-mlx.netlify.app/docs/blog/kivi#where-kivi-fits" class="hash-link" aria-label="Direct link to Where KIVI fits" title="Direct link to Where KIVI fits" translate="no">​</a></h2>
<ul>
<li class=""><strong>Reach for KIVI</strong> when you want a <em>simple, deterministic, calibration-free</em> 2-bit baseline — or when you specifically need to compare against the literature's reference point.</li>
<li class=""><strong>Reach for RVQ</strong> when you want stronger compression-per-bit with zero calibration and near-fp16 throughput (its analytical codebooks do better than scalar min/max at low bit-rates).</li>
<li class=""><strong>Reach for VecInfer</strong> when you want the most aggressive key compression (up to 16× key-only) and have ~2 minutes for codebook calibration.</li>
</ul>
<p>KIVI's job in the suite isn't to win every axis; it's to be the honest yardstick the others are measured against.</p>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="try-it">Try it<a href="https://veloxquant-mlx.netlify.app/docs/blog/kivi#try-it" class="hash-link" aria-label="Direct link to Try it" title="Direct link to Try it" translate="no">​</a></h2>
<div class="language-bash codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-bash codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">pip </span><span class="token function" style="color:rgb(80, 250, 123)">install</span><span class="token plain"> VeloxQuant-MLX</span><span class="token operator">==</span><span class="token number">0.8</span><span class="token plain">.0</span><br></div></code></pre></div></div>
<ul>
<li class="">Docs: <code>/docs/algorithms/kivi/</code></li>
<li class="">GitHub: <a href="https://github.com/rajveer43/VeloxQuant-MLX" target="_blank" rel="noopener noreferrer" class="">https://github.com/rajveer43/VeloxQuant-MLX</a></li>
<li class="">Paper: KIVI, <a href="https://arxiv.org/abs/2402.02750" target="_blank" rel="noopener noreferrer" class="">arXiv:2402.02750</a> (ICML 2024)</li>
</ul>
<hr>
<p><em>What was measured vs. not: all compression, throughput, peak-memory, and token-count figures are from committed <code>figures/kivi/*/results.json</code> on a single Apple M4 (24 GB) at ~120 generated tokens with ~2.2–2.4k-token prompts; correctness is from 25 passing unit tests. We did not measure downstream-task accuracy (e.g. LongBench, perplexity sweeps) — "quality" here means reconstruction fidelity against the fp16 cache.</em></p>]]></content>
        <author>
            <name>Rajveer Rathod</name>
            <uri>https://github.com/rajveer43</uri>
        </author>
        <category label="quantization" term="quantization"/>
        <category label="apple-silicon" term="apple-silicon"/>
        <category label="mlx" term="mlx"/>
        <category label="kv-cache" term="kv-cache"/>
        <category label="kivi" term="kivi"/>
    </entry>
    <entry>
        <title type="html"><![CDATA[Hands-On: Compressing Your First LLM with VeloxQuant-MLX]]></title>
        <id>https://veloxquant-mlx.netlify.app/docs/blog/hands-on</id>
        <link href="https://veloxquant-mlx.netlify.app/docs/blog/hands-on"/>
        <updated>2026-05-28T00:00:00.000Z</updated>
        <summary type="html"><![CDATA[How to cut your KV cache memory by 8x and hit fp16 throughput — step by step, with real models.]]></summary>
        <content type="html"><![CDATA[<p><em>How to cut your KV cache memory by 8x and hit fp16 throughput — step by step, with real models.</em></p>
<hr>
<p>If you have an Apple Silicon Mac and you run local LLMs, you have already hit the wall: your MacBook Pro generates 20 tokens per second on Mistral 7B, memory pressure turns on after a few thousand tokens, and anything larger than 7B crawls. The bottleneck is not compute — it is memory bandwidth. Every token requires loading the entire KV cache for every layer. Make the cache smaller, and the model gets faster.</p>
<p>VeloxQuant-MLX is a drop-in KV cache quantization library for MLX. It compresses the KV cache to 2, 3, or 4 bits per value while keeping text quality close to full-precision. In this guide you will install it, run your first compressed model, pick the right algorithm for your use case, and understand what the benchmark numbers mean. No machine learning background required.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="what-you-need">What You Need<a href="https://veloxquant-mlx.netlify.app/docs/blog/hands-on#what-you-need" class="hash-link" aria-label="Direct link to What You Need" title="Direct link to What You Need" translate="no">​</a></h2>
<ul>
<li class="">Apple Silicon Mac (M1 or later)</li>
<li class="">Python 3.11 or 3.12</li>
<li class="">At least 16 GB unified memory (8 GB works for 4B models)</li>
<li class=""><code>mlx-lm</code> installed (<code>pip install mlx-lm</code>)</li>
</ul>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="step-1-install-veloxquant-mlx">Step 1: Install VeloxQuant-MLX<a href="https://veloxquant-mlx.netlify.app/docs/blog/hands-on#step-1-install-veloxquant-mlx" class="hash-link" aria-label="Direct link to Step 1: Install VeloxQuant-MLX" title="Direct link to Step 1: Install VeloxQuant-MLX" translate="no">​</a></h2>
<div class="language-bash codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-bash codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">pip </span><span class="token function" style="color:rgb(80, 250, 123)">install</span><span class="token plain"> VeloxQuant-MLX</span><br></div></code></pre></div></div>
<p>That installs the <code>mlx_kv_quant</code> package plus the <code>veloxquant</code> CLI. Verify it worked:</p>
<div class="language-bash codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-bash codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">veloxquant </span><span class="token parameter variable" style="color:rgb(189, 147, 249);font-style:italic">--help</span><br></div></code></pre></div></div>
<p>You should see the precompute and benchmark subcommands.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="step-2-your-first-compressed-inference">Step 2: Your First Compressed Inference<a href="https://veloxquant-mlx.netlify.app/docs/blog/hands-on#step-2-your-first-compressed-inference" class="hash-link" aria-label="Direct link to Step 2: Your First Compressed Inference" title="Direct link to Step 2: Your First Compressed Inference" translate="no">​</a></h2>
<p>The fastest path uses the <code>KVCacheBuilder</code>. Here is a minimal script that runs Mistral 7B with 4-bit KV cache compression:</p>
<div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">import</span><span class="token plain"> mlx</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">core </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">as</span><span class="token plain"> mx</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">import</span><span class="token plain"> mlx_lm</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">from</span><span class="token plain"> mlx_kv_quant </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">import</span><span class="token plain"> KVCacheBuilder</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> KVCacheConfig</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token comment" style="color:rgb(98, 114, 164)"># Load the model normally</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">model</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> tokenizer </span><span class="token operator">=</span><span class="token plain"> mlx_lm</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">load</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token string" style="color:rgb(255, 121, 198)">"mlx-community/Mistral-7B-Instruct-v0.3-4bit"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token comment" style="color:rgb(98, 114, 164)"># Build a quantized KV cache</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">config </span><span class="token operator">=</span><span class="token plain"> KVCacheConfig</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">bits</span><span class="token operator">=</span><span class="token number">4</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> algorithm</span><span class="token operator">=</span><span class="token string" style="color:rgb(255, 121, 198)">"turboquant_prod"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">cache </span><span class="token operator">=</span><span class="token plain"> KVCacheBuilder</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">config</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">for_model</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">model</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">build</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token comment" style="color:rgb(98, 114, 164)"># Generate with the compressed cache</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">prompt </span><span class="token operator">=</span><span class="token plain"> </span><span class="token string" style="color:rgb(255, 121, 198)">"Explain the difference between RAM and unified memory."</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">tokens </span><span class="token operator">=</span><span class="token plain"> mlx_lm</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">generate</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">model</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> tokenizer</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> prompt</span><span class="token operator">=</span><span class="token plain">prompt</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> kv_cache</span><span class="token operator">=</span><span class="token plain">cache</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> max_tokens</span><span class="token operator">=</span><span class="token number">200</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">print</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">tokens</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><br></div></code></pre></div></div>
<p>That is the entire integration. The cache is transparent to <code>mlx_lm.generate()</code> — you do not change anything else.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="step-3-understanding-the-algorithm-choices">Step 3: Understanding the Algorithm Choices<a href="https://veloxquant-mlx.netlify.app/docs/blog/hands-on#step-3-understanding-the-algorithm-choices" class="hash-link" aria-label="Direct link to Step 3: Understanding the Algorithm Choices" title="Direct link to Step 3: Understanding the Algorithm Choices" translate="no">​</a></h2>
<p>VeloxQuant-MLX ships five quantization algorithms. They trade reconstruction quality against memory and speed:</p>
<table><thead><tr><th>Algorithm name</th><th>Bits/dim</th><th>Best for</th><th>Notes</th></tr></thead><tbody><tr><td><code>turboquant_mse</code></td><td>b</td><td>Fast inference, b &gt;= 3</td><td>MSE-optimal Lloyd-Max codebook</td></tr><tr><td><code>turboquant_prod</code></td><td>b + 1</td><td>Inner-product tasks, RAG</td><td>Adds 1-bit QJL residual correction</td></tr><tr><td><code>turboquant_rvq</code></td><td>2b</td><td>2-bit where quality matters</td><td>Two-pass residual; cosine 0.98 at b=2</td></tr><tr><td><code>polarquant</code></td><td>b</td><td>Long contexts</td><td>Spherical Lloyd-Max, no norm storage</td></tr><tr><td><code>qjl</code></td><td>1</td><td>Ultra-low memory</td><td>Sign sketch only; rough approximation</td></tr></tbody></table>
<p><strong>Rule of thumb:</strong></p>
<ul>
<li class="">For most uses at 3–4 bit: use <code>turboquant_prod</code>. It is the default and the most tested.</li>
<li class="">At 2 bits: always use <code>turboquant_rvq</code>. The single-pass algorithms fall apart at 2-bit; RVQ does not.</li>
<li class="">For long contexts where cosine similarity matters more than reconstruction: <code>polarquant</code>.</li>
<li class="">To stress-test memory limits: <code>qjl</code>.</li>
</ul>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="step-4-using-rvq-2-bit-the-interesting-one">Step 4: Using RVQ 2-Bit (the Interesting One)<a href="https://veloxquant-mlx.netlify.app/docs/blog/hands-on#step-4-using-rvq-2-bit-the-interesting-one" class="hash-link" aria-label="Direct link to Step 4: Using RVQ 2-Bit (the Interesting One)" title="Direct link to Step 4: Using RVQ 2-Bit (the Interesting One)" translate="no">​</a></h2>
<p>The headline feature in v0.3.0 is <code>TurboQuantRVQ</code> — a two-pass residual vector quantizer that makes 2-bit KV cache actually usable.</p>
<p>The problem with naive 2-bit quantization: you only have 4 levels to represent a continuous value. The reconstruction error is large enough that attention scores get corrupted, and long reasoning chains (like Qwen3's <code>&lt;think&gt;</code> mode) collapse into repetition after a few dozen tokens.</p>
<p>RVQ fixes this by running two codebooks. The first pass quantizes the rotated vector. The second pass quantizes the residual — the error left over from the first pass — using a Laplacian-fit codebook (which matches the residual distribution better than a Gaussian one). The result: cosine similarity goes from 0.69 to 0.98 at 2 bits.</p>
<p>Here is how to use it:</p>
<div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">from</span><span class="token plain"> mlx_kv_quant </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">import</span><span class="token plain"> KVCacheBuilder</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> KVCacheConfig</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">config </span><span class="token operator">=</span><span class="token plain"> KVCacheConfig</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">bits</span><span class="token operator">=</span><span class="token number">2</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> algorithm</span><span class="token operator">=</span><span class="token string" style="color:rgb(255, 121, 198)">"turboquant_rvq"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">cache </span><span class="token operator">=</span><span class="token plain"> KVCacheBuilder</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">config</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">for_model</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">model</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">build</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><br></div></code></pre></div></div>
<p>That's it. The <code>turboquant_rvq</code> string routes to the registered <code>TurboQuantRVQ</code> class via the quantizer registry.</p>
<p><strong>When should you use RVQ 2-bit?</strong></p>
<p>Use it when memory is the binding constraint and you need coherent long-form output. It delivers:</p>
<ul>
<li class="">Mistral 7B at 2-bit: 22.3 tok/s — matches fp16 throughput (22.1 tok/s) on an M4 MacBook</li>
<li class="">Qwen3 4B at 2-bit: 36.0 tok/s (92% of fp16) with full thinking-mode output</li>
</ul>
<p>Use standard 3-bit or 4-bit when you have memory headroom and want zero quality trade-off.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="step-5-running-benchmarks-on-your-mac">Step 5: Running Benchmarks on Your Mac<a href="https://veloxquant-mlx.netlify.app/docs/blog/hands-on#step-5-running-benchmarks-on-your-mac" class="hash-link" aria-label="Direct link to Step 5: Running Benchmarks on Your Mac" title="Direct link to Step 5: Running Benchmarks on Your Mac" translate="no">​</a></h2>
<p>VeloxQuant-MLX ships benchmark scripts for several models. These measure tok/s and output completeness across five configurations: fp16, 2-bit RVQ, 3-bit, 4-bit, and 2-bit single-pass (for comparison).</p>
<p><strong>Qwen3 4B</strong> (good for 16GB Macs):</p>
<div class="language-bash codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-bash codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">python3 benchmark_qwen3_4b_v2.py</span><br></div></code></pre></div></div>
<p><strong>Mistral 7B</strong> (needs 16GB+, comfortable at 32GB):</p>
<div class="language-bash codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-bash codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">python3 benchmark_mistral7b_v2.py</span><br></div></code></pre></div></div>
<p>Each script runs all five configs, prints a results table, and saves six figures to <code>figures/updated_tests/&lt;model&gt;/</code>. The figures include:</p>
<ul>
<li class="">Throughput comparison bar chart</li>
<li class="">Token completeness (how many tokens were coherent out of 200)</li>
<li class="">Memory usage per config</li>
<li class="">Cosine similarity vs fp16 reference</li>
</ul>
<p>Here is what our numbers look like on an M4 MacBook (16GB):</p>
<p><strong>Mistral 7B throughput:</strong></p>
<table><thead><tr><th>Config</th><th>tok/s</th><th>Memory</th></tr></thead><tbody><tr><td>fp16</td><td>22.1</td><td>14.2 GB</td></tr><tr><td>RVQ 2-bit</td><td>22.3</td><td>3.8 GB</td></tr><tr><td>3-bit</td><td>21.8</td><td>5.4 GB</td></tr><tr><td>4-bit</td><td>21.6</td><td>7.1 GB</td></tr></tbody></table>
<p>Mistral 7B is memory-bandwidth bound — every config saturates around 22 tok/s because that is the bandwidth ceiling. But RVQ 2-bit uses 10x less cache memory than fp16.</p>
<p><strong>Qwen3 4B thinking-mode throughput:</strong></p>
<table><thead><tr><th>Config</th><th>tok/s</th><th>Tokens (out of 200)</th></tr></thead><tbody><tr><td>fp16</td><td>39.2</td><td>200 / 200</td></tr><tr><td>RVQ 2-bit</td><td>36.0</td><td>199 / 200</td></tr><tr><td>3-bit</td><td>37.1</td><td>200 / 200</td></tr><tr><td>4-bit TQ</td><td>24.8</td><td>50 / 200</td></tr></tbody></table>
<p>The 4-bit single-pass result (50/200) is the failure mode for standard quantization on a thinking model. RVQ 2-bit produces full coherent output and runs faster.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="step-6-integrating-with-mlx-lm-properly">Step 6: Integrating with mlx-lm Properly<a href="https://veloxquant-mlx.netlify.app/docs/blog/hands-on#step-6-integrating-with-mlx-lm-properly" class="hash-link" aria-label="Direct link to Step 6: Integrating with mlx-lm Properly" title="Direct link to Step 6: Integrating with mlx-lm Properly" translate="no">​</a></h2>
<p>For real usage you want the cache to persist across generation steps. Here is the pattern for a chat loop:</p>
<div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">import</span><span class="token plain"> mlx</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">core </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">as</span><span class="token plain"> mx</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">import</span><span class="token plain"> mlx_lm</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">from</span><span class="token plain"> mlx_kv_quant </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">import</span><span class="token plain"> KVCacheBuilder</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> KVCacheConfig</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">model</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> tokenizer </span><span class="token operator">=</span><span class="token plain"> mlx_lm</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">load</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token string" style="color:rgb(255, 121, 198)">"mlx-community/Qwen3-4B"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">config </span><span class="token operator">=</span><span class="token plain"> KVCacheConfig</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">bits</span><span class="token operator">=</span><span class="token number">2</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> algorithm</span><span class="token operator">=</span><span class="token string" style="color:rgb(255, 121, 198)">"turboquant_rvq"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">cache </span><span class="token operator">=</span><span class="token plain"> KVCacheBuilder</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">config</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">for_model</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">model</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">build</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">messages </span><span class="token operator">=</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">while</span><span class="token plain"> </span><span class="token boolean">True</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    user_input </span><span class="token operator">=</span><span class="token plain"> </span><span class="token builtin" style="color:rgb(189, 147, 249)">input</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token string" style="color:rgb(255, 121, 198)">"You: "</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">if</span><span class="token plain"> user_input</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">lower</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">in</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token string" style="color:rgb(255, 121, 198)">"exit"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token string" style="color:rgb(255, 121, 198)">"quit"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">break</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    messages</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">append</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token string" style="color:rgb(255, 121, 198)">"role"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain"> </span><span class="token string" style="color:rgb(255, 121, 198)">"user"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token string" style="color:rgb(255, 121, 198)">"content"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain"> user_input</span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    prompt </span><span class="token operator">=</span><span class="token plain"> tokenizer</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">apply_chat_template</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">messages</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> tokenize</span><span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> add_generation_prompt</span><span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    response </span><span class="token operator">=</span><span class="token plain"> mlx_lm</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">generate</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        model</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        tokenizer</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        prompt</span><span class="token operator">=</span><span class="token plain">prompt</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        kv_cache</span><span class="token operator">=</span><span class="token plain">cache</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        max_tokens</span><span class="token operator">=</span><span class="token number">512</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        verbose</span><span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    messages</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">append</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token string" style="color:rgb(255, 121, 198)">"role"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain"> </span><span class="token string" style="color:rgb(255, 121, 198)">"assistant"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token string" style="color:rgb(255, 121, 198)">"content"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain"> response</span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">print</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token string-interpolation string" style="color:rgb(255, 121, 198)">f"Assistant: </span><span class="token string-interpolation interpolation punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token string-interpolation interpolation">response</span><span class="token string-interpolation interpolation punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token string-interpolation string" style="color:rgb(255, 121, 198)">\n"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><br></div></code></pre></div></div>
<p>The <code>kv_cache</code> argument is passed directly to <code>mlx_lm.generate()</code>. No other changes. The cache grows with each turn and is quantized transparently.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="step-7-precomputing-codebooks-for-repeated-use">Step 7: Precomputing Codebooks for Repeated Use<a href="https://veloxquant-mlx.netlify.app/docs/blog/hands-on#step-7-precomputing-codebooks-for-repeated-use" class="hash-link" aria-label="Direct link to Step 7: Precomputing Codebooks for Repeated Use" title="Direct link to Step 7: Precomputing Codebooks for Repeated Use" translate="no">​</a></h2>
<p>If you run the same model repeatedly, you can precompute the Lloyd-Max codebooks offline and load them at inference time. This eliminates the one-time calibration cost at startup:</p>
<div class="language-bash codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-bash codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token comment" style="color:rgb(98, 114, 164)"># Precompute and save codebooks for Mistral 7B, 4-bit</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">veloxquant precompute </span><span class="token punctuation" style="color:rgb(248, 248, 242)">\</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token parameter variable" style="color:rgb(189, 147, 249);font-style:italic">--model</span><span class="token plain"> mlx-community/Mistral-7B-Instruct-v0.3-4bit </span><span class="token punctuation" style="color:rgb(248, 248, 242)">\</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token parameter variable" style="color:rgb(189, 147, 249);font-style:italic">--bits</span><span class="token plain"> </span><span class="token number">4</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">\</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token parameter variable" style="color:rgb(189, 147, 249);font-style:italic">--algorithm</span><span class="token plain"> turboquant_prod </span><span class="token punctuation" style="color:rgb(248, 248, 242)">\</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token parameter variable" style="color:rgb(189, 147, 249);font-style:italic">--output</span><span class="token plain"> codebooks/mistral7b_4bit/</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token comment" style="color:rgb(98, 114, 164)"># Later, load at inference time</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">config </span><span class="token operator">=</span><span class="token plain"> KVCacheConfig</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token assign-left variable" style="color:rgb(189, 147, 249);font-style:italic">bits</span><span class="token operator">=</span><span class="token number">4</span><span class="token plain">,</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token assign-left variable" style="color:rgb(189, 147, 249);font-style:italic">algorithm</span><span class="token operator">=</span><span class="token string" style="color:rgb(255, 121, 198)">"turboquant_prod"</span><span class="token plain">,</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token assign-left variable" style="color:rgb(189, 147, 249);font-style:italic">artifact_dir</span><span class="token operator">=</span><span class="token string" style="color:rgb(255, 121, 198)">"codebooks/mistral7b_4bit/"</span><span class="token plain">,</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">cache </span><span class="token operator">=</span><span class="token plain"> KVCacheBuilder</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">config</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain">.for_model</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">model</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain">.build</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><br></div></code></pre></div></div>
<p>Codebook size is tiny (kilobytes). You can commit them to your project directory and avoid the calibration step entirely on every run.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="step-8-picking-the-right-bit-width">Step 8: Picking the Right Bit Width<a href="https://veloxquant-mlx.netlify.app/docs/blog/hands-on#step-8-picking-the-right-bit-width" class="hash-link" aria-label="Direct link to Step 8: Picking the Right Bit Width" title="Direct link to Step 8: Picking the Right Bit Width" translate="no">​</a></h2>
<p>Here is the practical decision tree:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">Do you have &gt;= 32GB memory?</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  Yes → fp16 is fine; use VeloxQuant only for very long contexts</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  No  → continue</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">Is your model &gt;= 7B parameters?</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  Yes → 3-bit TurboQuant (turboquant_prod)</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  No  → 2-bit RVQ (turboquant_rvq)</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">Are you doing RAG or similarity search over the KV cache?</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  Yes → turboquant_prod (inner-product correction matters)</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  No  → turboquant_mse (simpler, same quality)</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">Do you need to run 13B+ on a 16GB Mac?</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  Yes → 2-bit RVQ — the only option that keeps quality</span><br></div></code></pre></div></div>
<p>For generation tasks (chat, summarization, code completion), MSE-optimal reconstruction is what you want. For retrieval tasks that compute attention over a cached context, inner-product preservation matters more — use <code>turboquant_prod</code>.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="step-9-what-the-optimization-journey-looked-like">Step 9: What the Optimization Journey Looked Like<a href="https://veloxquant-mlx.netlify.app/docs/blog/hands-on#step-9-what-the-optimization-journey-looked-like" class="hash-link" aria-label="Direct link to Step 9: What the Optimization Journey Looked Like" title="Direct link to Step 9: What the Optimization Journey Looked Like" translate="no">​</a></h2>
<p>Getting from 17.7 tok/s to 22.3 tok/s on Mistral 7B RVQ 2-bit required four changes, each independently measurable:</p>
<p><strong>1. Batch all heads into one MLX call (+22%)</strong></p>
<p>The original code ran a Python <code>for h in range(H)</code> loop over attention heads, giving each head its own quantizer instance. For Mistral 7B (8 heads, 32 layers) that meant 256 small kernel dispatches per token. The fix: reshape <code>(B, H, S, D)</code> to <code>(B*H*S, D)</code> and call one shared quantizer once. MLX already handles batched input — we just stopped fragmenting it.</p>
<p><strong>2. Switch to Hadamard rotation</strong></p>
<p>The default preconditioner used QR decomposition: a full <code>(d, d)</code> matrix multiply (16,384 ops at d=128). <code>mx.hadamard_transform</code> is a Metal-native fused kernel that does the same job in O(d log d) — 896 ops at d=128, ~18x less arithmetic. Quality is mathematically identical because Hadamard with random ±1 diagonal is also a Haar-equivalent rotation.</p>
<p><strong>3. Replace broadcast-argmin with boundary-sum</strong></p>
<p>Codebook lookup materialized a <code>(batch, 128, k)</code> distance tensor for argmin — three kernels: broadcast-subtract, abs, argmin. Lloyd-Max boundaries are just midpoints between sorted centroids. The nearest centroid index is the number of boundaries the value exceeds: one comparison and one sum, two kernels total. Index match vs the old path: 100.0000%.</p>
<p><strong>4. Drop redundant fp32 casts</strong></p>
<p>The update path was casting fp16 → fp32 → fp16 → fp32 → fp16 unnecessarily because the rotation already handles internal precision internally. Removing the round-trips saved ~1.05x per call and is invisible to output quality.</p>
<p>The full write-up with stage-by-stage numbers is in <a href="https://github.com/rajveer43/VeloxQuant-MLX/blob/master/OPTIMIZATION_FINDINGS.md" target="_blank" rel="noopener noreferrer" class="">OPTIMIZATION_FINDINGS.md</a>.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="step-10-understanding-memory-numbers">Step 10: Understanding Memory Numbers<a href="https://veloxquant-mlx.netlify.app/docs/blog/hands-on#step-10-understanding-memory-numbers" class="hash-link" aria-label="Direct link to Step 10: Understanding Memory Numbers" title="Direct link to Step 10: Understanding Memory Numbers" translate="no">​</a></h2>
<p>KV cache memory scales with: <code>2 × n_layers × n_kv_heads × head_dim × seq_len × bytes_per_element</code></p>
<p>For Mistral 7B (32 layers, 8 KV heads, head_dim 128) at sequence length 2048:</p>
<ul>
<li class="">fp16: 2 × 32 × 8 × 128 × 2048 × 2 bytes = <strong>536 MB</strong></li>
<li class="">4-bit: 268 MB (2x reduction)</li>
<li class="">2-bit RVQ: 134 MB (4x reduction, plus codebook overhead ~8 KB)</li>
</ul>
<p>At longer contexts (32K tokens), these numbers scale linearly:</p>
<ul>
<li class="">fp16: 8.4 GB for context alone</li>
<li class="">2-bit RVQ: 2.1 GB</li>
</ul>
<p>On a 16GB Mac, the difference between fp16 and 2-bit RVQ at 32K context is the difference between OOM and running.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="common-questions">Common Questions<a href="https://veloxquant-mlx.netlify.app/docs/blog/hands-on#common-questions" class="hash-link" aria-label="Direct link to Common Questions" title="Direct link to Common Questions" translate="no">​</a></h2>
<p><strong>Does this work with quantized models (4-bit weights)?</strong></p>
<p>Yes. The KV cache quantization is independent of weight quantization. You can run a 4-bit weight model with a 2-bit KV cache. The two compressions are additive: <code>mlx-community/Mistral-7B-Instruct-v0.3-4bit</code> loads as a 4-bit weight model; the KV cache is then further compressed by VeloxQuant.</p>
<p><strong>Will I notice quality degradation?</strong></p>
<p>At 4-bit and 3-bit: barely measurable on generation tasks. At 2-bit with RVQ: cosine similarity 0.98 (vs 1.0 for fp16). On our Qwen3 4B thinking-mode test, RVQ 2-bit produced 199/200 coherent tokens vs fp16's 200/200. One token difference across a 200-token reasoning chain.</p>
<p>At 2-bit without RVQ (single-pass): noticeable. Thinking-mode models collapse early. Use RVQ.</p>
<p><strong>What models are supported?</strong></p>
<p>Any model that uses standard multi-head attention with <code>mlx_lm.make_cache()</code>. Tested: Mistral 7B, Qwen3 4B, Qwen3 8B, Qwen2.5 32B, Falcon3 7B, Phi-4, Gemma-4. Not supported: architectures with multi-latent attention (DeepSeek-V2) or non-standard cache formats.</p>
<p><strong>Does it work with streaming generation?</strong></p>
<p>Yes. The cache is stateful — each <code>generate()</code> call updates it. Stream tokens with the standard <code>mlx_lm</code> streaming API; the cache update happens transparently inside the model forward pass.</p>
<hr>
<p><em>VeloxQuant-MLX is MIT licensed. Contributions welcome — especially benchmark results for new models and hardware.</em></p>]]></content>
        <author>
            <name>Rajveer Rathod</name>
            <uri>https://github.com/rajveer43</uri>
        </author>
        <category label="tutorial" term="tutorial"/>
        <category label="apple-silicon" term="apple-silicon"/>
        <category label="mlx" term="mlx"/>
        <category label="kv-cache" term="kv-cache"/>
    </entry>
    <entry>
        <title type="html"><![CDATA[I Wrote a Metal Kernel to Stop My Mac From OOMing]]></title>
        <id>https://veloxquant-mlx.netlify.app/docs/blog/metal-kernels</id>
        <link href="https://veloxquant-mlx.netlify.app/docs/blog/metal-kernels"/>
        <updated>2026-05-25T00:00:00.000Z</updated>
        <summary type="html"><![CDATA[How a 30-line Metal compute shader replaced the worst hot path in VeloxQuant-MLX 0.5.1, what I learned about Apple Silicon kernel launch overhead, and why this matters if you run LLMs locally on Mac.]]></summary>
        <content type="html"><![CDATA[<p><em>How a 30-line Metal compute shader replaced the worst hot path in VeloxQuant-MLX 0.5.1, what I learned about Apple Silicon kernel launch overhead, and why this matters if you run LLMs locally on Mac.</em></p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="the-bug-that-wouldnt-die">The Bug That Wouldn't Die<a href="https://veloxquant-mlx.netlify.app/docs/blog/metal-kernels#the-bug-that-wouldnt-die" class="hash-link" aria-label="Direct link to The Bug That Wouldn't Die" title="Direct link to The Bug That Wouldn't Die" translate="no">​</a></h2>
<p>A few weeks back I shipped VeloxQuant-MLX 0.5.0 — a Python library that compresses the KV cache for any model you load through <code>mlx_lm</code>. The headline algorithm is <strong>VecInfer</strong>, which uses product vector quantization to squeeze keys down to 1 bit per element. That is <strong>16× compression</strong>. Sounds great.</p>
<p>It worked beautifully on Llama-3.1-8B, Mistral-7B, Qwen2.5-7B, Phi-4 — every model with <code>head_dim=128</code>. And then I tested Falcon3-7B.</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">[VecInfer-2bit] generating...</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  Out of memory: requested 712 MB, available 0</span><br></div></code></pre></div></div>
<p>Falcon3-7B has <code>head_dim=256</code>. The chunked nearest-centroid search at the heart of <code>quantize_vq</code> allocates a tensor of shape <code>[chunk_size, n_centroids, sub_dim]</code> on every chunk. For Falcon's geometry that's a multi-hundred-megabyte intermediate — at every single token, on every layer, on every step. The GPU runs out of memory before generating a single token.</p>
<p>I shipped 0.5.0 with the OOM marked as a known limitation. It bothered me. I knew the fix conceptually — accumulate the squared distance in registers, never materialize the diff matrix — but doing that meant writing a Metal compute shader, and I had never written one.</p>
<p>This post is what happened when I did.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="what-even-is-a-kv-cache-and-why-should-you-care">What Even Is a KV Cache And Why Should You Care<a href="https://veloxquant-mlx.netlify.app/docs/blog/metal-kernels#what-even-is-a-kv-cache-and-why-should-you-care" class="hash-link" aria-label="Direct link to What Even Is a KV Cache And Why Should You Care" title="Direct link to What Even Is a KV Cache And Why Should You Care" translate="no">​</a></h2>
<p>Quick recap. Every transformer layer needs to remember the keys and values it computed for every token it's already seen. For a 7B model with 32 layers, 8 KV heads, and head_dim=128, generating an 8,000-token response means storing:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">32 layers × 8 heads × 8000 tokens × 128 dims × 2 (K + V) × 2 bytes (fp16)</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">≈ 1 GB</span><br></div></code></pre></div></div>
<p>On a 16 GB MacBook running the model weights (~5 GB at 4-bit) plus the OS and your app, that 1 GB is the difference between a fluent response and a hard crash. <strong>The KV cache is the silent killer of long-context inference on Mac.</strong></p>
<p>KV-cache <em>quantization</em> — storing those keys and values at fewer bits — is the answer. There are several flavors. The aggressive one I shipped, VecInfer, uses <strong>product vector quantization</strong>:</p>
<ol>
<li class="">Split each <code>[head_dim]</code> key vector into small sub-vectors of length <code>sub_dim</code> (typically 4 or 8).</li>
<li class="">Pre-train a codebook of K-means centroids on calibration data.</li>
<li class="">At inference, encode each sub-vector as the index of its nearest centroid.</li>
</ol>
<p>A 128-dim fp16 key (256 bytes) becomes 16 indices at 8 bits each (16 bytes). That's the 16× compression.</p>
<p>The hot operation is step 3: finding the nearest centroid. On every layer, on every token, you do a vectorized argmin against the codebook. That's <code>quantize_vq</code>.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="what-quantize_vq-was-doing-wrong">What <code>quantize_vq</code> Was Doing Wrong<a href="https://veloxquant-mlx.netlify.app/docs/blog/metal-kernels#what-quantize_vq-was-doing-wrong" class="hash-link" aria-label="Direct link to what-quantize_vq-was-doing-wrong" title="Direct link to what-quantize_vq-was-doing-wrong" translate="no">​</a></h2>
<p>Here's what the pure-MLX implementation looks like (paraphrased):</p>
<div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">def</span><span class="token plain"> </span><span class="token function" style="color:rgb(80, 250, 123)">quantize_vq</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">x</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> codebook</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> sub_dim</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token comment" style="color:rgb(98, 114, 164)"># x: [N, sub_dim]    -- the sub-vectors to encode</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token comment" style="color:rgb(98, 114, 164)"># codebook: [n_centroids, sub_dim]</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    diff </span><span class="token operator">=</span><span class="token plain"> x</span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token boolean">None</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token plain"> </span><span class="token operator">-</span><span class="token plain"> codebook</span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token boolean">None</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token plain">  </span><span class="token comment" style="color:rgb(98, 114, 164)"># [N, n_centroids, sub_dim]</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    d2 </span><span class="token operator">=</span><span class="token plain"> mx</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token builtin" style="color:rgb(189, 147, 249)">sum</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">diff </span><span class="token operator">*</span><span class="token plain"> diff</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> axis</span><span class="token operator">=</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain">  </span><span class="token comment" style="color:rgb(98, 114, 164)"># [N, n_centroids]</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">return</span><span class="token plain"> mx</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">argmin</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">d2</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> axis</span><span class="token operator">=</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain">  </span><span class="token comment" style="color:rgb(98, 114, 164)"># [N]</span><br></div></code></pre></div></div>
<p>That <code>diff</code> tensor is the killer. Its shape is <code>[N, n_centroids, sub_dim]</code>. For Falcon3-7B-shape inputs:</p>
<ul>
<li class=""><code>N = 4096 tokens × 4 KV heads × 64 sub-vectors per head = 1,048,576</code></li>
<li class=""><code>n_centroids = 256</code></li>
<li class=""><code>sub_dim = 4</code></li>
<li class="">Total: 1,048,576 × 256 × 4 × 2 bytes (fp16) = <strong>2.1 GB intermediate</strong></li>
</ul>
<p>The implementation tries to mitigate this by chunking N — processing 4,096 sub-vectors at a time — but even one chunk is still ~32 MB, and a 7B model's GPU memory pressure means even that gets fragmented and OOMs in practice.</p>
<p>What you actually want is for each thread to compute the argmin <strong>in registers</strong>, only writing out a single uint32 index. No intermediate tensor. Total intermediate memory: zero.</p>
<p>That's exactly what a Metal compute kernel can do.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="what-is-mlx-mxfastmetal_kernel">What Is MLX <code>mx.fast.metal_kernel</code>?<a href="https://veloxquant-mlx.netlify.app/docs/blog/metal-kernels#what-is-mlx-mxfastmetal_kernel" class="hash-link" aria-label="Direct link to what-is-mlx-mxfastmetal_kernel" title="Direct link to what-is-mlx-mxfastmetal_kernel" translate="no">​</a></h2>
<p>MLX (Apple's array library for Apple Silicon) has a feature most people don't know about: <code>mx.fast.metal_kernel</code>. It lets you write a Metal Shading Language function inline as a Python string and have MLX JIT-compile it, manage the buffer bindings, and dispatch it on the GPU.</p>
<p>The whole thing takes a few lines of Python:</p>
<div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">kernel </span><span class="token operator">=</span><span class="token plain"> mx</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">fast</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">metal_kernel</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    name</span><span class="token operator">=</span><span class="token string" style="color:rgb(255, 121, 198)">"vecinfer_quantize"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    input_names</span><span class="token operator">=</span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token string" style="color:rgb(255, 121, 198)">"x"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token string" style="color:rgb(255, 121, 198)">"codebook"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    output_names</span><span class="token operator">=</span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token string" style="color:rgb(255, 121, 198)">"out"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    source</span><span class="token operator">=</span><span class="token plain">METAL_SOURCE</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain">  </span><span class="token comment" style="color:rgb(98, 114, 164)"># a string of MSL</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">result </span><span class="token operator">=</span><span class="token plain"> kernel</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    inputs</span><span class="token operator">=</span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token plain">x</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> codebook</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    output_shapes</span><span class="token operator">=</span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">N</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    output_dtypes</span><span class="token operator">=</span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token plain">mx</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">uint32</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    grid</span><span class="token operator">=</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">N</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    threadgroup</span><span class="token operator">=</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token number">256</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><br></div></code></pre></div></div>
<p>MLX handles all the boilerplate: function signature generation, dtype binding, threadgroup memory, dispatch encoding. You write the kernel body. It's the easiest GPU programming experience I've ever had — closer to writing a Python function than to traditional CUDA.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="the-kernel-18-lines-of-msl">The Kernel: 18 Lines of MSL<a href="https://veloxquant-mlx.netlify.app/docs/blog/metal-kernels#the-kernel-18-lines-of-msl" class="hash-link" aria-label="Direct link to The Kernel: 18 Lines of MSL" title="Direct link to The Kernel: 18 Lines of MSL" translate="no">​</a></h2>
<p>Here's the entire fused-argmin kernel that replaces that 2 GB intermediate tensor:</p>
<div class="language-metal codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-metal codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">uint vec_idx = thread_position_in_grid.x;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">uint N_total = x_shape[0];</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">if (vec_idx &gt;= N_total) {</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    return;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">}</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">uint n_centroids = codebook_shape[0];</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">uint sub_dim     = codebook_shape[1];</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">uint x_base      = vec_idx * sub_dim;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">// Track running argmin in registers — never materialize the diff matrix.</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">float best_dist = INFINITY;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">uint  best_idx  = 0;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">for (uint c = 0; c &lt; n_centroids; ++c) {</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    uint cb_base = c * sub_dim;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    float dist = 0.0f;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    for (uint i = 0; i &lt; sub_dim; ++i) {</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        float d = float(x[x_base + i]) - float(codebook[cb_base + i]);</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        dist += d * d;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    }</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    if (dist &lt; best_dist) {</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        best_dist = dist;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        best_idx  = c;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    }</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">}</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">out[vec_idx] = best_idx;</span><br></div></code></pre></div></div>
<p>That's it. Each GPU thread handles one sub-vector. It loops over all centroids, accumulates squared distance in a single float register, tracks the running minimum, and writes one uint32 index. The intermediate "diff matrix" never exists anywhere except in those two register-resident floats per thread.</p>
<p>Memory complexity: <code>O(N)</code> total output, vs <code>O(N × n_centroids × sub_dim)</code> for the Python path.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="the-numbers">The Numbers<a href="https://veloxquant-mlx.netlify.app/docs/blog/metal-kernels#the-numbers" class="hash-link" aria-label="Direct link to The Numbers" title="Direct link to The Numbers" translate="no">​</a></h2>
<p>I wrote a benchmark script — <code>scripts/plot_metal_benchmarks.py</code> in the repo — that runs both paths across realistic shapes and saves figures. Here are the results.</p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="throughput-69147-speedup">Throughput: 6.9–14.7× Speedup<a href="https://veloxquant-mlx.netlify.app/docs/blog/metal-kernels#throughput-69147-speedup" class="hash-link" aria-label="Direct link to Throughput: 6.9–14.7× Speedup" title="Direct link to Throughput: 6.9–14.7× Speedup" translate="no">​</a></h3>
<table><thead><tr><th>Shape</th><th style="text-align:right">pure-MLX</th><th style="text-align:right">Metal</th><th style="text-align:right">Speedup</th></tr></thead><tbody><tr><td>S=128, D=128</td><td style="text-align:right">3.64 ms</td><td style="text-align:right">0.53 ms</td><td style="text-align:right"><strong>6.9×</strong></td></tr><tr><td>S=512, D=128</td><td style="text-align:right">13.5 ms</td><td style="text-align:right">1.26 ms</td><td style="text-align:right"><strong>10.7×</strong></td></tr><tr><td>S=2048, D=128</td><td style="text-align:right">55.1 ms</td><td style="text-align:right">4.18 ms</td><td style="text-align:right"><strong>13.2×</strong></td></tr><tr><td>S=8192, D=128</td><td style="text-align:right">228.6 ms</td><td style="text-align:right">15.6 ms</td><td style="text-align:right"><strong>14.7×</strong></td></tr><tr><td>S=1024, D=256</td><td style="text-align:right">27.0 ms</td><td style="text-align:right">2.23 ms</td><td style="text-align:right"><strong>12.1×</strong></td></tr><tr><td>S=4096, D=256</td><td style="text-align:right">108.8 ms</td><td style="text-align:right">7.98 ms</td><td style="text-align:right"><strong>13.6×</strong></td></tr></tbody></table>
<p>The speedup scales with sequence length — longer contexts (where the Python path is bandwidth-bound on those huge diff tensors) get bigger wins. At <code>S=8192, D=128</code> we go from 228 ms per call to 16 ms per call. Per call. Multiply by 32 layers × 1 quantize per step × hundreds of tokens and you're talking minutes saved per long generation.</p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="memory-729-mb--12-mb">Memory: 729 MB → 12 MB<a href="https://veloxquant-mlx.netlify.app/docs/blog/metal-kernels#memory-729-mb--12-mb" class="hash-link" aria-label="Direct link to Memory: 729 MB → 12 MB" title="Direct link to Memory: 729 MB → 12 MB" translate="no">​</a></h3>
<p>At the Falcon3-7B OOM trigger shape (<code>head_dim=256, n_centroids=256, sub_dim=4, S=4096</code>):</p>
<table><thead><tr><th>Path</th><th style="text-align:right">Peak memory</th></tr></thead><tbody><tr><td>Pure-MLX <code>quantize_vq</code></td><td style="text-align:right"><strong>729.3 MB</strong></td></tr><tr><td>Metal <code>vecinfer_quantize_metal</code></td><td style="text-align:right"><strong>12.0 MB</strong></td></tr><tr><td>Reduction</td><td style="text-align:right"><strong>98.4%</strong> (saved 717 MB)</td></tr></tbody></table>
<p>This is the result that matters. The kernel doesn't just make existing models faster — it makes models that previously OOMed actually run.</p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="correctness-bit-exact-on-fp32-mse-identical-on-fp16">Correctness: Bit-Exact on fp32, MSE-Identical on fp16<a href="https://veloxquant-mlx.netlify.app/docs/blog/metal-kernels#correctness-bit-exact-on-fp32-mse-identical-on-fp16" class="hash-link" aria-label="Direct link to Correctness: Bit-Exact on fp32, MSE-Identical on fp16" title="Direct link to Correctness: Bit-Exact on fp32, MSE-Identical on fp16" translate="no">​</a></h3>
<p>This is where I had to be careful. The Metal kernel and the pure-MLX path don't produce identical indices on fp16 inputs — about <strong>0.1% of indices differ</strong>.</p>
<p>Why? When two centroids are nearly equidistant from a point, the choice of "nearest" depends on the order of floating-point operations. The pure-MLX path does the subtract in fp16 (because the inputs are fp16); the Metal kernel promotes to fp32 inside the inner loop. When the tiebreaker happens at the 5th decimal place, the two paths pick different winners.</p>
<p>But here's the thing: <strong>the reconstruction quality is identical</strong>. I validated this by reconstructing keys from both index sets and measuring MSE against the original input:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">B=1 H=8 S=2048 D=128 sub_dim=8 n_c=256 dtype=float16</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  idx_diff = 0.104%</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  mse_ref = 3.7211e-01    mse_metal = 3.7211e-01</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  rel_err = 5.61e-07</span><br></div></code></pre></div></div>
<p>Reconstruction MSE matches to <strong>7 decimal places</strong>. The two paths produce functionally identical compressed representations — they just disagree on which arbitrary tie-breaker to pick.</p>
<p>The parity tests in <code>veloxquant_mlx/tests/cache/test_vecinfer_metal_parity.py</code> validate this directly: assert that reconstruction MSE is within 1% relative error, not that indices match.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="what-i-got-wrong-on-the-first-try">What I Got Wrong on the First Try<a href="https://veloxquant-mlx.netlify.app/docs/blog/metal-kernels#what-i-got-wrong-on-the-first-try" class="hash-link" aria-label="Direct link to What I Got Wrong on the First Try" title="Direct link to What I Got Wrong on the First Try" translate="no">​</a></h2>
<p>I want to be honest about the missteps, because they're the actually interesting part.</p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="mistake-1-i-wrote-the-dequant-kernel-first">Mistake 1: I Wrote the Dequant Kernel First<a href="https://veloxquant-mlx.netlify.app/docs/blog/metal-kernels#mistake-1-i-wrote-the-dequant-kernel-first" class="hash-link" aria-label="Direct link to Mistake 1: I Wrote the Dequant Kernel First" title="Direct link to Mistake 1: I Wrote the Dequant Kernel First" translate="no">​</a></h3>
<p>My first instinct was to write a Metal kernel for <code>dequantize_vq</code> — the operation that takes codebook indices and reconstructs the float vectors. It's conceptually simpler (just a gather), so I started there.</p>
<p>After getting bit-exact correctness, I benchmarked it:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">shape                                pure-mlx     metal    speedup</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">B=1 H=8 S=128 n_sub=16 sub_dim=8       223.3 µs   185.6 µs   1.20x</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">B=1 H=8 S=512                          183.6 µs   209.3 µs   0.88x</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">B=1 H=8 S=2048                         258.3 µs   275.9 µs   0.94x</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">B=1 H=8 S=8192                         467.8 µs   577.6 µs   0.81x</span><br></div></code></pre></div></div>
<p><strong>My kernel was slower than MLX's <code>mx.take</code>.</strong> That stung. After staring at the numbers for an hour, the reason became obvious: MLX's <code>mx.take</code> is already a highly tuned Metal gather kernel under the hood. There is no "Python overhead" to eliminate. The pure-MLX path <em>is</em> a Metal kernel. My kernel was duplicating it badly.</p>
<p><strong>The lesson:</strong> before writing a custom kernel, profile to find the operation that has actual Python/intermediate-tensor overhead. <code>mx.take</code> does not. <code>quantize_vq</code> does, because of the <code>[N, n_centroids, sub_dim]</code> materialization. The 30-line MSL shader had to fuse an <em>algorithm</em> — argmin over distances — not just replace a builtin.</p>
<p>I kept the dequant kernel as a building block for Phase 2 (fused dequant+SDPA), but the headline result is the quantize kernel.</p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="mistake-2-wrong-threadgroup-layout">Mistake 2: Wrong Threadgroup Layout<a href="https://veloxquant-mlx.netlify.app/docs/blog/metal-kernels#mistake-2-wrong-threadgroup-layout" class="hash-link" aria-label="Direct link to Mistake 2: Wrong Threadgroup Layout" title="Direct link to Mistake 2: Wrong Threadgroup Layout" translate="no">​</a></h3>
<p>My first quantize kernel dispatched <strong>one thread per (input_vector, sub_dim_component)</strong> pair. That made each thread tiny — one subtract, one square, one accumulate — and meant launching <code>N × sub_dim</code> threads. For typical shapes, that's millions of threads.</p>
<p>Apple Silicon GPUs have 32-wide SIMD groups and an internal cost per thread launch. Launching 8× more threads than you need is pure overhead.</p>
<p>The fix was to dispatch <strong>one thread per input vector</strong> and let each thread loop over all sub_dim components in registers. Same total work, 8× fewer thread launches, much better register reuse. That's the layout in the kernel above.</p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="mistake-3-i-assumed-end-to-end-would-always-be-faster">Mistake 3: I Assumed End-to-End Would Always Be Faster<a href="https://veloxquant-mlx.netlify.app/docs/blog/metal-kernels#mistake-3-i-assumed-end-to-end-would-always-be-faster" class="hash-link" aria-label="Direct link to Mistake 3: I Assumed End-to-End Would Always Be Faster" title="Direct link to Mistake 3: I Assumed End-to-End Would Always Be Faster" translate="no">​</a></h3>
<p>After validating the kernel was 13× faster on synthetic shapes, I ran the full benchmark on SmolLM2-135M (a 135-million-parameter tiny model) expecting to see a speedup in end-to-end token generation.</p>
<p>I got the opposite. The Metal path was <strong>slower</strong> end-to-end — 75 tok/s vs 178 tok/s for the pure-MLX path.</p>
<p>The reason: Metal kernel dispatch has a fixed per-launch overhead of roughly 50–200 µs on Apple Silicon. SmolLM2 has 30 layers, each doing 2 quantize calls per token, so that's ~60 kernel launches per generated token. The per-launch overhead exceeded the work each kernel did.</p>
<p><strong>The kernel is designed for the regime where it matters: 7B+ models with realistic context lengths, where each <code>quantize_vq</code> call is doing milliseconds of work.</strong> On those, the launch overhead is negligible relative to the kernel runtime, and you get the full 10–14× speedup.</p>
<p>This is a limitation of MLX's kernel launch path — MLX doesn't yet expose a way to amortize launch overhead across multiple layers in a single dispatch. That's a Phase 3 problem and probably out of scope for a Python-level library.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="how-to-use-this-today">How to Use This Today<a href="https://veloxquant-mlx.netlify.app/docs/blog/metal-kernels#how-to-use-this-today" class="hash-link" aria-label="Direct link to How to Use This Today" title="Direct link to How to Use This Today" translate="no">​</a></h2>
<p>VeloxQuant-MLX 0.5.1 is on PyPI. Install:</p>
<div class="language-bash codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-bash codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">pip </span><span class="token function" style="color:rgb(80, 250, 123)">install</span><span class="token plain"> </span><span class="token parameter variable" style="color:rgb(189, 147, 249);font-style:italic">--upgrade</span><span class="token plain"> VeloxQuant-MLX</span><br></div></code></pre></div></div>
<p>The Metal kernels are <strong>on by default</strong> when available. No code changes needed. Your existing <code>VecInferKVCache</code> calls auto-detect Metal and use the fast path:</p>
<div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">import</span><span class="token plain"> mlx_lm</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">from</span><span class="token plain"> veloxquant_mlx </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">import</span><span class="token plain"> KVCacheConfig</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> KVCacheFactory</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">model</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> tokenizer </span><span class="token operator">=</span><span class="token plain"> mlx_lm</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">load</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token string" style="color:rgb(255, 121, 198)">"mlx-community/Falcon3-7B-Instruct-4bit"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token comment" style="color:rgb(98, 114, 164)"># Metal auto-detected. To force off for debugging: use_metal_kernels=False</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">config </span><span class="token operator">=</span><span class="token plain"> KVCacheConfig</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    method</span><span class="token operator">=</span><span class="token string" style="color:rgb(255, 121, 198)">"vecinfer"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    head_dim</span><span class="token operator">=</span><span class="token number">256</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    key_sub_dim</span><span class="token operator">=</span><span class="token number">4</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    value_sub_dim</span><span class="token operator">=</span><span class="token number">4</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    key_codebook_bits</span><span class="token operator">=</span><span class="token number">8</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    value_codebook_bits</span><span class="token operator">=</span><span class="token number">8</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    smooth_factors</span><span class="token operator">=</span><span class="token plain">calibrated_smooth_factors</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    key_codebook</span><span class="token operator">=</span><span class="token plain">calibrated_key_codebook</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    use_metal_kernels</span><span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain">  </span><span class="token comment" style="color:rgb(98, 114, 164)"># None = auto, True = require, False = forbid</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><br></div></code></pre></div></div>
<p>The new <code>use_metal_kernels</code> flag is three-state:</p>
<ul>
<li class=""><code>None</code> (default) — auto-detect; use Metal if available, silently fall back if not</li>
<li class=""><code>True</code> — require Metal; raise at construction time if unavailable</li>
<li class=""><code>False</code> — forbid Metal; use the pure-MLX path (for parity testing and debugging)</li>
</ul>
<p>To verify the speedup on your own machine:</p>
<div class="language-bash codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-bash codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token function" style="color:rgb(80, 250, 123)">git</span><span class="token plain"> clone https://github.com/rajveer43/VeloxQuant-MLX</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token builtin class-name" style="color:rgb(189, 147, 249)">cd</span><span class="token plain"> VeloxQuant-MLX</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token assign-left variable" style="color:rgb(189, 147, 249);font-style:italic">PYTHONPATH</span><span class="token operator">=</span><span class="token plain">. python scripts/plot_metal_benchmarks.py</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token comment" style="color:rgb(98, 114, 164)"># Produces figures/metal/summary.png with your hardware's numbers</span><br></div></code></pre></div></div>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="whats-next-phase-2">What's Next: Phase 2<a href="https://veloxquant-mlx.netlify.app/docs/blog/metal-kernels#whats-next-phase-2" class="hash-link" aria-label="Direct link to What's Next: Phase 2" title="Direct link to What's Next: Phase 2" translate="no">​</a></h2>
<p>The quantize kernel is the biggest single win, but it's not the end. <strong>Phase 2 is fusing dequantize + scaled-dot-product-attention</strong> into a single kernel.</p>
<p>Right now, even with Phase 1, the cache still materializes the full fp16 key tensor on every attention call. The dequant happens — efficiently, since <code>mx.take</code> is already fast — but we hold the result in GPU memory long enough to pass it to MLX's SDPA. For very long contexts, that materialized key tensor is still significant memory pressure.</p>
<p>The Phase 2 kernel would:</p>
<ol>
<li class="">Take codebook indices, the per-query LUT (<code>q_tilde @ codebook.T</code>), and value indices</li>
<li class="">Compute attention scores directly via LUT lookup, never materializing fp16 keys</li>
<li class="">Compute the softmax-weighted value sum in-kernel</li>
<li class="">Output the final attention result in one fused pass</li>
</ol>
<p>This is what the VecInfer paper's CUDA kernel does. Porting it to Metal is the goal. If you've written Metal compute shaders before and want to collaborate, the GitHub issue is open.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="the-meta-lesson-custom-kernels-are-more-accessible-than-you-think">The Meta-Lesson: Custom Kernels Are More Accessible Than You Think<a href="https://veloxquant-mlx.netlify.app/docs/blog/metal-kernels#the-meta-lesson-custom-kernels-are-more-accessible-than-you-think" class="hash-link" aria-label="Direct link to The Meta-Lesson: Custom Kernels Are More Accessible Than You Think" title="Direct link to The Meta-Lesson: Custom Kernels Are More Accessible Than You Think" translate="no">​</a></h2>
<p>I had never written a Metal shader before this project. The mental model is straightforward once you get past the syntax:</p>
<ol>
<li class=""><strong>Identify the operation with materialization overhead</strong> (not just a slow Python loop — those are usually wrapped in optimized C++ already; look for operations that create big intermediate tensors)</li>
<li class=""><strong>Write the algorithm with the intermediate as register-state instead of memory-state</strong> (running min, running sum, running argmin)</li>
<li class=""><strong>Dispatch one thread per output element</strong>, not per input or per output-component</li>
<li class=""><strong>Validate with reconstruction error</strong>, not bit-exact equality, when fp16 is involved</li>
<li class=""><strong>Benchmark at realistic shapes</strong>, not toy shapes — kernel launch overhead can dominate for small workloads</li>
</ol>
<p>Total time investment for this Phase 1: about 6 hours of focused work, including the two failed approaches above. The resulting kernel unblocks <code>head_dim=256</code> models that previously OOMed, gives a 10–14× speedup on the hot path, and is 30 lines of MSL.</p>
<p>If you've been hesitant to write custom GPU kernels because it sounds intimidating — <code>mx.fast.metal_kernel</code> makes the bar way lower than it used to be on CUDA. Try it.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="tldr">TL;DR<a href="https://veloxquant-mlx.netlify.app/docs/blog/metal-kernels#tldr" class="hash-link" aria-label="Direct link to TL;DR" title="Direct link to TL;DR" translate="no">​</a></h2>
<ul>
<li class="">VeloxQuant-MLX 0.5.1 adds a Metal compute kernel for <code>quantize_vq</code>, the hot path in VecInfer KV-cache compression</li>
<li class=""><strong>13× faster</strong> on realistic shapes (S=2048+)</li>
<li class=""><strong>98% less peak memory</strong> at the Falcon3-7B OOM trigger configuration</li>
<li class=""><strong>Drop-in, zero API change</strong> — auto-detected when Metal is available</li>
<li class="">Free, MIT-licensed, on PyPI: <code>pip install VeloxQuant-MLX</code></li>
<li class="">The kernel is 30 lines of Metal Shading Language inside Python</li>
<li class="">Phase 2 (fused dequant+SDPA attention kernel) is next</li>
</ul>
<p>GitHub: <a href="https://github.com/rajveer43/VeloxQuant-MLX" target="_blank" rel="noopener noreferrer" class="">github.com/rajveer43/VeloxQuant-MLX</a>
PyPI: <a href="https://pypi.org/project/VeloxQuant-MLX" target="_blank" rel="noopener noreferrer" class="">pypi.org/project/VeloxQuant-MLX</a>
Benchmark figures: <a href="https://github.com/rajveer43/VeloxQuant-MLX/blob/master/figures/metal/summary.png" target="_blank" rel="noopener noreferrer" class=""><code>figures/metal/summary.png</code></a> in the repo</p>
<p>If this saves your Mac from OOMing tonight, leave a star — or open an issue if it doesn't.</p>]]></content>
        <author>
            <name>Rajveer Rathod</name>
            <uri>https://github.com/rajveer43</uri>
        </author>
        <category label="metal" term="metal"/>
        <category label="apple-silicon" term="apple-silicon"/>
        <category label="mlx" term="mlx"/>
        <category label="gpu" term="gpu"/>
        <category label="performance" term="performance"/>
    </entry>
    <entry>
        <title type="html"><![CDATA[TurboQuant + Metal Kernels: The Combined Writeup]]></title>
        <id>https://veloxquant-mlx.netlify.app/docs/blog/turboquant-metal-kernels</id>
        <link href="https://veloxquant-mlx.netlify.app/docs/blog/turboquant-metal-kernels"/>
        <updated>2026-05-20T00:00:00.000Z</updated>
        <summary type="html"><![CDATA[How I wrote five hand-tuned Metal compute kernels in MLX for TurboQuant — and what every bug taught me about Apple GPU programming.]]></summary>
        <content type="html"><![CDATA[<p><em>How I wrote five hand-tuned Metal compute kernels in MLX for TurboQuant — and what every bug taught me about Apple GPU programming.</em></p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="the-problem">The Problem<a href="https://veloxquant-mlx.netlify.app/docs/blog/turboquant-metal-kernels#the-problem" class="hash-link" aria-label="Direct link to The Problem" title="Direct link to The Problem" translate="no">​</a></h2>
<p>My Mac was choking on long-context LLM inference.</p>
<p>Not because the model was too large — I had already quantized the weights. The bottleneck was the <strong>KV cache</strong>. At 8k context, a single layer's key cache is <code>[1, 32, 8192, 128]</code> in fp16 — over 67 MB per layer, 2 GB across 32 layers. On Apple Silicon, where the GPU and CPU share the same physical memory, that pressure is immediate and painful.</p>
<p>VeloxQuant-MLX already had several compression algorithms: TurboQuantRVQ (7.5× via two-stage scalar RVQ), QJL (16× via 1-bit Johnson-Lindenstrauss sketching), and VecInfer (16× via product VQ). But they were all running through pure MLX graph operations — no custom GPU kernels. The hot paths were either slow or allocating huge intermediate tensors.</p>
<p>The fix: write the hot paths in <strong>Metal Shading Language</strong> and JIT-compile them via <code>mx.fast.metal_kernel</code>.</p>
<p>This is the story of how I did it, what broke, and what I learned.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="the-stack">The Stack<a href="https://veloxquant-mlx.netlify.app/docs/blog/turboquant-metal-kernels#the-stack" class="hash-link" aria-label="Direct link to The Stack" title="Direct link to The Stack" translate="no">​</a></h2>
<p>Before diving into the kernels, here's the relevant context:</p>
<ul>
<li class=""><strong>MLX</strong> — Apple's NumPy-style ML framework with lazy evaluation and Metal GPU backend</li>
<li class=""><strong><code>mx.fast.metal_kernel</code></strong> — Python API to write raw Metal Shading Language compute shaders that plug into MLX's lazy graph</li>
<li class=""><strong>TurboQuant</strong> — a family of KV cache quantization algorithms (MSE, Prod, RVQ) implemented in VeloxQuant-MLX</li>
<li class=""><strong>QJL</strong> — Quantized Johnson-Lindenstrauss: compress keys to 1-bit sign sketches + a scalar norm</li>
</ul>
<p>The goal was to replace the slowest pure-MLX operations with Metal kernels that live in five focused submodules:</p>
<table><thead><tr><th>Submodule</th><th>What it does</th></tr></thead><tbody><tr><td><code>_bit_packing.py</code></td><td>Pack/unpack b-bit indices into uint8 bytes</td></tr><tr><td><code>_scalar_quant.py</code></td><td>Nearest-centroid quantize, dequantize, fused Hadamard+quant</td></tr><tr><td><code>_qjl.py</code></td><td>QJL sign encode and inner product scoring</td></tr><tr><td><code>_rvq_attend.py</code></td><td>Fused RVQ key decode + FlashAttention-style online softmax</td></tr></tbody></table>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="how-mxfastmetal_kernel-works">How <code>mx.fast.metal_kernel</code> Works<a href="https://veloxquant-mlx.netlify.app/docs/blog/turboquant-metal-kernels#how-mxfastmetal_kernel-works" class="hash-link" aria-label="Direct link to how-mxfastmetal_kernel-works" title="Direct link to how-mxfastmetal_kernel-works" translate="no">​</a></h2>
<p>Before showing any kernel code, there's one thing you need to understand about the API — because getting it wrong produces silent, subtle bugs.</p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="the-api-in-30-seconds">The API in 30 seconds<a href="https://veloxquant-mlx.netlify.app/docs/blog/turboquant-metal-kernels#the-api-in-30-seconds" class="hash-link" aria-label="Direct link to The API in 30 seconds" title="Direct link to The API in 30 seconds" translate="no">​</a></h3>
<div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">import</span><span class="token plain"> mlx</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">core </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">as</span><span class="token plain"> mx</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">kernel </span><span class="token operator">=</span><span class="token plain"> mx</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">fast</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">metal_kernel</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    name</span><span class="token operator">=</span><span class="token string" style="color:rgb(255, 121, 198)">"my_kernel"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    input_names</span><span class="token operator">=</span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token string" style="color:rgb(255, 121, 198)">"x"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token string" style="color:rgb(255, 121, 198)">"y"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    output_names</span><span class="token operator">=</span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token string" style="color:rgb(255, 121, 198)">"out"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    source</span><span class="token operator">=</span><span class="token triple-quoted-string string" style="color:rgb(255, 121, 198)">"""</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token triple-quoted-string string" style="color:rgb(255, 121, 198)">        uint i = thread_position_in_grid.x;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token triple-quoted-string string" style="color:rgb(255, 121, 198)">        out[i] = x[i] + y[i];</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token triple-quoted-string string" style="color:rgb(255, 121, 198)">    """</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">result </span><span class="token operator">=</span><span class="token plain"> kernel</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    inputs</span><span class="token operator">=</span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token plain">a</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> b</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    grid</span><span class="token operator">=</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">N</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    threadgroup</span><span class="token operator">=</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token number">256</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    output_shapes</span><span class="token operator">=</span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">N</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    output_dtypes</span><span class="token operator">=</span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token plain">mx</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">float32</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><br></div></code></pre></div></div>
<p>The <code>source</code> string is raw Metal Shading Language — no <code>kernel</code> keyword, no function signature. MLX wraps it. Shape information is injected automatically: inside the kernel, <code>x_shape[0]</code> gives you the first dimension of <code>x</code>.</p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="the-1-gotcha-grid--total-threads">The #1 Gotcha: Grid = Total Threads<a href="https://veloxquant-mlx.netlify.app/docs/blog/turboquant-metal-kernels#the-1-gotcha-grid--total-threads" class="hash-link" aria-label="Direct link to The #1 Gotcha: Grid = Total Threads" title="Direct link to The #1 Gotcha: Grid = Total Threads" translate="no">​</a></h3>
<p>This is the single most important thing to get right, and the MLX documentation is easy to misread on this point.</p>
<p>In standard Metal (Obj-C / Swift), you call <code>dispatchThreadgroups(n_groups, threadsPerThreadgroup: tg_size)</code> — so the grid is in <em>threadgroup</em> units.</p>
<p><strong>MLX uses <code>dispatchThreads</code> — the grid is in <em>total thread</em> units.</strong></p>
<p>That means if you want B threadgroups of T threads each:</p>
<div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token comment" style="color:rgb(98, 114, 164)"># WRONG — only dispatches 1 thread per threadgroup</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">grid</span><span class="token operator">=</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">B</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> threadgroup</span><span class="token operator">=</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">T</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token comment" style="color:rgb(98, 114, 164)"># CORRECT — dispatches B threadgroups of T threads each</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">grid</span><span class="token operator">=</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">B </span><span class="token operator">*</span><span class="token plain"> T</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> threadgroup</span><span class="token operator">=</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">T</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><br></div></code></pre></div></div>
<p>I made this mistake on four out of five kernels. The symptom was identical every time: <strong>only the first batch element had correct output; everything else was zero</strong>. It looked like a memory layout bug or an indexing error, not a dispatch error. I spent hours debugging before I found it.</p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="the-lazy-graph-contract">The Lazy Graph Contract<a href="https://veloxquant-mlx.netlify.app/docs/blog/turboquant-metal-kernels#the-lazy-graph-contract" class="hash-link" aria-label="Direct link to The Lazy Graph Contract" title="Direct link to The Lazy Graph Contract" translate="no">​</a></h3>
<p><code>mx.fast.metal_kernel</code> returns a lazy node — nothing runs until <code>mx.eval()</code> is called. <code>mx.eval()</code> internally:</p>
<ol>
<li class="">Encodes the compute command into a <code>MTLCommandBuffer</code></li>
<li class="">Calls <code>commandBuffer.commit()</code> to submit to the GPU</li>
<li class="">Calls <code>commandBuffer.waitUntilCompleted()</code> to synchronize</li>
</ol>
<p>You never write any of this yourself. MLX owns the entire Metal command buffer lifecycle.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="kernel-1-bit-packing--30-over-numpy">Kernel 1: Bit-Packing — 30× Over NumPy<a href="https://veloxquant-mlx.netlify.app/docs/blog/turboquant-metal-kernels#kernel-1-bit-packing--30-over-numpy" class="hash-link" aria-label="Direct link to Kernel 1: Bit-Packing — 30× Over NumPy" title="Direct link to Kernel 1: Bit-Packing — 30× Over NumPy" translate="no">​</a></h2>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="the-problem-1">The problem<a href="https://veloxquant-mlx.netlify.app/docs/blog/turboquant-metal-kernels#the-problem-1" class="hash-link" aria-label="Direct link to The problem" title="Direct link to The problem" translate="no">​</a></h3>
<p>TurboQuantRVQ stores KV cache keys as b-bit indices (b ∈ {1, 2, 4}). The pure-Python path used a loop to pack these into uint8 bytes. At 65k elements it was ~8 ms — unacceptable.</p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="the-kernel">The kernel<a href="https://veloxquant-mlx.netlify.app/docs/blog/turboquant-metal-kernels#the-kernel" class="hash-link" aria-label="Direct link to The kernel" title="Direct link to The kernel" translate="no">​</a></h3>
<div class="language-metal codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-metal codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">constexpr int  ELEMS_PER_BYTE = 8 / B_BITS;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">constexpr uint MASK           = (1u &lt;&lt; B_BITS) - 1u;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">uint byte_idx = thread_position_in_grid.x;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">uint base     = byte_idx * ELEMS_PER_BYTE;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">uint packed_byte = 0u;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">for (int i = 0; i &lt; ELEMS_PER_BYTE; ++i) {</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    uint val = uint(indices[base + i]) &amp; MASK;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    packed_byte |= (val &lt;&lt; (i * B_BITS));</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">}</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">packed[byte_idx] = uint8_t(packed_byte);</span><br></div></code></pre></div></div>
<p>One thread per output byte. <code>B_BITS</code> is a <strong>template parameter</strong> — a compile-time integer constant. This lets the compiler statically unroll the inner loop (2 iterations for b=4, 4 for b=2, 8 for b=1) and inline the constants.</p>
<p>The dispatch:</p>
<div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">grid </span><span class="token operator">=</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">n_bytes</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">threadgroup </span><span class="token operator">=</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token builtin" style="color:rgb(189, 147, 249)">min</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token number">256</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> n_bytes</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><br></div></code></pre></div></div>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="results">Results<a href="https://veloxquant-mlx.netlify.app/docs/blog/turboquant-metal-kernels#results" class="hash-link" aria-label="Direct link to Results" title="Direct link to Results" translate="no">​</a></h3>
<table><thead><tr><th>N</th><th>NumPy</th><th>Metal</th><th>Speedup</th></tr></thead><tbody><tr><td>4,096</td><td>0.52 ms</td><td>0.18 ms</td><td>2.9×</td></tr><tr><td>16,384</td><td>2.1 ms</td><td>0.17 ms</td><td>12.5×</td></tr><tr><td>65,536</td><td>8.4 ms</td><td>0.28 ms</td><td><strong>29.5×</strong></td></tr></tbody></table>
<p>The kernel dispatch overhead is ~0.17 ms regardless of N. Below ~2k elements NumPy wins because there's nothing to hide the launch cost behind. Above 16k elements, Metal dominates by an order of magnitude.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="kernel-2-scalar-quantize--dequantize--11-over-numpy">Kernel 2: Scalar Quantize / Dequantize — 11× Over NumPy<a href="https://veloxquant-mlx.netlify.app/docs/blog/turboquant-metal-kernels#kernel-2-scalar-quantize--dequantize--11-over-numpy" class="hash-link" aria-label="Direct link to Kernel 2: Scalar Quantize / Dequantize — 11× Over NumPy" title="Direct link to Kernel 2: Scalar Quantize / Dequantize — 11× Over NumPy" translate="no">​</a></h2>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="the-problem-2">The problem<a href="https://veloxquant-mlx.netlify.app/docs/blog/turboquant-metal-kernels#the-problem-2" class="hash-link" aria-label="Direct link to The problem" title="Direct link to The problem" translate="no">​</a></h3>
<p>TurboQuantMSE quantizes each key dimension independently against a Lloyd-Max codebook. The pure-MLX path computed <code>|x - centroids|²</code> as a full <code>[N, 2^b]</code> matrix, then took <code>argmin</code> — allocating a tensor that was <code>2^b</code> times the input size.</p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="the-quantize-kernel">The quantize kernel<a href="https://veloxquant-mlx.netlify.app/docs/blog/turboquant-metal-kernels#the-quantize-kernel" class="hash-link" aria-label="Direct link to The quantize kernel" title="Direct link to The quantize kernel" translate="no">​</a></h3>
<div class="language-metal codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-metal codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">constexpr int N_CENTS = 1 &lt;&lt; B_BITS;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">uint  elem      = thread_position_in_grid.x;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">float val       = float(x[elem]);</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">int   best      = 0;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">float best_dist = INFINITY;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">for (int j = 0; j &lt; N_CENTS; ++j) {</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    float d    = val - centroids[j];</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    float dist = d * d;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    if (dist &lt; best_dist) { best_dist = dist; best = j; }</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">}</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">indices[elem] = uint8_t(best);</span><br></div></code></pre></div></div>
<p>One thread per element. The centroid scan lives entirely in registers — no intermediate allocation. With <code>B_BITS</code> as a template, the loop body is known at compile time: the compiler generates 2, 4, 8, or 16 iterations of straight-line code.</p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="the-dequantize-kernel">The dequantize kernel<a href="https://veloxquant-mlx.netlify.app/docs/blog/turboquant-metal-kernels#the-dequantize-kernel" class="hash-link" aria-label="Direct link to The dequantize kernel" title="Direct link to The dequantize kernel" translate="no">​</a></h3>
<p>Even simpler — a pure gather:</p>
<div class="language-metal codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-metal codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">uint elem   = thread_position_in_grid.x;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">x_hat[elem] = half(centroids[uint(indices[elem])]);</span><br></div></code></pre></div></div>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="results-1">Results<a href="https://veloxquant-mlx.netlify.app/docs/blog/turboquant-metal-kernels#results-1" class="hash-link" aria-label="Direct link to Results" title="Direct link to Results" translate="no">​</a></h3>
<table><thead><tr><th>N</th><th>NumPy argmin</th><th>Metal</th><th>Speedup</th></tr></thead><tbody><tr><td>16,384</td><td>0.21 ms</td><td>0.17 ms</td><td>1.2×</td></tr><tr><td>65,536</td><td>0.86 ms</td><td>0.19 ms</td><td>4.5×</td></tr><tr><td>262,144</td><td>3.5 ms</td><td>0.31 ms</td><td><strong>11.3×</strong></td></tr></tbody></table>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="kernel-3-fused-hadamard--quantize--the-hardest-one">Kernel 3: Fused Hadamard + Quantize — The Hardest One<a href="https://veloxquant-mlx.netlify.app/docs/blog/turboquant-metal-kernels#kernel-3-fused-hadamard--quantize--the-hardest-one" class="hash-link" aria-label="Direct link to Kernel 3: Fused Hadamard + Quantize — The Hardest One" title="Direct link to Kernel 3: Fused Hadamard + Quantize — The Hardest One" translate="no">​</a></h2>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="the-problem-3">The problem<a href="https://veloxquant-mlx.netlify.app/docs/blog/turboquant-metal-kernels#the-problem-3" class="hash-link" aria-label="Direct link to The problem" title="Direct link to The problem" translate="no">​</a></h3>
<p>TurboQuantMSE (with Hadamard preconditioner) runs:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">y = diag * H * x / sqrt(D)    [randomized Hadamard rotation]</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">idx = argmin_k |y - c_k|²     [nearest-centroid quantize]</span><br></div></code></pre></div></div>
<p>Two separate dispatches, with a <code>[B, D]</code> fp16 intermediate between them. Fusing them into one kernel eliminates that allocation and the round-trip to GPU memory.</p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="the-kernel-design">The kernel design<a href="https://veloxquant-mlx.netlify.app/docs/blog/turboquant-metal-kernels#the-kernel-design" class="hash-link" aria-label="Direct link to The kernel design" title="Direct link to The kernel design" translate="no">​</a></h3>
<p>Walsh-Hadamard Transform (WHT) is an in-place butterfly — each pass halves the stride. On GPU, D threads share a threadgroup, and each butterfly step needs a barrier.</p>
<div class="language-metal codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-metal codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">threadgroup float buf[MAX_D];   // static threadgroup memory; MAX_D injected at compile time</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">// 1. Load + diagonal sign flip</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">float v = float(x[tg * D + lane]);</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">v *= float(diag[lane]);</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">buf[lane] = v;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">threadgroup_barrier(mem_flags::mem_threadgroup);</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">// 2. In-place WHT — range-based parallel butterfly</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">for (uint stride = 1; stride &lt; D; stride &lt;&lt;= 1) {</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    uint local    = lane % (stride &lt;&lt; 1u);</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    bool is_upper = local &gt;= stride;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    uint partner  = is_upper ? (lane - stride) : (lane + stride);</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    float a = buf[lane];</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    float b = buf[partner];</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    threadgroup_barrier(mem_flags::mem_threadgroup);</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    buf[lane] = is_upper ? (b - a) : (a + b);</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    threadgroup_barrier(mem_flags::mem_threadgroup);</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">}</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">// 3. Scale</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">float y = buf[lane] * metal::rsqrt(float(D));</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">// 4. Nearest-centroid argmin (register-local)</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">int   best      = 0;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">float best_dist = INFINITY;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">for (int j = 0; j &lt; N_CENTS; ++j) {</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    float d    = y - centroids[j];</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    float dist = d * d;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    if (dist &lt; best_dist) { best_dist = dist; best = j; }</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">}</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">indices[tg * D + lane] = uint8_t(best);</span><br></div></code></pre></div></div>
<p>The threadgroup array <code>buf[MAX_D]</code> requires <code>MAX_D</code> to be a compile-time constant — which is why it's injected as a <code>#define</code> in the kernel header:</p>
<div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">_hadamard_quantize_kernel </span><span class="token operator">=</span><span class="token plain"> mx</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">fast</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">metal_kernel</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    header</span><span class="token operator">=</span><span class="token string-interpolation string" style="color:rgb(255, 121, 198)">f"#define MAX_D </span><span class="token string-interpolation interpolation punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token string-interpolation interpolation">D</span><span class="token string-interpolation interpolation punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token string-interpolation string" style="color:rgb(255, 121, 198)">\n"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    source</span><span class="token operator">=</span><span class="token plain">_HADAMARD_QUANTIZE_SRC</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><br></div></code></pre></div></div>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="the-butterfly-bug">The butterfly bug<a href="https://veloxquant-mlx.netlify.app/docs/blog/turboquant-metal-kernels#the-butterfly-bug" class="hash-link" aria-label="Direct link to The butterfly bug" title="Direct link to The butterfly bug" translate="no">​</a></h3>
<p>My first implementation used:</p>
<div class="language-metal codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-metal codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">uint partner = lane ^ stride;    // XOR butterfly</span><br></div></code></pre></div></div>
<p>This looked right — it's the standard Cooley-Tukey bit-reversal trick. But on GPU, it produced ~90% index mismatch vs the sequential reference.</p>
<p>The problem: <code>lane ^ stride</code> traverses the WHT in <strong>bit-reversal order</strong>, which is fine for sequential execution (because you can reorder the output at the end), but on GPU where lanes run simultaneously, XOR pairing creates <strong>data races</strong> within a butterfly pass — some lanes read values that other lanes in the same pass are simultaneously writing.</p>
<p>The fix is a <strong>range-based butterfly</strong> that unambiguously partitions each pass into non-overlapping upper/lower pairs:</p>
<div class="language-metal codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-metal codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">uint local    = lane % (stride &lt;&lt; 1u);</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">bool is_upper = local &gt;= stride;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">uint partner  = is_upper ? (lane - stride) : (lane + stride);</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">float a = buf[lane];</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">float b = buf[partner];          // read BEFORE the barrier write below</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">threadgroup_barrier(mem_flags::mem_threadgroup);</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">buf[lane] = is_upper ? (b - a) : (a + b);</span><br></div></code></pre></div></div>
<p>Reading <code>a</code> and <code>b</code> before the barrier guarantees both values come from the previous pass. After this fix, 100% of indices matched the reference.</p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="grid">Grid<a href="https://veloxquant-mlx.netlify.app/docs/blog/turboquant-metal-kernels#grid" class="hash-link" aria-label="Direct link to Grid" title="Direct link to Grid" translate="no">​</a></h3>
<p>The grid uses B threadgroups of D threads — <strong>not</strong> B × D total:</p>
<div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token comment" style="color:rgb(98, 114, 164)"># Wrong:</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">grid</span><span class="token operator">=</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">B</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> threadgroup</span><span class="token operator">=</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">D</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain">   </span><span class="token comment" style="color:rgb(98, 114, 164)"># only 1 thread per threadgroup!</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token comment" style="color:rgb(98, 114, 164)"># Correct:</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">grid</span><span class="token operator">=</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">B </span><span class="token operator">*</span><span class="token plain"> D</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> threadgroup</span><span class="token operator">=</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">D</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain">   </span><span class="token comment" style="color:rgb(98, 114, 164)"># B threadgroups of D threads</span><br></div></code></pre></div></div>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="kernel-4-qjl-encode--simdgroup-sign-packing">Kernel 4: QJL Encode — Simdgroup Sign Packing<a href="https://veloxquant-mlx.netlify.app/docs/blog/turboquant-metal-kernels#kernel-4-qjl-encode--simdgroup-sign-packing" class="hash-link" aria-label="Direct link to Kernel 4: QJL Encode — Simdgroup Sign Packing" title="Direct link to Kernel 4: QJL Encode — Simdgroup Sign Packing" translate="no">​</a></h2>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="the-problem-4">The problem<a href="https://veloxquant-mlx.netlify.app/docs/blog/turboquant-metal-kernels#the-problem-4" class="hash-link" aria-label="Direct link to The problem" title="Direct link to The problem" translate="no">​</a></h3>
<p>QJL encoding requires:</p>
<ol>
<li class="">For each key vector <code>x[b]</code>, compute <code>sign(S @ x[b])</code> for all m sketch dimensions — giving m bits</li>
<li class="">Pack those m bits into m/8 uint8 bytes (LSB-first)</li>
<li class="">Compute <code>‖x[b]‖</code> (one scalar per key)</li>
</ol>
<p>The pure-MLX path materialized the full <code>[B, m]</code> float matrix <code>S @ x.T</code> before sign-taking — <code>m * d * B * 4</code> bytes, growing linearly with batch and sketch size.</p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="simdgroup-design">Simdgroup design<a href="https://veloxquant-mlx.netlify.app/docs/blog/turboquant-metal-kernels#simdgroup-design" class="hash-link" aria-label="Direct link to Simdgroup design" title="Direct link to Simdgroup design" translate="no">​</a></h3>
<p>Each simdgroup (32 lanes) handles 32 consecutive sketch dimensions. Lane <code>j</code> computes <code>dot(S[simd_blk*32 + j, :], x[b, :])</code> via a scalar loop:</p>
<div class="language-metal codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-metal codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">uint b_idx    = flat_tg / n_simd_per_batch;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">uint simd_blk = flat_tg % n_simd_per_batch;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">uint sketch_j = simd_blk * 32u + lane;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">float dot_val = 0.0f;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">if (sketch_j &lt; m) {</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    uint S_row = sketch_j * d;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    uint x_row = b_idx   * d;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    for (uint i = 0; i &lt; d; ++i) {</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        dot_val += float(S[S_row + i]) * float(x[x_row + i]);</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    }</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">}</span><br></div></code></pre></div></div>
<p>After the dot product, all 32 lanes <strong>cooperate to pack 32 sign bits into 4 bytes</strong> using <code>simd_shuffle</code>:</p>
<div class="language-metal codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-metal codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">uint sign_bit    = (dot_val &gt;= 0.0f) ? 1u : 0u;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">uint byte_in_blk = lane / 8u;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">uint bit_in_byte = lane % 8u;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">uint packed_byte = 0u;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">for (uint bit = 0; bit &lt; 8u; ++bit) {</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    uint src = byte_in_blk * 8u + bit;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    packed_byte |= (simd_shuffle(sign_bit, src) &lt;&lt; bit);</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">}</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">if (bit_in_byte == 0 &amp;&amp; sketch_j &lt; m) {</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    packed_signs[out_byte] = uint8_t(packed_byte);</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">}</span><br></div></code></pre></div></div>
<p><code>simd_shuffle(val, lane_id)</code> broadcasts <code>sign_bit</code> from lane <code>src</code> to the current lane — no shared memory needed. Lane 0 (of each byte group) does the final write.</p>
<p>The norm is computed cooperatively by simd_blk 0:</p>
<div class="language-metal codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-metal codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">if (simd_blk == 0) {</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    float x_sq = 0.0f;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    for (uint i = lane; i &lt; d; i += 32u) {</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        float v = float(x[x_row + i]);</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        x_sq += v * v;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    }</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    float norm_sq = simd_sum(x_sq);</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    if (lane == 0) norms[b_idx] = half(metal::sqrt(norm_sq));</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">}</span><br></div></code></pre></div></div>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="grid-the-bug-again">Grid (the bug, again)<a href="https://veloxquant-mlx.netlify.app/docs/blog/turboquant-metal-kernels#grid-the-bug-again" class="hash-link" aria-label="Direct link to Grid (the bug, again)" title="Direct link to Grid (the bug, again)" translate="no">​</a></h3>
<div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">n_simd_per_batch </span><span class="token operator">=</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">m </span><span class="token operator">+</span><span class="token plain"> </span><span class="token number">31</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token operator">//</span><span class="token plain"> </span><span class="token number">32</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">n_total_threads  </span><span class="token operator">=</span><span class="token plain"> B </span><span class="token operator">*</span><span class="token plain"> n_simd_per_batch </span><span class="token operator">*</span><span class="token plain"> </span><span class="token number">32</span><span class="token plain">   </span><span class="token comment" style="color:rgb(98, 114, 164)"># ← must multiply by 32</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">grid</span><span class="token operator">=</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">n_total_threads</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> threadgroup</span><span class="token operator">=</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token number">32</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><br></div></code></pre></div></div>
<p>Without the <code>* 32</code>, only <code>B * n_simd_per_batch</code> total threads dispatched — meaning only the first simdgroup ran, and only the first key had any output.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="kernel-5-fused-rvq-decode--attend--online-softmax-without-materializing-k">Kernel 5: Fused RVQ Decode + Attend — Online Softmax Without Materializing K<a href="https://veloxquant-mlx.netlify.app/docs/blog/turboquant-metal-kernels#kernel-5-fused-rvq-decode--attend--online-softmax-without-materializing-k" class="hash-link" aria-label="Direct link to Kernel 5: Fused RVQ Decode + Attend — Online Softmax Without Materializing K" title="Direct link to Kernel 5: Fused RVQ Decode + Attend — Online Softmax Without Materializing K" translate="no">​</a></h2>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="the-problem-5">The problem<a href="https://veloxquant-mlx.netlify.app/docs/blog/turboquant-metal-kernels#the-problem-5" class="hash-link" aria-label="Direct link to The problem" title="Direct link to The problem" translate="no">​</a></h3>
<p>Attention with a quantized KV cache normally requires two dispatches:</p>
<ol>
<li class="">Decode all compressed keys → <code>K_hat</code> tensor <code>[B, H, S_kv, D]</code> (fp16, potentially GBs)</li>
<li class="">Run <code>softmax(q @ K_hat.T / sqrt(D)) @ V</code></li>
</ol>
<p>The <code>K_hat</code> tensor is allocated, filled, used once, and thrown away. For RVQ keys this is unavoidable in the two-dispatch design — but we can fuse everything into a single FlashAttention-style pass that decodes keys <strong>on the fly</strong> without ever materializing <code>K_hat</code>.</p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="design">Design<a href="https://veloxquant-mlx.netlify.app/docs/blog/turboquant-metal-kernels#design" class="hash-link" aria-label="Direct link to Design" title="Direct link to Design" translate="no">​</a></h3>
<p>Each threadgroup handles one query position <code>(b, h, sq)</code>. Lanes stripe across the D-dimensional vectors in steps of TG = min(D, 32):</p>
<div class="language-metal codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-metal codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">float running_m = -INFINITY;   // online softmax running max</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">float running_d = 0.0f;        // online softmax running denominator</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">float my_out[8];               // per-lane output accumulator</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">for (int i = 0; i &lt; 8; ++i) my_out[i] = 0.0f;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">for (uint sk = 0; sk &lt; S_kv; ++sk) {</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    // 1. Decode key on-the-fly: k[i] = cents1[idx1[i]] + cents2[idx2[i]]</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    float partial_dot = 0.0f;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    for (uint i = tg_lane; i &lt; D; i += TG) {</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        float ki = centroids1[uint(k_indices1[k_off])]</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">                 + centroids2[uint(k_indices2[k_off])];</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        partial_dot += float(q[q_base + i]) * ki;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    }</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    float score = simd_sum(partial_dot) * inv_sqrt_d;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    // 2. Online softmax update (Dao et al. FlashAttention)</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    float m_new  = metal::max(running_m, score);</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    float factor = metal::exp(running_m - m_new);</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    float w      = metal::exp(score     - m_new);</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    running_d    = running_d * factor + w;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    running_m    = m_new;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    // 3. Rescale and accumulate value</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    for (uint i = 0; i &lt; n_owned; ++i) my_out[i] *= factor;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    for (uint i = tg_lane; i &lt; D; i += TG) {</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        float vi    = float(v_codebook[cb_off]);</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        uint  out_i = (i - tg_lane) / TG;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        my_out[out_i] += w * vi;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    }</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">}</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">// 4. Normalize and write</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">for (uint i = tg_lane; i &lt; D; i += TG) {</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    uint out_i   = (i - tg_lane) / TG;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    out[out_off] = half(my_out[out_i] / running_d);</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">}</span><br></div></code></pre></div></div>
<p><code>simd_sum(partial_dot)</code> broadcasts the full dot product to all lanes in the simdgroup — this is the SIMD-level reduction that gives the correct score without any threadgroup memory.</p>
<p>The local accumulator index <code>out_i = (i - tg_lane) / TG</code> is the critical piece: lane 0 owns dims {0, TG, 2×TG, ...}, lane 1 owns {1, TG+1, ...}, and <code>out_i</code> is the position within that lane's private array.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="the-benchmarks">The Benchmarks<a href="https://veloxquant-mlx.netlify.app/docs/blog/turboquant-metal-kernels#the-benchmarks" class="hash-link" aria-label="Direct link to The Benchmarks" title="Direct link to The Benchmarks" translate="no">​</a></h2>
<p>After fixing all the dispatch bugs, here are the results on Apple M-series (figures saved to <code>figures/metal/turboquant_kernels/</code>):</p>
<table><thead><tr><th>Kernel</th><th>Peak speedup vs NumPy</th><th>Notes</th></tr></thead><tbody><tr><td><code>turboquant_bit_pack</code> (b=4, N=65k)</td><td><strong>29.5×</strong></td><td>NumPy loop vs Metal one-thread-per-byte</td></tr><tr><td><code>turboquant_scalar_quantize</code> (N=256k)</td><td><strong>11.3×</strong></td><td>Eliminates <code>[N, 2^b]</code> diff tensor</td></tr><tr><td><code>turboquant_hadamard_quantize</code> (D=1024)</td><td>1.1×</td><td>Fused saves 1 allocation; WHT itself is fast</td></tr><tr><td><code>qjl_encode</code> (B=256)</td><td>0.2× (small B); ~1× (large B)</td><td><code>np.packbits</code> is BLAS-level; Metal overhead dominates at <code>B&lt;64</code></td></tr><tr><td><code>turboquant_fused_rvq_decode_attend</code></td><td>—</td><td>No NumPy baseline (different algorithm)</td></tr></tbody></table>
<p><strong>Memory savings</strong> are the bigger story for the RVQ attend kernel — it eliminates the <code>[B, H, S_kv, D]</code> fp16 <code>K_hat</code> tensor entirely. At <code>S_kv=4096, H=32, D=128</code> that's 33 MB per layer, ~1 GB across a 32-layer model, allocated and freed every forward pass.</p>
<p><strong>1-bit bit-packing alone gives 16× memory compression</strong> on the key cache (1 bit per dimension vs fp16). Combined with the Metal kernel's 30× throughput advantage, the packing/unpacking step goes from a bottleneck to essentially free.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="what-i-learned">What I Learned<a href="https://veloxquant-mlx.netlify.app/docs/blog/turboquant-metal-kernels#what-i-learned" class="hash-link" aria-label="Direct link to What I Learned" title="Direct link to What I Learned" translate="no">​</a></h2>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="1-grid--total-threads-is-the-most-common-mlx-metal-mistake">1. Grid = total threads is the most common MLX Metal mistake<a href="https://veloxquant-mlx.netlify.app/docs/blog/turboquant-metal-kernels#1-grid--total-threads-is-the-most-common-mlx-metal-mistake" class="hash-link" aria-label="Direct link to 1. Grid = total threads is the most common MLX Metal mistake" title="Direct link to 1. Grid = total threads is the most common MLX Metal mistake" translate="no">​</a></h3>
<p>Every tutorial and reference for Metal uses <code>dispatchThreadgroups</code>. MLX uses <code>dispatchThreads</code>. These are different. If your output is correct for the first batch element and zero elsewhere, check your grid first.</p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="2-xor-butterflies-are-wrong-for-parallel-wht">2. XOR butterflies are wrong for parallel WHT<a href="https://veloxquant-mlx.netlify.app/docs/blog/turboquant-metal-kernels#2-xor-butterflies-are-wrong-for-parallel-wht" class="hash-link" aria-label="Direct link to 2. XOR butterflies are wrong for parallel WHT" title="Direct link to 2. XOR butterflies are wrong for parallel WHT" translate="no">​</a></h3>
<p>The standard sequential WHT uses <code>pair = i ^ stride</code>. On GPU this causes data races within a butterfly pass because multiple threads simultaneously read from and write to overlapping pairs. Use range-based pairing (<code>local = lane % (stride*2); is_upper = local &gt;= stride</code>) and read both values before the barrier.</p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="3-simd_sum-and-simd_shuffle-are-your-first-tools-not-shared-memory">3. <code>simd_sum</code> and <code>simd_shuffle</code> are your first tools, not shared memory<a href="https://veloxquant-mlx.netlify.app/docs/blog/turboquant-metal-kernels#3-simd_sum-and-simd_shuffle-are-your-first-tools-not-shared-memory" class="hash-link" aria-label="Direct link to 3-simd_sum-and-simd_shuffle-are-your-first-tools-not-shared-memory" title="Direct link to 3-simd_sum-and-simd_shuffle-are-your-first-tools-not-shared-memory" translate="no">​</a></h3>
<p>For reductions and broadcasts within a simdgroup (32 lanes), <code>simd_sum</code> and <code>simd_shuffle</code> are zero-cost compared to <code>threadgroup_barrier</code> + shared memory. Design around simdgroups first; only escalate to threadgroup memory when you need communication beyond 32 lanes.</p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="4-template-parameters-unlock-static-unrolling">4. Template parameters unlock static unrolling<a href="https://veloxquant-mlx.netlify.app/docs/blog/turboquant-metal-kernels#4-template-parameters-unlock-static-unrolling" class="hash-link" aria-label="Direct link to 4. Template parameters unlock static unrolling" title="Direct link to 4. Template parameters unlock static unrolling" translate="no">​</a></h3>
<p><code>template &lt;int B_BITS&gt;</code> turns runtime constants into compile-time constants. The inner loop over centroids becomes 2, 4, 8, or 16 unrolled iterations — no branch, no loop counter. This is how Metal kernels beat NumPy at large N despite higher launch overhead: the arithmetic is genuinely faster.</p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="5-you-dont-manage-commandbuffer">5. You don't manage <code>commandBuffer</code><a href="https://veloxquant-mlx.netlify.app/docs/blog/turboquant-metal-kernels#5-you-dont-manage-commandbuffer" class="hash-link" aria-label="Direct link to 5-you-dont-manage-commandbuffer" title="Direct link to 5-you-dont-manage-commandbuffer" translate="no">​</a></h3>
<p>MLX handles <code>commandBuffer.commit()</code> and <code>commandBuffer.waitUntilCompleted()</code> inside <code>mx.eval()</code>. You never touch Metal command buffers when using <code>mx.fast.metal_kernel</code>. This is by design — MLX's lazy graph batches multiple kernel dispatches into one command buffer where possible.</p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="6-the-launch-overhead-is-real-and-017-ms">6. The launch overhead is real and ~0.17 ms<a href="https://veloxquant-mlx.netlify.app/docs/blog/turboquant-metal-kernels#6-the-launch-overhead-is-real-and-017-ms" class="hash-link" aria-label="Direct link to 6. The launch overhead is real and ~0.17 ms" title="Direct link to 6. The launch overhead is real and ~0.17 ms" translate="no">​</a></h3>
<p>Every Metal kernel dispatch costs ~0.17 ms regardless of work size. For small N (&lt; ~2k elements), NumPy is faster. For large N (&gt; ~16k), Metal wins by 10–30×. Design your batching strategy accordingly — combine small operations into a single larger kernel rather than dispatching many small ones.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="code-organization">Code Organization<a href="https://veloxquant-mlx.netlify.app/docs/blog/turboquant-metal-kernels#code-organization" class="hash-link" aria-label="Direct link to Code Organization" title="Direct link to Code Organization" translate="no">​</a></h2>
<p>The five kernels are organized into focused submodules under <code>veloxquant_mlx/metal/</code>:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">metal/</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">├── __init__.py          # lazy re-exports</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">├── kernels.py           # thin facade — imports from all submodules</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">├── _bit_packing.py      # turboquant_bit_pack, turboquant_bit_unpack</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">├── _scalar_quant.py     # turboquant_scalar_quantize, _dequantize, _hadamard_quantize</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">├── _qjl.py              # qjl_encode, qjl_inner_product</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">├── _rvq_attend.py       # turboquant_fused_rvq_decode_attend</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">└── _vecinfer.py         # vecinfer_dequant_metal, vecinfer_quantize_metal, ...</span><br></div></code></pre></div></div>
<p>Each submodule has its own <code>_cache: dict = {}</code> for the kernel singleton pattern — build the <code>MTLComputePipelineState</code> once on first call, reuse forever:</p>
<div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">def</span><span class="token plain"> </span><span class="token function" style="color:rgb(80, 250, 123)">_pack_kernel</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">b</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain"> </span><span class="token builtin" style="color:rgb(189, 147, 249)">int</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    key </span><span class="token operator">=</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token string" style="color:rgb(255, 121, 198)">"bit_pack"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> b</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">if</span><span class="token plain"> key </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">not</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">in</span><span class="token plain"> _cache</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        _cache</span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token plain">key</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token plain"> </span><span class="token operator">=</span><span class="token plain"> mx</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">fast</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">metal_kernel</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">            name</span><span class="token operator">=</span><span class="token string-interpolation string" style="color:rgb(255, 121, 198)">f"turboquant_bit_pack_b</span><span class="token string-interpolation interpolation punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token string-interpolation interpolation">b</span><span class="token string-interpolation interpolation punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token string-interpolation string" style="color:rgb(255, 121, 198)">"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">            input_names</span><span class="token operator">=</span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token string" style="color:rgb(255, 121, 198)">"indices"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">            output_names</span><span class="token operator">=</span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token string" style="color:rgb(255, 121, 198)">"packed"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">            source</span><span class="token operator">=</span><span class="token plain">_PACK_SRC</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        </span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">return</span><span class="token plain"> _cache</span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token plain">key</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><br></div></code></pre></div></div>
<p><code>kernels.py</code> is now a 47-line re-export facade:</p>
<div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">from</span><span class="token plain"> veloxquant_mlx</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">metal</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">_bit_packing </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">import</span><span class="token plain"> turboquant_bit_pack</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> turboquant_bit_unpack</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">from</span><span class="token plain"> veloxquant_mlx</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">metal</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">_scalar_quant </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">import</span><span class="token plain"> turboquant_scalar_quantize</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">from</span><span class="token plain"> veloxquant_mlx</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">metal</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">_qjl </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">import</span><span class="token plain"> qjl_encode</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> qjl_inner_product</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">from</span><span class="token plain"> veloxquant_mlx</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">metal</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">_rvq_attend </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">import</span><span class="token plain"> turboquant_fused_rvq_decode_attend</span><br></div></code></pre></div></div>
<p>All 40 tests pass after the restructuring — the facade is transparent to callers.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="the-broader-point">The Broader Point<a href="https://veloxquant-mlx.netlify.app/docs/blog/turboquant-metal-kernels#the-broader-point" class="hash-link" aria-label="Direct link to The Broader Point" title="Direct link to The Broader Point" translate="no">​</a></h2>
<p>Apple Silicon is a genuinely good target for this kind of work. Unified memory means you don't pay PCIe bandwidth to move data between CPU and GPU — the Metal kernel reads the same bytes your Python code just wrote. The simdgroup primitives (<code>simd_sum</code>, <code>simd_shuffle</code>) are clean and well-documented. And <code>mx.fast.metal_kernel</code> makes the iteration loop fast: write Metal source in Python, evaluate, fix, repeat.</p>
<p>The hard part isn't the Metal itself — it's understanding how MLX dispatches kernels. Once you internalize "grid = total threads, not threadgroups" and "lazy graph, so nothing runs until mx.eval()", the rest is straightforward shader programming.</p>
<p>The full source is in <a href="https://github.com/rajveer43/VeloxQuant-MLX" target="_blank" rel="noopener noreferrer" class="">VeloxQuant-MLX</a> under <code>veloxquant_mlx/metal/</code>. The benchmark script is at <code>veloxquant_mlx/benchmarks/metal_kernel_benchmark.py</code> and produces all the figures discussed here.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="references">References<a href="https://veloxquant-mlx.netlify.app/docs/blog/turboquant-metal-kernels#references" class="hash-link" aria-label="Direct link to References" title="Direct link to References" translate="no">​</a></h2>
<ul>
<li class=""><a href="https://arxiv.org/abs/2504.19874" target="_blank" rel="noopener noreferrer" class="">TurboQuant (ICLR 2026)</a> — Zandieh et al., "Online Vector Quantization with Near-optimal Distortion Rate"</li>
<li class=""><a href="https://arxiv.org/abs/2406.03482" target="_blank" rel="noopener noreferrer" class="">QJL (2024)</a> — Zandieh et al., "QJL: 1-Bit Quantized JL Transform for KV Cache Quantization"</li>
<li class=""><a href="https://arxiv.org/abs/2205.14135" target="_blank" rel="noopener noreferrer" class="">FlashAttention (NeurIPS 2022)</a> — Dao et al., online softmax algorithm</li>
<li class=""><a href="https://github.com/ml-explore/mlx" target="_blank" rel="noopener noreferrer" class="">Apple MLX</a> — the framework</li>
<li class=""><a href="https://developer.apple.com/metal/Metal-Shading-Language-Specification.pdf" target="_blank" rel="noopener noreferrer" class="">Metal Shading Language Specification</a> — simd_sum, simd_shuffle, threadgroup_barrier reference</li>
</ul>
<hr>
<p><em>Code: <a href="https://github.com/rajveer43/VeloxQuant-MLX" target="_blank" rel="noopener noreferrer" class="">github.com/rajveer43/VeloxQuant-MLX</a> · Previous post: <a class="" href="https://veloxquant-mlx.netlify.app/docs/blog/metal-kernels">I Wrote a Metal Kernel to Stop My Mac From OOMing on LLM Inference</a></em></p>]]></content>
        <author>
            <name>Rajveer Rathod</name>
            <uri>https://github.com/rajveer43</uri>
        </author>
        <category label="metal" term="metal"/>
        <category label="apple-silicon" term="apple-silicon"/>
        <category label="mlx" term="mlx"/>
        <category label="gpu" term="gpu"/>
        <category label="turboquant" term="turboquant"/>
        <category label="performance" term="performance"/>
    </entry>
    <entry>
        <title type="html"><![CDATA[Benchmark Results: 10 Models, 8 Compression Configs]]></title>
        <id>https://veloxquant-mlx.netlify.app/docs/blog/results</id>
        <link href="https://veloxquant-mlx.netlify.app/docs/blog/results"/>
        <updated>2026-05-17T00:00:00.000Z</updated>
        <summary type="html"><![CDATA[A full 8-model benchmark of residual vector quantization for KV cache compression on Apple M4 16GB. What worked, what didn't, and the one result that surprised me.]]></summary>
        <content type="html"><![CDATA[<p><em>A full 8-model benchmark of residual vector quantization for KV cache compression on Apple M4 16GB. What worked, what didn't, and the one result that surprised me.</em></p>
<hr>
<p>Here is the result I did not expect: a 32-billion-parameter model running <strong>faster</strong> with the KV cache compressed than without.</p>
<p>Qwen2.5-32B at RVQ 2-bit: 4.2 tok/s. The same model at full fp16 precision: 3.7 tok/s. That is a 14% throughput improvement from compression — on a 16GB MacBook. Not from a faster algorithm. From freeing up memory bandwidth that the model weights were competing for.</p>
<p>This is the full story: what we built, how it works, and what the benchmarks across 8 models actually show.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="the-memory-problem-every-mac-llm-user-knows">The Memory Problem Every Mac LLM User Knows<a href="https://veloxquant-mlx.netlify.app/docs/blog/results#the-memory-problem-every-mac-llm-user-knows" class="hash-link" aria-label="Direct link to The Memory Problem Every Mac LLM User Knows" title="Direct link to The Memory Problem Every Mac LLM User Knows" translate="no">​</a></h2>
<p>Every token your LLM generates writes two vectors into memory — a Key and a Value — for every attention head, across every layer. This is the KV cache. It grows linearly with context length.</p>
<p>For Mistral 7B (32 layers, 8 KV heads, head dimension 128) at 32K tokens, the KV cache at fp16 is:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">2 × 32 layers × 8 heads × 128 dims × 32,000 tokens × 2 bytes = 8.4 GB</span><br></div></code></pre></div></div>
<p>On a 16GB Mac, that is 8.4 GB for context alone — before model weights, before the OS, before your browser tab.</p>
<p>The KV cache is also the performance bottleneck. Most 7-8B models on Apple Silicon are memory-bandwidth bound: the chip can do the arithmetic faster than it can load data from unified memory. Every token requires streaming the full KV cache through the memory bus once per layer. Smaller cache = faster generation.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="why-2-bit-usually-fails">Why 2-Bit Usually Fails<a href="https://veloxquant-mlx.netlify.app/docs/blog/results#why-2-bit-usually-fails" class="hash-link" aria-label="Direct link to Why 2-Bit Usually Fails" title="Direct link to Why 2-Bit Usually Fails" translate="no">​</a></h2>
<p>The obvious approach is to quantize each float16 value (2 bytes) down to 2 bits — an 8x reduction. The problem: with only 4 quantization levels, reconstruction error is large enough to corrupt attention scores.</p>
<p>Cosine similarity between the original and compressed key vector — which directly determines attention quality — drops to <strong>0.69</strong> at single-pass 2-bit. For short generation (&lt; 100 tokens), this is often tolerable. For long reasoning chains, it is not. We measured this:</p>
<table><thead><tr><th>Model</th><th>Single-pass 2-bit: tokens generated</th></tr></thead><tbody><tr><td>Phi-4</td><td><strong>0 / 200</strong> — immediate EOS</td></tr><tr><td>Llama 3.1 8B</td><td><strong>32 / 200</strong> — collapses mid-sequence</td></tr><tr><td>Falcon3 7B</td><td><strong>38 / 200</strong> — degrades rapidly</td></tr><tr><td>Qwen2.5 32B</td><td><strong>5 / 200</strong> — immediate near-EOS</td></tr></tbody></table>
<p>This is not a mild degradation. For these models, standard 2-bit quantization simply does not work.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="the-fix-two-pass-residual-quantization">The Fix: Two-Pass Residual Quantization<a href="https://veloxquant-mlx.netlify.app/docs/blog/results#the-fix-two-pass-residual-quantization" class="hash-link" aria-label="Direct link to The Fix: Two-Pass Residual Quantization" title="Direct link to The Fix: Two-Pass Residual Quantization" translate="no">​</a></h2>
<p>The key insight: if your first codebook leaves large error, run a second codebook on the error itself.</p>
<p><strong>Stage 1</strong> fits a Gaussian Lloyd-Max codebook on the rotated key vector. The rotation (a randomized Hadamard transform) spreads information uniformly across dimensions, so the distribution after rotation looks Gaussian and the codebook fits it well.</p>
<p><strong>Stage 2</strong> takes the residual — the difference between the original and the stage-1 reconstruction — and fits a Laplacian codebook on that. The Laplacian distribution matches the residual better than a Gaussian: it is the tail of a Gaussian, peaked at zero, with heavier tails. Using the wrong distribution family for stage 2 leaves measurable error on the table.</p>
<p>Result: cosine similarity from <strong>0.69 → 0.98</strong> at 2 bits per dimension. The two-pass approach recovers information the single-pass approach discards.</p>
<p><strong>Storage per vector (d=128):</strong></p>
<ul>
<li class="">RVQ 2-bit: <code>ceil(128 × 2 × 2 / 8)</code> = 64 bytes per key vector vs 256 bytes at fp16 — <strong>4x compression</strong></li>
<li class="">RVQ 1-bit (sign quantizer + Laplacian residual): 32 bytes per vector — <strong>8x compression</strong>, cosine 0.917</li>
</ul>
<p>The 1-bit variant uses a two-level sign quantizer as stage 1 (<code>{−0.798, +0.798}</code> — the exact Gaussian Lloyd-Max solution at 1 bit) and a Laplacian codebook on the sign-quantization error for stage 2. It achieves 7.5x KV cache compression while keeping cosine above 0.91.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="the-throughput-problem-and-how-we-solved-it">The Throughput Problem (and How We Solved It)<a href="https://veloxquant-mlx.netlify.app/docs/blog/results#the-throughput-problem-and-how-we-solved-it" class="hash-link" aria-label="Direct link to The Throughput Problem (and How We Solved It)" title="Direct link to The Throughput Problem (and How We Solved It)" translate="no">​</a></h2>
<p>Compression reduces memory pressure. But if the quantization compute itself costs more than the bandwidth savings, the net result is slower generation.</p>
<p>Our initial implementation ran at 17.7 tok/s on Mistral 7B with RVQ 2-bit — slower than fp16's 22.1 tok/s. Four changes fixed this, each independently measurable:</p>
<p><strong>1. Batch all heads into one MLX call (+22%)</strong></p>
<p>The original code maintained a separate quantizer per attention head and looped over them in Python. For Mistral 7B (8 KV heads, 32 layers), that is 256 small kernel dispatches per token. The fix: reshape <code>(B, H, S, D) → (B·H·S, D)</code> and call one shared quantizer on the entire batch. MLX compiles the operation as a single graph node and dispatches it once.</p>
<p><strong>2. Switch to Hadamard rotation</strong></p>
<p>The rotation preconditioner used QR decomposition — a full <code>d×d</code> matrix multiply (16,384 operations at d=128). <code>mx.hadamard_transform</code> is a Metal-native fused kernel that achieves the same statistical effect in O(d log d) — 896 operations at d=128. The quality is mathematically identical: a Hadamard with a random ±1 diagonal is Haar-distributed, the same family as a random orthogonal matrix.</p>
<p><strong>3. Boundary-sum quantization instead of broadcast-argmin</strong></p>
<p>Codebook lookup was materializing a <code>(batch, d, k)</code> distance tensor and running argmin — three kernel launches: broadcast-subtract, absolute value, argmin. Lloyd-Max boundaries are just midpoints between sorted centroids. The nearest centroid index equals the number of boundaries the value exceeds: one comparison and one sum. Two kernels, no intermediate tensor. Verified bitwise-identical to the old path at 100.00% index match.</p>
<p><strong>4. Remove redundant dtype casts</strong></p>
<p>The update path was casting fp16 → fp32 → fp16 → fp32 → fp16 from accumulated unnecessary coercions. Removing them saved ~5% per call with no effect on output quality.</p>
<p>Net result: <strong>17.7 → 22.3 tok/s</strong> on Mistral 7B. The compression now matches fp16 throughput.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="full-8-model-benchmark">Full 8-Model Benchmark<a href="https://veloxquant-mlx.netlify.app/docs/blog/results#full-8-model-benchmark" class="hash-link" aria-label="Direct link to Full 8-Model Benchmark" title="Direct link to Full 8-Model Benchmark" translate="no">​</a></h2>
<p>Hardware: Apple M4 MacBook, 16GB unified memory. Each run: 200 tokens, one fresh Python subprocess per (model, config) to avoid MLX graph compilation bugs. 6 configs: fp16, TQ 2/3/4-bit (single-pass), RVQ 2-bit, RVQ 1-bit.</p>
<table><thead><tr><th>Model</th><th>fp16</th><th>RVQ 1-bit ★</th><th>RVQ 2-bit ★</th><th>TQ 4-bit</th><th>TQ 2-bit</th></tr></thead><tbody><tr><td>Mistral 7B</td><td>23.3 tok/s</td><td><strong>22.2</strong> (201/201)</td><td>22.5 (201)</td><td>21.4 (201)</td><td>22.1 (201)</td></tr><tr><td>Falcon3 7B</td><td>24.0 tok/s</td><td><strong>23.1</strong> (200/200)</td><td>22.7 (200)</td><td>22.1 (200)</td><td>17.4 (<strong>38/200</strong>)</td></tr><tr><td>Phi-4</td><td>11.9 tok/s</td><td><strong>11.8</strong> (200/200)</td><td>11.7 (200)</td><td>11.4 (200)</td><td>0.0 (<strong>0/200</strong>)</td></tr><tr><td>Qwen3 4B</td><td>40.2 tok/s</td><td><strong>34.3</strong> (187/200)</td><td>35.0 (197)</td><td>33.5 (199)</td><td>31.0 (170/200)</td></tr><tr><td>Qwen3 8B</td><td>20.5 tok/s</td><td><strong>21.1</strong> (200/200)</td><td>20.7 (200)</td><td>19.8 (200)</td><td>20.9 (200)</td></tr><tr><td>Llama 3.1 8B</td><td>22.0 tok/s</td><td><strong>21.5</strong> (201/201)</td><td>20.9 (201)</td><td>20.3 (201)</td><td>3.4 (<strong>32/201</strong>)</td></tr><tr><td>Gemma3 4B</td><td>32.5 tok/s</td><td><strong>30.5</strong> (201/201)</td><td>29.2 (201)</td><td>27.7 (201)</td><td>28.6 (198/201)</td></tr><tr><td><strong>Qwen2.5 32B</strong></td><td><strong>3.7</strong> tok/s</td><td><strong>3.9</strong> (200/200)</td><td><strong>4.2</strong> (200)</td><td>3.9 (200)</td><td>0.3 (<strong>5/200</strong>)</td></tr></tbody></table>
<p>★ = RVQ configs at 7.5x compression. Bolded token counts in TQ 2-bit column = failure.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="three-findings-worth-highlighting">Three Findings Worth Highlighting<a href="https://veloxquant-mlx.netlify.app/docs/blog/results#three-findings-worth-highlighting" class="hash-link" aria-label="Direct link to Three Findings Worth Highlighting" title="Direct link to Three Findings Worth Highlighting" translate="no">​</a></h2>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="1-rvq-1-bit-is-the-reliability-result">1. RVQ 1-bit is the reliability result<a href="https://veloxquant-mlx.netlify.app/docs/blog/results#1-rvq-1-bit-is-the-reliability-result" class="hash-link" aria-label="Direct link to 1. RVQ 1-bit is the reliability result" title="Direct link to 1. RVQ 1-bit is the reliability result" translate="no">​</a></h3>
<p>Across all 8 models at all 200 tokens, RVQ 1-bit with 7.5x compression produced complete, coherent output every time. TQ single-pass 2-bit failed catastrophically on 4 of 8 models.</p>
<p>This is the practical takeaway: if you want 2-bit compression and need your model to actually finish its output, residual quantization is not optional. The gap between 0.69 and 0.92 cosine is the gap between broken and working.</p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="2-at-32b-scale-compression-beats-fp16">2. At 32B scale, compression beats fp16<a href="https://veloxquant-mlx.netlify.app/docs/blog/results#2-at-32b-scale-compression-beats-fp16" class="hash-link" aria-label="Direct link to 2. At 32B scale, compression beats fp16" title="Direct link to 2. At 32B scale, compression beats fp16" translate="no">​</a></h3>
<p>Qwen2.5-32B is too large for 16GB without aggressive weight quantization. With 4-bit weights (the <code>Qwen2.5-32B-Instruct-4bit</code> model), it fits — but barely. The model consumes ~17.5 GB including the OS and runtime, leaving almost nothing for KV cache headroom. The chip is running at its memory bandwidth ceiling.</p>
<p>Compressing the KV cache frees bandwidth that the weight loads were competing for. The result is a net gain:</p>
<ul>
<li class="">fp16 KV cache: 3.7 tok/s</li>
<li class="">RVQ 2-bit KV cache: <strong>4.2 tok/s (+14%)</strong></li>
<li class="">RVQ 1-bit KV cache: <strong>3.9 tok/s (+7%)</strong></li>
</ul>
<p>The effect is largest at 32B because the bandwidth contention is sharpest. At 7-8B, the same effect exists but is smaller (Qwen3-8B: +3% at RVQ 1-bit). For models that fit comfortably in unified memory, the effect disappears.</p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="3-the-qwen3-4b-exception">3. The Qwen3 4B exception<a href="https://veloxquant-mlx.netlify.app/docs/blog/results#3-the-qwen3-4b-exception" class="hash-link" aria-label="Direct link to 3. The Qwen3 4B exception" title="Direct link to 3. The Qwen3 4B exception" translate="no">​</a></h3>
<p>Qwen3 4B runs at 40.2 tok/s at fp16 — unusually fast for a model this size, because it is small and the chip can move through layers quickly. At RVQ 1-bit, it drops to 34.3 tok/s (85% of fp16).</p>
<p>This is the trade-off inverting. At small model sizes, fp16 is fast enough that the quantization compute overhead is a larger fraction of total runtime. At large model sizes, the weight loads dominate and the overhead is amortized. The crossover is somewhere around 8B parameters on this hardware.</p>
<p>If you are running a 4B model on a Mac with 32GB, use fp16. If you are running it on 16GB and memory is the constraint, RVQ 2-bit at 35.0 tok/s and 197/200 tokens is the right trade.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="the-vlm-extension">The VLM Extension<a href="https://veloxquant-mlx.netlify.app/docs/blog/results#the-vlm-extension" class="hash-link" aria-label="Direct link to The VLM Extension" title="Direct link to The VLM Extension" translate="no">​</a></h2>
<p>We also extended quantization to Qwen2-VL-7B — a vision language model that uses bfloat16 internally (wider exponent range, needed for large-norm image patch tokens). The original code always cast to float16 before normalizing, which discarded the bfloat16 exponent range at exactly the wrong moment for image tokens.</p>
<p>The fix is one variable: <code>kdtype = keys.dtype</code>. Normalize in the actual dtype, cast to float16 only for the codebook lookup. The VLM key-distribution diagnostic across all 28 layers showed that image patch tokens and text tokens have nearly identical distributions after RMSNorm — the quantizer does not need special handling for image tokens. RVQ 2-bit cosine on real Qwen2-VL keys: <strong>0.979 for both image and text tokens</strong> across all layers.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="what-this-means-in-practice">What This Means in Practice<a href="https://veloxquant-mlx.netlify.app/docs/blog/results#what-this-means-in-practice" class="hash-link" aria-label="Direct link to What This Means in Practice" title="Direct link to What This Means in Practice" translate="no">​</a></h2>
<p><strong>16GB Mac users:</strong></p>
<p>For 7-8B models (Mistral, Llama, Falcon, Phi-4, Qwen3-8B), RVQ 1-bit gives you 7.5x KV cache compression at 94-99% of fp16 throughput. At 32K context, your KV cache drops from ~8 GB to ~1 GB. That is the difference between OOM and comfortable operation.</p>
<p>For Qwen2.5-32B on 16GB, RVQ is what makes the model faster than fp16. The compression is load-bearing.</p>
<p>For 4B models (Qwen3-4B, Gemma3-4B), use fp16 if you have memory. Use RVQ 2-bit if you do not.</p>
<p><strong>Do not use single-pass 2-bit.</strong> It fails on Phi-4, Llama, Falcon, and Qwen2.5-32B. If you need 2-bit compression, you need residual quantization.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="using-it">Using It<a href="https://veloxquant-mlx.netlify.app/docs/blog/results#using-it" class="hash-link" aria-label="Direct link to Using It" title="Direct link to Using It" translate="no">​</a></h2>
<div class="language-bash codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-bash codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">pip </span><span class="token function" style="color:rgb(80, 250, 123)">install</span><span class="token plain"> VeloxQuant-MLX</span><br></div></code></pre></div></div>
<div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">import</span><span class="token plain"> mlx_lm</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">from</span><span class="token plain"> mlx_kv_quant </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">import</span><span class="token plain"> KVCacheBuilder</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> KVCacheConfig</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">model</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> tokenizer </span><span class="token operator">=</span><span class="token plain"> mlx_lm</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">load</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token string" style="color:rgb(255, 121, 198)">"mlx-community/Mistral-7B-Instruct-v0.3-4bit"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token comment" style="color:rgb(98, 114, 164)"># 7.5x compression, 95% throughput</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">config </span><span class="token operator">=</span><span class="token plain"> KVCacheConfig</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">bits</span><span class="token operator">=</span><span class="token number">1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> algorithm</span><span class="token operator">=</span><span class="token string" style="color:rgb(255, 121, 198)">"turboquant_rvq"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">cache </span><span class="token operator">=</span><span class="token plain"> KVCacheBuilder</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">config</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">for_model</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">model</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">build</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">response </span><span class="token operator">=</span><span class="token plain"> mlx_lm</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">generate</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    model</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    tokenizer</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    prompt</span><span class="token operator">=</span><span class="token string" style="color:rgb(255, 121, 198)">"Explain the difference between RAM and unified memory."</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    kv_cache</span><span class="token operator">=</span><span class="token plain">cache</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    max_tokens</span><span class="token operator">=</span><span class="token number">500</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">print</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">response</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><br></div></code></pre></div></div>
<p>For RVQ 2-bit: <code>bits=2</code>. For single-pass 4-bit (safe on all models, less compression): <code>algorithm="turboquant_prod"</code>, <code>bits=4</code>.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="what-the-figures-show">What the Figures Show<a href="https://veloxquant-mlx.netlify.app/docs/blog/results#what-the-figures-show" class="hash-link" aria-label="Direct link to What the Figures Show" title="Direct link to What the Figures Show" translate="no">​</a></h2>
<p>Each model folder in <code>figures/2026-05-12/</code> contains 6 panels:</p>
<ol>
<li class=""><strong>Throughput comparison</strong> — tok/s across all 6 configs</li>
<li class=""><strong>Quality vs compression</strong> — cosine similarity curve with RVQ ★ markers</li>
<li class=""><strong>Memory at scale</strong> — KV cache bytes vs sequence length for each config</li>
<li class=""><strong>Attention distortion</strong> — per-config distortion bars</li>
<li class=""><strong>Output comparison</strong> — tokens generated per config (the failure-detection panel)</li>
<li class=""><strong>Full report</strong> — all panels on one sheet</li>
</ol>
<p>The output comparison panel (fig5) is the one to look at for reliability. Models where single-pass 2-bit generates 0 or 5 tokens show it plainly. RVQ configs all extend to the full 200-token bar.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="reproducibility">Reproducibility<a href="https://veloxquant-mlx.netlify.app/docs/blog/results#reproducibility" class="hash-link" aria-label="Direct link to Reproducibility" title="Direct link to Reproducibility" translate="no">​</a></h2>
<p>All runs use subprocess isolation — one fresh Python process per (model, config) — to avoid MLX's graph-reuse bug that causes 2nd+ configs to generate 0 tokens in the same process when the cache type changes. If you run these scripts yourself and see anomalously low token counts on non-fp16 configs, check that you are not running multiple configs in the same Python session.</p>
<div class="language-bash codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-bash codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token function" style="color:rgb(80, 250, 123)">git</span><span class="token plain"> clone https://github.com/rajveer43/veloxquant-mlx</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token builtin class-name" style="color:rgb(189, 147, 249)">cd</span><span class="token plain"> veloxquant-mlx</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">pip </span><span class="token function" style="color:rgb(80, 250, 123)">install</span><span class="token plain"> </span><span class="token parameter variable" style="color:rgb(189, 147, 249);font-style:italic">-e</span><span class="token plain"> </span><span class="token builtin class-name" style="color:rgb(189, 147, 249)">.</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token assign-left variable" style="color:rgb(189, 147, 249);font-style:italic">PYTHONPATH</span><span class="token operator">=</span><span class="token plain">. python3 benchmark_scripts/run_full_reports.py </span><span class="token parameter variable" style="color:rgb(189, 147, 249);font-style:italic">--models</span><span class="token plain"> mistral7b falcon3_7b llama31_8b</span><br></div></code></pre></div></div>
<p>Figures save to <code>figures/2026-05-12/&lt;model&gt;/</code>. Each model takes 10–15 minutes on M4.</p>
<hr>
<p><em>VeloxQuant-MLX is MIT licensed. Hardware: Apple M4, 16GB unified memory. MLX 0.24.x, mlx-lm 0.21.x.</em></p>]]></content>
        <author>
            <name>Rajveer Rathod</name>
            <uri>https://github.com/rajveer43</uri>
        </author>
        <category label="benchmarks" term="benchmarks"/>
        <category label="apple-silicon" term="apple-silicon"/>
        <category label="mlx" term="mlx"/>
        <category label="kv-cache" term="kv-cache"/>
    </entry>
    <entry>
        <title type="html"><![CDATA[I Ported a Google Research Paper to Apple Silicon]]></title>
        <id>https://veloxquant-mlx.netlify.app/docs/blog/10-model-study</id>
        <link href="https://veloxquant-mlx.netlify.app/docs/blog/10-model-study"/>
        <updated>2026-05-12T00:00:00.000Z</updated>
        <summary type="html"><![CDATA[How I built VeloxQuant-MLX — KV cache compression for LLMs running on your Mac]]></summary>
        <content type="html"><![CDATA[<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="how-i-built-veloxquant-mlx--kv-cache-compression-for-llms-running-on-your-mac">How I built VeloxQuant-MLX — KV cache compression for LLMs running on your Mac<a href="https://veloxquant-mlx.netlify.app/docs/blog/10-model-study#how-i-built-veloxquant-mlx--kv-cache-compression-for-llms-running-on-your-mac" class="hash-link" aria-label="Direct link to How I built VeloxQuant-MLX — KV cache compression for LLMs running on your Mac" title="Direct link to How I built VeloxQuant-MLX — KV cache compression for LLMs running on your Mac" translate="no">​</a></h2>
<hr>
<p>Every time a language model generates a token, it writes two vectors into memory: a <strong>Key</strong> and a <strong>Value</strong>. These accumulate silently in what's called the <strong>KV cache</strong> — one pair per token, per attention head, per layer.</p>
<p>At a short context that's fine. But at 4,000 tokens with a 7B model on a 16GB Mac, the KV cache alone can consume 4–6 GB. At 32K tokens, it can exceed the model weights themselves.</p>
<p>I wanted to fix this for Apple Silicon. So I spent the last few weeks porting three research algorithms from GPU-targeted papers into a native MLX library — and published it to PyPI as <strong>VeloxQuant-MLX</strong>.</p>
<p>Here's exactly how it works, what I had to rebuild from scratch, and what the benchmarks actually show.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="the-core-idea-why-kv-vectors-can-be-compressed">The Core Idea: Why KV Vectors Can Be Compressed<a href="https://veloxquant-mlx.netlify.app/docs/blog/10-model-study#the-core-idea-why-kv-vectors-can-be-compressed" class="hash-link" aria-label="Direct link to The Core Idea: Why KV Vectors Can Be Compressed" title="Direct link to The Core Idea: Why KV Vectors Can Be Compressed" translate="no">​</a></h2>
<p>Key vectors in a transformer aren't arbitrary. After a learned linear projection, they tend to follow a roughly Gaussian distribution — especially after layer normalization. This matters because <strong>scalar quantization theory tells us the optimal codebook for a Gaussian is the Lloyd-Max codebook</strong>.</p>
<p>The Lloyd-Max algorithm iterates two conditions until convergence:</p>
<ol>
<li class=""><strong>Optimal boundaries</strong> — the boundary between two quantization levels sits at the midpoint of their centroids</li>
<li class=""><strong>Optimal centroids</strong> — each centroid is the conditional mean of the distribution over its Voronoi cell</li>
</ol>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">c_i = ∫[b_{i-1} to b_i] x·f(x)dx  /  ∫[b_{i-1} to b_i] f(x)dx</span><br></div></code></pre></div></div>
<p>For a standard Gaussian, this converges to fixed centroids that you can precompute once and reuse for every layer, every model, every token. No per-block scale constants. No calibration data. Just a lookup table.</p>
<p>The problem: raw KV vectors are <em>not</em> standard Gaussian. They have varying magnitudes, correlated dimensions, and outlier channels that dominate the quantization error.</p>
<p>This is what TurboQuant solves.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="stage-1-random-rotation">Stage 1: Random Rotation<a href="https://veloxquant-mlx.netlify.app/docs/blog/10-model-study#stage-1-random-rotation" class="hash-link" aria-label="Direct link to Stage 1: Random Rotation" title="Direct link to Stage 1: Random Rotation" translate="no">​</a></h2>
<p>Before quantizing, multiply each Key vector by a random orthogonal matrix <strong>Π</strong>:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">k̃ = k · Πᵀ</span><br></div></code></pre></div></div>
<p>Why? Because a random rotation <strong>spreads information uniformly</strong> across all dimensions. Outlier channels — dimensions with unusually high variance — get diluted into all other dimensions. After rotation, the vector looks much more like an isotropic Gaussian, and the Lloyd-Max codebook fits it well.</p>
<p>The original TurboQuant paper uses QR decomposition on a random Gaussian matrix to generate <strong>Π</strong>. That's O(d²) per rotation.</p>
<p>For VeloxQuant-MLX, I added a second option: the <strong>randomized Hadamard transform</strong>:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">k̃ = D · diag(r) · WHT(k)</span><br></div></code></pre></div></div>
<p>where <code>D</code> is a fixed scaling factor, <code>r</code> is a random Rademacher vector (±1), and <code>WHT</code> is the Walsh-Hadamard Transform. This runs in <strong>O(d log d)</strong> and maps directly to <code>mx.hadamard_transform</code> — Metal-accelerated on Apple Silicon.</p>
<div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">class</span><span class="token plain"> </span><span class="token class-name">HadamardPreconditioner</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">Preconditioner</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">def</span><span class="token plain"> </span><span class="token function" style="color:rgb(80, 250, 123)">apply</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">self</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> x</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain"> mx</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">array</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token plain"> mx</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">array</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        </span><span class="token comment" style="color:rgb(98, 114, 164)"># x shape: (..., d)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        scaled </span><span class="token operator">=</span><span class="token plain"> x </span><span class="token operator">*</span><span class="token plain"> self</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">_diag_scale  </span><span class="token comment" style="color:rgb(98, 114, 164)"># diagonal randomization</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">return</span><span class="token plain"> mx</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">hadamard_transform</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">scaled</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token operator">/</span><span class="token plain"> math</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">sqrt</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">self</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">_d</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><br></div></code></pre></div></div>
<p>For head dimensions that are powers of 2 (128, 256, 512) — which covers nearly every production model — the Hadamard preconditioner is automatically selected.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="stage-2-lloyd-max-scalar-quantization">Stage 2: Lloyd-Max Scalar Quantization<a href="https://veloxquant-mlx.netlify.app/docs/blog/10-model-study#stage-2-lloyd-max-scalar-quantization" class="hash-link" aria-label="Direct link to Stage 2: Lloyd-Max Scalar Quantization" title="Direct link to Stage 2: Lloyd-Max Scalar Quantization" translate="no">​</a></h2>
<p>After rotation, normalize each vector to unit norm (store the norm as fp16 for later), then quantize each dimension independently using the precomputed codebook:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">for each dimension j in rotated key:</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    find nearest centroid in codebook</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    store index (b-1 bits)</span><br></div></code></pre></div></div>
<p>The codebook has 2^(b-1) centroids for a b-bit configuration. At b=4, that's 8 centroids — enough to achieve ~0.95 cosine similarity with the original key vector after dequantization.</p>
<p>The <code>lloyd_max()</code> function in the library solves this numerically using trapezoidal quadrature:</p>
<div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">def</span><span class="token plain"> </span><span class="token function" style="color:rgb(80, 250, 123)">lloyd_max</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">pdf_fn</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> support</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> n_levels</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> n_iter</span><span class="token operator">=</span><span class="token number">100</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> tol</span><span class="token operator">=</span><span class="token number">1e-8</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    centroids </span><span class="token operator">=</span><span class="token plain"> np</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">linspace</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">lo</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> hi</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> n_levels</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">for</span><span class="token plain"> _ </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">in</span><span class="token plain"> </span><span class="token builtin" style="color:rgb(189, 147, 249)">range</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">n_iter</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        boundaries </span><span class="token operator">=</span><span class="token plain"> midpoints</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">centroids</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        centroids </span><span class="token operator">=</span><span class="token plain"> conditional_means</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">pdf_fn</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> boundaries</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain">  </span><span class="token comment" style="color:rgb(98, 114, 164)"># numerical integration</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">if</span><span class="token plain"> converged</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">            </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">break</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">return</span><span class="token plain"> centroids</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> boundaries</span><br></div></code></pre></div></div>
<p>You run this once at startup. The result is a tiny lookup table (~8 floats for 3-bit) that gets reused for every token in every layer for the entire generation.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="stage-3-qjl-residual-correction">Stage 3: QJL Residual Correction<a href="https://veloxquant-mlx.netlify.app/docs/blog/10-model-study#stage-3-qjl-residual-correction" class="hash-link" aria-label="Direct link to Stage 3: QJL Residual Correction" title="Direct link to Stage 3: QJL Residual Correction" translate="no">​</a></h2>
<p>Even at 4-bit, MSE quantization leaves a residual error:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">r = k - k̂_MSE</span><br></div></code></pre></div></div>
<p>TurboQuant's key insight is that you can correct for this without storing the full residual. Instead, store only the <strong>sign sketch</strong>: apply a random Johnson-Lindenstrauss projection matrix <strong>S</strong> to the residual, then store only the signs:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">z = sign(S·r) ∈ {-1, +1}^m</span><br></div></code></pre></div></div>
<p>One bit per sketch dimension. For m=128 sketch dimensions, that's 128 bits = 16 bytes per key vector.</p>
<p>At attention time, the corrected inner product estimate becomes:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">⟨q,k⟩ ≈ ⟨q,k̂_MSE⟩ + ‖r‖ · (π/2m) · Σ_j z_j · sign(⟨S_j, q⟩)</span><br></div></code></pre></div></div>
<p>The residual norm <code>‖r‖</code> is stored as fp16 (2 bytes). The signs are bit-packed (1 bit each). The correction is <strong>unbiased</strong> — on expectation, it perfectly reconstructs the true inner product.</p>
<p>This is what makes TurboQuant different from plain quantization: the QJL stage recovers information that was thrown away, using only 1 bit per sketch dimension.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="memory-layout-what-actually-gets-stored">Memory Layout: What Actually Gets Stored<a href="https://veloxquant-mlx.netlify.app/docs/blog/10-model-study#memory-layout-what-actually-gets-stored" class="hash-link" aria-label="Direct link to Memory Layout: What Actually Gets Stored" title="Direct link to Memory Layout: What Actually Gets Stored" translate="no">​</a></h2>
<p>For each token, per attention head, VeloxQuant-MLX stores:</p>
<table><thead><tr><th>Component</th><th>Bits per dimension</th><th>Total bytes (d=128)</th></tr></thead><tbody><tr><td>MSE indices</td><td>b-1 bits × d</td><td>ceil(128 × 3 / 8) = 48 B</td></tr><tr><td>QJL signs</td><td>1 bit × m</td><td>ceil(128 / 8) = 16 B</td></tr><tr><td>Residual norm</td><td>fp16</td><td>2 B</td></tr><tr><td>Per-vector norm</td><td>fp16</td><td>2 B</td></tr><tr><td><strong>Key total</strong></td><td></td><td><strong>68 B</strong></td></tr><tr><td>Values (int8 + scale)</td><td>8 bits × d + fp16</td><td>130 B</td></tr><tr><td><strong>Grand total</strong></td><td></td><td><strong>198 B</strong></td></tr></tbody></table>
<p>Compare to fp16: 128 dimensions × 2 bytes = <strong>256 B for keys alone</strong>.</p>
<p>At 4-bit, key compression is <strong>4.27×</strong>. Including values, total KV compression is ~1.6×. With value compression (int8 per-token, already implemented), total KV compression reaches ~3.5–4×.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="the-bit-packing-problem">The Bit-Packing Problem<a href="https://veloxquant-mlx.netlify.app/docs/blog/10-model-study#the-bit-packing-problem" class="hash-link" aria-label="Direct link to The Bit-Packing Problem" title="Direct link to The Bit-Packing Problem" translate="no">​</a></h2>
<p>MLX has no sub-byte dtype. A 3-bit index stored naively in <code>uint8</code> wastes 5 bits and kills your compression ratio.</p>
<p>The solution is a <code>BitPackBuffer</code> — pack multiple indices into each byte:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">3-bit example: pack [2, 5, 1, 7, 3, ...] into bytes</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  byte 0: bits 0-2 = index[0], bits 3-5 = index[1], bits 6-7 = index[2][0:1]</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  byte 1: bit 0 = index[2][2], bits 1-3 = index[3], ...</span><br></div></code></pre></div></div>
<p>For unpack at attend time, the library uses vectorized numpy paths for b ∈ {1, 2, 4} and a loop fallback for b=3:</p>
<div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">if</span><span class="token plain"> b </span><span class="token operator">==</span><span class="token plain"> </span><span class="token number">2</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    shifts </span><span class="token operator">=</span><span class="token plain"> np</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">array</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token number">0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">2</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">4</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">6</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> dtype</span><span class="token operator">=</span><span class="token plain">np</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">uint8</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    vals </span><span class="token operator">=</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">packed</span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token boolean">None</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token plain"> </span><span class="token operator">&gt;&gt;</span><span class="token plain"> shifts</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token operator">&amp;</span><span class="token plain"> </span><span class="token number">0x3</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">reshape</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">n</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">return</span><span class="token plain"> vals</span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain">d</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><br></div></code></pre></div></div>
<p>This runs entirely in numpy before handing off to MLX for the attention computation — keeping the Metal graph clean.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="outlier-two-stream-cache">Outlier Two-Stream Cache<a href="https://veloxquant-mlx.netlify.app/docs/blog/10-model-study#outlier-two-stream-cache" class="hash-link" aria-label="Direct link to Outlier Two-Stream Cache" title="Direct link to Outlier Two-Stream Cache" translate="no">​</a></h2>
<p>Some attention heads have a handful of dimensions with dramatically higher variance than the others. These <strong>outlier channels</strong> dominate quantization error: if dimension 47 has 10× the typical variance, every other dimension's codebook entry gets contaminated.</p>
<p>VeloxQuant-MLX handles this with an optional two-stream cache:</p>
<ol>
<li class=""><strong>Calibration phase</strong> — observe the first N tokens, track per-channel variance</li>
<li class=""><strong>Detection</strong> — identify the top-K highest-variance channels</li>
<li class=""><strong>Split encoding</strong> — outlier channels → int8 with per-token scale; inlier channels → TurboQuant as normal</li>
</ol>
<div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token comment" style="color:rgb(98, 114, 164)"># During append:</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">k</span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> outlier_idx</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token plain"> </span><span class="token operator">=</span><span class="token plain"> </span><span class="token number">0</span><span class="token plain">           </span><span class="token comment" style="color:rgb(98, 114, 164)"># zero out outliers in inlier stream</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">encode inlier key </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">with</span><span class="token plain"> TurboQuant</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">store outlier channels </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">as</span><span class="token plain"> int8</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token comment" style="color:rgb(98, 114, 164)"># During attend:</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">scores </span><span class="token operator">=</span><span class="token plain"> turboquant_ip</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">q</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> encoded_keys</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">scores </span><span class="token operator">+=</span><span class="token plain"> int8_ip</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">q</span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token plain">outlier_idx</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> outlier_cache</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token operator">*</span><span class="token plain"> outlier_scales</span><br></div></code></pre></div></div>
<p>The result in benchmarks: at K=8 outlier channels, output quality is <strong>indistinguishable from fp16</strong> even at 4-bit compression. The cost is slightly higher memory usage (8 × 2 = 16 extra bytes per token per head) and a numpy↔MLX copy per token — which is the main throughput bottleneck in the current implementation.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="weight-quantization-compressing-the-model-too">Weight Quantization: Compressing the Model Too<a href="https://veloxquant-mlx.netlify.app/docs/blog/10-model-study#weight-quantization-compressing-the-model-too" class="hash-link" aria-label="Direct link to Weight Quantization: Compressing the Model Too" title="Direct link to Weight Quantization: Compressing the Model Too" translate="no">​</a></h2>
<p>One thing missing from the original papers: they only quantize the KV cache. The model weights stay fp16.</p>
<p>VeloxQuant-MLX adds a <code>QuantizedLinear</code> layer — a drop-in <code>nn.Module</code> replacement that applies TurboQuant to the weight matrix itself:</p>
<div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">from</span><span class="token plain"> mlx_kv_quant</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">weight </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">import</span><span class="token plain"> quantize_model</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">model </span><span class="token operator">=</span><span class="token plain"> load_model</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">quantize_model</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">model</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> bits</span><span class="token operator">=</span><span class="token number">4</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> seed</span><span class="token operator">=</span><span class="token number">42</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token comment" style="color:rgb(98, 114, 164)"># All nn.Linear layers are now QuantizedLinear</span><br></div></code></pre></div></div>
<p>Each row of the weight matrix is normalized, rotated with the same Hadamard preconditioner, and Lloyd-Max quantized. At inference, dequantize on the fly. At 3-bit, this compresses weight matrices by ~5× with minimal accuracy loss.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="benchmark-results-across-7-models">Benchmark Results Across 7 Models<a href="https://veloxquant-mlx.netlify.app/docs/blog/10-model-study#benchmark-results-across-7-models" class="hash-link" aria-label="Direct link to Benchmark Results Across 7 Models" title="Direct link to Benchmark Results Across 7 Models" translate="no">​</a></h2>
<p>I ran the full benchmark suite (fp16, 2-bit, 3-bit, 4-bit) across 7 models on an Apple M4 MacBook (16GB unified memory):</p>
<table><thead><tr><th>Model</th><th>fp16 tok/s</th><th>3-bit quality</th><th>4-bit quality</th></tr></thead><tbody><tr><td>Llama 3.2 3B</td><td>47.2</td><td>⚠️ Repetition loops</td><td>✅ Near-lossless</td></tr><tr><td>Mistral 7B</td><td>22.5</td><td>✅ Near-lossless</td><td>✅ Near-lossless</td></tr><tr><td>Falcon3 7B</td><td>22.1</td><td>✅ Near-lossless</td><td>✅ Near-lossless</td></tr><tr><td>Qwen3 4B</td><td>38.7</td><td>✅ Near-lossless</td><td>⚠️ Early stop</td></tr><tr><td>Qwen3 8B</td><td>20.6</td><td>⚠️ Partial</td><td>⚠️ Partial</td></tr><tr><td>Gemma-4</td><td>19.3</td><td>✅ Near-lossless</td><td>✅ Near-lossless</td></tr><tr><td>Qwen2.5 32B</td><td>7.1</td><td>✅ Near-lossless</td><td>✅ Near-lossless</td></tr></tbody></table>
<p><strong>Key finding:</strong> 4-bit is near-lossless on 5 of 7 models. 3-bit is near-lossless on 4 of 7. 2-bit (11.6× compression) breaks generation on all models tested — which matches the theoretical SNR floor (~4 dB at 2-bit vs ~10 dB at 4-bit).</p>
<p>The Qwen3 family underperforms because its thinking-mode models generate a <code>&lt;think&gt;</code> token stream that terminates early when attention quality degrades — a more sensitive indicator than output text quality.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="what-i-had-to-rebuild-for-apple-silicon">What I Had to Rebuild for Apple Silicon<a href="https://veloxquant-mlx.netlify.app/docs/blog/10-model-study#what-i-had-to-rebuild-for-apple-silicon" class="hash-link" aria-label="Direct link to What I Had to Rebuild for Apple Silicon" title="Direct link to What I Had to Rebuild for Apple Silicon" translate="no">​</a></h2>
<p>The TurboQuant paper targets NVIDIA GPUs with CUDA kernels. Porting to MLX required rebuilding several components from scratch:</p>
<p><strong>1. No in-place mutation.</strong> MLX uses lazy evaluation — array slices return views, not copies, and <code>array[i] = value</code> silently fails. Every cache write goes through numpy (pre-allocate numpy arrays, write indices into numpy, hand to MLX only for compute).</p>
<p><strong>2. No sub-byte dtypes.</strong> The <code>BitPackBuffer</code> class handles arbitrary bit-widths in pure numpy, with vectorized unpack for b ∈ {1,2,4} and a loop path for b=3.</p>
<p><strong>3. Metal-accelerated Hadamard.</strong> MLX exposes <code>mx.hadamard_transform</code> natively. Using it instead of the paper's QR rotation reduces rotation cost from O(d²) to O(d log d) — the entire operation runs on the GPU die of the M-series chip.</p>
<p><strong>4. No monkey-patching.</strong> The original <code>turboquant-mlx</code> research code patched <code>mlx_lm.scaled_dot_product_attention()</code> globally. VeloxQuant-MLX exposes a standalone <code>cache.attend(q)</code> method — no global side effects, clean integration with any framework.</p>
<p><strong>5. Pluggable architecture.</strong> The production library uses a Factory + Strategy + Registry pattern so you can swap quantizers at runtime without changing model code:</p>
<div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">cache </span><span class="token operator">=</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    KVCacheBuilder</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">with_method</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token string" style="color:rgb(255, 121, 198)">"turboquant_prod"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain">  </span><span class="token comment" style="color:rgb(98, 114, 164)"># swap to "polar", "qjl", "turboquant_mse"</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">with_head_dim</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token number">128</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">with_bit_width</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">inlier</span><span class="token operator">=</span><span class="token number">4</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">with_jl_dim</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token number">128</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">build</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><br></div></code></pre></div></div>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="three-algorithms-one-interface">Three Algorithms, One Interface<a href="https://veloxquant-mlx.netlify.app/docs/blog/10-model-study#three-algorithms-one-interface" class="hash-link" aria-label="Direct link to Three Algorithms, One Interface" title="Direct link to Three Algorithms, One Interface" translate="no">​</a></h2>
<p>VeloxQuant-MLX implements three quantization algorithms behind the same <code>KVCache</code> interface:</p>
<p><strong>TurboQuantProd</strong> — Rotation + Lloyd-Max (b-1 bits) + QJL residual (1 bit). Best quality-per-bit for production use.</p>
<p><strong>TurboQuantMSE</strong> — Rotation + Lloyd-Max (b bits), no residual correction. Lower memory overhead, slightly lower quality.</p>
<p><strong>PolarQuantizer</strong> — Recursive polar coordinate decomposition. Represents each vector as a sequence of angles at 4 levels, plus a final radius. Geometrically motivated — useful for very low-bit regimes.</p>
<p><strong>QJLQuantizer</strong> — Pure 1-bit: sign sketch only. Extreme compression (32× vs fp16), loses most content but preserves attention topology. Useful for very long contexts where quality is secondary to fitting in RAM.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="installing-and-using-it">Installing and Using It<a href="https://veloxquant-mlx.netlify.app/docs/blog/10-model-study#installing-and-using-it" class="hash-link" aria-label="Direct link to Installing and Using It" title="Direct link to Installing and Using It" translate="no">​</a></h2>
<div class="language-bash codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-bash codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">pip </span><span class="token function" style="color:rgb(80, 250, 123)">install</span><span class="token plain"> VeloxQuant-MLX</span><br></div></code></pre></div></div>
<p>Requires Python ≥ 3.11 and an Apple Silicon Mac.</p>
<div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">from</span><span class="token plain"> mlx_kv_quant </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">import</span><span class="token plain"> KVCacheBuilder</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">import</span><span class="token plain"> mlx</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">core </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">as</span><span class="token plain"> mx</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">import</span><span class="token plain"> numpy </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">as</span><span class="token plain"> np</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">cache </span><span class="token operator">=</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    KVCacheBuilder</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">with_method</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token string" style="color:rgb(255, 121, 198)">"turboquant_prod"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">with_head_dim</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token number">128</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">with_bit_width</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">inlier</span><span class="token operator">=</span><span class="token number">4</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">with_jl_dim</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token number">128</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">with_seed</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token number">42</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">build</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">rng </span><span class="token operator">=</span><span class="token plain"> np</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">random</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">default_rng</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token number">0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">for</span><span class="token plain"> _ </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">in</span><span class="token plain"> </span><span class="token builtin" style="color:rgb(189, 147, 249)">range</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token number">1000</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    k </span><span class="token operator">=</span><span class="token plain"> mx</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">array</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">rng</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">standard_normal</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token number">128</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">astype</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">np</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">float16</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    v </span><span class="token operator">=</span><span class="token plain"> mx</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">array</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">rng</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">standard_normal</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token number">128</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">astype</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">np</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">float16</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    cache</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">append</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">k</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> v</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">q </span><span class="token operator">=</span><span class="token plain"> mx</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">array</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">rng</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">standard_normal</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token number">128</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">astype</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">np</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">float16</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">output </span><span class="token operator">=</span><span class="token plain"> cache</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">attend</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">q</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">print</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token string-interpolation string" style="color:rgb(255, 121, 198)">f"Memory: </span><span class="token string-interpolation interpolation punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token string-interpolation interpolation">cache</span><span class="token string-interpolation interpolation punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token string-interpolation interpolation">memory_bytes</span><span class="token string-interpolation interpolation punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token string-interpolation interpolation punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token string-interpolation interpolation"> </span><span class="token string-interpolation interpolation operator">/</span><span class="token string-interpolation interpolation"> </span><span class="token string-interpolation interpolation number">1024</span><span class="token string-interpolation interpolation punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token string-interpolation interpolation format-spec">.1f</span><span class="token string-interpolation interpolation punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token string-interpolation string" style="color:rgb(255, 121, 198)"> KB for </span><span class="token string-interpolation interpolation punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token string-interpolation interpolation builtin" style="color:rgb(189, 147, 249)">len</span><span class="token string-interpolation interpolation punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token string-interpolation interpolation">cache</span><span class="token string-interpolation interpolation punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token string-interpolation interpolation punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token string-interpolation string" style="color:rgb(255, 121, 198)"> tokens"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token comment" style="color:rgb(98, 114, 164)"># Memory: 193.0 KB for 1000 tokens  (vs 500.0 KB fp16)</span><br></div></code></pre></div></div>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="whats-next">What's Next<a href="https://veloxquant-mlx.netlify.app/docs/blog/10-model-study#whats-next" class="hash-link" aria-label="Direct link to What's Next" title="Direct link to What's Next" translate="no">​</a></h2>
<p>The main bottleneck right now is the Python-level encode/decode loop. Every token requires a numpy↔MLX transfer per attention head per layer — that's <code>n_layers × n_heads</code> small copies per generation step. A fused Metal kernel would reduce this to near-zero overhead.</p>
<p>Other items on the roadmap:</p>
<ul>
<li class=""><strong>Perplexity benchmark</strong> on WikiText-2 for quantitative quality measurement</li>
<li class=""><strong>Value compression</strong> — int8 per-token is implemented, needs tuning at longer contexts</li>
<li class=""><strong>Longer context evaluation</strong> — 8K, 32K token sequences where KV memory dominates</li>
<li class=""><strong>Fused attention kernel</strong> — integrate <code>BitPackBuffer</code> unpack + Lloyd-Max decode + attention dot product into a single Metal dispatch</li>
</ul>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="references">References<a href="https://veloxquant-mlx.netlify.app/docs/blog/10-model-study#references" class="hash-link" aria-label="Direct link to References" title="Direct link to References" translate="no">​</a></h2>
<ul>
<li class=""><a href="https://arxiv.org/abs/2504.19874" target="_blank" rel="noopener noreferrer" class="">TurboQuant (ICLR 2026)</a> — Zandieh et al., <em>"Online Vector Quantization with Near-optimal Distortion Rate"</em></li>
<li class=""><a href="https://arxiv.org/abs/2502.02617" target="_blank" rel="noopener noreferrer" class="">PolarQuant (AISTATS 2026)</a> — <em>"PolarQuant: Quantizing KV Caches with Polar Transformation"</em></li>
<li class=""><a href="https://arxiv.org/abs/2406.03482" target="_blank" rel="noopener noreferrer" class="">QJL (2024)</a> — Zandieh et al., <em>"QJL: 1-Bit Quantized JL Transform for KV Cache Quantization"</em></li>
<li class=""><a href="https://github.com/ml-explore/mlx" target="_blank" rel="noopener noreferrer" class="">Apple MLX</a></li>
<li class=""><a href="https://pypi.org/project/VeloxQuant-MLX/" target="_blank" rel="noopener noreferrer" class="">VeloxQuant-MLX on PyPI</a></li>
</ul>]]></content>
        <author>
            <name>Rajveer Rathod</name>
            <uri>https://github.com/rajveer43</uri>
        </author>
        <category label="quantization" term="quantization"/>
        <category label="apple-silicon" term="apple-silicon"/>
        <category label="mlx" term="mlx"/>
        <category label="kv-cache" term="kv-cache"/>
        <category label="benchmarks" term="benchmarks"/>
    </entry>
    <entry>
        <title type="html"><![CDATA[VeloxQuant-MLX: Fast KV Cache Quantization for Apple Silicon]]></title>
        <id>https://veloxquant-mlx.netlify.app/docs/blog/overview</id>
        <link href="https://veloxquant-mlx.netlify.app/docs/blog/overview"/>
        <updated>2026-05-10T00:00:00.000Z</updated>
        <summary type="html"><![CDATA[TL;DR: The KV cache is the last major unoptimised bottleneck for local LLM inference on Apple Silicon. llama.cpp, Ollama, and LM Studio don't compress it. I built VeloxQuant-MLX to fix that — 9 quantization algorithms, custom Metal GPU kernels, up to 16× compression, plug into mlx_lm with 3 lines.]]></summary>
        <content type="html"><![CDATA[<blockquote>
<p><strong>TL;DR:</strong> The KV cache is the last major unoptimised bottleneck for local LLM inference on Apple Silicon. llama.cpp, Ollama, and LM Studio don't compress it. I built VeloxQuant-MLX to fix that — 9 quantization algorithms, custom Metal GPU kernels, up to 16× compression, plug into <code>mlx_lm</code> with 3 lines.</p>
</blockquote>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="the-wall-every-mac-llm-user-hits">The wall every Mac LLM user hits<a href="https://veloxquant-mlx.netlify.app/docs/blog/overview#the-wall-every-mac-llm-user-hits" class="hash-link" aria-label="Direct link to The wall every Mac LLM user hits" title="Direct link to The wall every Mac LLM user hits" translate="no">​</a></h2>
<p>You've got a MacBook with an M-series chip. You've downloaded llama.cpp, or Ollama, or LM Studio. You load a 7B model, start a conversation, and for the first few hundred tokens everything feels fast.</p>
<p>Then you push it. A long document. A complex coding task. A multi-turn conversation that's been going for a while. And suddenly — generation slows to a crawl, the fans spin up, or the process just dies.</p>
<p>This isn't a bug in those tools. It's a fundamental memory problem that none of them solve. And it lives in a part of the inference stack that almost nobody talks about: <strong>the KV cache</strong>.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="what-the-kv-cache-actually-is">What the KV cache actually is<a href="https://veloxquant-mlx.netlify.app/docs/blog/overview#what-the-kv-cache-actually-is" class="hash-link" aria-label="Direct link to What the KV cache actually is" title="Direct link to What the KV cache actually is" translate="no">​</a></h2>
<p>To understand the problem, you need to understand what happens inside a transformer when it generates text.</p>
<p>Every token in your context window requires the model to compute two matrices at every layer — a <strong>key</strong> and a <strong>value</strong>. These represent what that token "means" in context. Without caching, generating each new token would require recomputing these for every prior token — quadratic cost that makes long-context generation completely impractical.</p>
<p>So instead, the model stores them. Every time a token is processed, its key and value matrices are written to a cache. Future tokens look them up instead of recomputing them.</p>
<p>This is the KV cache. And it grows with every token you generate.</p>
<p>The math is simple:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">memory = num_layers × num_kv_heads × head_dim × seq_len × 2 (K+V) × 2 bytes (fp16)</span><br></div></code></pre></div></div>
<p>For <strong>Llama-3.1-8B at 8k context: 4.2 GB</strong>. At 32k context: <strong>16.8 GB</strong>. On a machine with 16 GB of total unified memory.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="why-apple-silicon-makes-this-worse-not-better">Why Apple Silicon makes this worse, not better<a href="https://veloxquant-mlx.netlify.app/docs/blog/overview#why-apple-silicon-makes-this-worse-not-better" class="hash-link" aria-label="Direct link to Why Apple Silicon makes this worse, not better" title="Direct link to Why Apple Silicon makes this worse, not better" translate="no">​</a></h2>
<p>On a discrete GPU setup — say, an NVIDIA card with 24 GB VRAM — the GPU has its own dedicated memory pool. The KV cache lives there, separate from your system RAM.</p>
<p>Apple Silicon doesn't work that way. M-series chips use <strong>unified memory</strong>: the CPU, GPU, and Neural Engine all share the same physical memory pool. Your model weights, your KV cache, your browser tabs, your IDE, your OS — all competing for the same 16 or 24 GB.</p>
<p>A 7B model at 4-bit quantisation takes roughly 4–5 GB. Add OS and background processes (~4 GB), Chrome with a few tabs (~2 GB), your IDE (~1 GB). You're at 11–12 GB before you've generated a single token. On a 16 GB machine, you have 4 GB left for the KV cache — which runs out around 6k context for a 7B model.</p>
<p>On a 24 GB machine it's better, but not solved. During the development of VeloxQuant-MLX, I ran a benchmark sweep across 8 models. <strong>Qwen2.5-32B failed every quantization config</strong> because the weights alone (~17.5 GB) plus OS and development tools left negative headroom for activations and cache buffers. The memory watchdog had to kill the process before it triggered a kernel panic:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">[07:28:09] free=62MB   inactive=1094MB pressure=unknown</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">[07:28:19] free=63MB   inactive=891MB  pressure=unknown</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">[07:28:19] CRITICAL: only 954MB available — killing PID 21438</span><br></div></code></pre></div></div>
<p>System recovered to 10.2 GB free immediately after the kill, with no GPU stall.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="what-existing-tools-do-about-this-essentially-nothing">What existing tools do about this: essentially nothing<a href="https://veloxquant-mlx.netlify.app/docs/blog/overview#what-existing-tools-do-about-this-essentially-nothing" class="hash-link" aria-label="Direct link to What existing tools do about this: essentially nothing" title="Direct link to What existing tools do about this: essentially nothing" translate="no">​</a></h2>
<p>This is the part that surprised me most when I started digging.</p>
<p><strong>llama.cpp</strong> has done extraordinary work on attention kernel optimisation, weight quantisation, batching, and speculative decoding. But the KV cache is stored at fp16 by default. There is experimental support (<code>--cache-type-k q8_0</code>) but it's not the default, not Metal-optimised, and the quantisation methods are basic scalar quantisation with no adaptation to actual key distributions.</p>
<p><strong>Ollama</strong> builds on top of llama.cpp and inherits all of this. The UX is excellent. The KV cache situation is unchanged.</p>
<p><strong>LM Studio</strong> adds a polished GUI and nice model management. Same story on the cache.</p>
<p><strong>mlx_lm</strong> — Apple's own MLX-based inference library — is the most natural fit for Apple Silicon and does excellent work. But its default KV cache is full fp16. No built-in compression.</p>
<p>All of these tools have optimised everything <em>around</em> the KV cache. None of them have solved the KV cache itself.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="the-insight-compress-the-cache-not-just-the-weights">The insight: compress the cache, not just the weights<a href="https://veloxquant-mlx.netlify.app/docs/blog/overview#the-insight-compress-the-cache-not-just-the-weights" class="hash-link" aria-label="Direct link to The insight: compress the cache, not just the weights" title="Direct link to The insight: compress the cache, not just the weights" translate="no">​</a></h2>
<p>Most quantisation work in the LLM ecosystem targets <strong>weight quantisation</strong> — compressing model parameters from fp16 down to 8-bit, 4-bit, or lower. This is what GGUF, GPTQ, AWQ, and most Ollama models use.</p>
<p>Weight quantisation is a one-time operation. You compress once, save, load the compressed version.</p>
<p>KV cache quantisation is different. It happens <strong>at inference time</strong>, on every token, for every layer. The keys and values are different every generation — you can't pre-compute anything.</p>
<p>The key insight is that while weight distributions are relatively uniform, <strong>KV cache distributions are not</strong>. Keys tend to follow Gaussian or Laplacian distributions after a rotation transform. This structure can be exploited with much more aggressive compression than weight quantisation — down to <strong>1 bit per dimension</strong> with surprisingly low quality loss.</p>
<p>The reason this works at such extreme bit rates: the attention computation only needs an <em>approximation</em> of the inner product <code>q · k</code>. Small errors in the reconstructed key vector translate to small errors in the attention score, which average out across many keys in the softmax.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="what-i-built-veloxquant-mlx">What I built: VeloxQuant-MLX<a href="https://veloxquant-mlx.netlify.app/docs/blog/overview#what-i-built-veloxquant-mlx" class="hash-link" aria-label="Direct link to What I built: VeloxQuant-MLX" title="Direct link to What I built: VeloxQuant-MLX" translate="no">​</a></h2>
<p>VeloxQuant-MLX is a KV cache compression library for Apple Silicon, built on top of MLX. It implements nine quantisation algorithms — from zero-calibration 1-bit methods to mixed-precision allocators — all backed by custom Metal GPU kernels compiled at runtime.</p>
<hr>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="the-algorithms">The algorithms<a href="https://veloxquant-mlx.netlify.app/docs/blog/overview#the-algorithms" class="hash-link" aria-label="Direct link to The algorithms" title="Direct link to The algorithms" translate="no">​</a></h3>
<h4 class="anchor anchorTargetStickyNavbar_Vzrq" id="turboquant-rvq--the-recommended-default">TurboQuant RVQ — the recommended default<a href="https://veloxquant-mlx.netlify.app/docs/blog/overview#turboquant-rvq--the-recommended-default" class="hash-link" aria-label="Direct link to TurboQuant RVQ — the recommended default" title="Direct link to TurboQuant RVQ — the recommended default" translate="no">​</a></h4>
<p>Zero calibration. Works on any model immediately. Uses Residual Vector Quantisation with analytical Gaussian and Laplacian codebooks precomputed from distribution theory.</p>
<p>Two residual passes at 1 bit each → <strong>7.5× compression</strong> with cosine similarity above 0.97.</p>
<div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">import</span><span class="token plain"> mlx_lm</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">from</span><span class="token plain"> veloxquant_mlx </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">import</span><span class="token plain"> KVCacheBuilder</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> KVCacheConfig</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">model</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> tokenizer </span><span class="token operator">=</span><span class="token plain"> mlx_lm</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">load</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token string" style="color:rgb(255, 121, 198)">"mlx-community/Llama-3.2-3B-Instruct-4bit"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">config </span><span class="token operator">=</span><span class="token plain"> KVCacheConfig</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">method</span><span class="token operator">=</span><span class="token string" style="color:rgb(255, 121, 198)">"turboquant_rvq"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> bit_width_inlier</span><span class="token operator">=</span><span class="token number">1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> seed</span><span class="token operator">=</span><span class="token number">42</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">caches </span><span class="token operator">=</span><span class="token plain"> KVCacheBuilder</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">for_model</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">model</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> config</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">model</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">make_cache </span><span class="token operator">=</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">lambda</span><span class="token plain"> </span><span class="token operator">*</span><span class="token plain">_a</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token operator">**</span><span class="token plain">_k</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain"> caches</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">response </span><span class="token operator">=</span><span class="token plain"> mlx_lm</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">generate</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    model</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    tokenizer</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    prompt</span><span class="token operator">=</span><span class="token string" style="color:rgb(255, 121, 198)">"Write a 3000-word analysis of the transformer architecture."</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    max_tokens</span><span class="token operator">=</span><span class="token number">3000</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><br></div></code></pre></div></div>
<h4 class="anchor anchorTargetStickyNavbar_Vzrq" id="vecinfer--16-with-metal-acceleration">VecInfer — 16× with Metal acceleration<a href="https://veloxquant-mlx.netlify.app/docs/blog/overview#vecinfer--16-with-metal-acceleration" class="hash-link" aria-label="Direct link to VecInfer — 16× with Metal acceleration" title="Direct link to VecInfer — 16× with Metal acceleration" translate="no">​</a></h4>
<p>Trades a 2-minute calibration step for <strong>16× compression</strong> via Product Vector Quantisation. Per-channel smooth scaling handles outlier dimensions that defeat standard VQ. The hot path runs through a Metal GPU kernel that is <strong>13× faster</strong> than equivalent MLX Python ops.</p>
<p>Standout result: <strong>Qwen2.5-7B VecInfer-1bit exceeds fp16 throughput</strong> at 16× compression, likely due to its strong GQA ratio reducing the number of KV heads.</p>
<h4 class="anchor anchorTargetStickyNavbar_Vzrq" id="ratequant--best-accuracy-per-bit">RateQuant — best accuracy per bit<a href="https://veloxquant-mlx.netlify.app/docs/blog/overview#ratequant--best-accuracy-per-bit" class="hash-link" aria-label="Direct link to RateQuant — best accuracy per bit" title="Direct link to RateQuant — best accuracy per bit" translate="no">​</a></h4>
<p>Runs a 90-second sensitivity probe across all transformer layers, learns which layers are most sensitive to quantisation noise, then uses reverse-waterfilling to allocate more bits to sensitive layers and fewer to insensitive ones.</p>
<p>At 2.0 average bits, RateQuant achieves <strong>2.7× lower perplexity degradation</strong> than uniform 2-bit quantisation at identical memory cost.</p>
<table><thead><tr><th>Model</th><th>Sensitivity ratio</th><th>Allocation</th><th>Result vs fp16</th></tr></thead><tbody><tr><td>Falcon3-7B</td><td>6.48×</td><td>14 × b=2, 14 × b=1</td><td><strong>100%</strong> at 5.22× compression</td></tr><tr><td>Gemma3-4B</td><td>14.39×</td><td>3 × b=3, 11 × b=2, 20 × b=1</td><td><strong>91%</strong> at 5.22× compression</td></tr></tbody></table>
<h4 class="anchor anchorTargetStickyNavbar_Vzrq" id="spectralquant--best-quality-at-long-context">SpectralQuant — best quality at long context<a href="https://veloxquant-mlx.netlify.app/docs/blog/overview#spectralquant--best-quality-at-long-context" class="hash-link" aria-label="Direct link to SpectralQuant — best quality at long context" title="Direct link to SpectralQuant — best quality at long context" translate="no">​</a></h4>
<p>Keys in transformer models concentrate ~96% of their variance in just 3–4% of dimensions. SpectralQuant rotates keys into their eigenvector basis via SVD, then applies separate codebooks to the "signal" dimensions (high variance) and "noise" dimensions (low variance).</p>
<table><thead><tr><th>Model</th><th>SpectralQuant</th><th>TurboQuant 3-bit</th><th>Quality gain</th></tr></thead><tbody><tr><td>Qwen2.5-0.5B</td><td>0.9072 cosim</td><td>0.8329 cosim</td><td><strong>+7.4pp</strong></td></tr><tr><td>Gemma 4 4B</td><td>0.8625 cosim</td><td>0.7581 cosim</td><td><strong>+10.4pp</strong></td></tr></tbody></table>
<p>At 16k context the rotation trick matters enormously — quantisation errors accumulate over long sequences, and aligning the codec to the actual variance structure cuts that accumulation dramatically.</p>
<h4 class="anchor anchorTargetStickyNavbar_Vzrq" id="rabitq--maximum-context-length">RaBitQ — maximum context length<a href="https://veloxquant-mlx.netlify.app/docs/blog/overview#rabitq--maximum-context-length" class="hash-link" aria-label="Direct link to RaBitQ — maximum context length" title="Direct link to RaBitQ — maximum context length" translate="no">​</a></h4>
<p>1-bit key compression via randomised Hadamard transform + binary sign packing + IVF clustering. Pairs with 4-bit MSE scalar quantisation on values for <strong>6× full KV compression</strong>.</p>
<table><thead><tr><th>Method</th><th>KV memory @ 1024 tok</th><th>Compression</th><th>Context @ 8 GB</th></tr></thead><tbody><tr><td>fp16 baseline</td><td>117.4 MB</td><td>1×</td><td>~17k tokens</td></tr><tr><td>RaBitQ keys + MSE-b4 values</td><td><strong>19.7 MB</strong></td><td><strong>6×</strong></td><td><strong>~103k tokens</strong></td></tr></tbody></table>
<p>6× more context in the same RAM budget.</p>
<h4 class="anchor anchorTargetStickyNavbar_Vzrq" id="commvq--rope-compatible-exact-vq-icml-2025">CommVQ — RoPE-compatible exact VQ (ICML 2025)<a href="https://veloxquant-mlx.netlify.app/docs/blog/overview#commvq--rope-compatible-exact-vq-icml-2025" class="hash-link" aria-label="Direct link to CommVQ — RoPE-compatible exact VQ (ICML 2025)" title="Direct link to CommVQ — RoPE-compatible exact VQ (ICML 2025)" translate="no">​</a></h4>
<p>Standard VQ breaks with RoPE positional encodings because <code>quantize(rotate(x)) ≠ rotate(quantize(x))</code>. CommVQ (Apple ML Research, ICML 2025) trains codebooks on pre-RoPE keys with a commutativity projection in the EM M-step. RoPE is applied exactly at decode time. <strong>64× key compression</strong> with exact positional encoding.</p>
<hr>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="the-metal-kernels">The Metal kernels<a href="https://veloxquant-mlx.netlify.app/docs/blog/overview#the-metal-kernels" class="hash-link" aria-label="Direct link to The Metal kernels" title="Direct link to The Metal kernels" translate="no">​</a></h3>
<p>All the compression math in the world doesn't help if the quantisation itself is slow. VeloxQuant-MLX compiles nine Metal GPU kernels at runtime using <code>mx.fast.metal_kernel</code>:</p>
<table><thead><tr><th>Kernel</th><th>What it does</th><th>Speedup</th></tr></thead><tbody><tr><td><code>vecinfer_quantize_metal</code></td><td>Fused nearest-centroid product VQ</td><td><strong>13×</strong></td></tr><tr><td><code>rabitq_hamming_score</code></td><td>XOR + popcount Hamming distance</td><td><strong>11×</strong></td></tr><tr><td><code>turboquant_hadamard_quantize</code></td><td>WHT rotation + scalar quant fused</td><td><strong>8.6×</strong></td></tr><tr><td><code>turboquant_fused_rvq_decode_attend</code></td><td>RVQ decode + attention in one dispatch</td><td><strong>6.9×</strong></td></tr><tr><td><code>metal_fused_sdpa</code></td><td>Dequant + scaled dot-product attention</td><td>avoids fp16 materialisation</td></tr></tbody></table>
<p>The fused SDPA kernel is the most impactful. Without fusion, dequantisation creates a full fp16 key matrix in memory before attention — which defeats much of the point of compression. The fused path keeps the cache compressed all the way until attention scores are computed.</p>
<p>The 30-line Metal kernel that powers VecInfer's 13× speedup:</p>
<div class="language-metal codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-metal codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">// One thread per sub-vector. Argmin lives in registers — no diff tensor.</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">uint vec_idx = thread_position_in_grid.x;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">float best_dist = INFINITY;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">uint  best_idx  = 0;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">for (uint c = 0; c &lt; n_centroids; ++c) {</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    float dist = 0.0f;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    for (uint i = 0; i &lt; sub_dim; ++i) {</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        float d = float(x[x_base + i]) - float(codebook[cb_base + i]);</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        dist += d * d;</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    }</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    if (dist &lt; best_dist) { best_dist = dist; best_idx = c; }</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">}</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">out[vec_idx] = best_idx;</span><br></div></code></pre></div></div>
<p>The key insight: the <code>[N, n_centroids, sub_dim]</code> diff tensor is never materialised. The argmin accumulator lives entirely in thread-local registers — <strong>98% peak memory reduction</strong> at long context shapes.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="real-numbers-across-10-models">Real numbers across 10 models<a href="https://veloxquant-mlx.netlify.app/docs/blog/overview#real-numbers-across-10-models" class="hash-link" aria-label="Direct link to Real numbers across 10 models" title="Direct link to Real numbers across 10 models" translate="no">​</a></h2>
<p>End-to-end <code>mlx_lm.generate</code>, 200-token prompt, 120-token generation, Apple M-series:</p>
<table><thead><tr><th>Model</th><th>fp16 tok/s</th><th>RVQ-1bit tok/s</th><th>VecInfer-1bit tok/s</th><th>VecInfer compression</th></tr></thead><tbody><tr><td>Llama-3.2-3B</td><td>47.6</td><td><strong>46.2</strong></td><td>40.2</td><td>16×</td></tr><tr><td>Llama-3.1-8B</td><td>20.5</td><td><strong>20.6</strong></td><td>19.6</td><td>16×</td></tr><tr><td>Mistral-7B</td><td>23.6</td><td><strong>22.8</strong></td><td>9.8</td><td>16×</td></tr><tr><td>Qwen2.5-7B</td><td>21.0</td><td>20.7</td><td><strong>21.5</strong> ⬆ exceeds fp16</td><td>16×</td></tr><tr><td>Falcon3-7B</td><td>17.3</td><td><strong>21.7</strong></td><td>17.0</td><td>16×</td></tr><tr><td>Gemma-3-4B</td><td>26.0</td><td>24.2</td><td><strong>22.6</strong></td><td>16×</td></tr></tbody></table>
<p><strong>RVQ-1bit</strong> is the safe default — within 5% of fp16 on most 7–8B models with zero calibration. <strong>VecInfer-1bit</strong> wins on compression (always 16×) and throughput on models with strong Grouped Query Attention ratios.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="what-this-means-in-practice">What this means in practice<a href="https://veloxquant-mlx.netlify.app/docs/blog/overview#what-this-means-in-practice" class="hash-link" aria-label="Direct link to What this means in practice" title="Direct link to What this means in practice" translate="no">​</a></h2>
<p>On a <strong>16 GB MacBook M3</strong>:</p>
<ul>
<li class="">Before: 7B model maxes out around 6k context before generation slows</li>
<li class="">After with RVQ 1-bit: that same cache now fits in ~450 MB, leaving headroom for 50k+ token contexts</li>
</ul>
<p>On a <strong>24 GB MacBook M3 Pro</strong>:</p>
<ul>
<li class="">Before: 13B models are borderline; long conversations risk OOM</li>
<li class="">After: 13B at 32k context fits comfortably; the cache that would have consumed 8 GB uses 500 MB</li>
</ul>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="quick-decision-guide">Quick decision guide<a href="https://veloxquant-mlx.netlify.app/docs/blog/overview#quick-decision-guide" class="hash-link" aria-label="Direct link to Quick decision guide" title="Direct link to Quick decision guide" translate="no">​</a></h2>
<table><thead><tr><th>Goal</th><th>Method</th></tr></thead><tbody><tr><td>No calibration, get started now</td><td><code>turboquant_rvq</code> b=1 — 7.5× compression</td></tr><tr><td>Maximum compression</td><td><code>vecinfer</code> 1-bit — 16× (needs 2 min calibration)</td></tr><tr><td>Best quality at any bit rate</td><td><code>spectral</code> b=3 — 5.33× with +7–10pp cosine sim gain</td></tr><tr><td>Best accuracy per average bit</td><td><code>ratequant</code> — 2.7× better than uniform at same memory</td></tr><tr><td>Maximum context length</td><td><code>rabitq</code> keys + MSE-b4 values — 6× full KV, 6× more context</td></tr><tr><td>RoPE-compatible exact VQ</td><td><code>comm_vq</code> — 64× key compression, exact positions</td></tr></tbody></table>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="try-it">Try it<a href="https://veloxquant-mlx.netlify.app/docs/blog/overview#try-it" class="hash-link" aria-label="Direct link to Try it" title="Direct link to Try it" translate="no">​</a></h2>
<div class="language-bash codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-bash codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><div class="token-line" style="color:#F8F8F2"><span class="token plain">pip </span><span class="token function" style="color:rgb(80, 250, 123)">install</span><span class="token plain"> VeloxQuant-MLX</span><br></div></code></pre></div></div>
<p>Requires macOS on Apple M1 or later · Python 3.11+ · MLX ≥ 0.18</p>
<p>📖 Documentation: <a href="https://veloxquant-mlx.netlify.app/docs/" target="_blank" rel="noopener noreferrer" class="">https://veloxquant-mlx.netlify.app/docs/</a>
📦 PyPI: <a href="https://pypi.org/project/VeloxQuant-MLX/" target="_blank" rel="noopener noreferrer" class="">https://pypi.org/project/VeloxQuant-MLX/</a>
⭐ GitHub: <a href="https://github.com/rajveer43/turboquant_mac_implementation" target="_blank" rel="noopener noreferrer" class="">https://github.com/rajveer43/turboquant_mac_implementation</a></p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="whats-next">What's next<a href="https://veloxquant-mlx.netlify.app/docs/blog/overview#whats-next" class="hash-link" aria-label="Direct link to What's next" title="Direct link to What's next" translate="no">​</a></h2>
<ul>
<li class=""><strong>Vision-language model support</strong> — visual tokens have fundamentally different KV distributions; heavy outliers require adapted algorithms</li>
<li class=""><strong>Per-head RateQuant granularity</strong> — the paper allocates bits per layer-head group; current implementation is per-layer, leaving ~30% accuracy improvement on the table</li>
<li class=""><strong>Gradient-based sensitivity</strong> for RateQuant — more accurate than activation-based, at the cost of a slightly longer calibration pass</li>
</ul>
<hr>
<p>The KV cache is the last major unoptimised bottleneck for local LLM inference on Apple Silicon. llama.cpp, Ollama, and LM Studio have built excellent tools — but none of them have solved this. VeloxQuant-MLX is my attempt.</p>
<p>If you're running models locally on a Mac and hitting memory walls — at any context length, on any model — I'd genuinely like to hear about it.</p>
<hr>
<p><em>MIT License · Built for Apple Silicon · Engineered for speed</em></p>]]></content>
        <author>
            <name>Rajveer Rathod</name>
            <uri>https://github.com/rajveer43</uri>
        </author>
        <category label="quantization" term="quantization"/>
        <category label="apple-silicon" term="apple-silicon"/>
        <category label="mlx" term="mlx"/>
        <category label="kv-cache" term="kv-cache"/>
    </entry>
</feed>