Accelerating Suffix Jailbreak attacks with Prefix-Shared KV-cache

Xinhai Wang, Shaopeng Fu, Shu Yang, Liangyu Wang, Tianhang Zheng, Di WangarXiv 2026

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.

Figure 1: Computational pipeline of iterative suffix attacks, where many candidate suffixes are repeatedly scored under the same instruc- tion prefix.

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:

  1. 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.
  2. 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.
  3. Native Autograd Support: Because PSKV is implemented using standard PyTorch tensor operations, it seamlessly maintains the gradient pathways required for white-box adversarial attacks.
Figure 1: Computational pipeline of iterative suffix attacks, where many candidate suffixes are repeatedly scored under the same instruc- tion prefix.

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.
Figure 3: Alignment strategies for batched suffix attacks. The standard method’s misaligned structure prevents prompt KV cache reuse. PSKV uses suffix-centric alignment, which enables each prompt’s KV cache to be reused across its candidate suffixes and allows suffix-side operations to be processed as dense tensor operations.

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

BibTeX
@misc{wang2026accelerating,
  author = {Xinhai Wang and Shaopeng Fu and Shu Yang and Liangyu Wang and Tianhang Zheng and Di Wang},
  title = {{Accelerating Suffix Jailbreak attacks with Prefix-Shared KV-cache}},
  year = {2026},
  eprint = {2603.13420},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2603.13420},
}
Figure