TL;DR
Evaluating suffix-based jailbreak attacks on Large Language Models (LLMs) is incredibly slow and memory-intensive because thousands of candidate suffixes must be repeatedly checked against the same long harmful instruction prefix. This paper introduces Prefix-Shared KV Cache (PSKV), a plug-and-play optimization that stores just one copy of the prefix key-value states and dynamically expands it layer-by-layer during inference. PSKV cuts evaluation time by roughly 40% and peak memory usage by 50% while fully preserving PyTorch autograd compatibility for gradient-based attacks.
What Problem Does This Paper Address?
Red-teaming LLMs via suffix jailbreak attacks involves finding an adversarial string of tokens (a suffix) that, when appended to a harmful prompt, tricks the model into bypassing its safety guardrails. Optimization-based attacks (like GCG or AutoDAN) do this by iteratively evaluating huge batches of candidate suffixes against a target LLM.
The core bottleneck is structural: the harmful instruction prefix is typically very long, while the candidate suffixes are short. In standard PyTorch implementations, the long prefix's Key-Value (KV) cache must be physically duplicated for every single candidate suffix in a batch, leading to massive memory redundancy and out-of-memory (OOM) errors. While advanced serving frameworks like vLLM or SGLang optimize KV cache reuse, they operate outside the standard PyTorch autograd graph. This makes them incompatible with white-box, gradient-based attacks that require backpropagation through the suffix tokens.
Methods
The authors propose Prefix-Shared KV Cache (PSKV), a lightweight, workload-aware framework tailored specifically for synchronous adversarial optimization loops. It introduces three core techniques:
- Layer-Wise Lazy Expansion: Instead of eagerly duplicating the prefix KV cache across the entire batch dimension at initialization, PSKV stores a single, compact copy of the prefix cache. During the forward pass of each transformer layer, it virtually broadcasts or expands this cache only for that specific layer's attention computation, immediately discarding the expanded view afterward.
- Suffix-Centric Alignment: To handle batched multi-instruction evaluations where prompts have varying lengths, PSKV left-pads instructions and right-pads target responses. This aligns all suffix tokens at the exact same column index, turning ragged tensors into dense, vectorized operations.
- Native Autograd Support: Because PSKV is implemented using standard PyTorch tensor operations, it seamlessly maintains the gradient pathways required for white-box adversarial attacks.
Datasets
The framework was evaluated using:
- HarmBench: The authors used the first 50 examples from the test set (consisting of a harmful instruction and a targeted refusal-bypassing response) to benchmark attack success rates and efficiency.
- WildJailbreak: Used specifically to evaluate how well PSKV scales when dealing with exceptionally long instruction prefixes.
Key Insights and Ideas
- Exploiting Invariance: Standard serving systems optimize for asynchronous, independent user requests. Adversarial optimization is the exact opposite: it is a synchronous inner loop where the prefix is completely invariant across thousands of candidate evaluations. Tailoring the cache architecture to this specific workload yields massive efficiency gains without complex scheduling overhead.
- Hiding Memory Costs: By using transient, per-layer workspaces for the expanded cache, the memory overhead of the prefix becomes independent of the attack batch size. The temporary expansion memory is completely masked beneath the high-water mark already set by the intermediate activation tensors needed for gradient computation.
- Architecture Matters: The relative memory savings of PSKV are more pronounced on models using Multi-Head Attention (MHA), like Llama-2, compared to Grouped-Query Attention (GQA) models, because MHA inherently stores unique KV pairs for every single attention head.
Key Results
The authors benchmarked PSKV across six distinct suffix attacks (including GCG, GCQ, AutoDAN, and BEAST) and five open-source LLMs (Vicuna-7B, Llama-2-7B, Llama-3-8B, Mistral-7B, and Qwen2.5-7B).
- Speedup: PSKV consistently achieved a 1.4× to 1.6× speedup (saving ~40% of inference time) compared to the no-cache baseline. For hierarchical genetic algorithms like AutoDAN, the speedup reached up to 1.95×.
- Memory Reduction: Peak memory usage was cut by approximately 50% compared to standard KV cache implementations, effectively preventing frequent OOM errors during large beam-width searches.
- Preserved Effectiveness: Because PSKV is a mathematically exact drop-in optimization, it does not alter the underlying attack logic or numerical precision. The Attack Success Rate (ASR) remained completely unaffected.
Limitations and Future Work
While highly effective for its intended workload, PSKV's efficiency gains scale down if the instruction prefixes are very short or if the candidate batch size is small. Additionally, in setups that use dual models (like AdvPrompter, which loads both a target LLM and an attacker LLM simultaneously), the non-KV memory footprint of the static model weights can partially dilute the relative percentage of memory saved.
Future work could explore extending this layer-wise lazy expansion strategy to broader automated red-teaming frameworks beyond purely suffix-based attacks.
Summary updated