Abstract
Large Language Models (LLMs) are susceptible to adversarial "jailbreak" prompts—often gibberish-like sequences designed to bypass safety filters. This paper introduces a novel defense that detects these attacks at the token level by analyzing model perplexity alongside contextual information. Using optimization-based and probabilistic graphical model (PGM) approaches, the researchers can pinpoint malicious segments within a prompt. Notably, small models like GPT-2 (124M) are shown to be highly effective at protecting much larger models, providing a low-cost and interpretable defense mechanism.
Introduction: The Challenge of Blind Obedience
LLMs are designed to be helpful assistants, but they suffer from a "blind obedience" problem: they often process inputs without judging whether the prompt is out-of-distribution or malicious. Attackers exploit this by using discrete optimization to find specific sequences of characters—often looking like nonsense—that bypass safety filters and force the model to generate harmful content.
While existing defenses often evaluate entire sentences as single units, this paper addresses the need for fine-grained, token-level detection. The goal is to highlight specific segments of adversarial text, which reduces false positives (where a rare but legitimate word might look suspicious) and provides better interpretability for human moderators.
Methodology: Perplexity and Context
The authors propose that adversarial tokens can be identified because they exhibit high perplexity—the LLM finds them highly unpredictable compared to natural language. However, to prevent single rare words from triggering false alarms, the system integrates contextual information.
The researchers developed two primary algorithmic approaches:
- Optimization-based Detection: This uses a "fused lasso" regularization technique to balance the perplexity of individual tokens with a penalty for "jumping" between benign and adversarial labels. This encourages the identification of contiguous blocks of adversarial text.
- Probabilistic Graphical Model (PGM): This treats the status of each token as a random variable in a chain. By calculating the Bayesian posterior, the system provides a probability score for each token, offering a nuanced "heatmap" of suspicion.
Both methods are solved using Dynamic Programming, ensuring $O(n)$ time complexity for real-time deployment. The algorithms also include specific handling for the first token of a sentence, which typically exhibits high perplexity due to a lack of preceding context, thereby preventing false alarms at the start of queries.
Experimental Results
The methods were evaluated using 107 adversarial prompts generated by the GCG (Greedy Coordinate Gradient) algorithm and a variety of foundation models, including the GPT-2 family and Llama-2.
Performance and Interpretability
Both the Optimization and PGM methods achieved perfect scores (1.0) in Precision, Recall, and F1-score for identifying whether a sentence contained an attack. In pinpointing the exact location of attacks, the PGM-based method achieved a Token F1-score of approximately 0.94 and an Intersection over Union (IoU) of 0.88.
The "Small Protector" Hypothesis
A significant finding is that massive models are not required for detection. GPT-2 Small (124M parameters) proved highly effective at detecting adversarial prompts intended for much larger models like Llama-2. This allows for low-cost, CPU-friendly defense deployment requiring less than 1GB of memory.
Discussion and Future Work
While highly effective against current "gibberish" attacks, the method relies on the assumption that adversarial prompts look like high-perplexity text. If attackers develop "low-perplexity" or "natural-looking" adversarial prompts, this defense may need adaptation. Additionally, the performance depends on hyperparameters ($\lambda$ and $\mu$) that balance perplexity and context, which may vary across different text distributions. Future work will focus on evolving these defenses alongside more sophisticated attack vectors.
Summary updated