TL;DR
Safety Representation Ranking (SRR) is a novel, lightweight framework that secures Large Language Models (LLMs) by evaluating their internal hidden states rather than their final text outputs. By generating multiple candidate responses and ranking them using a single-layer Transformer trained on intermediate layer representations, SRR significantly mitigates adversarial jailbreaks and safety risks without altering the model's decoding logic or degrading its performance on benign tasks.
What Problem Does This Paper Address?
Aligning LLMs with human safety guidelines is an ongoing game of cat-and-mouse. Current defense mechanisms generally fall into two categories, both of which have major drawbacks:
- Decoding-time interventions: These methods alter token distributions or inject safe prompts during generation. While effective at blocking harm, they often degrade the model's fluency and natural performance on benign tasks (the "alignment tax").
- Post-processing LLM judges: These methods use an external LLM to review generated text. However, text-only judges are computationally expensive, slow, and notoriously overcautious—frequently causing "over-refusal" by blocking completely harmless prompts.
The authors identify a critical gap: existing methods look only at the final textual surface, completely ignoring the rich safety signals embedded within the LLM's internal representations (hidden states).
Methods
The authors introduce Safety Representation Ranking (SRR), a listwise learning-to-rank framework that operates in two phases:
- Candidate Generation: For a given instruction, the frozen base LLM samples multiple plausible responses using stochastic decoding. To ensure the training pool contains both safe answers and realistic adversarial failures, the authors inject jailbreak prompts to generate "hard negatives."
- Lightweight Ranker Architecture: Instead of using a massive external reward model, SRR extracts hidden-state vectors from the intermediate layers (specifically the bottom 25%) of the base LLM. The final layers are skipped because they tend to overfit to the next-token prediction task. These high-dimensional vectors are projected to a lower-dimensional space and fed into a single-layer Transformer encoder (<5M parameters).
The ranker computes a contextualized cosine similarity score between the instruction and each candidate response. It is trained end-to-end using a listwise ranking loss (Kullback–Leibler divergence), which forces the model to rank safe responses higher than unsafe ones. At inference time, the highest-ranked safe response is selected.
Datasets
The framework was trained and evaluated across several diverse benchmarks:
- Safety & Jailbreaking: HarmBench, SorryBench, and JailbreakBench were used to test robustness against adversarial prompts.
- Privacy: Harmcopy was utilized to evaluate the framework's ability to detect privacy infringements.
- Fairness: The BBQ dataset (referred to as Biasedbenchmark for QA) evaluated the mitigation of social biases.
- Utility/Natural Performance: The MATH dataset (12,500 competition-level problems) verified that the defense does not harm the LLM's core reasoning capabilities.
Key Insights and Ideas
- Intermediate Layers Hold the Key: The final layers of an LLM are heavily specialized for generating the very next token, which can obscure broader semantic context. Intermediate layers provide a cleaner, more comprehensive look at the internal safety features.
- Zero Alignment Tax: Because SRR only ranks naturally generated outputs and does not alter the decoding distribution, it completely avoids the trade-off between safety and performance.
- Internal States Generalize Better: Safety signals in the latent space are foundational rather than surface-level. As a result, a ranker trained on internal features generalizes remarkably well to entirely unseen datasets and attack styles.
Key Results
SRR was tested using three popular base models: Qwen2.5-7B-Instruct, Mistral-7B-v0.3, and Vicuna-7B-v1.5.
- Superior Accuracy: Despite having fewer than 5 million parameters, SRR vastly outperformed a GPT-2 baseline reward model (which is 20 times larger). SRR achieved an average ranking accuracy of 88.10% on HarmBench, 87.90% on SorryBench, and 90.30% on JailbreakBench.
- Strong Cross-Dataset Generalization: When trained on HarmBench and evaluated on JailbreakBench, SRR maintained an impressive 86.40% average accuracy, proving it doesn't just memorize specific datasets.
- Extension to Privacy and Fairness: SRR excelled at identifying privacy violations on the Harmcopy dataset (94.28% average accuracy). However, performance on the complex, nuanced social biases of the BBQ dataset was much lower (52.52%), indicating that fairness requires more specialized latent features.
- Preserved Utility: When evaluated on the MATH dataset, the base model's accuracy remained completely unchanged (68.7% baseline vs. 68.5%–69.1% with SRR), proving that safety can be achieved without sacrificing intelligence.
Limitations and Future Work
While highly effective, SRR has a few notable limitations:
- Candidate Diversity Dependency: SRR relies on the base LLM's ability to generate at least one safe response in its candidate pool. If the model only generates harmful completions under an intense attack, SRR has nothing safe to choose from.
- Fairness Bottleneck: The moderate performance on the BBQ dataset highlights that abstract concepts like fairness and bias are harder to capture via simple internal representation ranking than explicit harms or privacy violations.
Future work will likely focus on boosting candidate generation diversity and exploring more sophisticated architectures to better capture nuanced alignment dimensions like fairness.
Summary updated