Abstract
Researchers have developed a novel "Triplet Defense" that uses contrastive representation learning to fundamentally rewire how Large Language Models (LLMs) handle harmful prompts. By training the model to cluster harmful internal states together and push them far away from benign ones, they reduced the attack success rate on Llama 3 8B from 29% to just 5% against sophisticated embedding-space attacks, all without degrading the model's general intelligence.
Introduction
Despite extensive safety alignment (like RLHF), LLMs remain vulnerable to "jailbreaks"—adversarial prompts designed to bypass safety filters. Most current defenses focus on the "input-space" (the text we type) or the "output-space" (the text the model generates). However, a more potent class of "embedding-space" attacks exists, where an attacker manipulates the internal mathematical vectors of the model directly.
Existing internal defenses, such as "circuit breakers," attempt to disrupt harmful representations but often struggle to generalize across different types of attacks. They frequently fail to create a clear enough boundary between "safe" and "unsafe" internal states, or they accidentally damage the model's ability to perform standard tasks (like math or logic) in the process of making it safe.
Methods
The authors propose a framework based on Contrastive Representation Learning (CRL), specifically utilizing a Triplet Loss formulation.
- The Triplet Loss: Instead of just telling the model "don't say this," they guide the model's internal "plumbing." For any given internal representation, they define an anchor, a positive sample (what it should look like), and a negative sample (what it should avoid).
- For Benign Prompts: The model is trained to keep its new representations close to the original, high-quality ones.
- For Harmful Prompts: The model is trained to push these representations far away from the original "useful" harmful states and cluster them into a "broken" state that generates nonsense instead of dangerous content.
- Adversarial Hard Negative Mining: To make the defense robust, the authors used a separate adversarial neural network to find "hard negatives"—harmful representations that look very similar to benign ones—and forced the model to learn the difference.
- Representation Engineering: The defense is applied to the middle-to-late layers of the model (layers 20-31 in Llama 3 8B), where the high-level "intent" of a response is typically formed.
Datasets and Evaluation
- Benign Data: UltraChat, a dataset of 1.5 million multi-turn dialogues covering art, history, and technology.
- Harmful Data: A mix of WildGuardMix (real-world jailbreaks) and WildJailbreaks (synthetic harmful prompt-response pairs).
- Evaluation Benchmarks:
- HarmBench: Used to test 80 distinct harmful behaviors.
- StrongREJECT: A fine-grained classifier used to detect even subtle safety failures.
- General Capability: MMLU, GSM8K, HellaSwag, and TruthfulQA were used to ensure the model didn't lose its reasoning skills.
Key Insights
- Relative Distance is King: In high-dimensional embedding spaces, absolute positions matter less than relative ones. By focusing on the relationship between representations (Contrastive Learning), the defense becomes much harder to bypass than simple "refusal" training.
- Circuit Breaking as a Subset: The authors mathematically prove that previous state-of-the-art defenses like "Circuit Breakers" are actually just simplified, limited versions of their Triplet Loss framework.
- Defending the "Internal Logic": This method disrupts the model's ability to even form a coherent harmful thought, causing it to "short-circuit" into gibberish when attacked.
- Generalization to OOD Formats: Because the defense works on the internal representation level, it generalizes well to "out-of-distribution" attacks, such as prompts translated into different languages or those using weird capitalization.
Key Results
- Robustness Boost: On Llama 3 8B, the Triplet Defense reduced the Attack Success Rate (ASR) of the REINFORCE-GCG input attack from 14% to 0%.
- Embedding Attack Mastery: Against direct embedding-space attacks, it outperformed all previous methods, bringing the ASR down to 5% (compared to 29% for standard circuit breakers).
- Preserving Intelligence: Unlike previous methods like RepBend, which caused the model's performance on math (GSM8K) to plummet from 75% to 49%, the Triplet Defense maintained nearly identical performance to the base model across all academic benchmarks.
- Cross-Model Success: The method was successfully validated across multiple architectures, including Llama 3 8B, Mistral 7B, Phi-3 Mini, and Zephyr 7B.
Limitations and Future Work
- Model Sensitivity: Mistral 7B remained more vulnerable than Llama 3, suggesting that some architectures might require more specific hyperparameter tuning.
- Compute Cost: Training with adversarial hard negative mining is computationally intensive.
- The "Gibberish" Problem: When the "circuit breaks," the model generates incoherent text. Future work could focus on directing the "broken" state toward a standard refusal message.
- Scalability: Testing this on 70B+ or 400B+ parameter models remains an open challenge.
Summary updated