TORAD LABS
Book a call

Inference · CUDA · Quantization

Running a 27B model on a 16 GB gaming card

How my fork of llama.cpp serves Ternary Bonsai 2 27B on one RTX 5080 at about 200 tokens per second, and still writes 154.8 tokens per second with 131,072 tokens already in the conversation.

Opening

What this article covers

I run a 27-billion-weight language model, Ternary Bonsai 2 27B, on one RTX 5080 gaming card with 16 GB of memory, using my own fork of llama.cpp. By the end of this post you'll understand:

  1. How a ternary 27B model fits in 7.65 GB, and why the card's memory speed limits how fast it writes.
  2. Why writing slows down in long conversations, and how an attention kernel that reads the 4-bit cache directly keeps it fast at 131,072 tokens.
  3. How speculative decoding with the model's MTP draft head works, why it adds 76 to 81% to writing speed, and why I keep it at two drafted tokens.
  4. Why PrismML's 1.75-bit PTQ1_0 format read 18% fewer bytes and still wrote slower with the CUDA kernel I wrote for it.
  5. How the weight kernel on main uses the card's L2 cache and TMA on sm_120, and what turning each default off costs.
  6. The quality bar for changes that alter the numbers, and what a CPU reference showed about the tensor-core weight kernel.
  7. The work that keeps long conversations fast and accurate, from the recurrent state's number format to the mask pre-pass.
  8. How much of the card a 131,072-token conversation uses.
  9. How I measure, so that a 1% change shows up above the noise.
  10. How to use the fork as a local model for Claude Code.
  11. How to build and run the fork yourself.

Every number in this article comes from one RTX 5080 at its stock 396 W power limit, driver 615.71.10, and from the fork's main branch at bdb47f8, unless I name the build. The build I compare main against is c008fe8, the fork's last release. In a served chat with the draft head on, main writes 198.8 tokens per second with a short conversation, 190.5 with 32,768 tokens before it and 154.8 with 131,072, which is 33 to 36% more than c008fe8 at each length. Without the draft head, in llama-bench, main writes 15 to 20% faster than c008fe8 and reads prompts 1.8 to 2.9% faster.

1

How the model fits in 7.65 GB, and what limits its speed

The model is Ternary Bonsai 2 27B, which is PrismML's version of Qwen3.8-27B. A weight is one of the numbers a model learns in training, and most models store each weight in 4 to 16 bits. In a ternary model every weight is -1, 0 or +1, and each group of 128 weights shares one scale, which PrismML packs at 2.13 bits per weight. That brings the model's 27.32 billion weights down to 7.65 GB, so the whole model fits on a 16 GB card and still leaves room for a long conversation.

The software is my fork of PrismML's llama.cpp, which in turn builds on llama.cpp, the open-source engine most people use to run models on their own machines. The fork is public at github.com/torad-labs/llama.cpp, and its Bonsai page has the serve command, the chat template and the benchmark scripts.

A language model writes one token at a time, where a token is roughly a word or part of a word. To write each token, the engine runs all 64 layers of the model, and each layer uses all of its weights, which means that for every token the card reads the full 7.65 GB of weights from its memory. The math on those weights is quick compared with fetching them, so most of the time per token goes into the reads. Work like this is memory-bound, meaning its speed depends on how fast the card reads memory, and writing one conversation at a time on this model is memory-bound.

That's also why ternary weights help so much on this card. At 4 bits per weight, the same 27.32 billion weights would take about 13.7 GB, most of the card, before the engine stored any conversation at all. At 2.13 bits they take less than half of the card, and since the card reads every weight for every token, each token also has about half as much to read.

The card reads all 7.65 GB of weights for every token it writes, so at 2.13 bits per weight each token has about half the bytes to read that it would at 4 bits.

2

Long conversations and the 4-bit attention kernel

Long conversations add a second cost, because each new token has to look back at every token that came before it, in a step we call attention. Attention works with a key and a value for every past token, and recomputing all of them for every new token would repeat the same work again and again. To avoid that, the engine keeps a record of the conversation, called the KV cache, which stores the keys and values once so the engine can reuse them for every new token. The cache removes the recomputing, but attention still has to read the whole cache for each new token it writes, and the cache gets longer with every token in the conversation. Reading it is the part of the cost that keeps growing, and that's why in every engine writing slows down as the conversation grows.

Bonsai 2 27B's design limits that slowdown, because only 16 of its 64 layers use full attention and keep a KV cache. The other 48 are Gated DeltaNet layers, each of which keeps a fixed-size summary of the conversation, called a recurrent state, so their cost stays the same however long the conversation gets. For the 16 attention layers, every run in this article stores the KV cache in a 4-bit format called q4_0, which saves memory and also cuts the bytes attention has to read for each token.

How the stock attention kernels read the 4-bit cache

Flash attention is the standard way to compute attention without writing large intermediate results to memory, and llama.cpp has a version of it that runs on the tensor cores, the part of the card built for matrix math. That tensor-core version can't read a q4_0 cache, though. So whenever the stock code used it, it first converted all of the conversation's K and V, the two halves of the cache, into 16-bit numbers in the card's memory, and then every tile of queries read that copy. The conversion plus the reads of the copy add up to 3.6 times the bytes of the 4-bit cache, in every attention layer, every time the kernel ran.

During decode, the phase where the model writes, the stock code used a different kernel, the vector kernel, which can read the 4-bit cache directly and so never makes the copy. But the vector kernel read K and V once for each query head, and this model has six query heads for each KV head (engineers call this sharing grouped-query attention). As a result, the vector kernel read every part of the cache six times, where the tensor-core kernel reads it once for each group.

Commit 60feea0 fixes both problems with a version of the tensor-core kernel built for this model's shape, which means head size 256, q4_0 K and V, and more than four query heads per KV head. Like the stock tensor-core kernel, it reads the cache once for each group of query heads, but it copies the 4-bit blocks into shared memory, the small fast memory inside each of the card's processors, and converts them there. That way it never writes a 16-bit copy back to the card's main memory. On the RTX 5080 at 131,072 tokens of context, it took decode from 44.5 to 73.3 tokens per second and prompt reading from 930 to 1,382. Near the full window, at 261,874 tokens, decode went from 29.0 to 60.0 tokens per second, as attention dropped from 1.43 to 0.36 milliseconds per layer per token.

Because the kernel converts the same values, only in shared memory, and feeds them into the same math, it should give exactly the same results as the path that converts in main memory. We checked this with perplexity, which measures how surprised the model is by real text (lower is better). Over 8 chunks of 8,192 tokens the perplexity was 4.5065, and every chunk was identical to the old path.

The kernel then goes one step further, and this step does change the results slightly. It rounds the queries to 8-bit integers, with one scale per column, so that it can run the Q·K multiplication directly against the raw 4-bit values with the tensor cores' integer instructions. Perplexity moved from 4.5065 to 4.5074, 0.02% higher, and that rounding later became the quality bar for other changes (chapter 6). Decode switches to the new kernel from 4,096 tokens of context on, and below that it keeps the vector kernel, because the vector kernel is 0.5% faster at those lengths.

At 131,072 tokens of context on the RTX 5080, the 4-bit attention kernel took decode from 44.5 to 73.3 tokens per second.

Main against the last release

Main adds a new weight kernel on top of that attention kernel (chapter 5). To measure what it adds, I ran c008fe8, then main, then c008fe8 again with llama-bench, using the q4_0 cache and flash attention, no draft head and five repetitions per leg:

Main writes 15 to 20% faster than c008fe8 at every depth but reads prompts only 1.8 to 2.9% faster, and the difference comes from where main's new weight kernel runs. Main multiplies the PQ2_0 weights on the tensor cores whenever a batch holds 1 to 8 tokens, which covers every decode step, while prompt reading sends batches of 512 tokens through a different kernel. The gain shrinks to 14.9% at 131,072 tokens because at that length attention takes a larger share of each token, and the new weight kernel leaves attention alone.

3

Speculative decoding with the MTP draft head

Most of the time per token goes into reading the weights, and one read of the weights can serve several tokens at once, so the model can check a handful of tokens in one pass in about the time it takes to write one. That means if something cheap can guess the next few tokens, the big model can check all the guesses in one pass, keep the ones it agrees with, and write several tokens with a single read of the weights. This technique is speculative decoding.

The model file I use, ProCreations' pack of PrismML's weights, includes a small extra piece for this, a 15-tensor multi-token-prediction head, or MTP head, trained to guess the next tokens from the big model's last state. In each round the head drafts two tokens, and the big model checks both in one pass, keeps every guess up to the first one it disagrees with, and then adds one token of its own, so each round writes between one and three tokens.

PrismML's release refuses to load the head, and its log explains that the token table "is read without the inverse transform". PrismML stores the model's token table rotated with a Hadamard transform, a fixed rotation that makes the numbers easier to store in few bits. The main model undoes the rotation whenever it looks up a token, but the draft head's code looked tokens up in the same table without undoing it, so on this pack the head would have received rotated rows it never saw in training. Commit 514c53c undoes the rotation after the lookup, the same way the main model does, so the head gets the rows it was trained on and the fork loads it.

For the served test, I run six prompts that each produce up to 512 tokens, with greedy decoding, meaning the engine always picks its most likely next token. I ran each build three times, alternating them, at three conversation lengths:

The standard deviation across the three runs of each cell stayed under 0.2 tokens per second. Each check step runs a batch of three tokens through the model, and on main that batch also goes through the tensor-core weight kernel from chapter 5, so the served gain is larger than the llama-bench gain from chapter 2.

With the draft head on, main wrote 198.8 tokens per second with a short conversation and 154.8 with 131,072 tokens before it, on one 16 GB card.

To see what the head itself adds, I ran main once without it at each length. Main wrote 113.2, 105.6 and 85.6 tokens per second, so the head added 76%, 80% and 81%. Those no-head numbers are single runs, but they fit the llama-bench numbers from chapter 2, and the head helps slightly more in long conversations because each round shares one pass through attention among up to three tokens.

Code is where the head helps most after arithmetic

How much the head helps depends on the text, because the head guesses more of the next tokens when they follow from what came before. Code and SQL follow their own syntax closely, so the head guesses well on them:

The code prompt wrote 214.0 tokens per second with a short conversation and 170.5 with 131,072 tokens before it, which is about where a coding agent works, with a long file or a whole repo map in context. With thinking on, the model first writes its reasoning in prose, and the head guesses prose less well, so the same code request wrote 157.6.

On the code prompt, main wrote 214.0 tokens per second with a short conversation and 170.5 with 131,072 tokens of context.

Two drafted tokens or three

Drafting three tokens a round in place of two should help when the head guesses well, so I measured it. In single runs, three tokens made main 2.2% faster with a short conversation and 8.6% faster at 131,072 tokens, even though the model accepted only 52.9% and 58.6% of the drafts. Checking four tokens in one pass changes the numbers the model computes, though, so I compared its output against the model with no draft at all. The mean KL divergence was 0.0028, against a bar of 0.00150, and the two put the same token first 97.4% of the time, against a bar of 98.3% (chapter 6 explains the bar). Three drafted tokens failed both parts, so the serve command keeps --spec-draft-n-max 2.

Two smaller changes made the check step cheaper. In every Gated DeltaNet layer the three-token check batch includes a small 48-row matrix that the tiled kernel can't take. PrismML's code sent that matrix to a general cuBLAS matrix multiply that costs about 26 microseconds, and commit 55b7db6 sends it to the vector kernel. The other change concerns two kernels that the stock code limited to NVIDIA's DGX Spark, and commit d371dff turns them on for GeForce Blackwell cards too, with identical results.

4

Why the 1.75-bit PTQ1_0 format ran slower

Writing on this model is memory-bound, and the CUDA-core kernel that multiplied the weights before main already pulled most of the bandwidth the card's memory can deliver. That's why one experiment with the existing format didn't help. We tried decoding each block of weights once and using it for several columns at a time, and at 2 to 6 columns the speed stayed the same, because the kernel had no memory time left to save. With the bandwidth nearly used up, the next thing to try was reading fewer bytes.

PrismML's format, PQ2_0, spends 2 bits on each weight plus the group's scale, but a value that can only be -1, 0 or +1 carries about 1.585 bits of information (the base-2 logarithm of 3), so a 2-bit slot leaves some room unused. PrismML's tighter format, PTQ1_0, uses that room by packing five ternary digits into each byte, the way you'd write a number in base 3. A group of 128 weights then takes 28 bytes where PQ2_0 takes 34, which works out to 1.75 bits per weight against 2.125. The packing is lossless, so every weight unpacks bit-identical to PQ2_0, and the model file shrinks from 7.65 GB to 6.40 GB.

If bytes set the speed, reading about 18% fewer of them should make the model faster, and since I expected exactly that, I wrote a CUDA decode kernel for PTQ1_0.

The first version of that kernel kept only 25 to 33% of the card's processors occupied, so most of the room for running threads sat empty. Splitting each 128-weight block across four threads raised that to 72%, but even then PTQ1_0 wrote slower than PQ2_0 in every A-B-A run on the same binary, on 18% fewer bytes.

The profiler showed why. For this format the card does its main arithmetic with an instruction called dp4a, which multiplies four pairs of 8-bit numbers and adds up the results in one step, and before each dp4a the kernel has to get the weights ready for it. With PQ2_0 that takes about 4.3 instructions of machine code, mostly shifts and masks that pull two bits at a time out of a byte. With five base-3 digits in a byte, a shift can't pull out the third digit, so the kernel has to compute each digit with arithmetic on the whole byte, and that took **about 10 instructions per dp4a**. In total the PTQ1_0 kernel executed 1.75 to 2.2 times as many instructions as the PQ2_0 kernel, and it used only 43 to 60% of the memory bandwidth while doing it.

PTQ1_0 needed about 10 machine instructions per dp4a to unpack its weights, against 4.3 for PQ2_0, so it wrote slower on 18% fewer bytes.

Those extra instructions cost time because each processor on the card can start only so many instructions per clock cycle. The PQ2_0 kernel never got near that limit, so it spent its time waiting on memory, which is what memory-bound means. The PTQ1_0 kernel hit the instruction limit first and left part of the memory bandwidth unused, which makes it issue-bound. Because it waits on its own instructions, reading fewer bytes can't speed it up.

So unpacking PTQ1_0 on this card, with this kernel, cost more time than the smaller reads saved. A different card, with more memory per instruction it can issue, could come out the other way, and so could a better kernel. I didn't find one, so I left PTQ1_0 out of the fork's main branch.

5

The L2 cache, TMA and launch overlap on sm_120

The CUDA-core weight kernel ran near the card's bandwidth while it ran, but each decode step also spends time between those kernels. One step launches 257 PQ2_0 weight multiplies, 192 in the Gated DeltaNet layers, 64 in the attention layers and one for the output, and between them run many smaller kernels that read little from memory. During those small kernels the card's memory sits mostly idle, and the next weight kernel starts with none of its weights in the L2 cache, the cache between the card's processors and its memory. On sm_120, main fills that idle time with three changes, which all run by default.

  • The weights go through the tensor cores. For batches of 1 to 8 tokens, main multiplies the PQ2_0 weights with the tensor cores' 8-bit integer instruction, mma.sync in its m16n8k32 shape. Each warp, a group of 32 threads that run together, keeps a ring of two slots in shared memory and fills them with TMA, the card's copy engine for bulk transfers, so one slot loads while the warp computes on the other. The loads carry an evict_first hint, which tells the L2 cache to drop those lines first, since the step reads each weight once. sm_120 has no wgmma or tcgen05, the larger tensor-core instructions of Hopper and B200, so the kernel uses mma.sync.
  • A paced L2 issuer fetches the next weights early. After each heavy weight multiply, main launches a small kernel on its own stream that loads the weights of the next kernels into the L2 cache with cp.async.bulk.prefetch.L2. It paces those requests at 90% of the memory's peak rate, which leaves room for the kernels in front of it.
  • Launches overlap with PDL. Programmatic dependent launch lets the next kernel start its setup and its weight requests before the previous kernel finishes, so the gap between two kernels shrinks.

To measure what each default is worth, I turned each one off with its environment switch and ran default, switch and default again on the RTX 5080, with llama-bench tg128 on an empty conversation and ten repetitions each:

Every default beat its switch, by 0.65% for the evict_first hint and up to 5.8% for the tensor-core kernel, so none of the defaults needs to change. The 257 launches per step suggested one more change. A candidate that folds the Gated DeltaNet layers' alpha and beta projections into the convolution-state update removes 48 launches and passes the operator tests, but it wrote 0.53% slower on the RTX 5080, so I haven't merged it.

Switching main back to the CUDA-core weight kernel cost 5.8% of writing speed on the RTX 5080, and turning off launch overlap cost 4.9%.

The tensor-core weight kernel, with its TMA ring, its evict_first hint and its L2 prefetch, runs only on sm_120. The paced L2 issuer and PDL run on sm_90 and newer, and the rest of the fork runs on sm_80 and newer or on every card (chapter 11).

6

The quality bar, and what a CPU reference showed

Some changes make the engine faster by changing the numbers it computes, like the 8-bit queries in the attention kernel, so before I keep one I check how far it moves the model's output. We run the same text through the new path and through the path it replaces, and then measure how far apart their predictions for each next token are with KL divergence (llama-perplexity --kl-divergence, over 4,088 scored tokens). We also count how often the two paths put the same token first, since that's the token greedy decoding would write.

The bar is the 8-bit query rounding itself, a mean KL of 0.00150 and the same first token at least 98.3% of the time. A change at or under the bar adds no more error than that rounding. Three faster changes went over it and stayed out of the defaults:

  • Computing P·V in 8-bit floating point (FP8). GeForce Blackwell runs FP8 matrix math at twice the rate of 16-bit, so we moved half of the attention kernel to it, and prompt reading at 131,072 tokens rose by about 6%. The KL was 0.00162, just over the bar, so I left it out.
  • Sending batches of 7 or 8 rows to the tiled tensor-core kernel (MMQ). This sped up seven- and eight-row batches by about a quarter, but its KL was 0.00204, with the same first token 98.1% of the time.
  • Drafting three tokens a round, from chapter 3, at a KL of 0.0028.

The chunked Gated DeltaNet prefill from commit b120f63, which reads prompts many tokens at a time on the tensor cores, scored 0.00148, under the bar, so it ships.

The tensor-core weight kernel against a CPU reference

Main's tensor-core weight kernel from chapter 5 also changes the numbers, so I put it through the same test against the CUDA-core kernel it replaces, at the three batch sizes the server uses most, 1, 3 and 8 tokens. First I ran the CUDA-core kernel against itself to find the noise floor, and it was zero, with a mean KL under 0.000001 at every batch size. Against the CUDA-core kernel, the tensor-core kernel scored 0.0020, 0.0017 and 0.0015 at 1, 3 and 8 tokens, which is over the bar at the two smaller batches.

That result alone would say the tensor-core kernel is less accurate, so I ran both GPU kernels against a CPU reference that uses the same PQ2_0 weights. If the tensor-core kernel had lost accuracy, it would sit further from the CPU than the CUDA-core kernel does. Instead the two GPU kernels sat about equally far from the CPU, between 0.0020 and 0.0028, and at 3 and 8 tokens the tensor-core kernel came out slightly closer at one batch size and slightly further at the other:

Both kernels do exact integer dot products and add them up in 32-bit floats, and they differ in how they group the scales and in what order they add the partial sums. Those two differences move each GPU kernel away from the CPU by about the same amount, which is why the bar, measured between the two GPU kernels, reads as a gap that neither kernel owns. Perplexity on wikitext-2, eight chunks, tells the same story, since the two kernels differ by less than 0.02 and the sign flips from one batch size to the next. I also tried scaling each chunk of the sum separately to close the gap, and it didn't help, so I left it out.

Against a CPU reference, the tensor-core and CUDA-core weight kernels scored KLs between 0.0020 and 0.0028, and their wikitext-2 perplexity differed by less than 0.02.

7

Keeping long conversations fast and accurate

The 131,072-token numbers in this article come from a chain of changes that each target long conversations, and the first one is about accuracy.

The recurrent state in 16 bits

Each Gated DeltaNet layer carries its recurrent state from token to token, so any rounding in how the engine stores that state adds up over a long conversation. Commit a941add lets the server store the state in a narrower type with --cache-type-s (-cts), and the update math stays in 32 bits inside the kernel, so only the stored copy gets rounded. Commit 48ebd21 fixed a crash that hit the f16 and bf16 types under a draft head, which made f16 usable for serving.

To pick a type, we measured drift at 131,072 tokens of context over 8,192 decoded tokens, against a reference with a 32-bit state. That run was on an RTX 5090, the only measurement in this article from another card. A q8_0 state reached a KL of 0.001839 and was still climbing, bf16 reached 0.000292, and f16 reached 0.000108, so an f16 state drifts about 17 times less than q8_0, for 34 MiB more per state copy. Every speed and memory number in this article ran with a q8_0 state, and so does the serve command in chapter 11. I'd pick f16 for very long conversations where drift counts, but I haven't benchmarked it on the RTX 5080, so I don't have its speed or memory there.

At 131,072 tokens of context, over 8,192 decoded tokens on an RTX 5090, an f16 recurrent state drifted to a KL of 0.000108 against 0.001839 for q8_0.

Faster reads of a long cache

Speed at long context starts with the 4-bit attention kernel from chapter 2 (60feea0), which took decode at 131,072 tokens from 44.5 to 73.3 tokens per second on the RTX 5080 by reading the q4_0 cache in place. Commit cbe42cc then helps when several conversations share one cache. With --kv-unified, one buffer holds every conversation's tokens, so a conversation that decodes while another one holds a long context used to read the other one's tokens too, tile by tile, and throw them away because the mask hid them. The mask pre-pass now finds the first and last tiles each query can see, from both ends, so decode and draft checks skip the hidden tiles. When nothing is hidden, the pre-pass costs about 4 microseconds per attention layer.

The draft head stays useful at long context too, because commit 514c53c gives it the same token rows the main model sees. At 131,072 tokens the model still accepted 64.6% of main's drafts, about what it accepted with a short conversation, and the head added 81% there (chapter 3).

Why the draft head ships inside the model file

We tried DFlash2 first. It matched MTP's speedup, but it needed about ten times the memory and didn't fit beside a long context on a 16 GB card, so we ship the MTP head built into the model file.

8

Memory: what a 131,072-token conversation uses

Because the weights use less than half the card, the rest of it can hold conversations. In the served runs, with one conversation, a 139,264-token window, the draft head on and 131,072 tokens already in it, main peaked at 10,496 MiB, about 10.5 GB. That leaves room on a 16 GB card, and it fits a 12 GB card. c008fe8 peaked at 10,428 MiB in the same test, and drafting three tokens a round in place of two added 46 MiB.

Three of the fork's changes save memory more than time:

  • A narrower recurrent state (-cts, commit a941add). The draft head needs the engine to undo the guesses the model rejects. The engine can cut a KV cache back to an earlier token, but it can't cut back a recurrent state, so it keeps snapshots of the state to roll back to, and every snapshot adds another copy of the state to memory. On the RTX 5080, with four conversations and two snapshots each, storing the state in q8_0 took it from 1,795.5 to 526.5 MiB, and f16 sits between the two.
  • The attention mask at one bit per cell (--attn-mask-bits, commit 9e358e6). The mask records which earlier tokens each token may see, and since each cell only says yes or no, one bit per cell is enough. At 294,912 cells by 512 tokens, the mask went from 288 to 18 MB for the main model and from 64 to 4 MB for the draft head, while the greedy tokens stayed the same and the top-5 log probabilities were bit-identical.
  • Checkpoints every N prompt tokens (--checkpoint-every). These save the engine's state at regular points in a long prompt, so when a new or compacted session starts with the same long prompt, it resumes from a checkpoint and skips reading the whole prompt again. For a coding agent that sends the same long system prompt at the start of every session, that skips most of the prefill.

With 131,072 tokens in the conversation and the draft head on, main peaked at 10,496 MiB on the RTX 5080.

9

How I measure

A change of 1 or 2% only means something if the noise between runs is smaller than that, so every number here comes from a session built to keep the noise down. The machine with the RTX 5080 also runs a desktop and other work, and that load moves decode speed by several tokens per second, so my benchmark script waits for a quiet machine. It checks the 1-minute load every 10 seconds, starts only after the load stays under 6 for two minutes, and logs the load during the run.

Within a quiet session I alternate builds, A-B-A, so that any slow drift in the card's speed lands on both sides. I also log the card's clocks, temperature and throttle reasons. Under load the card ran at about 2,900 MHz with its memory at 14,800 to 15,000 MHz, it reached at most 79 °C, and the only throttle reason under load was the normal software power cap, with no thermal or hardware slowdown. In the served runs the standard deviation across the three runs of each build stayed under 0.2 tokens per second, and in llama-bench it stayed under 0.7 for main's five repetitions.

Across three served runs of each build, the standard deviation stayed under 0.2 tokens per second, against gains of 33 to 36%.

The single runs are the exceptions, and I mark them where they appear: main without the draft head, and main with three drafted tokens. One more detail from the served runs is that main's prose answer at 131,072 tokens stopped at 491 tokens, where the others ran to 512.

10

Use it with Claude Code

The fork's server speaks both the OpenAI chat API and the Anthropic Messages API (/v1/messages), so Claude Code can use it as a local model. Since the code prompt writes 170.5 tokens per second with 131,072 tokens of context, a coding agent can keep a large part of a repo in the conversation and still get replies at about that speed.

I run it through Splice, our open-source (MIT) proxy that sits between Claude Code and a model and gives each backend its own command. Splice v0.4.0 has a local profile for OpenAI-compatible servers on a loopback address, and llama-server is one of them. Splice needs Java 21 or newer and Claude Code on your PATH, and its installer checks both before it writes anything, then checks the download against the release's published checksums:

curl -fsSL https://github.com/torad-labs/splice/releases/download/v0.4.0/install.sh \
  | env SPLICE_VERSION=v0.4.0 bash

With the server from chapter 11 running, add it to Splice as a head called bonsai. The model id is the server's --alias, and the number after the colon is the context window in tokens, the same 139,264 that -c gives the server:

splice add local --name bonsai --base-url http://127.0.0.1:8080/v1 --model bonsai-2-27b:139264
claude-bonsai

Before it saves anything, splice add checks that the server answers and lists bonsai-2-27b. Then it links the claude-bonsai command and offers to restart Splice's background service, so the new head loads. **claude-bonsai starts Claude Code on the local model**, and splice doctor --live sends the server one small streamed request with one tool call, so you see tool calling work before a coding session depends on it.

To run Claude Code without Splice, point it straight at the server:

export ANTHROPIC_BASE_URL=http://127.0.0.1:8080
export ANTHROPIC_AUTH_TOKEN=local
export ANTHROPIC_MODEL=bonsai-2-27b
export ANTHROPIC_SMALL_FAST_MODEL=bonsai-2-27b
claude

Any tool that speaks the OpenAI API can use http://127.0.0.1:8080/v1 with the model name bonsai-2-27b in the same way.

11

How to build and run the fork

Everything here is in the fork's repo at docs/torad/bonsai-27b, including the benchmark scripts, a serve command and the chat template. You need an NVIDIA GPU with at least 12 GB of memory for the serve command below, which holds one conversation of up to 139,264 tokens.

The fork runs on any NVIDIA GPU from Ampere on, but I tuned it and measured every speed in this article on sm_120 (GeForce and RTX PRO Blackwell), and some of its changes only run on newer chips. The tensor-core kernel that multiplies the PQ2_0 weights during decode, with its TMA ring, its evict_first hint and its L2 prefetch, runs only on sm_120, so every other NVIDIA card, including B200 (sm_100) and GB10 (sm_121), uses the slower CUDA-core PQ2_0 kernel. The paced L2 issuer and PDL run on sm_90 and newer, which covers Hopper, B200, the RTX 50-series and GB10, while older cards run their kernels in program order and each kernel fetches its own weights. The 4-bit attention kernel and the chunked Gated DeltaNet prefill run on sm_80 and newer, which covers Ampere, Ada, Hopper, B200, the RTX 50-series and DGX Spark GB10, and the remaining changes, such as the draft head's token lookup and the narrower recurrent state, run on every card. That's why the model works on all of those cards but writes slower than the numbers here outside sm_120. The numbers in this article come from main at bdb47f8, and the later commits on main change only comments and docs. The hardware support table in the fork's README lists each change, where it runs, and what runs in its place elsewhere.

Every change in the fork has an environment switch that turns it off, so you can measure each one on your own card.

To have a coding agent do the setup, paste this prompt into Claude Code, Cursor or any agent that can run shell commands on the machine with the card:

Set up and benchmark Ternary Bonsai 2 27B on this machine with the
torad-labs llama.cpp fork. Use only the commands and flags below. Do not add
or change flags. Stop and tell me if a check fails.

1. Check the hardware and tools.
   - Run: nvidia-smi --query-gpu=name,memory.total,compute_cap --format=csv
     Tell me the GPU name, its memory and its compute capability. I need an
     NVIDIA GPU with 12 GB of memory or more for step 5. The fork runs on
     any NVIDIA GPU from Ampere on (compute capability 8.0 or newer), but
     the PQ2_0 tensor-core decode path, and the published numbers, are
     RTX 50-series and RTX PRO Blackwell only (sm_120, compute capability
     12.0). On any other card from Ampere on, expect it to work but run
     slower than those numbers, and keep going. If the compute capability
     is below 8.0, stop and tell me.
   - Run: nvcc --version. I need CUDA 12.8 or newer.
   - Make sure git, cmake, ninja, sha256sum and the Hugging Face CLI (hf)
     are installed. If hf is missing: pip install -U huggingface_hub
   - The model file is about 7.7 GB. Make sure there is room on disk.

2. Clone main at the measured commit and build it. The build targets the GPU in this machine, so run
   it here, with CMake 3.24 or newer.
git clone https://github.com/torad-labs/llama.cpp && cd llama.cpp && git checkout bdb47f8
cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release \
  -DGGML_CUDA=ON -DGGML_NATIVE=ON -DCMAKE_CUDA_ARCHITECTURES=120a \
  -DGGML_CUDA_FA=ON -DGGML_CUDA_FA_ALL_QUANTS=OFF -DGGML_CUDA_GRAPHS=ON \
  -DLLAMA_BUILD_SERVER=ON -DLLAMA_BUILD_TOOLS=ON -DLLAMA_BUILD_EXAMPLES=OFF -DLLAMA_BUILD_TESTS=OFF
cmake --build build --target llama-server llama-bench llama-perplexity -j
   If the compute capability is 12.0, add -DCMAKE_CUDA_ARCHITECTURES=120a
   to the first cmake line, as the measured build did.

3. Download the model with its MTP draft head (one file: PrismML's weights
   plus the 8-bit draft head, Apache-2.0) and verify the checksum.
hf download ProCreations/Ternary-Bonsai-2-27B-MTP \
  Ternary-Bonsai-2-27B-PQ2_0-MTP-Q8_0.gguf \
  --revision efffdea64c1f9e93cc7fa6bb24f72ae9d66ecf51 --local-dir .
echo "3cb3f0056d2e34ee44245a64396004a21f8492573d6ce1266ec4b7222c131dd4  ./Ternary-Bonsai-2-27B-PQ2_0-MTP-Q8_0.gguf" | sha256sum -c

4. Quick speed check before starting the server (no draft head, empty
   context). Report pp512 and tg128 in tok/s. On an RTX 5080 at its stock
   power limit, main's published numbers are about 2,720 and 110.9.
build/bin/llama-bench -m ./Ternary-Bonsai-2-27B-PQ2_0-MTP-Q8_0.gguf \
  -ngl 99 -fa 1 -ctk q4_0 -ctv q4_0 -p 512 -n 128 -r 5

5. Start the server (one conversation, draft head on) and leave it running.
   This is the configuration behind the published numbers, and it peaked
   at 10,496 MiB on the RTX 5080, so it fits a 12 GB card.
build/bin/llama-server -m ./Ternary-Bonsai-2-27B-PQ2_0-MTP-Q8_0.gguf \
  --alias bonsai-2-27b -ngl 99 -fa on -c 139264 -np 1 --jinja \
  --cache-type-k q4_0 --cache-type-v q4_0 -cts q8_0 --attn-mask-bits \
  --chat-template-file docs/torad/bonsai-27b/chat-template.jinja \
  --reasoning-format deepseek --temp 1.0 --top-p 0.95 --top-k 20 \
  --spec-type draft-mtp --spec-draft-n-max 2 -ctkd q4_0 -ctvd q4_0 \
  --host 127.0.0.1 --port 8080
   When it is up, curl http://127.0.0.1:8080/health, then send one short
   chat request to http://127.0.0.1:8080/v1/chat/completions with model
   "bonsai-2-27b" and show me the reply. With the draft head on, an RTX
   5080 writes about 200 tok/s on a short request.

6. Optional: the full depth table. It takes a while. Stop the server first
   to free the card.
build/bin/llama-bench -m ./Ternary-Bonsai-2-27B-PQ2_0-MTP-Q8_0.gguf \
  -ngl 99 -fa 1 -ctk q4_0 -ctv q4_0 -p 512 -n 128 -d 0,32768,131072 -r 5
   Published main numbers on an RTX 5080: tg128 110.9, 103.2 and 83.8.

Finish with a short summary: GPU, CUDA version, build result, checksum
result, the tok/s numbers, and whether the server answered.

To do it by hand, follow three steps. First, build the fork from source (CUDA 12.8 or newer) on the machine with the card, because the build targets the GPU it finds there:

git clone https://github.com/torad-labs/llama.cpp && cd llama.cpp && git checkout bdb47f8
cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release \
  -DGGML_CUDA=ON -DGGML_NATIVE=ON -DCMAKE_CUDA_ARCHITECTURES=120a \
  -DGGML_CUDA_FA=ON -DGGML_CUDA_FA_ALL_QUANTS=OFF -DGGML_CUDA_GRAPHS=ON \
  -DLLAMA_BUILD_SERVER=ON -DLLAMA_BUILD_TOOLS=ON -DLLAMA_BUILD_EXAMPLES=OFF -DLLAMA_BUILD_TESTS=OFF
cmake --build build --target llama-server llama-bench llama-perplexity -j

These are the flags of the build I measured, at bdb47f8 with CUDA 13.3. -DCMAKE_CUDA_ARCHITECTURES=120a is there because the PQ2_0 tensor-core kernel only launches when the binary includes an sm_120 image. On any other card, drop it, and with CMake 3.24 or newer -DGGML_NATIVE=ON builds for the GPUs in the machine you build on, or set CMAKE_CUDA_ARCHITECTURES to your card's architecture. The README lists the value for each card.

Next, fetch the model file from Hugging Face (Apache-2.0) with the [hf command][hf-cli] and check it against its checksum. The file holds PrismML's weights byte for byte plus the 8-bit draft head, and it's the file I measured every number in this article with.

hf download ProCreations/Ternary-Bonsai-2-27B-MTP Ternary-Bonsai-2-27B-PQ2_0-MTP-Q8_0.gguf \
  --revision efffdea64c1f9e93cc7fa6bb24f72ae9d66ecf51 --local-dir .
echo "3cb3f0056d2e34ee44245a64396004a21f8492573d6ce1266ec4b7222c131dd4  ./Ternary-Bonsai-2-27B-PQ2_0-MTP-Q8_0.gguf" | sha256sum -c

Then serve it. This is the configuration every served number in this article ran with, one conversation in a 139,264-token window, and it peaked at 10,496 MiB on the RTX 5080, so it fits a 12 GB card:

build/bin/llama-server -m ./Ternary-Bonsai-2-27B-PQ2_0-MTP-Q8_0.gguf --alias bonsai-2-27b \
  -ngl 99 -fa on -c 139264 -np 1 --jinja \
  --cache-type-k q4_0 --cache-type-v q4_0 -cts q8_0 --attn-mask-bits \
  --chat-template-file docs/torad/bonsai-27b/chat-template.jinja --reasoning-format deepseek \
  --temp 1.0 --top-p 0.95 --top-k 20 \
  --spec-type draft-mtp --spec-draft-n-max 2 -ctkd q4_0 -ctvd q4_0 \
  --host 127.0.0.1 --port 8080

For lower drift in very long conversations, swap -cts q8_0 for -cts f16 (chapter 7). It costs 34 MiB more per state copy, and I haven't benchmarked it on the RTX 5080, so the speed and memory numbers here are for q8_0. The fork's Bonsai page also has a four-conversation command with --kv-unified, --checkpoint-every and --cache-ram, which I didn't measure for this article.

Reproduce the numbers

To check my numbers against yours, use the same commands I ran. The runs pinned the card with CUDA_DEVICE_ORDER=PCI_BUS_ID CUDA_VISIBLE_DEVICES=0. The llama-bench table in chapter 2 is three legs (c008fe8, main, c008fe8) of this command:

CUDA_DEVICE_ORDER=PCI_BUS_ID CUDA_VISIBLE_DEVICES=0 \
build/bin/llama-bench -m ./Ternary-Bonsai-2-27B-PQ2_0-MTP-Q8_0.gguf -ngl 99 -fa 1 -ctk q4_0 -ctv q4_0 \
  -p 512 -n 128 -d 0,32768,131072 -r 5 -o csv

The served tables used the serve command above, with -c 65536 in place of -c 139264 for the short conversation, and with --spec-type draft-mtp --spec-draft-n-max 2 -ctkd q4_0 -ctvd q4_0 left out for the runs without the draft head, or --spec-draft-n-max 3 for three drafted tokens. Each request to /v1/chat/completions asked for max_tokens 512 with temperature 0, top_k 1 and seed 1, which overrides the server's sampling flags so every run writes the same greedy tokens. Speeds are the server's own predicted_per_second timing. docs/torad/bonsai-27b/bench-server.sh reads the same timings from a running server.

The KL numbers in chapter 6 came from llama-perplexity, built with --target llama-perplexity, over 8 chunks of 1,024 tokens:

build/bin/llama-perplexity -m ./Ternary-Bonsai-2-27B-PQ2_0-MTP-Q8_0.gguf -c 1024 -b 1024 --chunks 8 \
  -fa on -ctk q4_0 -ctv q4_0 -cts q8_0 --attn-mask-bits -ngl 99 -ub 1 -f text.txt \
  --kl-divergence-base base.kld

Run it once to write the base file (-ngl 0 --threads 4 for the CPU reference), then again with --kl-divergence and the -ub batch size under test (1, 3 or 8). GGML_CUDA_PQ2_MMA_LEGACY=1 selects the CUDA-core weight kernel. Then turn off one change at a time with the switches in chapter 5 to see what each does on your card. If your numbers differ from mine, I'd like to see them.

Closing

Next steps

  1. Improve the draft head's guesses. The model accepts from 48% of them on prose to 88% on arithmetic, and on this model every accepted guess is a token that costs no extra read of the weights, so better guesses turn directly into speed.
  2. Benchmark the f16 recurrent state on the RTX 5080, for its speed and memory next to q8_0.