Article · Part 2

Qwen3.8-27B on M4 Max. How I pushed prefill past 400 tok/s

Qwen3.8-27B on a 36 GB Mac. Cached continuations reached 415.5 tok/s prefill with context up to 240k, plus a separate KV cache for short requests.

In the first part, I got a long Qwen 27B context to fit in 36 GB of memory on an M4 Max Mac and set up cache recovery from checkpoints. Around 200,000 tokens, prefill held at about 350 tok/s. Sometimes a simple “Hello” was enough for the chat to read the whole history again on the next turn.

I grew the conversation again and got 415.5 tok/s at 200k, 411.8 at 230k, and 407.3 near 240k on direct continuations with cached history. That is prefill, the work of processing the input before the model answers. I tested the history reset separately. In a roughly 58k-token conversation, the chat processed just 31–33 new tokens, even with a short request queued ahead of it.

What I measured

I used an M4 Max with 32 GPU cores and 36 GB of memory. The first article used a Bare Speed build for reproduction. For this series I ran a local Qwen3.8-27B Uncensored variant with Q4 weights, a Q8 KV cache and DFlash2. The server was still Kapitsa, with a local patch. The baseline below is the previous profile of the same model and engine on the same Mac. The Kapitsa project is available on GitHub.

First I ran one growing conversation from 10k to 250k in 10k steps and added a 256k point. At each step I sent a new batch of material and a separate work question. Generation used temperature 0 with thinking disabled, and answers were 381–489 tokens long. I calculated prefill speed by dividing the tokens actually processed, the full input minus cache hits, by the server's prefill time. I measured generation speed after the first token. I recorded time to first token separately because it can include time in the queue.

With the old profile, direct continuations at selected points around 200–240k reached 349–353 tok/s. Checkpoint restoration sometimes caused dips, including 296.5 tok/s at 220k. At 250k and 256k, two continuations with cache restoration reached 171.3 and 158.4 tok/s. A separate cold request of 255,999 tokens reached 196.0 tok/s. Its first token arrived after 21 minutes and 46 seconds.

Those results led me to choose a working window of 240,000 tokens for input and output combined. I leave room for the answer near the upper limit. The final measurement there had 239,467 input tokens.

How I sped up prefill for a long continuation

I started with attention computation. In the Metal kernel, I added a separate buffer for exchanging intermediate scores between thread groups. This removed two synchronizations per block. For a profile with known attention intervals, the kernel computes the mask from their boundaries, including cases where part of the tail is restored with dense attention. That saves work and memory otherwise needed to prepare a separate bit mask. Given the same set of allowed positions, the kernel's results matched the control bit for bit in all 159 synthetic cases in the final GPU check.

On a full request, the new kernel and larger chunks gave me a modest speedup. I then tested how many old positions needed to be examined while preparing a new batch. In the selected profile, the fast mode activates with a cached prefix of at least 180,000 tokens and a batch of at least 8,192 new tokens. The global attention portion for that batch is limited to the first 4,096 positions, and access to the recent part of the old history starts with a window of up to 2,048 positions. Within the new batch, the previous recent-window rule still applies. The final 128 input positions use dense attention.

All positions remain in the Q8 KV cache. Generation uses dense attention over the available cache. The representations of new tokens are nevertheless computed with the changed mask, so I evaluated that stage for both speed and answers.

On the test bench, I restored the same 190,004-token prefix and added roughly 10k tokens. Each mode had two runs. The question required a JSON response with nine exact values from different parts of the old archive and the new batch.

Scroll sideways to see the whole table.

Mode at approximately 200k input, after restoration Median prefill Facts found in each run
Previous profile 349.0 tok/s 9 of 9
New kernel and larger chunk 361.5 tok/s 9 of 9
Final profile with changed mask 407.3 tok/s 9 of 9

In the bench run near 240k, the engine released Neural Engine buffers to grow the KV cache. In the log this coincided with speed falling to approximately 206.5 tok/s on a direct continuation. In the working build, I reduced the maximum chunk size from 4,096 to 3,584 positions and raised the engine's internal budget from 30 to 31 GB. A batch of about 10k tokens fit into three chunks. In the final requests, the Neural Engine continued to do its share of prefill, including both runs near the upper limit.

Prefill benchmarks with context up to 240k

On the working server, I grew the history again and sent a batch of about 10k new tokens at each target depth. The “Before” column contains direct-continuation measurements from the initial series at similar depths. The bench table above is a paired comparison using the same prefix.

Scroll sideways to see the whole table.

Input in final test Before, tok/s After, tok/s Cached Reprocessed Prefill time
200,001 352.9 415.5 190,004 9,997 24.06 s
229,984 348.7 411.8 219,987 9,997 24.28 s
239,467 352.4 407.3 230,076 9,391 23.06 s

At 200k, that is an increase of about 18% over the previous measurement. I then changed the branch of the last request twice near the upper limit. Both times the server restored from a checkpoint at 230,076 tokens and reached 393.3 and 393.9 tok/s. All three results above 400 tok/s in the table came from direct continuations. Checkpoint restoration reached 406.5 tok/s in an intermediate API request at 220k and 400.4–400.6 tok/s at 230k on the test bench.

The model returned 9 of 9 target facts in all five final API requests, including the two restoration runs. This quality check covered retrieval of specific values from a long context.

I also tested generation separately. With one experimental profile at about 65k input, the model generated code at 72.6 tok/s and Russian text at 30.4 tok/s. These were separate requests, and speed was measured after the first token. In the original long conversation, Russian generation reached 28.9 tok/s at 100k, 25.1 at 150k, and 24.6 at 200k. The goal of 35 tok/s in Russian with a long context remains open.

Why even “Hello” could trigger reprocessing

After speeding things up, I tested an ordinary conversation. I said hello and then asked the model to tell me about itself. According to my API log notes, a separate request with 230 input tokens appeared between those two messages and finished at the 64-token limit. The server had one cache slot. The short request took it, and the main continuation at 58,554 tokens arrived without its history in memory.

The checkpoints belonged to the history loaded in the single slot. When switching to another sequence, the server filled that slot again. I added a separate small KV slot for text requests with up to 2,048 input and up to 512 output tokens. It has its own history and checkpoints, while requests still run one at a time. The extra slot increased allocated memory at startup by about 0.63 GB. The main slot is configured with a 240k window.

I repeated the scenario through the same API gateway. Between main turns, I sent a short request that ran to its 64-token limit. In the second run, I sent the short request and continuation almost simultaneously to test the queue. The short requests in these two tests contained 105 and 85 input tokens respectively.

Scroll sideways to see the whole table.

Main conversation turn Input Cached New tokens Prefill time
First message 58,463 0 58,463 125.37 s
“Tell me about yourself” after short request 58,498 58,467 31 0.44 s
Next turn, short request already queued 58,577 58,544 33 0.44 s

In the second run, the first token arrived after 1.46 s, including queue time. Prefill itself took the same 0.44 s. The original history's cache survived both short requests. I took the 200–240k speed measurements before adding the separate slot and tested isolation on a history of about 58.5k tokens. According to my notes, the build with isolation also passed 198 API checks, 67 prefix-cache checks, and 756 CPU checks of mask rules.

How to repeat the check on your own server

  1. Record the model, KV quantization, window, memory limit and thinking mode. Prepare a synthetic archive with several checkable values at different depths.
  2. Grow the conversation to the desired size. Send two separate continuations, one by adding text directly and the other by changing the branch of the last question. Save input size, cache hits, prefill time, time to first token and answer accuracy.
  3. Send a short independent request with a 64-token limit between two turns. Repeat with that request queued immediately before the main continuation. Compare the number of reprocessed tokens and the restoration category in the metrics.
  4. Near the upper limit, check the engine log to make sure the Neural Engine still participates in prefill. The resulting speed also depends on which buffers the engine can keep in memory.

The acceleration and cache-isolation code belongs to a local patch from this series. The download in part one contains an earlier build. The CSV keeps each measurement series separate so you can check input sizes and recalculate speeds. Measurements and test results in CSV.