KAUST's PSKV Slashes AI Jailbreak Testing Memory by 3.7x
A new KV cache trick cuts suffix jailbreak evaluation time by 40 percent and memory by 50 percent, without touching the attack algorithm.
ProTakeaways
- PSKV shares one KV cache for the harmful-instruction prefix across all candidate suffixes in a batch.
- Layer-wise lazy expansion materializes candidate-expanded prefix tensors only inside each attention layer, then discards them.
- Cuts inference time by ~40% and peak memory by ~50% across six attacks and five open LLMs.
- Preserves autograd, so it works for gradient-based attacks like GCG and AdvPrompter that vLLM and SGLang cannot handle.
- Total memory stays flat as search width grows from 16 to 64, enabling much wider parallel searches.
- Full paper and integration guide available on arXiv; code release planned under research-only license.
Red-teaming large language models with suffix jailbreak attacks is one of those workloads where the compute bill quietly explodes. You take a harmful instruction, append thousands of candidate adversarial suffixes, score each one, and repeat for hundreds of iterations. A team from KAUST and Zhejiang University has released Prefix-Shared KV Cache (PSKV), a plug-and-play optimization that exploits an obvious but neglected fact: every candidate in a batch shares the same prefix.
The redundant work hiding in every jailbreak run
Suffix attacks like GCG, BEAST, and AutoDAN work by mutating the current suffix to generate a batch of N candidates, concatenating them with the static prompt and target response, and feeding the batch to compute prediction logits. This cycle repeats for hundreds of epochs. The harmful instruction prefix stays identical the entire time.
The prefix also dominates the sequence. On HarmBench, the average harmful instruction is 78.4 tokens, while jailbreak suffixes are typically only 20 tokens. Without caching, the model recomputes the prefix's key-value tensors for every candidate on every step. With a standard KV cache, you compute the prefix once but then have to physically duplicate that cache across the entire batch dimension, which blows up memory and forces smaller batches.
Serving engines like vLLM and SGLang already solve prefix reuse for production traffic, but their custom runtimes operate outside the standard PyTorch autograd graph, restricting their utility to a subset of gradient-free attacks. Gradient-based attacks like GCG and AdvPrompter need autograd, which rules those systems out.
Layer-wise lazy expansion
PSKV's trick is to store exactly one compact KV cache for the shared prefix, then expand it on demand inside each transformer layer's attention computation and immediately throw the expanded view away before moving to the next layer. The candidate-expanded prefix tensors exist only as transient per-layer workspace, never as persistent state.
For batched multi-instruction evaluation, the authors add a suffix-centric alignment strategy: they left-pad instructions and right-pad target responses so that all suffix tokens start at the same column index, producing a dense suffix tensor that enables vectorized candidate substitution, gradient extraction, and scoring. Because it is all standard PyTorch ops, gradients flow through normally.
The integration is genuinely minimal. Here is the essential pattern from the appendix:
cache = initialize_prefix_cache(model, prefix_input_ids, prefix_attention_mask, cache_mode="pskv")
outputs = forward_with_cache(
model=model,
cache=cache,
suffix_input_ids=suffix_input_ids,
suffix_attention_mask=suffix_attention_mask,
cache_mode="pskv",
expand_factor=num_candidates,
)
logits = outputs["logits"]
What the numbers look like
The authors evaluated PSKV against six suffix attacks (GCG, GCQ, AutoDAN, BEAST, AmpleGCG, AdvPrompter) on five open models (Vicuna-7B, Llama-2-7B, Llama-3-8B, Mistral-7B, Qwen2.5-7B), running on two A100 GPUs against 50 HarmBench prompts.
The results break down cleanly:
- Speedups of 1.4x to 1.6x for candidate-evaluation-dominated attacks (GCG, GCQ, BEAST, AmpleGCG), rising to nearly 2x for AutoDAN because AutoDAN requires more inference steps per iteration due to its hierarchical genetic algorithm, giving PSKV more redundant computation to eliminate.
- Memory savings up to ~3.7x over Standard KV Cache on MHA models like Vicuna and Llama-2, where every attention head keeps its own KV pair. GQA models (Llama-3, Mistral, Qwen2.5) show smaller relative gains because their baseline KV footprint is already compressed.
- Standard KV Cache hits OOM on GCQ, AutoDAN, and BEAST across most models, while PSKV completes them.
- ASR is preserved within run-to-run noise. A paired 10-seed GCG test gave p=0.74, and PSKV produces bit-identical logits to Standard KV Cache under matched precision.
The scalability angle
The most interesting result is in the scaling table. On Llama-3-8B running GCG, total peak memory stays essentially constant at about 40.8 GB whether the search width is 16, 32, or 64. The peak is set by activation and gradient tensors, not the prefix cache, so widening the search is nearly free in memory. That inverts the usual tradeoff where wider beams mean smaller batches.
Against specialized inference engines, PSKV holds its own. On the gradient-free BEAST attack, PSKV runs in the same order of magnitude as specialized inference engines while retaining compatibility with PyTorch-native attack pipelines and gradient-based methods. vLLM and SGLang are still faster on pure BEAST throughput, but they cannot touch GCG or AdvPrompter.
Where it fits and where it does not
PSKV is narrowly useful and honest about it. The authors note that gains shrink when prefix lengths are short, candidate widths are low, or non-cache tensors like logits, activations, or dual-model training state dominate memory. The AdvPrompter numbers show this clearly: because it trains a second model in the loop, the speedup drops to around 1.3x since PSKV only accelerates the inference portion.
The natural use case is safety research at scale: sweeping many models, longer prefixes, wider beam searches, or WildJailbreak-style long-prompt datasets where Standard KV Cache runs out of memory entirely. The code release is planned under a research-only license with the responsible-use safeguards discussed in the paper's ethics section, framed as making defensive evaluation cheaper rather than making attacks more dangerous.