Running Inkling-2-Mini with 500k context on a 16 GB GPU JetBrains has demonstrated running Thinking Machines' Inkling-2-Mini, a 26.4B-parameter Mixture-of-Experts model with 3.4B active parameters, at a 500K-token context window on a 16 GB GPU and 16 GB of system RAM without significant quality or performance penalty, using the Unsloth UD-Q3_K_XL quantization that occupies about 12.7 GiB of VRAM. On an RTX 4090, JetBrains measured 4,067/106 tok/s (prefill/decode) at 64K context and 2,679/76 tok/s at 384K context. The approach, available as an early preview in Junie Local, streams per-layer KV cache and weights from RAM during prefill, exploiting the roughly 350 ms per forward pass and 7 ms per layer at 6K tok/s prefill to transfer up to 500 MiB over PCIe 5.0. Agentic AI https://blog.jetbrains.com/junie/category/agentic-ai/ AI https://blog.jetbrains.com/junie/category/ai/ Coding https://blog.jetbrains.com/junie/category/coding/ JetBrains AI https://blog.jetbrains.com/junie/category/jetbrains-ai/ Running Inkling-2-Mini with 500k context on a 16 GB GPU Recently, Thinking Machines released a new model: Inkling-2-Mini https://thinkingmachines.ai/news/inkling-2-mini/ . This is a Mixture-of-Experts model with 26.4B total parameters and 3.4B active parameters—and, surprisingly, it performs somewhere between Qwen3.6-27B and Qwen3.8-27B. We found a way to run it with a 500K-token context window on a discrete GPU with 16GB of VRAM and a system with 16 GB of RAM, without a significant quality or performance penalty. On an RTX 4090, we measured 4,067 / 106 tok/s prefill / decode at a 64K context length and 2,679 / 76 tok/s at a 384K context length. Everything we describe here is already available to try in Junie Local today as an early preview. It is not yet a finished release, but we’re sharing it now so you can test it on your own hardware and send us feedback. In this blog post, we will walk you through how we achieved this and how you can try it how-to-try . To put this into perspective, the following graph shows how much VRAM is required for different context lengths and modes. How much memory do we need for a 500k context window Let’s first enumerate everything we need to support a 500k context. First, for model quantization, we have chosen the Unsloth UD-Q3 K XL version. This quantization will occupy ~12.7 GiB of VRAM, and with this quantization, the model behaves very similarly to the Q8 version. With a 500k Q8 context, you need over 8 GiB of memory. Yes, you could use Q4 at the cost of quality, but even with Q4, you will still need over 4 GiB. And you cannot store both the model weights and the entire KV cache, even at Q4, on a 16 GiB card And we haven’t even added the drafter weights, prefill buffers, drafter KV cache, etc. Okay, we can’t fit everything into VRAM, so should we probably move something to RAM? Optimization 1: During prefill, you actually need very little VRAM Let’s discuss what happens during prefill. You have probably heard of the prefill chunk size setting, called the u-batch in llama.cpp. Essentially, it determines the maximum number of tokens processed simultaneously during prefill. This process proceeds layer by layer. At each layer, we calculate either full attention or local attention. The KV cache for full-attention layers grows with the context length, while the cache for local or sliding-window attention has a constant size. Therefore, we can leave the latter as is. During the prefill of a particular minibatch at a particular layer, you need only: - The input vectors from the previous layer small - The current layer’s weights big, but constant - For a full-attention layer, the entire KV cache for that particular layer proportional to the context size And here comes the trick: if the minibatch is relatively large in our configuration, we use 2048 , then you have plenty of time to stream the KV cache for that particular layer from RAM. In fact, you can go even further and stream the current layer’s weights from RAM as well if your model cannot fit entirely in VRAM. This trick works because, even at a prefill speed of 6K tok/s, processing one forward pass takes about 350 ms and 7 ms per layer. This is enough to transfer 500 MiB via PCIe 5.0 during layer processing. We can therefore prepare all the necessary data for the next layer while processing the previous one. However, you cannot do the same during decode, because the model quickly iterates over all the layers and spends only 200–300 µs per layer. In that time, you can load only about 10–20 MiB—nowhere near the 8 GiB of KV cache Optimization 2: Let’s stream the most important attention elements During decoding, at every full-attention layer, the attention output for the current token is computed by multiplying each value vector by its corresponding attention weight and summing the results. We ran the following experiment: we retained only the top 64 attention weights and computed the weighted sum of their corresponding value vectors. It turns out that if you run the model this way, it still works So, if by some magic we could predict the 64 most important value vectors at every generation step, we would be able to stream only those values from RAM. The problem, however, is that we need all the keys to calculate the attention weights. To sum it up, we can do the following: - store the entire Q8-quantized key cache in VRAM, - identify the 64 most important value vectors, - and stream them from RAM. This is not ideal because we would need ~4 GiB of VRAM. In addition, we would need to wait until these 64 value vectors were copied from RAM, which would hurt decoding speed. Optimization 3: K2V1 encoding for the KV cache In the previous optimization, we identified our task: find the top 64 KV entries based on their attention weights. The precise approach is to calculate this using Q8 keys, but we can cheat a bit: if we store the keys at lower precision and select the top 128 or 256 entries instead of only 64, then this larger set will most likely contain the true top 64. And here is even better news: we can fetch the Q8 keys and values from RAM for these 256 “likely top KV entries” and then calculate their exact attention weights and precise contributions to the attention output. So, we only have to store lower-precision keys in VRAM. Here comes another clever part: if we also store the values at low precision, we can compute an approximate attention output for all the KV entries that were not fetched from RAM And we know that across the long tail, the attention weights are small, so approximating these entries introduces only a limited error. In our experiments, we settled on ~2 bits per key and ~1 bit per value. However, this encoding requires additional rotation matrices shared across each group of 8K KV entries, so the effective storage cost is roughly equivalent to a 2-bit KV cache. And that is just one-quarter of the size of the Q8 KV cache Optimization 4: Use earlier layers to predict the attention weights After the previous optimization, we significantly reduced the VRAM required for the KV cache. But we still have a performance penalty: during decoding, at each full-attention layer, we first calculate a rough estimate of the attention weights based on compressed keys and then fetch the top 256 KV entries from RAM. In practice, we have to wait for these entries to arrive, which costs us performance. To fix this, we need a way to predict which KV entries will receive the most attention. Ideally, this prediction should use information from earlier layers, giving us enough time to fetch the most important KV entries from RAM while processing the preceding layers. Every layer has an input vector and an output vector that is passed to the next layer. Many LLMs use residual connections, meaning that each layer modifies the vector incrementally and its output remains similar to its input. We can use this fact to estimate the attention weights for the current layer using an input vector from an earlier layer—even from two layers earlier. And we can use these approximate attention weights to predict the top 64 positions according to their true weights. We calculate approximate attention weights based on inputs from two layers earlier and select the top 1,024 KV entries according to these approximate weights. This candidate set contains most of the entries with the top 64 true attention weights. The 1,024 selected KV entries occupy around 300 KiB per full-attention layer. And we have plenty of time to fetch these entries from RAM while processing the preceding layers. One extra detail makes this even cheaper in practice: we don’t throw away the fetched Q8 rows after a single token. Each full-attention layer keeps a small resident LRU cache in VRAM 4,096 slots, with the first four “attention sink” tokens pinned permanently . Because adjacent generation steps tend to attend to the same hot tokens—the system prompt, active files, and recent stack traces—most of the top 1,024 rows are already in VRAM. Summary of proposed optimizations The overall mechanism for offloading the KV cache to RAM works as follows: - During prefill, temporarily remove the K2V1-encoded cache from VRAM to make room for compute buffers, then upload it again from RAM at the end ~10 ms - Stream only the parts required for the current layer from RAM - Store the compressed KV cache in VRAM using K2V1 encoding - Two layers before each full-attention layer: - Compute approximate attention weights using the input vector of the current layer and the compressed KV cache in VRAM - Prefetch the top 1,024 KV entries according to these approximate attention weights - At the full-attention layer: - Compute the exact attention weights for the 1,024 prefetched entries and approximate attention weights for the remaining entries using the compressed KV cache - Use the resulting attention weights to compute the attention output for this layer Key characteristics: - VRAM needs to hold only one-quarter of the Q8 KV cache - RAM needs to hold the full Q8 KV cache, plus a temporary spike of ~2 GiB during prefill - RAM → VRAM traffic remains constant—that is, it does not increase with the context length - No significant degradation in performance or quality Benchmarks To check whether these optimizations really preserve both speed and quality in realistic coding sessions, we benchmarked our build against stock llama.cpp using the same Inkling-2-Mini model and drafter on real multi-turn Junie coding trajectories with reasoning and tool calling enabled, using the model’s recommended sampling settings . In the speed charts below, we plot three curves so you can see both our overall speedup over stock llama.cpp and the isolated impact of two-tier KV offloading: - Junie Local with KV offloading — our full build, which fits a 500K-token context window into 16 GiB of VRAM - Junie Local without KV offloading — the exact same optimized build, but with the Q8 KV cache kept entirely in VRAM - Stock llama.cpp — the upstream llama.cpp main branch Memory: 500K context in 15.5 GiB With two-tier KV offloading and our prefill buffer optimizations, total active VRAM usage remains nearly flat, increasing from 14.8 GiB at 32K to 15.2 GiB at 384K and 15.5 GiB at 500K ~3.1 KiB per token . In stock llama.cpp , memory usage grows much faster—not just because of the KV cache itself, but also because its prefill attention mask buffer and vision projector buffers consume gigabytes of VRAM. Even with a Q4 or Q8 KV cache, stock llama.cpp cannot fit a 32K context on a 16 GB card and runs out of memory beyond 128K on a 24 GB RTX 4090. See the VRAM chart at the top of the post. Speed on discrete GPUs On an RTX 4090 24 GB, Windows , stock llama.cpp Q8 KV runs out of memory beyond a 128K context, and even our optimized build without KV offloading runs out of memory beyond 256K slowing to 44.7 tok/s at 256K . With two-tier KV offloading enabled, our build continues cleanly all the way to 384K with a prefill speed of 2,679 tok/s and a decode speed of 75.7 tok/s while using ~16 GiB. Even at context lengths where the other builds still fit, KV offloading is +24% faster during decoding at 128K 102.2 vs 82.4 tok/s compared to stock llama.cpp and 1.86× faster at 256K 83.0 vs 44.7 tok/s compared to our build without offloading . This is because reading the compact K2V1 codes and a small resident Q8 cache requires moving far less data per token than scanning the full KV cache. The 2.1× faster prefill at 128K 3,658 vs 1,710 tok/s in our build with KV offloading comes from attention-kernel optimizations that could also be applied to a standard Q8 cache and are not specific to KV offloading. To compare performance at context lengths up to 384K without hitting an out-of-memory limit, we ran the same coding trajectories on an RTX PRO 6000 96 GB, Linux . Even when the full Q8 KV cache fits comfortably in VRAM, our build delivers 5.9× faster prefill performance 4,045 vs 686 tok/s and 1.45× faster decode than stock llama.cpp 88.2 vs 60.8 tok/s . It is also 1.69× faster than our own build without KV offloading 52.3 tok/s . This shows that the decoding benefit of KV offloading increases at longer context lengths. Speed on unified-memory machines Even on systems with 128 GB of unified memory—where offloading does not reduce total memory usage—reducing the amount of KV-cache data read at each decoding step still provides a significant benefit at long context lengths: - DGX Spark GB10: 3.2× faster prefill 822 vs 256 tok/s and 2.15× faster decoding 30.3 vs 14.1 tok/s at 384K compared with stock llama.cpp using a Q8 KV cache , with our build outperforming the stock build at every context length between 17K and 384K. - RTX Spark N1X: 3.1× faster prefill 706 vs 226 tok/s and 2.01× faster decoding 29.7 vs 14.8 tok/s at 384K. The higher prefill speed in KV-offloading mode comes from additional optimizations applied in that mode, not from KV offloading itself. Output fidelity KL divergence & top-1 match To check how closely the compressed two-tier cache matches the full-precision output, we compared next-token probability distributions from nine model-weight and KV-cache configurations against a reference configuration consisting of a full Q8 model with a full-precision BF16 KV cache. The nine tested configurations included symmetric Q2, Q3, Q4, and Q8 model weights paired with either two-tier KV offloading or a traditional BF16 KV cache, plus a configuration using a quantized KV cache in VRAM. We ran the comparison on 25 real coding prompts ranging from 32K to 384K tokens, evaluating 4,044 next-token distributions in total. Our 16 GB configuration Q3 model weights + two-tier KV offloading, 15.2 GiB sits right at the memory/quality knee. It matches the full Q8 BF16 reference’s top-1 token 97.8% of the time, with a KL divergence of 0.0082 . This equals the top-1 accuracy of Q3 and Q4 configurations with a full BF16 KV cache 97.8% while using 13.4–17.2 GiB less VRAM . It also outperforms standard Q4 KV quantization 0.0101 KLD, 97.2% top-1 while using 4.6 GiB less VRAM , and it avoids the sharp quality drop that comes from reducing the model weights further to Q2 0.0212 KLD, 95.9% top-1 . Just as importantly, fidelity does not degrade as the context length increases: from 32K all the way to 384K, our 15.2 GiB build closely tracks the Q3 configurations with full Q8 and BF16 KV caches. Top-1 agreement remains nearly flat, changing only from 98.2% to 97.9% , while KL divergence increases only from 0.005 to 0.008 at 384K. Our build also outperforms standard Q4 KV quantization at every tested context length at 256K, Q4 KV is ahead in top-1 agreement by just two tokens out of 842 — 98.2% vs 98.0% — and all the differences occur at near-tie positions where the reference model itself is unsure which of two tokens to choose. Coding benchmarks SWE-bench & Terminal-Bench 2.1 Finally, we ran end-to-end coding evaluations to determine whether two-tier KV offloading causes any real-world degradation compared to stock llama.cpp running the exact same model weights with a full BF16 KV cache: - JetBrains Private coding benchmark 100 tasks, LOW / MEDIUM / HIGH reasoning effort : Across all three reasoning-effort settings, our 15.2 GiB two-tier KV build matches stock llama.cpp 28.6 GiB BF16 KV within normal run-to-run variance while using nearly half the VRAM. - Terminal-Bench 2.1 88 tasks, HIGH effort : Our goal here is simply to perform an apples-to-apples comparison to determine whether two-tier KV offloading hurts agent quality compared with stock llama.cpp. We ran the benchmark in a custom Junie local-workload setup with stricter timeouts designed to simulate realistic local AI sessions. Our 15.2 GiB two-tier KV build solved 44 / 88 tasks 50.0%; 51.2% of 86 scored versus 40 / 88 tasks 45.5%; 47.6% of 84 scored for stock llama.cpp with a full BF16 KV cache 28.6 GiB . This confirms parity within normal run-to-run variance while using nearly half the VRAM. How to try Important: you need to use the integrated GPU for display output and connect your monitor to the motherboard rather than the discrete GPU. This is necessary because even an idle Windows system with a browser open can use 0.5–1 GiB of VRAM. And we need all 16 GiB for the model. After that, you’re just one command away from trying it: powershell -NoProfile -Command " Environment ::SetEnvironmentVariable 'JUNIE LOCAL MODEL', '1', 'Process' ; iex irm 'https://junie.jetbrains.com/install.ps1' " This command will install Junie with the Inkling-2-Mini model on your device. By default, we set the context length to 150K, but you can change it in /settings → Change Local Model Settings. And once again, this is still a preview, so please try it and, if you encounter any problems, report them here https://youtrack.jetbrains.com/issues/JUNIE . F.A.Q. Which models can this optimization be applied to? We have tested and confirmed that it works with Qwen3.8-27B, Inkling-2-Mini, and SenseNova-Loop-9B-AWQ. Many other models have similar structural characteristics, so this approach should be applicable to them as well, but each model needs to be tested individually. Why is it still in preview? Currently, we are using a custom build https://github.com/JetBrains-Hardware/llama.cpp/tree/inkling2-mini-500k of llama.cpp with ~100 commits applied on top of its main branch. It will take some time to stabilize these changes and, hopefully, merge them into the llama.cpp main branch. Also, the Inkling-2-Mini model was only recently released, and new performance optimizations are being added to the main branch every hour. The point is that, at such a rapid pace of development, bugs in some corner cases are inevitable. What are the current limitations? The first and biggest limitation is that this is still a preview and has only been tested on Nvidia cards, so you should expect bugs. Please report them here https://youtrack.jetbrains.com/issues/JUNIE ; we will start fixing them right away. Second, we need to improve the UX in cases where Windows and the model share the same GPU. Then, there is a long list of TODOs: - Support multiple parallel requests - Reduce RAM consumption - Reduce the drafter’s memory consumption - Test and support additional models - Split the current development branch into branches that can be merged into the llama.cpp main branch - e.t.c. Do these optimizations help only with discrete GPUs? Yes and no. The main benefit of these optimizations is saving VRAM by using RAM instead. But total memory consumption is higher because we need to store not just the KV cache but also a compressed version of it. So, in terms of memory consumption, there is no benefit to using them on devices with unified memory. However, these optimizations also reduce the total amount of data read during each decoding pass. This is because we read only a fixed number of Q8 KV entries together with the complete compressed KV cache. So, in theory, this could improve decoding speed at longer context lengths on devices with unified memory as well. Are these optimizations specific to llama.cpp? No, they are not. We hope that the community will implement these optimizations in other inference engines as well. Can these optimizations help deploy huge models on server hardware? It depends on the model, traffic profile, and specific hardware. But at first glance, we believe they could. Where can I read more about the implementation? You can find a detailed description of the algorithm, written with the help of an LLM, here https://github.com/JetBrains-Hardware/llama.cpp/blob/inkling2-mini-500k/docs/development/kvq-algorithm.md . Acknowledgments Without the work of the entire local AI community, none of this would have been possible. We would like to specifically acknowledge the following projects and models: - The original Inkling-2-mini https://huggingface.co/thinkingmachines/Inkling-2-Mini model by Thinking Machines - The Unsloth UD-Q3 K XL quantization https://huggingface.co/unsloth/Inkling-2-Mini-GGUF - The Ninnix96 DSpark quantization https://huggingface.co/Ninnix96/Inkling-2-Mini-DSpark-GGUF - The llama.cpp https://github.com/ggml-org/llama.cpp project and its very active https://github.com/ggml-org/llama.cpp/pulls community around it - Other inference engines, such as Strata https://github.com/Niko1221/Strata and project-maya https://github.com/mw00/project-maya , which inspired ideas for several of our optimizations. Subscribe to Junie blog updates