Abstract
Existing interpretability methods for Large Language Models (LLMs) often fall short by focusing on linear directions or isolated features, overlooking the high-dimensional, nonlinear, and relational geometry within model representations. This study focuses on how adversarial inputs systematically affect the internal representation spaces of LLMs, a topic which remains poorly understood. We propose persistent homology (PH), a tool from topological data analysis, as a principled framework to characterize the multi-scale dynamics within LLM activations. Using PH, we systematically analyze six state-of-the-art models under two distinct adversarial conditions, indirect prompt injection and backdoor fine-tuning, and identify a consistent topological signature of adversarial influence. Across architectures and model sizes, adversarial inputs induce "topological compression", where the latent space becomes structurally simpler, collapsing from varied, compact, small-scale features into fewer, dominant, and more dispersed large-scale ones. This topological signature is statistically robust across layers, highly discriminative, and provides interpretable insights into how adversarial effects emerge and propagate. By quantifying the shape of activations and neuronal information flow, our architecture-agnostic framework reveals fundamental invariants of representational change, offering a complementary perspective to existing interpretability methods.
Introduction
As Large Language Models (LLMs) are increasingly deployed in security-critical applications, understanding their vulnerability to adversarial attacks—such as prompt injections and backdoors—is paramount. Traditional interpretability techniques often rely on linear probes or Sparse Autoencoders (SAEs) to identify specific features. However, these methods assume a linear structure that may not capture the complex, relational geometry of high-dimensional latent spaces. This paper introduces a topological approach to characterize adversarial influence, moving beyond individual features to analyze the global "shape" of model activations.
Methodology: Persistent Homology
To capture the multi-scale geometric structure of LLM activations, we employ Persistent Homology (PH), a technique from topological data analysis. Unlike dimensionality reduction techniques like t-SNE or UMAP, PH preserves the relational geometry of data points in their original high-dimensional space.
The core of our method is the Vietoris-Rips filtration. We treat the latent representations of a prompt's final token as a point cloud. We then connect points based on an expanding distance threshold, $\epsilon$. As $\epsilon$ increases, topological features such as connected components (0-dimensional) and loops (1-dimensional) are formed ("born") and eventually filled in ("die").
The resulting persistence barcodes provide a summary of the data's shape. We extract 41 statistical features from these barcodes—such as mean birth/death times and persistent entropy—to serve as inputs for downstream classification and interpretability tasks.
The Signature of Topological Compression
Our analysis across six state-of-the-art models (including LLaMA3, Mistral, and Phi-3) reveals a universal phenomenon we term "topological compression."
Under normal conditions, LLM latent spaces are characterized by a diverse array of compact, small-scale topological structures. In contrast, adversarial inputs—whether from indirect prompt injections or fine-tuned backdoors—cause these structures to collapse. The latent space becomes structurally simpler, dominated by a few large-scale, dispersed features.
This divergence typically intensifies in the middle-to-late layers of the network. While clean inputs maintain a level of complexity that simplifies slightly toward the output, poisoned inputs often start with simpler structures that sharply increase in topological scale as the network reallocates its capacity to process malicious instructions.
Key Results and Robustness
The topological features extracted from barcodes prove to be exceptionally powerful discriminators. A simple logistic regression model trained on these 41 features achieved a 1.00 AUC-ROC in distinguishing clean from poisoned states across various models and layers.
Furthermore, SHAP value analysis indicates that the most predictive features are the mean death time of connected components and the number of 1-dimensional loops. This confirms the "compression" hypothesis: clean inputs are characterized by many small loops and compact clusters, while poisoned inputs result in fewer, larger, and more dispersed structures.
Crucially, this topological signature remains robust against adaptive attacks (tested via the LLMail-Inject dataset) specifically designed to evade activation-based defenses. This suggests that topological compression is a fundamental invariant of how adversarial influence manifests in LLMs, rather than a surface-level artifact.
Conclusion
By shifting the focus from individual features to the global geometry of latent spaces, persistent homology provides a powerful, architecture-agnostic framework for LLM interpretability and security. The discovery of topological compression offers a new lens through which to view adversarial influence, providing both a highly accurate detection mechanism and a deeper understanding of how malicious inputs reshape the internal logic of Large Language Models.
Summary updated