On the Robustness of Transformers against Context Hijacking for Linear Classification
Tianle Li, Chenyang Zhang, Xingwu Chen, Yuan Cao, Difan Zou
TL;DR
This work analyzes the robustness of transformers to context hijacking by modeling in-context linear classification with multi-layer linear transformers. It shows that such transformers can implement multi-step gradient descent on context examples, and derives optimal initialization and step-size schedules that depend on the training context length $n$ and depth $L$. The results reveal that deeper models and longer training contexts yield finer optimization steps, reducing interference from hijacked context, with bounds and empirical validation supporting the theory. The findings provide theoretical justification for why deeper architectures can exhibit improved robustness to context hijacking and offer a framework transferable to other gradient-descent-based in-context learning problems.
Abstract
Transformer-based Large Language Models (LLMs) have demonstrated powerful in-context learning capabilities. However, their predictions can be disrupted by factually correct context, a phenomenon known as context hijacking, revealing a significant robustness issue. To understand this phenomenon theoretically, we explore an in-context linear classification problem based on recent advances in linear transformers. In our setup, context tokens are designed as factually correct query-answer pairs, where the queries are similar to the final query but have opposite labels. Then, we develop a general theoretical analysis on the robustness of the linear transformers, which is formulated as a function of the model depth, training context lengths, and number of hijacking context tokens. A key finding is that a well-trained deeper transformer can achieve higher robustness, which aligns with empirical observations. We show that this improvement arises because deeper layers enable more fine-grained optimization steps, effectively mitigating interference from context hijacking. This is also well supported by our numerical experiments. Our findings provide theoretical insights into the benefits of deeper architectures and contribute to enhancing the understanding of transformer architectures.
